-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
terrain.cpp
405 lines (355 loc) · 13.5 KB
/
terrain.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
/*
Copyright (C) 2003 - 2014 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 "gettext.hpp"
#include "log.hpp"
#include "serialization/string_utils.hpp"
#include "terrain.hpp"
#include "util.hpp"
#include <boost/foreach.hpp>
#include <set>
static lg::log_domain log_config("config");
#define ERR_G LOG_STREAM(err, lg::general)
#define WRN_G LOG_STREAM(warn, lg::general)
#define LOG_G LOG_STREAM(info, lg::general)
#define DBG_G LOG_STREAM(debug, lg::general)
terrain_type::terrain_type() :
minimap_image_(),
minimap_image_overlay_(),
editor_image_(),
id_(),
name_(),
editor_name_(),
description_(),
help_topic_text_(),
number_(t_translation::VOID_TERRAIN),
mvt_type_(1, t_translation::VOID_TERRAIN),
vision_type_(1, t_translation::VOID_TERRAIN),
def_type_(1, t_translation::VOID_TERRAIN),
union_type_(1, t_translation::VOID_TERRAIN),
height_adjust_(0),
height_adjust_set_(false),
submerge_(0.0),
submerge_set_(false),
light_modification_(0),
max_light_(0),
min_light_(0),
heals_(0),
income_description_(),
income_description_ally_(),
income_description_enemy_(),
income_description_own_(),
editor_group_(),
village_(false),
castle_(false),
keep_(false),
overlay_(false),
combined_(false),
editor_default_base_(t_translation::VOID_TERRAIN),
hide_help_(false),
hide_in_editor_(false)
{}
terrain_type::terrain_type(const config& cfg) :
icon_image_(cfg["icon_image"]),
minimap_image_(cfg["symbol_image"]),
minimap_image_overlay_(),
editor_image_(cfg["editor_image"].empty() ? "terrain/" + minimap_image_ + ".png" : "terrain/" + cfg["editor_image"].str() + ".png"),
id_(cfg["id"]),
name_(cfg["name"].t_str()),
editor_name_(cfg["editor_name"].t_str()),
description_(cfg["description"].t_str()),
help_topic_text_(cfg["help_topic_text"].t_str()),
number_(t_translation::read_terrain_code(cfg["string"])),
mvt_type_(),
vision_type_(),
def_type_(),
union_type_(),
height_adjust_(cfg["unit_height_adjust"]),
height_adjust_set_(!cfg["unit_height_adjust"].empty()),
submerge_(cfg["submerge"].to_double()),
submerge_set_(!cfg["submerge"].empty()),
light_modification_(cfg["light"]),
max_light_(cfg["max_light"].to_int(light_modification_)),
min_light_(cfg["min_light"].to_int(light_modification_)),
heals_(cfg["heals"]),
income_description_(),
income_description_ally_(),
income_description_enemy_(),
income_description_own_(),
editor_group_(cfg["editor_group"]),
village_(cfg["gives_income"].to_bool()),
castle_(cfg["recruit_onto"].to_bool()),
keep_(cfg["recruit_from"].to_bool()),
overlay_(number_.base == t_translation::NO_LAYER),
combined_(false),
editor_default_base_(t_translation::read_terrain_code(cfg["default_base"])),
hide_help_(cfg["hide_help"].to_bool(false)),
hide_in_editor_(cfg["hidden"].to_bool(false))
{
/**
* @todo reenable these validations. The problem is that all MP
* scenarios/campaigns share the same namespace and one rogue scenario
* can avoid the player to create a MP game. So every scenario/campaign
* should get its own namespace to be safe.
*/
#if 0
VALIDATE(number_ != t_translation::NONE_TERRAIN,
missing_mandatory_wml_key("terrain_type", "string"));
VALIDATE(!minimap_image_.empty(),
missing_mandatory_wml_key("terrain_type", "symbol_image", "string",
t_translation::write_terrain_code(number_)));
VALIDATE(!name_.empty(),
missing_mandatory_wml_key("terrain_type", "name", "string",
t_translation::write_terrain_code(number_)));
#endif
if(editor_image_.empty()) {
editor_image_ = "terrain/" + minimap_image_ + ".png";
}
if(hide_in_editor_) {
editor_image_ = "";
}
mvt_type_.push_back(number_);
def_type_.push_back(number_);
vision_type_.push_back(number_);
const t_translation::t_list& alias = t_translation::read_list(cfg["aliasof"]);
if(!alias.empty()) {
mvt_type_ = alias;
vision_type_ = alias;
def_type_ = alias;
}
const t_translation::t_list& mvt_alias = t_translation::read_list(cfg["mvt_alias"]);
if(!mvt_alias.empty()) {
mvt_type_ = mvt_alias;
}
const t_translation::t_list& def_alias = t_translation::read_list(cfg["def_alias"]);
if(!def_alias.empty()) {
def_type_ = def_alias;
}
const t_translation::t_list& vision_alias = t_translation::read_list(cfg["vision_alias"]);
if(!vision_alias.empty()) {
vision_type_ = vision_alias;
}
union_type_ = mvt_type_;
union_type_.insert( union_type_.end(), def_type_.begin(), def_type_.end() );
union_type_.insert( union_type_.end(), vision_type_.begin(), vision_type_.end() );
// remove + and -
union_type_.erase(std::remove(union_type_.begin(), union_type_.end(),
t_translation::MINUS), union_type_.end());
union_type_.erase(std::remove(union_type_.begin(), union_type_.end(),
t_translation::PLUS), union_type_.end());
// remove doubles
std::sort(union_type_.begin(),union_type_.end());
union_type_.erase(std::unique(union_type_.begin(), union_type_.end()), union_type_.end());
//mouse over message are only shown on villages
if(village_) {
income_description_ = cfg["income_description"];
if(income_description_ == "") {
income_description_ = _("Village");
}
income_description_ally_ = cfg["income_description_ally"];
if(income_description_ally_ == "") {
income_description_ally_ = _("Allied village");
}
income_description_enemy_ = cfg["income_description_enemy"];
if(income_description_enemy_ == "") {
income_description_enemy_ = _("Enemy village");
}
income_description_own_ = cfg["income_description_own"];
if(income_description_own_ == "") {
income_description_own_ = _("Owned village");
}
}
}
terrain_type::terrain_type(const terrain_type& base, const terrain_type& overlay) :
icon_image_(),
minimap_image_(base.minimap_image_),
minimap_image_overlay_(overlay.minimap_image_),
editor_image_(base.editor_image_ + "~BLIT(" + overlay.editor_image_ +")"),
id_(base.id_+"^"+overlay.id_),
name_(overlay.name_),
editor_name_(base.editor_name_ + " / " + overlay.editor_name_),
description_(overlay.description()),
help_topic_text_(),
number_(t_translation::t_terrain(base.number_.base, overlay.number_.overlay)),
mvt_type_(overlay.mvt_type_),
vision_type_(overlay.vision_type_),
def_type_(overlay.def_type_),
union_type_(),
height_adjust_(base.height_adjust_),
height_adjust_set_(base.height_adjust_set_),
submerge_(base.submerge_),
submerge_set_(base.submerge_set_),
light_modification_(base.light_modification_ + overlay.light_modification_),
max_light_(std::max(base.max_light_, overlay.max_light_)),
min_light_(std::min(base.min_light_, overlay.min_light_)),
heals_(std::max<int>(base.heals_, overlay.heals_)),
income_description_(),
income_description_ally_(),
income_description_enemy_(),
income_description_own_(),
editor_group_(),
village_(base.village_ || overlay.village_),
castle_(base.castle_ || overlay.castle_),
keep_(base.keep_ || overlay.keep_),
overlay_(false),
combined_(true),
editor_default_base_(),
hide_help_(base.hide_help_ || overlay.hide_help_),
hide_in_editor_(base.hide_in_editor_ || overlay.hide_in_editor_)
{
if(description_.empty()) {
description_ = base.description();
}
if(overlay.height_adjust_set_) {
height_adjust_set_ = true;
height_adjust_ = overlay.height_adjust_;
}
if(overlay.submerge_set_) {
submerge_set_ = true;
submerge_ = overlay.submerge_;
}
merge_alias_lists(mvt_type_, base.mvt_type_);
merge_alias_lists(def_type_, base.def_type_);
merge_alias_lists(vision_type_, base.vision_type_);
union_type_ = mvt_type_;
union_type_.insert( union_type_.end(), def_type_.begin(), def_type_.end() );
union_type_.insert( union_type_.end(), vision_type_.begin(), vision_type_.end() );
// remove + and -
union_type_.erase(std::remove(union_type_.begin(), union_type_.end(),
t_translation::MINUS), union_type_.end());
union_type_.erase(std::remove(union_type_.begin(), union_type_.end(),
t_translation::PLUS), union_type_.end());
// remove doubles
std::sort(union_type_.begin(),union_type_.end());
union_type_.erase(std::unique(union_type_.begin(), union_type_.end()), union_type_.end());
//mouse over message are only shown on villages
if(base.village_) {
income_description_ = base.income_description_;
income_description_ally_ = base.income_description_ally_;
income_description_enemy_ = base.income_description_enemy_;
income_description_own_ = base.income_description_own_;
}
else if (overlay.village_) {
income_description_ = overlay.income_description_;
income_description_ally_ = overlay.income_description_ally_;
income_description_enemy_ = overlay.income_description_enemy_;
income_description_own_ = overlay.income_description_own_;
}
}
t_translation::t_terrain terrain_type::terrain_with_default_base() const {
if(overlay_ && editor_default_base_ != t_translation::NONE_TERRAIN) {
return t_translation::t_terrain(editor_default_base_.base, number_.overlay);
}
return number_;
}
bool terrain_type::operator==(const terrain_type& other) const {
return minimap_image_ == other.minimap_image_
&& minimap_image_overlay_ == other.minimap_image_overlay_
&& editor_image_ == other.editor_image_
&& id_ == other.id_
&& name_.base_str() == other.name_.base_str()
&& editor_name_.base_str() == other.editor_name_.base_str()
&& number_ == other.number_
&& height_adjust_ == other.height_adjust_
&& height_adjust_set_ == other.height_adjust_set_
&& submerge_ == other.submerge_
&& submerge_set_ == other.submerge_set_
&& light_modification_ == other.light_modification_
&& max_light_ == other.max_light_
&& min_light_ == other.min_light_
&& heals_ == other.heals_
&& village_ == other.village_
&& castle_ == other.castle_
&& keep_ == other.keep_
&& editor_default_base_ == other.editor_default_base_
&& hide_in_editor_ == other.hide_in_editor_
&& hide_help_ == other.hide_help_;
}
void create_terrain_maps(const config::const_child_itors &cfgs,
t_translation::t_list& terrain_list,
std::map<t_translation::t_terrain, terrain_type>& letter_to_terrain)
{
BOOST_FOREACH(const config &t, cfgs)
{
terrain_type terrain(t);
DBG_G << "create_terrain_maps: " << terrain.number() << " "
<< terrain.id() << " " << terrain.name() << " : " << terrain.editor_group() << "\n";
std::pair<std::map<t_translation::t_terrain, terrain_type>::iterator, bool> res;
res = letter_to_terrain.insert(std::make_pair(terrain.number(), terrain));
if (!res.second) {
terrain_type& curr = res.first->second;
if(terrain == curr) {
LOG_G << "Merging terrain " << terrain.number()
<< ": " << terrain.id() << " (" << terrain.name() << ")\n";
std::vector<std::string> eg1 = utils::split(curr.editor_group());
std::vector<std::string> eg2 = utils::split(terrain.editor_group());
std::set<std::string> egs;
bool clean_merge = true;
BOOST_FOREACH(std::string& t, eg1)
clean_merge &= egs.insert(t).second;
BOOST_FOREACH(std::string& t, eg2)
clean_merge &= egs.insert(t).second;
std::string joined = utils::join(egs);
curr.set_editor_group(joined);
if(clean_merge) {
LOG_G << "Editor groups merged to: " << joined << "\n";
} else {
LOG_G << "Merged terrain " << terrain.number()
<< ": " << terrain.id() << " (" << terrain.name() << ") "
<< "with duplicate editor groups [" << terrain.editor_group() << "] "
<< "and [" << curr.editor_group() << "]\n";
}
} else {
ERR_G << "Duplicate terrain code definition found for " << terrain.number() << std::endl;
ERR_G << "Failed to add terrain "
<< terrain.id() << " (" << terrain.name() << ") "
<< "[" << terrain.editor_group() << "]" << "\n";
ERR_G << "which conflicts with "
<< curr.id() << " (" << curr.name() << ") "
<< "[" << curr.editor_group() << "]" << "\n";
}
} else {
terrain_list.push_back(terrain.number());
}
}
}
void merge_alias_lists(t_translation::t_list& first, const t_translation::t_list& second)
{
// Insert second vector into first when the terrain _ref^base is encountered
bool revert = (first.front() == t_translation::MINUS ? true : false);
t_translation::t_list::iterator i;
for(i = first.begin(); i != first.end(); ++i) {
if(*i == t_translation::PLUS) {
revert = false;
continue;
} else if(*i == t_translation::MINUS) {
revert = true;
continue;
}
if(*i == t_translation::BASE) {
t_translation::t_list::iterator insert_it = first.erase(i);
//if we are in reverse mode, insert PLUS before and MINUS after the base list
//so calculation of base aliases will work normal
if(revert) {
// insert_it = first.insert(insert_it, t_translation::PLUS);
// insert_it++;
insert_it = first.insert(insert_it, t_translation::MINUS);
}
else {
//else insert PLUS after the base aliases to restore previous "reverse state"
insert_it = first.insert(insert_it, t_translation::PLUS);
}
first.insert(insert_it, second.begin(), second.end());
break;
}
}
}