The Battle for Wesnoth  1.15.0-dev
game_config.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "game_config.hpp"
16 
17 #include "color_range.hpp"
18 #include "config.hpp"
19 #include "gettext.hpp"
20 #include "log.hpp"
21 #include "utils/general.hpp"
22 #include "utils/math.hpp"
23 #include "version.hpp"
24 #include "wesconfig.h"
26 
27 static lg::log_domain log_engine("engine");
28 #define LOG_NG LOG_STREAM(info, log_engine)
29 #define ERR_NG LOG_STREAM(err, log_engine)
30 
31 namespace game_config
32 {
33 //
34 // Path info
35 //
36 #ifdef WESNOTH_PATH
37 std::string path = WESNOTH_PATH;
38 #else
39 std::string path = "";
40 #endif
41 
42 #ifdef DEFAULT_PREFS_PATH
43 std::string default_preferences_path = DEFAULT_PREFS_PATH;
44 #else
45 std::string default_preferences_path = "";
46 #endif
47 
48 std::string wesnoth_program_dir;
49 
50 //
51 // Gameplay constants
52 //
53 int base_income = 2;
56 int recall_cost = 20;
58 
59 int poison_amount = 8;
61 
63 
64 //
65 // Terrain-related constants
66 //
67 unsigned int tile_size = 72;
68 
69 std::string default_terrain;
71 
72 std::vector<unsigned int> zoom_levels {36, 72, 144};
73 
74 //
75 // Display scale constants
76 //
77 double hp_bar_scaling = 0.666;
78 double xp_bar_scaling = 0.5;
79 double hex_brightening = 1.25;
80 
81 //
82 // Misc
83 //
85 
86 unsigned lobby_network_timer = 100;
87 unsigned lobby_refresh = 4000;
88 
89 const std::string observer_team_name = "observer";
90 
91 const std::size_t max_loop = 65536;
92 
93 std::vector<server_info> server_list;
94 
95 //
96 // Gamestate flags
97 //
98 bool
99  debug_impl = false,
100  debug_lua = false,
101  editor = false,
103  mp_debug = false,
104  exit_at_end = false,
105  no_delay = false,
107  no_addons = false;
108 
109 const bool& debug = debug_impl;
110 
111 void set_debug(bool new_debug) {
112  if(debug_impl && !new_debug) {
113  // Turning debug mode off; decrease deprecation severity
114  int severity;
115  if(lg::get_log_domain_severity("deprecation", severity)) {
116  lg::set_log_domain_severity("deprecation", severity - 2);
117  }
118  } else if(!debug_impl && new_debug) {
119  // Turning debug mode on; increase deprecation severity
120  int severity;
121  if(lg::get_log_domain_severity("deprecation", severity)) {
122  lg::set_log_domain_severity("deprecation", severity + 2);
123  }
124  }
125  debug_impl = new_debug;
126 }
127 
128 //
129 // Orb display flahs
130 //
131 bool
137 
138 //
139 // Music constants
140 //
142 
143 std::vector<std::string> default_defeat_music;
144 std::vector<std::string> default_victory_music;
145 
146 //
147 // Color info
148 //
149 std::string flag_rgb, unit_rgb;
150 
151 std::vector<color_t> red_green_scale;
152 std::vector<color_t> red_green_scale_text;
153 
154 static std::vector<color_t> blue_white_scale;
155 static std::vector<color_t> blue_white_scale_text;
156 
157 std::map<std::string, color_range> team_rgb_range;
158 std::map<std::string, t_string> team_rgb_name;
159 
160 std::map<std::string, std::vector<color_t>> team_rgb_colors;
161 
162 std::vector<std::string> default_colors;
163 
164 namespace colors {
165 
166 std::string
173 
174 } // colors
175 
176 //
177 // Image constants
178 //
179 std::vector<std::string> foot_speed_prefix;
180 
182 std::string foot_teleport_exit;
183 
184 namespace images {
185 
186 std::string
189  game_logo,
194  // orbs and hp/xp bar
195  orb,
196  energy,
197  // flags
198  flag,
199  flag_icon,
200  // hex overlay
201  terrain_mask,
202  grid_top,
203  grid_bottom,
204  mouseover,
205  selected,
206  editor_brush,
207  unreachable,
208  linger,
209  // GUI elements
210  observer,
211  tod_bright,
212  tod_dark,
213  ///@todo de-hardcode this
214  selected_menu = "buttons/radiobox-pressed.png",
215  deselected_menu = "buttons/radiobox.png",
216  checked_menu = "buttons/checkbox-pressed.png",
217  unchecked_menu = "buttons/checkbox.png",
218  wml_menu = "buttons/WML-custom.png",
219  level,
220  ellipsis,
221  missing,
222  // notifications icon
223  app_icon = "images/icons/icon-game.png";
224 
225 } //images
226 
227 //
228 // Sound constants
229 //
230 namespace sounds {
231 
232 std::string
233  turn_bell = "bell.wav",
234  timer_bell = "timer.wav",
235  public_message = "chat-[1~3].ogg",
236  private_message = "chat-highlight.ogg",
237  friend_message = "chat-friend.ogg",
238  server_message = "receive.wav",
239  player_joins = "arrive.wav",
240  player_leaves = "leave.wav",
241  game_user_arrive = "join.wav",
242  game_user_leave = "leave.wav",
243  ready_for_start = "bell.wav",
244  game_has_begun = "gamestart.ogg";
245 
246 namespace status {
247 
248 std::string
249  poisoned = "poison.ogg",
250  slowed = "slowed.wav",
251  petrified = "petrified.ogg";
252 
253 } // status
254 
255 } // sounds
256 
257 static void add_color_info(const config& v, bool build_defaults);
258 void add_color_info(const config& v)
259 {
260  add_color_info(v, false);
261 }
262 
263 void load_config(const config &v)
264 {
265  base_income = v["base_income"].to_int(2);
266  village_income = v["village_income"].to_int(1);
267  village_support = v["village_support"].to_int(1);
268  poison_amount = v["poison_amount"].to_int(8);
269  rest_heal_amount = v["rest_heal_amount"].to_int(2);
270  recall_cost = v["recall_cost"].to_int(20);
271  kill_experience = v["kill_experience"].to_int(8);
272  lobby_refresh = v["lobby_refresh"].to_int(2000);
273  default_terrain = v["default_terrain"].str();
274  tile_size = v["tile_size"].to_int(72);
275 
276  std::vector<std::string> zoom_levels_str = utils::split(v["zoom_levels"]);
277  if(!zoom_levels_str.empty()) {
278  zoom_levels.clear();
279  std::transform(zoom_levels_str.begin(), zoom_levels_str.end(), std::back_inserter(zoom_levels), [](const std::string zoom) {
280  return static_cast<int>(std::stold(zoom) * tile_size);
281  });
282  }
283 
284  title_music = v["title_music"].str();
285  lobby_music = v["lobby_music"].str();
286 
287  default_victory_music = utils::split(v["default_victory_music"].str());
288  default_defeat_music = utils::split(v["default_defeat_music"].str());
289 
290  if(const config& i = v.child("colors")){
291  using namespace game_config::colors;
292 
293  moved_orb_color = i["moved_orb_color"].str();
294  unmoved_orb_color = i["unmoved_orb_color"].str();
295  partial_orb_color = i["partial_orb_color"].str();
296  enemy_orb_color = i["enemy_orb_color"].str();
297  ally_orb_color = i["ally_orb_color"].str();
298  } // colors
299 
300  show_ally_orb = v["show_ally_orb"].to_bool(true);
301  show_enemy_orb = v["show_enemy_orb"].to_bool(false);
302  show_moved_orb = v["show_moved_orb"].to_bool(true);
303  show_partial_orb = v["show_partly_orb"].to_bool(true);
304  show_unmoved_orb = v["show_unmoved_orb"].to_bool(true);
305 
306  if(const config& i = v.child("images")){
307  using namespace game_config::images;
308 
309  game_title = i["game_title"].str();
310  game_title_background = i["game_title_background"].str();
311  game_logo = i["game_logo"].str();
312  game_logo_background = i["game_logo_background"].str();
313 
314  victory_laurel = i["victory_laurel"].str();
315  victory_laurel_hardest = i["victory_laurel_hardest"].str();
316  victory_laurel_easy = i["victory_laurel_easy"].str();
317 
318  orb = i["orb"].str();
319  energy = i["energy"].str();
320 
321  flag = i["flag"].str();
322  flag_icon = i["flag_icon"].str();
323 
324  terrain_mask = i["terrain_mask"].str();
325  grid_top = i["grid_top"].str();
326  grid_bottom = i["grid_bottom"].str();
327  mouseover = i["mouseover"].str();
328  selected = i["selected"].str();
329  editor_brush = i["editor_brush"].str();
330  unreachable = i["unreachable"].str();
331  linger = i["linger"].str();
332 
333  observer = i["observer"].str();
334  tod_bright = i["tod_bright"].str();
335  tod_dark = i["tod_dark"].str();
336  level = i["level"].str();
337  ellipsis = i["ellipsis"].str();
338  missing = i["missing"].str();
339  } // images
340 
341  hp_bar_scaling = v["hp_bar_scaling"].to_double(0.666);
342  xp_bar_scaling = v["xp_bar_scaling"].to_double(0.5);
343  hex_brightening = v["hex_brightening"].to_double(1.25);
344 
345  foot_speed_prefix = utils::split(v["footprint_prefix"]);
346  foot_teleport_enter = v["footprint_teleport_enter"].str();
347  foot_teleport_exit = v["footprint_teleport_exit"].str();
348 
349  shroud_prefix = v["shroud_prefix"].str();
350  fog_prefix = v["fog_prefix"].str();
351 
352  add_color_info(v, true);
353 
354  if(const config::attribute_value* a = v.get("flag_rgb")) {
355  flag_rgb = a->str();
356  }
357 
358  if(const config::attribute_value* a = v.get("unit_rgb")) {
359  unit_rgb = a->str();
360  }
361 
362  const auto parse_config_color_list = [&](
363  const std::string& key,
364  const color_t fallback)->std::vector<color_t>
365  {
366  std::vector<color_t> color_vec;
367 
368  for(const auto& s : utils::split(v[key].str())) {
369  try {
370  color_vec.push_back(color_t::from_hex_string(s));
371  } catch(const std::invalid_argument& e) {
372  ERR_NG << "Error parsing color list '" << key << "'.\n" << e.what() << std::endl;
373  color_vec.push_back(fallback);
374  }
375  }
376 
377  return color_vec;
378  };
379 
380  red_green_scale = parse_config_color_list("red_green_scale", {255, 255, 255});
381  red_green_scale_text = parse_config_color_list("red_green_scale_text", {255, 255, 255});
382  blue_white_scale = parse_config_color_list("blue_white_scale", {0 , 0 , 255});
383  blue_white_scale_text = parse_config_color_list("blue_white_scale_text", {0 , 0 , 255});
384 
385  server_list.clear();
386 
387  for(const config& server : v.child_range("server")) {
388  server_info sinf;
389  sinf.name = server["name"].str();
390  sinf.address = server["address"].str();
391  server_list.push_back(sinf);
392  }
393 
394  if(const config& s = v.child("sounds")) {
395  using namespace game_config::sounds;
396 
397  const auto load_attribute = [](const config& c, const std::string& key, std::string& member) {
398  if(c.has_attribute(key)) {
399  member = c[key].str();
400  }
401  };
402 
403  load_attribute(s, "turn_bell", turn_bell);
404  load_attribute(s, "timer_bell", timer_bell);
405  load_attribute(s, "public_message", public_message);
406  load_attribute(s, "private_message", private_message);
407  load_attribute(s, "friend_message", friend_message);
408  load_attribute(s, "server_message", server_message);
409  load_attribute(s, "player_joins", player_joins);
410  load_attribute(s, "player_leaves", player_leaves);
411  load_attribute(s, "game_user_arrive", game_user_arrive);
412  load_attribute(s, "game_user_leave", game_user_leave);
413  load_attribute(s, "ready_for_start", ready_for_start);
414  load_attribute(s, "game_has_begun", game_has_begun);
415 
416  if(const config & ss = s.child("status")) {
417  using namespace game_config::sounds::status;
418 
419  load_attribute(ss, "poisoned", poisoned);
420  load_attribute(ss, "slowed", slowed);
421  load_attribute(ss, "petrified", petrified);
422  }
423  }
424 }
425 
426 void add_color_info(const config& v, bool build_defaults)
427 {
428  if(build_defaults) {
429  default_colors.clear();
430  }
431 
432  for(const config& teamC : v.child_range("color_range")) {
433  const config::attribute_value* a1 = teamC.get("id"), *a2 = teamC.get("rgb");
434  if(!a1 || !a2) {
435  continue;
436  }
437 
438  std::string id = *a1;
439  std::vector<color_t> temp;
440 
441  for(const auto& s : utils::split(*a2)) {
442  try {
443  temp.push_back(color_t::from_hex_string(s));
444  } catch(const std::invalid_argument&) {
445  std::stringstream ss;
446  ss << "can't parse color string:\n" << teamC.debug() << "\n";
447  throw config::error(ss.str());
448  }
449  }
450 
451  team_rgb_range.emplace(id, color_range(temp));
452  team_rgb_name.emplace(id, teamC["name"].t_str());
453 
454  LOG_NG << "registered color range '" << id << "': " << team_rgb_range[id].debug() << '\n';
455 
456  // Ggenerate palette of same name;
457  std::vector<color_t> tp = palette(team_rgb_range[id]);
458  if(!tp.empty()) {
459  team_rgb_colors.emplace(id, tp);
460  }
461 
462  if(build_defaults && teamC["default"].to_bool()) {
463  default_colors.push_back(*a1);
464  }
465  }
466 
467  for(const config &cp : v.child_range("color_palette")) {
468  for(const config::attribute& rgb : cp.attribute_range()) {
469  std::vector<color_t> temp;
470  for(const auto& s : utils::split(rgb.second)) {
471  try {
472  temp.push_back(color_t::from_hex_string(s));
473  } catch(const std::invalid_argument&) {
474  ERR_NG << "Invalid color in palette: " << s << std::endl;
475  }
476  }
477 
478  team_rgb_colors.emplace(rgb.first, temp);
479  LOG_NG << "registered color palette: " << rgb.first << '\n';
480  }
481  }
482 }
483 
485 {
486  default_colors.clear();
487  team_rgb_colors.clear();
488  team_rgb_name.clear();
489  team_rgb_range.clear();
490 }
491 
492 const color_range& color_info(const std::string& name)
493 {
494  auto i = team_rgb_range.find(name);
495  if(i != team_rgb_range.end()) {
496  return i->second;
497  }
498 
499  std::vector<color_t> temp;
500  for(const auto& s : utils::split(name)) {
501  try {
502  temp.push_back(color_t::from_hex_string(s));
503  } catch(const std::invalid_argument&) {
504  throw config::error(_("Invalid color in range: ") + s);
505  }
506  }
507 
508  team_rgb_range.emplace(name, color_range(temp));
509  return color_info(name);
510 }
511 
512 const std::vector<color_t>& tc_info(const std::string& name)
513 {
514  auto i = team_rgb_colors.find(name);
515  if(i != team_rgb_colors.end()) {
516  return i->second;
517  }
518 
519  std::vector<color_t> temp;
520  for(const auto& s : utils::split(name)) {
521  try {
522  temp.push_back(color_t::from_hex_string(s));
523  } catch(const std::invalid_argument&) {
524  static std::vector<color_t> stv;
525  ERR_NG << "Invalid color in palette: " << s << std::endl;
526  return stv;
527  }
528  }
529 
530  team_rgb_colors.emplace(name, temp);
531  return tc_info(name);
532 }
533 
534 color_t red_to_green(int val, bool for_text)
535 {
536  const std::vector<color_t>& color_scale = for_text ? red_green_scale_text : red_green_scale;
537 
538  val = utils::clamp(val, 0, 100);
539  const int lvl = (color_scale.size() - 1) * val / 100;
540 
541  return color_scale[lvl];
542 }
543 
544 color_t blue_to_white(int val, bool for_text)
545 {
546  const std::vector<color_t>& color_scale = for_text ? blue_white_scale_text : blue_white_scale;
547 
548  val = utils::clamp(val, 0, 100);
549  const int lvl = (color_scale.size() - 1) * val / 100;
550 
551  return color_scale[lvl];
552 }
553 
555 {
556  std::string ret = _("The Battle for Wesnoth") + " - " + revision;
557  return ret;
558 }
559 
560 } // game_config
constexpr const T & clamp(const T &value, const T &min, const T &max)
Definition: general.hpp:31
std::vector< std::string > default_victory_music
std::string game_user_leave
std::string victory_laurel
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:423
std::string tod_dark
bool show_unmoved_orb
double hp_bar_scaling
Definition: game_config.cpp:77
double xp_bar_scaling
Definition: game_config.cpp:78
std::map< std::string, color_range > team_rgb_range
int village_support
Definition: game_config.cpp:55
#define ERR_NG
Definition: game_config.cpp:29
Variant for storing WML attributes.
std::string mouseover
std::vector< unsigned int > zoom_levels
Definition: game_config.cpp:72
std::vector< color_t > palette(const color_range &cr)
Creates a reference color palette from a color range.
Definition: color_range.cpp:83
static l_noret error(LoadState *S, const char *why)
Definition: lundump.cpp:39
color_t blue_to_white(int val, bool for_text)
bool has_attribute(config_key_type key) const
Definition: config.cpp:217
#define a
std::string private_message
std::string game_title
child_itors child_range(config_key_type key)
Definition: config.cpp:366
std::string selected_menu
static std::vector< color_t > blue_white_scale
std::string lobby_music
attribute_map::value_type attribute
Definition: config.hpp:226
std::string unreachable
const attribute_value * get(config_key_type key) const
Returns a pointer to the attribute with the given key or nullptr if it does not exist.
Definition: config.cpp:695
std::string unit_rgb
std::string friend_message
std::string get_default_title_string()
std::string unchecked_menu
std::string victory_laurel_easy
std::string enemy_orb_color
const std::size_t max_loop
The maximum number of hexes on a map and items in an array and also used as maximum in wml loops...
Definition: game_config.cpp:91
bool show_enemy_orb
std::string player_joins
Definitions for the interface to Wesnoth Markup Language (WML).
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 address
may include &#39;:&#39; followed by port number
Definition: game_config.hpp:69
void load_config(const config &v)
static void add_color_info(const config &v, bool build_defaults)
int rest_heal_amount
Definition: game_config.cpp:60
bool show_ally_orb
std::vector< std::string > default_defeat_music
std::string terrain_mask
std::string flag_icon
std::string turn_bell
std::string timer_bell
#define LOG_NG
Definition: game_config.cpp:28
std::string ally_orb_color
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:89
std::string deselected_menu
const t_string id
bool get_log_domain_severity(const std::string &name, int &severity)
Definition: log.cpp:142
std::string victory_laurel_hardest
std::vector< color_t > red_green_scale_text
const t_string name
unsigned lobby_network_timer
Definition: game_config.cpp:86
bool disable_autosave
static color_t from_hex_string(const std::string &c)
Creates a new color_t object from a string variable in hex format.
Definition: color.cpp:61
std::string default_color_list
std::string ready_for_start
const int gold_carryover_percentage
Default percentage gold carried over to the next scenario.
Definition: game_config.cpp:62
std::string game_user_arrive
bool ignore_replay_errors
std::string selected
std::string foot_teleport_enter
std::string game_title_background
General math utility functions.
int kill_experience
Definition: game_config.cpp:57
std::string grid_bottom
std::string path
Definition: game_config.cpp:39
std::string game_logo_background
std::string shroud_prefix
Definition: game_config.cpp:70
Manage the empty-palette in the editor.
Definition: action.cpp:29
std::string default_terrain
Definition: game_config.cpp:69
std::string moved_orb_color
std::string flag_rgb
std::string server_message
Some defines: VERSION, PACKAGE, MIN_SAVEGAME_VERSION.
std::size_t i
Definition: function.cpp:933
std::map< std::string, std::vector< color_t > > team_rgb_colors
std::string editor_brush
std::string unmoved_orb_color
const std::string revision
Definition: version.cpp:42
Game configuration data as global variables.
Definition: build_info.cpp:46
std::string public_message
int village_income
Definition: game_config.cpp:54
static map_location::DIRECTION s
void reset_color_info()
A color range definition is made of four reference RGB colors, used for calculating conversions from ...
Definition: color_range.hpp:50
std::vector< color_t > red_green_scale
std::string title_music
bool show_moved_orb
const bool & debug
std::vector< std::string > foot_speed_prefix
std::string observer
bool set_log_domain_severity(const std::string &name, int severity)
Definition: log.cpp:118
std::string player_leaves
std::string foot_teleport_exit
color_t red_to_green(int val, bool for_text)
Return a color corresponding to the value val red for val=0 to green for val=100, passing by yellow...
unsigned int tile_size
Definition: game_config.cpp:67
unsigned lobby_refresh
Definition: game_config.cpp:87
const color_range & color_info(const std::string &name)
Standard logging facilities (interface).
double hex_brightening
Definition: game_config.cpp:79
std::string tod_bright
std::string game_logo
std::string ellipsis
const std::vector< color_t > & tc_info(const std::string &name)
static std::vector< color_t > blue_white_scale_text
#define e
std::string checked_menu
bool show_partial_orb
std::string partial_orb_color
std::vector< std::string > default_colors
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
mock_char c
std::string wml_menu
std::map< std::string, t_string > team_rgb_name
std::string game_has_begun
Interfaces for manipulating version numbers of engine, add-ons, etc.
std::string grid_top
std::string fog_prefix
Definition: game_config.cpp:70
std::vector< server_info > server_list
Definition: game_config.cpp:93
std::string app_icon
std::string wesnoth_program_dir
Definition: game_config.cpp:48
void set_debug(bool new_debug)
static lg::log_domain log_engine("engine")
std::string default_preferences_path
Definition: game_config.cpp:45
const std::string observer_team_name
observer team name used for observer team chat
Definition: game_config.cpp:89
int cache_compression_level
Definition: game_config.cpp:84