The Battle for Wesnoth  1.19.1+dev
undo.cpp
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  * Undoing, redoing.
19  */
20 
21 #include "actions/undo.hpp"
22 
23 #include "game_board.hpp" // for game_board
24 #include "game_display.hpp" // for game_display
26 #include "log.hpp" // for LOG_STREAM, logger, etc
27 #include "map/location.hpp" // for map_location, operator<<, etc
28 #include "mouse_handler_base.hpp" // for command_disabler
29 #include "replay.hpp" // for recorder, replay
30 #include "resources.hpp" // for screen, teams, units, etc
31 #include "synced_context.hpp" // for set_scontext_synced
32 #include "team.hpp" // for team
33 #include "units/id.hpp"
34 #include "units/ptr.hpp" // for unit_const_ptr, unit_ptr
35 #include "units/types.hpp" // for unit_type, unit_type_data, etc
36 #include "whiteboard/manager.hpp" // for manager
37 
38 #include "actions/vision.hpp" // for clearer_info, etc
45 
46 #include <algorithm> // for reverse
47 #include <cassert> // for assert
48 
49 static lg::log_domain log_engine("engine");
50 #define ERR_NG LOG_STREAM(err, log_engine)
51 #define LOG_NG LOG_STREAM(info, log_engine)
52 
53 
54 namespace actions {
55 
56 
57 
58 /**
59  * Creates an undo_action based on a config.
60  * @return a pointer that must be deleted, or nullptr if the @a cfg could not be parsed.
61  */
63 {
64  const std::string str = cfg["type"];
65  undo_action_base * res = nullptr;
66  // The general division of labor in this function is that the various
67  // constructors will parse the "unit" child config, while this function
68  // parses everything else.
69 
70  if ( str == "move" ) {
71  res = new undo::move_action(cfg, cfg.child_or_empty("unit"),
72  cfg["starting_moves"],
73  map_location::parse_direction(cfg["starting_direction"]));
74  }
75 
76  else if ( str == "recruit" ) {
77  // Validate the unit type.
78  const config & child = cfg.mandatory_child("unit");
79  const unit_type * u_type = unit_types.find(child["type"]);
80 
81  if ( !u_type ) {
82  // Bad data.
83  ERR_NG << "Invalid recruit found in [undo] or [redo]; unit type '"
84  << child["type"] << "' was not found.\n";
85  return nullptr;
86  }
87  res = new undo::recruit_action(cfg, *u_type, map_location(cfg.child_or_empty("leader"), nullptr));
88  }
89 
90  else if ( str == "recall" )
91  res = new undo::recall_action(cfg, map_location(cfg.child_or_empty("leader"), nullptr));
92 
93  else if ( str == "dismiss" )
94  res = new undo::dismiss_action(cfg, cfg.mandatory_child("unit"));
95 
96  else if ( str == "auto_shroud" )
97  res = new undo::auto_shroud_action(cfg["active"].to_bool());
98 
99  else if ( str == "update_shroud" )
100  res = new undo::update_shroud_action();
101  else if ( str == "dummy" )
102  res = new undo_dummy_action(cfg);
103  else
104  {
105  // Unrecognized type.
106  ERR_NG << "Unrecognized undo action type: " << str << ".";
107  return nullptr;
108  }
109  return res;
110 }
111 
112 
113 /**
114  * Constructor.
115  * The config is allowed to be invalid.
116  */
118  undos_(), redos_(), side_(1), committed_actions_(false)
119 {
120 }
121 
122 /**
123  * Destructor.
124  */
126 {
127  // Default destructor, but defined out-of-line to localize the templating.
128  // (Might make compiles faster.)
129 }
130 
131 
132 /**
133  * Adds an auto-shroud toggle to the undo stack.
134  */
135 void undo_list::add_auto_shroud(bool turned_on)
136 {
137  add(new undo::auto_shroud_action(turned_on));
138 }
139 
141 {
142  add(new undo_dummy_action());
143 }
144 
145 /**
146  * Adds a dismissal to the undo stack.
147  */
149 {
150  add(new undo::dismiss_action(u));
151 }
152 
153 /**
154  * Adds a move to the undo stack.
155  */
157  const std::vector<map_location>::const_iterator & begin,
158  const std::vector<map_location>::const_iterator & end,
159  int start_moves, int timebonus, int village_owner,
160  const map_location::DIRECTION dir)
161 {
162  add(new undo::move_action(u, begin, end, start_moves, timebonus, village_owner, dir));
163 }
164 
165 /**
166  * Adds a recall to the undo stack.
167  */
169  const map_location& from, int orig_village_owner, bool time_bonus)
170 {
171  add(new undo::recall_action(u, loc, from, orig_village_owner, time_bonus));
172 }
173 
174 /**
175  * Adds a recruit to the undo stack.
176  */
178  const map_location& from, int orig_village_owner, bool time_bonus)
179 {
180  add(new undo::recruit_action(u, loc, from, orig_village_owner, time_bonus));
181 }
182 
183 /**
184  * Adds a shroud update to the undo stack.
185  * This is called from within commit_vision(), so there should be no need
186  * for this to be publicly visible.
187  */
189 {
191 }
192 
193 
194 /**
195  * Clears the stack of undoable (and redoable) actions.
196  * (Also handles updating fog/shroud if needed.)
197  * Call this if an action alters the game state, but add that action to the
198  * stack before calling this (if the action is a kind that can be undone).
199  * This may fire events and change the game state.
200  */
202 {
203  // The fact that this function was called indicates that something was done.
204  // (Some actions, such as attacks, are never put on the stack.)
205  committed_actions_ = true;
206 
207  // We can save some overhead by not calling apply_shroud_changes() for an
208  // empty stack.
209  if ( !undos_.empty() ) {
211  undos_.clear();
212  }
213  // No special handling for redos, so just clear that stack.
214  redos_.clear();
215 }
216 
217 
218 /**
219  * Updates fog/shroud based on the undo stack, then updates stack as needed.
220  * Call this when "updating shroud now".
221  * This may fire events and change the game state.
222  * @param[in] is_replay Set to true when this is called during a replay.
223  */
225 {
226  // Update fog/shroud.
227  bool cleared_something = apply_shroud_changes();
228 
229  if (cleared_something) {
230  // The actions that led to information being revealed can no longer
231  // be undone.
232  undos_.clear();
233  //undos_.erase(undos_.begin(), undos_.begin() + erase_to);
234  committed_actions_ = true;
235  }
236  return cleared_something;
237 }
238 
239 
240 /**
241  * Performs some initializations and error checks when starting a new side-turn.
242  * @param[in] side The side whose turn is about to start.
243  */
245 {
246  // Error checks.
247  if ( !undos_.empty() ) {
248  ERR_NG << "Undo stack not empty in new_side_turn().";
249  // At worst, someone missed some sighted events, so try to recover.
250  undos_.clear();
251  redos_.clear();
252  }
253  else if ( !redos_.empty() ) {
254  ERR_NG << "Redo stack not empty in new_side_turn().";
255  // Sloppy tracking somewhere, but not critically so.
256  redos_.clear();
257  }
258 
259  // Reset the side.
260  side_ = side;
261  committed_actions_ = false;
262 }
263 
264 
265 /**
266  * Read the undo_list from the provided config.
267  * Currently, this is only used when the undo_list is empty, but in theory
268  * it could be used to append the config to the current data.
269  */
270 void undo_list::read(const config & cfg)
271 {
272  // Merge header data.
273  side_ = cfg["side"].to_int(side_);
274  committed_actions_ = committed_actions_ || cfg["committed"].to_bool();
275 
276  // Build the undo stack.
277  for (const config & child : cfg.child_range("undo")) {
278  try {
279  undo_action_base * action = create_action(child);
280  if ( action ) {
281  undos_.emplace_back(action);
282  }
283  } catch (const bad_lexical_cast &) {
284  ERR_NG << "Error when parsing undo list from config: bad lexical cast.";
285  ERR_NG << "config was: " << child.debug();
286  ERR_NG << "Skipping this undo action...";
287  } catch (const config::error& e) {
288  ERR_NG << "Error when parsing undo list from config: " << e.what();
289  ERR_NG << "config was: " << child.debug();
290  ERR_NG << "Skipping this undo action...";
291  }
292  }
293 
294  // Build the redo stack.
295  for (const config & child : cfg.child_range("redo")) {
296  try {
297  redos_.emplace_back(new config(child));
298  } catch (const bad_lexical_cast &) {
299  ERR_NG << "Error when parsing redo list from config: bad lexical cast.";
300  ERR_NG << "config was: " << child.debug();
301  ERR_NG << "Skipping this redo action...";
302  } catch (const config::error& e) {
303  ERR_NG << "Error when parsing redo list from config: " << e.what();
304  ERR_NG << "config was: " << child.debug();
305  ERR_NG << "Skipping this redo action...";
306  }
307  }
308 }
309 
310 
311 /**
312  * Write the undo_list into the provided config.
313  */
314 void undo_list::write(config & cfg) const
315 {
316  cfg["side"] = side_;
317  cfg["committed"] = committed_actions_;
318 
319  for ( const auto& action_ptr : undos_)
320  action_ptr->write(cfg.add_child("undo"));
321 
322  for ( const auto& cfg_ptr : redos_)
323  cfg.add_child("redo") = *cfg_ptr;
324 }
325 
326 
327 /**
328  * Undoes the top action on the undo stack.
329  */
331 {
332  if ( undos_.empty() )
333  return;
334 
335  const events::command_disabler disable_commands;
336 
338 
339  // Get the action to undo. (This will be placed on the redo stack, but
340  // only if the undo is successful.)
341  auto action = std::move(undos_.back());
342  undos_.pop_back();
343  if (undo_action* undoable_action = dynamic_cast<undo_action*>(action.get()))
344  {
345  int last_unit_id = resources::gameboard->unit_id_manager().get_save_id();
346  if ( !undoable_action->undo(side_) ) {
347  return;
348  }
349  if(last_unit_id - undoable_action->unit_id_diff < 0) {
350  ERR_NG << "Next unit id is below 0 after undoing";
351  }
352  resources::gameboard->unit_id_manager().set_save_id(last_unit_id - undoable_action->unit_id_diff);
353 
354  // Bookkeeping.
355  redos_.emplace_back(new config());
357 
358  resources::whiteboard->on_gamestate_change();
359 
360  // Screen updates.
361  gui.invalidate_unit();
362  gui.invalidate_game_status();
363  gui.redraw_minimap();
364  }
365  else
366  {
367  //ignore this action, and undo the previous one.
368  config replay_data;
369  resources::recorder->undo_cut(replay_data);
370  undo();
371  resources::recorder->redo(replay_data);
372  undos_.emplace_back(std::move(action));
373  }
374  if(std::all_of(undos_.begin(), undos_.end(), [](const action_ptr_t& action){ return dynamic_cast<undo_action*>(action.get()) == nullptr; }))
375  {
376  //clear the undo stack if it only contains dsu related actions, this in particular makes sure loops like `while(can_undo()) { undo(); }`always stop.
377  undos_.clear();
378  }
379 }
380 
381 
382 
383 /**
384  * Redoes the top action on the redo stack.
385  */
387 {
388  if (redos_.empty()) {
389  return;
390  }
391  // Get the action to redo.
392  auto action = std::move(redos_.back());
393  redos_.pop_back();
394 
395  auto [commandname, data] = action->mandatory_child("command").all_children_range().front();
396 
397  // Note that this might add more than one [command]
398  resources::recorder->redo(*action);
399 
400  auto error_handler = [](const std::string& msg) {
401  ERR_NG << "Out of sync when redoing: " << msg;
402  gui2::show_transient_message(_("Redo Error"),
403  _("The redo stack is out of sync. This is most commonly caused by a corrupt save file or by faulty WML code in the scenario or era. Details:") + msg);
404 
405  };
406  // synced_context::run readds the undo command with the normal
407  // undo_list::add function which clears the redo stack which would
408  // make redoing of more than one move impossible. To work around
409  // that we save redo stack here and set it later.
410  redos_list temp;
411  temp.swap(redos_);
412  synced_context::run(commandname, data, /*use_undo*/ true, /*show*/ true, error_handler);
413  temp.swap(redos_);
414 
415  // Screen updates.
417  gui.invalidate_unit();
418  gui.invalidate_game_status();
419  gui.redraw_minimap();
420 }
421 
422 
423 
424 
425 
426 /**
427  * Applies the pending fog/shroud changes from the undo stack.
428  * Does nothing if the the current side does not use fog or shroud.
429  * @returns true if shroud or fog was cleared.
430  */
432 {
435  // No need to do clearing if fog/shroud has been kept up-to-date.
436  if ( tm.auto_shroud_updates() || !tm.fog_or_shroud() ) {
437  return false;
438  }
439  shroud_clearer clearer;
440  bool cleared_shroud = false;
441  const std::size_t list_size = undos_.size();
442 
443 
444  // Loop through the list of undo_actions.
445  for( std::size_t i = 0; i != list_size; ++i ) {
446  if (const shroud_clearing_action* action = dynamic_cast<const shroud_clearing_action*>(undos_[i].get())) {
447  LOG_NG << "Turning an undo...";
448 
449  // Clear the hexes this unit can see from each hex occupied during
450  // the action.
451  std::vector<map_location>::const_iterator step;
452  for (step = action->route.begin(); step != action->route.end(); ++step) {
453  // Clear the shroud, collecting new sighted events.
454  // (This can be made gradual by changing "true" to "false".)
455  if ( clearer.clear_unit(*step, tm, action->view_info, true) ) {
456  cleared_shroud = true;
457  }
458  }
459  }
460  }
461 
462 
463  if (!cleared_shroud) {
464  return false;
465  }
466  // If we clear fog or shroud outside a synced context we get OOS
467  // Note that it can happen that we call this function from ouside a synced context
468  // when we reload a game and want to prevent undoing. But in this case this is
469  // preceded by a manual update_shroud call so that cleared_shroud is false.
470  assert(synced_context::is_synced());
471 
472  // The entire stack needs to be cleared in order to preserve replays.
473  // (The events that fired might depend on current unit positions.)
474  // (Also the events that did not fire might depend on unit positions (they whould have fired if the unit would have standed on different positions, for example this can happen if they have a [have_unit] in [filter_condition]))
475 
476  // Update the display before pumping events.
477  clearer.invalidate_after_clear();
478 
479  // Fire sighted events
480  if ( std::get<0>(clearer.fire_events() )) {
481  // Fix up the display in case WML changed stuff.
483  disp.invalidate_unit();
484  }
485 
486  return true;
487 }
488 
489 }//namespace actions
Class to encapsulate fog/shroud clearing and the resultant sighted events.
Definition: vision.hpp:72
void invalidate_after_clear()
The invalidations that should occur after invoking clear_unit().
Definition: vision.cpp:576
game_events::pump_result_t fire_events()
Fires the sighted events that were earlier recorded by fog/shroud clearing.
Definition: vision.cpp:541
bool clear_unit(const map_location &view_loc, team &view_team, std::size_t viewer_id, int sight_range, bool slowed, const movetype::terrain_costs &costs, const map_location &real_loc, const std::set< map_location > *known_units=nullptr, std::size_t *enemy_count=nullptr, std::size_t *friend_count=nullptr, move_unit_spectator *spectator=nullptr, bool instant=true)
Clears shroud (and fog) around the provided location for view_team based on sight_range,...
Definition: vision.cpp:330
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
void read(const config &cfg)
Read the undo_list from the provided config.
Definition: undo.cpp:270
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
void write(config &cfg) const
Write the undo_list into the provided config.
Definition: undo.cpp:314
action_list undos_
Definition: undo.hpp:115
~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
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
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
const config & child_or_empty(config_key_type key) const
Returns the first child with the given key, or an empty config if there is none.
Definition: config.cpp:395
config & mandatory_child(config_key_type key, int n=0)
Returns the nth child with the given key, or throws an error if there is none.
Definition: config.cpp:367
child_itors child_range(config_key_type key)
Definition: config.cpp:273
config & add_child(config_key_type key)
Definition: config.cpp:441
team & get_team(int i)
Definition: game_board.hpp:91
n_unit::id_manager & unit_id_manager()
Definition: game_board.hpp:73
void invalidate_unit()
Function to invalidate that unit status displayed on the sidebar.
static game_display * get_singleton()
std::size_t get_save_id() const
Used for saving id to savegame.
Definition: id.cpp:42
void set_save_id(std::size_t)
Definition: id.cpp:47
void undo_cut(config &dst)
Definition: replay.cpp:502
void redo(const config &dst, bool set_to_end=false)
Definition: replay.cpp:417
static bool run(const std::string &commandname, const config &data, bool use_undo=true, bool show=true, synced_command::error_handler_function error_handler=default_error_function)
Sets the context to 'synced', initialises random context, and calls the given function.
static bool is_synced()
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:74
bool fog_or_shroud() const
Definition: team.hpp:305
bool auto_shroud_updates() const
Definition: team.hpp:324
const unit_type * find(const std::string &key, unit_type::BUILD_STATUS status=unit_type::FULL) const
Finds a unit_type by its id() and makes sure it is built to the specified level.
Definition: types.cpp:1267
A single unit type that the player may recruit.
Definition: types.hpp:43
std::size_t i
Definition: function.cpp:968
static std::string _(const char *str)
Definition: gettext.hpp:93
Standard logging facilities (interface).
bool clear_shroud(int side, bool reset_fog, bool fire_events)
Function that will clear shroud (and fog) based on current unit positions.
Definition: vision.cpp:749
CURSOR_TYPE get()
Definition: cursor.cpp:216
void show_transient_message(const std::string &title, const std::string &message, const std::string &image, const bool message_use_markup, const bool title_use_markup)
Shows a transient message to the user.
General purpose widgets.
game_board * gameboard
Definition: resources.cpp:20
replay * recorder
Definition: resources.cpp:28
std::shared_ptr< wb::manager > whiteboard
Definition: resources.cpp:33
std::shared_ptr< action > action_ptr
Definition: typedefs.hpp:62
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
std::string_view data
Definition: picture.cpp:194
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
Replay control code.
base class for classes that clear srhoud (move/recruit/recall)
Records information to be able to undo an action.
Definition: undo_action.hpp:42
actions that are undoable (this does not include update_shroud and auto_shroud)
Definition: undo_action.hpp:66
entry for player actions that do not need any special code to be performed when undoing such as right...
Thrown when a lexical_cast fails.
Encapsulates the map of the game.
Definition: location.hpp:38
static DIRECTION parse_direction(const std::string &str)
Definition: location.cpp:65
DIRECTION
Valid directions which can be moved in our hexagonal world.
Definition: location.hpp:40
unit_type_data unit_types
Definition: types.cpp:1486
static lg::log_domain log_engine("engine")
#define ERR_NG
Definition: undo.cpp:50
#define LOG_NG
Definition: undo.cpp:51
Various functions that implement the undoing (and redoing) of in-game commands.
Various functions implementing vision (through fog of war and shroud).
#define e