The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
undo_move_action.cpp
Go to the documentation of this file.
1 #include "undo_move_action.hpp"
2 #include "move.hpp"
3 
4 #include "resources.hpp"
5 #include "replay.hpp"
6 #include "units/map.hpp"
8 #include "log.hpp"
9 #include "game_display.hpp"
10 #include "units/udisplay.hpp"
11 #include "game_board.hpp"
12 #include "map/map.hpp"
13 
14 static lg::log_domain log_engine("engine");
15 #define ERR_NG LOG_STREAM(err, log_engine)
16 #define LOG_NG LOG_STREAM(info, log_engine)
17 
18 namespace actions
19 {
20 namespace undo
21 {
22 
23 
24 /**
25  * Writes this into the provided config.
26  */
27 void move_action::write(config & cfg) const
28 {
29  undo_action::write(cfg);
31  cfg["starting_direction"] = map_location::write_direction(starting_dir);
32  cfg["starting_moves"] = starting_moves;
33  config & child = cfg.child("unit");
34  child["goto_x"] = goto_hex.x + 1;
35  child["goto_y"] = goto_hex.y + 1;
36 }
37 
38 /**
39  * Undoes this action.
40  * @return true on success; false on an error.
41  */
43 {
46 
47  // Copy some of our stored data.
48  const int saved_moves = starting_moves;
49  std::vector<map_location> rev_route = route;
50  std::reverse(rev_route.begin(), rev_route.end());
51 
52  // Check units.
53  unit_map::iterator u = units.find(rev_route.front());
54  const unit_map::iterator u_end = units.find(rev_route.back());
55  if ( u == units.end() || u_end != units.end() ) {
56  //this can actually happen if the scenario designer has abused the [allow_undo] command
57  ERR_NG << "Illegal 'undo' found. Possible abuse of [allow_undo]?" << std::endl;
58  return false;
59  }
60  this->return_village();
61 
62  // Record the unit's current state so it can be redone.
63  starting_moves = u->movement_left();
64  goto_hex = u->get_goto();
65 
66  // Move the unit.
67  unit_display::move_unit(rev_route, u.get_shared_ptr(), true, starting_dir);
68  units.move(u->get_location(), rev_route.back());
70 
71  // Restore the unit's old state.
72  u = units.find(rev_route.back());
73  u->set_goto(map_location());
74  u->set_movement(saved_moves, true);
75  u->anim_comp().set_standing();
76 
77  gui.invalidate_unit_after_move(rev_route.front(), rev_route.back());
79  return true;
80 }
81 
82 /**
83  * Redoes this action.
84  * @return true on success; false on an error.
85  */
87 {
90 
91  // Check units.
92  unit_map::iterator u = units.find(route.front());
93  if ( u == units.end() ) {
94  ERR_NG << "Illegal movement 'redo'." << std::endl;
95  assert(false);
96  return false;
97  }
98 
99  // Adjust starting moves.
100  const int saved_moves = starting_moves;
101  starting_moves = u->movement_left();
102 
103  // Move the unit.
105  units.move(u->get_location(), route.back());
106  u = units.find(route.back());
108 
109  // Set the unit's state.
110  u->set_goto(goto_hex);
111  u->set_movement(saved_moves, true);
112  u->anim_comp().set_standing();
113 
114  this->take_village();
115 
116  gui.invalidate_unit_after_move(route.front(), route.back());
118  replay_data.clear();
120  return true;
121 }
122 
123 }
124 }
void invalidate_unit_after_move(const map_location &src, const map_location &dst)
Same as invalidate_unit() if moving the displayed unit.
unit_iterator end()
Definition: map.hpp:311
void take_village()
Change village owner on redo.
game_display * screen
Definition: resources.cpp:27
General purpose widgets.
virtual bool redo(int side)
Redoes this action.
Replay control code.
map_location::DIRECTION starting_dir
void clear()
Definition: config.cpp:1055
virtual bool undo(int side)
Undoes this action.
void return_village()
Change village owner on undo.
static void clear_status_caches()
Clear the unit status cache for all units.
Definition: unit.cpp:610
void redo(const config &dst)
Definition: replay.cpp:410
pointer get_shared_ptr() const
Definition: map.hpp:180
#define ERR_NG
replay * recorder
Definition: resources.cpp:30
virtual void write(config &cfg) const
Writes this into the provided config.
Encapsulates the map of the game.
Definition: location.hpp:38
Various functions related to moving units.
virtual void write(config &cfg) const
Writes this into the provided config.
t_route route
The hexes occupied by the affected unit during this action.
std::pair< unit_iterator, bool > move(const map_location &src, const map_location &dst)
Moves a unit from location src to location dst.
Definition: map.cpp:79
config & child(const std::string &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:658
Standard logging facilities (interface).
Container associating units to locations.
Definition: map.hpp:90
static lg::log_domain log_engine("engine")
unit_iterator find(size_t id)
Definition: map.cpp:285
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
static std::string write_direction(DIRECTION dir)
Definition: location.cpp:144
config replay_data
the replay data to do this action, this is only !empty() when this action is on the redo stack we nee...
Definition: undo_action.hpp:69
unit_map * units
Definition: resources.cpp:35
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:486