The Battle for Wesnoth  1.13.10+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 /*
2  Copyright (C) 2017 the Battle for Wesnoth Project http://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
15 #include "actions/move.hpp"
16 
17 #include "resources.hpp"
18 #include "replay.hpp"
19 #include "units/map.hpp"
21 #include "log.hpp"
22 #include "game_display.hpp"
23 #include "units/udisplay.hpp"
24 #include "game_board.hpp"
25 #include "map/map.hpp"
26 
27 static lg::log_domain log_engine("engine");
28 #define ERR_NG LOG_STREAM(err, log_engine)
29 #define LOG_NG LOG_STREAM(info, log_engine)
30 
31 namespace actions
32 {
33 namespace undo
34 {
35 
36 
37 /**
38  * Writes this into the provided config.
39  */
40 void move_action::write(config & cfg) const
41 {
42  undo_action::write(cfg);
44  cfg["starting_direction"] = map_location::write_direction(starting_dir);
45  cfg["starting_moves"] = starting_moves;
46  config & child = cfg.child("unit");
47  child["goto_x"] = goto_hex.wml_x();
48  child["goto_y"] = goto_hex.wml_y();
49 }
50 
51 /**
52  * Undoes this action.
53  * @return true on success; false on an error.
54  */
56 {
59 
60  // Copy some of our stored data.
61  const int saved_moves = starting_moves;
62  std::vector<map_location> rev_route = route;
63  std::reverse(rev_route.begin(), rev_route.end());
64 
65  // Check units.
66  unit_map::iterator u = units.find(rev_route.front());
67  const unit_map::iterator u_end = units.find(rev_route.back());
68  if ( u == units.end() || u_end != units.end() ) {
69  //this can actually happen if the scenario designer has abused the [allow_undo] command
70  ERR_NG << "Illegal 'undo' found. Possible abuse of [allow_undo]?" << std::endl;
71  return false;
72  }
73  this->return_village();
74 
75  // Record the unit's current state so it can be redone.
76  starting_moves = u->movement_left();
77  goto_hex = u->get_goto();
78 
79  // Move the unit.
80  unit_display::move_unit(rev_route, u.get_shared_ptr(), true, starting_dir);
81  units.move(u->get_location(), rev_route.back());
83 
84  // Restore the unit's old state.
85  u = units.find(rev_route.back());
86  u->set_goto(map_location());
87  u->set_movement(saved_moves, true);
88  u->anim_comp().set_standing();
89 
90  gui.invalidate_unit_after_move(rev_route.front(), rev_route.back());
92  return true;
93 }
94 
95 
96 }
97 }
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:352
virtual const unit_map & units() const
Definition: game_board.hpp:97
unit_iterator end()
Definition: map.hpp:415
game_display * screen
Definition: resources.cpp:27
General purpose widgets.
Replay control code.
int wml_y() const
Definition: location.hpp:116
map_location::DIRECTION starting_dir
virtual bool undo(int side)
Undoes this action.
void return_village()
Change village owner on undo.
static void clear_status_caches()
Clear this unit status cache for all units.
Definition: unit.cpp:617
int wml_x() const
Definition: location.hpp:115
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:94
#define ERR_NG
virtual void write(config &cfg) const
Writes this into the provided config.
Encapsulates the map of the game.
Definition: location.hpp:40
Various functions related to moving units.
virtual void write(config &cfg) const
Writes this into the provided config.
Standard logging facilities (interface).
Container associating units to locations.
Definition: map.hpp:99
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:193
unit_iterator find(size_t id)
Definition: map.cpp:311
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
static std::string write_direction(DIRECTION dir)
Definition: location.cpp:139
unit_map * units
Definition: resources.cpp:34
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:495