The Battle for Wesnoth  1.17.23+dev
undo_recruit_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"
24 #include "units/map.hpp"
25 #include "units/unit.hpp"
26 #include "units/types.hpp"
27 #include "statistics.hpp"
28 #include "log.hpp"
29 #include "game_display.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(recruited, loc, orig_village_owner, time_bonus)
44  , u_type(recruited->type())
45  , recruit_from(from)
46 {}
47 
49  : undo_action(cfg)
51  , u_type(type)
52  , recruit_from(from)
53 {}
54 
55 /**
56  * Writes this into the provided config.
57  */
58 void recruit_action::write(config & cfg) const
59 {
60  undo_action::write(cfg);
62 
63  recruit_from.write(cfg.add_child("leader"));
64  config & child = cfg.mandatory_child("unit");
65  child["type"] = u_type.parent_id();
66 }
67 
68 /**
69  * Undoes this action.
70  * @return true on success; false on an error.
71  */
72 bool recruit_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 & recruit_loc = route.front();
79  unit_map::iterator un_it = units.find(recruit_loc);
80  if ( un_it == units.end() ) {
81  return false;
82  }
83 
84  const unit &un = *un_it;
86  current_team.spend_gold(-un.type().cost());
87 
88  //MP_COUNTDOWN take away recruit bonus
89  current_team.set_action_bonus_count(current_team.action_bonus_count() - 1);
90 
91  // invalidate before erasing allow us
92  // to also do the overlapped hexes
93  gui.invalidate(recruit_loc);
94  units.erase(recruit_loc);
95  this->return_village();
97  return true;
98 }
99 
100 }
101 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
config & mandatory_child(config_key_type key, int n=0)
Returns the nth child with the given key, or throws an error if there is none.
Definition: config.cpp:371
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 un_recruit_unit(const unit &u)
Definition: statistics.cpp:194
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:76
void set_action_bonus_count(const int count)
Definition: team.hpp:202
int action_bonus_count() const
Definition: team.hpp:201
void spend_gold(const int amount)
Definition: team.hpp:196
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
A single unit type that the player may recruit.
Definition: types.hpp:46
const std::string & parent_id() const
The id of the original type from which this (variation) descended.
Definition: types.hpp:148
int cost() const
Definition: types.hpp:175
This class represents a single unit of a specific type.
Definition: unit.hpp:135
Various functions related to the creation of units (recruits, recalls, and placed units).
const unit_type & type() const
This unit's type, accounting for gender and variation.
Definition: unit.hpp:357
Standard logging facilities (interface).
General purpose widgets.
game_board * gameboard
Definition: resources.cpp:21
play_controller * controller
Definition: resources.cpp:22
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
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 void write(config &cfg) const
Writes this into the provided config.
virtual bool undo(int side)
Undoes this action.
recruit_action(const unit_const_ptr recruited, 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:70
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
static lg::log_domain log_engine("engine")