The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
undo_recruit_action.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 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 #include "game_display.hpp"
27 
28 static lg::log_domain log_engine("engine");
29 #define ERR_NG LOG_STREAM(err, log_engine)
30 #define LOG_NG LOG_STREAM(info, log_engine)
31 
32 namespace actions
33 {
34 namespace undo
35 {
36 
38  const map_location& from, int orig_village_owner, bool time_bonus)
39  : undo_action()
40  , shroud_clearing_action(recruited, loc, orig_village_owner, time_bonus)
41  , u_type(recruited->type())
42  , recruit_from(from)
43 {}
44 
46  : undo_action(cfg)
48  , u_type(type)
49  , recruit_from(from)
50 {}
51 
52 /**
53  * Writes this into the provided config.
54  */
55 void recruit_action::write(config & cfg) const
56 {
57  undo_action::write(cfg);
59 
60  recruit_from.write(cfg.add_child("leader"));
61  config & child = cfg.child("unit");
62  child["type"] = u_type.base_id();
63 }
64 
65 /**
66  * Undoes this action.
67  * @return true on success; false on an error.
68  */
69 bool recruit_action::undo(int side)
70 {
73  team &current_team = resources::gameboard->get_team(side);
74 
75  const map_location & recruit_loc = route.front();
76  unit_map::iterator un_it = units.find(recruit_loc);
77  if ( un_it == units.end() ) {
78  return false;
79  }
80 
81  const unit &un = *un_it;
83  current_team.spend_gold(-un.type().cost());
84 
85  //MP_COUNTDOWN take away recruit bonus
86  current_team.set_action_bonus_count(current_team.action_bonus_count() - 1);
87 
88  // invalidate before erasing allow us
89  // to also do the overlapped hexes
90  gui.invalidate(recruit_loc);
91  units.erase(recruit_loc);
92  this->return_village();
94  return true;
95 }
96 
97 }
98 }
boost::intrusive_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:30
base class for classes that clear srhoud (move/recruit/recall)
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:400
virtual const unit_map & units() const
Definition: game_board.hpp:97
unit_iterator end()
Definition: map.hpp:415
virtual bool undo(int side)
Undoes this action.
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:3023
This class represents a single unit of a specific type.
Definition: unit.hpp:100
game_display * screen
Definition: resources.cpp:27
General purpose widgets.
Replay control code.
-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:43
void return_village()
Change village owner on undo.
virtual void write(config &cfg) const
Writes this into the provided config.
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:44
team & get_team(int i)
Definition: game_board.hpp:94
game_board * gameboard
Definition: resources.cpp:20
void spend_gold(const int amount)
Definition: team.hpp:207
void write(config &cfg) const
Definition: location.cpp:210
int cost() const
Definition: types.hpp:159
Encapsulates the map of the game.
Definition: location.hpp:40
virtual void write(config &cfg) const
Writes this into the provided config.
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:256
config & add_child(config_key_type key)
Definition: config.cpp:456
size_t erase(const map_location &l)
Erases the unit at location l, if any.
Definition: map.cpp:298
const std::string & base_id() const
The id of the original type from which this (variation) descended.
Definition: types.hpp:144
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.
unit_iterator find(size_t id)
Definition: map.cpp:311
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:93
int action_bonus_count() const
Definition: team.hpp:212
unit_map * units
Definition: resources.cpp:34
actions that are undoable (this does not include update_shroud and auto_shroud)
Definition: undo_action.hpp:60