The Battle for Wesnoth  1.19.5+dev
undo_recall_action.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2024
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 
17 #include "game_board.hpp"
18 #include "play_controller.hpp"
19 #include "resources.hpp"
20 #include "team.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 #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::undo
34 {
36  const map_location& from, int orig_village_owner, bool time_bonus)
37  : undo_action()
38  , shroud_clearing_action(recalled, loc, orig_village_owner, time_bonus)
39  , id(recalled->id())
40  , recall_from(from)
41 {}
42 
44  : undo_action(cfg)
46  , id(cfg["id"])
47  , recall_from(from)
48 {}
49 
50 /**
51  * Writes this into the provided config.
52  */
53 void recall_action::write(config & cfg) const
54 {
55  undo_action::write(cfg);
57 
58  recall_from.write(cfg.add_child("leader"));
59  cfg["id"] = id;
60 }
61 
62 /**
63  * Undoes this action.
64  * @return true on success; false on an error.
65  */
66 bool recall_action::undo(int side)
67 {
69  unit_map & units = resources::gameboard->units();
70  team &current_team = resources::gameboard->get_team(side);
71 
72  const map_location & recall_loc = route.front();
73  unit_map::iterator un_it = units.find(recall_loc);
74  if ( un_it == units.end() ) {
75  return false;
76  }
77 
78  unit_ptr un = un_it.get_shared_ptr();
79  if (!un) {
80  return false;
81  }
82 
84  int cost = un->recall_cost();
85  if (cost < 0) {
86  current_team.spend_gold(-current_team.recall_cost());
87  }
88  else {
89  current_team.spend_gold(-cost);
90  }
91 
92  current_team.recall_list().add(un);
93  // Invalidate everything, not just recall_loc, in case the sprite
94  // extends into adjacent hexes (Horseman) or even farther away (Fire
95  // Dragon)
96  gui.invalidate_all();
97  units.erase(recall_loc);
98  resources::whiteboard->on_kill_unit();
99  un->anim_comp().clear_haloes();
100  this->return_village();
102  return true;
103 }
104 
105 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
config & add_child(config_key_type key)
Definition: config.cpp:440
team & get_team(int i)
Definition: game_board.hpp:92
virtual const unit_map & units() const override
Definition: game_board.hpp:107
static game_display * get_singleton()
statistics_t & statistics()
void add(const unit_ptr &ptr, int pos=-1)
Add a unit to the list.
void un_recall_unit(const unit &u)
Definition: statistics.cpp:187
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:75
int recall_cost() const
Definition: team.hpp:180
void spend_gold(const int amount)
Definition: team.hpp:195
recall_list_manager & recall_list()
Definition: team.hpp:201
Container associating units to locations.
Definition: map.hpp:98
unit_iterator end()
Definition: map.hpp:428
unit_iterator find(std::size_t id)
Definition: map.cpp:302
std::size_t erase(const map_location &l)
Erases the unit at location l, if any.
Definition: map.cpp:289
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:198
Standard logging facilities (interface).
General purpose widgets.
game_board * gameboard
Definition: resources.cpp:20
play_controller * controller
Definition: resources.cpp:21
std::shared_ptr< wb::manager > whiteboard
Definition: resources.cpp:33
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
std::shared_ptr< unit > unit_ptr
Definition: ptr.hpp:26
base class for classes that clear srhoud (move/recruit/recall)
route_t route
The hexes occupied by the affected unit during this action.
void return_village()
Change village owner on undo.
virtual bool undo(int side)
Undoes this action.
virtual void write(config &cfg) const
Writes this into the provided config.
recall_action(const unit_const_ptr recalled, const map_location &loc, const map_location &from, int orig_village_owner, bool time_bonus)
actions that are undoable (this does not include update_shroud and auto_shroud)
Definition: undo_action.hpp:66
virtual void write(config &cfg) const
Writes this into the provided config.
Encapsulates the map of the game.
Definition: location.hpp:45
void write(config &cfg) const
Definition: location.cpp:225
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:217
static lg::log_domain log_engine("engine")