Skip to content

Commit

Permalink
don't copy units when recruiting/recalling
Browse files Browse the repository at this point in the history
units are rather big objects so we don't want to copy them unless
needed.
  • Loading branch information
gfgtdf committed Mar 14, 2016
1 parent a91cb9a commit 43275cd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
32 changes: 15 additions & 17 deletions src/actions/create.cpp
Expand Up @@ -608,37 +608,35 @@ namespace { // Helpers for place_recruit()
}
}// anonymous namespace
//Used by recalls and recruits
place_recruit_result place_recruit(const unit &u, const map_location &recruit_location, const map_location& recruited_from,
place_recruit_result place_recruit(unit_ptr u, const map_location &recruit_location, const map_location& recruited_from,
int cost, bool is_recall, bool show, bool fire_event, bool full_movement,
bool wml_triggered)
{
place_recruit_result res(false, 0, false);
LOG_NG << "placing new unit on location " << recruit_location << "\n";
unit new_unit = u;
if (full_movement) {
new_unit.set_movement(new_unit.total_movement(), true);
u->set_movement(u->total_movement(), true);
} else {
new_unit.set_movement(0, true);
new_unit.set_attacks(0);
u->set_movement(0, true);
u->set_attacks(0);
}
new_unit.heal_all();
new_unit.set_hidden(true);
u->heal_all();
u->set_hidden(true);

// Get the leader location before adding the unit to the board.
const map_location leader_loc = !show ? map_location::null_location() :
find_recruit_leader(new_unit.side(), recruit_location, recruited_from);

find_recruit_leader(u->side(), recruit_location, recruited_from);
u->set_location(recruit_location);
// Add the unit to the board.
std::pair<unit_map::iterator, bool> add_result =
resources::units->add(recruit_location, new_unit);
std::pair<unit_map::iterator, bool> add_result = resources::units->insert(u);
assert(add_result.second);
unit_map::iterator & new_unit_itor = add_result.first;
map_location current_loc = recruit_location;

set_recruit_facing(new_unit_itor, new_unit, recruit_location, leader_loc);
set_recruit_facing(new_unit_itor, *u, recruit_location, leader_loc);

// Do some bookkeeping.
recruit_checksums(new_unit, wml_triggered);
recruit_checksums(*u, wml_triggered);
resources::whiteboard->on_gamestate_change();

resources::game_events->pump().fire("unit placed", current_loc);
Expand All @@ -654,7 +652,7 @@ place_recruit_result place_recruit(const unit &u, const map_location &recruit_lo
new_unit_itor->set_hidden(true);
}
preferences::encountered_units().insert(new_unit_itor->type_id());
(*resources::teams)[new_unit.side()-1].spend_gold(cost);
(*resources::teams)[u->side()-1].spend_gold(cost);

if ( show ) {
unit_display::unit_recruited(current_loc, leader_loc);
Expand Down Expand Up @@ -706,7 +704,7 @@ void recruit_unit(const unit_type & u_type, int side_num, const map_location & l


// Place the recruit.
place_recruit_result res = place_recruit(*new_unit, loc, from, u_type.cost(), false, show);
place_recruit_result res = place_recruit(new_unit, loc, from, u_type.cost(), false, show);
statistics::recruit_unit(*new_unit);

// To speed things a bit, don't bother with the undo stack during
Expand Down Expand Up @@ -749,11 +747,11 @@ bool recall_unit(const std::string & id, team & current_team,
// we use the team's recall cost otherwise the unit's.
place_recruit_result res;
if (recall->recall_cost() < 0) {
res = place_recruit(*recall, loc, from, current_team.recall_cost(),
res = place_recruit(recall, loc, from, current_team.recall_cost(),
true, show);
}
else {
res = place_recruit(*recall, loc, from, recall->recall_cost(),
res = place_recruit(recall, loc, from, recall->recall_cost(),
true, show);
}
statistics::recall_unit(*recall);
Expand Down
2 changes: 1 addition & 1 deletion src/actions/create.hpp
Expand Up @@ -149,7 +149,7 @@ std::vector<unit_const_ptr > get_recalls(int side, const map_location &recall_lo
* @returns true if an event (or fog clearing) has mutated the game state.
*/
typedef boost::tuple<bool /*event modified*/, int /*previous village owner side*/, bool /*capture bonus time*/> place_recruit_result;
place_recruit_result place_recruit(const unit &u, const map_location &recruit_location, const map_location& recruited_from,
place_recruit_result place_recruit(unit_ptr u, const map_location &recruit_location, const map_location& recruited_from,
int cost, bool is_recall, bool show = false, bool fire_event = true, bool full_movement = false,
bool wml_triggered = false);

Expand Down
4 changes: 2 additions & 2 deletions src/game_events/action_wml.cpp
Expand Up @@ -546,7 +546,7 @@ WML_HANDLER_FUNCTION(recall, /*event_info*/, cfg)
if(resources::gameboard->map().on_board(loc)) {
DBG_NG << "...valid location for the recall found. Recalling.\n";
avail.erase(u); // Erase before recruiting, since recruiting can fire more events
actions::place_recruit(*to_recruit, loc, leader->get_location(), 0, true,
actions::place_recruit(to_recruit, loc, leader->get_location(), 0, true,
cfg["show"].to_bool(true), cfg["fire_event"].to_bool(false),
true, true);
return;
Expand All @@ -562,7 +562,7 @@ WML_HANDLER_FUNCTION(recall, /*event_info*/, cfg)
DBG_NG << "No usable leader found, but found usable location. Recalling.\n";
avail.erase(u); // Erase before recruiting, since recruiting can fire more events
map_location null_location = map_location::null_location();
actions::place_recruit(*to_recruit, loc, null_location, 0, true, cfg["show"].to_bool(true),
actions::place_recruit(to_recruit, loc, null_location, 0, true, cfg["show"].to_bool(true),
cfg["fire_event"].to_bool(false), true, true);
return;
}
Expand Down

0 comments on commit 43275cd

Please sign in to comment.