The Battle for Wesnoth  1.19.0-dev
undo.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
3  by David White <dave@whitevine.net>
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 /**
17  * @file
18  * Various functions that implement the undoing (and redoing) of in-game commands.
19  */
20 
21 #pragma once
22 
23 #include "vision.hpp"
24 #include "map/location.hpp"
25 #include "units/ptr.hpp"
26 #include "undo_action.hpp"
27 
28 #include <vector>
29 
30 namespace actions {
31 
32 
33 /** Class to store the actions that a player can undo and redo. */
34 class undo_list {
35 
36  typedef std::unique_ptr<undo_action_base> action_ptr_t;
37  typedef std::vector<action_ptr_t> action_list;
38  typedef std::vector<std::unique_ptr<config>> redos_list;
39 
40 public:
41  undo_list(const undo_list&) = delete;
42  undo_list& operator=(const undo_list&) = delete;
43 
44  undo_list();
45  ~undo_list();
46 
47  /**
48  * Creates an undo_action based on a config.
49  * Throws bad_lexical_cast or config::error if it cannot parse the config properly.
50  */
51  static undo_action_base * create_action(const config & cfg);
52 
53  // Functions related to managing the undo stack:
54 
55  /** Adds an auto-shroud toggle to the undo stack. */
56  void add_auto_shroud(bool turned_on);
57  /** Adds an auto-shroud toggle to the undo stack. */
58  void add_dummy();
59  /** Adds a dismissal to the undo stack. */
60  void add_dismissal(const unit_const_ptr u);
61  /** Adds a move to the undo stack. */
62  void add_move(const unit_const_ptr u,
63  const std::vector<map_location>::const_iterator & begin,
64  const std::vector<map_location>::const_iterator & end,
65  int start_moves, int timebonus=0, int village_owner=-1,
67  /** Adds a recall to the undo stack. */
68  void add_recall(const unit_const_ptr u, const map_location& loc,
69  const map_location& from, int orig_village_owner, bool time_bonus);
70  /** Adds a recruit to the undo stack. */
71  void add_recruit(const unit_const_ptr u, const map_location& loc,
72  const map_location& from, int orig_village_owner, bool time_bonus);
73  /** Adds a shroud update to the undo stack. */
74  void add_update_shroud();
75 private:
76 public:
77  /** Clears the stack of undoable (and redoable) actions. */
78  void clear();
79  /**
80  * Updates fog/shroud based on the undo stack, then updates stack as needed.
81  * Returns true if somethign was cleared.
82  */
83  bool commit_vision();
84  /**
85  * Performs some initializations and error checks when starting a new
86  * side-turn.
87  */
88  void new_side_turn(int side);
89  /** Returns true if the player has performed any actions this turn. */
90  bool player_acted() const { return committed_actions_ || !undos_.empty(); }
91  /** Read the undo_list from the provided config. */
92  void read(const config & cfg);
93  /** Write the undo_list into the provided config. */
94  void write(config & cfg) const;
95 
96  // Functions related to using the undo stack:
97 
98  /** True if there are actions that can be undone. */
99  bool can_undo() const { return !undos_.empty(); }
100  /** True if there are actions that can be redone. */
101  bool can_redo() const { return !redos_.empty(); }
102  /** Undoes the top action on the undo stack. */
103  void undo();
104  /** Redoes the top action on the redo stack. */
105  void redo();
106 
107 private: // functions
108  /** Adds an action to the undo stack. */
109  void add(undo_action_base * action)
110  { undos_.emplace_back(action); redos_.clear(); }
111  /** Applies the pending fog/shroud changes from the undo stack. */
112  bool apply_shroud_changes() const;
113 
114 private: // data
117 
118  /** Tracks the current side. */
119  int side_;
120  /** Tracks if actions have been cleared from the stack since the turn began. */
122 };
123 
124 
125 }//namespace actions
Class to store the actions that a player can undo and redo.
Definition: undo.hpp:34
static undo_action_base * create_action(const config &cfg)
Creates an undo_action based on a config.
Definition: undo.cpp:62
void new_side_turn(int side)
Performs some initializations and error checks when starting a new side-turn.
Definition: undo.cpp:244
void add_dismissal(const unit_const_ptr u)
Adds a dismissal to the undo stack.
Definition: undo.cpp:148
bool can_undo() const
True if there are actions that can be undone.
Definition: undo.hpp:99
void read(const config &cfg)
Read the undo_list from the provided config.
Definition: undo.cpp:270
undo_list(const undo_list &)=delete
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:168
bool committed_actions_
Tracks if actions have been cleared from the stack since the turn began.
Definition: undo.hpp:121
undo_list & operator=(const undo_list &)=delete
void write(config &cfg) const
Write the undo_list into the provided config.
Definition: undo.cpp:314
action_list undos_
Definition: undo.hpp:115
std::vector< action_ptr_t > action_list
Definition: undo.hpp:37
~undo_list()
Destructor.
Definition: undo.cpp:125
bool commit_vision()
Updates fog/shroud based on the undo stack, then updates stack as needed.
Definition: undo.cpp:224
void redo()
Redoes the top action on the redo stack.
Definition: undo.cpp:386
undo_list()
Constructor.
Definition: undo.cpp:117
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:156
bool apply_shroud_changes() const
Applies the pending fog/shroud changes from the undo stack.
Definition: undo.cpp:431
std::vector< std::unique_ptr< config > > redos_list
Definition: undo.hpp:38
void clear()
Clears the stack of undoable (and redoable) actions.
Definition: undo.cpp:201
void add_dummy()
Adds an auto-shroud toggle to the undo stack.
Definition: undo.cpp:140
int side_
Tracks the current side.
Definition: undo.hpp:119
void add_auto_shroud(bool turned_on)
Adds an auto-shroud toggle to the undo stack.
Definition: undo.cpp:135
void add_update_shroud()
Adds a shroud update to the undo stack.
Definition: undo.cpp:188
redos_list redos_
Definition: undo.hpp:116
bool player_acted() const
Returns true if the player has performed any actions this turn.
Definition: undo.hpp:90
std::unique_ptr< undo_action_base > action_ptr_t
Definition: undo.hpp:36
void add(undo_action_base *action)
Adds an action to the undo stack.
Definition: undo.hpp:109
bool can_redo() const
True if there are actions that can be redone.
Definition: undo.hpp:101
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:177
void undo()
Undoes the top action on the undo stack.
Definition: undo.cpp:330
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
Records information to be able to undo an action.
Definition: undo_action.hpp:43
Encapsulates the map of the game.
Definition: location.hpp:38
DIRECTION
Valid directions which can be moved in our hexagonal world.
Definition: location.hpp:40
Various functions implementing vision (through fog of war and shroud).