The Battle for Wesnoth  1.13.11+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 http://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 "version.hpp"
23 #include "wesconfig.h"
25 #ifdef LOAD_REVISION
26 #include "revision.h"
27 #endif
28 
29 static lg::log_domain log_engine("engine");
30 #define LOG_NG LOG_STREAM(info, log_engine)
31 #define ERR_NG LOG_STREAM(err, log_engine)
32 
33 namespace game_config
34 {
35 
36 //
37 // Path and revision info
38 //
40 
43 const version_info test_version("test");
44 
45 #ifdef REVISION
46 const std::string revision = VERSION " (" REVISION ")";
47 #elif defined(VCS_SHORT_HASH) && defined(VCS_WC_MODIFIED)
48 const std::string revision = std::string(VERSION) + " (" + VCS_SHORT_HASH + (VCS_WC_MODIFIED ? "-Modified" : "-Clean") + ")";
49 #else
50 const std::string revision = VERSION;
51 #endif
52 
53 #ifdef WESNOTH_PATH
54 std::string path = WESNOTH_PATH;
55 #else
56 std::string path = "";
57 #endif
58 
59 #ifdef DEFAULT_PREFS_PATH
60 std::string default_preferences_path = DEFAULT_PREFS_PATH;
61 #else
62 std::string default_preferences_path = "";
63 #endif
64 
66 
67 //
68 // Gameplay constants
69 //
70 int base_income = 2;
73 int recall_cost = 20;
75 
76 int poison_amount = 8;
78 
80 
81 //
82 // Terrain-related constants
83 //
84 unsigned int tile_size = 72;
85 
88 
89 std::vector<unsigned int> zoom_levels {36, 72, 144};
90 
91 //
92 // Display scale constants
93 //
94 double hp_bar_scaling = 0.666;
95 double xp_bar_scaling = 0.5;
96 double hex_brightening = 1.25;
97 
98 //
99 // Misc
100 //
102 
103 unsigned lobby_network_timer = 100;
104 unsigned lobby_refresh = 4000;
105 
106 const std::string observer_team_name = "observer";
107 
108 const size_t max_loop = 65536;
109 
110 std::vector<server_info> server_list;
111 
112 //
113 // Gamestate flags
114 //
115 bool
116  debug = false,
117  debug_lua = false,
118  editor = false,
120  mp_debug = false,
121  exit_at_end = false,
122  no_delay = false,
124  no_addons = false;
125 
126 //
127 // Orb display flahs
128 //
129 bool
135 
136 //
137 // Music constants
138 //
140 
141 std::vector<std::string> default_defeat_music;
142 std::vector<std::string> default_victory_music;
143 
144 //
145 // Color info
146 //
148 
149 std::vector<color_t> red_green_scale;
150 std::vector<color_t> red_green_scale_text;
151 
152 static std::vector<color_t> blue_white_scale;
153 static std::vector<color_t> blue_white_scale_text;
154 
155 std::map<std::string, color_range> team_rgb_range;
156 std::map<std::string, t_string> team_rgb_name;
157 
158 std::map<std::string, std::vector<color_t>> team_rgb_colors;
159 
160 std::vector<std::string> default_colors;
161 
162 namespace colors {
163 
171 
172 } // colors
173 
174 //
175 // Image constants
176 //
177 std::vector<std::string> foot_speed_prefix;
178 
181 
182 namespace images {
183 
187  game_logo,
192  // orbs and hp/xp bar
193  orb,
194  energy,
195  // flags
196  flag,
197  flag_icon,
198  // hex overlay
199  terrain_mask,
200  grid_top,
201  grid_bottom,
202  mouseover,
203  selected,
204  editor_brush,
205  unreachable,
206  linger,
207  // GUI elements
208  observer,
209  tod_bright,
210  tod_dark,
211  ///@todo de-hardcode this
212  selected_menu = "buttons/radiobox-pressed.png",
213  deselected_menu = "buttons/radiobox.png",
214  checked_menu = "buttons/checkbox-pressed.png",
215  unchecked_menu = "buttons/checkbox.png",
216  wml_menu = "buttons/WML-custom.png",
217  level,
218  ellipsis,
219  missing,
220  // notifications icon
221  app_icon = "images/icons/icon-game.png";
222 
223 } //images
224 
225 //
226 // Sound constants
227 //
228 namespace sounds {
229 
231  turn_bell = "bell.wav",
232  timer_bell = "timer.wav",
233  public_message = "chat-[1~3].ogg",
234  private_message = "chat-highlight.ogg",
235  friend_message = "chat-friend.ogg",
236  server_message = "receive.wav",
237  player_joins = "arrive.wav",
238  player_leaves = "leave.wav",
239  game_user_arrive = "join.wav",
240  game_user_leave = "leave.wav",
241  ready_for_start = "bell.wav",
242  game_has_begun = "gamestart.ogg";
243 
244 const std::string
245  button_press = "button.wav",
246  checkbox_release = "checkbox.wav",
247  slider_adjust = "slider.wav",
248  menu_expand = "expand.wav",
249  menu_contract = "contract.wav",
250  menu_select = "select.wav";
251 
252 namespace status {
253 
255  poisoned = "poison.ogg",
256  slowed = "slowed.wav",
257  petrified = "petrified.ogg";
258 
259 } // status
260 
261 } // sounds
262 
263 static void add_color_info(const config& v, bool build_defaults);
264 void add_color_info(const config& v)
265 {
266  add_color_info(v, false);
267 }
268 
269 void load_config(const config &v)
270 {
271  base_income = v["base_income"].to_int(2);
272  village_income = v["village_income"].to_int(1);
273  village_support = v["village_support"].to_int(1);
274  poison_amount = v["poison_amount"].to_int(8);
275  rest_heal_amount = v["rest_heal_amount"].to_int(2);
276  recall_cost = v["recall_cost"].to_int(20);
277  kill_experience = v["kill_experience"].to_int(8);
278  lobby_refresh = v["lobby_refresh"].to_int(2000);
279  default_terrain = v["default_terrain"].str();
280  tile_size = v["tile_size"].to_int(72);
281 
282  std::vector<std::string> zoom_levels_str = utils::split(v["zoom_levels"]);
283  if(!zoom_levels_str.empty()) {
284  zoom_levels.clear();
285  std::transform(zoom_levels_str.begin(), zoom_levels_str.end(), std::back_inserter(zoom_levels), [](const std::string zoom) {
286  return static_cast<int>(std::stold(zoom) * tile_size);
287  });
288  }
289 
290  title_music = v["title_music"].str();
291  lobby_music = v["lobby_music"].str();
292 
293  default_victory_music = utils::split(v["default_victory_music"].str());
294  default_defeat_music = utils::split(v["default_defeat_music"].str());
295 
296  if(const config& i = v.child("colors")){
297  using namespace game_config::colors;
298 
299  moved_orb_color = i["moved_orb_color"].str();
300  unmoved_orb_color = i["unmoved_orb_color"].str();
301  partial_orb_color = i["partial_orb_color"].str();
302  enemy_orb_color = i["enemy_orb_color"].str();
303  ally_orb_color = i["ally_orb_color"].str();
304  } // colors
305 
306  show_ally_orb = v["show_ally_orb"].to_bool(true);
307  show_enemy_orb = v["show_enemy_orb"].to_bool(false);
308  show_moved_orb = v["show_moved_orb"].to_bool(true);
309  show_partial_orb = v["show_partly_orb"].to_bool(true);
310  show_unmoved_orb = v["show_unmoved_orb"].to_bool(true);
311 
312  if(const config& i = v.child("images")){
313  using namespace game_config::images;
314 
315  game_title = i["game_title"].str();
316  game_title_background = i["game_title_background"].str();
317  game_logo = i["game_logo"].str();
318  game_logo_background = i["game_logo_background"].str();
319 
320  victory_laurel = i["victory_laurel"].str();
321  victory_laurel_hardest = i["victory_laurel_hardest"].str();
322  victory_laurel_easy = i["victory_laurel_easy"].str();
323 
324  orb = i["orb"].str();
325  energy = i["energy"].str();
326 
327  flag = i["flag"].str();
328  flag_icon = i["flag_icon"].str();
329 
330  terrain_mask = i["terrain_mask"].str();
331  grid_top = i["grid_top"].str();
332  grid_bottom = i["grid_bottom"].str();
333  mouseover = i["mouseover"].str();
334  selected = i["selected"].str();
335  editor_brush = i["editor_brush"].str();
336  unreachable = i["unreachable"].str();
337  linger = i["linger"].str();
338 
339  observer = i["observer"].str();
340  tod_bright = i["tod_bright"].str();
341  tod_dark = i["tod_dark"].str();
342  level = i["level"].str();
343  ellipsis = i["ellipsis"].str();
344  missing = i["missing"].str();
345  } // images
346 
347  hp_bar_scaling = v["hp_bar_scaling"].to_double(0.666);
348  xp_bar_scaling = v["xp_bar_scaling"].to_double(0.5);
349  hex_brightening = v["hex_brightening"].to_double(1.25);
350 
351  foot_speed_prefix = utils::split(v["footprint_prefix"]);
352  foot_teleport_enter = v["footprint_teleport_enter"].str();
353  foot_teleport_exit = v["footprint_teleport_exit"].str();
354 
355  shroud_prefix = v["shroud_prefix"].str();
356  fog_prefix = v["fog_prefix"].str();
357 
358  add_color_info(v, true);
359 
360  if(const config::attribute_value* a = v.get("flag_rgb")) {
361  flag_rgb = a->str();
362  }
363 
364  if(const config::attribute_value* a = v.get("unit_rgb")) {
365  unit_rgb = a->str();
366  }
367 
368  const auto parse_config_color_list = [&](
369  const std::string& key,
370  const color_t fallback)->std::vector<color_t>
371  {
372  std::vector<color_t> color_vec;
373 
374  for(const auto& s : utils::split(v[key].str())) {
375  try {
376  color_vec.push_back(color_t::from_hex_string(s));
377  } catch(std::invalid_argument& e) {
378  ERR_NG << "Error parsing color list '" << key << "'.\n" << e.what() << std::endl;
379  color_vec.push_back(fallback);
380  }
381  }
382 
383  return color_vec;
384  };
385 
386  red_green_scale = parse_config_color_list("red_green_scale", {255, 255, 255});
387  red_green_scale_text = parse_config_color_list("red_green_scale_text", {255, 255, 255});
388  blue_white_scale = parse_config_color_list("blue_white_scale", {0 , 0 , 255});
389  blue_white_scale_text = parse_config_color_list("blue_white_scale_text", {0 , 0 , 255});
390 
391  server_list.clear();
392 
393  for(const config& server : v.child_range("server")) {
394  server_info sinf;
395  sinf.name = server["name"].str();
396  sinf.address = server["address"].str();
397  server_list.push_back(sinf);
398  }
399 
400  if(const config& s = v.child("sounds")) {
401  using namespace game_config::sounds;
402 
403  const auto load_attribute = [](const config& c, const std::string& key, std::string& member) {
404  if(c.has_attribute(key)) {
405  member = c[key].str();
406  }
407  };
408 
409  load_attribute(s, "turn_bell", turn_bell);
410  load_attribute(s, "timer_bell", timer_bell);
411  load_attribute(s, "public_message", public_message);
412  load_attribute(s, "private_message", private_message);
413  load_attribute(s, "friend_message", friend_message);
414  load_attribute(s, "server_message", server_message);
415  load_attribute(s, "player_joins", player_joins);
416  load_attribute(s, "player_leaves", player_leaves);
417  load_attribute(s, "game_user_arrive", game_user_arrive);
418  load_attribute(s, "game_user_leave", game_user_leave);
419  load_attribute(s, "ready_for_start", ready_for_start);
420  load_attribute(s, "game_has_begun", game_has_begun);
421 
422  if(const config & ss = s.child("status")) {
423  using namespace game_config::sounds::status;
424 
425  load_attribute(ss, "poisoned", poisoned);
426  load_attribute(ss, "slowed", slowed);
427  load_attribute(ss, "petrified", petrified);
428  }
429  }
430 }
431 
432 void add_color_info(const config& v, bool build_defaults)
433 {
434  if(build_defaults) {
435  default_colors.clear();
436  }
437 
438  for(const config& teamC : v.child_range("color_range")) {
439  const config::attribute_value* a1 = teamC.get("id"), *a2 = teamC.get("rgb");
440  if(!a1 || !a2) {
441  continue;
442  }
443 
444  std::string id = *a1;
445  std::vector<color_t> temp;
446 
447  for(const auto& s : utils::split(*a2)) {
448  try {
449  temp.push_back(color_t::from_hex_string(s));
450  } catch(std::invalid_argument&) {
451  std::stringstream ss;
452  ss << "can't parse color string:\n" << teamC.debug() << "\n";
453  throw config::error(ss.str());
454  }
455  }
456 
457  team_rgb_range.emplace(id, color_range(temp));
458  team_rgb_name.emplace(id, teamC["name"].t_str());
459 
460  LOG_NG << "registered color range '" << id << "': " << team_rgb_range[id].debug() << '\n';
461 
462  // Ggenerate palette of same name;
463  std::vector<color_t> tp = palette(team_rgb_range[id]);
464  if(!tp.empty()) {
465  team_rgb_colors.emplace(id, tp);
466  }
467 
468  if(build_defaults && teamC["default"].to_bool()) {
469  default_colors.push_back(*a1);
470  }
471  }
472 
473  for(const config &cp : v.child_range("color_palette")) {
474  for(const config::attribute& rgb : cp.attribute_range()) {
475  std::vector<color_t> temp;
476  for(const auto& s : utils::split(rgb.second)) {
477  try {
478  temp.push_back(color_t::from_hex_string(s));
479  } catch(std::invalid_argument&) {
480  ERR_NG << "Invalid color in palette: " << s << std::endl;
481  }
482  }
483 
484  team_rgb_colors.emplace(rgb.first, temp);
485  LOG_NG << "registered color palette: " << rgb.first << '\n';
486  }
487  }
488 }
489 
491 {
492  default_colors.clear();
493  team_rgb_colors.clear();
494  team_rgb_name.clear();
495  team_rgb_range.clear();
496 }
497 
499 {
500  auto i = team_rgb_range.find(name);
501  if(i != team_rgb_range.end()) {
502  return i->second;
503  }
504 
505  std::vector<color_t> temp;
506  for(const auto& s : utils::split(name)) {
507  try {
508  temp.push_back(color_t::from_hex_string(s));
509  } catch(std::invalid_argument&) {
510  throw config::error(_("Invalid color in range: ") + s);
511  }
512  }
513 
514  team_rgb_range.emplace(name, color_range(temp));
515  return color_info(name);
516 }
517 
518 const std::vector<color_t>& tc_info(const std::string& name)
519 {
520  auto i = team_rgb_colors.find(name);
521  if(i != team_rgb_colors.end()) {
522  return i->second;
523  }
524 
525  std::vector<color_t> temp;
526  for(const auto& s : utils::split(name)) {
527  try {
528  temp.push_back(color_t::from_hex_string(s));
529  } catch(std::invalid_argument&) {
530  static std::vector<color_t> stv;
531  ERR_NG << "Invalid color in palette: " << s << std::endl;
532  return stv;
533  }
534  }
535 
536  team_rgb_colors.emplace(name, temp);
537  return tc_info(name);
538 }
539 
540 color_t red_to_green(int val, bool for_text)
541 {
542  const std::vector<color_t>& color_scale = for_text ? red_green_scale_text : red_green_scale;
543 
544  val = utils::clamp(val, 0, 100);
545  const int lvl = (color_scale.size() - 1) * val / 100;
546 
547  return color_scale[lvl];
548 }
549 
550 color_t blue_to_white(int val, bool for_text)
551 {
552  const std::vector<color_t>& color_scale = for_text ? blue_white_scale_text : blue_white_scale;
553 
554  val = utils::clamp(val, 0, 100);
555  const int lvl = (color_scale.size() - 1) * val / 100;
556 
557  return color_scale[lvl];
558 }
559 
561 {
562  std::string ret = _("The Battle for Wesnoth") + " - " + revision;
563  return ret;
564 }
565 
566 } // game_config
std::vector< std::string > default_victory_music
std::string game_user_leave
static std::map< std::string, std::vector< std::string > > images
Definition: about.cpp:30
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:417
std::string tod_dark
bool show_unmoved_orb
double hp_bar_scaling
Definition: game_config.cpp:94
std::vector< char_t > string
double xp_bar_scaling
Definition: game_config.cpp:95
std::map< std::string, color_range > team_rgb_range
int village_support
Definition: game_config.cpp:72
#define ERR_NG
Definition: game_config.cpp:31
Variant for storing WML attributes.
std::string mouseover
std::vector< unsigned int > zoom_levels
Definition: game_config.cpp:89
const std::string menu_expand
std::vector< color_t > palette(const color_range &cr)
Creates a reference color palette from a color range.
Definition: color_range.cpp:84
static l_noret error(LoadState *S, const char *why)
Definition: lundump.cpp:39
color_t blue_to_white(int val, bool for_text)
#define a
std::string private_message
std::string game_title
child_itors child_range(config_key_type key)
Definition: config.cpp:360
std::string selected_menu
static std::vector< color_t > blue_white_scale
std::string lobby_music
attribute_map::value_type attribute
Definition: config.hpp:257
std::string unreachable
const std::string slider_adjust
std::string unit_rgb
std::string friend_message
std::string get_default_title_string()
const std::string menu_select
std::string unchecked_menu
std::string victory_laurel_easy
std::string enemy_orb_color
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 ':' 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:77
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:30
std::string ally_orb_color
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:89
std::string deselected_menu
std::string victory_laurel_hardest
std::vector< color_t > red_green_scale_text
unsigned lobby_network_timer
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:79
std::string game_user_arrive
bool ignore_replay_errors
const version_info test_version("test")
std::string selected
std::string foot_teleport_enter
std::string game_title_background
const 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...
int kill_experience
Definition: game_config.cpp:74
std::string grid_bottom
std::string path
Definition: game_config.cpp:56
std::string game_logo_background
std::string shroud_prefix
Definition: game_config.cpp:87
Manage the empty-palette in the editor.
Definition: action.cpp:29
std::string default_terrain
Definition: game_config.cpp:86
std::string moved_orb_color
std::string flag_rgb
bool has_attribute(config_key_type key) const
Definition: config.cpp:213
#define MIN_SAVEGAME_VERSION
Some older savegames of Wesnoth cannot be loaded anymore, this variable defines the minimum required ...
Definition: wesconfig.h:33
std::string server_message
Some defines: VERSION, PACKAGE, MIN_SAVEGAME_VERSION.
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: game_config.cpp:50
#define VERSION
Definition: wesconfig.h:43
Game configuration data as global variables.
Definition: build_info.cpp:53
std::string public_message
int village_income
Definition: game_config.cpp:71
static map_location::DIRECTION s
void reset_color_info()
const std::string menu_contract
A color range definition is made of four reference RGB colors, used for calculating conversions from ...
Definition: color_range.hpp:50
size_t i
Definition: function.cpp:933
std::vector< color_t > red_green_scale
std::string title_music
bool show_moved_orb
std::vector< std::string > foot_speed_prefix
const version_info wesnoth_version(VERSION)
std::string observer
Represents version numbers.
Definition: version.hpp:43
std::string player_leaves
const std::string button_press
std::string foot_teleport_exit
CONSTEXPR const T & clamp(const T &value, const T &min, const T &max)
Definition: general.hpp:31
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:689
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:84
unsigned lobby_refresh
const color_range & color_info(const std::string &name)
Standard logging facilities (interface).
double hex_brightening
Definition: game_config.cpp:96
std::string tod_bright
std::string game_logo
std::string ellipsis
const std::vector< color_t > & tc_info(const std::string &name)
static const char * name(const std::vector< SDL_Joystick * > &joysticks, const size_t index)
Definition: joystick.cpp:48
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:93
mock_char c
std::string wml_menu
const version_info min_savegame_version(MIN_SAVEGAME_VERSION)
std::map< std::string, t_string > team_rgb_name
std::string game_has_begun
Interfaces for manipulating version numbers of engine, add-ons, etc.
const std::string version
Definition: game_config.cpp:39
std::string grid_top
std::string fog_prefix
Definition: game_config.cpp:87
std::vector< server_info > server_list
std::string app_icon
std::string wesnoth_program_dir
Definition: game_config.cpp:65
static lg::log_domain log_engine("engine")
std::string default_preferences_path
Definition: game_config.cpp:62
const std::string observer_team_name
observer team name used for observer team chat
const std::string checkbox_release
int cache_compression_level