39 #define LOG_CF LOG_STREAM(info, log_config) 40 #define ERR_CF LOG_STREAM(err, log_config) 43 #define DBG_MP LOG_STREAM(debug, log_mp_connect_engine) 44 #define LOG_MP LOG_STREAM(info, log_mp_connect_engine) 45 #define WRN_MP LOG_STREAM(warn, log_mp_connect_engine) 46 #define ERR_MP LOG_STREAM(err, log_mp_connect_engine) 49 #define LOG_NW LOG_STREAM(info, log_network) 53 const std::array controller_names {
54 side_controller::human,
55 side_controller::human,
57 side_controller::none,
58 side_controller::reserved
61 const std::set<std::string> children_to_swap {
73 , params_(state.mp_settings())
75 , mp_metadata_(metadata)
76 , first_scenario_(first_scenario)
77 , force_lock_settings_()
101 std::vector<std::string> original_team_names;
102 std::string team_prefix(
_(
"Team") +
" ");
105 for(
config& side : sides) {
106 const std::string side_str = std::to_string(side_count);
112 if(team_name.
empty()) {
113 team_name = side_str;
117 user_team_name = team_name;
120 bool add_team =
true;
124 return data.team_name == team_name.
str();
130 auto name_itor = std::find(original_team_names.begin(), original_team_names.end(), team_name.
str());
134 if(name_itor == original_team_names.end()) {
135 original_team_names.push_back(team_name);
137 team_name =
"Team " + std::to_string(original_team_names.size());
139 team_name =
"Team " + std::to_string(std::distance(original_team_names.begin(), name_itor) + 1);
142 user_team_name = team_prefix + side_str;
189 if(lhs[
"random_faction"].to_bool() && !rhs[
"random_faction"].to_bool()) {
193 if(!lhs[
"random_faction"].to_bool() && rhs[
"random_faction"].to_bool()) {
238 user_data[
"name"] = name;
244 const std::string& username = data[
"name"];
245 assert(!username.empty());
256 bool side_assigned =
false;
257 if(side_taken >= 0) {
259 side_assigned =
true;
264 if(side->reserved_for() == username && side->player_id().empty() && side->controller() !=
CNTR_COMPUTER) {
265 side->place_user(data);
267 side_assigned =
true;
273 if(side_taken < 0 && !side_assigned) {
275 if(side->available_for_user(username) ||
277 side->place_user(data);
279 side_assigned =
true;
288 if(user_side->player_id() == username && !user_side->previous_save_id().empty()) {
290 if(side->player_id().empty() && side->previous_save_id() == user_side->previous_save_id()) {
291 side->place_user(data);
301 if(side->available_for_user()) {
311 DBG_MP <<
"updating level";
328 scenario_diff.
add_child(
"scenario_diff", std::move(diff));
341 if(!side->ready_for_start()) {
342 const int side_num = side->index() + 1;
343 DBG_MP <<
"not all sides are ready, side " <<
344 side_num <<
" not ready";
350 DBG_MP <<
"all sides are ready";
359 if(side->controller() !=
CNTR_EMPTY && side->allow_player()) {
369 std::multimap<std::string, config> children;
371 for(
const std::string& to_swap : children_to_swap) {
373 children.emplace(to_swap, child);
382 for(
const std::string& children_to_remove : children_to_swap) {
383 cfg_.clear_children(children_to_remove);
386 for(std::pair<std::string, config> child_map : children) {
387 cfg_.add_child(child_map.first, child_map.second);
393 DBG_MP <<
"starting a new game";
399 std::vector<std::string> avoid_faction_ids;
402 if(
params_.
mode != random_faction_mode::type::independent) {
404 if(!side2->flg().is_random_faction()) {
406 case random_faction_mode::type::no_mirror:
407 avoid_faction_ids.push_back(side2->flg().current_faction()[
"id"].str());
409 case random_faction_mode::type::no_ally_mirror:
410 if(side2->team() == side->team()) {
411 avoid_faction_ids.push_back(side2->flg().current_faction()[
"id"].str());
420 side->resolve_random(rng, avoid_faction_ids);
428 std::vector<int> playable_sides;
430 if(side->allow_player() && side->allow_shuffle()) {
431 playable_sides.push_back(side->index());
436 for(
int i = playable_sides.size();
i > 1;
i--) {
438 const int i_side = playable_sides[
i - 1];
440 if(i_side == j_side)
continue;
443 std::swap(side_engines_[j_side], side_engines_[i_side]);
446 std::multimap<std::string, config> tmp_side_children = side_engines_[j_side]->get_side_children();
447 side_engines_[j_side]->set_side_children(side_engines_[i_side]->get_side_children());
448 side_engines_[i_side]->set_side_children(tmp_side_children);
451 std::swap(side_engines_[j_side]->index_, side_engines_[i_side]->index_);
452 std::swap(side_engines_[j_side]->team_, side_engines_[i_side]->team_);
457 config lock(
"stop_updates");
472 DBG_MP <<
"starting a new game in commandline mode";
483 if(side_num == num) {
485 [fid = faction_id](
const config* faction) {
return (*faction)[
"id"] == fid; })
488 DBG_MP <<
"\tsetting side " << side_num <<
"\tfaction: " << faction_id;
489 side->set_faction_commandline(faction_id);
491 ERR_MP <<
"failed to set side " << side_num <<
" to faction " << faction_id;
500 if(side_num == num) {
501 DBG_MP <<
"\tsetting side " << side_num <<
"\tfaction: " << faction_id;
502 side->set_controller_commandline(faction_id);
509 std::string ai_algorithm = game_config.
child(
"ais")[
"default_ai_algorithm"].str();
510 side->set_ai_algorithm(ai_algorithm);
514 if(side_num == num) {
515 DBG_MP <<
"\tsetting side " << side_num <<
"\tfaction: " << faction_id;
516 side->set_ai_algorithm(faction_id);
523 side->resolve_random(rng);
537 if(side_num == side[
"side"].to_unsigned()) {
538 DBG_MP <<
"\tsetting side " << side[
"side"] <<
"\tai_config: " << faction_id;
539 side[
"ai_config"] = faction_id;
554 for(
const auto& [side_num, pname, pvalue] : *cmdline_opts.
multiplayer_parm) {
555 if(side_num == side[
"side"].to_unsigned()) {
556 DBG_MP <<
"\tsetting side " << side[
"side"] <<
" " << pname <<
": " << pvalue;
557 side[pname] = pvalue;
572 DBG_MP <<
"leaving the game";
579 std::pair<bool, bool> result(
false,
true);
581 if(data.
child(
"leave_game")) {
587 if(
const config& side_drop = data.
child(
"side_drop")) {
588 unsigned side_index = side_drop[
"side_num"].to_int() - 1;
597 side_to_drop->reset();
606 if(!data[
"side"].empty()) {
607 unsigned side_taken = data[
"side"].to_int() - 1;
610 const std::string name = data[
"name"];
613 response[
"failed"] =
true;
616 ERR_CF <<
"ERROR: No username provided with the side.";
626 response[
"failed"] =
true;
627 response[
"message"] =
"The nickname '" + name +
628 "' is already in use.";
636 observer_quit.
add_child(
"observer_quit")[
"name"] = name;
648 if(
s->available_for_user()) {
655 if(side_taken >= side_engines_.size()) {
657 response[
"failed"] =
true;
662 kick[
"username"] = data[
"name"];
667 ERR_CF <<
"ERROR: Couldn't assign a side to '" <<
674 LOG_CF <<
"client has taken a valid position";
682 result.second =
false;
684 LOG_NW <<
"sent player data";
686 ERR_CF <<
"tried to take illegal side: " << side_taken;
689 response[
"failed"] =
true;
694 if(
const config& change_faction = data.
child(
"change_faction")) {
708 const std::string& observer_name =
observer[
"name"];
729 if(side->player_id() ==
id) {
736 if(i >= side_engines_.size()) {
752 "auto_hosted",
false,
769 const std::string& save_id = side->save_id();
770 const std::string& player_id = side->player_id();
771 if(!save_id.empty() && !player_id.empty()) {
772 side_users[save_id] = player_id;
782 std::set<std::string>
names;
784 const std::string& save_id = side->previous_save_id();
785 if(side_users.find(save_id) != side_users.end()) {
786 side->set_reserved_for(side_users[save_id]);
790 names.insert(side_users[save_id]);
793 side->update_controller_options();
798 for(
const std::string& name : names)
809 side->update_controller_options();
819 static std::set<std::string> empty;
831 , parent_(parent_engine)
833 , current_controller_index_(0)
834 , controller_options_()
835 , allow_player_(cfg[
"allow_player"].to_bool(true))
839 , color_(
std::min(index,
gamemap::MAX_PLAYERS - 1))
840 , gold_(cfg[
"gold"].to_int(100))
841 , income_(cfg[
"income"])
842 , reserved_for_(cfg[
"current_player"])
845 , chose_random_(cfg[
"chose_random"].to_bool(false))
846 , disallow_shuffle_(cfg[
"disallow_shuffle"].to_bool(false))
849 , waiting_to_choose_faction_(allow_changes_)
852 , color_id_(color_options_.
at(color_))
858 "type",
cfg_[
"type"],
859 "gender",
cfg_[
"gender"],
860 "faction", cfg_[
"faction"],
861 "recruit", cfg_[
"recruit"],
865 ERR_CF <<
"found invalid side=" <<
cfg_[
"side"].to_int(
index_ + 1) <<
" in definition of side number " <<
index_ + 1;
871 const std::size_t side_cntr_index =
cfg_[
"controller"].to_int(-1) - 1;
878 ERR_MP <<
"controller=<number> is deperecated";
881 if(
cfg_[
"controller"] != side_controller::human &&
cfg_[
"controller"] != side_controller::ai &&
cfg_[
"controller"] != side_controller::none) {
890 cfg_[
"controller"] = side_controller::ai;
893 if(
cfg_[
"controller"] == side_controller::none) {
895 }
else if(
cfg_[
"controller"] == side_controller::ai) {
909 unsigned team_name_index = 0;
921 WRN_MP <<
"In side_engine constructor: Could not find my team_name " << cfg[
"team_name"] <<
" among the mp connect engine's list of team names. I am being assigned to the first team. This may indicate a bug!";
923 team_ = team_name_index;
929 if(!given_color.empty()) {
954 return N_(
"Anonymous player");
959 return N_(
"Computer Player");
977 LOG_MP <<
"side_engine::new_config: side=" <<
index_ + 1 <<
" faction=" << faction[
"id"] <<
" recruit=" << faction[
"recruit"];
978 res[
"faction_name"] = faction[
"name"];
979 res[
"faction"] = faction[
"id"];
980 faction.
remove_attributes(
"id",
"name",
"image",
"gender",
"type",
"description");
999 res[
"user_description"] =
t_string(desc,
"wesnoth");
1001 desc =
VGETTEXT(
"$playername $side", {
1002 {
"playername",
_(desc.c_str())},
1003 {
"side", res[
"side"].str()}
1009 if(res[
"name"].str().
empty() && !desc.empty()) {
1041 res[
"current_player"] = desc;
1061 leader = &side_unit;
1065 std::string leader_id = (*leader)[
"id"];
1068 if(!leader_id.empty()) {
1069 (*leader)[
"id"] = leader_id;
1081 LOG_MP <<
"side_engine::new_config: side=" <<
index_ + 1 <<
" type=" << (*leader)[
"type"] <<
" gender=" << (*leader)[
"gender"];
1084 (*leader)[
"type"] =
"null";
1085 (*leader)[
"gender"] =
"null";
1130 res[
"gold"] =
gold_;
1137 res[
"name"] =
cfg_[
"name"];
1140 res[
"user_description"] =
cfg_[
"user_description"];
1226 data[
"name"] = name;
1236 if(data[
"change_faction"].to_bool() && contains_selection) {
1326 if(controller_name == side_controller::ai) {
1330 if(controller_name == side_controller::none) {
1338 const std::string& name,
const std::string& controller_value)
bool ready_for_start() const
bool empty() const
Tests for an attribute that either was never set or was set to "".
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
std::string join_map(const T &v, const std::string &major=",", const std::string &minor=":")
void save_reserved_sides_information()
void set_waiting_to_choose_status(bool status)
void clear_children(T... keys)
void append(const config &cfg)
Append data from another config object to this one.
const std::string & current_gender() const
static std::string get_side_color_id_from_config(const config &cfg)
void import_user(const std::string &name, const bool observer, int side_taken=-1)
std::optional< std::vector< std::pair< unsigned int, std::string > > > multiplayer_ai_config
Non-empty if –ai-config was given on the command line.
std::vector< side_engine_ptr > side_engines_
Variant for storing WML attributes.
void load_previous_sides_users()
static game_config_view wrap(const config &cfg)
bool has_attribute(config_key_type key) const
void add_controller_option(ng::controller controller, const std::string &name, const std::string &controller_value)
void resolve_random(randomness::mt_rng &rng, const std::vector< std::string > &avoid)
int compare(const std::string &s1, const std::string &s2)
Case-sensitive lexicographical comparison.
static lg::log_domain log_config("config")
config initial_level_config(saved_game &state)
const std::string get_ignored_delim()
child_itors child_range(config_key_type key)
void place_user(const std::string &name)
void remove_attributes(T... keys)
void set_faction_commandline(const std::string &faction_name)
uint32_t get_next_random()
Get a new random number.
bool multiplayer_ignore_map_settings
True if –ignore-map-settings was given at the command line.
void set_controller_commandline(const std::string &controller_name)
bool is_normal_mp_game() const
std::string ai_algorithm_
std::optional< std::vector< std::tuple< unsigned int, std::string, std::string > > > multiplayer_parm
Non-empty if –parm was given on the command line.
static void add_mod_ai_from_config(config::const_child_itors configs)
void set_current_leader(const unsigned index)
std::vector< team_data_pod > team_data_
void set_current_gender(const unsigned index)
std::map< std::string, std::string > map_split(const std::string &val, char major, char minor, int flags, const std::string &default_value)
Splits a string based on two separators into a map.
void level_to_gamestate(const config &level, saved_game &state)
const bool allow_changes_
static std::string _(const char *str)
config new_config() const
void remove_attribute(config_key_type key)
std::vector< controller_option > controller_options_
void resolve_random(randomness::mt_rng &rng, const std::vector< std::string > &avoid_faction_ids=std::vector< std::string >())
bool available_for_user(const std::string &name="") const
void update_and_send_diff()
config get_diff(const config &c) const
A function to get the differences between this object, and 'c', as another config object...
mp_game_metadata * mp_metadata_
bool waiting_to_choose_faction_
std::vector< const mp::user_info * > user_data
The associated user data for each node, index-to-index.
std::string user_description() const
A small explanation about what's going on here: Each action has access to two game_info objects First...
static std::string at(const std::string &file, int line)
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
int find_user_side_index_by_id(const std::string &id) const
std::vector< side_engine_ptr > & side_engines()
void start_game_commandline(const commandline_options &cmdline_opts, const game_config_view &game_config)
The base template for associating string values with enum values.
const std::string & current_leader() const
config & add_child_at(config_key_type key, const config &val, unsigned index)
void set_controller(ng::controller controller)
const config * default_leader_cfg() const
Encapsulates the map of the game.
std::vector< std::string > color_options_
void send_level_data() const
saved_game_mode::type saved_game
const bool first_scenario_
std::optional< std::vector< std::pair< unsigned int, std::string > > > multiplayer_algorithm
Non-empty if –algorithm was given on the command line.
ng::controller controller_
std::vector< const config * > era_factions_
static void add_color_info(const game_config_view &v, bool build_defaults)
void swap(config &lhs, config &rhs)
Implement non-member swap function for std::swap (calls config::swap).
const ng::controller default_controller_
std::string id
Text to match against addon_info.tags()
void set_current_faction(const unsigned index)
const config & current_faction() const
config * current_config()
static void add_era_ai_from_config(const config &game_config)
void update_controller_options()
Game configuration data as global variables.
std::optional< std::vector< std::pair< unsigned int, std::string > > > multiplayer_side
Non-empty if –side was given on the command line.
static map_location::DIRECTION s
std::vector< std::string > names
std::string reserved_for_
static lg::log_domain log_network("network")
random_faction_mode::type mode
std::string to_serialized() const
void update_current_controller_index()
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
unsigned current_controller_index_
std::shared_ptr< side_engine > side_engine_ptr
config & add_child(config_key_type key)
std::pair< ng::controller, std::string > controller_option
const std::set< std::string > & connected_users() const
std::set< std::string > & connected_users_rw()
std::string user_team_name
game_classification & classification()
boost::iterator_range< child_iterator > child_itors
Managing the AIs configuration - headers.
const bool controller_lock_
Standard logging facilities (interface).
bool controller_changed(const int selection)
void set_side_children(std::multimap< std::string, config > children)
std::optional< std::string > multiplayer_turns
Non-empty if –turns was given on the command line.
static lg::log_domain log_mp_connect_engine("mp/connect/engine")
const mp_game_settings & params_
void send_to_server(const config &data)
Attempts to send given data to server if a connection is open.
connect_engine(saved_game &state, const bool first_scenario, mp_game_metadata *metadata)
std::optional< std::vector< std::pair< unsigned int, std::string > > > multiplayer_controller
Non-empty if –controller was given on the command line.
const config & child_or_empty(config_key_type key) const
Returns the first child with the given key, or an empty config if there is none.
std::vector< std::string > default_colors
ng::controller controller() const
bool can_start_game() const
A config object defines a single node in a WML file, with access to child nodes.
mp_game_settings & mp_settings()
Multiplayer parameters for this game.
static const config & get_ai_config_for(const std::string &id)
Return the config for a specified ai.
side_engine(const config &cfg, connect_engine &parent_engine, const int index)
std::multimap< std::string, config > get_side_children()
const config & child(config_key_type key) const
bool sides_available() const
static std::string controller_name(const team &t)
std::pair< bool, bool > process_network_data(const config &data)
std::string str(const std::string &fallback="") const
bool force_lock_settings_
void update_side_controller_options()