The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
map_context.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2017 by Tomasz Sniatowski <kailoran@gmail.com>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
18 #include "game_classification.hpp"
19 #include "map/label.hpp"
20 #include "mp_game_settings.hpp"
21 #include "sound_music_track.hpp"
22 #include "team.hpp"
23 #include "tod_manager.hpp"
24 #include "units/map.hpp"
25 #include "overlay.hpp"
26 #include "display_context.hpp"
27 
28 namespace editor {
29 
31  editor_team_info(const team& t);
32 
33  int side;
36  int gold;
37  int income;
40  bool fog;
41  bool shroud;
42  team::SHARE_VISION share_vision;
43  team::CONTROLLER controller;
44  bool no_leader;
45  bool hidden;
46 };
47 
48 /**
49  * This class wraps around a map to provide a concise interface for the editor to work with.
50  * The actual map object can change rapidly (be assigned to), the map context persists
51  * data (like the undo stacks) in this case. The functionality is here, not in editor_controller
52  * as e.g. the undo stack is part of the map, not the editor as a whole. This might allow many
53  * maps to be open at the same time.
54  */
56 {
57 public:
58  map_context(const map_context&) = delete;
59  map_context& operator=(const map_context&) = delete;
60 
61  /**
62  * Create a map context from an existing map. The filename is set to be
63  * empty, indicating a new map.
64  * Marked "explicit" to avoid automatic conversions.
65  */
66  explicit map_context(const editor_map& map, bool pure_map, const config& schedule);
67 
68  /**
69  * Create map_context from a map file. If the map cannot be loaded, an
70  * exception will be thrown and the object will not be constructed. If the
71  * map file is a scenario, the map specified in its map_data key will be
72  * loaded, and the stored filename updated accordingly. Maps embedded
73  * inside scenarios do not change the filename, but set the "embedded" flag
74  * instead.
75  */
76  map_context(const config& game_config, const std::string& filename);
77 
78  /**
79  * Map context destructor
80  */
81  virtual ~map_context();
82 
83  /**
84  * Select the nth tod area.
85  * @param index of the tod area to select.
86  */
87  bool select_area(int index);
88 
89  /**
90  * Map accessor
91  */
92  editor_map& get_map() { return map_; }
93 
94  /**
95  * Map accessor - const version
96  */
97  const editor_map& get_map() const { return map_; }
98 
99  /** Adds a new side to the map */
100  void new_side();
101 
102  /** removes the last side from the scenario */
103  void remove_side() {
104  teams_.pop_back();
106  }
107 
108  void save_area(const std::set<map_location>& area) {
109  tod_manager_->replace_area_locations(active_area_, area);
110  }
111 
112  void new_area(const std::set<map_location>& area) {
113  tod_manager_->add_time_area("", area, config());
114  active_area_ = tod_manager_->get_area_ids().size() -1;
116  }
117 
118  void remove_area(int index);
119 
120  /** Get the team from the current map context object */
121  std::vector<team>& get_teams() {
122  return teams_;
123  }
124 
126  return labels_;
127  }
128 
129  /** Get the unit map from the current map context object */
131  return units_;
132  }
133 
134  const unit_map& get_units() const {
135  return units_;
136  }
137 
138  void replace_schedule(const std::vector<time_of_day>& schedule);
139 
140  /**
141  * Const accessor names needed to implement "display_context" interface
142  */
143  virtual const unit_map & units() const {
144  return units_;
145  }
146  virtual const std::vector<team>& teams() const {
147  return teams_;
148  }
149  virtual const gamemap & map() const {
150  return map_;
151  }
152  virtual const std::vector<std::string>& hidden_label_categories() const {
153  return lbl_categories_;
154  }
155 
156  /**
157  * Replace the [time]s of the currently active area.
158  */
159  void replace_local_schedule(const std::vector<time_of_day>& schedule);
160 
161  /**
162  * TODO
163  */
164  void set_starting_time(int time);
165 
166  /**
167  * TODO
168  */
169  void set_local_starting_time(int time) {
170  tod_manager_->set_current_time(time, active_area_);
172  }
173 
175  return tod_manager_.get();
176  }
177 
179  return mp_settings_;
180  }
182  return game_classification_;
183  }
184 
185  /**
186  *
187  * @return the index of the currently active area.
188  */
189  int get_active_area() const {
190  return active_area_;
191  }
192 
193  void set_active_area(int index) {
195  }
196 
197  bool is_in_playlist(std::string track_id) {
198  return music_tracks_.find(track_id) != music_tracks_.end();
199  }
200 
201  void add_to_playlist(const sound::music_track& track) {
202 
203  if (music_tracks_.find(track.id()) == music_tracks_.end())
204  music_tracks_.emplace(track.id(), track);
205  else music_tracks_.erase(track.id());
206  }
207 
208  /**
209  * Draw a terrain on a single location on the map.
210  * Sets the refresh flags accordingly.
211  */
213  bool one_layer_only = false);
214 
215  /**
216  * Actual drawing function used by both overloaded variants of draw_terrain.
217  */
219  bool one_layer_only = false);
220 
221  /**
222  * Draw a terrain on a set of locations on the map.
223  * Sets the refresh flags accordingly.
224  */
225  void draw_terrain(const t_translation::terrain_code & terrain, const std::set<map_location>& locs,
226  bool one_layer_only = false);
227 
228  /**
229  * Getter for the reload flag. Reload is the highest level of required refreshing,
230  * set when the map size has changed or the map was reassigned.
231  */
232  bool needs_reload() const { return needs_reload_; }
233 
234  /**
235  * Setter for the reload flag
236  */
237  void set_needs_reload(bool value=true) { needs_reload_ = value; }
238 
239  /**
240  * Getter for the terrain rebuild flag. Set whenever any terrain has changed.
241  */
243 
244  /**
245  * Setter for the terrain rebuild flag
246  */
247  void set_needs_terrain_rebuild(bool value=true) { needs_terrain_rebuild_ = value; }
248 
249  /**
250  * TODO
251  */
252  void set_scenario_setup(const std::string& id, const std::string& name, const std::string& description,
253  int turns, int xp_mod, bool victory_defeated, bool random_time);
254 
255  /**
256  * TODO
257  */
259 
260  /**
261  * Getter for the labels reset flag. Set when the labels need to be refreshed.
262  */
263  bool needs_labels_reset() const { return needs_labels_reset_; }
264 
265  /**
266  * Setter for the labels reset flag
267  */
268  void set_needs_labels_reset(bool value=true) { needs_labels_reset_ = value; }
269 
270  const std::set<map_location> changed_locations() const { return changed_locations_; }
272  void add_changed_location(const map_location& loc);
273  void add_changed_location(const std::set<map_location>& locs);
274  void set_everything_changed();
275  bool everything_changed() const;
276 
277  void set_labels(display& disp);
278 
280 
282 
284 
285  const std::string& get_filename() const { return filename_; }
286 
287  void set_filename(const std::string& fn) { filename_ = fn; }
288 
289  const std::string& get_map_data_key() const { return map_data_key_; }
290 
291  const std::string& get_id() const { return scenario_id_; }
293  const std::string& get_name() const { return scenario_name_; }
294 
295  const t_string get_default_context_name() const;
296 
297  int get_xp_mod() const { return xp_mod_; }
298 
299  bool random_start_time() const { return random_time_; }
300  bool victory_defeated() const { return victory_defeated_; }
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  bool 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  bool save_scenario();
319 
320 
321  void load_scenario(const config& game_config);
322 
323  config to_config();
324 
325  void set_map(const editor_map& map);
326 
327  /**
328  * Performs an action (thus modifying the map). An appropriate undo action is added to
329  * the undo stack. The redo stack is cleared. Note that this may throw, use caution
330  * when calling this with a dereferenced pointer that you own (i.e. use a smart pointer).
331  */
332  void perform_action(const editor_action& action);
333 
334  /**
335  * Performs a partial action, assumes that the top undo action has been modified to
336  * maintain coherent state of the undo stacks, and so a new undo action is not
337  * created.
338  */
339  void perform_partial_action(const editor_action& action);
340 
341  /** @return whether the map was modified since the last save */
342  bool modified() const;
343 
344  /** Clear the modified state */
345  void clear_modified();
346 
347  /** Adds the map to the editor's recent files list. */
348  void add_to_recent_files();
349 
350  /** @return true when undo can be performed, false otherwise */
351  bool can_undo() const;
352 
353  /** @return true when redo can be performed, false otherwise */
354  bool can_redo() const;
355 
356  /** @return a pointer to the last undo action or nullptr if the undo stack is empty */
358 
359  /** @return a pointer to the last redo action or nullptr if the undo stack is empty */
361 
362  /** const version of last_undo_action */
363  const editor_action* last_undo_action() const;
364 
365  /** const version of last_redo_action */
366  const editor_action* last_redo_action() const;
367 
368  /** Un-does the last action, and puts it in the redo stack for a possible redo */
369  void undo();
370 
371  /** Re-does a previously undid action, and puts it back in the undo stack. */
372  void redo();
373 
374  /**
375  * Un-does a single step from a undo action chain. The action is separated
376  * from the chain and it's undo (the redo) is added as a stand-alone action
377  * to the redo stack.
378  * Precondition: the last undo action has to actually be an action chain.
379  */
380  void partial_undo();
381 
382  /**
383  * Clear the undo and redo stacks
384  */
385  void clear_undo_redo();
386 
387 protected:
388  /**
389  * The actual filename of this map. An empty string indicates a new map.
390  */
392 
393  /**
394  * When a scenario file is loaded, the referenced map is loaded instead.
395  * The verbatim form of the reference is kept here.
396  */
398 
399  /**
400  * Whether the map context refers to a map embedded in a scenario file.
401  * This distinction is important in order to avoid overwriting the scenario.
402  */
403  bool embedded_;
404 
405  /**
406  * Whether the map context refers to a file containing only the pure map data.
407  */
408  bool pure_map_;
409 
410  /**
411  * The map object of this map_context.
412  */
414 
415  /**
416  * Checks if an action stack reached its capacity and removes the front element if so.
417  */
418  void trim_stack(action_stack& stack);
419 
420  /**
421  * Perform an action at the back of one stack, and then move it to the back of the other stack.
422  * This is the implementation of both undo and redo which only differ in the direction.
423  */
425 
426  /**
427  * The undo stack. A double-ended queues due to the need to add items to one end,
428  * and remove from both when performing the undo or when trimming the size. This container owns
429  * all contents, i.e. no action in the stack shall be deleted, and unless otherwise noted the contents
430  * could be deleted at an time during normal operation of the stack. To work on an action, either
431  * remove it from the container or make a copy. Actions are inserted at the back of the container
432  * and disappear from the front when the capacity is exceeded.
433  * @todo Use boost's pointer-owning container?
434  */
436 
437  /**
438  * The redo stack. @see undo_stack_
439  */
441 
442  /**
443  * Action stack (i.e. undo and redo) maximum size
444  */
445  static const size_t max_action_stack_size_;
446 
447  /**
448  * Number of actions performed since the map was saved. Zero means the map was not modified.
449  */
451 
452  /**
453  * Cache of set starting position labels. Necessary for removing them.
454  */
455  std::set<map_location> starting_position_label_locs_;
456 
457  /**
458  * Refresh flag indicating the map in this context should be completely reloaded by the display
459  */
461 
462  /**
463  * Refresh flag indicating the terrain in the map has changed and requires a rebuild
464  */
466 
467  /**
468  * Refresh flag indicating the labels in the map have changed
469  */
471 
472  std::set<map_location> changed_locations_;
474 
475 private:
476 
478 
479  int xp_mod_;
481 
483 
486  std::vector<team> teams_;
487  std::vector<std::string> lbl_categories_;
488  std::unique_ptr<tod_manager> tod_manager_;
491 
492  typedef std::map<std::string, sound::music_track> music_map;
493  music_map music_tracks_;
494 
495  typedef std::multimap<map_location, overlay> overlay_map;
496  overlay_map overlays_;
497 
498 public:
499 
500  overlay_map& get_overlays() { return overlays_; }
501 
502 };
503 
504 
505 } //end namespace editor
std::multimap< map_location, overlay > overlay_map
bool needs_terrain_rebuild() const
Getter for the terrain rebuild flag.
void add_changed_location(const map_location &loc)
void set_side_setup(editor_team_info &info)
TODO.
int actions_since_save_
Number of actions performed since the map was saved.
action_stack undo_stack_
The undo stack.
bool victory_defeated() const
std::string map_data_key_
When a scenario file is loaded, the referenced map is loaded instead.
editor_action * last_redo_action()
void undo()
Un-does the last action, and puts it in the redo stack for a possible redo.
virtual const gamemap & map() const
editor_map map_
The map object of this map_context.
game_classification game_classification_
std::vector< char_t > string
std::set< map_location > starting_position_label_locs_
Cache of set starting position labels.
bool needs_labels_reset() const
Getter for the labels reset flag.
void add_to_playlist(const sound::music_track &track)
size_t index(const utf8::string &str, const size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
bool needs_terrain_rebuild_
Refresh flag indicating the terrain in the map has changed and requires a rebuild.
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...
team::SHARE_VISION share_vision
Definition: map_context.hpp:42
void save_area(const std::set< map_location > &area)
void new_side()
Adds a new side to the map.
bool save_scenario()
Saves the scenario under the current filename.
logger & info()
Definition: log.cpp:91
virtual ~map_context()
Map context destructor.
const unit_map & get_units() const
void new_area(const std::set< map_location > &area)
void set_active_area(int index)
map_labels & get_labels()
bool modified() const
bool needs_reload_
Refresh flag indicating the map in this context should be completely reloaded by the display...
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.
virtual const std::vector< std::string > & hidden_label_categories() const
map_context & operator=(const map_context &)=delete
bool everything_changed() const
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:55
void redo()
Re-does a previously undid action, and puts it back in the undo stack.
mp_game_settings & get_mp_settings()
const std::string & get_filename() const
-file sdl_utils.hpp
std::deque< editor_action_ptr > action_stack
Action stack typedef.
std::string scenario_description_
std::string scenario_name_
void set_needs_reload(bool value=true)
Setter for the reload flag.
const std::string & get_description() const
int get_xp_mod() const
void clear_modified()
Clear the modified state.
void partial_undo()
Un-does a single step from a undo action chain.
bool embedded_
Whether the map context refers to a map embedded in a scenario file.
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)
TODO.
const std::set< map_location > changed_locations() const
void remove_area(int index)
void set_map(const editor_map &map)
virtual const std::vector< team > & teams() const
overlay_map & get_overlays()
const t_string get_default_context_name() const
game_classification & get_classification()
bool pure_map_
Whether the map context refers to a file containing only the pure map data.
static const size_t max_action_stack_size_
Action stack (i.e.
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:44
const std::string & get_name() const
tod_manager * get_time_manager()
std::map< std::string, sound::music_track > music_map
const editor_map & get_map() const
Map accessor - const version.
Definition: map_context.hpp:97
void set_local_starting_time(int time)
TODO.
bool is_pure_map() const
std::unique_ptr< tod_manager > tod_manager_
std::vector< team > teams_
editor_team_info(const team &t)
Definition: map_context.cpp:38
void remove_side()
removes the last side from the scenario
void reset_starting_position_labels(display &disp)
void set_embedded(bool v)
editor_map & get_map()
Map accessor.
Definition: map_context.hpp:92
Encapsulates the map of the game.
Definition: map.hpp:34
void replace_schedule(const std::vector< time_of_day > &schedule)
void add_to_recent_files()
Adds the map to the editor's recent files list.
void set_starting_time(int time)
TODO.
bool save_map()
Saves the map under the current filename.
editor_action * last_undo_action()
Manage the empty-palette in the editor.
Definition: action.cpp:29
void perform_action(const editor_action &action)
Performs an action (thus modifying the map).
bool can_undo() const
static const ::config * terrain
The terrain used to create the cache.
Definition: minimap.cpp:133
void set_needs_terrain_rebuild(bool value=true)
Setter for the terrain rebuild flag.
virtual const unit_map & units() const
Const accessor names needed to implement "display_context" interface.
bool needs_labels_reset_
Refresh flag indicating the labels in the map have changed.
std::string scenario_id_
Encapsulates the map of the game.
Definition: location.hpp:40
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...
void clear_changed_locations()
void set_starting_position_labels(display &disp)
void set_labels(display &disp)
std::vector< team > & get_teams()
Get the team from the current map context object.
bool can_redo() const
This class adds extra editor-specific functionality to a normal gamemap.
Definition: editor_map.hpp:69
Game configuration data as global variables.
Definition: build_info.cpp:53
map_context(const map_context &)=delete
unit_map & get_units()
Get the unit map from the current map context object.
Base class for all editor actions.
Definition: action_base.hpp:40
Internal representation of music tracks.
const std::string & get_map_data_key() const
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 load_scenario(const config &game_config)
overlay_map overlays_
bool random_start_time() const
This class wraps around a map to provide a concise interface for the editor to work with...
Definition: map_context.hpp:55
action_stack redo_stack_
The redo stack.
std::vector< std::string > lbl_categories_
int turns()
Definition: game.cpp:560
double t
Definition: astarsearch.cpp:64
bool needs_reload() const
Getter for the reload flag.
team::CONTROLLER controller
Definition: map_context.hpp:43
void clear_undo_redo()
Clear the undo and redo stacks.
void set_needs_labels_reset(bool value=true)
Setter for the labels reset flag.
bool is_in_playlist(std::string track_id)
void set_filename(const std::string &fn)
void replace_local_schedule(const std::vector< time_of_day > &schedule)
Replace the [time]s of the currently active area.
Container associating units to locations.
Definition: map.hpp:99
int get_active_area() const
std::string filename_
The actual filename of this map.
static const char * name(const std::vector< SDL_Joystick * > &joysticks, const size_t index)
Definition: joystick.cpp:48
void trim_stack(action_stack &stack)
Checks if an action stack reached its capacity and removes the front element if so.
std::set< map_location > changed_locations_
void clear_starting_position_labels(display &disp)
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
bool is_embedded() const
mp_game_settings mp_settings_
bool select_area(int index)
Select the nth tod area.
const std::string & id() const
const std::string & get_id() const