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 "whiteboard/manager.hpp"
28 
29 static lg::log_domain log_engine("engine");
30 #define ERR_NG LOG_STREAM(err, log_engine)
31 #define LOG_NG LOG_STREAM(info, log_engine)
32 
33 namespace actions
34 {
35 namespace undo
36 {
37 
39  const map_location& from, int orig_village_owner, bool time_bonus)
40  : undo_action()
41  , shroud_clearing_action(recalled, loc, orig_village_owner, time_bonus)
42  , id(recalled->id())
43  , recall_from(from)
44 {}
45 
47  : undo_action(cfg)
49  , id(cfg["id"])
50  , recall_from(from)
51 {}
52 
53 /**
54  * Writes this into the provided config.
55  */
56 void recall_action::write(config & cfg) const
57 {
58  undo_action::write(cfg);
60 
61  recall_from.write(cfg.add_child("leader"));
62  cfg["id"] = id;
63 }
64 
65 /**
66  * Undoes this action.
67  * @return true on success; false on an error.
68  */
69 bool recall_action::undo(int side)
70 {
71  unit_map & units = resources::gameboard->units();
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 
96  units.erase(recall_loc);
97  resources::whiteboard->on_kill_unit();
98  un->anim_comp().clear_haloes();
99  this->return_village();
101  return true;
102 }
103 
104 }
105 }
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
void un_recall_unit(const unit &u)
Definition: statistics.cpp:520
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
const t_string id
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:479
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:68
actions that are undoable (this does not include update_shroud and auto_shroud)
Definition: undo_action.hpp:60
void write(config &cfg) const
Definition: location.cpp:210