The Battle for Wesnoth  1.15.4+dev
undo_recruit_action.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017-2018 the Battle for Wesnoth Project https://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 "units/types.hpp"
25 #include "statistics.hpp"
26 #include "log.hpp"
27 #include "game_display.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(recruited, loc, orig_village_owner, time_bonus)
42  , u_type(recruited->type())
43  , recruit_from(from)
44 {}
45 
47  : undo_action(cfg)
49  , u_type(type)
50  , recruit_from(from)
51 {}
52 
53 /**
54  * Writes this into the provided config.
55  */
56 void recruit_action::write(config & cfg) const
57 {
58  undo_action::write(cfg);
60 
61  recruit_from.write(cfg.add_child("leader"));
62  config & child = cfg.child("unit");
63  child["type"] = u_type.parent_id();
64 }
65 
66 /**
67  * Undoes this action.
68  * @return true on success; false on an error.
69  */
70 bool recruit_action::undo(int side)
71 {
73  unit_map & units = resources::gameboard->units();
74  team &current_team = resources::gameboard->get_team(side);
75 
76  const map_location & recruit_loc = route.front();
77  unit_map::iterator un_it = units.find(recruit_loc);
78  if ( un_it == units.end() ) {
79  return false;
80  }
81 
82  const unit &un = *un_it;
84  current_team.spend_gold(-un.type().cost());
85 
86  //MP_COUNTDOWN take away recruit bonus
87  current_team.set_action_bonus_count(current_team.action_bonus_count() - 1);
88 
89  // invalidate before erasing allow us
90  // to also do the overlapped hexes
91  gui.invalidate(recruit_loc);
92  units.erase(recruit_loc);
93  this->return_village();
95  return true;
96 }
97 
98 }
99 }
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: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:420
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:114
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:3025
This class represents a single unit of a specific type.
Definition: unit.hpp:129
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: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:44
const unit_type & type() const
This unit's type, accounting for gender and variation.
Definition: unit.hpp:353
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:29
void return_village()
Change village owner on undo.
int action_bonus_count() const
Definition: team.hpp:213
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:162
game_board * gameboard
Definition: resources.cpp:20
void spend_gold(const int amount)
Definition: team.hpp:208
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:476
virtual void write(config &cfg) const
Writes this into the provided config.
void set_action_bonus_count(const int count)
Definition: team.hpp:214
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:665
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
static game_display * get_singleton()
void write(config &cfg) const
Definition: location.cpp:211