Skip to content

Commit

Permalink
Fix Crash due to Agent dying in vehicle
Browse files Browse the repository at this point in the history
Fixes Crash by implementing custom bypass when agent dying is inside a crashing vehicle
  • Loading branch information
kgd192 committed Sep 5, 2024
1 parent 667eea9 commit 294dea8
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
3 changes: 2 additions & 1 deletion game/state/city/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1903,8 +1903,9 @@ void Vehicle::die(GameState &state, bool silent, StateRef<Vehicle> attacker)
// Dying will remove agent from current agents list
for (auto agent : currentAgents)
{
agent->die(state, true);
agent->die(state, true, true);
}
currentAgents.clear();

// Adjust relationships
if (attacker && !crashed)
Expand Down
5 changes: 3 additions & 2 deletions game/state/shared/agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -943,15 +943,16 @@ bool Agent::getNewGoal(GameState &state)
return acquired;
}

void Agent::die(GameState &state, bool silent)
void Agent::die(GameState &state, bool silent, bool vehicleCrash)
{
auto thisRef = StateRef<Agent>{&state, shared_from_this()};

// Set health to zero so agent will die on next update
modified_stats.health = 0;

// Remove from vehicle
if (currentVehicle)
// Dont do while dying from a vehicle crash to prevent iterator invalidation in vehicle.cpp
if (currentVehicle && !vehicleCrash)
{
currentVehicle->currentAgents.erase(thisRef);
}
Expand Down
2 changes: 1 addition & 1 deletion game/state/shared/agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class Agent : public StateObject<Agent>,
// Get new goal for vehicle position or facing
bool getNewGoal(GameState &state);

void die(GameState &state, bool silent = false);
void die(GameState &state, bool silent = false, bool vehicleCrash = false);
bool isDead() const;
void handleDeath(GameState &state);

Expand Down

0 comments on commit 294dea8

Please sign in to comment.