Cleaned interface of recruit_unit.

This commit is contained in:
Guillaume Melquiond 2009-07-18 12:06:05 +00:00
parent 918a193adb
commit bc54c00761
7 changed files with 40 additions and 35 deletions

View File

@ -32,6 +32,7 @@
#include "mouse_handler_base.hpp"
#include "pathfind.hpp"
#include "replay.hpp"
#include "resources.hpp"
#include "statistics.hpp"
#include "unit_abilities.hpp"
#include "unit_display.hpp"
@ -206,19 +207,20 @@ bool can_recruit_on(const gamemap& map, const map_location& leader, const map_lo
return !rt.steps.empty();
}
std::string recruit_unit(const gamemap& map, const int side, unit_map& units,
unit new_unit, map_location& recruit_location,const bool is_recall,
const bool show, const bool need_castle, const bool full_movement,
const bool wml_triggered)
std::string recruit_unit(int side, const unit &new_u,
const map_location &recruit_loc, bool is_recall,
bool show, bool need_castle, bool full_movement,
bool wml_triggered)
{
const events::command_disabler disable_commands;
LOG_NG << "recruiting unit for side " << side << "\n";
// Find the unit that can recruit
unit_map::const_iterator u = units.begin();
unit_map::const_iterator u = resources::units->begin(),
u_end = resources::units->end();
for(; u != units.end(); ++u) {
for(; u != u_end; ++u) {
if(u->second.can_recruit() &&
static_cast<int>(u->second.side()) == side) {
@ -226,36 +228,39 @@ std::string recruit_unit(const gamemap& map, const int side, unit_map& units,
}
}
if(u == units.end() && (need_castle || !map.on_board(recruit_location))) {
map_location recruit_location = recruit_loc;
if (u == u_end && (need_castle || !resources::game_map->on_board(recruit_location))) {
return _("You don't have a leader to recruit with.");
}
assert(u != units.end() || !need_castle);
assert(u != u_end || !need_castle);
if(need_castle && map.is_keep(u->first) == false) {
if (need_castle && resources::game_map->is_keep(u->first) == false) {
LOG_NG << "Leader not on start: leader is on " << u->first << '\n';
return _("You must have your leader on a keep to recruit or recall units.");
}
if(need_castle) {
if (units.find(recruit_location) != units.end() ||
!can_recruit_on(map, u->first, recruit_location)) {
if (resources::units->find(recruit_location) != resources::units->end() ||
!can_recruit_on(*resources::game_map, u->first, recruit_location))
{
recruit_location = map_location();
}
}
if(!map.on_board(recruit_location)) {
recruit_location = find_vacant_tile(map,units,u->first,
if (!resources::game_map->on_board(recruit_location)) {
recruit_location = find_vacant_tile(*resources::game_map, *resources::units, u->first,
need_castle ? VACANT_CASTLE : VACANT_ANY);
} else if(units.count(recruit_location) == 1) {
recruit_location = find_vacant_tile(map,units,recruit_location,VACANT_ANY);
} else if (resources::units->count(recruit_location) == 1) {
recruit_location = find_vacant_tile(*resources::game_map, *resources::units, recruit_location, VACANT_ANY);
}
if(!map.on_board(recruit_location)) {
if (!resources::game_map->on_board(recruit_location)) {
return _("There are no vacant castle tiles in which to recruit a unit.");
}
unit new_unit = new_u;
if(full_movement) {
new_unit.set_movement(new_unit.total_movement());
} else {
@ -265,7 +270,7 @@ std::string recruit_unit(const gamemap& map, const int side, unit_map& units,
new_unit.heal_all();
new_unit.set_hidden(true);
units.add(recruit_location, new_unit);
resources::units->add(recruit_location, new_unit);
if (is_recall)
{
@ -277,8 +282,8 @@ std::string recruit_unit(const gamemap& map, const int side, unit_map& units,
LOG_NG << "firing prerecruit event\n";
game_events::fire("prerecruit",recruit_location);
}
const unit_map::iterator new_unit_itor = units.find(recruit_location);
if(new_unit_itor != units.end()) new_unit_itor->second.set_hidden(false);
const unit_map::iterator new_unit_itor = resources::units->find(recruit_location);
if (new_unit_itor != resources::units->end()) new_unit_itor->second.set_hidden(false);
if (show) {
if (u.valid()) {
unit_display::unit_recruited(recruit_location,u->first);

View File

@ -60,10 +60,10 @@ struct end_level_exception;
* @return an empty string on success. Otherwise a human-readable message
* describing the failure is returned.
*/
std::string recruit_unit(const gamemap& map, const int side, unit_map& units,
unit u, map_location& recruit_location,const bool is_recall,
const bool show=false,const bool need_castle=true,
const bool full_movement=false,const bool wml_triggered=false);
std::string recruit_unit(int side, const unit &u,
const map_location &recruit_location, bool is_recall,
bool show = false, bool need_castle = true,
bool full_movement = false, bool wml_triggered = false);
/** Computes the statistics of a battle between an attacker and a defender unit. */
class battle_context

View File

@ -666,7 +666,7 @@ void recruit_result::do_execute()
replay_undo replay_guard(recorder);
unit_type_data::unit_type_map::const_iterator u = unit_type_data::types().find_unit_type(unit_name_);
unit new_unit(&info.units, &u->second, get_side(), true);
std::string recruit_err = recruit_unit(info.map,get_side(),info.units,new_unit,recruit_location_,false,preferences::show_ai_moves());
std::string recruit_err = recruit_unit(get_side(), new_unit, recruit_location_, false, preferences::show_ai_moves());
if(recruit_err.empty()) {
statistics::recruit_unit(new_unit);
get_my_team(info).spend_gold(u->second.cost());

View File

@ -185,7 +185,7 @@ bool readwrite_context_impl::recruit(const std::string& unit_name, map_location
unit new_unit(&get_info().units, &u->second, get_side(), true);
// See if we can actually recruit (i.e. have enough room etc.)
std::string recruit_err = recruit_unit(get_info().map,get_side(),get_info().units,new_unit,loc,false,preferences::show_ai_moves());
std::string recruit_err = recruit_unit(get_side(), new_unit, loc, false, preferences::show_ai_moves());
if(recruit_err.empty()) {
statistics::recruit_unit(new_unit);

View File

@ -2019,7 +2019,7 @@ WML_HANDLER_FUNCTION(recall, /*event_info*/, cfg)
temp_config["y"] = "recall";
vconfig unit_filter(temp_config);
for(int index = 0; !unit_recalled && index < int(resources::teams->size()); ++index) {
LOG_NG << "for side " << index << "...\n";
LOG_NG << "for side " << index + 1 << "...\n";
const std::string player_id = (*resources::teams)[index].save_id();
if((*resources::teams)[index].recall_list().size() < 1) {
@ -2038,7 +2038,7 @@ WML_HANDLER_FUNCTION(recall, /*event_info*/, cfg)
map_location loc = cfg_to_loc(cfg);
unit to_recruit(*u);
avail.erase(u); // Erase before recruiting, since recruiting can fire more events
recruit_unit(*resources::game_map, index + 1, *resources::units, to_recruit, loc, true, utils::string_bool(cfg["show"], true), false, true, true);
recruit_unit(index + 1, to_recruit, loc, true, utils::string_bool(cfg["show"], true), false, true, true);
unit_recalled = true;
break;
}

View File

@ -783,7 +783,7 @@ private:
recorder.add_recruit(recruit_num, last_hex);
unit new_unit(&units_, &u_type->second, side_num, true);
map_location loc = last_hex;
const std::string &msg = recruit_unit(map_, side_num, units_,
const std::string &msg = recruit_unit(side_num,
new_unit, loc, false, gui_ != NULL);
if(msg.empty()) {
current_team.spend_gold(u_type->second.cost());
@ -940,7 +940,7 @@ private:
map_location loc = last_hex;
recorder.add_recall(res,loc);
un.set_game_context(&units_);
const std::string &err = recruit_unit(map_, side_num, units_,
const std::string &err = recruit_unit(side_num,
un, loc, true, gui_ != NULL);
if(!err.empty()) {
recorder.undo();
@ -1110,8 +1110,8 @@ private:
recorder.add_recall(action.recall_pos,action.recall_loc);
un.set_game_context(&units_);
const std::string &msg = recruit_unit(map_, side_num,
units_, un, action.recall_loc, true, gui_ != NULL);
const std::string &msg = recruit_unit(side_num,
un, action.recall_loc, true, gui_ != NULL);
if(msg.empty()) {
statistics::recall_unit(un);
current_team.spend_gold(game_config::recall_cost);
@ -1149,7 +1149,7 @@ private:
recorder.add_recruit(recruit_num,loc);
unit new_unit = action.affected_unit;
//unit new_unit(action.affected_unit.type(),team_num_,true);
const std::string &msg = recruit_unit(map_, side_num, units_,
const std::string &msg = recruit_unit(side_num,
new_unit, loc, false, gui_ != NULL);
if(msg.empty()) {
current_team.spend_gold(new_unit.type()->cost());

View File

@ -931,7 +931,7 @@ bool do_replay_handle(game_display& disp, const gamemap& map,
}
unit new_unit(&units, &u_type->second, team_num, true, false);
const std::string& res = recruit_unit(map,team_num,units,new_unit,loc,false,!get_replay_source().is_skipping());
const std::string& res = recruit_unit(team_num, new_unit, loc, false, !get_replay_source().is_skipping());
if(!res.empty()) {
std::stringstream errbuf;
errbuf << "cannot recruit unit: " << res << "\n";
@ -972,7 +972,7 @@ bool do_replay_handle(game_display& disp, const gamemap& map,
if(val >= 0 && val < int(current_team.recall_list().size())) {
statistics::recall_unit(current_team.recall_list()[val]);
current_team.recall_list()[val].set_game_context(&units);
recruit_unit(map,team_num,units,current_team.recall_list()[val],loc,true,!get_replay_source().is_skipping());
recruit_unit(team_num, current_team.recall_list()[val], loc, true, !get_replay_source().is_skipping());
current_team.recall_list().erase(current_team.recall_list().begin()+val);
current_team.spend_gold(game_config::recall_cost);
} else {