From 294dea8d9d9fb6e5df21e33355f6fcbcfe29be32 Mon Sep 17 00:00:00 2001 From: kgd192 <15697480+kgd192@users.noreply.github.com> Date: Thu, 5 Sep 2024 22:18:41 +0200 Subject: [PATCH] Fix Crash due to Agent dying in vehicle Fixes Crash by implementing custom bypass when agent dying is inside a crashing vehicle --- game/state/city/vehicle.cpp | 3 ++- game/state/shared/agent.cpp | 5 +++-- game/state/shared/agent.h | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/game/state/city/vehicle.cpp b/game/state/city/vehicle.cpp index 4c756b212..e55c3d259 100644 --- a/game/state/city/vehicle.cpp +++ b/game/state/city/vehicle.cpp @@ -1903,8 +1903,9 @@ void Vehicle::die(GameState &state, bool silent, StateRef 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) diff --git a/game/state/shared/agent.cpp b/game/state/shared/agent.cpp index c16f258d7..1d83acaff 100644 --- a/game/state/shared/agent.cpp +++ b/game/state/shared/agent.cpp @@ -943,7 +943,7 @@ 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{&state, shared_from_this()}; @@ -951,7 +951,8 @@ void Agent::die(GameState &state, bool silent) 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); } diff --git a/game/state/shared/agent.h b/game/state/shared/agent.h index 616b0f005..de68569ba 100644 --- a/game/state/shared/agent.h +++ b/game/state/shared/agent.h @@ -190,7 +190,7 @@ class Agent : public StateObject, // 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);