Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Alexander Grund <[email protected]>
  • Loading branch information
wichern and Flamefire committed Jun 26, 2024
1 parent c6274f5 commit 979a3a6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 70 deletions.
4 changes: 2 additions & 2 deletions extras/ai-battle/HeadlessGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ void HeadlessGame::Close()
replay_.Close();
}

void HeadlessGame::StartReplay(const bfs::path& path, unsigned random_init)
void HeadlessGame::RecordReplay(const bfs::path& path, unsigned random_init)
{
// Remove old replay
bfs::remove(path);
Expand Down Expand Up @@ -248,7 +248,7 @@ HANDLE setupStdOut()

void printConsole(const char* fmt, ...)
{
static char buffer[512];
char buffer[512];
va_list args;
va_start(args, fmt);
const int len = vsnprintf(buffer, sizeof(buffer), fmt, args);
Expand Down
6 changes: 2 additions & 4 deletions extras/ai-battle/HeadlessGame.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
#include "Replay.h"
#include "ai/AIPlayer.h"
#include "gameTypes/AIInfo.h"

#include <boost/filesystem.hpp>

#include <chrono>
#include <limits>
#include <vector>
Expand All @@ -29,13 +27,13 @@ class HeadlessGame
void Run(unsigned maxGF = std::numeric_limits<unsigned>::max());
void Close();

void StartReplay(const boost::filesystem::path& path, unsigned random_init);
void RecordReplay(const boost::filesystem::path& path, unsigned random_init);
void SaveGame(const boost::filesystem::path& path) const;

private:
void PrintState();

const boost::filesystem::path& map_;
boost::filesystem::path map_;
Game game_;
GameWorld& world_;
EventManager& em_;
Expand Down
104 changes: 40 additions & 64 deletions extras/ai-battle/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,33 @@
#include <boost/nowide/args.hpp>
#include <boost/nowide/filesystem.hpp>
#include <boost/nowide/iostream.hpp>
#include <boost/optional.hpp>
#include <boost/program_options.hpp>

#include <atomic>

namespace bnw = boost::nowide;
namespace bfs = boost::filesystem;
namespace po = boost::program_options;

std::atomic<bool> g_abort = false;

int main(int argc, char** argv)
{
bnw::nowide_filesystem();
bnw::args _(argc, argv);

boost::optional<std::string> replay_path;
boost::optional<std::string> savegame_path;
unsigned random_init = static_cast<unsigned>(std::chrono::high_resolution_clock::now().time_since_epoch().count());

po::options_description desc("Allowed options");
// clang-format off
desc.add_options()
("help,h", "Show help")
("map,m", po::value<std::string>(),"Map to load")
("ai", po::value<std::vector<std::string>>(),"AI player(s) to add")
("objective", po::value<std::string>(),"domination(default)|conquer")
("replay", po::value<std::string>(),"Filename to write replay to (optional)")
("save", po::value<std::string>(),"Filename to write savegame to (optional)")
("random_init", po::value<unsigned>(),"Seed value for the random number generator (optional)")
("maxGF", po::value<unsigned>(),"Maximum number of game frames to run (optional)")
("map,m", po::value<std::string>()->required(),"Map to load")
("ai", po::value<std::vector<std::string>>()->required(),"AI player(s) to add")
("objective", po::value<std::string>()->default_value("domination"),"domination(default)|conquer")
("replay", po::value(&replay_path),"Filename to write replay to (optional)")
("save", po::value(&savegame_path),"Filename to write savegame to (optional)")
("random_init", po::value(&random_init),"Seed value for the random number generator (optional)")
("maxGF", po::value<unsigned>()->default_value(std::numeric_limits<unsigned>::max()),"Maximum number of game frames to run (optional)")
("version", "Show version information and exit")
;
// clang-format on
Expand All @@ -55,6 +56,20 @@ int main(int argc, char** argv)
try
{
po::store(po::command_line_parser(argc, argv).options(desc).run(), options);

if(options.count("help"))
{
bnw::cout << desc << std::endl;
return 0;
}
if(options.count("version"))
{
bnw::cout << rttr::version::GetTitle() << " v" << rttr::version::GetVersion() << "-"
<< rttr::version::GetRevision() << std::endl
<< "Compiled with " << System::getCompilerName() << " for " << System::getOSName() << std::endl;
return 0;
}

po::notify(options);
} catch(const std::exception& e)
{
Expand All @@ -63,35 +78,8 @@ int main(int argc, char** argv)
return 1;
}

if(options.count("help"))
{
bnw::cout << desc << std::endl;
return 0;
}
if(options.count("version"))
{
bnw::cout << rttr::version::GetTitle() << " v" << rttr::version::GetVersion() << "-"
<< rttr::version::GetRevision() << std::endl
<< "Compiled with " << System::getCompilerName() << " for " << System::getOSName() << std::endl;
return 0;
}
if(options.count("map") == 0)
{
bnw::cerr << "No map specified" << std::endl;
return 1;
}
if(options.count("ai") == 0)
{
bnw::cerr << "No AI specified" << std::endl;
return 1;
}

try
{
auto random_init = static_cast<unsigned>(std::chrono::high_resolution_clock::now().time_since_epoch().count());
if(options.count("random_init"))
random_init = options["random_init"].as<unsigned>();

// We print arguments and seed in order to be able to reproduce crashes.
for(int i = 0; i < argc; ++i)
bnw::cout << argv[i] << " ";
Expand All @@ -106,42 +94,30 @@ int main(int argc, char** argv)
const std::vector<AI::Info> ais = ParseAIOptions(options["ai"].as<std::vector<std::string>>());

GlobalGameSettings ggs;
if(options.count("objective"))
{
std::string objective = options["objective"].as<std::string>();
if(objective == "domination")
ggs.objective = GameObjective::TotalDomination;
else if(objective == "conquer")
ggs.objective = GameObjective::Conquer3_4;
else
{
bnw::cerr << "unknown objective: " << objective << std::endl;
return 1;
}
} else
const auto objective = options["objective"].as<std::string>();
if(objective == "domination")
ggs.objective = GameObjective::TotalDomination;
else if(objective == "conquer")
ggs.objective = GameObjective::Conquer3_4;
else
{
bnw::cerr << "unknown objective: " << objective << std::endl;
return 1;
}

ggs.objective = GameObjective::TotalDomination;
HeadlessGame game(ggs, mapPath, ais);
if(options.count("replay"))
game.StartReplay(options["replay"].as<std::string>(), random_init);
if(replay_path)
game.RecordReplay(*replay_path, random_init);

unsigned maxGF = std::numeric_limits<unsigned>::max();
if(options.count("maxGF"))
maxGF = options["maxGF"].as<unsigned>();

game.Run(maxGF);
game.Run(options["maxGF"].as<unsigned>());
game.Close();
if(options.count("save"))
game.SaveGame(options["save"].as<std::string>());
if(savegame_path)
game.SaveGame(*savegame_path);
} catch(const std::exception& e)
{
bnw::cerr << e.what() << std::endl;
return 1;
} catch(...)
{
bnw::cerr << "An unknown exception occurred" << std::endl;
return 1;
}

return 0;
Expand Down

0 comments on commit 979a3a6

Please sign in to comment.