Skip to content

Commit

Permalink
less unit copying
Browse files Browse the repository at this point in the history
  • Loading branch information
gfgtdf committed Apr 14, 2017
1 parent 2349296 commit c462519
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 38 deletions.
4 changes: 3 additions & 1 deletion src/actions/advancement.cpp
Expand Up @@ -346,13 +346,15 @@ void advance_unit(map_location loc, const advancement_option &advance_to, bool f
bool use_amla = boost::get<std::string>(&advance_to) == nullptr;
unit_ptr new_unit = use_amla ? get_amla_unit(*u, *boost::get<const config*>(advance_to)) :
get_advanced_unit(*u, boost::get<std::string>(advance_to));
new_unit->set_location(loc);
if ( !use_amla )
{
statistics::advance_unit(*new_unit);
preferences::encountered_units().insert(new_unit->type_id());
LOG_CF << "Added '" << new_unit->type_id() << "' to the encountered units.\n";
}
u = resources::gameboard->units().replace(loc, *new_unit).first;
resources::gameboard->units().erase(loc);
u = resources::gameboard->units().insert(new_unit).first;

// Update fog/shroud.
actions::shroud_clearer clearer;
Expand Down
18 changes: 9 additions & 9 deletions src/actions/attack.cpp
Expand Up @@ -1154,26 +1154,26 @@ namespace {
if (reanimator)
{
LOG_NG << "found unit type:" << reanimator->id() << '\n';
unit newunit(*reanimator, attacker.get_unit().side(),
true, unit_race::MALE);
newunit.set_attacks(0);
newunit.set_movement(0, true);
unit_ptr newunit(new unit(*reanimator, attacker.get_unit().side(), true, unit_race::MALE));
newunit->set_attacks(0);
newunit->set_movement(0, true);
// Apply variation
if (undead_variation != "null")
{
config mod;
config &variation = mod.add_child("effect");
variation["apply_to"] = "variation";
variation["name"] = undead_variation;
newunit.add_modification("variation",mod);
newunit.heal_fully();
newunit->add_modification("variation",mod);
newunit->heal_fully();
}
units_.add(death_loc, newunit);
newunit->set_location(death_loc);
units_.insert(newunit);

game_events::entity_location reanim_loc(defender.loc_, newunit.underlying_id());
game_events::entity_location reanim_loc(defender.loc_, newunit->underlying_id());
resources::game_events->pump().fire("unit_placed", reanim_loc);

preferences::encountered_units().insert(newunit.type_id());
preferences::encountered_units().insert(newunit->type_id());
if (update_display_) {
resources::screen->invalidate(death_loc);
}
Expand Down
6 changes: 4 additions & 2 deletions src/actions/unit_creator.cpp
Expand Up @@ -170,7 +170,8 @@ void unit_creator::add_unit(const config &cfg, const vconfig* vcfg)
map_location loc = find_location(temp_cfg, new_unit.get());
if ( loc.valid() ) {
//add the new unit to map
board_->units().replace(loc, *new_unit);
new_unit->set_location(loc);
board_->units().insert(new_unit);
LOG_NG << "inserting unit for side " << new_unit->side() << "\n";
post_create(loc,*(board_->units().find(loc)),animate);
}
Expand All @@ -184,7 +185,8 @@ void unit_creator::add_unit(const config &cfg, const vconfig* vcfg)
//get unit from recall list
map_location loc = find_location(temp_cfg, recall_list_element.get());
if ( loc.valid() ) {
board_->units().replace(loc, *recall_list_element);
recall_list_element->set_location(loc);
board_->units().insert(recall_list_element);
LOG_NG << "inserting unit from recall list for side " << recall_list_element->side()<< " with id="<< id << "\n";
post_create(loc,*(board_->units().find(loc)),animate);
//if id is not empty, delete units with this ID from recall list
Expand Down
35 changes: 19 additions & 16 deletions src/ai/simulated_actions.cpp
Expand Up @@ -209,12 +209,13 @@ void helper_check_village(const map_location& loc, int side){
}

void helper_place_unit(const unit& u, const map_location& loc){
unit new_unit = u;
new_unit.set_movement(0, true);
new_unit.set_attacks(0);
new_unit.heal_fully();
unit_ptr new_unit(new unit(u));
new_unit->set_movement(0, true);
new_unit->set_attacks(0);
new_unit->heal_fully();
new_unit->set_location(loc);

std::pair<unit_map::iterator, bool> add_result = resources::gameboard->units().add(loc, new_unit);
std::pair<unit_map::iterator, bool> add_result = resources::gameboard->units().insert(new_unit);
assert(add_result.second);
unit_map::iterator& new_unit_itor = add_result.first;

Expand All @@ -239,7 +240,7 @@ void helper_advance_unit(const map_location& loc){
int options_num = unit_helper::number_of_possible_advances(*advance_unit);

size_t advance_choice = rand() % options_num;
unit advanced_unit(*advance_unit);
unit_ptr advanced_unit(new unit(*advance_unit));

if(advance_choice < options.size()){
std::string advance_unit_typename = options[advance_choice];
Expand All @@ -248,20 +249,22 @@ void helper_advance_unit(const map_location& loc){
ERR_AI_SIM_ACTIONS << "Simulating advancing to unknown unit type: " << advance_unit_typename;
assert(false && "simulating to unknown unit type");
}
advanced_unit.set_experience(advanced_unit.experience() - advanced_unit.max_experience());
advanced_unit.advance_to(*advanced_type);
advanced_unit.heal_fully();
advanced_unit.set_state(unit::STATE_POISONED, false);
advanced_unit.set_state(unit::STATE_SLOWED, false);
advanced_unit.set_state(unit::STATE_PETRIFIED, false);
advanced_unit->set_experience(advanced_unit->experience() - advanced_unit->max_experience());
advanced_unit->advance_to(*advanced_type);
advanced_unit->heal_fully();
advanced_unit->set_state(unit::STATE_POISONED, false);
advanced_unit->set_state(unit::STATE_SLOWED, false);
advanced_unit->set_state(unit::STATE_PETRIFIED, false);
}else{
const config &mod_option = mod_options[advance_choice-options.size()];
advanced_unit.set_experience(advanced_unit.experience()-advanced_unit.max_experience());
advanced_unit.add_modification("advancement", mod_option);
advanced_unit->set_experience(advanced_unit->experience()-advanced_unit->max_experience());
advanced_unit->add_modification("advancement", mod_option);
}

resources::gameboard->units().replace(loc, advanced_unit);
LOG_AI_SIM_ACTIONS << advance_unit->type_name() << " at " << loc << " advanced to " << advanced_unit.type_name() << std::endl;
advanced_unit->set_location(loc);
resources::gameboard->units().erase(loc);
resources::gameboard->units().insert(advanced_unit);
LOG_AI_SIM_ACTIONS << advance_unit->type_name() << " at " << loc << " advanced to " << advanced_unit->type_name() << std::endl;
}

}// End namespace
3 changes: 2 additions & 1 deletion src/scripting/lua_unit.cpp
Expand Up @@ -84,7 +84,8 @@ bool lua_unit::put_map(const map_location &loc)
if (it) {
side = 0;
// uid may be changed by unit_map on insertion
uid = resources::gameboard->units().replace(loc, *it).first->underlying_id();
it->set_location(loc);
uid = resources::gameboard->units().insert(it).first->underlying_id();
} else {
ERR_LUA << "Could not find unit " << uid << " on recall list of side " << side << '\n';
return false;
Expand Down
18 changes: 9 additions & 9 deletions src/synced_commands.cpp
Expand Up @@ -439,12 +439,12 @@ SYNCED_COMMAND_HANDLER_FUNCTION(debug_unit, child, use_undo, /*show*/, /*error_

// Attempt to create a new unit. If there are error (such an invalid type key), exit.
try{
unit new_u(cfg, true);

unit_ptr new_u(new unit(cfg, true));
new_u->set_location(loc);
// Don't remove the unit until after we've verified there are no errors in creating the new one,
// or else the unit would simply be removed from the map with no replacement.
resources::gameboard->units().erase(loc);
resources::gameboard->units().add(loc, new_u);
resources::gameboard->units().insert(new_u);
} catch(unit_type::error& e) {
ERR_REPLAY << e.what() << std::endl; // TODO: more appropriate error message log
return false;
Expand Down Expand Up @@ -477,22 +477,22 @@ SYNCED_COMMAND_HANDLER_FUNCTION(debug_create_unit, child, use_undo, /*show*/, e
? resources::controller->current_side() : 1;

// Create the unit.
unit created(*u_type, side_num, true, gender);
created.new_turn();

unit_ptr created(new unit(*u_type, side_num, true, gender));
created->new_turn();
created->set_location(loc);
// Add the unit to the board.
std::pair<unit_map::iterator, bool> add_result = resources::gameboard->units().replace(loc, created);
std::pair<unit_map::iterator, bool> add_result = resources::gameboard->units().insert(created);
resources::screen->invalidate_unit();
resources::game_events->pump().fire("unit_placed", loc);
unit_display::unit_recruited(loc);

// Village capture?
if ( resources::gameboard->map().is_village(loc) )
actions::get_village(loc, created.side());
actions::get_village(loc, created->side());

// Update fog/shroud.
actions::shroud_clearer clearer;
clearer.clear_unit(loc, created);
clearer.clear_unit(loc, *created);
clearer.fire_events();
if ( add_result.first.valid() ) // In case sighted events messed with the unit.
actions::actor_sighted(*add_result.first);
Expand Down

0 comments on commit c462519

Please sign in to comment.