The Battle for Wesnoth  1.13.11+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 http://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"
23 #include "formula/string_utils.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(false),
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_bool())
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["shuffle_sides"] = shuffle_sides;
132  cfg["random_faction_mode"] = random_faction_mode;
133  cfg["savegame"] = saved_game;
134  cfg.add_child("options", options);
135 
136  for(auto& p : addons) {
137  config & c = cfg.add_child("addon");
138  p.second.write(c);
139  c["id"] = p.first;
140  }
141 
142  return cfg;
143 }
144 
146  : version()
147  , min_version()
148  , name(cfg["name"])
149 {
150  if (!cfg["version"].empty()) {
151  version = cfg["version"].str();
152  }
153  if (!cfg["min_version"].empty()) {
154  min_version = cfg["min_version"].str();
155  }
156 }
157 
159  if (version) {
160  cfg["version"] = *version;
161  }
162  if (min_version) {
163  cfg["min_version"] = *min_version;
164  }
165 
166  cfg["name"] = name;
167 }
168 
170  if (cfg["id"].empty()) {
171  WRN_NG << "Tried to add add-on metadata to a game, missing mandatory id field... skipping.\n" << cfg.debug() << "\n";
172  return;
173  }
174 
176 
177  // Check if this add-on already has an entry as a dependency for this scenario. If so, try to reconcile their version info,
178  // by taking the larger of the min versions. The version should be the same for all WML from the same add-on...
180  if (it != addons.end()) {
181  addon_version_info & addon = it->second;
182 
183  if (new_data.version) {
184  if (!addon.version || (*addon.version != *new_data.version)) {
185  WRN_NG << "Addon version data mismatch -- not all local WML has same version of '" << cfg["id"].str() << "' addon.\n";
186  }
187  }
188  if (addon.version && !new_data.version) {
189  WRN_NG << "Addon version data mismatch -- not all local WML has same version of '" << cfg["id"].str() << "' addon.\n";
190  }
191  if (new_data.min_version) {
192  if (!addon.min_version || (*new_data.min_version > *addon.min_version)) {
193  addon.min_version = *new_data.min_version;
194  }
195  }
196  } else {
197  // Didn't find this addon-id in the map, so make a new entry.
198  addons.emplace(cfg["id"].str(), new_data);
199  }
200 }
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:485
#define WRN_NG
int village_support
Definition: game_config.cpp:72
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:690
config to_config() const
#define a
std::string mp_campaign
child_itors child_range(config_key_type key)
Definition: config.cpp:360
bool shuffle_sides()
Definition: game.cpp:475
boost::optional< version_info > min_version
std::string mp_era_name
std::string debug() const
Definition: config.cpp:1209
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
const config & options()
Definition: game.cpp:570
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:465
int village_gold()
Definition: game.cpp:670
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:493
std::map< std::string, std::string > side_users
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.
config & add_child(config_key_type key)
Definition: config.cpp:473
bool allow_observers()
Definition: game.cpp:455
Standard logging facilities (interface).
static const char * name(const std::vector< SDL_Joystick * > &joysticks, const size_t index)
Definition: joystick.cpp:48
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
mock_char c
bool random_start_time()
Definition: game.cpp:530
const std::string version
Definition: game_config.cpp:39
std::string::const_iterator iterator
Definition: tokenizer.hpp:24