The Battle for Wesnoth  1.15.0-dev
undo_recruit_action.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017-2018 the Battle for Wesnoth Project http://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
15 #include "actions/create.hpp"
16 
18 #include "game_board.hpp"
19 #include "resources.hpp"
20 #include "team.hpp"
21 #include "replay.hpp"
22 #include "units/map.hpp"
23 #include "units/unit.hpp"
24 #include "statistics.hpp"
25 #include "log.hpp"
26 
27 static lg::log_domain log_engine("engine");
28 #define ERR_NG LOG_STREAM(err, log_engine)
29 #define LOG_NG LOG_STREAM(info, log_engine)
30 
31 namespace actions
32 {
33 namespace undo
34 {
35 
37  const map_location& from, int orig_village_owner, bool time_bonus)
38  : undo_action()
39  , shroud_clearing_action(recruited, loc, orig_village_owner, time_bonus)
40  , u_type(recruited->type())
41  , recruit_from(from)
42 {}
43 
45  : undo_action(cfg)
47  , u_type(type)
48  , recruit_from(from)
49 {}
50 
51 /**
52  * Writes this into the provided config.
53  */
54 void recruit_action::write(config & cfg) const
55 {
56  undo_action::write(cfg);
58 
59  recruit_from.write(cfg.add_child("leader"));
60  config & child = cfg.child("unit");
61  child["type"] = u_type.base_id();
62 }
63 
64 /**
65  * Undoes this action.
66  * @return true on success; false on an error.
67  */
68 bool recruit_action::undo(int side)
69 {
70  unit_map & units = resources::gameboard->units();
71  team &current_team = resources::gameboard->get_team(side);
72 
73  const map_location & recruit_loc = route.front();
74  unit_map::iterator un_it = units.find(recruit_loc);
75  if ( un_it == units.end() ) {
76  return false;
77  }
78 
79  const unit &un = *un_it;
81  current_team.spend_gold(-un.type().cost());
82 
83  //MP_COUNTDOWN take away recruit bonus
84  current_team.set_action_bonus_count(current_team.action_bonus_count() - 1);
85 
86  units.erase(recruit_loc);
87  this->return_village();
89  return true;
90 }
91 
92 }
93 }
boost::intrusive_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:30
base class for classes that clear srhoud (move/recruit/recall)
const std::string & base_id() const
The id of the original type from which this (variation) descended.
Definition: types.hpp:143
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:423
unit_iterator end()
Definition: map.hpp:415
virtual bool undo(int side)
Undoes this action.
virtual const unit_map & units() const override
Definition: game_board.hpp:114
This class represents a single unit of a specific type.
Definition: unit.hpp:99
Replay control code.
std::size_t erase(const map_location &l)
Erases the unit at location l, if any.
Definition: map.cpp:298
virtual void write(config &cfg) const
Writes this into the provided config.
-file sdl_utils.hpp
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:42
const unit_type & type() const
This unit&#39;s type, accounting for gender and variation.
Definition: unit.hpp:277
void return_village()
Change village owner on undo.
int action_bonus_count() const
Definition: team.hpp:212
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:44
team & get_team(int i)
Definition: game_board.hpp:104
int cost() const
Definition: types.hpp:158
game_board * gameboard
Definition: resources.cpp:20
void spend_gold(const int amount)
Definition: team.hpp:207
Encapsulates the map of the game.
Definition: location.hpp:42
unit_iterator find(std::size_t id)
Definition: map.cpp:311
Various functions related to the creation of units (recruits, recalls, and placed units)...
config & add_child(config_key_type key)
Definition: config.cpp:479
virtual void write(config &cfg) const
Writes this into the provided config.
void set_action_bonus_count(const int count)
Definition: team.hpp:213
Standard logging facilities (interface).
Container associating units to locations.
Definition: map.hpp:99
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:527
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
actions that are undoable (this does not include update_shroud and auto_shroud)
Definition: undo_action.hpp:60
void write(config &cfg) const
Definition: location.cpp:210