The Battle for Wesnoth  1.17.0-dev
undo_move_action.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2021
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 #include "actions/move.hpp"
17 
18 #include "resources.hpp"
19 #include "replay.hpp"
20 #include "units/map.hpp"
22 #include "log.hpp"
23 #include "game_display.hpp"
24 #include "units/udisplay.hpp"
25 #include "units/unit.hpp"
26 #include "game_board.hpp"
27 #include "map/map.hpp"
28 
29 static lg::log_domain log_engine("engine");
30 #define ERR_NG LOG_STREAM(err, log_engine)
31 #define LOG_NG LOG_STREAM(info, log_engine)
32 
33 namespace actions
34 {
35 namespace undo
36 {
37 
39  const std::vector<map_location>::const_iterator & begin,
40  const std::vector<map_location>::const_iterator & end,
41  int sm, int timebonus, int orig, const map_location::DIRECTION dir)
42  : undo_action()
43  , shroud_clearing_action(moved, begin, end, orig, timebonus != 0)
44  , starting_moves(sm)
45  , starting_dir(dir == map_location::NDIRECTIONS ? moved->facing() : dir)
46  , goto_hex(moved->get_goto())
47 {
48 }
49 
50 /**
51  * Writes this into the provided config.
52  */
53 void move_action::write(config & cfg) const
54 {
55  undo_action::write(cfg);
57  cfg["starting_direction"] = map_location::write_direction(starting_dir);
58  cfg["starting_moves"] = starting_moves;
59  config & child = cfg.child("unit");
60  child["goto_x"] = goto_hex.wml_x();
61  child["goto_y"] = goto_hex.wml_y();
62 }
63 
64 /**
65  * Undoes this action.
66  * @return true on success; false on an error.
67  */
69 {
71  unit_map & units = resources::gameboard->units();
72 
73  // Copy some of our stored data.
74  const int saved_moves = starting_moves;
75  std::vector<map_location> rev_route = route;
76  std::reverse(rev_route.begin(), rev_route.end());
77 
78  // Check units.
79  unit_map::iterator u = units.find(rev_route.front());
80  const unit_map::iterator u_end = units.find(rev_route.back());
81  if ( u == units.end() || u_end != units.end() ) {
82  //this can actually happen if the scenario designer has abused the [allow_undo] command
83  ERR_NG << "Illegal 'undo' found. Possible abuse of [allow_undo]?" << std::endl;
84  return false;
85  }
86  this->return_village();
87 
88  // Record the unit's current state so it can be redone.
89  starting_moves = u->movement_left();
90  goto_hex = u->get_goto();
91 
92  // Move the unit.
93  unit_display::move_unit(rev_route, u.get_shared_ptr(), true, starting_dir);
94  units.move(u->get_location(), rev_route.back());
96 
97  // Restore the unit's old state.
98  u = units.find(rev_route.back());
99  u->set_goto(map_location());
100  u->set_movement(saved_moves, true);
101  u->anim_comp().set_standing();
102 
103  gui.invalidate_unit_after_move(rev_route.front(), rev_route.back());
105  return true;
106 }
107 
108 
109 }
110 }
base class for classes that clear srhoud (move/recruit/recall)
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, int timebonus, int orig, const map_location::DIRECTION dir)
void invalidate_unit_after_move(const map_location &src, const map_location &dst)
Same as invalidate_unit() if moving the displayed unit.
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:417
unit_iterator end()
Definition: map.hpp:427
virtual const unit_map & units() const override
Definition: game_board.hpp:111
General purpose widgets.
int wml_x() const
Definition: location.hpp:152
Replay control code.
map_location::DIRECTION starting_dir
virtual void write(config &cfg) const
Writes this into the provided config.
virtual bool undo(int side)
Undoes this action.
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:26
void return_village()
Change village owner on undo.
static void clear_status_caches()
Clear this unit status cache for all units.
Definition: unit.cpp:642
int wml_y() const
Definition: location.hpp:153
game_board * gameboard
Definition: resources.cpp:20
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:91
#define ERR_NG
Encapsulates the map of the game.
Definition: location.hpp:37
Various functions related to moving units.
unit_iterator find(std::size_t id)
Definition: map.cpp:308
virtual void write(config &cfg) const
Writes this into the provided config.
DIRECTION
Valid directions which can be moved in our hexagonal world.
Definition: location.hpp:39
Standard logging facilities (interface).
Container associating units to locations.
Definition: map.hpp:96
route_t route
The hexes occupied by the affected unit during this action.
static lg::log_domain log_engine("engine")
static void reverse(lua_State *L, StkId from, StkId to)
Definition: lapi.cpp:203
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
static std::string write_direction(DIRECTION dir)
Definition: location.cpp:140
Display units performing various actions: moving, attacking, and dying.
void move_unit(const std::vector< map_location > &path, unit_ptr u, bool animate, map_location::DIRECTION dir, bool force_scroll)
Display a unit moving along a given path.
Definition: udisplay.cpp:509
actions that are undoable (this does not include update_shroud and auto_shroud)
Definition: undo_action.hpp:65
static game_display * get_singleton()