-
-
Notifications
You must be signed in to change notification settings - Fork 990
/
game_config.cpp
538 lines (437 loc) · 13.5 KB
/
game_config.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*
Copyright (C) 2003 - 2016 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "global.hpp"
#include "game_config.hpp"
#include "color_range.hpp"
#include "config.hpp"
#include "gettext.hpp"
#include "log.hpp"
#include "util.hpp"
#include "version.hpp"
#include "wesconfig.h"
#include "serialization/string_utils.hpp"
#ifdef LOAD_REVISION
#include "revision.h"
#endif
static lg::log_domain log_engine("engine");
#define LOG_NG LOG_STREAM(info, log_engine)
#define ERR_NG LOG_STREAM(err, log_engine)
namespace game_config
{
//
// Path and revision info
//
const std::string version = VERSION;
const version_info wesnoth_version(VERSION);
const version_info min_savegame_version(MIN_SAVEGAME_VERSION);
const version_info test_version("test");
#ifdef REVISION
const std::string revision = VERSION " (" REVISION ")";
#elif defined(VCS_SHORT_HASH) && defined(VCS_WC_MODIFIED)
const std::string revision = std::string(VERSION) + " (" + VCS_SHORT_HASH + (VCS_WC_MODIFIED ? "-Modified" : "-Clean") + ")";
#else
const std::string revision = VERSION;
#endif
#ifdef WESNOTH_PATH
std::string path = WESNOTH_PATH;
#else
std::string path = "";
#endif
#ifdef DEFAULT_PREFS_PATH
std::string default_preferences_path = DEFAULT_PREFS_PATH;
#else
std::string default_preferences_path = "";
#endif
std::string wesnoth_program_dir;
//
// Gameplay constants
//
int base_income = 2;
int village_income = 1;
int village_support = 1;
int recall_cost = 20;
int kill_experience = 8;
int poison_amount = 8;
int rest_heal_amount = 2;
const int gold_carryover_percentage = 80;
//
// Terrain-related constants
//
int tile_size = 72;
std::string default_terrain;
std::string shroud_prefix, fog_prefix;
//
// Display scale constants
//
double hp_bar_scaling = 0.666;
double xp_bar_scaling = 0.5;
double hex_brightening = 1.25;
//
// Misc
//
int cache_compression_level = 6;
unsigned lobby_network_timer = 100;
unsigned lobby_refresh = 4000;
const std::string observer_team_name = "observer";
const size_t max_loop = 65536;
std::vector<server_info> server_list;
//
// Gamestate flags
//
bool
debug = false,
debug_lua = false,
editor = false,
ignore_replay_errors = false,
mp_debug = false,
exit_at_end = false,
no_delay = false,
disable_autosave = false,
no_addons = false;
//
// Orb display flahs
//
bool
show_ally_orb,
show_enemy_orb,
show_moved_orb,
show_partial_orb,
show_unmoved_orb;
//
// Music constants
//
std::string
title_music,
lobby_music,
default_victory_music,
default_defeat_music;
//
// Color info
//
std::string flag_rgb, unit_rgb;
std::vector<color_t> red_green_scale;
std::vector<color_t> red_green_scale_text;
static std::vector<color_t> blue_white_scale;
static std::vector<color_t> blue_white_scale_text;
std::map<std::string, color_range> team_rgb_range;
std::map<std::string, t_string> team_rgb_name;
std::map<std::string, std::vector<color_t>> team_rgb_colors;
std::vector<std::string> default_colors;
namespace colors {
std::string
moved_orb_color,
unmoved_orb_color,
partial_orb_color,
enemy_orb_color,
ally_orb_color,
default_color_list;
} // colors
//
// Image constants
//
std::vector<std::string> foot_speed_prefix;
std::string foot_teleport_enter;
std::string foot_teleport_exit;
namespace images {
std::string
game_title,
game_title_background,
game_logo,
game_logo_background,
// orbs and hp/xp bar
orb,
energy,
// flags
flag,
flag_icon,
// hex overlay
terrain_mask,
grid_top,
grid_bottom,
mouseover,
selected,
editor_brush,
unreachable,
linger,
// GUI elements
observer,
tod_bright,
tod_dark,
///@todo de-hardcode this
selected_menu = "buttons/radiobox-pressed.png",
deselected_menu = "buttons/radiobox.png",
checked_menu = "buttons/checkbox-pressed.png",
unchecked_menu = "buttons/checkbox.png",
wml_menu = "buttons/WML-custom.png",
level,
ellipsis,
missing,
// notifications icon
app_icon = "images/icons/icon-game.png";
} //images
//
// Sound constants
//
namespace sounds {
std::string
turn_bell = "bell.wav",
timer_bell = "timer.wav",
public_message = "chat-[1~3].ogg",
private_message = "chat-highlight.ogg",
friend_message = "chat-friend.ogg",
server_message = "receive.wav",
player_joins = "arrive.wav",
player_leaves = "leave.wav",
game_user_arrive = "join.wav",
game_user_leave = "leave.wav",
ready_for_start = "bell.wav",
game_has_begun = "gamestart.ogg";
const std::string
button_press = "button.wav",
checkbox_release = "checkbox.wav",
slider_adjust = "slider.wav",
menu_expand = "expand.wav",
menu_contract = "contract.wav",
menu_select = "select.wav";
namespace status {
std::string
poisoned = "poison.ogg",
slowed = "slowed.wav",
petrified = "petrified.ogg";
} // status
} // sounds
void load_config(const config &v)
{
base_income = v["base_income"].to_int(2);
village_income = v["village_income"].to_int(1);
village_support = v["village_support"].to_int(1);
poison_amount = v["poison_amount"].to_int(8);
rest_heal_amount = v["rest_heal_amount"].to_int(2);
recall_cost = v["recall_cost"].to_int(20);
kill_experience = v["kill_experience"].to_int(8);
lobby_refresh = v["lobby_refresh"].to_int(2000);
default_terrain = v["default_terrain"].str();
tile_size = v["tile_size"].to_int(72);
title_music = v["title_music"].str();
lobby_music = v["lobby_music"].str();
default_victory_music = v["default_victory_music"].str();
default_defeat_music = v["default_defeat_music"].str();
if(const config& i = v.child("colors")){
using namespace game_config::colors;
moved_orb_color = i["moved_orb_color"].str();
unmoved_orb_color = i["unmoved_orb_color"].str();
partial_orb_color = i["partial_orb_color"].str();
enemy_orb_color = i["enemy_orb_color"].str();
ally_orb_color = i["ally_orb_color"].str();
default_color_list = i["default_color_list"].str();
} // colors
show_ally_orb = v["show_ally_orb"].to_bool(true);
show_enemy_orb = v["show_enemy_orb"].to_bool(false);
show_moved_orb = v["show_moved_orb"].to_bool(true);
show_partial_orb = v["show_partly_orb"].to_bool(true);
show_unmoved_orb = v["show_unmoved_orb"].to_bool(true);
if(const config& i = v.child("images")){
using namespace game_config::images;
game_title = i["game_title"].str();
game_title_background = i["game_title_background"].str();
game_logo = i["game_logo"].str();
game_logo_background = i["game_logo_background"].str();
orb = i["orb"].str();
energy = i["energy"].str();
flag = i["flag"].str();
flag_icon = i["flag_icon"].str();
terrain_mask = i["terrain_mask"].str();
grid_top = i["grid_top"].str();
grid_bottom = i["grid_bottom"].str();
mouseover = i["mouseover"].str();
selected = i["selected"].str();
editor_brush = i["editor_brush"].str();
unreachable = i["unreachable"].str();
linger = i["linger"].str();
observer = i["observer"].str();
tod_bright = i["tod_bright"].str();
tod_dark = i["tod_dark"].str();
level = i["level"].str();
ellipsis = i["ellipsis"].str();
missing = i["missing"].str();
} // images
hp_bar_scaling = v["hp_bar_scaling"].to_double(0.666);
xp_bar_scaling = v["xp_bar_scaling"].to_double(0.5);
hex_brightening = v["hex_brightening"].to_double(1.25);
foot_speed_prefix = utils::split(v["footprint_prefix"]);
foot_teleport_enter = v["footprint_teleport_enter"].str();
foot_teleport_exit = v["footprint_teleport_exit"].str();
shroud_prefix = v["shroud_prefix"].str();
fog_prefix = v["fog_prefix"].str();
add_color_info(v);
if(const config::attribute_value* a = v.get("flag_rgb")) {
flag_rgb = a->str();
}
if(const config::attribute_value* a = v.get("unit_rgb")) {
unit_rgb = a->str();
}
const auto parse_config_color_list = [&](
const std::string& key,
const color_t fallback)->std::vector<color_t>
{
std::vector<color_t> color_vec;
for(const auto& s : utils::split(v[key].str())) {
try {
color_vec.push_back(color_t::from_hex_string(s));
} catch(std::invalid_argument& e) {
ERR_NG << "Error parsing color list '" << key << "'.\n" << e.what() << std::endl;
color_vec.push_back(fallback);
}
}
return color_vec;
};
red_green_scale = parse_config_color_list("red_green_scale", {255, 255, 255});
red_green_scale_text = parse_config_color_list("red_green_scale_text", {255, 255, 255});
blue_white_scale = parse_config_color_list("blue_white_scale", {0 , 0 , 255});
blue_white_scale_text = parse_config_color_list("blue_white_scale_text", {0 , 0 , 255});
server_list.clear();
for(const config& server : v.child_range("server")) {
server_info sinf;
sinf.name = server["name"].str();
sinf.address = server["address"].str();
server_list.push_back(sinf);
}
if(const config& s = v.child("sounds")) {
using namespace game_config::sounds;
const auto load_attribute = [](const config& c, const std::string& key, std::string& member) {
if(c.has_attribute(key)) {
member = c[key].str();
}
};
load_attribute(s, "turn_bell", turn_bell);
load_attribute(s, "timer_bell", timer_bell);
load_attribute(s, "public_message", public_message);
load_attribute(s, "private_message", private_message);
load_attribute(s, "friend_message", friend_message);
load_attribute(s, "server_message", server_message);
load_attribute(s, "player_joins", player_joins);
load_attribute(s, "player_leaves", player_leaves);
load_attribute(s, "game_user_arrive", game_user_arrive);
load_attribute(s, "game_user_leave", game_user_leave);
load_attribute(s, "ready_for_start", ready_for_start);
load_attribute(s, "game_has_begun", game_has_begun);
if(const config & ss = s.child("status")) {
using namespace game_config::sounds::status;
load_attribute(ss, "poisoned", poisoned);
load_attribute(ss, "slowed", slowed);
load_attribute(ss, "petrified", petrified);
}
}
}
void add_color_info(const config& v)
{
for(const config& teamC : v.child_range("color_range")) {
const config::attribute_value* a1 = teamC.get("id"), *a2 = teamC.get("rgb");
if(!a1 || !a2) {
continue;
}
std::string id = *a1;
std::vector<color_t> temp;
for(const auto& s : utils::split(*a2)) {
try {
temp.push_back(color_t::from_hex_string(s));
} catch(std::invalid_argument& e) {
std::stringstream ss;
ss << "can't parse color string:\n" << teamC.debug() << "\n";
throw config::error(ss.str());
}
}
team_rgb_range.insert({id, color_range(temp)});
team_rgb_name[id] = teamC["name"];
LOG_NG << "registered color range '" << id << "': " << team_rgb_range[id].debug() << '\n';
// Ggenerate palette of same name;
std::vector<color_t> tp = palette(team_rgb_range[id]);
if(tp.empty()) {
continue;
}
team_rgb_colors.insert({id, tp});
}
for(const config &cp : v.child_range("color_palette")) {
for(const config::attribute& rgb : cp.attribute_range()) {
std::vector<color_t> temp;
for(const auto& s : utils::split(rgb.second)) {
try {
temp.push_back(color_t::from_hex_string(s));
} catch(std::invalid_argument& e) {
ERR_NG << "Invalid color in palette: " << s << std::endl;
}
}
team_rgb_colors.insert({rgb.first, temp});
LOG_NG << "registered color palette: " << rgb.first << '\n';
}
}
default_colors.clear();
for(const auto& color : utils::split(game_config::colors::default_color_list)) {
if(team_rgb_name.find(color) != team_rgb_name.end()) {
default_colors.push_back(color);
}
}
}
const color_range& color_info(const std::string& name)
{
auto i = team_rgb_range.find(name);
if(i != team_rgb_range.end()) {
return i->second;
}
std::vector<color_t> temp;
for(const auto& s : utils::split(name)) {
try {
temp.push_back(color_t::from_hex_string(s));
} catch(std::invalid_argument& e) {
throw config::error(_("Invalid color in range: ") + s);
}
}
team_rgb_range.insert({name, color_range(temp)});
return color_info(name);
}
const std::vector<color_t>& tc_info(const std::string& name)
{
auto i = team_rgb_colors.find(name);
if(i != team_rgb_colors.end()) {
return i->second;
}
std::vector<color_t> temp;
try {
temp.push_back(color_t::from_hex_string(name));
} catch(std::invalid_argument& e) {
static std::vector<color_t> stv;
ERR_NG << "Invalid color palette: " << name << std::endl;
return stv;
}
team_rgb_colors.insert({name, temp});
return tc_info(name);
}
color_t red_to_green(int val, bool for_text)
{
const std::vector<color_t>& color_scale = for_text ? red_green_scale_text : red_green_scale;
val = util::clamp(val, 0, 100);
const int lvl = (color_scale.size() - 1) * val / 100;
return color_scale[lvl];
}
color_t blue_to_white(int val, bool for_text)
{
const std::vector<color_t>& color_scale = for_text ? blue_white_scale_text : blue_white_scale;
val = util::clamp(val, 0, 100);
const int lvl = (color_scale.size() - 1) * val / 100;
return color_scale[lvl];
}
std::string get_default_title_string()
{
std::string ret = _("The Battle for Wesnoth") + " - " + revision;
return ret;
}
} // game_config