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;
167 data.user_team_name = user_team_name.
str();
168 data.is_player_team = side[
"allow_player"].to_bool(
true);
185 const config& lhs = *c1;
186 const config& rhs = *c2;
189 if(lhs[
"random_faction"].to_bool() && !rhs[
"random_faction"].to_bool()) {
193 if(!lhs[
"random_faction"].to_bool() && rhs[
"random_faction"].to_bool()) {
208 if(first_scenario_) {
216 load_previous_sides_users();
227 config* connect_engine::current_config() {
231 void connect_engine::import_user(
const std::string& name,
const bool observer,
int side_taken)
240 const std::string& username =
data[
"name"];
241 assert(!username.empty());
243 connected_users_rw().insert(username);
246 update_side_controller_options();
252 bool side_assigned =
false;
253 if(side_taken >= 0) {
254 side_engines_[side_taken]->place_user(
data,
true);
255 side_assigned =
true;
260 if(side->reserved_for() == username && side->player_id().empty() && side->controller() !=
CNTR_COMPUTER) {
261 side->place_user(
data);
263 side_assigned =
true;
269 if(side_taken < 0 && !side_assigned) {
271 if(side->available_for_user(username) ||
273 side->place_user(
data);
275 side_assigned =
true;
284 if(user_side->player_id() == username && !user_side->previous_save_id().empty()) {
286 if(side->player_id().empty() && side->previous_save_id() == user_side->previous_save_id()) {
287 side->place_user(
data);
294 bool connect_engine::sides_available()
const
297 if(side->available_for_user()) {
305 void connect_engine::update_level()
307 DBG_MP <<
"updating level";
312 scenario().add_child(
"side", side->new_config());
316 void connect_engine::update_and_send_diff()
318 config old_level = level_;
324 scenario_diff.
add_child(
"scenario_diff", std::move(diff));
329 bool connect_engine::can_start_game()
const
331 if(side_engines_.empty()) {
337 if(!side->ready_for_start()) {
338 const int side_num = side->index() + 1;
339 DBG_MP <<
"not all sides are ready, side " <<
340 side_num <<
" not ready";
346 DBG_MP <<
"all sides are ready";
355 if(side->controller() !=
CNTR_EMPTY && side->allow_player()) {
363 std::multimap<std::string, config> side_engine::get_side_children()
365 std::multimap<std::string, config> children;
367 for(
const std::string& to_swap : children_to_swap) {
369 children.emplace(to_swap, child);
376 void side_engine::set_side_children(std::multimap<std::string, config> children)
378 for(
const std::string& children_to_remove : children_to_swap) {
379 cfg_.clear_children(children_to_remove);
382 for(std::pair<std::string, config> child_map : children) {
383 cfg_.add_child(child_map.first, child_map.second);
387 void connect_engine::start_game()
389 DBG_MP <<
"starting a new game";
395 std::vector<std::string> avoid_faction_ids;
398 if(params_.mode != random_faction_mode::type::independent) {
400 if(!side2->flg().is_random_faction()) {
401 switch(params_.mode) {
402 case random_faction_mode::type::no_mirror:
403 avoid_faction_ids.push_back(side2->flg().current_faction()[
"id"].str());
405 case random_faction_mode::type::no_ally_mirror:
406 if(side2->team() == side->team()) {
407 avoid_faction_ids.push_back(side2->flg().current_faction()[
"id"].str());
416 side->resolve_random(rng, avoid_faction_ids);
421 if(state_.mp_settings().shuffle_sides && !force_lock_settings_ && !(level_.has_child(
"snapshot") && level_.mandatory_child(
"snapshot").has_child(
"side"))) {
424 std::vector<int> playable_sides;
426 if(side->allow_player() && side->allow_shuffle()) {
427 playable_sides.push_back(side->index());
432 for(
int i = playable_sides.size();
i > 1;
i--) {
434 const int i_side = playable_sides[
i - 1];
436 if(i_side == j_side)
continue;
439 std::swap(side_engines_[j_side], side_engines_[i_side]);
442 std::multimap<std::string, config> tmp_side_children = side_engines_[j_side]->get_side_children();
443 side_engines_[j_side]->set_side_children(side_engines_[i_side]->get_side_children());
444 side_engines_[i_side]->set_side_children(tmp_side_children);
447 std::swap(side_engines_[j_side]->index_, side_engines_[i_side]->index_);
448 std::swap(side_engines_[j_side]->team_, side_engines_[i_side]->team_);
453 config lock(
"stop_updates");
456 update_and_send_diff();
458 save_reserved_sides_information();
468 DBG_MP <<
"starting a new game in commandline mode";
479 if(side_num == num) {
480 if(std::find_if(era_factions_.begin(), era_factions_.end(),
481 [fid = faction_id](
const config* faction) { return (*faction)[
"id"] == fid; })
482 != era_factions_.end()
484 DBG_MP <<
"\tsetting side " << side_num <<
"\tfaction: " << faction_id;
485 side->set_faction_commandline(faction_id);
487 ERR_MP <<
"failed to set side " << side_num <<
" to faction " << faction_id;
496 if(side_num == num) {
497 DBG_MP <<
"\tsetting side " << side_num <<
"\tfaction: " << faction_id;
498 side->set_controller_commandline(faction_id);
505 std::string ai_algorithm =
game_config.mandatory_child(
"ais")[
"default_ai_algorithm"].str();
506 side->set_ai_algorithm(ai_algorithm);
510 if(side_num == num) {
511 DBG_MP <<
"\tsetting side " << side_num <<
"\tfaction: " << faction_id;
512 side->set_ai_algorithm(faction_id);
519 side->resolve_random(rng);
522 update_and_send_diff();
533 if(side_num == side[
"side"].to_unsigned()) {
534 DBG_MP <<
"\tsetting side " << side[
"side"] <<
"\tai_config: " << faction_id;
535 side[
"ai_config"] = faction_id;
550 for(
const auto& [side_num, pname, pvalue] : *cmdline_opts.
multiplayer_parm) {
551 if(side_num == side[
"side"].to_unsigned()) {
552 DBG_MP <<
"\tsetting side " << side[
"side"] <<
" " << pname <<
": " << pvalue;
553 side[pname] = pvalue;
559 save_reserved_sides_information();
566 void connect_engine::leave_game()
568 DBG_MP <<
"leaving the game";
573 std::pair<bool, bool> connect_engine::process_network_data(
const config&
data)
575 std::pair<bool, bool> result(
false,
true);
577 if(
data.has_child(
"leave_game")) {
583 if(
auto side_drop =
data.optional_child(
"side_drop")) {
584 unsigned side_index = side_drop[
"side_num"].to_int() - 1;
586 if(side_index < side_engines_.size()) {
590 connected_users_rw().erase(side_to_drop->player_id());
591 update_side_controller_options();
593 side_to_drop->reset();
595 update_and_send_diff();
602 if(!
data[
"side"].empty()) {
603 unsigned side_taken =
data[
"side"].to_int() - 1;
606 const std::string name =
data[
"name"];
609 response[
"failed"] =
true;
612 ERR_CF <<
"ERROR: No username provided with the side.";
617 if(connected_users().find(name) != connected_users().end()) {
620 if(find_user_side_index_by_id(name) != -1) {
622 response[
"failed"] =
true;
623 response[
"message"] =
"The nickname '" + name +
624 "' is already in use.";
629 connected_users_rw().erase(name);
630 update_side_controller_options();
632 observer_quit.
add_child(
"observer_quit")[
"name"] = name;
638 if(side_taken < side_engines_.size()) {
639 if(!side_engines_[side_taken]->available_for_user(name)) {
644 if(
s->available_for_user()) {
651 if(side_taken >= side_engines_.size()) {
653 response[
"failed"] =
true;
658 kick[
"username"] =
data[
"name"];
661 update_and_send_diff();
663 ERR_CF <<
"ERROR: Couldn't assign a side to '" <<
670 LOG_CF <<
"client has taken a valid position";
672 import_user(
data,
false, side_taken);
673 update_and_send_diff();
676 side_engines_[side_taken]->set_waiting_to_choose_status(side_engines_[side_taken]->allow_changes());
677 LOG_MP <<
"waiting to choose status = " << side_engines_[side_taken]->allow_changes();
678 result.second =
false;
680 LOG_NW <<
"sent player data";
682 ERR_CF <<
"tried to take illegal side: " << side_taken;
685 response[
"failed"] =
true;
690 if(
auto change_faction =
data.optional_child(
"change_faction")) {
691 int side_taken = find_user_side_index_by_id(change_faction[
"name"]);
692 if(side_taken != -1 || !first_scenario_) {
693 import_user(*change_faction,
false, side_taken);
694 update_and_send_diff();
700 update_and_send_diff();
703 if(
auto observer =
data.optional_child(
"observer_quit")) {
704 const std::string& observer_name =
observer[
"name"];
706 if(connected_users().find(observer_name) != connected_users().end()) {
707 connected_users_rw().erase(observer_name);
708 update_side_controller_options();
712 if(find_user_side_index_by_id(observer_name) != -1) {
713 update_and_send_diff();
721 int connect_engine::find_user_side_index_by_id(
const std::string&
id)
const
725 if(side->player_id() ==
id) {
732 if(
i >= side_engines_.size()) {
739 void connect_engine::send_level_data()
const
742 if(first_scenario_) {
745 "name", params_.name,
746 "password", params_.password,
748 "auto_hosted",
false,
754 next_level.
add_child(
"store_next_scenario", level_);
759 void connect_engine::save_reserved_sides_information()
763 std::map<std::string, std::string> side_users =
utils::map_split(level_.child_or_empty(
"multiplayer")[
"side_users"]);
765 const std::string& save_id = side->save_id();
766 const std::string& player_id = side->player_id();
767 if(!save_id.empty() && !player_id.empty()) {
768 side_users[save_id] = player_id;
772 level_.mandatory_child(
"multiplayer")[
"side_users"] =
utils::join_map(side_users);
775 void connect_engine::load_previous_sides_users()
777 std::map<std::string, std::string> side_users =
utils::map_split(level_.mandatory_child(
"multiplayer")[
"side_users"]);
778 std::set<std::string>
names;
780 const std::string& save_id = side->previous_save_id();
781 if(side_users.find(save_id) != side_users.end()) {
782 side->set_reserved_for(side_users[save_id]);
786 names.insert(side_users[save_id]);
789 side->update_controller_options();
794 for(
const std::string& name :
names)
796 if(connected_users().find(name) != connected_users().end() || !mp_metadata_) {
797 import_user(name,
false);
802 void connect_engine::update_side_controller_options()
805 side->update_controller_options();
809 const std::set<std::string>& connect_engine::connected_users()
const
812 return mp_metadata_->connected_players;
815 static std::set<std::string> empty;
819 std::set<std::string>& connect_engine::connected_users_rw()
821 assert(mp_metadata_);
822 return mp_metadata_->connected_players;
827 , parent_(parent_engine)
829 , current_controller_index_(0)
830 , controller_options_()
831 , allow_player_(cfg[
"allow_player"].to_bool(true))
832 , controller_lock_(cfg[
"controller_lock"].to_bool(parent_.force_lock_settings_) && parent_.params_.
use_map_settings)
836 , gold_(cfg[
"gold"].to_int(100))
837 , income_(cfg[
"income"])
838 , reserved_for_(cfg[
"current_player"])
841 , chose_random_(cfg[
"chose_random"].to_bool(false))
842 , disallow_shuffle_(cfg[
"disallow_shuffle"].to_bool(false))
844 , 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))
845 , waiting_to_choose_faction_(allow_changes_)
848 , color_id_(color_options_.
at(color_))
854 "type",
cfg_[
"type"],
855 "gender",
cfg_[
"gender"],
856 "faction",
cfg_[
"faction"],
857 "recruit",
cfg_[
"recruit"],
861 ERR_CF <<
"found invalid side=" <<
cfg_[
"side"].to_int(
index_ + 1) <<
" in definition of side number " <<
index_ + 1;
867 const std::size_t side_cntr_index =
cfg_[
"controller"].to_int(-1) - 1;
874 ERR_MP <<
"controller=<number> is deperecated";
877 if(
cfg_[
"controller"] != side_controller::human &&
cfg_[
"controller"] != side_controller::ai &&
cfg_[
"controller"] != side_controller::none) {
886 cfg_[
"controller"] = side_controller::ai;
889 if(
cfg_[
"controller"] == side_controller::none) {
891 }
else if(
cfg_[
"controller"] == side_controller::ai) {
905 unsigned team_name_index = 0;
907 if(
data.team_name ==
cfg[
"team_name"]) {
917 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!";
919 team_ = team_name_index;
925 if(!given_color.empty()) {
950 return N_(
"Anonymous player");
955 return N_(
"Computer Player");
973 LOG_MP <<
"side_engine::new_config: side=" <<
index_ + 1 <<
" faction=" << faction[
"id"] <<
" recruit=" << faction[
"recruit"];
974 res[
"faction_name"] = faction[
"name"];
975 res[
"faction"] = faction[
"id"];
976 faction.
remove_attributes(
"id",
"name",
"image",
"gender",
"type",
"description");
995 res[
"user_description"] =
t_string(desc,
"wesnoth");
997 desc =
VGETTEXT(
"$playername $side", {
998 {
"playername",
_(desc.c_str())},
999 {
"side", res[
"side"].str()}
1005 if(res[
"name"].str().
empty() && !desc.empty()) {
1037 res[
"current_player"] = desc;
1057 leader = &side_unit;
1061 std::string leader_id = (*leader)[
"id"];
1064 if(!leader_id.empty()) {
1065 (*leader)[
"id"] = leader_id;
1077 LOG_MP <<
"side_engine::new_config: side=" <<
index_ + 1 <<
" type=" << (*leader)[
"type"] <<
" gender=" << (*leader)[
"gender"];
1080 (*leader)[
"type"] =
"null";
1081 (*leader)[
"gender"] =
"null";
1126 res[
"gold"] =
gold_;
1133 res[
"name"] =
cfg_[
"name"];
1136 res[
"user_description"] =
cfg_[
"user_description"];
1222 data[
"name"] = name;
1232 if(
data[
"change_faction"].to_bool() && contains_selection) {
1334 const std::string& name,
const std::string& controller_value)
std::vector< std::string > names
static void add_mod_ai_from_config(config::const_child_itors configs)
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)
std::optional< std::string > multiplayer_turns
Non-empty if –turns was given on the command line.
std::optional< std::vector< std::pair< unsigned int, std::string > > > multiplayer_controller
Non-empty if –controller was given on the command line.
std::optional< std::vector< std::pair< unsigned int, std::string > > > multiplayer_algorithm
Non-empty if –algorithm was given on the command line.
std::optional< std::vector< std::tuple< unsigned int, std::string, std::string > > > multiplayer_parm
Non-empty if –parm was given on the command line.
std::optional< std::vector< std::pair< unsigned int, std::string > > > multiplayer_side
Non-empty if –side was given on the command line.
std::optional< std::vector< std::pair< unsigned int, std::string > > > multiplayer_ai_config
Non-empty if –ai-config was given on the command line.
bool multiplayer_ignore_map_settings
True if –ignore-map-settings was given at 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)
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_attribute(config_key_type key) const
child_itors child_range(config_key_type 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)
Euivalent 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_
std::vector< side_engine_ptr > & side_engines()
const ng::controller default_controller_
config * current_config()
bool force_lock_settings_
const std::set< std::string > & connected_users() const
const mp_game_settings & params_
mp_game_metadata * mp_metadata_
connect_engine(saved_game &state, const bool first_scenario, mp_game_metadata *metadata)
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)
const config * default_leader_cfg() const
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 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()
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_
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
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
const std::string get_ignored_delim()
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 index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
std::size_t size(const std::string &str)
Length in characters of 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=":")
saved_game_mode::type saved_game
The base template for associating string values with enum values.
static map_location::DIRECTION s