Skip to content

Commit

Permalink
fix hazard removal while iterating
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscoDA committed Mar 18, 2019
1 parent 7d52415 commit 5c8e6c4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
17 changes: 9 additions & 8 deletions game/state/battle/battle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1326,7 +1326,7 @@ sp<BattleHazard> Battle::placeHazard(GameState &state, StateRef<Organisation> ow
LogWarning(
"Ensure we are not putting out a fire that is attached to a feature!");
}
existingHazard->die(state, false);
existingHazard->die(state, false, true);
}
}
map->addObjectToMap(hazard);
Expand Down Expand Up @@ -1671,8 +1671,10 @@ void Battle::update(GameState &state, unsigned int ticks)
Trace::start("Battle::update::hazards->update");
for (auto it = this->hazards.begin(); it != this->hazards.end();)
{
auto d = *it++;
d->update(state, ticks);
if ((*it)->update(state, ticks))
it = hazards.erase(it);
else
++it;
}
Trace::end("Battle::update::hazards->update");
Trace::start("Battle::update::explosions->update");
Expand Down Expand Up @@ -1844,11 +1846,10 @@ void Battle::updateTBEnd(GameState &state)
Trace::start("Battle::updateTBEnd::hazards->update");
for (auto it = this->hazards.begin(); it != this->hazards.end();)
{
auto d = *it++;
if (d->ownerOrganisation == currentActiveOrganisation)
{
d->updateTB(state);
}
if ((*it)->ownerOrganisation == currentActiveOrganisation && (*it)->updateTB(state))
it = hazards.erase(it);
else
++it;
}
Trace::end("Battle::updateTBEnd::hazards->update");
Trace::start("Battle::updateTBEnd::items->update");
Expand Down
28 changes: 16 additions & 12 deletions game/state/battle/battlehazard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ BattleHazard::BattleHazard(GameState &state, StateRef<DamageType> damageType, bo
randBoundsInclusive(state.rng, (unsigned)0, TICKS_PER_HAZARD_UPDATE);
}

void BattleHazard::die(GameState &state, bool violently)
void BattleHazard::die(GameState &state, bool violently, bool remove)
{
auto this_shared = shared_from_this();
state.current_battle->hazards.erase(this_shared);

if (remove)
{
state.current_battle->hazards.erase(this_shared);
}
tileObject->removeFromMap();
tileObject.reset();

Expand Down Expand Up @@ -215,7 +217,7 @@ bool BattleHazard::expand(GameState &state, const TileMap &map, const Vec3<int>
{
if (replaceWeaker)
{
existingHazard->die(state, false);
existingHazard->die(state, false, true);
}
state.current_battle->placeHazard(
state, ownerOrganisation, ownerUnit, spreadDamageType, {to.x, to.y, to.z},
Expand Down Expand Up @@ -426,7 +428,7 @@ void BattleHazard::applyEffect(GameState &state)
}
}

void BattleHazard::updateInner(GameState &state, unsigned int ticks)
bool BattleHazard::updateInner(GameState &state, unsigned int ticks)
{
nextUpdateTicksAccumulated += ticks;
while (nextUpdateTicksAccumulated >= TICKS_PER_HAZARD_UPDATE)
Expand Down Expand Up @@ -458,8 +460,8 @@ void BattleHazard::updateInner(GameState &state, unsigned int ticks)
}
if (age >= 130)
{
die(state);
return;
die(state, true, false);
return true;
}
}
else
Expand All @@ -476,14 +478,15 @@ void BattleHazard::updateInner(GameState &state, unsigned int ticks)
}
if (age >= lifetime)
{
die(state);
return;
die(state, true, false);
return true;
}
}
}
return false;
}

void BattleHazard::update(GameState &state, unsigned int ticks)
bool BattleHazard::update(GameState &state, unsigned int ticks)
{
bool realTime = state.current_battle->mode == Battle::Mode::RealTime;

Expand All @@ -509,11 +512,12 @@ void BattleHazard::update(GameState &state, unsigned int ticks)

if (realTime)
{
updateInner(state, ticks);
return updateInner(state, ticks);
}
return false;
}

void BattleHazard::updateTB(GameState &state) { updateInner(state, TICKS_PER_TURN); }
bool BattleHazard::updateTB(GameState &state) { return updateInner(state, TICKS_PER_TURN); }

void BattleHazard::updateTileVisionBlock(GameState &state)
{
Expand Down
11 changes: 6 additions & 5 deletions game/state/battle/battlehazard.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ class BattleHazard : public std::enable_shared_from_this<BattleHazard>
bool fireSmoke = false);
void grow(GameState &state);
void applyEffect(GameState &state);
void die(GameState &state, bool violently = true);
void die(GameState &state, bool violently, bool remove);
void updateTileVisionBlock(GameState &state);

void update(GameState &state, unsigned int ticks);
void updateInner(GameState &state, unsigned int ticks);
void updateTB(GameState &state);
// these return true if this hazard has died and needs to be deleted
bool update(GameState &state, unsigned int ticks);
bool updateInner(GameState &state, unsigned int ticks);
bool updateTB(GameState &state);

BattleHazard() = default;
BattleHazard(GameState &state, StateRef<DamageType> damageType, bool delayVisibility = true);
Expand Down Expand Up @@ -129,4 +130,4 @@ Smoke cannot extinguish fire that is burning a scenery, but can extinguish fire
ground.
Walls are apparently immune to fire.
*/
*/

0 comments on commit 5c8e6c4

Please sign in to comment.