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 "units/unit.hpp"
25 #include "game_board.hpp"
26 #include "map/map.hpp"
27 
28 static lg::log_domain log_engine("engine");
29 #define ERR_NG LOG_STREAM(err, log_engine)
30 #define LOG_NG LOG_STREAM(info, log_engine)
31 
32 namespace actions
33 {
34 namespace undo
35 {
36 
38  const std::vector<map_location>::const_iterator & begin,
39  const std::vector<map_location>::const_iterator & end,
40  int sm, int timebonus, int orig, const map_location::DIRECTION dir)
41  : undo_action()
42  , shroud_clearing_action(moved, begin, end, orig, timebonus != 0)
43  , starting_moves(sm)
44  , starting_dir(dir == map_location::NDIRECTIONS ? moved->facing() : dir)
45  , goto_hex(moved->get_goto())
46 {
47 }
48 
49 /**
50  * Writes this into the provided config.
51  */
52 void move_action::write(config & cfg) const
53 {
54  undo_action::write(cfg);
56  cfg["starting_direction"] = map_location::write_direction(starting_dir);
57  cfg["starting_moves"] = starting_moves;
58  config & child = cfg.child("unit");
59  child["goto_x"] = goto_hex.wml_x();
60  child["goto_y"] = goto_hex.wml_y();
61 }
62 
63 /**
64  * Undoes this action.
65  * @return true on success; false on an error.
66  */
68 {
71 
72  // Copy some of our stored data.
73  const int saved_moves = starting_moves;
74  std::vector<map_location> rev_route = route;
75  std::reverse(rev_route.begin(), rev_route.end());
76 
77  // Check units.
78  unit_map::iterator u = units.find(rev_route.front());
79  const unit_map::iterator u_end = units.find(rev_route.back());
80  if ( u == units.end() || u_end != units.end() ) {
81  //this can actually happen if the scenario designer has abused the [allow_undo] command
82  ERR_NG << "Illegal 'undo' found. Possible abuse of [allow_undo]?" << std::endl;
83  return false;
84  }
85  this->return_village();
86 
87  // Record the unit's current state so it can be redone.
88  starting_moves = u->movement_left();
89  goto_hex = u->get_goto();
90 
91  // Move the unit.
92  unit_display::move_unit(rev_route, u.get_shared_ptr(), true, starting_dir);
93  units.move(u->get_location(), rev_route.back());
95 
96  // Restore the unit's old state.
97  u = units.find(rev_route.back());
98  u->set_goto(map_location());
99  u->set_movement(saved_moves, true);
100  u->anim_comp().set_standing();
101 
102  gui.invalidate_unit_after_move(rev_route.front(), rev_route.back());
104  return true;
105 }
106 
107 
108 }
109 }
boost::intrusive_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:30
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:400
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.
DIRECTION
Valid directions which can be moved in our hexagonal world.
Definition: location.hpp:42
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:481
actions that are undoable (this does not include update_shroud and auto_shroud)
Definition: undo_action.hpp:60