-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
editor_map.cpp
399 lines (357 loc) · 10.5 KB
/
editor_map.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
/*
Copyright (C) 2008 - 2014 by Tomasz Sniatowski <kailoran@gmail.com>
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.
*/
#define GETTEXT_DOMAIN "wesnoth-editor"
#include "../action/action_base.hpp"
#include "editor_map.hpp"
#include "formula_string_utils.hpp"
#include "../../display.hpp"
#include "../../gettext.hpp"
#include "../../map_exception.hpp"
#include "../../map_label.hpp"
#include "../../wml_exception.hpp"
#include <boost/foreach.hpp>
namespace editor {
editor_map_load_exception wrap_exc(const char* type, const std::string& e_msg, const std::string& filename)
{
WRN_ED << type << " error in load map " << filename << ": " << e_msg << std::endl;
utils::string_map symbols;
symbols["type"] = type;
const char* error_msg = "There was an error ($type) while loading the file:";
std::string msg = vgettext(error_msg, symbols);
msg += "\n";
msg += e_msg;
return editor_map_load_exception(filename, msg);
}
editor_map::editor_map(const config& terrain_cfg)
: gamemap(terrain_cfg, gamemap::default_map_header)
, selection_()
{
}
editor_map::editor_map(const config& terrain_cfg, const std::string& data)
: gamemap(terrain_cfg, data)
, selection_()
{
sanity_check();
}
editor_map editor_map::from_string(const config& terrain_cfg, const std::string& data)
{
try {
return editor_map(terrain_cfg, data);
} catch (incorrect_map_format_error& e) {
throw wrap_exc("format", e.message, "");
} catch (twml_exception& e) {
throw wrap_exc("wml", e.user_message, "");
} catch (config::error& e) {
throw wrap_exc("config", e.message, "");
}
}
editor_map::editor_map(const config& terrain_cfg, size_t width, size_t height, const t_translation::t_terrain & filler)
: gamemap(terrain_cfg, gamemap::default_map_header + t_translation::write_game_map(
t_translation::t_map(width + 2, t_translation::t_list(height + 2, filler))))
, selection_()
{
sanity_check();
}
editor_map::editor_map(const gamemap& map)
: gamemap(map)
, selection_()
{
sanity_check();
}
editor_map::~editor_map()
{
}
void editor_map::sanity_check()
{
int errors = 0;
if (total_width() != static_cast<int>(tiles_.size())) {
ERR_ED << "total_width is " << total_width() << " but tiles_.size() is " << tiles_.size() << std::endl;
++errors;
}
if (total_height() != static_cast<int>(tiles_[0].size())) {
ERR_ED << "total_height is " << total_height() << " but tiles_[0].size() is " << tiles_.size() << std::endl;
++errors;
}
if (w() + 2 * border_size() != total_width()) {
ERR_ED << "h is " << h_ << " and border_size is " << border_size() << " but total_width is " << total_width() << std::endl;
++errors;
}
if (h() + 2 * border_size() != total_height()) {
ERR_ED << "w is " << w_ << " and border_size is " << border_size() << " but total_height is " << total_height() << std::endl;
++errors;
}
for (size_t i = 1; i < tiles_.size(); ++i) {
if (tiles_[i].size() != tiles_[0].size()) {
ERR_ED << "tiles_[ " << i << "] has size() " << tiles_[i].size() << " but tiles[0] has size() " << tiles_[0].size() << std::endl;
++errors;
}
}
BOOST_FOREACH(const map_location& loc, selection_) {
if (!on_board_with_border(loc)) {
ERR_ED << "Off-map tile in selection: " << loc << std::endl;
}
}
if (errors) {
throw editor_map_integrity_error();
}
}
std::set<map_location> editor_map::get_contiguous_terrain_tiles(const map_location& start) const
{
t_translation::t_terrain terrain = get_terrain(start);
std::set<map_location> result;
std::deque<map_location> queue;
result.insert(start);
queue.push_back(start);
//this is basically a breadth-first search along adjacent hexes
do {
map_location adj[6];
get_adjacent_tiles(queue.front(), adj);
for (int i = 0; i < 6; ++i) {
if (on_board_with_border(adj[i]) && get_terrain(adj[i]) == terrain
&& result.find(adj[i]) == result.end()) {
result.insert(adj[i]);
queue.push_back(adj[i]);
}
}
queue.pop_front();
} while (!queue.empty());
return result;
}
std::set<map_location> editor_map::set_starting_position_labels(display& disp)
{
std::set<map_location> label_locs;
std::string label = _("Player");
label += " ";
for (int i = 1; i <= gamemap::MAX_PLAYERS; i++) {
if (startingPositions_[i].valid()) {
disp.labels().set_label(startingPositions_[i], label + lexical_cast<std::string>(i));
label_locs.insert(startingPositions_[i]);
}
}
return label_locs;
}
bool editor_map::in_selection(const map_location& loc) const
{
return selection_.find(loc) != selection_.end();
}
bool editor_map::add_to_selection(const map_location& loc)
{
return on_board_with_border(loc) ? selection_.insert(loc).second : false;
}
bool editor_map::set_selection(const std::set<map_location>& area)
{
clear_selection();
BOOST_FOREACH(const map_location& loc, area) {
if (!add_to_selection(loc))
return false;
}
return true;
}
bool editor_map::remove_from_selection(const map_location& loc)
{
return selection_.erase(loc) != 0;
}
void editor_map::clear_selection()
{
selection_.clear();
}
void editor_map::invert_selection()
{
std::set<map_location> new_selection;
for (int x = -1; x < w() + 1; ++x) {
for (int y = -1; y < h() + 1; ++y) {
if (selection_.find(map_location(x, y)) == selection_.end()) {
new_selection.insert(map_location(x, y));
}
}
}
selection_.swap(new_selection);
}
void editor_map::select_all()
{
clear_selection();
invert_selection();
}
bool editor_map::everything_selected() const
{
LOG_ED << selection_.size() << " " << total_width() * total_height() << "\n";
return static_cast<int>(selection_.size()) == total_width() * total_height();
}
void editor_map::resize(int width, int height, int x_offset, int y_offset,
const t_translation::t_terrain & filler)
{
int old_w = w();
int old_h = h();
if (old_w == width && old_h == height && x_offset == 0 && y_offset == 0) {
return;
}
// Determine the amount of resizing is required
const int left_resize = -x_offset;
const int right_resize = (width - old_w) + x_offset;
const int top_resize = -y_offset;
const int bottom_resize = (height - old_h) + y_offset;
if(right_resize > 0) {
expand_right(right_resize, filler);
} else if(right_resize < 0) {
shrink_right(-right_resize);
}
if(bottom_resize > 0) {
expand_bottom(bottom_resize, filler);
} else if(bottom_resize < 0) {
shrink_bottom(-bottom_resize);
}
if(left_resize > 0) {
expand_left(left_resize, filler);
} else if(left_resize < 0) {
shrink_left(-left_resize);
}
if(top_resize > 0) {
expand_top(top_resize, filler);
} else if(top_resize < 0) {
shrink_top(-top_resize);
}
// fix the starting positions
if(x_offset || y_offset) {
for(size_t i = 0; i < MAX_PLAYERS+1; ++i) {
if(startingPositions_[i] != map_location()) {
startingPositions_[i].x -= x_offset;
startingPositions_[i].y -= y_offset;
}
}
}
sanity_check();
}
gamemap editor_map::mask_to(const gamemap& target) const
{
if (target.w() != w() || target.h() != h()) {
throw editor_action_exception(_("The size of the target map is different from the current map"));
}
gamemap mask(target);
map_location iter;
for (iter.x = -border_size(); iter.x < w() + border_size(); ++iter.x) {
for (iter.y = -border_size(); iter.y < h() + border_size(); ++iter.y) {
if (target.get_terrain(iter) == get_terrain(iter)) {
mask.set_terrain(iter, t_translation::FOGGED);
}
}
}
return mask;
}
bool editor_map::same_size_as(const gamemap& other) const
{
return h() == other.h()
&& w() == other.w();
}
t_translation::t_list editor_map::clone_column(int x, const t_translation::t_terrain & filler)
{
int h = tiles_[1].size();
t_translation::t_list column(h);
for (int y = 0; y < h; ++y) {
column[y] =
filler != t_translation::NONE_TERRAIN ?
filler :
tiles_[x][y];
assert(column[y] != t_translation::NONE_TERRAIN);
}
return column;
}
void editor_map::expand_right(int count, const t_translation::t_terrain & filler)
{
int w = tiles_.size();
for (int x = 0; x < count; ++x) {
tiles_.push_back(clone_column(w - 1 , filler));
}
w_ += count;
total_width_ += count;
}
void editor_map::expand_left(int count, const t_translation::t_terrain & filler)
{
for (int x = 0; x < count; ++x) {
tiles_.insert(tiles_.begin(), 1, clone_column(0, filler));
clear_border_cache();
}
w_ += count;
total_width_ += count;
}
void editor_map::expand_top(int count, const t_translation::t_terrain & filler)
{
for (int y = 0; y < count; ++y) {
for (int x = 0; x < static_cast<int>(tiles_.size()); ++x) {
t_translation::t_terrain terrain =
filler != t_translation::NONE_TERRAIN ?
filler :
tiles_[x][0];
assert(terrain != t_translation::NONE_TERRAIN);
tiles_[x].insert(tiles_[x].begin(), 1, terrain);
clear_border_cache();
}
}
h_ += count;
total_height_ += count;
}
void editor_map::expand_bottom(int count, const t_translation::t_terrain & filler)
{
int h = tiles_[1].size();
for (int y = 0; y < count; ++y) {
for (int x = 0; x < static_cast<int>(tiles_.size()); ++x) {
t_translation::t_terrain terrain =
filler != t_translation::NONE_TERRAIN ?
filler :
tiles_[x][h - 1];
assert(terrain != t_translation::NONE_TERRAIN);
tiles_[x].push_back(terrain);
}
}
h_ += count;
total_height_ += count;
}
void editor_map::shrink_right(int count)
{
if(count < 0 || count > static_cast<int>(tiles_.size())) {
throw editor_map_operation_exception();
}
tiles_.resize(tiles_.size() - count);
w_ -= count;
total_width_ -= count;
}
void editor_map::shrink_left(int count)
{
if(count < 0 || count > static_cast<int>(tiles_.size())) {
throw editor_map_operation_exception();
}
tiles_.erase(tiles_.begin(), tiles_.begin() + count);
w_ -= count;
total_width_ -= count;
}
void editor_map::shrink_top(int count)
{
if(count < 0 || count > static_cast<int>(tiles_[0].size())) {
throw editor_map_operation_exception();
}
for (size_t x = 0; x < tiles_.size(); ++x) {
tiles_[x].erase(tiles_[x].begin(), tiles_[x].begin() + count);
}
h_ -= count;
total_height_ -= count;
}
void editor_map::shrink_bottom(int count)
{
if(count < 0 || count > static_cast<int>(tiles_[0].size())) {
throw editor_map_operation_exception();
}
for (size_t x = 0; x < tiles_.size(); ++x) {
tiles_[x].erase(tiles_[x].end() - count, tiles_[x].end());
}
h_ -= count;
total_height_ -= count;
}
} //end namespace editor