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 {
63 void sort_faction_options(std::vector<const config*>& factions)
67 std::sort(factions.begin(), factions.end(), [](
const config* lhs,
const config* rhs) {
68 bool lhs_rand = (*lhs)[
"random_faction"].to_bool();
69 bool rhs_rand = (*rhs)[
"random_faction"].to_bool();
72 if(lhs_rand && !rhs_rand) {
76 if(!lhs_rand && rhs_rand) {
91 , params_(state.mp_settings())
93 , mp_metadata_(metadata)
94 , first_scenario_(first_scenario)
95 , force_lock_settings_()
119 std::vector<std::string> original_team_names;
120 std::string team_prefix(
_(
"Team") +
" ");
123 for(
config& side : sides) {
124 const std::string side_str = std::to_string(side_count);
130 if(team_name.
empty()) {
131 team_name = side_str;
135 user_team_name = team_name;
138 bool add_team =
true;
142 return data.team_name == team_name.str();
148 auto name_itor =
std::find(original_team_names.begin(), original_team_names.end(), team_name.
str());
152 if(name_itor == original_team_names.end()) {
153 original_team_names.push_back(team_name);
155 team_name =
"Team " + std::to_string(original_team_names.size());
157 team_name =
"Team " + std::to_string(std::distance(original_team_names.begin(), name_itor) + 1);
160 user_team_name = team_prefix + side_str;
185 data.user_team_name = user_team_name.
str();
186 data.is_player_team = side[
"allow_player"].to_bool(
true);
199 if(era_config[
"auto_sort"].to_bool(
true)) {
243 const std::string& username =
data[
"name"];
244 assert(!username.empty());
255 bool side_assigned =
false;
256 if(side_taken >= 0) {
258 side_assigned =
true;
263 if(side->reserved_for() == username && side->player_id().empty() && side->controller() !=
CNTR_COMPUTER) {
264 side->place_user(
data);
266 side_assigned =
true;
272 if(side_taken < 0 && !side_assigned) {
274 if(side->available_for_user(username) ||
276 side->place_user(
data);
278 side_assigned =
true;
287 if(user_side->player_id() == username && !user_side->previous_save_id().empty()) {
289 if(side->player_id().empty() && side->previous_save_id() == user_side->previous_save_id()) {
290 side->place_user(
data);
300 if(side->available_for_user()) {
310 DBG_MP <<
"updating level";
327 scenario_diff.
add_child(
"scenario_diff", std::move(diff));
340 if(!side->ready_for_start()) {
341 const int side_num = side->index() + 1;
342 DBG_MP <<
"not all sides are ready, side " <<
343 side_num <<
" not ready";
349 DBG_MP <<
"all sides are ready";
358 if(side->controller() !=
CNTR_EMPTY && side->allow_player()) {
368 std::multimap<std::string, config> children;
370 for(
const std::string& to_swap : children_to_swap) {
372 children.emplace(to_swap, child);
381 for(
const std::string& children_to_remove : children_to_swap) {
385 for(std::pair<std::string, config> child_map : children) {
392 DBG_MP <<
"starting a new game";
398 std::vector<std::string> avoid_faction_ids;
401 if(
params_.
mode != random_faction_mode::type::independent) {
403 if(!side2->flg().is_random_faction()) {
405 case random_faction_mode::type::no_mirror:
406 avoid_faction_ids.push_back(side2->flg().current_faction()[
"id"].str());
408 case random_faction_mode::type::no_ally_mirror:
409 if(side2->team() == side->team()) {
410 avoid_faction_ids.push_back(side2->flg().current_faction()[
"id"].str());
419 side->resolve_random(rng, avoid_faction_ids);
427 std::vector<int> playable_sides;
429 if(side->allow_player() && side->allow_shuffle()) {
430 playable_sides.push_back(side->index());
435 for(
int i = playable_sides.size();
i > 1;
i--) {
437 const int i_side = playable_sides[
i - 1];
439 if(i_side == j_side)
continue;
445 std::multimap<std::string, config> tmp_side_children =
side_engines_[j_side]->get_side_children();
456 config lock(
"stop_updates");
471 DBG_MP <<
"starting a new game in commandline mode";
482 if(side_num == num) {
484 [fid = faction_id](
const config* faction) { return (*faction)[
"id"] == fid; })
487 DBG_MP <<
"\tsetting side " << side_num <<
"\tfaction: " << faction_id;
488 side->set_faction_commandline(faction_id);
490 ERR_MP <<
"failed to set side " << side_num <<
" to faction " << faction_id;
499 if(side_num == num) {
500 DBG_MP <<
"\tsetting side " << side_num <<
"\tfaction: " << faction_id;
501 side->set_controller_commandline(faction_id);
508 std::string ai_algorithm =
game_config.mandatory_child(
"ais")[
"default_ai_algorithm"].str();
509 side->set_ai_algorithm(ai_algorithm);
513 if(side_num == num) {
514 DBG_MP <<
"\tsetting side " << side_num <<
"\tfaction: " << faction_id;
515 side->set_ai_algorithm(faction_id);
522 side->resolve_random(rng);
536 if(side_num == side[
"side"].to_unsigned()) {
537 DBG_MP <<
"\tsetting side " << side[
"side"] <<
"\tai_config: " << faction_id;
538 side[
"ai_config"] = faction_id;
553 for(
const auto& [side_num, pname, pvalue] : *cmdline_opts.
multiplayer_parm) {
554 if(side_num == side[
"side"].to_unsigned()) {
555 DBG_MP <<
"\tsetting side " << side[
"side"] <<
" " << pname <<
": " << pvalue;
556 side[pname] = pvalue;
571 DBG_MP <<
"leaving the game";
578 std::pair<bool, bool> result(
false,
true);
580 if(
data.has_child(
"leave_game")) {
586 if(
auto side_drop =
data.optional_child(
"side_drop")) {
587 unsigned side_index = side_drop[
"side_num"].to_int() - 1;
596 side_to_drop->reset();
605 if(!
data[
"side"].empty()) {
606 unsigned side_taken =
data[
"side"].to_int() - 1;
609 const std::string name =
data[
"name"];
612 response[
"failed"] =
true;
615 ERR_CF <<
"ERROR: No username provided with the side.";
625 response[
"failed"] =
true;
626 response[
"message"] =
"The nickname '" + name +
627 "' is already in use.";
635 observer_quit.
add_child(
"observer_quit")[
"name"] = name;
647 if(
s->available_for_user()) {
656 response[
"failed"] =
true;
661 kick[
"username"] =
data[
"name"];
666 ERR_CF <<
"ERROR: Couldn't assign a side to '" <<
673 LOG_CF <<
"client has taken a valid position";
681 result.second =
false;
683 LOG_NW <<
"sent player data";
685 ERR_CF <<
"tried to take illegal side: " << side_taken;
688 response[
"failed"] =
true;
693 if(
auto change_faction =
data.optional_child(
"change_faction")) {
706 if(
auto observer =
data.optional_child(
"observer_quit")) {
707 const std::string& observer_name =
observer[
"name"];
728 if(side->player_id() ==
id) {
771 const std::string& save_id = side->save_id();
772 const std::string& player_id = side->player_id();
773 if(!save_id.empty() && !player_id.empty()) {
774 side_users[save_id] = player_id;
784 std::set<std::string>
names;
786 const std::string& save_id = side->previous_save_id();
787 if(side_users.find(save_id) != side_users.end()) {
788 side->set_reserved_for(side_users[save_id]);
792 names.insert(side_users[save_id]);
795 side->update_controller_options();
800 for(
const std::string& name :
names)
811 side->update_controller_options();
821 static std::set<std::string> empty;
833 , parent_(parent_engine)
835 , current_controller_index_(0)
836 , controller_options_()
837 , allow_player_(cfg[
"allow_player"].to_bool(true))
838 , controller_lock_(cfg[
"controller_lock"].to_bool(parent_.force_lock_settings_) && parent_.params_.use_map_settings)
842 , gold_(cfg[
"gold"].to_int(100))
843 , income_(cfg[
"income"].to_int())
844 , reserved_for_(cfg[
"current_player"])
847 , chose_random_(cfg[
"chose_random"].to_bool(false))
848 , disallow_shuffle_(cfg[
"disallow_shuffle"].to_bool(false))
849 , flg_(parent_.era_factions_, cfg_, parent_.force_lock_settings_, parent_.params_.use_map_settings, parent_.params_.
saved_game ==
saved_game_mode::
type::midgame)
850 , 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))
851 , waiting_to_choose_faction_(allow_changes_)
854 , color_id_(color_options_.
at(color_))
861 "faction",
cfg_[
"faction"],
862 "recruit",
cfg_[
"recruit"],
866 "type", (p_cfg)[
"type"],
867 "gender", (p_cfg)[
"gender"],
873 ERR_CF <<
"found invalid side=" <<
cfg_[
"side"].to_int(
index_ + 1) <<
" in definition of side number " <<
index_ + 1;
878 if(
cfg_[
"controller"] != side_controller::human &&
cfg_[
"controller"] != side_controller::ai &&
cfg_[
"controller"] != side_controller::none) {
887 cfg_[
"controller"] = side_controller::ai;
890 if(
cfg_[
"controller"] == side_controller::none) {
892 }
else if(
cfg_[
"controller"] == side_controller::ai) {
906 unsigned team_name_index = 0;
908 if(
data.team_name ==
cfg[
"team_name"]) {
918 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!";
920 team_ = team_name_index;
926 if(!given_color.empty()) {
951 return N_(
"Anonymous player");
956 return N_(
"Computer Player");
974 LOG_MP <<
"side_engine::new_config: side=" <<
index_ + 1 <<
" faction=" << faction[
"id"] <<
" recruit=" << faction[
"recruit"];
975 res[
"faction_name"] = faction[
"name"];
976 res[
"faction"] = faction[
"id"];
977 faction.
remove_attributes(
"id",
"name",
"image",
"gender",
"type",
"description");
996 res[
"user_description"] =
t_string(desc,
"wesnoth");
998 desc =
VGETTEXT(
"$playername $side", {
999 {
"playername",
_(desc.c_str())},
1000 {
"side", res[
"side"].str()}
1006 if(res[
"name"].str().
empty() && !desc.empty()) {
1038 res[
"current_player"] = desc;
1055 LOG_MP <<
"side_engine::new_config: side=" <<
index_ + 1 <<
" type=" << leader[
"type"]
1056 <<
" gender=" << leader[
"gender"];
1066 res[
"team_name"] = new_team_name;
1072 res[
"gold"] =
gold_;
1079 res[
"name"] =
cfg_[
"name"];
1082 res[
"user_description"] =
cfg_[
"user_description"];
1168 data[
"name"] = name;
1178 if(
data[
"change_faction"].to_bool() && contains_selection) {
1280 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_
config * current_config()
std::set< std::string > & connected_users_rw()
std::pair< bool, bool > process_network_data(const config &data)
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 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)
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)
int compare(const std::string &s1, const std::string &s2)
Case-sensitive lexicographical comparison.
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