The Battle for Wesnoth  1.19.10+dev
undo_move_action.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2025
3  Part of the Battle for Wesnoth Project https://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 
16 
17 #include "resources.hpp"
18 #include "units/map.hpp"
20 #include "log.hpp"
21 #include "game_display.hpp"
22 #include "units/udisplay.hpp"
23 #include "units/unit.hpp"
24 #include "game_board.hpp"
25 
26 static lg::log_domain log_engine("engine");
27 #define ERR_NG LOG_STREAM(err, log_engine)
28 #define LOG_NG LOG_STREAM(info, log_engine)
29 
30 namespace actions::undo
31 {
33  const std::vector<map_location>::const_iterator & begin,
34  const std::vector<map_location>::const_iterator & end,
35  int sm, const map_location::direction dir)
36  : undo_action()
37  , shroud_clearing_action(moved, begin, end)
38  , starting_moves(sm)
39  , starting_dir(dir == map_location::direction::indeterminate ? moved->facing() : dir)
40  , goto_hex(moved->get_goto())
41 {
42 }
43 
44 /**
45  * Writes this into the provided config.
46  */
47 void move_action::write(config & cfg) const
48 {
49  undo_action::write(cfg);
51  cfg["starting_direction"] = map_location::write_direction(starting_dir);
52  cfg["starting_moves"] = starting_moves;
53  config & child = cfg.mandatory_child("unit");
54  child["goto_x"] = goto_hex.wml_x();
55  child["goto_y"] = goto_hex.wml_y();
56 }
57 
58 /**
59  * Undoes this action.
60  * @return true on success; false on an error.
61  */
63 {
65  unit_map & units = resources::gameboard->units();
66 
67  // Copy some of our stored data.
68  const int saved_moves = starting_moves;
69  std::vector<map_location> rev_route = route;
70  std::reverse(rev_route.begin(), rev_route.end());
71 
72  // Check units.
73  unit_map::iterator u = units.find(rev_route.front());
74  const unit_map::iterator u_end = units.find(rev_route.back());
75  if(u == units.end() || u_end != units.end()) {
76  // this can actually happen if the scenario designer has abused the [allow_undo] command
77  ERR_NG << "Illegal 'undo' found. Possible abuse of [allow_undo]?";
78  return false;
79  }
80 
81  // Record the unit's current state so it can be redone.
82  starting_moves = u->movement_left();
83  goto_hex = u->get_goto();
84 
85  // Move the unit.
87  u->anim_comp().reset_affect_adjacent(gui);
88  units.move(u->get_location(), rev_route.back());
90  u->anim_comp().reset_affect_adjacent(gui);
91 
92  // Restore the unit's old state.
93  u = units.find(rev_route.back());
94  u->set_goto(map_location());
95  u->set_movement(saved_moves, true);
96  u->anim_comp().set_standing();
97 
98  gui.invalidate_unit_after_move(rev_route.front(), rev_route.back());
99  return true;
100 }
102 
103 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:158
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:362
virtual const unit_map & units() const override
Definition: game_board.hpp:107
static game_display * get_singleton()
Container associating units to locations.
Definition: map.hpp:98
unit_iterator end()
Definition: map.hpp:428
unit_iterator find(std::size_t id)
Definition: map.cpp:302
umap_retval_pair_t move(const map_location &src, const map_location &dst)
Moves a unit from location src to location dst.
Definition: map.cpp:92
static void clear_status_caches()
Clear this unit status cache for all units.
Definition: unit.cpp:750
Standard logging facilities (interface).
static auto reg_undo_move
General purpose widgets.
game_board * gameboard
Definition: resources.cpp:20
void move_unit(const std::vector< map_location > &path, const unit_ptr &u, bool animate, map_location::direction dir, bool force_scroll)
Display a unit moving along a given path.
Definition: udisplay.cpp:509
constexpr auto reverse
Definition: ranges.hpp:40
@ moved
All moves and possible attacks have been done.
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
base class for classes that clear srhoud (move/recruit/recall)
route_t route
The hexes occupied by the affected unit during this action.
map_location::direction starting_dir
virtual bool undo(int side)
Undoes this action.
move_action(const unit_const_ptr &moved, const std::vector< map_location >::const_iterator &begin, const std::vector< map_location >::const_iterator &end, int sm, const map_location::direction dir)
virtual void write(config &cfg) const
Writes this into the provided config.
Records information to be able to undo an action.
Definition: undo_action.hpp:80
virtual void write(config &cfg) const
Writes this into the provided config.
Definition: undo_action.hpp:91
Encapsulates the map of the game.
Definition: location.hpp:45
static std::string write_direction(direction dir)
Definition: location.cpp:154
int wml_y() const
Definition: location.hpp:218
direction
Valid directions which can be moved in our hexagonal world.
Definition: location.hpp:47
int wml_x() const
Definition: location.hpp:217
pointer get_shared_ptr() const
This is exactly the same as operator-> but it's slightly more readable, and can replace &*iter syntax...
Definition: map.hpp:217
Display units performing various actions: moving, attacking, and dying.
static lg::log_domain log_engine("engine")
#define ERR_NG