The Battle for Wesnoth  1.15.2+dev
mp_game_settings.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2018 by Joerg Hinrichs <joerg.hinrichs@alice-dsl.de>
3  wesnoth playlevel Copyright (C) 2003 by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * @file
18  * Container for multiplayer game-creation parameters.
19  */
20 
21 #include "log.hpp"
22 #include "mp_game_settings.hpp"
24 
25 static lg::log_domain log_engine("engine");
26 #define ERR_NG LOG_STREAM(err, log_engine)
27 #define WRN_NG LOG_STREAM(warn, log_engine)
28 #define LOG_NG LOG_STREAM(info, log_engine)
29 #define DBG_NG LOG_STREAM(debug, log_engine)
30 
32  name(),
33  password(),
34  hash(),
35  mp_era(),
36  mp_era_name(),
37  mp_scenario(),
38  mp_scenario_name(),
39  mp_campaign(),
40  active_mods(),
41  side_users(),
42  num_turns(0),
43  village_gold(0),
44  village_support(1),
45  xp_modifier(100),
46  mp_countdown_init_time(0),
47  mp_countdown_reservoir_time(0),
48  mp_countdown_turn_bonus(0),
49  mp_countdown_action_bonus(0),
50  mp_countdown(false),
51  use_map_settings(false),
52  random_start_time(false),
53  fog_game(false),
54  shroud_game(false),
55  allow_observers(true),
56  registered_users_only(false),
57  shuffle_sides(false),
58  saved_game(SAVED_GAME_MODE::NONE),
59  random_faction_mode(RANDOM_FACTION_MODE::DEFAULT),
60  options(),
61  addons()
62 {}
63 
65  : name(cfg["scenario"].str())
66  , password()
67  , hash(cfg["hash"].str())
68  , mp_era(cfg["mp_era"].str())
69  , mp_era_name(cfg["mp_era_name"].str())
70  , mp_scenario(cfg["mp_scenario"].str())
71  , mp_scenario_name(cfg["mp_scenario_name"].str())
72  , mp_campaign(cfg["mp_campaign"].str())
73  , active_mods(utils::split(cfg["active_mods"], ','))
74  , side_users(utils::map_split(cfg["side_users"]))
75  , num_turns(cfg["mp_num_turns"])
76  , village_gold(cfg["mp_village_gold"])
77  , village_support(cfg["mp_village_support"])
78  , xp_modifier(cfg["experience_modifier"].to_int(100))
79  , mp_countdown_init_time(cfg["mp_countdown_init_time"])
80  , mp_countdown_reservoir_time(cfg["mp_countdown_reservoir_time"])
81  , mp_countdown_turn_bonus(cfg["mp_countdown_turn_bonus"])
82  , mp_countdown_action_bonus(cfg["mp_countdown_action_bonus"])
83  , mp_countdown(cfg["mp_countdown"].to_bool())
84  , use_map_settings(cfg["mp_use_map_settings"].to_bool())
85  , random_start_time(cfg["mp_random_start_time"].to_bool())
86  , fog_game(cfg["mp_fog"].to_bool())
87  , shroud_game(cfg["mp_shroud"].to_bool())
88  , allow_observers(cfg["observer"].to_bool())
89  , registered_users_only(cfg["registered_users_only"].to_bool())
90  , shuffle_sides(cfg["shuffle_sides"].to_bool())
91  , saved_game(cfg["savegame"].to_enum<SAVED_GAME_MODE>(SAVED_GAME_MODE::NONE))
92  , random_faction_mode(cfg["random_faction_mode"].to_enum<RANDOM_FACTION_MODE>(RANDOM_FACTION_MODE::DEFAULT))
93  , options(cfg.child_or_empty("options"))
94  , addons()
95 {
96  for (const config & a : cfg.child_range("addon")) {
97  if (!a["id"].empty()) {
98  addons.emplace(a["id"].str(), addon_version_info(a));
99  }
100  }
101 }
102 
104 {
105  config cfg;
106 
107  cfg["scenario"] = name;
108  cfg["hash"] = hash;
109  cfg["mp_era"] = mp_era;
110  cfg["mp_era_name"] = mp_era_name;
111  cfg["mp_scenario"] = mp_scenario;
112  cfg["mp_scenario_name"] = mp_scenario_name;
113  cfg["mp_campaign"] = mp_campaign;
114  cfg["active_mods"] = utils::join(active_mods, ",");
115  cfg["side_users"] = utils::join_map(side_users);
116  cfg["experience_modifier"] = xp_modifier;
117  cfg["mp_countdown"] = mp_countdown;
118  cfg["mp_countdown_init_time"] = mp_countdown_init_time;
119  cfg["mp_countdown_turn_bonus"] = mp_countdown_turn_bonus;
120  cfg["mp_countdown_reservoir_time"] = mp_countdown_reservoir_time;
121  cfg["mp_countdown_action_bonus"] = mp_countdown_action_bonus;
122  cfg["mp_num_turns"] = num_turns;
123  cfg["mp_village_gold"] = village_gold;
124  cfg["mp_village_support"] = village_support;
125  cfg["mp_fog"] = fog_game;
126  cfg["mp_shroud"] = shroud_game;
127  cfg["mp_use_map_settings"] = use_map_settings;
128  cfg["mp_random_start_time"] = random_start_time;
129  cfg["observer"] = allow_observers;
130  cfg["registered_users_only"] = registered_users_only;
131  cfg["private_replay"] = private_replay;
132  cfg["shuffle_sides"] = shuffle_sides;
133  cfg["random_faction_mode"] = random_faction_mode;
134  cfg["savegame"] = saved_game;
135  cfg.add_child("options", options);
136 
137  for(auto& p : addons) {
138  config & c = cfg.add_child("addon");
139  p.second.write(c);
140  c["id"] = p.first;
141  }
142 
143  return cfg;
144 }
145 
147  : version()
148  , min_version()
149  , name(cfg["name"])
150 {
151  if (!cfg["version"].empty()) {
152  version = cfg["version"].str();
153  }
154  if (!cfg["min_version"].empty()) {
155  min_version = cfg["min_version"].str();
156  }
157 }
158 
160  if (version) {
161  cfg["version"] = *version;
162  }
163  if (min_version) {
164  cfg["min_version"] = *min_version;
165  }
166 
167  cfg["name"] = name;
168 }
169 
171  if (cfg["id"].empty()) {
172  WRN_NG << "Tried to add add-on metadata to a game, missing mandatory id field... skipping.\n" << cfg.debug() << "\n";
173  return;
174  }
175 
177 
178  // Check if this add-on already has an entry as a dependency for this scenario. If so, try to reconcile their version info,
179  // by taking the larger of the min versions. The version should be the same for all WML from the same add-on...
181  if (it != addons.end()) {
182  addon_version_info & addon = it->second;
183 
184  if (new_data.version) {
185  if (!addon.version || (*addon.version != *new_data.version)) {
186  WRN_NG << "Addon version data mismatch -- not all local WML has same version of '" << cfg["id"].str() << "' addon.\n";
187  }
188  }
189  if (addon.version && !new_data.version) {
190  WRN_NG << "Addon version data mismatch -- not all local WML has same version of '" << cfg["id"].str() << "' addon.\n";
191  }
192  if (new_data.min_version) {
193  if (!addon.min_version || (*new_data.min_version > *addon.min_version)) {
194  addon.min_version = *new_data.min_version;
195  }
196  }
197  } else {
198  // Didn't find this addon-id in the map, so make a new entry.
199  addons.emplace(cfg["id"].str(), new_data);
200  }
201 }
boost::optional< version_info > version
std::string mp_scenario
std::string join_map(const T &v, const std::string &major=",", const std::string &minor=":")
std::string random_faction_mode()
Definition: game.cpp:508
#define WRN_NG
int village_support
Definition: game_config.cpp:55
static lg::log_domain log_engine("engine")
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
int xp_modifier()
Definition: game.cpp:713
#define a
std::string mp_campaign
child_itors child_range(config_key_type key)
Definition: config.cpp:362
bool shuffle_sides()
Definition: game.cpp:498
boost::optional< version_info > min_version
std::string mp_era_name
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::vector< std::string > split(const std::string &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
std::string mp_scenario_name
config to_config() const
const config & options()
Definition: game.cpp:593
std::vector< std::string > active_mods
void update_addon_requirements(const config &addon_cfg)
Takes a config with addon metadata (id, name, version, min_version) and adds it as a requirement for ...
bool registered_users_only()
Definition: game.cpp:488
int village_gold()
Definition: game.cpp:693
Default, unset return value.
Definition: retval.hpp:31
mock_party p
std::map< std::string, addon_version_info > addons
std::string password(const std::string &server, const std::string &login)
bool use_map_settings()
Definition: game.cpp:516
std::map< std::string, std::string > side_users
config & add_child(config_key_type key)
Definition: config.cpp:476
bool allow_observers()
Definition: game.cpp:478
Standard logging facilities (interface).
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
mock_char c
bool random_start_time()
Definition: game.cpp:553
std::string::const_iterator iterator
Definition: tokenizer.hpp:24
std::string debug() const
Definition: config.cpp:1277