From a716d1e27e79415f540658417d41a56312b63e93 Mon Sep 17 00:00:00 2001 From: Paul Wichern Date: Mon, 17 Jun 2024 19:07:38 +0200 Subject: [PATCH] Apply suggested changes from review --- extras/ai-battle/HeadlessGame.cpp | 2 +- extras/ai-battle/HeadlessGame.h | 2 +- extras/ai-battle/main.cpp | 89 +++++++++++++------------------ 3 files changed, 40 insertions(+), 53 deletions(-) diff --git a/extras/ai-battle/HeadlessGame.cpp b/extras/ai-battle/HeadlessGame.cpp index 2189ce354..4941abb0e 100644 --- a/extras/ai-battle/HeadlessGame.cpp +++ b/extras/ai-battle/HeadlessGame.cpp @@ -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); diff --git a/extras/ai-battle/HeadlessGame.h b/extras/ai-battle/HeadlessGame.h index 19451cbe4..c52c90625 100644 --- a/extras/ai-battle/HeadlessGame.h +++ b/extras/ai-battle/HeadlessGame.h @@ -27,7 +27,7 @@ class HeadlessGame void Run(unsigned maxGF = std::numeric_limits::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: diff --git a/extras/ai-battle/main.cpp b/extras/ai-battle/main.cpp index 0c16f6622..65a1da3ec 100644 --- a/extras/ai-battle/main.cpp +++ b/extras/ai-battle/main.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include namespace bnw = boost::nowide; @@ -26,17 +27,20 @@ int main(int argc, char** argv) bnw::nowide_filesystem(); bnw::args _(argc, argv); + boost::optional replay_path; + boost::optional savegame_path; + po::options_description desc("Allowed options"); // clang-format off desc.add_options() ("help,h", "Show help") ("map,m", po::value()->required(),"Map to load") ("ai", po::value>()->required(),"AI player(s) to add") - ("objective", po::value(),"domination(default)|conquer") - ("replay", po::value(),"Filename to write replay to (optional)") - ("save", po::value(),"Filename to write savegame to (optional)") - ("random_init", po::value(),"Seed value for the random number generator (optional)") - ("maxGF", po::value(),"Maximum number of game frames to run (optional)") + ("objective", po::value()->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()->default_value(std::chrono::high_resolution_clock::now().time_since_epoch().count()),"Seed value for the random number generator (optional)") + ("maxGF", po::value()->default_value(std::numeric_limits::max()),"Maximum number of game frames to run (optional)") ("version", "Show version information and exit") ; // clang-format on @@ -51,6 +55,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) { @@ -59,34 +77,9 @@ 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(std::chrono::high_resolution_clock::now().time_since_epoch().count()); - if(options.count("random_init")) - random_init = options["random_init"].as(); + auto random_init = options["random_init"].as(); // We print arguments and seed in order to be able to reproduce crashes. for(int i = 0; i < argc; ++i) @@ -102,34 +95,28 @@ int main(int argc, char** argv) const std::vector ais = ParseAIOptions(options["ai"].as>()); GlobalGameSettings ggs; - if(options.count("objective")) - { - const auto objective = options["objective"].as(); - 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(); + 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(), random_init); + if(replay_path) + game.RecordReplay(*replay_path, random_init); - unsigned maxGF = std::numeric_limits::max(); - if(options.count("maxGF")) - maxGF = options["maxGF"].as(); + unsigned maxGF = options["maxGF"].as(); game.Run(maxGF); game.Close(); - if(options.count("save")) - game.SaveGame(options["save"].as()); + if(savegame_path) + game.SaveGame(*savegame_path); } catch(const std::exception& e) { bnw::cerr << e.what() << std::endl;