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 "statistics.hpp"
24 #include "log.hpp"
25 #include "game_display.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  * Writes this into the provided config.
38  */
39 void recall_action::write(config & cfg) const
40 {
41  undo_action::write(cfg);
43 
44  recall_from.write(cfg.add_child("leader"));
45  cfg["id"] = id;
46 }
47 
48 /**
49  * Undoes this action.
50  * @return true on success; false on an error.
51  */
52 bool recall_action::undo(int side)
53 {
56  team &current_team = resources::gameboard->get_team(side);
57 
58  const map_location & recall_loc = route.front();
59  unit_map::iterator un_it = units.find(recall_loc);
60  if ( un_it == units.end() ) {
61  return false;
62  }
63 
64  unit_ptr un = un_it.get_shared_ptr();
65  if (!un) {
66  return false;
67  }
68 
70  int cost = statistics::un_recall_unit_cost(*un);
71  if (cost < 0) {
72  current_team.spend_gold(-current_team.recall_cost());
73  }
74  else {
75  current_team.spend_gold(-cost);
76  }
77 
78  current_team.recall_list().add(un);
79  // invalidate before erasing allow us
80  // to also do the overlapped hexes
81  gui.invalidate(recall_loc);
82  units.erase(recall_loc);
83  this->return_village();
85  return true;
86 }
87 
88 }
89 }
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:2981
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")
config & add_child(config_key_type key)
Definition: config.cpp:408
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