The Battle for Wesnoth  1.19.15+dev
map_context.hpp
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 #pragma once
17 
18 #include "display_context.hpp"
20 #include "game_classification.hpp"
21 #include "map/label.hpp"
22 #include "mp_game_settings.hpp"
23 #include "overlay.hpp"
24 #include "side_controller.hpp"
25 #include "sound_music_track.hpp"
26 #include "team.hpp"
27 #include "tod_manager.hpp"
28 #include "units/map.hpp"
29 #include "utils/optional_fwd.hpp"
30 
31 #include <vector>
32 class game_config_view;
33 
34 namespace editor {
35 
37  editor_team_info(const team& t);
38 
39  int side;
40  std::string id;
41  std::string name;
42  std::string recruit_list;
43  int gold;
44  int income;
47  bool fog;
48  bool shroud;
51  bool no_leader;
52  bool hidden;
53 };
54 
55 /**
56  * This class wraps around a map to provide a concise interface for the editor to work with.
57  * The actual map object can change rapidly (be assigned to), the map context persists
58  * data (like the undo stacks) in this case. The functionality is here, not in editor_controller
59  * as e.g. the undo stack is part of the map, not the editor as a whole. This might allow many
60  * maps to be open at the same time.
61  */
63 {
64 public:
65  map_context(const map_context&) = delete;
66  map_context& operator=(const map_context&) = delete;
67 
68  /**
69  * Create a map context from an existing map. The filename is set to be
70  * empty, indicating a new map.
71  * Marked "explicit" to avoid automatic conversions.
72  */
73  explicit map_context(const editor_map& map, bool pure_map, const config& schedule, const std::string& addon_id);
74 
75  /**
76  * Create map_context from a map file. If the map cannot be loaded, an
77  * exception will be thrown and the object will not be constructed. If the
78  * map file is a scenario, the map specified in its map_data key will be
79  * loaded, and the stored filename updated accordingly. Maps embedded
80  * inside scenarios do not change the filename, but set the "embedded" flag
81  * instead.
82  */
83  map_context(const game_config_view& game_config, const std::string& filename, const std::string& addon_id);
84 
85  /**
86  * Map context destructor
87  */
88  virtual ~map_context();
89 
90  /**
91  * Select the nth tod area.
92  * @param index of the tod area to select.
93  */
94  bool select_area(int index);
95 
96  /** Adds a new side to the map */
97  void new_side();
98 
99  /** removes the last side from the scenario */
100  void remove_side() {
101  teams_.pop_back();
103  }
104 
105  void save_area(const std::set<map_location>& area) {
106  tod_manager_->replace_area_locations(active_area_, area);
107  }
108 
109  void new_area(const std::set<map_location>& area) {
110  tod_manager_->add_time_area("", area, config());
111  active_area_ = tod_manager_->get_area_ids().size() -1;
113  }
114 
115  void remove_area(int index);
116 
118  return labels_;
119  }
120 
121  void replace_schedule(const std::vector<time_of_day>& schedule);
122 
123  // Import symbols from base class.
126  using display_context::map;
127 
128  /** Const units accessor. */
129  virtual const unit_map& units() const override
130  {
131  return units_;
132  }
133 
134  /** Local non-const overload of @ref units */
136  {
137  return units_;
138  }
139 
140  /** Const teams accessor. */
141  virtual const std::vector<team>& teams() const override
142  {
143  return teams_;
144  }
145 
146  /** Local non-const overload of @ref teams */
147  std::vector<team>& teams()
148  {
149  return teams_;
150  }
151 
152  /** Const map accessor. */
153  virtual const editor_map& map() const override
154  {
155  return map_;
156  }
157 
158  /** Local non-const overload of @ref map */
160  {
161  return map_;
162  }
163 
164  virtual const std::vector<std::string>& hidden_label_categories() const override
165  {
166  return lbl_categories_;
167  }
168 
169  virtual std::vector<std::string>& hidden_label_categories() override
170  {
171  return lbl_categories_;
172  }
173 
174  /**
175  * Replace the [time]s of the currently active area.
176  */
177  void replace_local_schedule(const std::vector<time_of_day>& schedule);
178 
179  void set_starting_time(int time);
180 
181  void set_local_starting_time(int time) {
182  tod_manager_->set_current_time(time, active_area_);
184  }
185 
186  const tod_manager* get_time_manager() const {
187  return tod_manager_.get();
188  }
189 
191  return tod_manager_.get();
192  }
193 
195  return mp_settings_;
196  }
198  return game_classification_;
199  }
200 
201  /**
202  * @return the index of the currently active area.
203  */
204  int get_active_area() const {
205  return active_area_;
206  }
207 
208  void set_active_area(int index) {
210  }
211 
212  /** Checks whether the given track is part of current playlist. */
213  bool playlist_contains(const std::shared_ptr<sound::music_track>& track) const;
214 
215  /** Remove the given track from the current playlist if present, else appends it. */
216  void toggle_track(const std::shared_ptr<sound::music_track>& track);
217 
218  /**
219  * Draw a terrain on a single location on the map.
220  * Sets the refresh flags accordingly.
221  */
222  void draw_terrain(const t_translation::terrain_code & terrain, const map_location& loc,
223  bool one_layer_only = false);
224 
225  /**
226  * Actual drawing function used by both overloaded variants of draw_terrain.
227  */
229  bool one_layer_only = false);
230 
231  /**
232  * Draw a terrain on a set of locations on the map.
233  * Sets the refresh flags accordingly.
234  */
235  void draw_terrain(const t_translation::terrain_code & terrain, const std::set<map_location>& locs,
236  bool one_layer_only = false);
237 
238  /**
239  * Getter for the reload flag. Reload is the highest level of required refreshing,
240  * set when the map size has changed or the map was reassigned.
241  */
242  bool needs_reload() const { return needs_reload_; }
243 
244  /**
245  * Setter for the reload flag
246  */
247  void set_needs_reload(bool value=true) { needs_reload_ = value; }
248 
249  /**
250  * Getter for the terrain rebuild flag. Set whenever any terrain has changed.
251  */
253 
254  /**
255  * Setter for the terrain rebuild flag
256  */
257  void set_needs_terrain_rebuild(bool value=true) { needs_terrain_rebuild_ = value; }
258 
259  void set_scenario_setup(const std::string& id, const std::string& name, const std::string& description,
260  int turns, int xp_mod, bool victory_defeated, bool random_time);
261 
263 
264  /**
265  * Getter for the labels reset flag. Set when the labels need to be refreshed.
266  */
267  bool needs_labels_reset() const { return needs_labels_reset_; }
268 
269  /**
270  * Setter for the labels reset flag
271  */
272  void set_needs_labels_reset(bool value=true) { needs_labels_reset_ = value; }
273 
274  const std::set<map_location> changed_locations() const { return changed_locations_; }
277  void add_changed_location(const std::set<map_location>& locs);
278  void set_everything_changed();
279  bool everything_changed() const;
280 
282 
284 
286 
287  const std::string& get_filename() const { return filename_; }
288 
289  void set_filename(const std::string& fn) { filename_ = fn; }
290 
291  const std::string& get_id() const { return scenario_id_; }
292  const std::string& get_description() const { return scenario_description_; }
293  const std::string& get_name() const { return scenario_name_; }
294 
295  const t_string get_default_context_name() const;
296 
297  utils::optional<int> get_xp_mod() const { return xp_mod_; }
298 
299  bool random_start_time() const { return random_time_; }
301 
302  bool is_embedded() const { return embedded_; }
303 
304  bool is_pure_map() const { return pure_map_; }
305 
306  void set_embedded(bool v) { embedded_ = v; }
307 
308  /**
309  * Saves the map under the current filename. Filename must be valid.
310  * May throw an exception on failure.
311  */
312  void save_map();
313 
314  /**
315  * Saves the scenario under the current filename. Filename must be valid.
316  * May throw an exception on failure.
317  */
318  void save_scenario();
319 
320  /**
321  * Save custom time of day schedule in the utils directory.
322  */
323  void save_schedule(const std::string& schedule_id, const std::string& schedule_name);
324 
325  /**
326  * Convert an old-style editor scenario config to a config with a top level [multiplayer] tag.
327  *
328  * @param old_scenario the original scenario config
329  * @return the converted scenario config
330  */
331  config convert_scenario(const config& old_scenario);
332  void load_scenario();
333 
334  config to_config();
335 
336  void set_map(const editor_map& map);
337 
338  /**
339  * Performs an action (thus modifying the map). An appropriate undo action is added to
340  * the undo stack. The redo stack is cleared. Note that this may throw, use caution
341  * when calling this with a dereferenced pointer that you own (i.e. use a smart pointer).
342  */
343  void perform_action(const editor_action& action);
344 
345  /**
346  * Performs a partial action, assumes that the top undo action has been modified to
347  * maintain coherent state of the undo stacks, and so a new undo action is not
348  * created.
349  */
350  void perform_partial_action(const editor_action& action);
351 
352  /** @return whether the map was modified since the last save */
353  bool modified() const;
354 
355  /** Clear the modified state */
356  void clear_modified();
357 
358  /** Adds the map to the editor's recent files list. */
359  void add_to_recent_files();
360 
361  /** @return true when undo can be performed, false otherwise */
362  bool can_undo() const;
363 
364  /** @return true when redo can be performed, false otherwise */
365  bool can_redo() const;
366 
367  /** @return a pointer to the last undo action or nullptr if the undo stack is empty */
369 
370  /** @return a pointer to the last redo action or nullptr if the undo stack is empty */
372 
373  /** const version of last_undo_action */
374  const editor_action* last_undo_action() const;
375 
376  /** const version of last_redo_action */
377  const editor_action* last_redo_action() const;
378 
379  /** Un-does the last action, and puts it in the redo stack for a possible redo */
380  void undo();
381 
382  /** Re-does a previously undid action, and puts it back in the undo stack. */
383  void redo();
384 
385  /**
386  * Un-does a single step from a undo action chain. The action is separated
387  * from the chain and it's undo (the redo) is added as a stand-alone action
388  * to the redo stack.
389  * Precondition: the last undo action has to actually be an action chain.
390  */
391  void partial_undo();
392 
393  /**
394  * Clear the undo and redo stacks
395  */
396  void clear_undo_redo();
397 
398  void set_addon_id(const std::string& addon_id)
399  {
400  addon_id_ = addon_id;
401  }
402 
403 protected:
404  /**
405  * The actual filename of this map. An empty string indicates a new map.
406  */
407  std::string filename_;
408 
409  /**
410  * When a scenario file is loaded, the referenced map is loaded instead.
411  * The verbatim form of the reference is kept here.
412  */
413  std::string map_data_key_;
414 
415  /**
416  * Whether the map context refers to a map embedded in a scenario file.
417  * This distinction is important in order to avoid overwriting the scenario.
418  */
419  bool embedded_;
420 
421  /**
422  * Whether the map context refers to a file containing only the pure map data.
423  */
424  bool pure_map_;
425 
426  /**
427  * The map object of this map_context.
428  */
430 
431  /**
432  * Checks if an action stack reached its capacity and removes the front element if so.
433  */
434  void trim_stack(action_stack& stack);
435 
436  /**
437  * Perform an action at the back of one stack, and then move it to the back of the other stack.
438  * This is the implementation of both undo and redo which only differ in the direction.
439  */
441 
442  /**
443  * The undo stack. A double-ended queue due to the need to add items to one end,
444  * and remove from both when performing the undo or when trimming the size. This container owns
445  * all contents, i.e. no action in the stack shall be deleted, and unless otherwise noted the contents
446  * could be deleted at an time during normal operation of the stack. To work on an action, either
447  * remove it from the container or make a copy. Actions are inserted at the back of the container
448  * and disappear from the front when the capacity is exceeded.
449  * @todo Use boost's pointer-owning container?
450  */
452 
453  /**
454  * The redo stack. @see undo_stack_
455  */
457 
458  /**
459  * Action stack (i.e. undo and redo) maximum size
460  */
461  static const std::size_t max_action_stack_size_;
462 
463  /**
464  * Number of actions performed since the map was saved. Zero means the map was not modified.
465  */
467 
468  /**
469  * Cache of set starting position labels. Necessary for removing them.
470  */
471  std::set<map_location> starting_position_label_locs_;
472 
473  /**
474  * Refresh flag indicating the map in this context should be completely reloaded by the display
475  */
477 
478  /**
479  * Refresh flag indicating the terrain in the map has changed and requires a rebuild
480  */
482 
483  /**
484  * Refresh flag indicating the labels in the map have changed
485  */
487 
488  std::set<map_location> changed_locations_;
490 
491 private:
492  std::string addon_id_;
493  utils::optional<config> previous_cfg_;
495 
496  utils::optional<int> xp_mod_;
497  utils::optional<bool> victory_defeated_;
499 
501 
504  std::vector<team> teams_;
505  std::vector<std::string> lbl_categories_;
506  std::unique_ptr<tod_manager> tod_manager_;
509 
510  std::list<std::shared_ptr<sound::music_track>> music_tracks_;
511 
512  typedef std::map<map_location, std::vector<overlay>> overlay_map;
514 
515 public:
516 
518 
519 };
520 
521 
522 } //end namespace editor
map_location loc
Definition: move.cpp:172
double t
Definition: astarsearch.cpp:63
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:158
Abstract class for exposing game data that doesn't depend on the GUI, however which for historical re...
virtual const gamemap & map() const =0
virtual const std::vector< team > & teams() const =0
virtual const unit_map & units() const =0
Sort-of-Singleton that many classes, both GUI and non-GUI, use to access the game data.
Definition: display.hpp:88
Base class for all editor actions.
Definition: action_base.hpp:42
This class adds extra editor-specific functionality to a normal gamemap.
Definition: editor_map.hpp:70
This class wraps around a map to provide a concise interface for the editor to work with.
Definition: map_context.hpp:63
game_classification game_classification_
overlay_map & get_overlays()
std::set< map_location > starting_position_label_locs_
Cache of set starting position labels.
std::string filename_
The actual filename of this map.
void save_area(const std::set< map_location > &area)
void new_area(const std::set< map_location > &area)
utils::optional< config > previous_cfg_
void perform_partial_action(const editor_action &action)
Performs a partial action, assumes that the top undo action has been modified to maintain coherent st...
bool playlist_contains(const std::shared_ptr< sound::music_track > &track) const
Checks whether the given track is part of current playlist.
void remove_side()
removes the last side from the scenario
mp_game_settings mp_settings_
void set_active_area(int index)
bool modified() const
bool pure_map_
Whether the map context refers to a file containing only the pure map data.
bool needs_terrain_rebuild_
Refresh flag indicating the terrain in the map has changed and requires a rebuild.
virtual const unit_map & units() const override
Const units accessor.
action_stack redo_stack_
The redo stack.
std::unique_ptr< tod_manager > tod_manager_
void set_local_starting_time(int time)
bool embedded_
Whether the map context refers to a map embedded in a scenario file.
int get_active_area() const
void set_needs_labels_reset(bool value=true)
Setter for the labels reset flag.
void set_needs_reload(bool value=true)
Setter for the reload flag.
std::string scenario_name_
bool needs_labels_reset_
Refresh flag indicating the labels in the map have changed.
void toggle_track(const std::shared_ptr< sound::music_track > &track)
Remove the given track from the current playlist if present, else appends it.
void clear_starting_position_labels(display &disp)
std::vector< std::string > lbl_categories_
editor_map map_
The map object of this map_context.
void new_side()
Adds a new side to the map.
const std::string & get_id() const
static const std::size_t max_action_stack_size_
Action stack (i.e.
action_stack undo_stack_
The undo stack.
editor_action * last_undo_action()
std::string map_data_key_
When a scenario file is loaded, the referenced map is loaded instead.
void redo()
Re-does a previously undid action, and puts it back in the undo stack.
std::set< map_location > changed_locations_
void set_starting_time(int time)
void draw_terrain(const t_translation::terrain_code &terrain, const map_location &loc, bool one_layer_only=false)
Draw a terrain on a single location on the map.
bool random_start_time() const
void set_map(const editor_map &map)
bool select_area(int index)
Select the nth tod area.
bool can_redo() const
void set_side_setup(editor_team_info &info)
std::vector< team > & teams()
Local non-const overload of teams.
void trim_stack(action_stack &stack)
Checks if an action stack reached its capacity and removes the front element if so.
bool can_undo() const
int actions_since_save_
Number of actions performed since the map was saved.
std::string scenario_description_
void save_scenario()
Saves the scenario under the current filename.
bool needs_reload_
Refresh flag indicating the map in this context should be completely reloaded by the display.
editor_map & map()
Local non-const overload of map.
std::vector< team > teams_
void perform_action_between_stacks(action_stack &from, action_stack &to)
Perform an action at the back of one stack, and then move it to the back of the other stack.
void perform_action(const editor_action &action)
Performs an action (thus modifying the map).
void undo()
Un-does the last action, and puts it in the redo stack for a possible redo.
const std::string & get_description() const
void replace_schedule(const std::vector< time_of_day > &schedule)
utils::optional< bool > victory_defeated_
void remove_area(int index)
void set_addon_id(const std::string &addon_id)
void clear_undo_redo()
Clear the undo and redo stacks.
bool is_embedded() const
virtual ~map_context()
Map context destructor.
unit_map & units()
Local non-const overload of units.
tod_manager * get_time_manager()
std::list< std::shared_ptr< sound::music_track > > music_tracks_
virtual const std::vector< std::string > & hidden_label_categories() const override
void draw_terrain_actual(const t_translation::terrain_code &terrain, const map_location &loc, bool one_layer_only=false)
Actual drawing function used by both overloaded variants of draw_terrain.
void clear_changed_locations()
map_context(const map_context &)=delete
bool needs_labels_reset() const
Getter for the labels reset flag.
utils::optional< int > xp_mod_
void add_to_recent_files()
Adds the map to the editor's recent files list.
std::map< map_location, std::vector< overlay > > overlay_map
std::string scenario_id_
void replace_local_schedule(const std::vector< time_of_day > &schedule)
Replace the [time]s of the currently active area.
const tod_manager * get_time_manager() const
void save_map()
Saves the map under the current filename.
void reset_starting_position_labels(display &disp)
void set_embedded(bool v)
editor_action * last_redo_action()
void partial_undo()
Un-does a single step from a undo action chain.
bool needs_reload() const
Getter for the reload flag.
virtual const editor_map & map() const override
Const map accessor.
virtual std::vector< std::string > & hidden_label_categories() override
void set_needs_terrain_rebuild(bool value=true)
Setter for the terrain rebuild flag.
map_context & operator=(const map_context &)=delete
config convert_scenario(const config &old_scenario)
Convert an old-style editor scenario config to a config with a top level [multiplayer] tag.
bool everything_changed() const
const t_string get_default_context_name() const
const std::string & get_filename() const
void set_starting_position_labels(display &disp)
mp_game_settings & get_mp_settings()
map_labels & get_labels()
bool needs_terrain_rebuild() const
Getter for the terrain rebuild flag.
void clear_modified()
Clear the modified state.
virtual const std::vector< team > & teams() const override
Const teams accessor.
overlay_map overlays_
game_classification & get_classification()
bool is_pure_map() const
bool victory_defeated() const
void set_filename(const std::string &fn)
const std::set< map_location > changed_locations() const
utils::optional< int > get_xp_mod() const
std::string addon_id_
void set_scenario_setup(const std::string &id, const std::string &name, const std::string &description, int turns, int xp_mod, bool victory_defeated, bool random_time)
void add_changed_location(const map_location &loc)
const std::string & get_name() const
void save_schedule(const std::string &schedule_id, const std::string &schedule_name)
Save custom time of day schedule in the utils directory.
A class grating read only view to a vector of config objects, viewed as one config with all children ...
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:74
Container associating units to locations.
Definition: map.hpp:98
Manage the empty-palette in the editor.
Definition: action.cpp:31
std::deque< std::unique_ptr< editor_action > > action_stack
Action stack typedef.
Game configuration data as global variables.
Definition: build_info.cpp:61
logger & info()
Definition: log.cpp:351
std::size_t index(std::string_view str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:70
std::string filename
Filename.
editor_team_info(const team &t)
Definition: map_context.cpp:40
side_controller::type controller
Definition: map_context.hpp:50
team_shared_vision::type share_vision
Definition: map_context.hpp:49
Encapsulates the map of the game.
Definition: location.hpp:46
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:49