Skip to content

Commit

Permalink
Apply suggested changes from review
Browse files Browse the repository at this point in the history
  • Loading branch information
wichern committed Jun 17, 2024
1 parent 13ef0d9 commit a716d1e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 53 deletions.
2 changes: 1 addition & 1 deletion extras/ai-battle/HeadlessGame.cpp
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion extras/ai-battle/HeadlessGame.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ 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:
Expand Down
89 changes: 38 additions & 51 deletions extras/ai-battle/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <boost/nowide/args.hpp>
#include <boost/nowide/filesystem.hpp>
#include <boost/nowide/iostream.hpp>
#include <boost/optional.hpp>
#include <boost/program_options.hpp>

namespace bnw = boost::nowide;
Expand All @@ -26,17 +27,20 @@ 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;

po::options_description desc("Allowed options");
// clang-format off
desc.add_options()
("help,h", "Show help")
("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>(),"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)")
("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<unsigned>()->default_value(std::chrono::high_resolution_clock::now().time_since_epoch().count()),"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 @@ -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)
{
Expand All @@ -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<unsigned>(std::chrono::high_resolution_clock::now().time_since_epoch().count());
if(options.count("random_init"))
random_init = options["random_init"].as<unsigned>();
auto 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)
Expand All @@ -102,34 +95,28 @@ 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"))
{
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;
}
} 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>();
unsigned maxGF = options["maxGF"].as<unsigned>();

game.Run(maxGF);
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;
Expand Down

0 comments on commit a716d1e

Please sign in to comment.