35 #define LOG_CF LOG_STREAM(info, log_config)
36 #define ERR_CF LOG_STREAM(err, log_config)
39 #define DBG_MP LOG_STREAM(debug, log_mp_connect_engine)
40 #define LOG_MP LOG_STREAM(info, log_mp_connect_engine)
41 #define WRN_MP LOG_STREAM(warn, log_mp_connect_engine)
42 #define ERR_MP LOG_STREAM(err, log_mp_connect_engine)
45 #define LOG_NW LOG_STREAM(info, log_network)
49 const std::array controller_names {
50 side_controller::human,
51 side_controller::human,
53 side_controller::none,
54 side_controller::reserved
57 const std::set<std::string> children_to_swap {
70 , params_(state.mp_settings())
72 , mp_metadata_(metadata)
73 , first_scenario_(first_scenario)
74 , force_lock_settings_()
78 , era_info_(level_.mandatory_child(
"era"))
93 std::vector<std::string> original_team_names;
94 std::string team_prefix(
_(
"Team") +
" ");
97 for(
config& side : sides) {
98 const std::string side_str = std::to_string(side_count);
104 if(team_name.
empty()) {
105 team_name = side_str;
109 user_team_name = team_name;
112 bool add_team =
true;
116 return data.team_name == team_name.str();
122 auto name_itor =
std::find(original_team_names.begin(), original_team_names.end(), team_name.
str());
126 if(name_itor == original_team_names.end()) {
127 original_team_names.push_back(team_name);
129 team_name =
"Team " + std::to_string(original_team_names.size());
131 team_name =
"Team " + std::to_string(std::distance(original_team_names.begin(), name_itor) + 1);
134 user_team_name = team_prefix + side_str;
159 data.user_team_name = user_team_name.
str();
160 data.is_player_team = side[
"allow_player"].to_bool(
true);
208 const std::string& username =
data[
"name"];
209 assert(!username.empty());
220 bool side_assigned =
false;
221 if(side_taken >= 0) {
223 side_assigned =
true;
228 if(side->reserved_for() == username && side->player_id().empty() && side->controller() !=
CNTR_COMPUTER) {
229 side->place_user(
data);
231 side_assigned =
true;
237 if(side_taken < 0 && !side_assigned) {
239 if(side->available_for_user(username) ||
241 side->place_user(
data);
243 side_assigned =
true;
252 if(user_side->player_id() == username && !user_side->previous_save_id().empty()) {
254 if(side->player_id().empty() && side->previous_save_id() == user_side->previous_save_id()) {
255 side->place_user(
data);
265 if(side->available_for_user()) {
275 DBG_MP <<
"updating level";
302 if(!side->ready_for_start()) {
303 const int side_num = side->index() + 1;
304 DBG_MP <<
"not all sides are ready, side " <<
305 side_num <<
" not ready";
311 DBG_MP <<
"all sides are ready";
320 if(side->controller() !=
CNTR_EMPTY && side->allow_player()) {
330 std::multimap<std::string, config> children;
332 for(
const std::string& to_swap : children_to_swap) {
334 children.emplace(to_swap, child);
343 for(
const std::string& children_to_remove : children_to_swap) {
347 for(std::pair<std::string, config> child_map : children) {
354 DBG_MP <<
"starting a new game";
360 std::vector<std::string> avoid_faction_ids;
363 if(
params_.
mode != random_faction_mode::type::independent) {
365 if(!side2->flg().is_random_faction()) {
367 case random_faction_mode::type::no_mirror:
368 avoid_faction_ids.push_back(side2->flg().current_faction()[
"id"].str());
370 case random_faction_mode::type::no_ally_mirror:
371 if(side2->team() == side->team()) {
372 avoid_faction_ids.push_back(side2->flg().current_faction()[
"id"].str());
381 side->resolve_random(rng, avoid_faction_ids);
389 std::vector<int> playable_sides;
391 if(side->allow_player() && side->allow_shuffle()) {
392 playable_sides.push_back(side->index());
397 for(
int i = playable_sides.size();
i > 1;
i--) {
399 const int i_side = playable_sides[
i - 1];
401 if(i_side == j_side)
continue;
407 std::multimap<std::string, config> tmp_side_children =
side_engines_[j_side]->get_side_children();
432 DBG_MP <<
"starting a new game in commandline mode";
443 if(side_num == num) {
445 [fid = faction_id](
const config* faction) { return (*faction)[
"id"] == fid; })
448 DBG_MP <<
"\tsetting side " << side_num <<
"\tfaction: " << faction_id;
449 side->set_faction_commandline(faction_id);
451 ERR_MP <<
"failed to set side " << side_num <<
" to faction " << faction_id;
460 if(side_num == num) {
461 DBG_MP <<
"\tsetting side " << side_num <<
"\tfaction: " << faction_id;
462 side->set_controller_commandline(faction_id);
469 std::string ai_algorithm =
game_config.mandatory_child(
"ais")[
"default_ai_algorithm"].str();
470 side->set_ai_algorithm(ai_algorithm);
474 if(side_num == num) {
475 DBG_MP <<
"\tsetting side " << side_num <<
"\tfaction: " << faction_id;
476 side->set_ai_algorithm(faction_id);
483 side->resolve_random(rng);
497 if(side_num == side[
"side"].to_unsigned()) {
498 DBG_MP <<
"\tsetting side " << side[
"side"] <<
"\tai_config: " << faction_id;
499 side[
"ai_config"] = faction_id;
514 for(
const auto& [side_num, pname, pvalue] : *cmdline_opts.
multiplayer_parm) {
515 if(side_num == side[
"side"].to_unsigned()) {
516 DBG_MP <<
"\tsetting side " << side[
"side"] <<
" " << pname <<
": " << pvalue;
517 side[pname] = pvalue;
532 DBG_MP <<
"leaving the game";
538 if(
data.has_child(
"leave_game")) {
543 if(
auto side_drop =
data.optional_child(
"side_drop")) {
544 unsigned side_index = side_drop[
"side_num"].to_int() - 1;
553 side_to_drop->reset();
561 if(!
data[
"side"].empty()) {
562 unsigned side_taken =
data[
"side"].to_int() - 1;
565 const std::string name =
data[
"name"];
567 ERR_CF <<
"ERROR: No username provided with the side.";
589 if(
s->available_for_user()) {
601 ERR_CF <<
"ERROR: Couldn't assign a side to '" << name <<
"'";
606 LOG_CF <<
"client has taken a valid position";
614 LOG_NW <<
"sent player data";
616 ERR_CF <<
"tried to take illegal side: " << side_taken;
620 if(
auto change_faction =
data.optional_child(
"change_faction")) {
633 if(
auto observer =
data.optional_child(
"observer_quit")) {
634 const std::string& observer_name =
observer[
"name"];
655 if(side->player_id() ==
id) {
696 const std::string& save_id = side->save_id();
697 const std::string& player_id = side->player_id();
698 if(!save_id.empty() && !player_id.empty()) {
699 side_users[save_id] = player_id;
709 std::set<std::string>
names;
711 const std::string& save_id = side->previous_save_id();
712 if(side_users.find(save_id) != side_users.end()) {
713 side->set_reserved_for(side_users[save_id]);
717 names.insert(side_users[save_id]);
720 side->update_controller_options();
725 for(
const std::string& name :
names)
736 side->update_controller_options();
746 static std::set<std::string> empty;
758 , parent_(parent_engine)
760 , current_controller_index_(0)
761 , controller_options_()
762 , allow_player_(
cfg[
"allow_player"].to_bool(true))
763 , controller_lock_(
cfg[
"controller_lock"].to_bool(parent_.force_lock_settings_) && parent_.params_.use_map_settings)
767 , gold_(
cfg[
"gold"].to_int(100))
768 , income_(
cfg[
"income"].to_int())
769 , reserved_for_(
cfg[
"current_player"])
772 , chose_random_(
cfg[
"chose_random"].to_bool(false))
773 , disallow_shuffle_(
cfg[
"disallow_shuffle"].to_bool(false))
774 , flg_(parent_.era_info_, parent_.era_factions_, cfg_, parent_.force_lock_settings_, parent_.params_.use_map_settings, parent_.params_.
saved_game ==
saved_game_mode::
type::midgame)
775 , allow_changes_(parent_.params_.
saved_game !=
saved_game_mode::
type::midgame && !(flg_.choosable_factions().
size() == 1 && flg_.choosable_leaders().
size() == 1 && flg_.choosable_genders().
size() == 1))
776 , waiting_to_choose_faction_(allow_changes_)
779 , color_id_(color_options_.
at(color_))
785 "faction",
cfg_[
"faction"],
786 "recruit",
cfg_[
"recruit"],
790 "type", (p_cfg)[
"type"],
791 "gender", (p_cfg)[
"gender"],
796 ERR_CF <<
"found invalid side=" <<
cfg_[
"side"].to_int(
index_ + 1) <<
" in definition of side number " <<
index_ + 1;
801 if(
cfg_[
"controller"] != side_controller::human &&
cfg_[
"controller"] != side_controller::ai &&
cfg_[
"controller"] != side_controller::none) {
810 cfg_[
"controller"] = side_controller::ai;
813 if(
cfg_[
"controller"] == side_controller::none) {
815 }
else if(
cfg_[
"controller"] == side_controller::ai) {
829 unsigned team_name_index = 0;
831 if(
data.team_name ==
cfg[
"team_name"]) {
841 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!";
843 team_ = team_name_index;
849 if(!given_color.empty()) {
874 return N_(
"Anonymous player");
879 return N_(
"Computer Player");
897 LOG_MP <<
"side_engine::new_config: side=" <<
index_ + 1 <<
" faction=" << faction[
"id"] <<
" recruit=" << faction[
"recruit"];
898 res[
"faction_name"] = faction[
"name"];
899 res[
"faction"] = faction[
"id"];
900 faction.
remove_attributes(
"id",
"name",
"image",
"gender",
"type",
"description");
919 res[
"user_description"] =
t_string(desc,
"wesnoth");
921 desc =
VGETTEXT(
"$playername $side", {
922 {
"playername",
_(desc.c_str())},
923 {
"side", res[
"side"].str()}
929 if(res[
"name"].str().
empty() && !desc.empty()) {
961 res[
"current_player"] = desc;
978 LOG_MP <<
"side_engine::new_config: side=" <<
index_ + 1 <<
" type=" << leader[
"type"]
979 <<
" gender=" << leader[
"gender"];
989 res[
"team_name"] = new_team_name;
1002 res[
"name"] =
cfg_[
"name"];
1005 res[
"user_description"] =
cfg_[
"user_description"];
1091 data[
"name"] = name;
1101 if(
data[
"change_faction"].to_bool() && contains_selection) {
1203 const std::string& name,
const std::string& controller_value)
std::vector< std::string > names
static const config & get_ai_config_for(const std::string &id)
Return the config for a specified ai.
static void add_era_ai_from_config(const config &game_config)
static void add_mod_ai_from_config(const config::const_child_itors &configs)
utils::optional< std::vector< std::pair< unsigned int, std::string > > > multiplayer_side
Non-empty if –side was given on the command line.
utils::optional< std::vector< std::pair< unsigned int, std::string > > > multiplayer_ai_config
Non-empty if –ai-config was given on the command line.
utils::optional< std::string > multiplayer_turns
Non-empty if –turns was given on the command line.
bool multiplayer_ignore_map_settings
True if –ignore-map-settings was given at the command line.
utils::optional< std::vector< std::pair< unsigned int, std::string > > > multiplayer_controller
Non-empty if –controller was given on the command line.
utils::optional< std::vector< std::pair< unsigned int, std::string > > > multiplayer_algorithm
Non-empty if –algorithm was given on the command line.
utils::optional< std::vector< std::tuple< unsigned int, std::string, std::string > > > multiplayer_parm
Non-empty if –parm was given on the command line.
Variant for storing WML attributes.
std::string str(const std::string &fallback="") const
bool empty() const
Tests for an attribute that either was never set or was set to "".
A config object defines a single node in a WML file, with access to child nodes.
void append(const config &cfg)
Append data from another config object to this one.
void remove_attributes(T... keys)
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.
config & mandatory_child(config_key_type key, int n=0)
Returns the nth child with the given key, or throws an error if there is none.
boost::iterator_range< child_iterator > child_itors
config & add_child_at(config_key_type key, const config &val, std::size_t index)
void clear_children(T... keys)
bool has_child(config_key_type key) const
Determine whether a config has a child or not.
bool has_attribute(config_key_type key) const
void remove_children(config_key_type key, const std::function< bool(const config &)> &p={})
Removes all children with tag key for which p returns true.
child_itors child_range(config_key_type key)
config & child_or_add(config_key_type key)
Returns a reference to the first child with the given key.
void remove_attribute(config_key_type key)
config get_diff(const config &c) const
A function to get the differences between this object, and 'c', as another config object.
optional_config_impl< config > optional_child(config_key_type key, int n=0)
Equivalent to mandatory_child, but returns an empty optional if the nth child was not found.
config & add_child(config_key_type key)
bool is_normal_mp_game() const
A class grating read only view to a vector of config objects, viewed as one config with all children ...
static game_config_view wrap(const config &cfg)
Encapsulates the map of the game.
std::vector< const config * > era_factions_
std::vector< team_data_pod > team_data_
const ng::controller default_controller_
std::set< std::string > & connected_users_rw()
void import_user(const std::string &name, const bool observer, int side_taken=-1)
bool sides_available() const
bool force_lock_settings_
std::vector< side_engine_ptr > side_engines_
const bool first_scenario_
void update_and_send_diff()
void save_reserved_sides_information()
const std::set< std::string > & connected_users() const
const mp_game_settings & params_
void send_level_data() const
void load_previous_sides_users()
mp_game_metadata * mp_metadata_
void update_side_controller_options()
connect_engine(saved_game &state, const bool first_scenario, mp_game_metadata *metadata)
void start_game_commandline(const commandline_options &cmdline_opts, const game_config_view &game_config)
int find_user_side_index_by_id(const std::string &id) const
bool process_network_data(const config &data)
bool can_start_game() const
const std::string & current_gender() const
void set_current_faction(const unsigned index)
void resolve_random(randomness::mt_rng &rng, const std::vector< std::string > &avoid)
void set_current_leader(const unsigned index)
void set_current_gender(const unsigned index)
const config & current_faction() const
const std::string & current_leader() const
const config & cfg() const
unsigned current_controller_index_
void update_controller_options()
const bool controller_lock_
const bool allow_changes_
void set_controller_commandline(const std::string &controller_name)
void set_faction_commandline(const std::string &faction_name)
void place_user(const std::string &name)
void set_side_children(const std::multimap< std::string, config > &children)
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_faction_ids=std::vector< std::string >())
void update_current_controller_index()
side_engine(const config &cfg, connect_engine &parent_engine, const int index)
std::vector< controller_option > controller_options_
std::string ai_algorithm_
std::string reserved_for_
std::vector< std::string > color_options_
void set_controller(ng::controller controller)
void set_waiting_to_choose_status(bool status)
bool controller_changed(const int selection)
std::string user_description() const
ng::controller controller_
std::multimap< std::string, config > get_side_children()
bool ready_for_start() const
config new_config() const
bool waiting_to_choose_faction_
ng::controller controller() const
bool available_for_user(const std::string &name="") const
const std::string get_ignored_delim()
uint32_t get_next_random()
Get a new random number.
game_classification & classification()
mp_game_settings & mp_settings()
Multiplayer parameters for this game.
std::string to_serialized() const
static std::string get_side_color_id_from_config(const config &cfg)
void swap(config &lhs, config &rhs)
Implement non-member swap function for std::swap (calls config::swap).
Managing the AIs configuration - headers.
static lg::log_domain log_network("network")
static lg::log_domain log_mp_connect_engine("mp/connect/engine")
static lg::log_domain log_config("config")
static std::string _(const char *str)
std::vector< const mp::user_info * > user_data
The associated user data for each node, index-to-index.
Standard logging facilities (interface).
A small explanation about what's going on here: Each action has access to two game_info objects First...
Game configuration data as global variables.
std::vector< std::string > default_colors
static void add_color_info(const game_config_view &v, bool build_defaults)
static std::string controller_name(const team &t)
Main entry points of multiplayer mode.
config initial_level_config(saved_game &state)
void level_to_gamestate(const config &level, saved_game &state)
void send_to_server(const config &data)
Attempts to send given data to server if a connection is open.
std::pair< ng::controller, std::string > controller_option
std::shared_ptr< side_engine > side_engine_ptr
static std::string at(const std::string &file, int line)
std::size_t size(std::string_view str)
Length in characters of a UTF-8 string.
std::size_t index(std::string_view str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
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.
std::string join_map(const T &v, const std::string &major=",", const std::string &minor=":")
auto * find(Container &container, const Value &value)
Convenience wrapper for using find on a container without needing to comare to end()
random_faction_mode::type mode
saved_game_mode::type saved_game
The base template for associating string values with enum values.
static map_location::direction s