The Battle for Wesnoth  1.17.0-dev
undo_recall_action.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2021
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
16 #include "actions/create.hpp"
17 
19 #include "game_board.hpp"
20 #include "resources.hpp"
21 #include "team.hpp"
22 #include "replay.hpp"
24 #include "units/map.hpp"
25 #include "units/unit.hpp"
26 #include "statistics.hpp"
27 #include "log.hpp"
28 #include "game_display.hpp"
29 #include "whiteboard/manager.hpp"
30 
31 static lg::log_domain log_engine("engine");
32 #define ERR_NG LOG_STREAM(err, log_engine)
33 #define LOG_NG LOG_STREAM(info, log_engine)
34 
35 namespace actions
36 {
37 namespace undo
38 {
39 
41  const map_location& from, int orig_village_owner, bool time_bonus)
42  : undo_action()
43  , shroud_clearing_action(recalled, loc, orig_village_owner, time_bonus)
44  , id(recalled->id())
45  , recall_from(from)
46 {}
47 
49  : undo_action(cfg)
51  , id(cfg["id"])
52  , recall_from(from)
53 {}
54 
55 /**
56  * Writes this into the provided config.
57  */
58 void recall_action::write(config & cfg) const
59 {
60  undo_action::write(cfg);
62 
63  recall_from.write(cfg.add_child("leader"));
64  cfg["id"] = id;
65 }
66 
67 /**
68  * Undoes this action.
69  * @return true on success; false on an error.
70  */
71 bool recall_action::undo(int side)
72 {
74  unit_map & units = resources::gameboard->units();
75  team &current_team = resources::gameboard->get_team(side);
76 
77  const map_location & recall_loc = route.front();
78  unit_map::iterator un_it = units.find(recall_loc);
79  if ( un_it == units.end() ) {
80  return false;
81  }
82 
83  unit_ptr un = un_it.get_shared_ptr();
84  if (!un) {
85  return false;
86  }
87 
89  int cost = statistics::un_recall_unit_cost(*un);
90  if (cost < 0) {
91  current_team.spend_gold(-current_team.recall_cost());
92  }
93  else {
94  current_team.spend_gold(-cost);
95  }
96 
97  current_team.recall_list().add(un);
98  // Invalidate everything, not just recall_loc, in case the sprite
99  // extends into adjacent hexes (Horseman) or even farther away (Fire
100  // Dragon)
101  gui.invalidate_all();
102  units.erase(recall_loc);
103  resources::whiteboard->on_kill_unit();
104  un->anim_comp().clear_haloes();
105  this->return_village();
107  return true;
108 }
109 
110 }
111 }
virtual void write(config &cfg) const
Writes this into the provided config.
base class for classes that clear srhoud (move/recruit/recall)
unit_iterator end()
Definition: map.hpp:429
virtual const unit_map & units() const override
Definition: game_board.hpp:112
void un_recall_unit(const unit &u)
Definition: statistics.cpp:661
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:297
virtual void write(config &cfg) const
Writes this into the provided config.
std::shared_ptr< unit > unit_ptr
Definition: ptr.hpp:26
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
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:72
team & get_team(int i)
Definition: game_board.hpp:97
int un_recall_unit_cost(const unit &u)
Definition: statistics.cpp:675
game_board * gameboard
Definition: resources.cpp:21
void spend_gold(const int amount)
Definition: team.hpp:220
void invalidate_all()
Function to invalidate all tiles.
Definition: display.cpp:3019
int recall_cost() const
Definition: team.hpp:205
Encapsulates the map of the game.
Definition: location.hpp:38
unit_iterator find(std::size_t id)
Definition: map.cpp:310
std::shared_ptr< wb::manager > whiteboard
Definition: resources.cpp:34
Various functions related to the creation of units (recruits, recalls, and placed units)...
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:215
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:218
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:514
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:227
Container associating units to locations.
Definition: map.hpp:98
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:61
actions that are undoable (this does not include update_shroud and auto_shroud)
Definition: undo_action.hpp:66
static game_display * get_singleton()
void write(config &cfg) const
Definition: location.cpp:212