The Battle for Wesnoth  1.17.17+dev
undo_recall_action.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2023
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 "play_controller.hpp"
21 #include "resources.hpp"
22 #include "team.hpp"
23 #include "replay.hpp"
25 #include "units/map.hpp"
26 #include "units/unit.hpp"
27 #include "statistics.hpp"
28 #include "log.hpp"
29 #include "game_display.hpp"
30 #include "whiteboard/manager.hpp"
31 
32 static lg::log_domain log_engine("engine");
33 #define ERR_NG LOG_STREAM(err, log_engine)
34 #define LOG_NG LOG_STREAM(info, log_engine)
35 
36 namespace actions
37 {
38 namespace undo
39 {
40 
42  const map_location& from, int orig_village_owner, bool time_bonus)
43  : undo_action()
44  , shroud_clearing_action(recalled, loc, orig_village_owner, time_bonus)
45  , id(recalled->id())
46  , recall_from(from)
47 {}
48 
50  : undo_action(cfg)
52  , id(cfg["id"])
53  , recall_from(from)
54 {}
55 
56 /**
57  * Writes this into the provided config.
58  */
59 void recall_action::write(config & cfg) const
60 {
61  undo_action::write(cfg);
63 
64  recall_from.write(cfg.add_child("leader"));
65  cfg["id"] = id;
66 }
67 
68 /**
69  * Undoes this action.
70  * @return true on success; false on an error.
71  */
72 bool recall_action::undo(int side)
73 {
75  unit_map & units = resources::gameboard->units();
76  team &current_team = resources::gameboard->get_team(side);
77 
78  const map_location & recall_loc = route.front();
79  unit_map::iterator un_it = units.find(recall_loc);
80  if ( un_it == units.end() ) {
81  return false;
82  }
83 
84  unit_ptr un = un_it.get_shared_ptr();
85  if (!un) {
86  return false;
87  }
88 
90  int cost = un->recall_cost();
91  if (cost < 0) {
92  current_team.spend_gold(-current_team.recall_cost());
93  }
94  else {
95  current_team.spend_gold(-cost);
96  }
97 
98  current_team.recall_list().add(un);
99  // Invalidate everything, not just recall_loc, in case the sprite
100  // extends into adjacent hexes (Horseman) or even farther away (Fire
101  // Dragon)
102  gui.invalidate_all();
103  units.erase(recall_loc);
104  resources::whiteboard->on_kill_unit();
105  un->anim_comp().clear_haloes();
106  this->return_village();
108  return true;
109 }
110 
111 }
112 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
config & add_child(config_key_type key)
Definition: config.cpp:445
team & get_team(int i)
Definition: game_board.hpp:98
virtual const unit_map & units() const override
Definition: game_board.hpp:113
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:76
int recall_cost() const
Definition: team.hpp:181
void spend_gold(const int amount)
Definition: team.hpp:196
recall_list_manager & recall_list()
Definition: team.hpp:203
Container associating units to locations.
Definition: map.hpp:99
unit_iterator end()
Definition: map.hpp:429
unit_iterator find(std::size_t id)
Definition: map.cpp:301
std::size_t erase(const map_location &l)
Erases the unit at location l, if any.
Definition: map.cpp:288
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
Standard logging facilities (interface).
General purpose widgets.
game_board * gameboard
Definition: resources.cpp:21
play_controller * controller
Definition: resources.cpp:22
std::shared_ptr< wb::manager > whiteboard
Definition: resources.cpp:34
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
std::shared_ptr< unit > unit_ptr
Definition: ptr.hpp:26
Replay control code.
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:67
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:212
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:218
static lg::log_domain log_engine("engine")