The Battle for Wesnoth  1.17.0-dev
undo_recruit_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"
23 #include "units/map.hpp"
24 #include "units/unit.hpp"
25 #include "units/types.hpp"
26 #include "statistics.hpp"
27 #include "log.hpp"
28 #include "game_display.hpp"
29 
30 static lg::log_domain log_engine("engine");
31 #define ERR_NG LOG_STREAM(err, log_engine)
32 #define LOG_NG LOG_STREAM(info, log_engine)
33 
34 namespace actions
35 {
36 namespace undo
37 {
38 
40  const map_location& from, int orig_village_owner, bool time_bonus)
41  : undo_action()
42  , shroud_clearing_action(recruited, loc, orig_village_owner, time_bonus)
43  , u_type(recruited->type())
44  , recruit_from(from)
45 {}
46 
48  : undo_action(cfg)
50  , u_type(type)
51  , recruit_from(from)
52 {}
53 
54 /**
55  * Writes this into the provided config.
56  */
57 void recruit_action::write(config & cfg) const
58 {
59  undo_action::write(cfg);
61 
62  recruit_from.write(cfg.add_child("leader"));
63  config & child = cfg.child("unit");
64  child["type"] = u_type.parent_id();
65 }
66 
67 /**
68  * Undoes this action.
69  * @return true on success; false on an error.
70  */
71 bool recruit_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 & recruit_loc = route.front();
78  unit_map::iterator un_it = units.find(recruit_loc);
79  if ( un_it == units.end() ) {
80  return false;
81  }
82 
83  const unit &un = *un_it;
85  current_team.spend_gold(-un.type().cost());
86 
87  //MP_COUNTDOWN take away recruit bonus
88  current_team.set_action_bonus_count(current_team.action_bonus_count() - 1);
89 
90  // invalidate before erasing allow us
91  // to also do the overlapped hexes
92  gui.invalidate(recruit_loc);
93  units.erase(recruit_loc);
94  this->return_village();
96  return true;
97 }
98 
99 }
100 }
base class for classes that clear srhoud (move/recruit/recall)
const std::string & parent_id() const
The id of the original type from which this (variation) descended.
Definition: types.hpp:148
config & child(config_key_type key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:402
unit_iterator end()
Definition: map.hpp:429
virtual bool undo(int side)
Undoes this action.
virtual const unit_map & units() const override
Definition: game_board.hpp:112
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:3026
This class represents a single unit of a specific type.
Definition: unit.hpp:121
General purpose widgets.
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.
recruit_action(const unit_const_ptr recruited, const map_location &loc, const map_location &from, int orig_village_owner, bool time_bonus)
A single unit type that the player may recruit.
Definition: types.hpp:45
const unit_type & type() const
This unit's type, accounting for gender and variation.
Definition: unit.hpp:346
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
void return_village()
Change village owner on undo.
int action_bonus_count() const
Definition: team.hpp:225
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 cost() const
Definition: types.hpp:175
game_board * gameboard
Definition: resources.cpp:21
void spend_gold(const int amount)
Definition: team.hpp:220
Encapsulates the map of the game.
Definition: location.hpp:38
unit_iterator find(std::size_t id)
Definition: map.cpp:310
Various functions related to the creation of units (recruits, recalls, and placed units)...
config & add_child(config_key_type key)
Definition: config.cpp:514
virtual void write(config &cfg) const
Writes this into the provided config.
void set_action_bonus_count(const int count)
Definition: team.hpp:226
Standard logging facilities (interface).
Container associating units to locations.
Definition: map.hpp:98
route_t route
The hexes occupied by the affected unit during this action.
static lg::log_domain log_engine("engine")
void un_recruit_unit(const unit &u)
Definition: statistics.cpp:668
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