The Battle for Wesnoth  1.15.0-dev
undo_recall_action.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017-2018 the Battle for Wesnoth Project https://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"
23 #include "units/map.hpp"
24 #include "units/unit.hpp"
25 #include "statistics.hpp"
26 #include "log.hpp"
27 #include "game_display.hpp"
28 #include "whiteboard/manager.hpp"
29 
30 static lg::log_domain log_engine("engine");
31 #define ERR_NG LOG_STREAM(err, log_engine)
32 #define LOG_NG LOG_STREAM(info, log_engine)
33 
34 namespace actions
35 {
36 namespace undo
37 {
38 
40  const map_location& from, int orig_village_owner, bool time_bonus)
41  : undo_action()
42  , shroud_clearing_action(recalled, loc, orig_village_owner, time_bonus)
43  , id(recalled->id())
44  , recall_from(from)
45 {}
46 
48  : undo_action(cfg)
50  , id(cfg["id"])
51  , recall_from(from)
52 {}
53 
54 /**
55  * Writes this into the provided config.
56  */
57 void recall_action::write(config & cfg) const
58 {
59  undo_action::write(cfg);
61 
62  recall_from.write(cfg.add_child("leader"));
63  cfg["id"] = id;
64 }
65 
66 /**
67  * Undoes this action.
68  * @return true on success; false on an error.
69  */
70 bool recall_action::undo(int side)
71 {
73  unit_map & units = resources::gameboard->units();
74  team &current_team = resources::gameboard->get_team(side);
75 
76  const map_location & recall_loc = route.front();
77  unit_map::iterator un_it = units.find(recall_loc);
78  if ( un_it == units.end() ) {
79  return false;
80  }
81 
82  unit_ptr un = un_it.get_shared_ptr();
83  if (!un) {
84  return false;
85  }
86 
88  int cost = statistics::un_recall_unit_cost(*un);
89  if (cost < 0) {
90  current_team.spend_gold(-current_team.recall_cost());
91  }
92  else {
93  current_team.spend_gold(-cost);
94  }
95 
96  current_team.recall_list().add(un);
97  // invalidate before erasing allow us
98  // to also do the overlapped hexes
99  gui.invalidate(recall_loc);
100  units.erase(recall_loc);
101  resources::whiteboard->on_kill_unit();
102  un->anim_comp().clear_haloes();
103  this->return_village();
105  return true;
106 }
107 
108 }
109 }
virtual void write(config &cfg) const
Writes this into the provided config.
boost::intrusive_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:30
base class for classes that clear srhoud (move/recruit/recall)
unit_iterator end()
Definition: map.hpp:415
virtual const unit_map & units() const override
Definition: game_board.hpp:114
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:2979
void un_recall_unit(const unit &u)
Definition: statistics.cpp:520
General purpose widgets.
virtual bool undo(int side)
Undoes this action.
Replay control code.
std::size_t erase(const map_location &l)
Erases the unit at location l, if any.
Definition: map.cpp:298
virtual void write(config &cfg) const
Writes this into the provided config.
-file sdl_utils.hpp
void return_village()
Change village owner on undo.
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:44
team & get_team(int i)
Definition: game_board.hpp:104
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:208
int recall_cost() const
Definition: team.hpp:193
Encapsulates the map of the game.
Definition: location.hpp:42
unit_iterator find(std::size_t id)
Definition: map.cpp:311
std::shared_ptr< wb::manager > whiteboard
Definition: resources.cpp:33
Various functions related to the creation of units (recruits, recalls, and placed units)...
pointer get_shared_ptr() const
This is exactly the same as operator-> but it&#39;s slightly more readable, and can replace &*iter syntax...
Definition: map.hpp:220
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:480
boost::intrusive_ptr< unit > unit_ptr
Definition: ptr.hpp:29
void add(const unit_ptr &ptr, int pos=-1)
Add a unit to the list.
Standard logging facilities (interface).
recall_list_manager & recall_list()
Definition: team.hpp:215
Container associating units to locations.
Definition: map.hpp:99
route_t route
The hexes occupied by the affected unit during this action.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
actions that are undoable (this does not include update_shroud and auto_shroud)
Definition: undo_action.hpp:60
static game_display * get_singleton()
void write(config &cfg) const
Definition: location.cpp:210