The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
editor_map.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2008 - 2025
3 by Tomasz Sniatowski <kailoran@gmail.com>
4 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY.
12
13 See the COPYING file for more details.
14*/
15
16#define GETTEXT_DOMAIN "wesnoth-editor"
17
20
21#include "display.hpp"
23#include "gettext.hpp"
24#include "map/exception.hpp"
25#include "map/label.hpp"
27#include "wml_exception.hpp"
28
29namespace editor {
30
31editor_map_load_exception wrap_exc(const char* type, const std::string& e_msg, const std::string& filename)
32{
33 WRN_ED << type << " error in load map " << filename << ": " << e_msg;
34 utils::string_map symbols;
35 symbols["type"] = type;
36 const char* error_msg = "There was an error ($type) while loading the file:";
37 std::string msg = VGETTEXT(error_msg, symbols);
38 msg += "\n";
39 msg += e_msg;
41}
42
44 : gamemap("")
45 , selection_(*this)
46{
47}
48
50 : gamemap(data)
51 , selection_(*this)
52{
53}
54
56{
57 try {
58 return editor_map(data);
59 } catch (const incorrect_map_format_error& e) {
60 throw wrap_exc("format", e.message, "");
61 } catch (const wml_exception& e) {
62 throw wrap_exc("wml", e.user_message, "");
63 } catch (const config::error& e) {
64 throw wrap_exc("config", e.message, "");
65 }
66}
67
68editor_map::editor_map(std::size_t width, std::size_t height, const t_translation::terrain_code & filler)
69 : gamemap(width + 2, height + 2, filler)
70 , selection_(*this)
71{
72}
73
75 : gamemap(map)
76 , selection_(*this)
77{
78}
79
81{
83 std::set<map_location> result;
84 std::deque<map_location> queue;
85 result.insert(start);
86 queue.push_back(start);
87 //this is basically a breadth-first search along adjacent hexes
88 do {
89 for(const map_location& adj : get_adjacent_tiles(queue.front())) {
90 if (on_board_with_border(adj) && get_terrain(adj) == terrain
91 && result.find(adj) == result.end()) {
92 result.insert(adj);
93 queue.push_back(adj);
94 }
95 }
96 queue.pop_front();
97 } while (!queue.empty());
98 return result;
99}
100
102{
103 std::set<map_location> label_locs;
104 std::string label;
105
106
107 for (const auto& pair : special_locations().left) {
108
109 bool is_number = std::find_if(pair.first.begin(), pair.first.end(), [](char c) { return !utils::portable_isdigit(c); }) == pair.first.end();
110 if (is_number) {
111 label = VGETTEXT("Player $side_num", utils::string_map{ { "side_num", pair.first } });
112 }
113 else {
114 label = pair.first;
115 }
116
117 disp.labels().set_label(pair.second, label);
118 label_locs.insert(pair.second);
119 }
120 return label_locs;
121}
122
127
132
133void editor_map::set_selection(const std::set<map_location>& area)
134{
136 for(const map_location& loc : area) {
138 }
139}
140
141std::set<map_location> editor_map::selection() const
142{
144}
145
146std::set<map_location> editor_map::selection_inverse() const
147{
149}
150
155
160
165
170
172{
173 return selection_.mask().all();
174}
175
177{
178 return selection_.mask().any();
179}
180
182{
183 return selection_.mask().none();
184}
185
186std::size_t editor_map::num_selected() const
187{
188 return selection_.mask().count();
189}
190
191void editor_map::resize(int width, int height, int x_offset, int y_offset,
192 const t_translation::terrain_code & filler)
193{
194 int old_w = w();
195 int old_h = h();
196 if (old_w == width && old_h == height && x_offset == 0 && y_offset == 0) {
197 return;
198 }
199
200 // Save the old selections before resizing
201 std::set<map_location> old_selection = selection();
202
203 // Determine the amount of resizing is required
204 const int left_resize = -x_offset;
205 const int right_resize = (width - old_w) + x_offset;
206 const int top_resize = -y_offset;
207 const int bottom_resize = (height - old_h) + y_offset;
208
209 if(right_resize > 0) {
210 expand_right(right_resize, filler);
211 } else if(right_resize < 0) {
212 shrink_right(-right_resize);
213 }
214 if(bottom_resize > 0) {
215 expand_bottom(bottom_resize, filler);
216 } else if(bottom_resize < 0) {
217 shrink_bottom(-bottom_resize);
218 }
219 if(left_resize > 0) {
220 expand_left(left_resize, filler);
221 } else if(left_resize < 0) {
222 shrink_left(-left_resize);
223 }
224 if(top_resize > 0) {
225 expand_top(top_resize, filler);
226 } else if(top_resize < 0) {
227 shrink_top(-top_resize);
228 }
229
230 // Reset and resize selection mask
231 selection_ = selection_mask{*this};
232
233 for(const map_location& loc : old_selection) {
234 add_to_selection(loc.plus(-x_offset, -y_offset));
235 }
236
237 // fix the starting positions
238 if(x_offset || y_offset) {
239 for (auto it = special_locations().left.begin(); it != special_locations().left.end(); ++it) {
240 special_locations().left.modify_data(it, [=](t_translation::coordinate & loc) { loc.add(-x_offset, -y_offset); });
241 }
242 }
243
244 villages_.clear();
245
246 //
247 // NOTE: I'm not sure how inefficient it is to check every loc for its village-ness as
248 // opposed to operating on the villages_ vector itself and figuring out how to handle
249 // villages on the map border. Essentially, it's possible to simply remove all members
250 // from villages_ that are no longer on the map after a resize (including those that
251 // land on a border), but that doesn't account for villages that were *on* the border
252 // prior to resizing. Those should be included. As a catch-all fix, I just check every
253 // hex. It's possible that any more complex shenanigans would be even more inefficient.
254 //
255 // -- vultraz, 2018-02-25
256 //
257 for_each_loc([this](const map_location& loc) {
258 if(is_village(loc)) {
259 villages_.push_back(loc);
260 }
261 });
262}
263
265{
266 if (target.w() != w() || target.h() != h()) {
267 throw editor_action_exception(_("The size of the target map is different from the current map"));
268 }
269 gamemap mask(target);
270 map_location iter;
271 for (iter.x = -border_size(); iter.x < w() + border_size(); ++iter.x) {
272 for (iter.y = -border_size(); iter.y < h() + border_size(); ++iter.y) {
273 if (target.get_terrain(iter) == get_terrain(iter)) {
275 }
276 }
277 }
278 return mask;
279}
280
281bool editor_map::same_size_as(const gamemap& other) const
282{
283 return h() == other.h()
284 && w() == other.w();
285}
286
288{
289 t_translation::ter_map tiles_new(tiles().w + count, tiles().h);
290 for (int x = 0, x_end = tiles().w; x != x_end; ++x) {
291 for (int y = 0, y_end = tiles().h; y != y_end; ++y) {
292 tiles_new.get(x, y) = tiles().get(x, y);
293 }
294 }
295 for (int x = tiles().w, x_end = tiles().w + count; x != x_end; ++x) {
296 for (int y = 0, y_end = tiles().h; y != y_end; ++y) {
297 tiles_new.get(x, y) = filler == t_translation::NONE_TERRAIN ? tiles().get(tiles().w - 1, y) : filler;
298 }
299 }
300 tiles() = std::move(tiles_new);
301}
302
304{
305 t_translation::ter_map tiles_new(tiles().w + count, tiles().h);
306 for (int x = 0, x_end = tiles().w; x != x_end; ++x) {
307 for (int y = 0, y_end = tiles().h; y != y_end; ++y) {
308 tiles_new.get(x + count, y) = tiles().get(x, y);
309 }
310 }
311 for (int x = 0, x_end = count; x != x_end; ++x) {
312 for (int y = 0, y_end = tiles().h; y != y_end; ++y) {
313 tiles_new.get(x, y) = filler == t_translation::NONE_TERRAIN ? tiles().get(0, y) : filler;
314 }
315 }
316 tiles() = std::move(tiles_new);
317}
318
320{
321 t_translation::ter_map tiles_new(tiles().w, tiles().h + count);
322 for (int x = 0, x_end = tiles().w; x != x_end; ++x) {
323 for (int y = 0, y_end = tiles().h; y != y_end; ++y) {
324 tiles_new.get(x, y + count) = tiles().get(x, y);
325 }
326 }
327 for (int x = 0, x_end = tiles().w; x != x_end; ++x) {
328 for (int y = 0, y_end = count; y != y_end; ++y) {
329 tiles_new.get(x, y) = filler == t_translation::NONE_TERRAIN ? tiles().get(x, 0) : filler;
330 }
331 }
332 tiles() = std::move(tiles_new);
333}
334
336{
337 t_translation::ter_map tiles_new(tiles().w, tiles().h + count);
338 for (int x = 0, x_end = tiles().w; x != x_end; ++x) {
339 for (int y = 0, y_end = tiles().h; y != y_end; ++y) {
340 tiles_new.get(x, y) = tiles().get(x, y);
341 }
342 }
343 for (int x = 0, x_end = tiles().w; x != x_end; ++x) {
344 for (int y = tiles().h, y_end = tiles().h + count; y != y_end; ++y) {
345 tiles_new.get(x, y) = filler == t_translation::NONE_TERRAIN ? tiles().get(x, tiles().h - 1) : filler;
346 }
347 }
348 tiles() = std::move(tiles_new);
349}
350
352{
353 if(count < 0 || count > tiles().w) {
355 }
356 t_translation::ter_map tiles_new(tiles().w - count, tiles().h);
357 for (int x = 0, x_end = tiles_new.w; x != x_end; ++x) {
358 for (int y = 0, y_end = tiles_new.h; y != y_end; ++y) {
359 tiles_new.get(x, y) = tiles().get(x, y);
360 }
361 }
362 tiles() = std::move(tiles_new);
363}
364
366{
367 if (count < 0 || count > tiles().w) {
369 }
370 t_translation::ter_map tiles_new(tiles().w - count, tiles().h);
371 for (int x = 0, x_end = tiles_new.w; x != x_end; ++x) {
372 for (int y = 0, y_end = tiles_new.h; y != y_end; ++y) {
373 tiles_new.get(x, y) = tiles().get(x + count, y);
374 }
375 }
376 tiles() = std::move(tiles_new);
377}
378
380{
381 if (count < 0 || count > tiles().h) {
383 }
384 t_translation::ter_map tiles_new(tiles().w, tiles().h - count);
385 for (int x = 0, x_end = tiles_new.w; x != x_end; ++x) {
386 for (int y = 0, y_end = tiles_new.h; y != y_end; ++y) {
387 tiles_new.get(x, y) = tiles().get(x, y + count);
388 }
389 }
390 tiles() = std::move(tiles_new);
391}
392
394{
395 if (count < 0 || count > tiles().h) {
397 }
398 t_translation::ter_map tiles_new(tiles().w, tiles().h - count);
399 for (int x = 0, x_end = tiles_new.w; x != x_end; ++x) {
400 for (int y = 0, y_end = tiles_new.h; y != y_end; ++y) {
401 tiles_new.get(x, y) = tiles().get(x, y);
402 }
403 }
404 tiles() = std::move(tiles_new);
405}
406
407
408
409} //end namespace editor
Base class for editor actions.
map_location loc
Definition move.cpp:172
Sort-of-Singleton that many classes, both GUI and non-GUI, use to access the game data.
Definition display.hpp:88
map_labels & labels()
Definition display.cpp:2392
bool select(const map_location &loc)
Marks loc as selected.
static std::set< map_location > get_locations(const selection_mask &mask)
selection_mask inverted() const
bool selected(const map_location &loc) const
bool deselect(const map_location &loc)
Marks loc as unselected.
const boost::dynamic_bitset< uint64_t > & mask() const
Read-only access to the underlying bitset.
This class adds extra editor-specific functionality to a normal gamemap.
void shrink_left(int count)
bool everything_selected() const
bool add_to_selection(const map_location &loc)
Add a location to the selection.
bool anything_selected() const
void shrink_top(int count)
void expand_left(int count, const t_translation::terrain_code &filler)
selection_mask selection_
The selected hexes.
std::set< map_location > set_starting_position_labels(display &disp)
Set labels for staring positions in the given display object.
void shrink_bottom(int count)
static editor_map from_string(std::string_view data)
Wrapper around editor_map(cfg, data) that catches possible exceptions and wraps them in a editor_map_...
void invert_selection()
Invert the selection, i.e.
void shrink_right(int count)
void select_all()
Select all map hexes.
bool same_size_as(const gamemap &other) const
A precondition to several map operations.
gamemap mask_to(const gamemap &target) const
A sort-of diff operation returning a mask that, when applied to the current editor_map,...
std::set< map_location > selection() const
Return the selection set.
bool nothing_selected() const
void expand_right(int count, const t_translation::terrain_code &filler)
bool in_selection(const map_location &loc) const
bool remove_from_selection(const map_location &loc)
Remove a location to the selection.
void expand_top(int count, const t_translation::terrain_code &filler)
void set_selection(const std::set< map_location > &area)
Select the given area.
std::set< map_location > get_contiguous_terrain_tiles(const map_location &start) const
Get a contiguous set of tiles having the same terrain as the starting location.
void resize(int width, int height, int x_offset, int y_offset, const t_translation::terrain_code &filler=t_translation::NONE_TERRAIN)
Resize the map.
std::set< map_location > selection_inverse() const
Returns a set of all hexes not currently selected.
editor_map()
Empty map constructor.
void clear_selection()
Clear the selection.
void expand_bottom(int count, const t_translation::terrain_code &filler)
std::size_t num_selected() const
terrain_code get_terrain(const map_location &loc) const
Looks up terrain at a particular location.
Definition map.cpp:265
int w() const
Effective map width.
Definition map.hpp:50
location_map & special_locations()
Definition map.hpp:105
int h() const
Effective map height.
Definition map.hpp:53
void for_each_loc(const F &f) const
Definition map.hpp:140
bool on_board_with_border(const map_location &loc) const
Definition map.cpp:352
terrain_map & tiles()
Definition map.hpp:161
int border_size() const
Size of the map border.
Definition map.hpp:56
Encapsulates the map of the game.
Definition map.hpp:176
bool is_village(const map_location &loc) const
Definition map.cpp:60
std::vector< map_location > villages_
Definition map.hpp:252
gamemap_base::set_terrain_result set_terrain(const map_location &loc, const terrain_code &terrain, const terrain_type_data::merge_mode mode=terrain_type_data::BOTH, bool replace_if_failed=false) override
Clobbers over the terrain at location 'loc', with the given terrain.
Definition map.cpp:359
const terrain_label * set_label(const map_location &loc, const t_string &text, const int creator=-1, const std::string &team="", const color_t color=font::NORMAL_COLOR, const bool visible_in_fog=true, const bool visible_in_shroud=false, const bool immutable=false, const std::string &category="", const t_string &tooltip="")
Definition label.cpp:148
map_display and display: classes which take care of displaying the map and game-data on the screen.
#define WRN_ED
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
static std::string _(const char *str)
Definition gettext.hpp:100
std::string label
What to show in the filter's drop-down list.
Definition manager.cpp:201
void get_adjacent_tiles(const map_location &a, utils::span< map_location, 6 > res)
Function which, given a location, will place all adjacent locations in res.
Definition location.cpp:513
Manage the empty-palette in the editor.
Definition action.cpp:31
editor_map_load_exception wrap_exc(const char *type, const std::string &e_msg, const std::string &filename)
Exception wrapping utility.
EXIT_STATUS start(bool clear_id, const std::string &filename, bool take_screenshot, const std::string &screenshot_filename)
Main interface for launching the editor from the title screen.
constexpr terrain_code NONE_TERRAIN
const terrain_code FOGGED
std::map< std::string, t_string > string_map
std::string_view data
Definition picture.cpp:188
std::string filename
Filename.
Encapsulates the map of the game.
Definition location.hpp:46
void add(int x_diff, int y_diff)
Definition location.hpp:192
map_location plus(int x_diff, int y_diff) const
Definition location.hpp:193
terrain_code & get(int x, int y)
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Helper class, don't construct this directly.
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
#define e
#define c