The Battle for Wesnoth  1.13.4+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 - 2016 by Joerg Hinrichs <[email protected]>
3  wesnoth playlevel Copyright (C) 2003 by David White <[email protected]>
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_scenario(),
37  mp_scenario_name(),
38  mp_campaign(),
39  active_mods(),
40  side_users(),
41  show_connect(true),
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(false),
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_scenario(cfg["mp_scenario"].str())
70  , mp_scenario_name(cfg["mp_scenario_name"].str())
71  , mp_campaign(cfg["mp_campaign"].str())
72  , active_mods(utils::split(cfg["active_mods"], ','))
73  , side_users(utils::map_split(cfg["side_users"]))
74  , show_connect(cfg["show_connect"].to_bool(true))
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.insert(std::make_pair(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_scenario"] = mp_scenario;
111  cfg["mp_scenario_name"] = mp_scenario_name;
112  cfg["mp_campaign"] = mp_campaign;
113  cfg["active_mods"] = utils::join(active_mods, ",");
114  cfg["side_users"] = utils::join_map(side_users);
115  cfg["show_connect"] = show_connect;
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  typedef std::map<std::string,addon_version_info>::value_type ttt;
137  for (const ttt & 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 {
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 
168  if (cfg["id"].empty()) {
169  WRN_NG << "Tried to add add-on metadata to a game, missing mandatory id field... skipping.\n" << cfg.debug() << "\n";
170  return;
171  }
172 
174 
175  // Check if this add-on already has an entry as a dependency for this scenario. If so, try to reconcile their version info,
176  // by taking the larger of the min versions. The version should be the same for all WML from the same add-on...
178  if (it != addons.end()) {
179  addon_version_info & addon = it->second;
180 
181  if (new_data.version) {
182  if (!addon.version || (*addon.version != *new_data.version)) {
183  WRN_NG << "Addon version data mismatch -- not all local WML has same version of '" << cfg["id"].str() << "' addon.\n";
184  }
185  }
186  if (addon.version && !new_data.version) {
187  WRN_NG << "Addon version data mismatch -- not all local WML has same version of '" << cfg["id"].str() << "' addon.\n";
188  }
189  if (new_data.min_version) {
190  if (!addon.min_version || (*new_data.min_version > *addon.min_version)) {
191  addon.min_version = *new_data.min_version;
192  }
193  }
194  } else {
195  // Didn't find this addon-id in the map, so make a new entry.
196  addons.insert(std::make_pair(cfg["id"].str(), new_data));
197  }
198 }
child_itors child_range(const std::string &key)
Definition: config.cpp:613
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()
#define WRN_NG
int village_support
Definition: game_config.cpp:39
static lg::log_domain log_engine("engine")
config to_config() const
const GLfloat * c
Definition: glew.h:12741
std::string mp_campaign
std::map< std::string, std::string > map_split(std::string const &val, char major, char minor, int flags, std::string const &default_value)
Splits a string based on two separators into a map.
boost::optional< version_info > min_version
std::string debug() const
Definition: config.cpp:1438
std::string mp_scenario_name
const config & options()
std::vector< std::string > active_mods
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:7319
void update_addon_requirements(const config &addon_cfg)
config & add_child(const std::string &key)
Definition: config.cpp:743
bool registered_users_only()
GLfloat GLfloat p
Definition: glew.h:12766
std::string join(T const &v, const std::string &s=",")
Generates a new string joining container items in a list.
std::map< std::string, addon_version_info > addons
std::map< std::string, std::string > side_users
GLuint const GLchar * name
Definition: glew.h:1782
Standard logging facilities (interface).
std::vector< std::string > split(std::string const &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
std::string::const_iterator iterator
Definition: tokenizer.hpp:21
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
bool random_start_time()
const std::string version
Definition: game_config.cpp:48