From 126ffaf2e205243129ecdf18df10609401460701 Mon Sep 17 00:00:00 2001 From: Lukasz Dobrogowski Date: Thu, 9 Jun 2011 09:53:13 +0000 Subject: [PATCH] Added scenario, side options to commandline_options. Also changed and documented the helper functions in this class a bit. --- src/commandline_options.cpp | 38 +++++++++++++++++++---- src/commandline_options.hpp | 15 +++++---- src/game_controller.cpp | 43 +++++++++++++++++++------- src/tests/test_commandline_options.cpp | 10 ++++-- 4 files changed, 80 insertions(+), 26 deletions(-) diff --git a/src/commandline_options.cpp b/src/commandline_options.cpp index d06356fbf1d..392a0602f31 100644 --- a/src/commandline_options.cpp +++ b/src/commandline_options.cpp @@ -174,6 +174,8 @@ commandline_options::commandline_options ( int argc, char** argv ) : ("ai-config", po::value >()->composing(), "arg should have format side:value\nselects a configuration file to load for this side.") ("exit-at-end", "exit Wesnoth at the end of the scenario.") ("nogui", "runs the game without the GUI.") + ("scenario", po::value(), "selects a multiplayer scenario. The default scenario is \"multiplayer_The_Freelands\".") + ("side", po::value >()->composing(), " should have format side:value. selects a faction of the current era for this side by id.") ("turns", po::value(), "sets the number of turns. The default is \"50\".") ; @@ -210,7 +212,7 @@ commandline_options::commandline_options ( int argc, char** argv ) : po::store(po::command_line_parser(argc_,argv_).options(all_).positional(positional).style(parsing_style).run(),vm); if (vm.count("ai-config")) - multiplayer_ai_config = parse_to_int_string_tuples_(vm["ai-config"].as >()); + multiplayer_ai_config = parse_to_uint_string_tuples_(vm["ai-config"].as >()); if (vm.count("bpp")) bpp = vm["bpp"].as(); if (vm.count("campaign")) @@ -309,6 +311,8 @@ commandline_options::commandline_options ( int argc, char** argv ) : parse_resolution_(vm["resolution"].as()); if (vm.count("rng-seed")) rng_seed = vm["rng-seed"].as(); + if (vm.count("scenario")) + multiplayer_scenario = vm["scenario"].as(); if (vm.count("screenshot")) { screenshot = true; @@ -317,6 +321,8 @@ commandline_options::commandline_options ( int argc, char** argv ) : } if (vm.count("server")) server = vm["server"].as(); + if (vm.count("side")) + multiplayer_side = parse_to_uint_string_tuples_(vm["side"].as >()); if (vm.count("test")) test = vm["test"].as(); if (vm.count("turns")) @@ -354,18 +360,18 @@ void commandline_options::parse_resolution_ ( const std::string& resolution_stri resolution = boost::tuple(xres,yres); } -std::vector > commandline_options::parse_to_int_string_tuples_(const std::vector &strings) +std::vector > commandline_options::parse_to_uint_string_tuples_(const std::vector &strings, char separator) { - std::vector > vec; - boost::tuple elem; + std::vector > vec; + boost::tuple elem; foreach(const std::string &s, strings) { - const std::vector tokens = utils::split(s, ':'); + const std::vector tokens = utils::split(s, separator); if (tokens.size()!=2) { //TODO throw a meaningful exception } - elem.get<0>() = lexical_cast(tokens[0]); + elem.get<0>() = lexical_cast(tokens[0]); //TODO catch exception and pack in meaningful something elem.get<1>() = tokens[1]; vec.push_back(elem); @@ -373,6 +379,26 @@ std::vector > commandline_options::parse_to_int_st return vec; } +std::vector > commandline_options::parse_to_uint_string_string_tuples_(const std::vector &strings, char separator) +{ + std::vector > vec; + boost::tuple elem; + foreach(const std::string &s, strings) + { + const std::vector tokens = utils::split(s, separator); + if (tokens.size()!=3) + { + //TODO throw a meaningful exception + } + elem.get<0>() = lexical_cast(tokens[0]); + //TODO catch exception and pack in meaningful something + elem.get<1>() = tokens[1]; + elem.get<2>() = tokens[2]; + vec.push_back(elem); + } + return vec; +} + std::ostream& operator<<(std::ostream &os, const commandline_options& cmdline_opts) { os << "Usage: " << cmdline_opts.argv_[0] << " [] []\n"; diff --git a/src/commandline_options.hpp b/src/commandline_options.hpp index 41a6efd08d8..346d25848d1 100644 --- a/src/commandline_options.hpp +++ b/src/commandline_options.hpp @@ -77,11 +77,11 @@ public: /// True if --multiplayer was given on the command line. Goes directly into multiplayer mode. bool multiplayer; /// Non-empty if --ai-config was given on the command line. Vector of pairs (side number, value). Dependant on --multiplayer. - boost::optional > > multiplayer_ai_config; + boost::optional > > multiplayer_ai_config; /// Non-empty if --algorithm was given on the command line. Vector of pairs (side number, value). Dependant on --multiplayer. - boost::optional > > multiplayer_algorithm; + boost::optional > > multiplayer_algorithm; /// Non-empty if --controller was given on the command line. Vector of pairs (side number, controller). Dependant on --multiplayer. - boost::optional > > multiplayer_controller; + boost::optional > > multiplayer_controller; /// Non-empty if --era was given on the command line. Dependant on --multiplayer. boost::optional multiplayer_era; /// True if --exit-at-and was given on the command line. Dependant on --multiplayer. @@ -89,11 +89,11 @@ public: /// Non-empty if --label was given on the command line. Dependant on --multiplayer. boost::optional multiplayer_label; /// Non-empty if --parm was given on the command line. Vector of pairs (side number, parm name, parm value). Dependant on --multiplayer. - boost::optional > > multiplayer_parm; + boost::optional > > multiplayer_parm; /// Non-empty if --scenario was given on the command line. Dependant on --multiplayer. boost::optional multiplayer_scenario; /// Non-empty if --side was given on the command line. Vector of pairs (side number, faction id). Dependant on --multiplayer. - boost::optional > > multiplayer_side; + boost::optional > > multiplayer_side; /// Non-empty if --turns was given on the command line. Dependant on --multiplayer. boost::optional multiplayer_turns; /// Max FPS specified by --max-fps option. @@ -165,7 +165,10 @@ public: private: void parse_log_domains_(const std::string &domains_string, const int severity); void parse_resolution_ (const std::string &resolution_string); - std::vector > parse_to_int_string_tuples_(const std::vector &strings); + /// A helper function splitting vector of strings of format unsigned int:string to vector of tuples (unsigned int,string) + std::vector > parse_to_uint_string_tuples_(const std::vector &strings, char separator = ':'); + /// A helper function splitting vector of strings of format unsigned int:string:string to vector of tuples (unsigned int,string,string) + std::vector > parse_to_uint_string_string_tuples_(const std::vector &strings, char separator = ':'); int argc_; char **argv_; boost::program_options::options_description all_; diff --git a/src/game_controller.cpp b/src/game_controller.cpp index a50d3eca7ff..a3d98529330 100644 --- a/src/game_controller.cpp +++ b/src/game_controller.cpp @@ -464,8 +464,38 @@ bool game_controller::play_multiplayer_mode() size_t sides_counted = 0; + if (cmdline_opts_.multiplayer_ai_config) + { + for(std::vector >::const_iterator it=cmdline_opts_.multiplayer_ai_config->begin(); it!=cmdline_opts_.multiplayer_ai_config->end(); ++it) + { + const unsigned int side = it->get<0>(); + const std::string ai_cfg_name = it->get<1>(); + if (side > sides_counted) + { + std::cerr << "counted sides: " << side << "\n"; + sides_counted = side; + } + side_ai_configs[side] = ai_cfg_name; + } + } if (cmdline_opts_.multiplayer_exit_at_end) game_config::exit_at_end = true; + if (cmdline_opts_.multiplayer_scenario) + scenario = *cmdline_opts_.multiplayer_scenario; + if (cmdline_opts_.multiplayer_side) + { + for(std::vector >::const_iterator it=cmdline_opts_.multiplayer_side->begin(); it!=cmdline_opts_.multiplayer_side->end(); ++it) + { + const unsigned int side = it->get<0>(); + const std::string faction_id = it->get<1>(); + if (side > sides_counted) + { + std::cerr << "counted sides: " << side << "\n"; + sides_counted = side; + } + side_types[side] = faction_id; + } + } if (cmdline_opts_.multiplayer_turns) turns = *cmdline_opts_.multiplayer_turns; @@ -488,25 +518,14 @@ bool game_controller::play_multiplayer_mode() const bool last_digit = isdigit(name_tail) ? true:false; const size_t side = name_tail - '0'; - if(last_digit && side > sides_counted) { - std::cerr << "counted sides: " << side << "\n"; - sides_counted = side; - } - - if(name == "--scenario") { - scenario = value; - } else if(name == "--era") { + if(name == "--era") { era = value; } else if(name == "--label") { label = value; } else if(last_digit && name_head == "--controller") { side_controllers[side] = value; - } else if(last_digit && name_head == "--ai_config") { - side_ai_configs[side] = value; } else if(last_digit && name_head == "--algorithm") { side_algorithms[side] = value; - } else if(last_digit && name_head == "--side") { - side_types[side] = value; } else if(last_digit && name_head == "--parm") { const std::vector name_value = utils::split(value, ':'); if(name_value.size() != 2) { diff --git a/src/tests/test_commandline_options.cpp b/src/tests/test_commandline_options.cpp index 6cce4793901..a2d48be22e6 100644 --- a/src/tests/test_commandline_options.cpp +++ b/src/tests/test_commandline_options.cpp @@ -232,7 +232,10 @@ BOOST_AUTO_TEST_CASE (test_full_options) "--proxy-user=userfoo", "--resolution=800x600", "--rng-seed=1234", + "--scenario=scenfoo", "--screenshot", "mapfoo", "outssfoo", + "--side=1:sidefoo", + "--side=2:sidebar", "--server=servfoo", "--smallgui", "--test=testfoo", @@ -287,8 +290,11 @@ BOOST_AUTO_TEST_CASE (test_full_options) BOOST_CHECK(co.multiplayer_exit_at_end); BOOST_CHECK(!co.multiplayer_label); BOOST_CHECK(!co.multiplayer_parm); - BOOST_CHECK(!co.multiplayer_scenario); - BOOST_CHECK(!co.multiplayer_side); + BOOST_CHECK(co.multiplayer_scenario && *co.multiplayer_scenario == "scenfoo"); + BOOST_CHECK(co.multiplayer_side); + BOOST_CHECK(co.multiplayer_side->size() == 2); + BOOST_CHECK(co.multiplayer_side->at(0).get<0>() == 1 && co.multiplayer_side->at(0).get<1>() == "sidefoo"); + BOOST_CHECK(co.multiplayer_side->at(1).get<0>() == 2 && co.multiplayer_side->at(1).get<1>() == "sidebar"); BOOST_CHECK(co.multiplayer_turns && *co.multiplayer_turns == "42"); BOOST_CHECK(co.max_fps && *co.max_fps == 100); BOOST_CHECK(co.nocache);