The Battle for Wesnoth  1.13.4+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.
2 #include "create.hpp"
3 
5 #include "resources.hpp"
6 #include "team.hpp"
7 #include "replay.hpp"
8 #include "units/map.hpp"
9 #include "statistics.hpp"
10 #include "log.hpp"
11 #include "game_display.hpp"
12 
13 static lg::log_domain log_engine("engine");
14 #define ERR_NG LOG_STREAM(err, log_engine)
15 #define LOG_NG LOG_STREAM(info, log_engine)
16 
17 namespace actions
18 {
19 namespace undo
20 {
21 
22 /**
23  * Writes this into the provided config.
24  */
25 void recruit_action::write(config & cfg) const
26 {
27  undo_action::write(cfg);
29 
30  recruit_from.write(cfg.add_child("leader"));
31  config & child = cfg.child("unit");
32  child["type"] = u_type.base_id();
33 }
34 
35 /**
36  * Undoes this action.
37  * @return true on success; false on an error.
38  */
39 bool recruit_action::undo(int side)
40 {
43  team &current_team = (*resources::teams)[side-1];
44 
45  const map_location & recruit_loc = route.front();
46  unit_map::iterator un_it = units.find(recruit_loc);
47  if ( un_it == units.end() ) {
48  return false;
49  }
50 
51  const unit &un = *un_it;
53  current_team.spend_gold(-un.type().cost());
54 
55  //MP_COUNTDOWN take away recruit bonus
56  current_team.set_action_bonus_count(current_team.action_bonus_count() - 1);
57 
58  // invalidate before erasing allow us
59  // to also do the overlapped hexes
60  gui.invalidate(recruit_loc);
61  units.erase(recruit_loc);
62  this->return_village();
64  return true;
65 }
66 
67 /**
68  * Redoes this action.
69  * @return true on success; false on an error.
70  */
71 bool recruit_action::redo(int side)
72 {
74  team &current_team = (*resources::teams)[side-1];
75 
76  map_location loc = route.front();
78  const std::string & name = u_type.base_id();
79 
80  //search for the unit to be recruited in recruits
81  if ( !util::contains(get_recruits(side, loc), name) ) {
82  ERR_NG << "Trying to redo a recruit for side " << side
83  << ", which does not recruit type \"" << name << "\"\n";
84  assert(false);
85  return false;
86  }
87 
88  current_team.last_recruit(name);
89  const std::string &msg = find_recruit_location(side, loc, from, name);
90  if ( msg.empty() ) {
91  //MP_COUNTDOWN: restore recruitment bonus
92  current_team.set_action_bonus_count(1 + current_team.action_bonus_count());
96  recruit_unit(u_type, side, loc, from, true, false);
97 
98  // Quick error check. (Abuse of [allow_undo]?)
99  if ( loc != route.front() ) {
100  ERR_NG << "When redoing a recruit at " << route.front()
101  << ", the location was moved to " << loc << ".\n";
102  // Not really fatal, I suppose. Just update the action so
103  // undoing this works.
104  route.front() = loc;
105  }
106  sync.do_final_checkup();
107  } else {
109  return false;
110  }
111 
112  return true;
113 }
114 }
115 }
unit_iterator end()
Definition: map.hpp:311
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:3536
Definition: unit.hpp:95
void do_final_checkup(bool dont_throw=false)
bool contains(const Container &container, const Value &value)
Returns true iff value is found in container.
Definition: util.hpp:489
game_display * screen
Definition: resources.cpp:27
General purpose widgets.
void show_transient_message(CVideo &video, const std::string &title, const std::string &message, const std::string &image, const bool message_use_markup, const bool title_use_markup, const bool restore_background)
Shows a transient message to the user.
Replay control code.
void clear()
Definition: config.cpp:1055
-file sdl_utils.hpp
const std::string & last_recruit() const
Definition: team.hpp:233
const unit_type & type() const
The type of the unit (accounting for gender and variation).
Definition: unit.hpp:144
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:50
void recruit_unit(const unit_type &u_type, int side_num, const map_location &loc, const map_location &from, bool show, bool use_undo)
Recruits a unit of the given type for the given side.
Definition: create.cpp:699
void redo(const config &dst)
Definition: replay.cpp:410
const std::set< std::string > get_recruits(int side, const map_location &recruit_loc)
Gets the recruitable units from a side's leaders' personal recruit lists who can recruit on or from a...
Definition: create.cpp:63
#define ERR_NG
config & add_child(const std::string &key)
Definition: config.cpp:743
replay * recorder
Definition: resources.cpp:30
void spend_gold(const int amount)
Definition: team.hpp:213
void write(config &cfg) const
Definition: location.cpp:205
int cost() const
Definition: types.hpp:134
Encapsulates the map of the game.
Definition: location.hpp:38
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)...
t_route route
The hexes occupied by the affected unit during this action.
virtual bool redo(int side)
Redoes this action.
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:112
GLuint const GLchar * name
Definition: glew.h:1782
size_t erase(const map_location &l)
Erases the unit at location l, if any.
Definition: map.cpp:277
const std::string & base_id() const
The id of the original type from which this (variation) descended.
Definition: types.hpp:119
config & child(const std::string &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:658
std::string find_recruit_location(const int side, map_location &recruit_location, map_location &recruited_from, const std::string &unit_type)
Finds a location on which to place a unit.
Definition: create.cpp:464
void set_action_bonus_count(const int count)
Definition: team.hpp:219
Standard logging facilities (interface).
CVideo & video()
Gets the underlying screen object.
Definition: display.hpp:202
Container associating units to locations.
Definition: map.hpp:90
unit_iterator find(size_t id)
Definition: map.cpp:285
static lg::log_domain log_engine("engine")
void un_recruit_unit(const unit &u)
Definition: statistics.cpp:501
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
int action_bonus_count() const
Definition: team.hpp:218
config replay_data
the replay data to do this action, this is only !empty() when this action is on the redo stack we nee...
Definition: undo_action.hpp:69
GLsizei const GLcharARB ** string
Definition: glew.h:4503
unit_map * units
Definition: resources.cpp:35