The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
undo.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[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 /**
16  * @file
17  * Various functions that implement the undoing (and redoing) of in-game commands.
18  */
19 
20 #ifndef ACTIONS_UNDO_H_INCLUDED
21 #define ACTIONS_UNDO_H_INCLUDED
22 
23 #include "vision.hpp"
24 #include "map/location.hpp"
25 #include "units/ptr.hpp"
26 #include "undo_action.hpp"
27 
28 #include <boost/noncopyable.hpp>
29 #include <boost/ptr_container/ptr_vector.hpp>
30 #include <boost/optional.hpp>
31 #include <vector>
32 
33 
34 namespace actions {
35 
36 
37 /// Class to store the actions that a player can undo and redo.
38 class undo_list : boost::noncopyable {
39 
40  typedef boost::ptr_vector<undo_action_base> action_list;
41  typedef boost::ptr_vector<undo_action> redos_list;
42 
43 public:
44  explicit undo_list(const config & cfg);
45  ~undo_list();
46  /// Creates an undo_action based on a config.
47  /// Throws bad_lexical_cast or config::error if it cannot parse the config properly.
48  static undo_action_base * create_action(const config & cfg);
49 
50  // Functions related to managing the undo stack:
51 
52  /// Adds an auto-shroud toggle to the undo stack.
53  void add_auto_shroud(bool turned_on);
54  /// Adds an auto-shroud toggle to the undo stack.
55  void add_dummy();
56  /// Adds a dismissal to the undo stack.
57  void add_dismissal(const unit_const_ptr u);
58  /// Adds a move to the undo stack.
59  void add_move(const unit_const_ptr u,
60  const std::vector<map_location>::const_iterator & begin,
61  const std::vector<map_location>::const_iterator & end,
62  int start_moves, int timebonus=0, int village_owner=-1,
64  /// Adds a recall to the undo stack.
65  void add_recall(const unit_const_ptr u, const map_location& loc,
66  const map_location& from, int orig_village_owner, bool time_bonus);
67  /// Adds a recruit to the undo stack.
68  void add_recruit(const unit_const_ptr u, const map_location& loc,
69  const map_location& from, int orig_village_owner, bool time_bonus);
70  /// Adds a shroud update to the undo stack.
71  void add_update_shroud();
72 private:
73 public:
74  /// Clears the stack of undoable (and redoable) actions.
75  void clear();
76  /// Updates fog/shroud based on the undo stack, then updates stack as needed.
77  void commit_vision();
78  /// Performs some initializations and error checks when starting a new
79  /// side-turn.
80  void new_side_turn(int side);
81  /// Returns true if the player has performed any actions this turn.
82  bool player_acted() const { return committed_actions_ || !undos_.empty(); }
83  /// Read the undo_list from the provided config.
84  void read(const config & cfg);
85  /// Write the undo_list into the provided config.
86  void write(config & cfg) const;
87 
88  // Functions related to using the undo stack:
89 
90  /// True if there are actions that can be undone.
91  bool can_undo() const { return !undos_.empty(); }
92  /// True if there are actions that can be redone.
93  bool can_redo() const { return !redos_.empty(); }
94  /// Undoes the top action on the undo stack.
95  void undo();
96  /// Redoes the top action on the redo stack.
97  void redo();
98 
99 private: // functions
100  /// Adds an action to the undo stack.
101  void add(undo_action_base * action)
102  { undos_.push_back(action); redos_.clear(); }
103  /// Applies the pending fog/shroud changes from the undo stack.
104  bool apply_shroud_changes() const;
105 
106 private: // data
107  action_list undos_;
108  redos_list redos_;
109 
110  /// Tracks the current side.
111  int side_;
112  /// Tracks if actions have been cleared from the stack since the turn began.
114 };
115 
116 
117 }//namespace actions
118 
119 #endif
void clear()
Clears the stack of undoable (and redoable) actions.
Definition: undo.cpp:226
bool committed_actions_
Tracks if actions have been cleared from the stack since the turn began.
Definition: undo.hpp:113
Various functions implementing vision (through fog of war and shroud).
action_list undos_
Definition: undo.hpp:107
void undo()
Undoes the top action on the undo stack.
Definition: undo.cpp:362
void add(undo_action_base *action)
Adds an action to the undo stack.
Definition: undo.hpp:101
int side_
Tracks the current side.
Definition: undo.hpp:111
void new_side_turn(int side)
Performs some initializations and error checks when starting a new side-turn.
Definition: undo.cpp:268
bool apply_shroud_changes() const
Applies the pending fog/shroud changes from the undo stack.
Definition: undo.cpp:454
bool can_undo() const
True if there are actions that can be undone.
Definition: undo.hpp:91
boost::ptr_vector< undo_action_base > action_list
Definition: undo.hpp:40
Records information to be able to undo an action.
Definition: undo_action.hpp:28
void read(const config &cfg)
Read the undo_list from the provided config.
Definition: undo.cpp:294
GLuint GLuint end
Definition: glew.h:1221
bool can_redo() const
True if there are actions that can be redone.
Definition: undo.hpp:93
void commit_vision()
Updates fog/shroud based on the undo stack, then updates stack as needed.
Definition: undo.cpp:249
Encapsulates the map of the game.
Definition: location.hpp:38
void add_recruit(const unit_const_ptr u, const map_location &loc, const map_location &from, int orig_village_owner, bool time_bonus)
Adds a recruit to the undo stack.
Definition: undo.cpp:200
void add_dismissal(const unit_const_ptr u)
Adds a dismissal to the undo stack.
Definition: undo.cpp:171
static undo_action_base * create_action(const config &cfg)
Creates an undo_action based on a config.
Definition: undo.cpp:79
void redo()
Redoes the top action on the redo stack.
Definition: undo.cpp:413
redos_list redos_
Definition: undo.hpp:108
void add_recall(const unit_const_ptr u, const map_location &loc, const map_location &from, int orig_village_owner, bool time_bonus)
Adds a recall to the undo stack.
Definition: undo.cpp:191
boost::ptr_vector< undo_action > redos_list
Definition: undo.hpp:41
void add_move(const unit_const_ptr u, const std::vector< map_location >::const_iterator &begin, const std::vector< map_location >::const_iterator &end, int start_moves, int timebonus=0, int village_owner=-1, const map_location::DIRECTION dir=map_location::NDIRECTIONS)
Adds a move to the undo stack.
Definition: undo.cpp:179
DIRECTION
Valid directions which can be moved in our hexagonal world.
Definition: location.hpp:40
void add_update_shroud()
Adds a shroud update to the undo stack.
Definition: undo.cpp:211
Class to store the actions that a player can undo and redo.
Definition: undo.hpp:38
bool player_acted() const
Returns true if the player has performed any actions this turn.
Definition: undo.hpp:82
void add_dummy()
Adds an auto-shroud toggle to the undo stack.
Definition: undo.cpp:160
void add_auto_shroud(bool turned_on)
Adds an auto-shroud toggle to the undo stack.
Definition: undo.cpp:152
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
undo_list(const config &cfg)
Constructor.
Definition: undo.cpp:132
~undo_list()
Destructor.
Definition: undo.cpp:142
void write(config &cfg) const
Write the undo_list into the provided config.
Definition: undo.cpp:346