The Battle for Wesnoth  1.19.5+dev
recruit.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2024
3  by Gabriel Morin <gabrielmorin (at) gmail (dot) com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * @file
18  */
19 
20 #include "whiteboard/recruit.hpp"
21 
23 #include "whiteboard/utility.hpp"
24 #include "whiteboard/visitor.hpp"
25 
26 #include "fake_unit_ptr.hpp"
27 #include "menu_events.hpp"
28 #include "play_controller.hpp"
29 #include "resources.hpp"
30 #include "units/unit.hpp"
32 #include "units/map.hpp"
33 #include "units/types.hpp"
34 
35 namespace wb
36 {
37 
38 std::ostream& operator<<(std::ostream& s, recruit_ptr recruit)
39 {
40  assert(recruit);
41  return recruit->print(s);
42 }
43 std::ostream& operator<<(std::ostream& s, recruit_const_ptr recruit)
44 {
45  assert(recruit);
46  return recruit->print(s);
47 }
48 
49 std::ostream& recruit::print(std::ostream &s) const
50 {
51  s << "Recruiting " << unit_name_ << " on hex " << recruit_hex_;
52  return s;
53 }
54 
55 recruit::recruit(std::size_t team_index, bool hidden, const std::string& unit_name, const map_location& recruit_hex):
56  action(team_index,hidden),
57  unit_name_(unit_name),
58  recruit_hex_(recruit_hex),
59  temp_unit_(create_corresponding_unit()), //auto-ptr ownership transfer
60  fake_unit_(temp_unit_->clone()), //temp_unit_ *copied* into new fake unit
61  cost_(0)
62 {
63  this->init();
64 }
65 
66 recruit::recruit(const config& cfg, bool hidden)
67  : action(cfg,hidden)
68  , unit_name_(cfg["unit_name_"])
69  , recruit_hex_(cfg.mandatory_child("recruit_hex_")["x"],cfg.mandatory_child("recruit_hex_")["y"], wml_loc())
70  , temp_unit_()
71  , fake_unit_()
72  , cost_(0)
73 {
74  // Validate unit_name_
76  throw action::ctor_err("recruit: Invalid recruit unit type");
77 
78  // Construct temp_unit_ and fake_unit_
79  temp_unit_ = create_corresponding_unit(); //auto-ptr ownership transfer
80  fake_unit_.reset(temp_unit_->clone()); //temp_unit_ copied into new fake_unit
81 
82  this->init();
83 }
84 
86 {
87  temp_unit_->set_movement(0, true);
88  temp_unit_->set_attacks(0);
89 
90  fake_unit_->set_location(recruit_hex_);
91  fake_unit_->set_movement(0, true);
92  fake_unit_->set_attacks(0);
93  fake_unit_->anim_comp().set_ghosted(false);
95 
96  cost_ = fake_unit_->type().cost();
97 }
98 
100 {
101 }
102 
104 {
105  v.visit(shared_from_this());
106 }
107 
108 void recruit::execute(bool& success, bool& complete)
109 {
110  assert(valid());
111  temporary_unit_hider const raii(*fake_unit_);
112  const std::size_t old_id = fake_unit_->underlying_id();
114  const int side_num = team_index() + 1;
115  //Give back the spent gold so we don't get "not enough gold" message
116  resources::gameboard->teams().at(team_index()).get_side_actions()->change_gold_spent_by(-cost_);
117  bool const result = resources::controller->get_menu_handler().do_recruit(unit_name_, side_num, loc);
118  //If it failed, take back the gold
119  if (!result) {
120  resources::gameboard->teams().at(team_index()).get_side_actions()->change_gold_spent_by(cost_);
121  }
122  else {
123  auto mit = resources::gameboard->units().find(loc);
124  if(mit != resources::gameboard->units().end()) {
125  viewer_actions()->update_recruited_unit(old_id, *mit);
126  }
127 
128  }
129  success = complete = result;
130 }
131 
133 {
134  assert(valid());
135  temp_unit_->set_location(recruit_hex_);
136 
137  DBG_WB << "Inserting future recruit [" << temp_unit_->id()
138  << "] at position " << temp_unit_->get_location() << ".";
139 
140  // Add cost to money spent on recruits.
141  resources::gameboard->teams().at(team_index()).get_side_actions()->change_gold_spent_by(cost_);
142 
143  // Temporarily insert unit into unit_map
144  // unit map takes ownership of temp_unit
145  const size_t old_id = temp_unit_->underlying_id();
147 
148  //in the past there was a bug where the map changed the unit ids here (because a unit with that id already existed) which caused crashes later.
149  assert(temp_unit_->underlying_id() == old_id);
150 
151  // Update gold in the top bar
153 }
154 
156 {
157  //Unit map gives back ownership of temp_unit_
159 
160  //remove simulated unit refresh on new turn done by mapbuilder.
161  temp_unit_->set_movement(0, true);
162  temp_unit_->set_attacks(0);
163 
164  assert(temp_unit_.get());
165 }
166 
168 {
169  if (hex == recruit_hex_)
170  {
171  const double x_offset = 0.5;
172  const double y_offset = 0.7;
173  //position 0,0 in the hex is the upper left corner
174  std::stringstream number_text;
175  number_text << font::unicode_minus << cost_;
176  std::size_t font_size = 16;
177  color_t color {255, 0, 0}; //red
179  number_text.str(), font_size, color, x_offset, y_offset);
180  }
181 }
182 
184 {
186 }
187 
188 
190 {
192  assert(type);
193  int side_num = team_index() + 1;
194  //real_unit = false needed to avoid generating random traits and causing OOS
195  bool real_unit = false;
196  unit_ptr result = unit::create(*type, side_num, real_unit);
197  result->set_movement(0, true);
198  result->set_attacks(0);
199  return result; //ownership gets transferred to returned unique_ptr copy
200 }
201 
203 {
204  //Check that destination hex is still free
205  if(resources::gameboard->units().find(recruit_hex_) != resources::gameboard->units().end()) {
206  return LOCATION_OCCUPIED;
207  }
208  //Check that unit to recruit is still in side's recruit list
209  const std::set<std::string>& recruits = resources::gameboard->teams()[team_index()].recruits();
210  if(recruits.find(unit_name_) == recruits.end()) {
211  bool in_extra_recruit = any_recruiter(team_index() + 1, get_recruit_hex(), [&](unit& leader) {
212  return std::find(leader.recruits().begin(), leader.recruits().end(), unit_name_) != leader.recruits().end();
213  });
214  if (!in_extra_recruit) {
215  return UNIT_UNAVAILABLE;
216  }
217  }
218  //Check that there is still enough gold to recruit this unit
219  if(temp_unit_->cost() > resources::gameboard->teams()[team_index()].gold()) {
220  return NOT_ENOUGH_GOLD;
221  }
222  //Check that there is a leader available to recruit this unit
224  return NO_LEADER;
225  }
226 
227  return OK;
228 }
229 
231 {
232  config final_cfg = action::to_config();
233 
234  final_cfg["type"] = "recruit";
235  final_cfg["unit_name_"] = unit_name_;
236 // final_cfg["temp_cost_"] = temp_cost_; //Unnecessary
237 
238  config loc_cfg;
239  loc_cfg["x"]=recruit_hex_.wml_x();
240  loc_cfg["y"]=recruit_hex_.wml_y();
241  final_cfg.add_child("recruit_hex_", std::move(loc_cfg));
242 
243  return final_cfg;
244 }
245 
246 void recruit::do_hide() {fake_unit_->set_hidden(true);}
247 void recruit::do_show() {fake_unit_->set_hidden(false);}
248 
249 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
config & add_child(config_key_type key)
Definition: config.cpp:440
void draw_text_in_hex(const map_location &loc, const drawing_layer layer, const std::string &text, std::size_t font_size, color_t color, double x_in_hex=0.5, double y_in_hex=0.5)
Draw text on a hex.
Definition: display.cpp:1453
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:3087
void invalidate_game_status()
Function to invalidate the game status displayed on the sidebar.
Definition: display.hpp:316
static display * get_singleton()
Returns the display object if a display object exists.
Definition: display.hpp:111
bool do_recruit(const std::string &name, int side_num, map_location &target_hex)
void reset()
Reset the internal unit pointer, and deregister from the manager.
void place_on_fake_unit_manager(fake_unit_manager *d)
Place this on manager's fake_units_ dequeue.
virtual const std::vector< team > & teams() const override
Definition: game_board.hpp:80
virtual const unit_map & units() const override
Definition: game_board.hpp:107
events::menu_handler & get_menu_handler()
Container associating units to locations.
Definition: map.hpp:98
unit_ptr extract(const map_location &loc)
Extracts a unit from the map.
Definition: map.cpp:259
unit_iterator find(std::size_t id)
Definition: map.cpp:302
umap_retval_pair_t insert(unit_ptr p)
Inserts the unit pointed to by p into the map.
Definition: map.cpp:135
const unit_type * find(const std::string &key, unit_type::BUILD_STATUS status=unit_type::FULL) const
Finds a unit_type by its id() and makes sure it is built to the specified level.
Definition: types.cpp:1265
A single unit type that the player may recruit.
Definition: types.hpp:43
This class represents a single unit of a specific type.
Definition: unit.hpp:133
static unit_ptr create(const config &cfg, bool use_traits=false, const vconfig *vcfg=nullptr)
Initializes a unit from a config.
Definition: unit.hpp:201
Abstract base class for all the whiteboard planned actions.
Definition: action.hpp:34
bool valid()
Returns whether this action is valid or not.
Definition: action.hpp:135
std::size_t team_index() const
Returns the index of the team that owns this action.
Definition: action.hpp:84
virtual config to_config() const
Constructs and returns a config object representing this object.
Definition: action.cpp:51
error
Possible errors.
Definition: action.hpp:107
@ NOT_ENOUGH_GOLD
Definition: action.hpp:118
@ LOCATION_OCCUPIED
Definition: action.hpp:112
@ UNIT_UNAVAILABLE
Definition: action.hpp:117
virtual error check_validity() const
Check the validity of the action.
Definition: recruit.cpp:202
std::string unit_name_
Definition: recruit.hpp:85
virtual void do_hide()
Called by the non-virtual hide() and show(), respectively.
Definition: recruit.cpp:246
fake_unit_ptr fake_unit_
Definition: recruit.hpp:89
std::shared_ptr< recruit > shared_from_this()
Definition: recruit.hpp:81
virtual std::ostream & print(std::ostream &s) const
Definition: recruit.cpp:49
virtual void draw_hex(const map_location &hex)
Gets called by display when drawing a hex, to allow actions to draw to the screen.
Definition: recruit.cpp:167
map_location const get_recruit_hex() const
Definition: recruit.hpp:75
virtual config to_config() const
Constructs and returns a config object representing this object.
Definition: recruit.cpp:230
map_location recruit_hex_
Definition: recruit.hpp:86
unit_ptr temp_unit_
Definition: recruit.hpp:88
virtual void do_show()
Definition: recruit.cpp:247
virtual void execute(bool &success, bool &complete)
Output parameters: success: Whether or not to continue an execute-all after this execution complete: ...
Definition: recruit.cpp:108
recruit(std::size_t team_index, bool hidden, const std::string &unit_name, const map_location &recruit_hex)
Definition: recruit.cpp:55
virtual void apply_temp_modifier(unit_map &unit_map)
Applies temporarily the result of this action to the specified unit map.
Definition: recruit.cpp:132
void init()
Definition: recruit.cpp:85
virtual ~recruit()
Definition: recruit.cpp:99
unit_ptr create_corresponding_unit()
Definition: recruit.cpp:189
virtual void remove_temp_modifier(unit_map &unit_map)
Removes the result of this action from the specified unit map.
Definition: recruit.cpp:155
virtual void accept(visitor &v)
Definition: recruit.cpp:103
virtual void redraw()
Redrawing function, called each time the action situation might have changed.
Definition: recruit.cpp:183
Abstract base class for all the visitors (cf GoF Visitor Design Pattern) the whiteboard uses.
Definition: visitor.hpp:33
virtual void visit(move_ptr move)=0
@ actions_numbering
Move numbering for the whiteboard.
const std::vector< std::string > & recruits() const
The type IDs of the other units this unit may recruit, if possible.
Definition: unit.hpp:624
const std::string unicode_minus
Definition: constants.cpp:42
game_board * gameboard
Definition: resources.cpp:20
fake_unit_manager * fake_units
Definition: resources.cpp:30
play_controller * controller
Definition: resources.cpp:21
Definition: display.hpp:45
std::shared_ptr< recruit > recruit_ptr
Definition: typedefs.hpp:72
std::ostream & operator<<(std::ostream &s, action_ptr action)
Definition: action.cpp:34
unit * find_recruiter(std::size_t team_index, const map_location &hex)
Definition: utility.cpp:70
bool any_recruiter(int team_num, const map_location &loc, std::function< bool(unit &)> func)
executes func for each unti of side of side_num that can recruit on loc.
Definition: utility.cpp:83
std::shared_ptr< recruit const > recruit_const_ptr
Definition: typedefs.hpp:73
side_actions_ptr viewer_actions()
Definition: utility.cpp:42
std::shared_ptr< unit > unit_ptr
Definition: ptr.hpp:26
static config unit_name(const unit *u)
Definition: reports.cpp:160
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:59
Encapsulates the map of the game.
Definition: location.hpp:45
int wml_y() const
Definition: location.hpp:184
int wml_x() const
Definition: location.hpp:183
static map_location::direction s
#define DBG_WB
Definition: typedefs.hpp:28
unit_type_data unit_types
Definition: types.cpp:1500
visitor is an abstract interface : action.accept(visitor) calls visitor.visit(action)