The Battle for Wesnoth  1.13.4+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 - 2016 by David White <[email protected]>
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 "global.hpp"
16 #include "game_config.hpp"
17 
18 #include "color_range.hpp"
19 #include "config.hpp"
20 #include "gettext.hpp"
21 #include "log.hpp"
22 #include "util.hpp"
23 #include "version.hpp"
24 #include "wesconfig.h"
26 #ifdef LOAD_REVISION
27 #include "revision.h"
28 #endif
29 
30 static lg::log_domain log_engine("engine");
31 #define DBG_NG LOG_STREAM(debug, log_engine)
32 #define LOG_NG LOG_STREAM(info, log_engine)
33 #define ERR_NG LOG_STREAM(err, log_engine)
34 
35 namespace game_config
36 {
37  int base_income = 2;
38  int village_income = 1;
39  int village_support = 1;
40  int poison_amount= 8;
42  int recall_cost = 20;
43  int kill_experience = 8;
44  int tile_size = 72;
45  unsigned lobby_network_timer = 100;
46  unsigned lobby_refresh = 4000;
47  const int gold_carryover_percentage = 80;
50 #ifdef REVISION
51  const std::string revision = VERSION " (" REVISION ")";
52 #elif defined(VCS_SHORT_HASH) && defined(VCS_WC_MODIFIED)
53  const std::string revision
54  = std::string(VERSION) + " ("
55  + VCS_SHORT_HASH + (VCS_WC_MODIFIED ? "-Modified" : "-Clean") + ")";
56 #else
57  const std::string revision = VERSION;
58 #endif
59 
60 
62  bool debug = false, debug_lua = false, editor = false,
63  ignore_replay_errors = false, mp_debug = false, exit_at_end = false,
64  no_delay = false, disable_autosave = false, no_addons = false;
65 
67 
72 
73  namespace colors {
79  }
80 
82 
83  namespace images {
86  // orbs and hp/xp bar
87  orb,
88  energy,
89  // flags
90  flag,
91  flag_icon,
92  // hex overlay
94  grid_top,
96  mouseover,
97  selected,
100  linger,
101  // GUI elements
102  observer,
103  tod_bright,
104  tod_dark,
105  ///@todo de-hardcode this
106  selected_menu = "buttons/radiobox-pressed.png",
107  deselected_menu = "buttons/radiobox.png",
108  checked_menu = "buttons/checkbox-pressed.png",
109  unchecked_menu = "buttons/checkbox.png",
110  wml_menu = "buttons/WML-custom.png",
111  level,
112  ellipsis,
113  missing,
114  // notifications icon
115  app_icon = "images/icons/icon-game.png";
116  } //images
117 
119 
121  std::vector<Uint32> red_green_scale;
122  std::vector<Uint32> red_green_scale_text;
123 
124  static std::vector<Uint32> blue_white_scale;
125  static std::vector<Uint32> blue_white_scale_text;
126 
127  double hp_bar_scaling = 0.666;
128  double xp_bar_scaling = 0.5;
129 
130  double hex_brightening = 1.25;
131 
132  std::vector<std::string> foot_speed_prefix;
135 
136  std::map<std::string, color_range > team_rgb_range;
137  std::map<std::string, t_string > team_rgb_name;
138 
139  std::map<std::string, std::vector<Uint32> > team_rgb_colors;
140 
143  const version_info test_version("test");
144 
145  const std::string observer_team_name = "observer";
146 
147  const size_t max_loop = 65536;
148 
149  namespace sounds {
150  std::string turn_bell = "bell.wav",
151  timer_bell = "timer.wav",
152  public_message = "chat-[1~3].ogg",
153  private_message = "chat-highlight.ogg",
154  friend_message = "chat-friend.ogg",
155  server_message = "receive.wav",
156  player_joins = "arrive.wav",
157  player_leaves = "leave.wav",
158  game_user_arrive = "join.wav",
159  game_user_leave = "leave.wav",
160  ready_for_start = "bell.wav",
161  game_has_begun = "gamestart.ogg";
162 
163  const std::string button_press = "button.wav",
164  checkbox_release = "checkbox.wav",
165  slider_adjust = "slider.wav",
166  menu_expand = "expand.wav",
167  menu_contract = "contract.wav",
168  menu_select = "select.wav";
169 
170  namespace status {
171  std::string poisoned = "poison.ogg",
172  slowed = "slowed.wav",
173  petrified = "petrified.ogg";
174  }
175  }
176 
177 #ifdef WESNOTH_PATH
178  std::string path = WESNOTH_PATH;
179 #else
180  std::string path = "";
181 #endif
182 
183 #ifdef DEFAULT_PREFS_PATH
184  std::string default_preferences_path = DEFAULT_PREFS_PATH;
185 #else
186  std::string default_preferences_path = "";
187 #endif
188 
189  std::vector<server_info> server_list;
190 
191  void load_config(const config &v)
192  {
193  base_income = v["base_income"].to_int(2);
194  village_income = v["village_income"].to_int(1);
195  village_support = v["village_support"].to_int(1);
196  poison_amount = v["poison_amount"].to_int(8);
197  rest_heal_amount = v["rest_heal_amount"].to_int(2);
198  recall_cost = v["recall_cost"].to_int(20);
199  kill_experience = v["kill_experience"].to_int(8);
200  lobby_refresh = v["lobby_refresh"].to_int(2000);
201  default_terrain = v["default_terrain"].str();
202  tile_size = v["tile_size"].to_int(72);
203 
204  title_music = v["title_music"].str();
205  lobby_music = v["lobby_music"].str();
206  default_victory_music = v["default_victory_music"].str();
207  default_defeat_music = v["default_defeat_music"].str();
208 
209  if(const config &i = v.child("colors")){
210  using namespace game_config::colors;
211  moved_orb_color = i["moved_orb_color"].str();
212  unmoved_orb_color = i["unmoved_orb_color"].str();
213  partial_orb_color = i["partial_orb_color"].str();
214  enemy_orb_color = i["enemy_orb_color"].str();
215  ally_orb_color = i["ally_orb_color"].str();
216  } // colors
217 
218  show_ally_orb = v["show_ally_orb"].to_bool(true);
219  show_enemy_orb = v["show_enemy_orb"].to_bool(false);
220  show_moved_orb = v["show_moved_orb"].to_bool(true);
221  show_partial_orb = v["show_partly_orb"].to_bool(true);
222  show_unmoved_orb = v["show_unmoved_orb"].to_bool(true);
223 
224  if(const config &i = v.child("images")){
225  using namespace game_config::images;
226  game_title = i["game_title"].str();
227  game_title_background = i["game_title_background"].str();
228 
229  orb = i["orb"].str();
230  energy = i["energy"].str();
231 
232  flag = i["flag"].str();
233  flag_icon = i["flag_icon"].str();
234 
235  terrain_mask = i["terrain_mask"].str();
236  grid_top = i["grid_top"].str();
237  grid_bottom = i["grid_bottom"].str();
238  mouseover = i["mouseover"].str();
239  selected = i["selected"].str();
240  editor_brush = i["editor_brush"].str();
241  unreachable = i["unreachable"].str();
242  linger = i["linger"].str();
243 
244  observer = i["observer"].str();
245  tod_bright = i["tod_bright"].str();
246  tod_dark = i["tod_dark"].str();
247  level = i["level"].str();
248  ellipsis = i["ellipsis"].str();
249  missing = i["missing"].str();
250  } // images
251 
252  hp_bar_scaling = v["hp_bar_scaling"].to_double(0.666);
253  xp_bar_scaling = v["xp_bar_scaling"].to_double(0.5);
254  hex_brightening = v["hex_brightening"].to_double(1.25);
255 
256  foot_speed_prefix = utils::split(v["footprint_prefix"]);
257  foot_teleport_enter = v["footprint_teleport_enter"].str();
258  foot_teleport_exit = v["footprint_teleport_exit"].str();
259 
260  shroud_prefix = v["shroud_prefix"].str();
261  fog_prefix = v["fog_prefix"].str();
262 
263  add_color_info(v);
264 
265  if (const config::attribute_value *a = v.get("flag_rgb")) {
266  flag_rgb = a->str();
267  }
268 
269  std::string color_string = v["red_green_scale"].str();
270  if(!string2rgb(color_string, red_green_scale)) {
271  ERR_NG << "can't parse color string red_green_scale, ignoring: " << color_string << std::endl;
272  }
273  if (red_green_scale.empty()) {
274  red_green_scale.push_back(0x00FFFF00);
275  }
276 
277  color_string = v["red_green_scale_text"].str();
278  if(!string2rgb(color_string, red_green_scale_text)) {
279  ERR_NG << "can't parse color string red_green_scale_text, ignoring: " << color_string << std::endl;
280  }
281  if (red_green_scale_text.empty()) {
282  red_green_scale_text.push_back(0x00FFFF00);
283  }
284 
285  color_string = v["blue_white_scale"].str();
286  if(!string2rgb(color_string, blue_white_scale)) {
287  ERR_NG << "can't parse color string blue_white_scale, ignoring: " << color_string << std::endl;
288  }
289  if (blue_white_scale.empty()) {
290  blue_white_scale.push_back(0x00FFFFFF);
291  }
292 
293  color_string = v["blue_white_scale_text"].str();
294  if(!string2rgb(color_string, blue_white_scale_text)) {
295  ERR_NG << "can't parse color string blue_white_scale_text, ignoring: " << color_string << std::endl;
296  }
297  if (blue_white_scale_text.empty()) {
298  blue_white_scale_text.push_back(0x00FFFFFF);
299  }
300 
301  server_list.clear();
302  for (const config &server : v.child_range("server"))
303  {
304  server_info sinf;
305  sinf.name = server["name"].str();
306  sinf.address = server["address"].str();
307  server_list.push_back(sinf);
308  }
309 
310  if(const config & s = v.child("sounds")) {
311  using namespace game_config::sounds;
312  if (s.has_attribute("turn_bell")) turn_bell = s["turn_bell"].str();
313  if (s.has_attribute("timer_bell")) timer_bell = s["timer_bell"].str();
314  if (s.has_attribute("public_message")) public_message = s["public_message"].str();
315  if (s.has_attribute("private_message")) private_message = s["private_message"].str();
316  if (s.has_attribute("friend_message")) friend_message = s["friend_message"].str();
317  if (s.has_attribute("server_message")) server_message = s["server_message"].str();
318  if (s.has_attribute("player_joins")) player_joins = s["player_joins"].str();
319  if (s.has_attribute("player_leaves")) player_leaves = s["player_leaves"].str();
320  if (s.has_attribute("game_user_arrive")) game_user_arrive = s["game_user_arrive"].str();
321  if (s.has_attribute("game_user_leave")) game_user_leave = s["game_user_leave"].str();
322  if (s.has_attribute("ready_for_start")) ready_for_start = s["ready_for_start"].str();
323  if (s.has_attribute("game_has_begun")) game_has_begun = s["game_has_begun"].str();
324 
325  if(const config & ss = s.child("status")) {
326  using namespace game_config::sounds::status;
327  if (ss.has_attribute("poisoned")) poisoned = ss["poisoned"].str();
328  if (ss.has_attribute("slowed")) slowed = ss["slowed"].str();
329  if (ss.has_attribute("petrified")) petrified = ss["petrified"].str();
330  }
331  }
332  }
333 
334  void add_color_info(const config &v)
335  {
336  for (const config &teamC : v.child_range("color_range"))
337  {
338  const config::attribute_value *a1 = teamC.get("id"),
339  *a2 = teamC.get("rgb");
340  if (!a1 || !a2) {
341  continue;
342  }
343  std::string id = *a1;
344  std::vector<Uint32> temp;
345  if(!string2rgb(*a2, temp)) {
346  std::stringstream ss;
347  ss << "can't parse color string:\n" << teamC.debug() << "\n";
348  throw config::error(ss.str());
349  }
350  team_rgb_range.insert(std::make_pair(id,color_range(temp)));
351  team_rgb_name[id] = teamC["name"];
352 
353  LOG_NG << "registered color range '" << id << "': " << team_rgb_range[id].debug() << '\n';
354 
355  //generate palette of same name;
356  std::vector<Uint32> tp = palette(team_rgb_range[id]);
357  if (tp.empty()) {
358  continue;
359  }
360  team_rgb_colors.insert(std::make_pair(id,tp));
361  }
362 
363  for (const config &cp : v.child_range("color_palette"))
364  {
365  for (const config::attribute &rgb : cp.attribute_range())
366  {
367  std::vector<Uint32> temp;
368  if(!string2rgb(rgb.second, temp)) {
369  ERR_NG << "Invalid color palette: " << rgb.second << std::endl;
370  }
371  team_rgb_colors.insert(std::make_pair(rgb.first, temp));
372  LOG_NG << "registered color palette: " << rgb.first << '\n';
373  }
374  }
375  }
376 
378  {
379  std::map<std::string, color_range>::const_iterator i = team_rgb_range.find(name);
380  if(i == team_rgb_range.end()) {
381  std::vector<Uint32> temp;
382  if(!string2rgb(name, temp)) {
383  throw config::error(_("Invalid color range: ") + name);
384  }
385  team_rgb_range.insert(std::make_pair(name,color_range(temp)));
386  return color_info(name);
387  }
388  return i->second;
389  }
390 
391  const std::vector<Uint32>& tc_info(const std::string& name)
392  {
393  std::map<std::string, std::vector<Uint32> >::const_iterator i = team_rgb_colors.find(name);
394  if(i == team_rgb_colors.end()) {
395  std::vector<Uint32> temp;
396  if(!string2rgb(name, temp)) {
397  static std::vector<Uint32> stv;
398  ERR_NG << "Invalid color palette: " << name << std::endl;
399  return stv;
400  }
401  team_rgb_colors.insert(std::make_pair(name,temp));
402  return tc_info(name);
403  }
404  return i->second;
405  }
406 
407  Uint32 red_to_green(int val, bool for_text){
408  const std::vector<Uint32>& color_scale =
409  for_text ? red_green_scale_text : red_green_scale;
410  val = std::max<int>(0, std::min<int>(val, 100));
411  int lvl = (color_scale.size()-1) * val / 100;
412  return color_scale[lvl];
413  }
414 
415  Uint32 blue_to_white(int val, bool for_text){
416  const std::vector<Uint32>& color_scale =
417  for_text ? blue_white_scale_text : blue_white_scale;
418  val = std::max<int>(0, std::min<int>(val, 100));
419  int lvl = (color_scale.size()-1) * val / 100;
420  return color_scale[lvl];
421  }
422 
424  std::string ret = _("The Battle for Wesnoth") + " - " + revision;
425  return ret;
426  }
427 
428 } // game_config
child_itors child_range(const std::string &key)
Definition: config.cpp:613
std::string game_user_leave
std::string tod_dark
Definition: game_config.cpp:84
bool show_unmoved_orb
Definition: game_config.cpp:81
double hp_bar_scaling
double xp_bar_scaling
std::map< std::string, color_range > team_rgb_range
int village_support
Definition: game_config.cpp:39
GLint level
Definition: glew.h:1220
#define ERR_NG
Definition: game_config.cpp:33
const std::vector< Uint32 > & tc_info(const std::string &name)
std::string mouseover
Definition: game_config.cpp:84
const std::string menu_expand
static l_noret error(LoadState *S, const char *why)
Definition: lundump.cpp:29
std::string default_victory_music
Definition: game_config.cpp:68
std::string energy
Definition: game_config.cpp:84
std::string private_message
std::string game_title
Definition: game_config.cpp:84
std::string selected_menu
static std::vector< Uint32 > blue_white_scale_text
std::string lobby_music
Definition: game_config.cpp:68
bool string2rgb(const std::string &s, std::vector< Uint32 > &result)
Definition: color_range.cpp:91
attribute_map::value_type attribute
Definition: config.hpp:393
std::string unreachable
Definition: game_config.cpp:84
const std::string slider_adjust
std::string friend_message
GLuint const GLfloat * val
Definition: glew.h:2614
std::string get_default_title_string()
const std::string menu_select
std::string unchecked_menu
bool exit_at_end
Definition: game_config.cpp:63
std::string enemy_orb_color
Definition: game_config.cpp:74
bool show_enemy_orb
Definition: game_config.cpp:81
std::string player_joins
Definitions for the interface to Wesnoth Markup Language (WML).
std::string address
may include ':' followed by port number
Definition: game_config.hpp:66
void load_config(const config &v)
int rest_heal_amount
Definition: game_config.cpp:41
Uint32 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...
bool show_ally_orb
Definition: game_config.cpp:81
Variant for storing WML attributes.
Definition: config.hpp:223
std::string terrain_mask
Definition: game_config.cpp:84
std::string flag_icon
Definition: game_config.cpp:84
std::string turn_bell
std::string timer_bell
#define LOG_NG
Definition: game_config.cpp:32
std::string ally_orb_color
Definition: game_config.cpp:74
std::string default_defeat_music
Definition: game_config.cpp:68
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:82
std::string deselected_menu
GLsizei const char ** path
Definition: glew.h:4654
GLuint id
Definition: glew.h:1647
unsigned lobby_network_timer
Definition: game_config.cpp:45
bool disable_autosave
Definition: game_config.cpp:64
std::string ready_for_start
const int gold_carryover_percentage
Default percentage gold carried over to the next scenario.
Definition: game_config.cpp:47
void add_color_info(const config &v)
const GLdouble * v
Definition: glew.h:1359
std::string game_user_arrive
bool ignore_replay_errors
Definition: game_config.cpp:63
const version_info test_version("test")
std::string selected
Definition: game_config.cpp:84
std::string foot_teleport_enter
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:7319
std::string game_title_background
Definition: game_config.cpp:84
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...
static std::vector< Uint32 > blue_white_scale
int kill_experience
Definition: game_config.cpp:43
std::string grid_bottom
Definition: game_config.cpp:84
std::string shroud_prefix
Manage the empty-palette in the editor.
Definition: action.cpp:28
std::string default_terrain
Definition: game_config.cpp:49
std::string moved_orb_color
Definition: game_config.cpp:74
Templates and utility-routines for strings and numbers.
std::string flag_rgb
#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::string editor_brush
Definition: game_config.cpp:84
std::string unmoved_orb_color
Definition: game_config.cpp:74
const std::string revision
Definition: game_config.cpp:57
Game configuration data as global variables.
Definition: build_info.cpp:38
std::string public_message
Uint32 blue_to_white(int val, bool for_text)
int village_income
Definition: game_config.cpp:38
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:54
size_t i
Definition: function.cpp:1057
std::string title_music
Definition: game_config.cpp:68
bool show_moved_orb
Definition: game_config.cpp:81
std::vector< Uint32 > red_green_scale
std::vector< std::string > foot_speed_prefix
const version_info wesnoth_version(VERSION)
std::string observer
Definition: game_config.cpp:84
Represents version numbers.
Definition: version.hpp:44
GLuint const GLchar * name
Definition: glew.h:1782
const attribute_value * get(const std::string &key) const
Returns a pointer to the attribute with the given key or nullptr if it does not exist.
Definition: config.cpp:935
std::string player_leaves
const std::string button_press
std::string foot_teleport_exit
static std::map< std::string, std::string > images
Definition: about.cpp:58
std::map< std::string, std::vector< Uint32 > > team_rgb_colors
config & child(const std::string &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:658
unsigned lobby_refresh
Definition: game_config.cpp:46
const color_range & color_info(const std::string &name)
Standard logging facilities (interface).
double hex_brightening
std::string tod_bright
Definition: game_config.cpp:84
std::string ellipsis
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 checked_menu
bool show_partial_orb
Definition: game_config.cpp:81
std::vector< Uint32 > red_green_scale_text
std::string partial_orb_color
Definition: game_config.cpp:74
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
std::string wml_menu
GLdouble s
Definition: glew.h:1358
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.
std::vector< Uint32 > palette(color_range cr)
Creates a reference color palette from a color range.
#define VERSION
Definition: lundump.cpp:235
const std::string version
Definition: game_config.cpp:48
GLsizei const GLcharARB ** string
Definition: glew.h:4503
std::string grid_top
Definition: game_config.cpp:84
std::string fog_prefix
std::vector< server_info > server_list
std::string app_icon
std::string wesnoth_program_dir
Definition: game_config.cpp:61
static lg::log_domain log_engine("engine")
std::string linger
Definition: game_config.cpp:84
std::string default_preferences_path
const std::string observer_team_name
observer team name used for observer team chat
const std::string checkbox_release
int cache_compression_level
Definition: game_config.cpp:66