The Battle for Wesnoth  1.19.0-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
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 {
72  unit_map & units = resources::gameboard->units();
73  team &current_team = resources::gameboard->get_team(side);
74 
75  const map_location & recall_loc = route.front();
76  unit_map::iterator un_it = units.find(recall_loc);
77  if ( un_it == units.end() ) {
78  return false;
79  }
80 
81  unit_ptr un = un_it.get_shared_ptr();
82  if (!un) {
83  return false;
84  }
85 
87  int cost = un->recall_cost();
88  if (cost < 0) {
89  current_team.spend_gold(-current_team.recall_cost());
90  }
91  else {
92  current_team.spend_gold(-cost);
93  }
94 
95  current_team.recall_list().add(un);
96  // Invalidate everything, not just recall_loc, in case the sprite
97  // extends into adjacent hexes (Horseman) or even farther away (Fire
98  // Dragon)
99  gui.invalidate_all();
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 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
config & add_child(config_key_type key)
Definition: config.cpp:441
team & get_team(int i)
Definition: game_board.hpp:91
virtual const unit_map & units() const override
Definition: game_board.hpp:106
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:74
int recall_cost() const
Definition: team.hpp:179
void spend_gold(const int amount)
Definition: team.hpp:194
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:207
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:38
void write(config &cfg) const
Definition: location.cpp:211
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")