The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
undo_recall_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/create.hpp"
16 
18 #include "game_board.hpp"
19 #include "resources.hpp"
20 #include "team.hpp"
21 #include "replay.hpp"
22 #include "units/map.hpp"
23 #include "units/unit.hpp"
24 #include "statistics.hpp"
25 #include "log.hpp"
26 #include "game_display.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 map_location& from, int orig_village_owner, bool time_bonus)
39  : undo_action()
40  , shroud_clearing_action(recalled, loc, orig_village_owner, time_bonus)
41  , id(recalled->id())
42  , recall_from(from)
43 {}
44 
46  : undo_action(cfg)
48  , id(cfg["id"])
49  , recall_from(from)
50 {}
51 
52 /**
53  * Writes this into the provided config.
54  */
55 void recall_action::write(config & cfg) const
56 {
57  undo_action::write(cfg);
59 
60  recall_from.write(cfg.add_child("leader"));
61  cfg["id"] = id;
62 }
63 
64 /**
65  * Undoes this action.
66  * @return true on success; false on an error.
67  */
68 bool recall_action::undo(int side)
69 {
72  team &current_team = resources::gameboard->get_team(side);
73 
74  const map_location & recall_loc = route.front();
75  unit_map::iterator un_it = units.find(recall_loc);
76  if ( un_it == units.end() ) {
77  return false;
78  }
79 
80  unit_ptr un = un_it.get_shared_ptr();
81  if (!un) {
82  return false;
83  }
84 
86  int cost = statistics::un_recall_unit_cost(*un);
87  if (cost < 0) {
88  current_team.spend_gold(-current_team.recall_cost());
89  }
90  else {
91  current_team.spend_gold(-cost);
92  }
93 
94  current_team.recall_list().add(un);
95  // invalidate before erasing allow us
96  // to also do the overlapped hexes
97  gui.invalidate(recall_loc);
98  units.erase(recall_loc);
99  this->return_village();
101  return true;
102 }
103 
104 }
105 }
boost::intrusive_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:30
base class for classes that clear srhoud (move/recruit/recall)
virtual const unit_map & units() const
Definition: game_board.hpp:97
unit_iterator end()
Definition: map.hpp:415
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:3023
void un_recall_unit(const unit &u)
Definition: statistics.cpp:520
game_display * screen
Definition: resources.cpp:27
General purpose widgets.
virtual bool undo(int side)
Undoes this action.
Replay control code.
-file sdl_utils.hpp
void return_village()
Change village owner on undo.
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:44
team & get_team(int i)
Definition: game_board.hpp:94
int recall_cost() const
Definition: team.hpp:192
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:220
int un_recall_unit_cost(const unit &u)
Definition: statistics.cpp:534
game_board * gameboard
Definition: resources.cpp:20
void spend_gold(const int amount)
Definition: team.hpp:207
void write(config &cfg) const
Definition: location.cpp:210
Encapsulates the map of the game.
Definition: location.hpp:40
virtual void write(config &cfg) const
Writes this into the provided config.
Various functions related to the creation of units (recruits, recalls, and placed units)...
static lg::log_domain log_engine("engine")
recall_action(const unit_const_ptr recalled, const map_location &loc, const map_location &from, int orig_village_owner, bool time_bonus)
config & add_child(config_key_type key)
Definition: config.cpp:456
virtual void write(config &cfg) const
Writes this into the provided config.
size_t erase(const map_location &l)
Erases the unit at location l, if any.
Definition: map.cpp:298
boost::intrusive_ptr< unit > unit_ptr
Definition: ptr.hpp:29
Standard logging facilities (interface).
recall_list_manager & recall_list()
Definition: team.hpp:214
void add(const unit_ptr &ptr)
Add a unit to the list.
Container associating units to locations.
Definition: map.hpp:99
route_t route
The hexes occupied by the affected unit during this action.
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
unit_map * units
Definition: resources.cpp:34
actions that are undoable (this does not include update_shroud and auto_shroud)
Definition: undo_action.hpp:60