Skip to content

Commit

Permalink
Second attempt to fix bug #1941
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz committed Sep 4, 2017
1 parent 92d74f7 commit 7cef972
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/ai/default/attack.cpp
Expand Up @@ -99,6 +99,8 @@ void attack_analysis::analyze(const gamemap& map, unit_map& units,
std::vector<std::pair<map_location,map_location> >::const_iterator m;

std::unique_ptr<battle_context> bc(nullptr);
std::unique_ptr<battle_context> old_bc(nullptr);

const combatant *prev_def = nullptr;

for (m = movements.begin(); m != movements.end(); ++m) {
Expand All @@ -117,6 +119,11 @@ void attack_analysis::analyze(const gamemap& map, unit_map& units,

bool from_cache = false;

// Swap the two context pointers. old_bc should be null at this point, so bc is cleared
// and old_bc takes ownership of the context pointer. This allows prev_def to remain
// valid until it's reassigned.
old_bc.swap(bc);

// This cache is only about 99% correct, but speeds up evaluation by about 1000 times.
// We recalculate when we actually attack.
const readonly_context::unit_stats_cache_t::key_type cache_key = std::make_pair(target, &up->type());
Expand All @@ -136,6 +143,9 @@ void attack_analysis::analyze(const gamemap& map, unit_map& units,

prev_def = &bc->get_defender_combatant(prev_def);

// We no longer need the old context since prev_def has been reassigned.
old_bc.reset(nullptr);

if ( !from_cache ) {
ai_obj.unit_stats_cache().emplace(cache_key, std::make_pair(
bc->get_attacker_stats(),
Expand Down

0 comments on commit 7cef972

Please sign in to comment.