The Battle for Wesnoth  1.13.4+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 - 2016 by Tomasz Sniatowski <[email protected]>
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 #ifndef EDITOR_MAP_CONTEXT_HPP_INCLUDED
16 #define EDITOR_MAP_CONTEXT_HPP_INCLUDED
17 
18 #include "editor_map.hpp"
19 #include "game_classification.hpp"
20 #include "map/label.hpp"
21 #include "mp_game_settings.hpp"
22 #include "sound_music_track.hpp"
23 #include "team.hpp"
24 #include "tod_manager.hpp"
25 #include "units/map.hpp"
26 #include "overlay.hpp"
27 #include "display_context.hpp"
28 
29 #include <boost/utility.hpp>
30 #include <boost/scoped_ptr.hpp>
31 
32 namespace editor {
33 
34 /**
35  * This class wraps around a map to provide a concise interface for the editor to work with.
36  * The actual map object can change rapidly (be assigned to), the map context persists
37  * data (like the undo stacks) in this case. The functionality is here, not in editor_controller
38  * as e.g. the undo stack is part of the map, not the editor as a whole. This might allow many
39  * maps to be open at the same time.
40  */
41 class map_context : public display_context, private boost::noncopyable
42 {
43 public:
44  /**
45  * Create a map context from an existing map. The filename is set to be
46  * empty, indicating a new map.
47  * Marked "explicit" to avoid automatic conversions.
48  */
49  explicit map_context(const editor_map& map, const display& disp, bool pure_map, const config& schedule);
50 
51  /**
52  * Create map_context from a map file. If the map cannot be loaded, an
53  * exception will be thrown and the object will not be constructed. If the
54  * map file is a scenario, the map specified in its map_data key will be
55  * loaded, and the stored filename updated accordingly. Maps embedded
56  * inside scenarios do not change the filename, but set the "embedded" flag
57  * instead.
58  */
59  map_context(const config& game_config, const std::string& filename, const display& disp);
60 
61  /**
62  * Map context destructor
63  */
64  virtual ~map_context();
65 
66  /**
67  * Select the nth tod area.
68  * @param index of the tod area to select.
69  */
70  bool select_area(int index);
71 
72  /**
73  * Map accessor
74  */
75  editor_map& get_map() { return map_; }
76 
77  /**
78  * Map accessor - const version
79  */
80  const editor_map& get_map() const { return map_; }
81 
82  /** Adds a new side to the map */
83  void new_side() {
84  team t;
85  t.set_hidden(false);
86  teams_.push_back(t);
88  }
89 
90  /** removes the last side from the scenario */
91  void remove_side() {
92  teams_.pop_back();
94  }
95 
96  void save_area(const std::set<map_location>& area) {
97  tod_manager_->replace_area_locations(active_area_, area);
98  }
99 
100  void new_area(const std::set<map_location>& area) {
101  tod_manager_->add_time_area("", area, config());
102  active_area_ = tod_manager_->get_area_ids().size() -1;
104  }
105 
106  void remove_area(int index);
107 
108  /** Get the team from the current map context object */
109  std::vector<team>& get_teams() {
110  return teams_;
111  }
112 
114  return labels_;
115  }
116 
117  /** Get the unit map from the current map context object */
119  return units_;
120  }
121 
122  const unit_map& get_units() const {
123  return units_;
124  }
125 
126  void replace_schedule(const std::vector<time_of_day>& schedule);
127 
128  /**
129  * Const accessor names needed to implement "display_context" interface
130  */
131  virtual const unit_map & units() const {
132  return units_;
133  }
134  virtual const std::vector<team>& teams() const {
135  return teams_;
136  }
137  virtual const gamemap & map() const {
138  return map_;
139  }
140  virtual const std::vector<std::string>& hidden_label_categories() const {
141  return lbl_categories_;
142  }
143 
144  /**
145  * Replace the [time]s of the currently active area.
146  */
147  void replace_local_schedule(const std::vector<time_of_day>& schedule);
148 
149  /**
150  * TODO
151  */
152  void set_starting_time(int time);
153 
154  /**
155  * TODO
156  */
157  void set_local_starting_time(int time) {
158  tod_manager_->set_current_time(time, active_area_);
160  }
161 
163  return tod_manager_.get();
164  }
165 
167  return mp_settings_;
168  }
170  return game_classification_;
171  }
172 
173  /**
174  *
175  * @return the index of the currently active area.
176  */
177  int get_active_area() const {
178  return active_area_;
179  }
180 
181  void set_active_area(int index) {
183  }
184 
185  bool is_in_playlist(std::string track_id) {
186  return music_tracks_.find(track_id) != music_tracks_.end();
187  }
188 
189  void add_to_playlist(const sound::music_track& track) {
190 
191  if (music_tracks_.find(track.id()) == music_tracks_.end())
192  music_tracks_.insert(std::pair<std::string, sound::music_track>(track.id(), track));
193  else music_tracks_.erase(track.id());
194  }
195 
196  /**
197  * Draw a terrain on a single location on the map.
198  * Sets the refresh flags accordingly.
199  */
201  bool one_layer_only = false);
202 
203  /**
204  * Actual drawing function used by both overloaded variants of draw_terrain.
205  */
207  bool one_layer_only = false);
208 
209  /**
210  * Draw a terrain on a set of locations on the map.
211  * Sets the refresh flags accordingly.
212  */
213  void draw_terrain(const t_translation::t_terrain & terrain, const std::set<map_location>& locs,
214  bool one_layer_only = false);
215 
216  /**
217  * Getter for the reload flag. Reload is the highest level of required refreshing,
218  * set when the map size has changed or the map was reassigned.
219  */
220  bool needs_reload() const { return needs_reload_; }
221 
222  /**
223  * Setter for the reload flag
224  */
225  void set_needs_reload(bool value=true) { needs_reload_ = value; }
226 
227  /**
228  * Getter for the terrain rebuild flag. Set whenever any terrain has changed.
229  */
231 
232  /**
233  * Setter for the terrain rebuild flag
234  */
236 
237  /**
238  * TODO
239  */
240  void set_scenario_setup(const std::string& id, const std::string& name, const std::string& description,
241  int turns, int xp_mod, bool victory_defeated, bool random_time);
242 
243  /**
244  * TODO
245  */
246  void set_side_setup(int side, const std::string& id, const std::string& name,
247  int gold, int income, int village_gold, int village_support,
248  bool fog, bool shroud, team::SHARE_VISION share_vision,
249  team::CONTROLLER controller, bool hidden, bool no_leader);
250 
251  /**
252  * Getter for the labels reset flag. Set when the labels need to be refreshed.
253  */
254  bool needs_labels_reset() const { return needs_labels_reset_; }
255 
256  /**
257  * Setter for the labels reset flag
258  */
260 
261  const std::set<map_location> changed_locations() const { return changed_locations_; }
263  void add_changed_location(const map_location& loc);
264  void add_changed_location(const std::set<map_location>& locs);
265  void set_everything_changed();
266  bool everything_changed() const;
267 
268  void set_labels(display& disp);
269 
271 
273 
275 
276  const std::string& get_filename() const { return filename_; }
277 
278  void set_filename(const std::string& fn) { filename_ = fn; }
279 
280  const std::string& get_map_data_key() const { return map_data_key_; }
281 
282  const std::string& get_id() const { return scenario_id_; }
284  const std::string& get_name() const { return scenario_name_; }
285 
286  int get_xp_mod() const { return xp_mod_; }
287 
288  bool random_start_time() const { return random_time_; }
289  bool victory_defeated() const { return victory_defeated_; }
290 
291  bool is_embedded() const { return embedded_; }
292 
293  bool is_pure_map() const { return pure_map_; }
294 
295  void set_embedded(bool v) { embedded_ = v; }
296 
297  /**
298  * Saves the map under the current filename. Filename must be valid.
299  * May throw an exception on failure.
300  */
301  bool save_map();
302 
303  /**
304  * Saves the scenario under the current filename. Filename must be valid.
305  * May throw an exception on failure.
306  */
307  bool save_scenario();
308 
309 
310  void load_scenario(const config& game_config);
311 
312  config to_config();
313 
314  void set_map(const editor_map& map);
315 
316  /**
317  * Performs an action (thus modifying the map). An appropriate undo action is added to
318  * the undo stack. The redo stack is cleared. Note that this may throw, use caution
319  * when calling this with a dereferenced pointer that you own (i.e. use a smart pointer).
320  */
321  void perform_action(const editor_action& action);
322 
323  /**
324  * Performs a partial action, assumes that the top undo action has been modified to
325  * maintain coherent state of the undo stacks, and so a new undo action is not
326  * created.
327  */
328  void perform_partial_action(const editor_action& action);
329 
330  /** @return whether the map was modified since the last save */
331  bool modified() const;
332 
333  /** Clear the modified state */
334  void clear_modified();
335 
336  /** Adds the map to the editor's recent files list. */
337  void add_to_recent_files();
338 
339  /** @return true when undo can be performed, false otherwise */
340  bool can_undo() const;
341 
342  /** @return true when redo can be performed, false otherwise */
343  bool can_redo() const;
344 
345  /** @return a pointer to the last undo action or nullptr if the undo stack is empty */
347 
348  /** @return a pointer to the last redo action or nullptr if the undo stack is empty */
350 
351  /** const version of last_undo_action */
352  const editor_action* last_undo_action() const;
353 
354  /** const version of last_redo_action */
355  const editor_action* last_redo_action() const;
356 
357  /** Un-does the last action, and puts it in the redo stack for a possible redo */
358  void undo();
359 
360  /** Re-does a previously undid action, and puts it back in the undo stack. */
361  void redo();
362 
363  /**
364  * Un-does a single step from a undo action chain. The action is separated
365  * from the chain and it's undo (the redo) is added as a stand-alone action
366  * to the redo stack.
367  * Precondition: the last undo action has to actually be an action chain.
368  */
369  void partial_undo();
370 
371  /**
372  * Clear the undo and redo stacks
373  */
374  void clear_undo_redo();
375 
376 protected:
377  /**
378  * The actual filename of this map. An empty string indicates a new map.
379  */
381 
382  /**
383  * When a scenario file is loaded, the referenced map is loaded instead.
384  * The verbatim form of the reference is kept here.
385  */
387 
388  /**
389  * Whether the map context refers to a map embedded in a scenario file.
390  * This distinction is important in order to avoid overwriting the scenario.
391  */
392  bool embedded_;
393 
394  /**
395  * Whether the map context refers to a file containing only the pure map data.
396  */
397  bool pure_map_;
398 
399  /**
400  * The map object of this map_context.
401  */
403 
404 
405  /**
406  * Container type used to store actions in the undo and redo stacks
407  */
408  typedef std::deque<editor_action*> action_stack;
409 
410  /**
411  * Checks if an action stack reached its capacity and removes the front element if so.
412  */
413  void trim_stack(action_stack& stack);
414 
415  /**
416  * Clears an action stack and deletes all its contents. Helper function used when the undo
417  * or redo stack needs to be cleared
418  */
419  void clear_stack(action_stack& stack);
420 
421  /**
422  * Perform an action at the back of one stack, and then move it to the back of the other stack.
423  * This is the implementation of both undo and redo which only differ in the direction.
424  */
425  void perform_action_between_stacks(action_stack& from, action_stack& to);
426 
427  /**
428  * The undo stack. A double-ended queues due to the need to add items to one end,
429  * and remove from both when performing the undo or when trimming the size. This container owns
430  * all contents, i.e. no action in the stack shall be deleted, and unless otherwise noted the contents
431  * could be deleted at an time during normal operation of the stack. To work on an action, either
432  * remove it from the container or make a copy. Actions are inserted at the back of the container
433  * and disappear from the front when the capacity is exceeded.
434  * @todo Use boost's pointer-owning container?
435  */
436  action_stack undo_stack_;
437 
438  /**
439  * The redo stack. @see undo_stack_
440  */
441  action_stack redo_stack_;
442 
443  /**
444  * Action stack (i.e. undo and redo) maximum size
445  */
446  static const size_t max_action_stack_size_;
447 
448  /**
449  * Number of actions performed since the map was saved. Zero means the map was not modified.
450  */
452 
453  /**
454  * Cache of set starting position labels. Necessary for removing them.
455  */
456  std::set<map_location> starting_position_label_locs_;
457 
458  /**
459  * Refresh flag indicating the map in this context should be completely reloaded by the display
460  */
462 
463  /**
464  * Refresh flag indicating the terrain in the map has changed and requires a rebuild
465  */
467 
468  /**
469  * Refresh flag indicating the labels in the map have changed
470  */
472 
473  std::set<map_location> changed_locations_;
475 
476 private:
477 
479 
480  int xp_mod_;
482 
484 
487  std::vector<team> teams_;
488  std::vector<std::string> lbl_categories_;
489  boost::scoped_ptr<tod_manager> tod_manager_;
492 
493  typedef std::map<std::string, sound::music_track> music_map;
494  music_map music_tracks_;
495 
496  typedef std::multimap<map_location, overlay> overlay_map;
497  overlay_map overlays_;
498 
499 public:
500 
501  overlay_map& get_overlays() { return overlays_; }
502 
503 };
504 
505 
506 } //end namespace editor
507 
508 #endif
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)
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::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)
bool needs_terrain_rebuild_
Refresh flag indicating the terrain in the map has changed and requires a rebuild.
int village_support
Definition: game_config.cpp:39
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 save_area(const std::set< map_location > &area)
Definition: map_context.hpp:96
void new_side()
Adds a new side to the map.
Definition: map_context.hpp:83
bool save_scenario()
Saves the scenario under the current filename.
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...
virtual const std::vector< std::string > & hidden_label_categories() const
bool everything_changed() const
map_context(const editor_map &map, const display &disp, bool pure_map, const config &schedule)
Create a map context from an existing map.
Definition: map_context.cpp:44
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
void draw_terrain(const t_translation::t_terrain &terrain, const map_location &loc, bool one_layer_only=false)
Draw a terrain on a single location on the map.
void clear_stack(action_stack &stack)
Clears an action stack and deletes all its contents.
void set_hidden(bool value)
Definition: team.hpp:345
GLdouble GLdouble t
Definition: glew.h:1366
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
void set_side_setup(int side, const std::string &id, const std::string &name, int gold, int income, int village_gold, int village_support, bool fog, bool shroud, team::SHARE_VISION share_vision, team::CONTROLLER controller, bool hidden, bool no_leader)
TODO.
overlay_map & get_overlays()
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:50
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:80
void set_local_starting_time(int time)
TODO.
bool is_pure_map() const
std::vector< team > teams_
std::deque< editor_action * > action_stack
Container type used to store actions in the undo and redo stacks.
void remove_side()
removes the last side from the scenario
Definition: map_context.hpp:91
void reset_starting_position_labels(display &disp)
const GLdouble * v
Definition: glew.h:1359
GLsizei const GLfloat * value
Definition: glew.h:1817
void draw_terrain_actual(const t_translation::t_terrain &terrain, const map_location &loc, bool one_layer_only=false)
Actual drawing function used by both overloaded variants of draw_terrain.
void set_embedded(bool v)
editor_map & get_map()
Map accessor.
Definition: map_context.hpp:75
Encapsulates the map of the game.
Definition: map.hpp:37
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:28
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:135
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:47
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:38
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:70
Game configuration data as global variables.
Definition: build_info.cpp:38
unit_map & get_units()
Get the unit map from the current map context object.
GLuint index
Definition: glew.h:1782
Base class for all editor actions.
Definition: action_base.hpp:41
Internal representation of music tracks.
const std::string & get_map_data_key() const
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:41
action_stack redo_stack_
The redo stack.
std::vector< std::string > lbl_categories_
GLuint const GLchar * name
Definition: glew.h:1782
boost::scoped_ptr< tod_manager > tod_manager_
bool needs_reload() const
Getter for the reload flag.
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:90
int get_active_area() const
std::string filename_
The actual filename of this map.
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:83
GLsizei const GLcharARB ** string
Definition: glew.h:4503
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