diff --git a/src/openrct2/libopenrct2.vcxproj b/src/openrct2/libopenrct2.vcxproj
index ffab9adb9d..c8a2a40a58 100644
--- a/src/openrct2/libopenrct2.vcxproj
+++ b/src/openrct2/libopenrct2.vcxproj
@@ -1122,6 +1122,7 @@
+
diff --git a/src/openrct2/ride/Vehicle.Crash.cpp b/src/openrct2/ride/Vehicle.Crash.cpp
new file mode 100644
index 0000000000..a45dc6f897
--- /dev/null
+++ b/src/openrct2/ride/Vehicle.Crash.cpp
@@ -0,0 +1,585 @@
+/*****************************************************************************
+ * Copyright (c) 2014-2026 OpenRCT2 developers
+ *
+ * For a complete list of all authors, please refer to contributors.md
+ * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
+ *
+ * OpenRCT2 is licensed under the GNU General Public License version 3.
+ *****************************************************************************/
+
+#include "Vehicle.h"
+
+#include "../Context.h"
+#include "../Diagnostic.h"
+#include "../GameState.h"
+#include "../actions/RideSetStatusAction.h"
+#include "../audio/Audio.h"
+#include "../config/Config.h"
+#include "../entity/EntityRegistry.h"
+#include "../entity/Guest.h"
+#include "../entity/Particle.h"
+#include "../entity/Peep.h"
+#include "../localisation/Formatter.h"
+#include "../management/NewsItem.h"
+#include "../scenario/Scenario.h"
+#include "../scripting/HookEngine.h"
+#include "../scripting/ScriptEngine.h"
+#include "../windows/Intent.h"
+#include "../world/Map.h"
+#include "../world/tile_element/TileElement.h"
+#include "Ride.h"
+#include "VehicleGeometry.h"
+
+using namespace OpenRCT2;
+using namespace OpenRCT2::Audio;
+using namespace OpenRCT2::RideVehicle;
+
+#ifdef ENABLE_SCRIPTING
+/**
+ * Fires the "vehicle.crash" api hook
+ * @param vehicleId Entity id of the vehicle that just crashed
+ * @param crashId What the vehicle crashed into. Should be either "another_vehicle", "land", or "water"
+ */
+static void InvokeVehicleCrashHook(const EntityId vehicleId, const std::string_view crashId)
+{
+ auto& hookEngine = GetContext()->GetScriptEngine().GetHookEngine();
+ if (hookEngine.HasSubscriptions(Scripting::HookType::vehicleCrash))
+ {
+ auto ctx = GetContext()->GetScriptEngine().GetContext();
+
+ // Create event args object
+ auto obj = Scripting::DukObject(ctx);
+ obj.Set("id", vehicleId.ToUnderlying());
+ obj.Set("crashIntoType", crashId);
+
+ // Call the subscriptions
+ auto e = obj.Take();
+ hookEngine.Call(Scripting::HookType::vehicleCrash, e, true);
+ }
+}
+#endif
+
+void Vehicle::SimulateCrash() const
+{
+ auto curRide = GetRide();
+ if (curRide != nullptr)
+ {
+ curRide->lifecycleFlags |= RIDE_LIFECYCLE_CRASHED;
+ }
+}
+
+/**
+ * Setup function for a vehicle colliding with
+ * another vehicle.
+ *
+ * rct2: 0x006DA059
+ */
+void Vehicle::UpdateCollisionSetup()
+{
+ auto curRide = GetRide();
+ if (curRide == nullptr)
+ return;
+
+ if (curRide->status == RideStatus::simulating)
+ {
+ SimulateCrash();
+ return;
+ }
+
+ SetState(Status::crashed, sub_state);
+
+ if (!(curRide->lifecycleFlags & RIDE_LIFECYCLE_CRASHED))
+ {
+ auto frontVehicle = GetHead();
+ auto trainIndex = ride_get_train_index_from_vehicle(*curRide, frontVehicle->Id);
+ if (!trainIndex.has_value())
+ {
+ return;
+ }
+
+ curRide->crash(trainIndex.value());
+
+ if (curRide->status != RideStatus::closed)
+ {
+ // We require this to execute right away during the simulation, always ignore network and queue.
+ auto gameAction = GameActions::RideSetStatusAction(curRide->id, RideStatus::closed);
+ GameActions::ExecuteNested(&gameAction, getGameState());
+ }
+ }
+
+ curRide->lifecycleFlags |= RIDE_LIFECYCLE_CRASHED;
+ curRide->windowInvalidateFlags.set(RideInvalidateFlag::main, RideInvalidateFlag::list);
+ KillAllPassengersInTrain();
+
+ Vehicle* lastVehicle = this;
+ for (Vehicle* train = getGameState().entities.GetEntity(Id); train != nullptr;
+ train = getGameState().entities.GetEntity(train->next_vehicle_on_train))
+ {
+ lastVehicle = train;
+
+ train->sub_state = 2;
+
+#ifdef ENABLE_SCRIPTING
+ InvokeVehicleCrashHook(train->Id, "another_vehicle");
+#endif
+ const auto trainLoc = train->GetLocation();
+
+ Play3D(SoundId::crash, trainLoc);
+
+ ExplosionCloud::Create(trainLoc);
+
+ for (int32_t i = 0; i < 10; i++)
+ {
+ VehicleCrashParticle::Create(train->colours, trainLoc);
+ }
+
+ train->SetFlag(VehicleFlags::Crashed);
+ train->animationState = ScenarioRand() & 0xFFFF;
+
+ train->animation_frame = ScenarioRand() & 0x7;
+ train->SpriteData.Width = 13;
+ train->SpriteData.HeightMin = 45;
+ train->SpriteData.HeightMax = 5;
+
+ train->MoveTo(trainLoc);
+
+ train->SwingSpeed = 0;
+ }
+
+ // Remove the current train from the ride linked list of trains
+ auto prevTrain = getGameState().entities.GetEntity(prev_vehicle_on_ride);
+ auto nextTrain = getGameState().entities.GetEntity(lastVehicle->next_vehicle_on_ride);
+ if (prevTrain == nullptr || nextTrain == nullptr)
+ {
+ LOG_ERROR("Corrupted vehicle list for ride!");
+ }
+ else
+ {
+ prevTrain->next_vehicle_on_ride = lastVehicle->next_vehicle_on_ride;
+ nextTrain->prev_vehicle_on_ride = prev_vehicle_on_ride;
+ }
+
+ velocity = 0;
+}
+
+/**
+ *
+ * rct2: 0x006D9EFE
+ */
+void Vehicle::UpdateCrashSetup()
+{
+ auto curRide = GetRide();
+ if (curRide != nullptr && curRide->status == RideStatus::simulating)
+ {
+ SimulateCrash();
+ return;
+ }
+ SetState(Status::crashing, sub_state);
+
+ if (NumPeepsUntilTrainTail() != 0)
+ {
+ Play3D(SoundId::hauntedHouseScream2, GetLocation());
+ }
+
+ int32_t edx = velocity >> 10;
+
+ Vehicle* lastVehicle = this;
+ auto spriteId = Id;
+ for (Vehicle* trainVehicle; !spriteId.IsNull(); spriteId = trainVehicle->next_vehicle_on_train)
+ {
+ trainVehicle = getGameState().entities.GetEntity(spriteId);
+ if (trainVehicle == nullptr)
+ {
+ break;
+ }
+ lastVehicle = trainVehicle;
+
+ trainVehicle->sub_state = 0;
+ auto crashDirection = Geometry::getCrashDirectionComponents(trainVehicle->Orientation);
+ int32_t trainX = crashDirection.x;
+ int32_t trainY = crashDirection.y;
+
+ auto carLaunchDirection = Geometry::getPitchVector32(trainVehicle->pitch);
+
+ auto trainZ = carLaunchDirection.y >> 23;
+ int32_t ecx = carLaunchDirection.x >> 15;
+ trainX *= ecx;
+ trainY *= ecx;
+ trainX >>= 16;
+ trainY >>= 16;
+ trainX *= edx;
+ trainY *= edx;
+ trainZ *= edx;
+ trainX >>= 8;
+ trainY >>= 8;
+ trainZ >>= 8;
+
+ trainVehicle->crash_x = trainX;
+ trainVehicle->crash_y = trainY;
+ trainVehicle->crash_z = trainZ;
+ trainVehicle->crash_x += (ScenarioRand() & 0xF) - 8;
+ trainVehicle->crash_y += (ScenarioRand() & 0xF) - 8;
+ trainVehicle->crash_z += (ScenarioRand() & 0xF) - 8;
+
+ trainVehicle->TrackLocation = { 0, 0, 0 };
+ }
+
+ // Remove the current train from the ride linked list of trains
+ auto prevTrain = getGameState().entities.GetEntity(prev_vehicle_on_ride);
+ auto nextTrain = getGameState().entities.GetEntity(lastVehicle->next_vehicle_on_ride);
+ if (prevTrain == nullptr || nextTrain == nullptr)
+ {
+ LOG_ERROR("Corrupted vehicle list for ride!");
+ }
+ else
+ {
+ prevTrain->next_vehicle_on_ride = lastVehicle->next_vehicle_on_ride;
+ nextTrain->prev_vehicle_on_ride = prev_vehicle_on_ride;
+ }
+ velocity = 0;
+}
+
+/**
+ *
+ * rct2: 0x0068B8BD
+ * @returns the map element that the vehicle will collide with or NULL if no collisions.
+ */
+static TileElement* vehicle_check_collision(const CoordsXYZ& vehiclePosition)
+{
+ TileElement* tileElement = MapGetFirstElementAt(vehiclePosition);
+ if (tileElement == nullptr)
+ {
+ return nullptr;
+ }
+
+ uint8_t quadrant;
+ if ((vehiclePosition.x & 0x1F) >= 16)
+ {
+ quadrant = 1;
+ if ((vehiclePosition.y & 0x1F) < 16)
+ quadrant = 2;
+ }
+ else
+ {
+ quadrant = 4;
+ if ((vehiclePosition.y & 0x1F) >= 16)
+ quadrant = 8;
+ }
+
+ do
+ {
+ if (vehiclePosition.z < tileElement->GetBaseZ())
+ continue;
+
+ if (vehiclePosition.z >= tileElement->GetClearanceZ())
+ continue;
+
+ if (tileElement->GetOccupiedQuadrants() & quadrant)
+ return tileElement;
+ } while (!(tileElement++)->IsLastForTile());
+
+ return nullptr;
+}
+
+static void ride_train_crash(Ride& ride, uint16_t numFatalities)
+{
+ Formatter ft;
+ ft.Add(numFatalities);
+
+ uint8_t crashType = numFatalities == 0 ? RIDE_CRASH_TYPE_NO_FATALITIES : RIDE_CRASH_TYPE_FATALITIES;
+
+ if (crashType >= ride.lastCrashType)
+ ride.lastCrashType = crashType;
+
+ if (numFatalities != 0)
+ {
+ if (Config::Get().notifications.rideCasualties)
+ {
+ ride.formatNameTo(ft);
+ News::AddItemToQueue(
+ News::ItemType::ride, numFatalities == 1 ? STR_X_PERSON_DIED_ON_X : STR_X_PEOPLE_DIED_ON_X,
+ ride.id.ToUnderlying(), ft);
+ }
+
+ // TODO: get park id from ride/vehicle
+ auto& park = getGameState().park;
+ if (park.ratingCasualtyPenalty < 500)
+ {
+ park.ratingCasualtyPenalty += 200;
+ }
+ }
+}
+/**
+ *
+ * rct2: 0x006DE6C6
+ */
+void Vehicle::KillAllPassengersInTrain()
+{
+ auto curRide = GetRide();
+ if (curRide == nullptr)
+ return;
+
+ ride_train_crash(*curRide, NumPeepsUntilTrainTail());
+
+ for (Vehicle* trainCar = getGameState().entities.GetEntity(Id); trainCar != nullptr;
+ trainCar = getGameState().entities.GetEntity(trainCar->next_vehicle_on_train))
+ {
+ trainCar->KillPassengers(*curRide);
+ }
+}
+
+void Vehicle::KillPassengers(const Ride& curRide)
+{
+ if (num_peeps != next_free_seat)
+ return;
+
+ if (num_peeps == 0)
+ return;
+
+ for (auto i = 0; i < num_peeps; i++)
+ {
+ auto* curPeep = getGameState().entities.GetEntity(peep[i]);
+ if (curPeep == nullptr)
+ continue;
+
+ if (!curPeep->OutsideOfPark)
+ {
+ DecrementGuestsInPark();
+ auto intent = Intent(INTENT_ACTION_UPDATE_GUEST_COUNT);
+ ContextBroadcastIntent(&intent);
+ }
+ PeepEntityRemove(curPeep);
+ }
+
+ num_peeps = 0;
+ next_free_seat = 0;
+}
+
+void Vehicle::CrashOnLand()
+{
+ auto curRide = GetRide();
+ if (curRide == nullptr)
+ return;
+
+ if (curRide->status == RideStatus::simulating)
+ {
+ SimulateCrash();
+ return;
+ }
+ SetState(Status::crashed, sub_state);
+
+#ifdef ENABLE_SCRIPTING
+ InvokeVehicleCrashHook(Id, "land");
+#endif
+
+ if (!(curRide->lifecycleFlags & RIDE_LIFECYCLE_CRASHED))
+ {
+ auto frontVehicle = GetHead();
+ auto trainIndex = ride_get_train_index_from_vehicle(*curRide, frontVehicle->Id);
+ if (!trainIndex.has_value())
+ {
+ return;
+ }
+
+ curRide->crash(trainIndex.value());
+
+ if (curRide->status != RideStatus::closed)
+ {
+ // We require this to execute right away during the simulation, always ignore network and queue.
+ auto gameAction = GameActions::RideSetStatusAction(curRide->id, RideStatus::closed);
+ GameActions::ExecuteNested(&gameAction, getGameState());
+ }
+ }
+ curRide->lifecycleFlags |= RIDE_LIFECYCLE_CRASHED;
+ curRide->windowInvalidateFlags.set(RideInvalidateFlag::main, RideInvalidateFlag::list);
+
+ if (IsHead())
+ {
+ KillAllPassengersInTrain();
+ }
+
+ sub_state = 2;
+
+ const auto curLoc = GetLocation();
+ Play3D(SoundId::crash, curLoc);
+
+ ExplosionCloud::Create(curLoc);
+ ExplosionFlare::Create(curLoc);
+
+ uint8_t numParticles = std::min(SpriteData.Width, static_cast(7));
+
+ while (numParticles-- != 0)
+ VehicleCrashParticle::Create(colours, curLoc);
+
+ SetFlag(VehicleFlags::Crashed);
+ animation_frame = 0;
+ animationState = 0;
+ SpriteData.Width = 13;
+ SpriteData.HeightMin = 45;
+ SpriteData.HeightMax = 5;
+
+ MoveTo(curLoc);
+
+ crash_z = 0;
+}
+
+void Vehicle::CrashOnWater()
+{
+ auto curRide = GetRide();
+ if (curRide == nullptr)
+ return;
+
+ if (curRide->status == RideStatus::simulating)
+ {
+ SimulateCrash();
+ return;
+ }
+ SetState(Status::crashed, sub_state);
+
+#ifdef ENABLE_SCRIPTING
+ InvokeVehicleCrashHook(Id, "water");
+#endif
+
+ if (!(curRide->lifecycleFlags & RIDE_LIFECYCLE_CRASHED))
+ {
+ auto frontVehicle = GetHead();
+ auto trainIndex = ride_get_train_index_from_vehicle(*curRide, frontVehicle->Id);
+ if (!trainIndex.has_value())
+ {
+ return;
+ }
+
+ curRide->crash(trainIndex.value());
+
+ if (curRide->status != RideStatus::closed)
+ {
+ // We require this to execute right away during the simulation, always ignore network and queue.
+ auto gameAction = GameActions::RideSetStatusAction(curRide->id, RideStatus::closed);
+ GameActions::ExecuteNested(&gameAction, getGameState());
+ }
+ }
+ curRide->lifecycleFlags |= RIDE_LIFECYCLE_CRASHED;
+ curRide->windowInvalidateFlags.set(RideInvalidateFlag::main, RideInvalidateFlag::list);
+
+ if (IsHead())
+ {
+ KillAllPassengersInTrain();
+ }
+
+ sub_state = 2;
+
+ const auto curLoc = GetLocation();
+ Play3D(SoundId::water1, curLoc);
+
+ CrashSplashParticle::Create(curLoc);
+ CrashSplashParticle::Create(curLoc + CoordsXYZ{ -8, -9, 0 });
+ CrashSplashParticle::Create(curLoc + CoordsXYZ{ 11, -9, 0 });
+ CrashSplashParticle::Create(curLoc + CoordsXYZ{ 11, 8, 0 });
+ CrashSplashParticle::Create(curLoc + CoordsXYZ{ -4, 8, 0 });
+
+ for (int32_t i = 0; i < 10; ++i)
+ VehicleCrashParticle::Create(colours, curLoc + CoordsXYZ{ -4, 8, 0 });
+
+ SetFlag(VehicleFlags::Crashed);
+ animation_frame = 0;
+ animationState = 0;
+ SpriteData.Width = 13;
+ SpriteData.HeightMin = 45;
+ SpriteData.HeightMax = 5;
+
+ MoveTo(curLoc);
+
+ crash_z = -1;
+}
+
+/**
+ *
+ * rct2: 0x006D98CA
+ */
+void Vehicle::UpdateCrash()
+{
+ for (Vehicle* curVehicle = getGameState().entities.GetEntity(Id); curVehicle != nullptr;
+ curVehicle = getGameState().entities.GetEntity(curVehicle->next_vehicle_on_train))
+ {
+ CoordsXYZ curPos = curVehicle->GetLocation();
+
+ if (curVehicle->sub_state > 1)
+ {
+ if (curVehicle->crash_z <= 96)
+ {
+ curVehicle->crash_z++;
+ if ((ScenarioRand() & 0xFFFF) <= 0x1555)
+ {
+ int32_t xOffset = (ScenarioRand() & 2) - 1;
+ int32_t yOffset = (ScenarioRand() & 2) - 1;
+
+ ExplosionCloud::Create(curPos + CoordsXYZ{ xOffset, yOffset, 0 });
+ }
+ }
+ if (curVehicle->animationState <= 0xe388)
+ {
+ curVehicle->animationState += 0x1c71;
+ }
+ else
+ {
+ curVehicle->animationState = 0;
+ curVehicle->animation_frame++;
+ if (curVehicle->animation_frame >= 8)
+ curVehicle->animation_frame = 0;
+ curVehicle->Invalidate();
+ }
+ continue;
+ }
+
+ TileElement* collideElement = vehicle_check_collision(curPos);
+ if (collideElement == nullptr)
+ {
+ curVehicle->sub_state = 1;
+ }
+ else if (curVehicle->sub_state == 1)
+ {
+ curVehicle->CrashOnLand();
+ continue;
+ }
+
+ int16_t height = TileElementHeight(curPos);
+ int16_t waterHeight = TileElementWaterHeight(curPos);
+ int16_t zDiff;
+ if (waterHeight != 0)
+ {
+ zDiff = curPos.z - waterHeight;
+ if (zDiff <= 0 && zDiff >= -20)
+ {
+ curVehicle->CrashOnWater();
+ continue;
+ }
+ }
+
+ zDiff = curPos.z - height;
+ if ((zDiff <= 0 && zDiff >= -20) || curPos.z < 16)
+ {
+ curVehicle->CrashOnLand();
+ continue;
+ }
+
+ curVehicle->Invalidate();
+
+ curPos.x += static_cast(curVehicle->crash_x >> 8);
+ curPos.y += static_cast(curVehicle->crash_y >> 8);
+ curPos.z += static_cast(curVehicle->crash_z >> 8);
+ curVehicle->TrackLocation = { (curVehicle->crash_x << 8), (curVehicle->crash_y << 8), (curVehicle->crash_z << 8) };
+
+ if (!MapIsLocationValid(curPos))
+ {
+ curVehicle->CrashOnLand();
+ continue;
+ }
+
+ curVehicle->MoveTo(curPos);
+
+ if (curVehicle->sub_state == 1)
+ {
+ curVehicle->crash_z -= 20;
+ }
+ }
+}
diff --git a/src/openrct2/ride/Vehicle.cpp b/src/openrct2/ride/Vehicle.cpp
index 3f2c904eec..ee2453e3e6 100644
--- a/src/openrct2/ride/Vehicle.cpp
+++ b/src/openrct2/ride/Vehicle.cpp
@@ -133,31 +133,6 @@ bool EntityBase::Is() const
return Type == EntityType::vehicle;
}
-#ifdef ENABLE_SCRIPTING
-/**
- * Fires the "vehicle.crash" api hook
- * @param vehicleId Entity id of the vehicle that just crashed
- * @param crashId What the vehicle crashed into. Should be either "another_vehicle", "land", or "water"
- */
-static void InvokeVehicleCrashHook(const EntityId vehicleId, const std::string_view crashId)
-{
- auto& hookEngine = GetContext()->GetScriptEngine().GetHookEngine();
- if (hookEngine.HasSubscriptions(Scripting::HookType::vehicleCrash))
- {
- auto ctx = GetContext()->GetScriptEngine().GetContext();
-
- // Create event args object
- auto obj = Scripting::DukObject(ctx);
- obj.Set("id", vehicleId.ToUnderlying());
- obj.Set("crashIntoType", crashId);
-
- // Call the subscriptions
- auto e = obj.Take();
- hookEngine.Call(Scripting::HookType::vehicleCrash, e, true);
- }
-}
-#endif
-
static bool vehicle_move_info_valid(
VehicleTrackSubposition trackSubposition, TrackElemType type, uint8_t direction, int32_t offset)
{
@@ -1158,7 +1133,7 @@ void Vehicle::TrainReadyToDepart(uint8_t num_peeps_on_train, uint8_t num_used_se
SetState(Status::waitingForPassengers);
}
-static std::optional ride_get_train_index_from_vehicle(const Ride& ride, EntityId spriteIndex)
+std::optional ride_get_train_index_from_vehicle(const Ride& ride, EntityId spriteIndex)
{
uint32_t trainIndex = 0;
while (ride.vehicles[trainIndex] != spriteIndex)
@@ -2408,186 +2383,6 @@ void Vehicle::CheckIfMissing()
}
}
-void Vehicle::SimulateCrash() const
-{
- auto curRide = GetRide();
- if (curRide != nullptr)
- {
- curRide->lifecycleFlags |= RIDE_LIFECYCLE_CRASHED;
- }
-}
-
-/**
- * Setup function for a vehicle colliding with
- * another vehicle.
- *
- * rct2: 0x006DA059
- */
-void Vehicle::UpdateCollisionSetup()
-{
- auto curRide = GetRide();
- if (curRide == nullptr)
- return;
-
- if (curRide->status == RideStatus::simulating)
- {
- SimulateCrash();
- return;
- }
-
- SetState(Status::crashed, sub_state);
-
- if (!(curRide->lifecycleFlags & RIDE_LIFECYCLE_CRASHED))
- {
- auto frontVehicle = GetHead();
- auto trainIndex = ride_get_train_index_from_vehicle(*curRide, frontVehicle->Id);
- if (!trainIndex.has_value())
- {
- return;
- }
-
- curRide->crash(trainIndex.value());
-
- if (curRide->status != RideStatus::closed)
- {
- // We require this to execute right away during the simulation, always ignore network and queue.
- auto gameAction = GameActions::RideSetStatusAction(curRide->id, RideStatus::closed);
- GameActions::ExecuteNested(&gameAction, getGameState());
- }
- }
-
- curRide->lifecycleFlags |= RIDE_LIFECYCLE_CRASHED;
- curRide->windowInvalidateFlags.set(RideInvalidateFlag::main, RideInvalidateFlag::list);
- KillAllPassengersInTrain();
-
- Vehicle* lastVehicle = this;
- for (Vehicle* train = getGameState().entities.GetEntity(Id); train != nullptr;
- train = getGameState().entities.GetEntity(train->next_vehicle_on_train))
- {
- lastVehicle = train;
-
- train->sub_state = 2;
-
-#ifdef ENABLE_SCRIPTING
- InvokeVehicleCrashHook(train->Id, "another_vehicle");
-#endif
- const auto trainLoc = train->GetLocation();
-
- Play3D(SoundId::crash, trainLoc);
-
- ExplosionCloud::Create(trainLoc);
-
- for (int32_t i = 0; i < 10; i++)
- {
- VehicleCrashParticle::Create(train->colours, trainLoc);
- }
-
- train->SetFlag(VehicleFlags::Crashed);
- train->animationState = ScenarioRand() & 0xFFFF;
-
- train->animation_frame = ScenarioRand() & 0x7;
- train->SpriteData.Width = 13;
- train->SpriteData.HeightMin = 45;
- train->SpriteData.HeightMax = 5;
-
- train->MoveTo(trainLoc);
-
- train->SwingSpeed = 0;
- }
-
- // Remove the current train from the ride linked list of trains
- auto prevTrain = getGameState().entities.GetEntity(prev_vehicle_on_ride);
- auto nextTrain = getGameState().entities.GetEntity(lastVehicle->next_vehicle_on_ride);
- if (prevTrain == nullptr || nextTrain == nullptr)
- {
- LOG_ERROR("Corrupted vehicle list for ride!");
- }
- else
- {
- prevTrain->next_vehicle_on_ride = lastVehicle->next_vehicle_on_ride;
- nextTrain->prev_vehicle_on_ride = prev_vehicle_on_ride;
- }
-
- velocity = 0;
-}
-
-/**
- *
- * rct2: 0x006D9EFE
- */
-void Vehicle::UpdateCrashSetup()
-{
- auto curRide = GetRide();
- if (curRide != nullptr && curRide->status == RideStatus::simulating)
- {
- SimulateCrash();
- return;
- }
- SetState(Status::crashing, sub_state);
-
- if (NumPeepsUntilTrainTail() != 0)
- {
- Play3D(SoundId::hauntedHouseScream2, GetLocation());
- }
-
- int32_t edx = velocity >> 10;
-
- Vehicle* lastVehicle = this;
- auto spriteId = Id;
- for (Vehicle* trainVehicle; !spriteId.IsNull(); spriteId = trainVehicle->next_vehicle_on_train)
- {
- trainVehicle = getGameState().entities.GetEntity(spriteId);
- if (trainVehicle == nullptr)
- {
- break;
- }
- lastVehicle = trainVehicle;
-
- trainVehicle->sub_state = 0;
- auto crashDirection = Geometry::getCrashDirectionComponents(trainVehicle->Orientation);
- int32_t trainX = crashDirection.x;
- int32_t trainY = crashDirection.y;
-
- auto carLaunchDirection = Geometry::getPitchVector32(trainVehicle->pitch);
-
- auto trainZ = carLaunchDirection.y >> 23;
- int32_t ecx = carLaunchDirection.x >> 15;
- trainX *= ecx;
- trainY *= ecx;
- trainX >>= 16;
- trainY >>= 16;
- trainX *= edx;
- trainY *= edx;
- trainZ *= edx;
- trainX >>= 8;
- trainY >>= 8;
- trainZ >>= 8;
-
- trainVehicle->crash_x = trainX;
- trainVehicle->crash_y = trainY;
- trainVehicle->crash_z = trainZ;
- trainVehicle->crash_x += (ScenarioRand() & 0xF) - 8;
- trainVehicle->crash_y += (ScenarioRand() & 0xF) - 8;
- trainVehicle->crash_z += (ScenarioRand() & 0xF) - 8;
-
- trainVehicle->TrackLocation = { 0, 0, 0 };
- }
-
- // Remove the current train from the ride linked list of trains
- auto prevTrain = getGameState().entities.GetEntity(prev_vehicle_on_ride);
- auto nextTrain = getGameState().entities.GetEntity(lastVehicle->next_vehicle_on_ride);
- if (prevTrain == nullptr || nextTrain == nullptr)
- {
- LOG_ERROR("Corrupted vehicle list for ride!");
- }
- else
- {
- prevTrain->next_vehicle_on_ride = lastVehicle->next_vehicle_on_ride;
- nextTrain->prev_vehicle_on_ride = prev_vehicle_on_ride;
- }
- velocity = 0;
-}
-
/**
*
* rct2: 0x006D8937
@@ -3668,350 +3463,6 @@ void Vehicle::UpdateDoingCircusShow()
}
}
-/**
- *
- * rct2: 0x0068B8BD
- * @returns the map element that the vehicle will collide with or NULL if no collisions.
- */
-static TileElement* vehicle_check_collision(const CoordsXYZ& vehiclePosition)
-{
- TileElement* tileElement = MapGetFirstElementAt(vehiclePosition);
- if (tileElement == nullptr)
- {
- return nullptr;
- }
-
- uint8_t quadrant;
- if ((vehiclePosition.x & 0x1F) >= 16)
- {
- quadrant = 1;
- if ((vehiclePosition.y & 0x1F) < 16)
- quadrant = 2;
- }
- else
- {
- quadrant = 4;
- if ((vehiclePosition.y & 0x1F) >= 16)
- quadrant = 8;
- }
-
- do
- {
- if (vehiclePosition.z < tileElement->GetBaseZ())
- continue;
-
- if (vehiclePosition.z >= tileElement->GetClearanceZ())
- continue;
-
- if (tileElement->GetOccupiedQuadrants() & quadrant)
- return tileElement;
- } while (!(tileElement++)->IsLastForTile());
-
- return nullptr;
-}
-
-static void ride_train_crash(Ride& ride, uint16_t numFatalities)
-{
- Formatter ft;
- ft.Add(numFatalities);
-
- uint8_t crashType = numFatalities == 0 ? RIDE_CRASH_TYPE_NO_FATALITIES : RIDE_CRASH_TYPE_FATALITIES;
-
- if (crashType >= ride.lastCrashType)
- ride.lastCrashType = crashType;
-
- if (numFatalities != 0)
- {
- if (Config::Get().notifications.rideCasualties)
- {
- ride.formatNameTo(ft);
- News::AddItemToQueue(
- News::ItemType::ride, numFatalities == 1 ? STR_X_PERSON_DIED_ON_X : STR_X_PEOPLE_DIED_ON_X,
- ride.id.ToUnderlying(), ft);
- }
-
- // TODO: get park id from ride/vehicle
- auto& park = getGameState().park;
- if (park.ratingCasualtyPenalty < 500)
- {
- park.ratingCasualtyPenalty += 200;
- }
- }
-}
-/**
- *
- * rct2: 0x006DE6C6
- */
-void Vehicle::KillAllPassengersInTrain()
-{
- auto curRide = GetRide();
- if (curRide == nullptr)
- return;
-
- ride_train_crash(*curRide, NumPeepsUntilTrainTail());
-
- for (Vehicle* trainCar = getGameState().entities.GetEntity(Id); trainCar != nullptr;
- trainCar = getGameState().entities.GetEntity(trainCar->next_vehicle_on_train))
- {
- trainCar->KillPassengers(*curRide);
- }
-}
-
-void Vehicle::KillPassengers(const Ride& curRide)
-{
- if (num_peeps != next_free_seat)
- return;
-
- if (num_peeps == 0)
- return;
-
- for (auto i = 0; i < num_peeps; i++)
- {
- auto* curPeep = getGameState().entities.GetEntity(peep[i]);
- if (curPeep == nullptr)
- continue;
-
- if (!curPeep->OutsideOfPark)
- {
- DecrementGuestsInPark();
- auto intent = Intent(INTENT_ACTION_UPDATE_GUEST_COUNT);
- ContextBroadcastIntent(&intent);
- }
- PeepEntityRemove(curPeep);
- }
-
- num_peeps = 0;
- next_free_seat = 0;
-}
-
-void Vehicle::CrashOnLand()
-{
- auto curRide = GetRide();
- if (curRide == nullptr)
- return;
-
- if (curRide->status == RideStatus::simulating)
- {
- SimulateCrash();
- return;
- }
- SetState(Status::crashed, sub_state);
-
-#ifdef ENABLE_SCRIPTING
- InvokeVehicleCrashHook(Id, "land");
-#endif
-
- if (!(curRide->lifecycleFlags & RIDE_LIFECYCLE_CRASHED))
- {
- auto frontVehicle = GetHead();
- auto trainIndex = ride_get_train_index_from_vehicle(*curRide, frontVehicle->Id);
- if (!trainIndex.has_value())
- {
- return;
- }
-
- curRide->crash(trainIndex.value());
-
- if (curRide->status != RideStatus::closed)
- {
- // We require this to execute right away during the simulation, always ignore network and queue.
- auto gameAction = GameActions::RideSetStatusAction(curRide->id, RideStatus::closed);
- GameActions::ExecuteNested(&gameAction, getGameState());
- }
- }
- curRide->lifecycleFlags |= RIDE_LIFECYCLE_CRASHED;
- curRide->windowInvalidateFlags.set(RideInvalidateFlag::main, RideInvalidateFlag::list);
-
- if (IsHead())
- {
- KillAllPassengersInTrain();
- }
-
- sub_state = 2;
-
- const auto curLoc = GetLocation();
- Play3D(SoundId::crash, curLoc);
-
- ExplosionCloud::Create(curLoc);
- ExplosionFlare::Create(curLoc);
-
- uint8_t numParticles = std::min(SpriteData.Width, static_cast(7));
-
- while (numParticles-- != 0)
- VehicleCrashParticle::Create(colours, curLoc);
-
- SetFlag(VehicleFlags::Crashed);
- animation_frame = 0;
- animationState = 0;
- SpriteData.Width = 13;
- SpriteData.HeightMin = 45;
- SpriteData.HeightMax = 5;
-
- MoveTo(curLoc);
-
- crash_z = 0;
-}
-
-void Vehicle::CrashOnWater()
-{
- auto curRide = GetRide();
- if (curRide == nullptr)
- return;
-
- if (curRide->status == RideStatus::simulating)
- {
- SimulateCrash();
- return;
- }
- SetState(Status::crashed, sub_state);
-
-#ifdef ENABLE_SCRIPTING
- InvokeVehicleCrashHook(Id, "water");
-#endif
-
- if (!(curRide->lifecycleFlags & RIDE_LIFECYCLE_CRASHED))
- {
- auto frontVehicle = GetHead();
- auto trainIndex = ride_get_train_index_from_vehicle(*curRide, frontVehicle->Id);
- if (!trainIndex.has_value())
- {
- return;
- }
-
- curRide->crash(trainIndex.value());
-
- if (curRide->status != RideStatus::closed)
- {
- // We require this to execute right away during the simulation, always ignore network and queue.
- auto gameAction = GameActions::RideSetStatusAction(curRide->id, RideStatus::closed);
- GameActions::ExecuteNested(&gameAction, getGameState());
- }
- }
- curRide->lifecycleFlags |= RIDE_LIFECYCLE_CRASHED;
- curRide->windowInvalidateFlags.set(RideInvalidateFlag::main, RideInvalidateFlag::list);
-
- if (IsHead())
- {
- KillAllPassengersInTrain();
- }
-
- sub_state = 2;
-
- const auto curLoc = GetLocation();
- Play3D(SoundId::water1, curLoc);
-
- CrashSplashParticle::Create(curLoc);
- CrashSplashParticle::Create(curLoc + CoordsXYZ{ -8, -9, 0 });
- CrashSplashParticle::Create(curLoc + CoordsXYZ{ 11, -9, 0 });
- CrashSplashParticle::Create(curLoc + CoordsXYZ{ 11, 8, 0 });
- CrashSplashParticle::Create(curLoc + CoordsXYZ{ -4, 8, 0 });
-
- for (int32_t i = 0; i < 10; ++i)
- VehicleCrashParticle::Create(colours, curLoc + CoordsXYZ{ -4, 8, 0 });
-
- SetFlag(VehicleFlags::Crashed);
- animation_frame = 0;
- animationState = 0;
- SpriteData.Width = 13;
- SpriteData.HeightMin = 45;
- SpriteData.HeightMax = 5;
-
- MoveTo(curLoc);
-
- crash_z = -1;
-}
-
-/**
- *
- * rct2: 0x006D98CA
- */
-void Vehicle::UpdateCrash()
-{
- for (Vehicle* curVehicle = getGameState().entities.GetEntity(Id); curVehicle != nullptr;
- curVehicle = getGameState().entities.GetEntity(curVehicle->next_vehicle_on_train))
- {
- CoordsXYZ curPos = curVehicle->GetLocation();
-
- if (curVehicle->sub_state > 1)
- {
- if (curVehicle->crash_z <= 96)
- {
- curVehicle->crash_z++;
- if ((ScenarioRand() & 0xFFFF) <= 0x1555)
- {
- int32_t xOffset = (ScenarioRand() & 2) - 1;
- int32_t yOffset = (ScenarioRand() & 2) - 1;
-
- ExplosionCloud::Create(curPos + CoordsXYZ{ xOffset, yOffset, 0 });
- }
- }
- if (curVehicle->animationState <= 0xe388)
- {
- curVehicle->animationState += 0x1c71;
- }
- else
- {
- curVehicle->animationState = 0;
- curVehicle->animation_frame++;
- if (curVehicle->animation_frame >= 8)
- curVehicle->animation_frame = 0;
- curVehicle->Invalidate();
- }
- continue;
- }
-
- TileElement* collideElement = vehicle_check_collision(curPos);
- if (collideElement == nullptr)
- {
- curVehicle->sub_state = 1;
- }
- else if (curVehicle->sub_state == 1)
- {
- curVehicle->CrashOnLand();
- continue;
- }
-
- int16_t height = TileElementHeight(curPos);
- int16_t waterHeight = TileElementWaterHeight(curPos);
- int16_t zDiff;
- if (waterHeight != 0)
- {
- zDiff = curPos.z - waterHeight;
- if (zDiff <= 0 && zDiff >= -20)
- {
- curVehicle->CrashOnWater();
- continue;
- }
- }
-
- zDiff = curPos.z - height;
- if ((zDiff <= 0 && zDiff >= -20) || curPos.z < 16)
- {
- curVehicle->CrashOnLand();
- continue;
- }
-
- curVehicle->Invalidate();
-
- curPos.x += static_cast(curVehicle->crash_x >> 8);
- curPos.y += static_cast(curVehicle->crash_y >> 8);
- curPos.z += static_cast(curVehicle->crash_z >> 8);
- curVehicle->TrackLocation = { (curVehicle->crash_x << 8), (curVehicle->crash_y << 8), (curVehicle->crash_z << 8) };
-
- if (!MapIsLocationValid(curPos))
- {
- curVehicle->CrashOnLand();
- continue;
- }
-
- curVehicle->MoveTo(curPos);
-
- if (curVehicle->sub_state == 1)
- {
- curVehicle->crash_z -= 20;
- }
- }
-}
/**
*
* rct2: 0x006D7888
diff --git a/src/openrct2/ride/Vehicle.h b/src/openrct2/ride/Vehicle.h
index 6d2a98960d..f777966111 100644
--- a/src/openrct2/ride/Vehicle.h
+++ b/src/openrct2/ride/Vehicle.h
@@ -554,6 +554,7 @@ constexpr uint8_t kVehicleSeatNumMask = 0x7F;
Vehicle* TryGetVehicle(EntityId spriteIndex);
void VehicleUpdateAll();
void VehicleSoundsUpdate();
+std::optional ride_get_train_index_from_vehicle(const Ride& ride, EntityId spriteIndex);
uint16_t VehicleGetMoveInfoSize(VehicleTrackSubposition trackSubposition, OpenRCT2::TrackElemType type, uint8_t direction);
void RideUpdateMeasurementsSpecialElements_Default(Ride& ride, OpenRCT2::TrackElemType trackType);