The Battle for Wesnoth  1.19.7+dev
undo_move_action.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2024
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  * Reset halo of adjacent units when undo move.
60  */
61 static void reset_adjacent(bool& halo_adjacent, unit_map& units, const map_location& loc)
62 {
63  if(halo_adjacent){
64  unit_map::iterator u = units.find(loc);
65  const auto adjacent = get_adjacent_tiles(loc);
66  for(unsigned i = 0; i < adjacent.size(); ++i) {
67  const unit_map::const_iterator it = units.find(adjacent[i]);
68  if (it == units.end() || it->incapacitated())
69  continue;
70  if ( &*it == &*u )
71  continue;
72  it->anim_comp().set_standing();
73  }
74  }
75 }
76 
77 /**
78  * Undoes this action.
79  * @return true on success; false on an error.
80  */
82 {
84  unit_map & units = resources::gameboard->units();
85 
86  // Copy some of our stored data.
87  const int saved_moves = starting_moves;
88  std::vector<map_location> rev_route = route;
89  std::reverse(rev_route.begin(), rev_route.end());
90 
91  // Check units.
92  unit_map::iterator u = units.find(rev_route.front());
93  const unit_map::iterator u_end = units.find(rev_route.back());
94  if(u == units.end() || u_end != units.end()) {
95  // this can actually happen if the scenario designer has abused the [allow_undo] command
96  ERR_NG << "Illegal 'undo' found. Possible abuse of [allow_undo]?";
97  return false;
98  }
99 
100  // Record the unit's current state so it can be redone.
101  starting_moves = u->movement_left();
102  goto_hex = u->get_goto();
103 
104  // Move the unit.
105  unit_display::move_unit(rev_route, u.get_shared_ptr(), true, starting_dir);
106  bool halo_adjacent = false;
107  for(const auto [_, cfg] : u->abilities().all_children_view()){
108  if(!cfg["halo_image"].empty() && cfg.has_child("affect_adjacent")){
109  halo_adjacent = true;
110  break;
111  }
112  }
113  reset_adjacent(halo_adjacent, units, rev_route.front());
114  units.move(u->get_location(), rev_route.back());
116  reset_adjacent(halo_adjacent, units, rev_route.back());
117 
118  // Restore the unit's old state.
119  u = units.find(rev_route.back());
120  u->set_goto(map_location());
121  u->set_movement(saved_moves, true);
122  u->anim_comp().set_standing();
123 
124  gui.invalidate_unit_after_move(rev_route.front(), rev_route.back());
125  return true;
126 }
128 
129 }
map_location loc
Definition: move.cpp:172
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
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:366
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:741
std::size_t i
Definition: function.cpp:1029
static std::string _(const char *str)
Definition: gettext.hpp:93
void get_adjacent_tiles(const map_location &a, map_location *res)
Function which, given a location, will place all adjacent locations in res.
Definition: location.cpp:479
Standard logging facilities (interface).
static auto reg_undo_move
static void reset_adjacent(bool &halo_adjacent, unit_map &units, const map_location &loc)
Reset halo of adjacent units when 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:505
@ 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:184
direction
Valid directions which can be moved in our hexagonal world.
Definition: location.hpp:47
int wml_x() const
Definition: location.hpp:183
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