From 7a7a4ebb49ef592a534356263487ae3bef0dbf9b Mon Sep 17 00:00:00 2001 From: frozensnowy Date: Wed, 11 Feb 2026 20:57:28 +0100 Subject: [PATCH 1/6] Refactor: Move track motion logic to Vehicle.TrackMotion.cpp Moves velocity updates, forward and backward motion and block brake functions to a dedicated file --- src/openrct2/libopenrct2.vcxproj | 1 + src/openrct2/ride/Vehicle.TrackMotion.cpp | 1631 +++++++++++++++++++++ src/openrct2/ride/Vehicle.cpp | 1581 -------------------- 3 files changed, 1632 insertions(+), 1581 deletions(-) create mode 100644 src/openrct2/ride/Vehicle.TrackMotion.cpp diff --git a/src/openrct2/libopenrct2.vcxproj b/src/openrct2/libopenrct2.vcxproj index 3f70607753..17c8ef39aa 100644 --- a/src/openrct2/libopenrct2.vcxproj +++ b/src/openrct2/libopenrct2.vcxproj @@ -1135,6 +1135,7 @@ + diff --git a/src/openrct2/ride/Vehicle.TrackMotion.cpp b/src/openrct2/ride/Vehicle.TrackMotion.cpp new file mode 100644 index 0000000000..72dbb644b3 --- /dev/null +++ b/src/openrct2/ride/Vehicle.TrackMotion.cpp @@ -0,0 +1,1631 @@ +/***************************************************************************** + * 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 "../Diagnostic.h" +#include "../GameState.h" +#include "../audio/Audio.h" +#include "../core/Speed.hpp" +#include "../entity/EntityRegistry.h" +#include "../entity/EntityTweener.h" +#include "../scenario/Scenario.h" +#include "../world/Map.h" +#include "../world/MapAnimation.h" +#include "../world/tile_element/TileElement.h" +#include "../world/tile_element/TrackElement.h" +#include "Ride.h" +#include "RideData.h" +#include "Track.h" +#include "TrackData.h" +#include "VehicleGeometry.h" + +using namespace OpenRCT2; +using namespace OpenRCT2::Audio; +using namespace OpenRCT2::TrackMetaData; +using namespace OpenRCT2::RideVehicle; + +// External globals from Vehicle.cpp +extern Vehicle* gCurrentVehicle; +extern uint8_t _vehicleBreakdown; +extern StationIndex _vehicleStationIndex; +extern uint32_t _vehicleMotionTrackFlags; +extern int32_t _vehicleVelocityF64E08; +extern int32_t _vehicleVelocityF64E0C; +extern int32_t _vehicleUnkF64E10; +extern uint8_t _vehicleF64E2C; +extern Vehicle* _vehicleFrontVehicle; +extern CoordsXYZ _vehicleCurPosition; + +// Constants +constexpr uint8_t kTrackSpeedShiftAmount = 16; +constexpr uint8_t kBoosterAccelerationShiftAmount = 16; +constexpr int16_t kVehicleMaxSpinSpeedWaterRide = 512; +constexpr int16_t kVehicleMinSpinSpeedWaterRide = -kVehicleMaxSpinSpeedWaterRide; + +/** + * + * rct2: 0x006DAB90 + */ +void Vehicle::UpdateTrackMotionUpStopCheck() const +{ + const auto* carEntry = Entry(); + if (carEntry == nullptr) + { + return; + } + + // No up stops (coaster types) + if (carEntry->flags.has(CarEntryFlag::hasNoUpstopWheels)) + { + if (!IsOnCoveredTrack()) + { + auto gForces = GetGForces(); + gForces.lateralG = std::abs(gForces.lateralG); + if (gForces.lateralG <= 150) + { + if (Geometry::getAccelerationFromPitch(pitch) < 0) + { + if (gForces.verticalG > -40) + { + return; + } + } + else if (gForces.verticalG > -80) + { + return; + } + } + + if (pitch != VehiclePitch::down60) + { + _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_VEHICLE_DERAILED; + } + } + } + else if (carEntry->flags.has(CarEntryFlag::hasNoUpstopWheelsBobsleigh)) + { + // No up stops bobsleigh type + if (!IsOnCoveredTrack()) + { + auto gForces = GetGForces(); + + if (Geometry::getAccelerationFromPitch(pitch) < 0) + { + if (gForces.verticalG > -45) + { + return; + } + } + else + { + if (gForces.verticalG > -80) + { + return; + } + } + + if (pitch != VehiclePitch::down60 && pitch != VehiclePitch::down50) + { + _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_VEHICLE_DERAILED; + } + } + } +} + +/** + * Modifies the train's velocity to match the block-brake fixed velocity. + * This function must be called when the car is running through a non-stopping + * state block-brake (precondition), which means that the block brake is acting + * merely as a velocity regulator, in a closed state. When the brake is open, it + * boosts the train to the speed limit + */ +void Vehicle::ApplyNonStopBlockBrake() +{ + if (velocity >= 0) + { + // If the vehicle is below the speed limit + if (velocity <= kBlockBrakeBaseSpeed) + { + // Boost it to the fixed block brake speed + velocity = kBlockBrakeBaseSpeed; + acceleration = 0; + } + else if (velocity > (brake_speed << kTrackSpeedShiftAmount) + kBlockBrakeSpeedOffset) + { + velocity -= velocity >> 4; + acceleration = 0; + } + } +} + +/** + * + * Modifies the train's velocity influenced by a block brake + */ +void Vehicle::ApplyStopBlockBrake() +{ + // Slow it down till completely stop the car + _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_VEHICLE_AT_BLOCK_BRAKE; + acceleration = 0; + // If the this is slow enough, stop it. If not, slow it down + if (velocity <= 2.0_mph) + { + velocity = 0; + } + else + { + velocity -= velocity >> 3; + } +} + +void Vehicle::ApplyCableLiftBlockBrake(bool brakeClosed) +{ + // If we are already on the cable lift, ignore the brake + if (status == Status::travellingCableLift) + return; + + // Slow down if travelling faster than 4mph + if (velocity > kBlockBrakeBaseSpeed) + { + velocity -= velocity >> 3; + acceleration = 0; + } + // Once moving slow enough, keep moving forward until lined up with the catch car + else if (track_progress <= 18) + { + velocity = kBlockBrakeBaseSpeed; + acceleration = 0; + } + // Once in position, stop, and wait for the catch car + if (velocity > 0 && track_progress >= 18) + { + velocity = 0; + acceleration = 0; + if (!brakeClosed) + SetState(Status::waitingForCableLift, sub_state); + else + _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_VEHICLE_AT_BLOCK_BRAKE; + } +} + +/** + * + * rct2: 0x006DAC43 + */ +void Vehicle::CheckAndApplyBlockSectionStopSite() +{ + auto curRide = GetRide(); + if (curRide == nullptr) + return; + + auto carEntry = Entry(); + if (carEntry == nullptr) + return; + + // Is chair lift type + if (carEntry->flags.has(CarEntryFlag::isChairlift)) + { + velocity = _vehicleBreakdown == 0 ? 0 : curRide->speed << 16; + acceleration = 0; + } + + auto trackType = GetTrackType(); + + TileElement* trackElement = MapGetTrackElementAtOfType(TrackLocation, trackType); + + if (trackElement == nullptr) + { + return; + } + + switch (trackType) + { + case TrackElemType::blockBrakes: + // Check if this brake is the start of a cable lift + if (curRide->lifecycleFlags & RIDE_LIFECYCLE_CABLE_LIFT) + { + CoordsXYE track; + int32_t zUnused; + int32_t direction; + if (TrackBlockGetNextFromZero(TrackLocation, *curRide, GetTrackDirection(), &track, &zUnused, &direction, false) + && track.element != nullptr && track.element->AsTrack()->HasCableLift()) + { + ApplyCableLiftBlockBrake(curRide->isBlockSectioned() && trackElement->AsTrack()->IsBrakeClosed()); + break; + } + } + [[fallthrough]]; + case TrackElemType::diagBlockBrakes: + if (curRide->isBlockSectioned() && trackElement->AsTrack()->IsBrakeClosed()) + ApplyStopBlockBrake(); + else + ApplyNonStopBlockBrake(); + + break; + case TrackElemType::endStation: + if (trackElement->AsTrack()->IsBrakeClosed()) + _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_VEHICLE_AT_BLOCK_BRAKE; + + break; + case TrackElemType::up25ToFlat: + case TrackElemType::up60ToFlat: + case TrackElemType::cableLiftHill: + case TrackElemType::diagUp25ToFlat: + case TrackElemType::diagUp60ToFlat: + if (curRide->isBlockSectioned()) + { + if (trackType == TrackElemType::cableLiftHill || trackElement->AsTrack()->HasChain()) + { + if (trackElement->AsTrack()->IsBrakeClosed()) + { + ApplyStopBlockBrake(); + } + } + } + break; + default: + break; + } +} + +/** + * + * rct2: 0x006DADAE + */ +void Vehicle::UpdateVelocity() +{ + int32_t nextVelocity = acceleration + velocity; + if (HasFlag(VehicleFlags::StoppedBySafetyCutOut)) + { + nextVelocity = 0; + } + if (HasFlag(VehicleFlags::StoppedOnHoldingBrake)) + { + if (vertical_drop_countdown > 0) + { + nextVelocity = 0; + acceleration = 0; + vertical_drop_countdown--; + } + } + velocity = nextVelocity; + + _vehicleVelocityF64E08 = nextVelocity; + _vehicleVelocityF64E0C = (nextVelocity >> 10) * 42; +} + +static void BlockBrakesOpenPreviousSection(const Ride& ride, const CoordsXYZ& vehicleTrackLocation, TileElement* tileElement) +{ + CoordsXYZ location = vehicleTrackLocation; + TrackElement* trackElement = TrackGetPreviousBlock(location, tileElement); + if (trackElement == nullptr) + return; + + SetBrakeClosedMultiTile(*trackElement, location, false); + MapInvalidateElement(location, reinterpret_cast(trackElement)); + + auto trackType = trackElement->GetTrackType(); + if (trackType == TrackElemType::endStation) + { + Play3D(SoundId::blockBrakeClose, location); + } + else if (TrackTypeIsBlockBrakes(trackType)) + { + Play3D(SoundId::blockBrakeClose, location); + BlockBrakeSetLinkedBrakesClosed(location, *trackElement, false); + } +} + +/** + * + * rct2: 0x006DB38B + */ +static PitchAndRoll PitchAndRollStart(bool useInvertedSprites, TileElement* tileElement) +{ + auto trackType = tileElement->AsTrack()->GetTrackType(); + const auto& ted = GetTrackElementDescriptor(trackType); + return PitchAndRoll{ ted.definition.pitchStart, TrackGetActualBank3(useInvertedSprites, tileElement) }; +} + +void Vehicle::UpdateGoKartAttemptSwitchLanes() +{ + uint16_t probability = 0x8000; + if (HasFlag(VehicleFlags::CurrentlyColliding)) + { + ClearFlag(VehicleFlags::CurrentlyColliding); + } + else + { + probability = 0x0A3D; + } + if ((ScenarioRand() & 0xFFFF) <= probability) + { + // This changes "riding left" to "moving to right lane" and "riding right" to "moving to left lane". + TrackSubposition = VehicleTrackSubposition{ static_cast(static_cast(TrackSubposition) + 2u) }; + } +} + +static void vehicle_update_play_water_splash_sound() +{ + if (_vehicleVelocityF64E08 <= kBlockBrakeBaseSpeed) + { + return; + } + + Play3D(SoundId::waterSplash, { _vehicleCurPosition.x, _vehicleCurPosition.y, _vehicleCurPosition.z }); +} + +/** + * + * rct2: 0x006DB59E + */ +void Vehicle::UpdateHandleWaterSplash() const +{ + const auto* rideEntry = GetRideEntry(); + auto trackType = GetTrackType(); + + if (!rideEntry->flags.has(RideEntryFlag::playSplashSound) && rideEntry->flags.has(RideEntryFlag::playSplashSoundSlide)) + { + if (IsHead()) + { + if (IsOnCoveredTrack()) + { + Vehicle* nextVehicle = getGameState().entities.GetEntity(next_vehicle_on_ride); + if (nextVehicle == nullptr) + return; + + Vehicle* nextNextVehicle = getGameState().entities.GetEntity(nextVehicle->next_vehicle_on_ride); + if (nextNextVehicle == nullptr) + return; + if (!nextNextVehicle->IsOnCoveredTrack()) + { + if (track_progress == 4) + { + vehicle_update_play_water_splash_sound(); + } + } + } + } + } + else + { + if (trackType == TrackElemType::down25ToFlat) + { + if (track_progress == 12) + { + vehicle_update_play_water_splash_sound(); + } + } + } + if (IsHead()) + { + if (trackType == TrackElemType::watersplash) + { + if (track_progress == 48) + { + vehicle_update_play_water_splash_sound(); + } + } + } +} + +/** + * + * rct2: 0x006DBF3E + */ +void Vehicle::Sub6DBF3E() +{ + const auto* carEntry = Entry(); + + acceleration /= _vehicleUnkF64E10; + if (TrackSubposition == VehicleTrackSubposition::ChairliftGoingBack) + { + return; + } + + auto trackType = GetTrackType(); + const auto& ted = GetTrackElementDescriptor(trackType); + if (!ted.sequences[0].flags.has(SequenceFlag::trackOrigin)) + { + return; + } + + _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_3; + + TileElement* tileElement = nullptr; + if (MapIsLocationValid(TrackLocation)) + { + tileElement = MapGetTrackElementAtOfTypeSeq(TrackLocation, trackType, 0); + } + + if (tileElement == nullptr) + { + return; + } + + if (_vehicleStationIndex.IsNull()) + { + _vehicleStationIndex = tileElement->AsTrack()->GetStationIndex(); + } + + if (trackType == TrackElemType::towerBase && this == gCurrentVehicle) + { + if (track_progress > 3 && !HasFlag(VehicleFlags::PoweredCarInReverse)) + { + CoordsXYE output; + int32_t outputZ, outputDirection; + + CoordsXYE input = { TrackLocation, tileElement }; + if (!TrackBlockGetNext(&input, &output, &outputZ, &outputDirection)) + { + _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_12; + } + } + + if (track_progress <= 3) + { + _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_VEHICLE_AT_STATION; + } + } + + if (trackType != TrackElemType::endStation || this != gCurrentVehicle) + { + return; + } + + uint16_t ax = track_progress; + if (_vehicleVelocityF64E08 < 0) + { + if (ax <= 22) + { + _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_VEHICLE_AT_STATION; + } + } + else + { + uint16_t cx = 17; + if (carEntry->flags.has(CarEntryFlag::isChairlift)) + { + cx = 6; + } + if (carEntry->flags.has(CarEntryFlag::isGoKart)) + { + // Determine the stop positions for the karts. If in left lane it's further along the track than the right lane. + // Since it's not possible to overtake when the race has ended, this does not check for overtake states (7 and + // 8). + cx = TrackSubposition == VehicleTrackSubposition::GoKartsRightLane ? 18 : 20; + } + + if (ax > cx) + { + _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_VEHICLE_AT_STATION; + } + } +} + +/** + * Determine whether to use block brake speed or brake speed. If block brake is closed or no block brake present, use the + * brake's speed; if block brake is open, use maximum of brake speed or block brake speed. + */ +uint8_t Vehicle::ChooseBrakeSpeed() const +{ + if (!TrackTypeIsBrakes(GetTrackType())) + return brake_speed; + auto trackElement = MapGetTrackElementAtOfTypeSeq(TrackLocation, GetTrackType(), 0); + if (trackElement != nullptr) + { + if (trackElement->AsTrack()->IsBrakeClosed()) + return brake_speed; + else + return std::max(brake_speed, BlockBrakeSpeed); + } + return brake_speed; +} + +/** + * Populate the vehicle's brake_speed and BlockBrakeSpeed values. + */ +void Vehicle::PopulateBrakeSpeed(const CoordsXYZ& vehicleTrackLocation, TrackElement& brake) +{ + auto trackSpeed = brake.GetBrakeBoosterSpeed(); + brake_speed = trackSpeed; + if (!TrackTypeIsBrakes(brake.GetTrackType())) + { + BlockBrakeSpeed = trackSpeed; + return; + } + // As soon as feasible, encode block brake speed into track element so the lookforward can be skipped here. + + CoordsXYE output = CoordsXYE(vehicleTrackLocation.x, vehicleTrackLocation.y, reinterpret_cast(&brake)); + int32_t outputZ = vehicleTrackLocation.z; + uint16_t timeoutCount = 256; + do + { + if (TrackTypeIsBlockBrakes(output.element->AsTrack()->GetTrackType())) + { + BlockBrakeSpeed = output.element->AsTrack()->GetBrakeBoosterSpeed(); + return; + } + if (!TrackTypeIsBrakes(output.element->AsTrack()->GetTrackType())) + { + break; + } + timeoutCount--; + } while (TrackBlockGetNext(&output, &output, &outputZ, nullptr) && timeoutCount); + + // If block brake is not found, use the track's speed + BlockBrakeSpeed = trackSpeed; +} + +/** + * + * rct2: 0x006DB08C + */ +bool Vehicle::UpdateTrackMotionForwardsGetNewTrack( + TrackElemType trackType, const Ride& curRide, const RideObjectEntry& rideEntry) +{ + CoordsXYZD location = {}; + + auto pitchAndRollEnd = TrackPitchAndRollEnd(trackType); + TileElement* tileElement = MapGetTrackElementAtOfTypeSeq(TrackLocation, trackType, 0); + + if (tileElement == nullptr) + { + return false; + } + + if (trackType == TrackElemType::cableLiftHill && this == gCurrentVehicle) + { + _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_11; + } + + if (tileElement->AsTrack()->IsBlockStart()) + { + if (next_vehicle_on_train.IsNull()) + { + SetBrakeClosedMultiTile(*tileElement->AsTrack(), TrackLocation, true); + if (TrackTypeIsBlockBrakes(trackType) || trackType == TrackElemType::endStation) + { + if (!rideEntry.Cars[0].flags.has(CarEntryFlag::isPowered)) + { + Play3D(SoundId::blockBrakeRelease, TrackLocation); + } + } + MapInvalidateElement(TrackLocation, tileElement); + BlockBrakesOpenPreviousSection(curRide, TrackLocation, tileElement); + if (TrackTypeIsBlockBrakes(trackType)) + { + BlockBrakeSetLinkedBrakesClosed(TrackLocation, *tileElement->AsTrack(), true); + } + } + } + + // Change from original: this used to check if the vehicle allowed doors. + UpdateSceneryDoor(); + + bool isGoingBack = false; + switch (TrackSubposition) + { + case VehicleTrackSubposition::ChairliftGoingBack: + case VehicleTrackSubposition::ChairliftEndBullwheel: + TrackSubposition = VehicleTrackSubposition::ChairliftGoingBack; + isGoingBack = true; + break; + case VehicleTrackSubposition::ChairliftStartBullwheel: + TrackSubposition = VehicleTrackSubposition::ChairliftGoingOut; + break; + case VehicleTrackSubposition::GoKartsMovingToRightLane: + TrackSubposition = VehicleTrackSubposition::GoKartsRightLane; + break; + case VehicleTrackSubposition::GoKartsMovingToLeftLane: + TrackSubposition = VehicleTrackSubposition::GoKartsLeftLane; + break; + default: + break; + } + + if (isGoingBack) + { + TrackBeginEnd trackBeginEnd; + if (!TrackBlockGetPrevious({ TrackLocation, tileElement }, &trackBeginEnd)) + { + return false; + } + location.x = trackBeginEnd.begin_x; + location.y = trackBeginEnd.begin_y; + location.z = trackBeginEnd.begin_z; + location.direction = trackBeginEnd.begin_direction; + tileElement = trackBeginEnd.begin_element; + } + else + { + { + int32_t curZ, direction; + CoordsXYE xyElement = { TrackLocation, tileElement }; + if (!TrackBlockGetNext(&xyElement, &xyElement, &curZ, &direction)) + { + return false; + } + tileElement = xyElement.element; + location = { xyElement, curZ, static_cast(direction) }; + } + if (tileElement->AsTrack()->GetTrackType() == TrackElemType::leftReverser + || tileElement->AsTrack()->GetTrackType() == TrackElemType::rightReverser) + { + if (IsHead() && velocity <= 3.0_mph) + { + velocity = 0; + } + } + + if (PitchAndRollStart(HasFlag(VehicleFlags::CarIsInverted), tileElement) != pitchAndRollEnd) + { + return false; + } + + // Update VehicleFlags::CarIsInverted flag + const auto previousCarIsInverted = HasFlag(VehicleFlags::CarIsInverted); + ClearFlag(VehicleFlags::CarIsInverted); + { + auto rideType = ::GetRide(tileElement->AsTrack()->GetRideIndex())->type; + if (GetRideTypeDescriptor(rideType).flags.has(RtdFlag::hasInvertedVariant)) + { + if (tileElement->AsTrack()->IsInverted()) + { + SetFlag(VehicleFlags::CarIsInverted); + } + } + if (previousCarIsInverted != HasFlag(VehicleFlags::CarIsInverted)) + { + EntityTweener::Get().RemoveEntity(this); + } + } + } + + const int32_t previousTrackHeight = TrackLocation.z; + TrackLocation = location; + + // TODO check if getting the vehicle entry again is necessary + const auto* carEntry = Entry(); + if (carEntry == nullptr) + { + return false; + } + if (carEntry->flags.has(CarEntryFlag::isGoKart) && TrackSubposition < VehicleTrackSubposition::GoKartsMovingToRightLane) + { + trackType = tileElement->AsTrack()->GetTrackType(); + if (trackType == TrackElemType::flat || trackType == TrackElemType::leftQuarterTurn3Tiles + || trackType == TrackElemType::rightQuarterTurn3Tiles || trackType == TrackElemType::leftQuarterTurn5Tiles + || trackType == TrackElemType::rightQuarterTurn5Tiles || trackType == TrackElemType::leftEighthToDiag + || trackType == TrackElemType::rightEighthToDiag || trackType == TrackElemType::leftEighthToOrthogonal + || trackType == TrackElemType::rightEighthToOrthogonal || trackType == TrackElemType::diagFlat + || trackType == TrackElemType::sBendLeft || trackType == TrackElemType::sBendRight + || ((curRide.lifecycleFlags & RIDE_LIFECYCLE_PASS_STATION_NO_STOPPING) && tileElement->AsTrack()->IsStation())) + { + UpdateGoKartAttemptSwitchLanes(); + } + } + + if (TrackSubposition >= VehicleTrackSubposition::ChairliftGoingOut + && TrackSubposition <= VehicleTrackSubposition::ChairliftStartBullwheel) + { + TileCoordsXYZ curLocation{ TrackLocation }; + + if (curLocation == curRide.chairliftBullwheelLocation[1]) + { + TrackSubposition = VehicleTrackSubposition::ChairliftEndBullwheel; + } + else if (curLocation == curRide.chairliftBullwheelLocation[0]) + { + TrackSubposition = VehicleTrackSubposition::ChairliftStartBullwheel; + } + } + + // Loc6DB500 + // Update VehicleFlags::OnLiftHill + ClearFlag(VehicleFlags::OnLiftHill); + if (tileElement->AsTrack()->HasChain()) + { + SetFlag(VehicleFlags::OnLiftHill); + } + + trackType = tileElement->AsTrack()->GetTrackType(); + if (trackType != TrackElemType::brakes) + { + target_seat_rotation = tileElement->AsTrack()->GetSeatRotation(); + } + SetTrackDirection(location.direction); + SetTrackType(trackType); + PopulateBrakeSpeed(TrackLocation, *tileElement->AsTrack()); + if (HasFlag(VehicleFlags::StoppedOnHoldingBrake) && vertical_drop_countdown <= 0) + { + ClearFlag(VehicleFlags::StoppedOnHoldingBrake); + } + if (trackType == TrackElemType::onRidePhoto) + { + tileElement->AsTrack()->SetPhotoTimeout(); + MapAnimations::CreateTemporary(TrackLocation, MapAnimations::TemporaryType::onRidePhoto); + } + if (trackType == TrackElemType::rotationControlToggle) + { + Flags ^= VehicleFlags::SpinningIsLocked; + } + // Change from original: this used to check if the vehicle allowed doors. + UpdateSceneryDoorBackwards(); + UpdateLandscapeDoors(previousTrackHeight); + + return true; +} + +/** + * + * rct2: 0x006DAEB9 + */ +bool Vehicle::UpdateTrackMotionForwards(const CarEntry* carEntry, const Ride& curRide, const RideObjectEntry& rideEntry) +{ + EntityId otherVehicleIndex = EntityId::GetNull(); + while (true) + { + auto trackType = GetTrackType(); + if (trackType == TrackElemType::heartLineTransferUp || trackType == TrackElemType::heartLineTransferDown) + { + if (track_progress == 80) + { + vehicle_type ^= 1; + carEntry = Entry(); + } + if (_vehicleVelocityF64E08 >= 0x40000) + { + acceleration = -_vehicleVelocityF64E08 * 8; + } + else if (_vehicleVelocityF64E08 < 0x20000) + { + acceleration = 0x50000; + } + } + else if (TrackTypeIsBrakes(trackType)) + { + bool hasBrakesFailure = curRide.lifecycleFlags & RIDE_LIFECYCLE_BROKEN_DOWN + && curRide.breakdownReasonPending == BREAKDOWN_BRAKES_FAILURE; + if (!hasBrakesFailure || curRide.mechanicStatus == MechanicStatus::hasFixedStationBrakes) + { + auto brakeSpeed = ChooseBrakeSpeed() << kTrackSpeedShiftAmount; + + if ((brakeSpeed) < _vehicleVelocityF64E08) + { + acceleration = -_vehicleVelocityF64E08 * 16; + } + else if (!(getGameState().currentTicks & 0x0F)) + { + if (_vehicleF64E2C == 0) + { + _vehicleF64E2C++; + Play3D(SoundId::brakeRelease, { x, y, z }); + } + } + } + } + else if (TrackTypeIsBooster(trackType)) + { + auto boosterSpeed = GetUnifiedBoosterSpeed(curRide.type, brake_speed) << kTrackSpeedShiftAmount; + if (boosterSpeed > _vehicleVelocityF64E08) + { + acceleration = GetRideTypeDescriptor(curRide.type).LegacyBoosterSettings.BoosterAcceleration + << kBoosterAccelerationShiftAmount; + } + } + else if (rideEntry.flags.has(RideEntryFlag::riderControlsSpeed) && num_peeps > 0) + { + acceleration += CalculateRiderBraking(); + } + + if ((trackType == TrackElemType::flat && curRide.getRideTypeDescriptor().flags.has(RtdFlag::hasLsmBehaviourOnFlat)) + || (trackType == TrackElemType::poweredLift)) + { + acceleration = GetRideTypeDescriptor(curRide.type).LegacyBoosterSettings.PoweredLiftAcceleration + << kBoosterAccelerationShiftAmount; + } + if (trackType == TrackElemType::brakeForDrop) + { + if (IsHead()) + { + if (!HasFlag(VehicleFlags::StoppedOnHoldingBrake)) + { + if (track_progress >= 8) + { + acceleration = -_vehicleVelocityF64E08 * 16; + if (track_progress >= 24) + { + SetFlag(VehicleFlags::StoppedOnHoldingBrake); + vertical_drop_countdown = 90; + } + } + } + } + } + if (trackType == TrackElemType::logFlumeReverser) + { + if (track_progress != 16 || velocity < 4.0_mph) + { + if (track_progress == 32) + { + vehicle_type = carEntry->ReversedCarIndex; + carEntry = Entry(); + } + } + else + { + track_progress += 17; + } + } + + uint16_t newTrackProgress = track_progress + 1; + + // Track Total Progress is in the two bytes before the move info list + uint16_t trackTotalProgress = GetTrackProgress(); + if (newTrackProgress >= trackTotalProgress) + { + UpdateCrossings(); + + if (!UpdateTrackMotionForwardsGetNewTrack(trackType, curRide, rideEntry)) + { + _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_5; + _vehicleVelocityF64E0C -= remaining_distance + 1; + remaining_distance = -1; + return false; + } + newTrackProgress = 0; + } + + track_progress = newTrackProgress; + UpdateHandleWaterSplash(); + + // Loc6DB706 + const auto moveInfo = GetMoveInfo(); + trackType = GetTrackType(); + { + auto nextVehiclePosition = TrackLocation + + CoordsXYZ{ moveInfo->x, moveInfo->y, + moveInfo->z + GetRideTypeDescriptor(curRide.type).Heights.VehicleZOffset }; + + bool useReverserDistance = TrackSubposition == VehicleTrackSubposition::ReverserRCFrontBogie + && (trackType == TrackElemType::leftReverser || trackType == TrackElemType::rightReverser) + && track_progress >= 30 && track_progress <= 66; + + // Loc6DB8A5 + remaining_distance -= Geometry::getTranslationDistance( + nextVehiclePosition - _vehicleCurPosition, useReverserDistance); + + if (TrackSubposition == VehicleTrackSubposition::ReverserRCRearBogie + && (trackType == TrackElemType::leftReverser || trackType == TrackElemType::rightReverser) + && track_progress == 96) + { + ReverseReverserCar(); + + const VehicleInfo* moveInfo2 = GetMoveInfo(); + nextVehiclePosition.x = x + moveInfo2->x; + nextVehiclePosition.y = y + moveInfo2->y; + } + + _vehicleCurPosition = nextVehiclePosition; + Orientation = moveInfo->yaw; + roll = moveInfo->roll; + pitch = moveInfo->pitch; + + if (carEntry->flags.has(CarEntryFlag::useWoodenWildMouseSwing) && moveInfo->pitch != VehiclePitch::flat) + { + SwingSprite = 0; + SwingPosition = 0; + SwingSpeed = 0; + } + + // this == frontVehicle + if (this == _vehicleFrontVehicle) + { + if (_vehicleVelocityF64E08 >= 0) + { + otherVehicleIndex = prev_vehicle_on_ride; + if (UpdateMotionCollisionDetection(nextVehiclePosition, &otherVehicleIndex)) + { + _vehicleVelocityF64E0C -= remaining_distance + 1; + remaining_distance = -1; + + // Might need to be bp rather than this, but hopefully not + auto otherVeh = getGameState().entities.GetEntity(otherVehicleIndex); + if (otherVeh == nullptr) + { + // This can never happen as prev_vehicle_on_ride will always be set to a vehicle + LOG_ERROR("Failed to get next vehicle during update!"); + return true; + } + auto head = otherVeh->TrainHead(); + + auto velocityDelta = abs(velocity - head->velocity); + if (!rideEntry.flags.has(RideEntryFlag::disableCollisionCrashes)) + { + if (velocityDelta > 14.0_mph) + { + if (!carEntry->flags.has(CarEntryFlag::useBoatHireCollisionDetection)) + { + _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_VEHICLE_COLLISION; + } + } + } + + if (carEntry->flags.has(CarEntryFlag::isGoKart)) + { + velocity -= velocity >> 2; + } + else + { + int32_t newHeadVelocity = velocity >> 1; + velocity = head->velocity >> 1; + head->velocity = newHeadVelocity; + } + _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_1; + return false; + } + } + } + } + + // Loc6DB928 + if (remaining_distance < 0x368A) + { + return true; + } + + acceleration += Geometry::getAccelerationFromPitch(pitch); + _vehicleUnkF64E10++; + } +} + +static PitchAndRoll PitchAndRollEnd( + const Ride& curRide, bool useInvertedSprites, TrackElemType trackType, TileElement* tileElement) +{ + bool isInverted = useInvertedSprites ^ tileElement->AsTrack()->IsInverted(); + const auto& ted = GetTrackElementDescriptor(trackType); + return { ted.definition.pitchEnd, TrackGetActualBank2(curRide.type, isInverted, ted.definition.rollEnd) }; +} + +/** + * + * rct2: 0x006DBAA6 + */ +bool Vehicle::UpdateTrackMotionBackwardsGetNewTrack(TrackElemType trackType, const Ride& curRide, uint16_t* progress) +{ + auto pitchAndRollStart = TrackPitchAndRollStart(trackType); + TileElement* tileElement = MapGetTrackElementAtOfTypeSeq(TrackLocation, trackType, 0); + + if (tileElement == nullptr) + return false; + + bool nextTileBackwards = true; + int32_t direction = 0; + // Loc6DBB08:; + auto trackPos = CoordsXYZ{ TrackLocation.x, TrackLocation.y, 0 }; + + switch (TrackSubposition) + { + case VehicleTrackSubposition::ChairliftEndBullwheel: + TrackSubposition = VehicleTrackSubposition::ChairliftGoingOut; + break; + case VehicleTrackSubposition::GoKartsMovingToRightLane: + TrackSubposition = VehicleTrackSubposition::GoKartsLeftLane; + break; + case VehicleTrackSubposition::GoKartsMovingToLeftLane: + TrackSubposition = VehicleTrackSubposition::GoKartsRightLane; + break; + case VehicleTrackSubposition::ChairliftGoingBack: + case VehicleTrackSubposition::ChairliftStartBullwheel: + TrackSubposition = VehicleTrackSubposition::ChairliftGoingBack; + nextTileBackwards = false; + break; + default: + break; + } + + if (nextTileBackwards) + { + // Loc6DBB7E:; + TrackBeginEnd trackBeginEnd; + if (!TrackBlockGetPrevious({ trackPos, tileElement }, &trackBeginEnd)) + { + return false; + } + tileElement = trackBeginEnd.begin_element; + + trackType = tileElement->AsTrack()->GetTrackType(); + if (trackType == TrackElemType::leftReverser || trackType == TrackElemType::rightReverser) + { + return false; + } + + if (PitchAndRollEnd(curRide, HasFlag(VehicleFlags::CarIsInverted), trackType, tileElement) != pitchAndRollStart) + { + return false; + } + + // Update VehicleFlags::CarIsInverted + const auto previousCarIsInverted = HasFlag(VehicleFlags::CarIsInverted); + ClearFlag(VehicleFlags::CarIsInverted); + if (GetRideTypeDescriptor(curRide.type).flags.has(RtdFlag::hasInvertedVariant)) + { + if (tileElement->AsTrack()->IsInverted()) + { + SetFlag(VehicleFlags::CarIsInverted); + } + if (previousCarIsInverted != HasFlag(VehicleFlags::CarIsInverted)) + { + EntityTweener::Get().RemoveEntity(this); + } + } + + trackPos = { trackBeginEnd.begin_x, trackBeginEnd.begin_y, trackBeginEnd.begin_z }; + direction = trackBeginEnd.begin_direction; + } + else + { + // Loc6DBB4F:; + CoordsXYE input; + CoordsXYE output; + int32_t outputZ{}; + + input.x = trackPos.x; + input.y = trackPos.y; + input.element = tileElement; + if (!TrackBlockGetNext(&input, &output, &outputZ, &direction)) + { + return false; + } + tileElement = output.element; + trackPos = { output, outputZ }; + } + + // Loc6DBC3B: + TrackLocation = trackPos; + + if (TrackSubposition >= VehicleTrackSubposition::ChairliftGoingOut + && TrackSubposition <= VehicleTrackSubposition::ChairliftStartBullwheel) + { + TileCoordsXYZ curLocation{ TrackLocation }; + + if (curLocation == curRide.chairliftBullwheelLocation[1]) + { + TrackSubposition = VehicleTrackSubposition::ChairliftEndBullwheel; + } + else if (curLocation == curRide.chairliftBullwheelLocation[0]) + { + TrackSubposition = VehicleTrackSubposition::ChairliftStartBullwheel; + } + } + + if (tileElement->AsTrack()->HasChain()) + { + if (_vehicleVelocityF64E08 < 0) + { + if (next_vehicle_on_train.IsNull()) + { + trackType = tileElement->AsTrack()->GetTrackType(); + const auto& ted = GetTrackElementDescriptor(trackType); + if (!ted.flags.has(TrackElementFlag::down)) + { + _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_9; + } + } + SetFlag(VehicleFlags::OnLiftHill); + } + } + else + { + if (HasFlag(VehicleFlags::OnLiftHill)) + { + ClearFlag(VehicleFlags::OnLiftHill); + if (next_vehicle_on_train.IsNull()) + { + if (_vehicleVelocityF64E08 < 0) + { + _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_8; + } + } + } + } + + trackType = tileElement->AsTrack()->GetTrackType(); + if (trackType != TrackElemType::brakes) + { + target_seat_rotation = tileElement->AsTrack()->GetSeatRotation(); + } + direction &= 3; + SetTrackType(trackType); + SetTrackDirection(direction); + PopulateBrakeSpeed(TrackLocation, *tileElement->AsTrack()); + if (HasFlag(VehicleFlags::StoppedOnHoldingBrake) && vertical_drop_countdown <= 0) + { + ClearFlag(VehicleFlags::StoppedOnHoldingBrake); + } + // There are two bytes before the move info list + uint16_t trackTotalProgress = GetTrackProgress(); + *progress = trackTotalProgress - 1; + return true; +} + +/** + * + * rct2: 0x006DBA33 + */ +bool Vehicle::UpdateTrackMotionBackwards(const CarEntry* carEntry, const Ride& curRide, const RideObjectEntry& rideEntry) +{ + EntityId otherVehicleIndex = EntityId::GetNull(); + + while (true) + { + auto trackType = GetTrackType(); + if (trackType == TrackElemType::flat && curRide.getRideTypeDescriptor().flags.has(RtdFlag::hasLsmBehaviourOnFlat)) + { + int32_t unkVelocity = _vehicleVelocityF64E08; + if (unkVelocity < -524288) + { + unkVelocity = abs(unkVelocity); + acceleration = unkVelocity * 2; + } + } + + if (TrackTypeIsBrakes(trackType)) + { + auto brakeSpeed = ChooseBrakeSpeed(); + + if (-(brakeSpeed << kTrackSpeedShiftAmount) > _vehicleVelocityF64E08) + { + acceleration = _vehicleVelocityF64E08 * -16; + } + } + + uint16_t newTrackProgress = track_progress - 1; + if (newTrackProgress == 0xFFFF) + { + UpdateCrossings(); + + if (!UpdateTrackMotionBackwardsGetNewTrack(trackType, curRide, &newTrackProgress)) + { + _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_5; + _vehicleVelocityF64E0C -= remaining_distance - 0x368A; + remaining_distance = 0x368A; + return false; + } + } + + // Loc6DBD42 + track_progress = newTrackProgress; + { + const VehicleInfo* moveInfo = GetMoveInfo(); + auto nextVehiclePosition = TrackLocation + + CoordsXYZ{ moveInfo->x, moveInfo->y, + moveInfo->z + GetRideTypeDescriptor(curRide.type).Heights.VehicleZOffset }; + + remaining_distance += Geometry::getTranslationDistance(nextVehiclePosition - _vehicleCurPosition, false); + + _vehicleCurPosition = nextVehiclePosition; + Orientation = moveInfo->yaw; + roll = moveInfo->roll; + pitch = moveInfo->pitch; + + if (carEntry->flags.has(CarEntryFlag::useWoodenWildMouseSwing) && pitch != VehiclePitch::flat) + { + SwingSprite = 0; + SwingPosition = 0; + SwingSpeed = 0; + } + + if (this == _vehicleFrontVehicle) + { + if (_vehicleVelocityF64E08 < 0) + { + otherVehicleIndex = next_vehicle_on_ride; + if (UpdateMotionCollisionDetection(nextVehiclePosition, &otherVehicleIndex)) + { + _vehicleVelocityF64E0C -= remaining_distance - 0x368A; + remaining_distance = 0x368A; + + Vehicle* v3 = getGameState().entities.GetEntity(otherVehicleIndex); + Vehicle* v4 = gCurrentVehicle; + if (v3 == nullptr) + { + return false; + } + + if (!rideEntry.flags.has(RideEntryFlag::disableCollisionCrashes)) + { + if (abs(v4->velocity - v3->velocity) > 14.0_mph) + { + if (!carEntry->flags.has(CarEntryFlag::useBoatHireCollisionDetection)) + { + _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_VEHICLE_COLLISION; + } + } + } + + if (carEntry->flags.has(CarEntryFlag::isGoKart)) + { + velocity -= velocity >> 2; + _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_2; + } + else + { + int32_t v3Velocity = v3->velocity; + v3->velocity = v4->velocity >> 1; + v4->velocity = v3Velocity >> 1; + _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_2; + } + + return false; + } + } + } + } + + // Loc6DBE3F + if (remaining_distance >= 0) + { + return true; + } + acceleration += Geometry::getAccelerationFromPitch(pitch); + _vehicleUnkF64E10++; + } +} + +static constexpr int32_t GetAccelerationDecrease2(const int32_t velocity, const int32_t totalMass) +{ + int32_t accelerationDecrease2 = velocity >> 8; + accelerationDecrease2 *= accelerationDecrease2; + if (velocity < 0) + { + accelerationDecrease2 = -accelerationDecrease2; + } + accelerationDecrease2 >>= 4; + // OpenRCT2: vehicles from different track types can have 0 mass. + if (totalMass != 0) + { + return accelerationDecrease2 / totalMass; + } + + return accelerationDecrease2; +} + +/** + * + * rct2: 0x006DC1E4 + */ +static uint8_t modified_speed(TrackElemType trackType, VehicleTrackSubposition trackSubposition, uint8_t speed) +{ + enum + { + FULL_SPEED, + THREE_QUARTER_SPEED, + HALF_SPEED + }; + + uint8_t speedModifier = FULL_SPEED; + + if (trackType == TrackElemType::leftQuarterTurn1Tile) + { + speedModifier = (trackSubposition == VehicleTrackSubposition::GoKartsLeftLane) ? HALF_SPEED : THREE_QUARTER_SPEED; + } + else if (trackType == TrackElemType::rightQuarterTurn1Tile) + { + speedModifier = (trackSubposition == VehicleTrackSubposition::GoKartsRightLane) ? HALF_SPEED : THREE_QUARTER_SPEED; + } + + switch (speedModifier) + { + case HALF_SPEED: + return speed >> 1; + case THREE_QUARTER_SPEED: + return speed - (speed >> 2); + } + return speed; +} + +int32_t Vehicle::UpdateTrackMotionPoweredRideAcceleration( + const CarEntry* carEntry, uint32_t totalMass, const int32_t curAcceleration) +{ + if (carEntry->flags.has(CarEntryFlag::isPoweredRideWithUnrestrictedGravity)) + { + if (velocity > (speed * 0x4000)) + { + // Same code as none powered rides + if (curAcceleration <= 0 && curAcceleration >= -500 && velocity <= 0.5_mph) + { + return curAcceleration + 400; + } + return curAcceleration; + } + } + uint8_t modifiedSpeed = modified_speed(GetTrackType(), TrackSubposition, speed); + int32_t poweredAcceleration = modifiedSpeed << 14; + int32_t quarterForce = (modifiedSpeed * totalMass) >> 2; + if (HasFlag(VehicleFlags::PoweredCarInReverse)) + { + poweredAcceleration = -poweredAcceleration; + } + poweredAcceleration -= velocity; + poweredAcceleration *= powered_acceleration << 1; + if (quarterForce != 0) + { + poweredAcceleration /= quarterForce; + } + + if (carEntry->flags.has(CarEntryFlag::isLift)) + { + poweredAcceleration *= 4; + } + + if (carEntry->flags.has(CarEntryFlag::isWaterRide)) + { + if (poweredAcceleration < 0) + { + poweredAcceleration >>= 4; + } + + if (carEntry->flags.has(CarEntryFlag::hasSpinning)) + { + spin_speed = std::clamp(spin_speed, kVehicleMinSpinSpeedWaterRide, kVehicleMaxSpinSpeedWaterRide); + } + + if (pitch != VehiclePitch::flat) + { + if (poweredAcceleration < 0) + { + poweredAcceleration = 0; + } + + if (carEntry->flags.has(CarEntryFlag::hasSpinning)) + { + // If the vehicle is on the up slope kill the spin speedModifier + if (pitch == VehiclePitch::up25) + { + spin_speed = 0; + } + } + return curAcceleration + poweredAcceleration; + } + } + + if (std::abs(velocity) <= 1.0_mph) + { + return poweredAcceleration; + } + + return curAcceleration + poweredAcceleration; +} + +void Vehicle::UpdateTrackMotionPreUpdate( + Vehicle& car, const Ride& curRide, const RideObjectEntry& rideEntry, const CarEntry* carEntry) +{ + if (carEntry->flags.has(CarEntryFlag::hasSwinging)) + { + car.UpdateSwingingCar(); + } + if (carEntry->flags.has(CarEntryFlag::hasSpinning)) + { + car.UpdateSpinningCar(); + } + if (carEntry->flags.hasAny(CarEntryFlag::hasVehicleAnimation, CarEntryFlag::hasRiderAnimation)) + { + car.UpdateAdditionalAnimation(); + } + car.acceleration = Geometry::getAccelerationFromPitch(car.pitch); + _vehicleUnkF64E10 = 1; + + if (!car.HasFlag(VehicleFlags::MoveSingleCar)) + { + car.remaining_distance += _vehicleVelocityF64E0C; + } + + car.sound2_flags &= ~VEHICLE_SOUND2_FLAGS_LIFT_HILL; + _vehicleCurPosition.x = car.x; + _vehicleCurPosition.y = car.y; + _vehicleCurPosition.z = car.z; + car.Invalidate(); + + while (true) + { + if (car.remaining_distance < 0) + { + // Backward loop + if (car.UpdateTrackMotionBackwards(carEntry, curRide, rideEntry)) + { + break; + } + + if (car.remaining_distance < 0x368A) + { + break; + } + car.acceleration += Geometry::getAccelerationFromPitch(car.pitch); + _vehicleUnkF64E10++; + continue; + } + if (car.remaining_distance < 0x368A) + { + // Location found + return; + } + if (car.UpdateTrackMotionForwards(carEntry, curRide, rideEntry)) + { + break; + } + + if (car.remaining_distance >= 0) + { + break; + } + car.acceleration = Geometry::getAccelerationFromPitch(car.pitch); + _vehicleUnkF64E10++; + } + // Loc6DBF20 + car.MoveTo(_vehicleCurPosition); +} + +/** + * + * rct2: 0x006DAB4C + */ +int32_t Vehicle::UpdateTrackMotion(int32_t* outStation) +{ + auto curRide = GetRide(); + if (curRide == nullptr) + return 0; + + const auto* rideEntry = GetRideEntry(); + const auto* carEntry = Entry(); + + if (carEntry == nullptr) + { + return 0; + } + + if (carEntry->flags.has(CarEntryFlag::isMiniGolf)) + { + return UpdateTrackMotionMiniGolf(outStation); + } + + _vehicleF64E2C = 0; + gCurrentVehicle = this; + _vehicleMotionTrackFlags = 0; + _vehicleStationIndex = StationIndex::GetNull(); + + UpdateTrackMotionUpStopCheck(); + CheckAndApplyBlockSectionStopSite(); + UpdateVelocity(); + + Vehicle* vehicle = this; + if (_vehicleVelocityF64E08 < 0 && !vehicle->HasFlag(VehicleFlags::MoveSingleCar)) + { + vehicle = vehicle->TrainTail(); + } + // This will be the front vehicle even when traveling + // backwards. + _vehicleFrontVehicle = vehicle; + + auto spriteId = vehicle->Id; + while (!spriteId.IsNull()) + { + Vehicle* car = getGameState().entities.GetEntity(spriteId); + if (car == nullptr) + { + break; + } + carEntry = car->Entry(); + if (carEntry != nullptr) + { + UpdateTrackMotionPreUpdate(*car, *curRide, *rideEntry, carEntry); + } + + car->Sub6DBF3E(); + + // Loc6DC0F7 + if (car->HasFlag(VehicleFlags::OnLiftHill)) + { + _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_VEHICLE_ON_LIFT_HILL; + } + if (car->HasFlag(VehicleFlags::MoveSingleCar)) + { + if (outStation != nullptr) + *outStation = _vehicleStationIndex.ToUnderlying(); + return _vehicleMotionTrackFlags; + } + if (_vehicleVelocityF64E08 >= 0) + { + spriteId = car->next_vehicle_on_train; + } + else + { + if (car == gCurrentVehicle) + { + break; + } + spriteId = car->prev_vehicle_on_ride; + } + } + // Loc6DC144 + vehicle = gCurrentVehicle; + + carEntry = vehicle->Entry(); + // eax + int32_t totalAcceleration = 0; + // ebp + int32_t totalMass = 0; + // ebx + int32_t numVehicles = 0; + + for (; vehicle != nullptr; vehicle = getGameState().entities.GetEntity(vehicle->next_vehicle_on_train)) + { + numVehicles++; + totalMass += vehicle->mass; + totalAcceleration += vehicle->acceleration; + } + + vehicle = gCurrentVehicle; + int32_t newAcceleration = (totalAcceleration / numVehicles) * 21; + if (newAcceleration < 0) + { + newAcceleration += 511; + } + newAcceleration >>= 9; + + int32_t curAcceleration = newAcceleration; + curAcceleration -= vehicle->velocity / 4096; + curAcceleration -= GetAccelerationDecrease2(vehicle->velocity, totalMass); + + if (carEntry->flags.has(CarEntryFlag::isPowered)) + { + curAcceleration = vehicle->UpdateTrackMotionPoweredRideAcceleration(carEntry, totalMass, curAcceleration); + } + else if (curAcceleration <= 0 && curAcceleration >= -500) + { + // Probably moving slowly on a flat track piece, low rolling resistance and drag. + + if (vehicle->velocity <= 0.5_mph && vehicle->velocity >= 0) + { + // Vehicle is creeping forwards very slowly (less than ~2km/h), boost speed a bit. + curAcceleration += 400; + } + } + + if (vehicle->GetTrackType() == TrackElemType::watersplash) + { + if (vehicle->track_progress >= 48 && vehicle->track_progress <= 128) + { + curAcceleration -= vehicle->velocity >> 6; + } + } + + if (rideEntry->flags.has(RideEntryFlag::playSplashSoundSlide)) + { + if (vehicle->IsHead()) + { + if (vehicle->IsOnCoveredTrack()) + { + if (vehicle->velocity > 2.0_mph) + { + curAcceleration -= vehicle->velocity >> 6; + } + } + } + } + + vehicle->acceleration = curAcceleration; + + // hook_setreturnregisters(®s); + if (outStation != nullptr) + *outStation = _vehicleStationIndex.ToUnderlying(); + return _vehicleMotionTrackFlags; +} diff --git a/src/openrct2/ride/Vehicle.cpp b/src/openrct2/ride/Vehicle.cpp index 6b52243a14..dcf2277d7e 100644 --- a/src/openrct2/ride/Vehicle.cpp +++ b/src/openrct2/ride/Vehicle.cpp @@ -1552,280 +1552,6 @@ std::optional Vehicle::DodgemsCarWouldCollideAt(const CoordsXY& coords return std::nullopt; } -/** - * - * rct2: 0x006DAB90 - */ -void Vehicle::UpdateTrackMotionUpStopCheck() const -{ - const auto* carEntry = Entry(); - if (carEntry == nullptr) - { - return; - } - - // No up stops (coaster types) - if (carEntry->flags.has(CarEntryFlag::hasNoUpstopWheels)) - { - if (!IsOnCoveredTrack()) - { - auto gForces = GetGForces(); - gForces.lateralG = std::abs(gForces.lateralG); - if (gForces.lateralG <= 150) - { - if (Geometry::getAccelerationFromPitch(pitch) < 0) - { - if (gForces.verticalG > -40) - { - return; - } - } - else if (gForces.verticalG > -80) - { - return; - } - } - - if (pitch != VehiclePitch::down60) - { - _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_VEHICLE_DERAILED; - } - } - } - else if (carEntry->flags.has(CarEntryFlag::hasNoUpstopWheelsBobsleigh)) - { - // No up stops bobsleigh type - if (!IsOnCoveredTrack()) - { - auto gForces = GetGForces(); - - if (Geometry::getAccelerationFromPitch(pitch) < 0) - { - if (gForces.verticalG > -45) - { - return; - } - } - else - { - if (gForces.verticalG > -80) - { - return; - } - } - - if (pitch != VehiclePitch::down60 && pitch != VehiclePitch::down50) - { - _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_VEHICLE_DERAILED; - } - } - } -} - -/** - * Modifies the train's velocity to match the block-brake fixed velocity. - * This function must be called when the car is running through a non-stopping - * state block-brake (precondition), which means that the block brake is acting - * merely as a velocity regulator, in a closed state. When the brake is open, it - * boosts the train to the speed limit - */ -void Vehicle::ApplyNonStopBlockBrake() -{ - if (velocity >= 0) - { - // If the vehicle is below the speed limit - if (velocity <= kBlockBrakeBaseSpeed) - { - // Boost it to the fixed block brake speed - velocity = kBlockBrakeBaseSpeed; - acceleration = 0; - } - else if (velocity > (brake_speed << kTrackSpeedShiftAmount) + kBlockBrakeSpeedOffset) - { - velocity -= velocity >> 4; - acceleration = 0; - } - } -} - -/** - * - * Modifies the train's velocity influenced by a block brake - */ -void Vehicle::ApplyStopBlockBrake() -{ - // Slow it down till completely stop the car - _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_VEHICLE_AT_BLOCK_BRAKE; - acceleration = 0; - // If the this is slow enough, stop it. If not, slow it down - if (velocity <= 2.0_mph) - { - velocity = 0; - } - else - { - velocity -= velocity >> 3; - } -} - -void Vehicle::ApplyCableLiftBlockBrake(bool brakeClosed) -{ - // If we are already on the cable lift, ignore the brake - if (status == Status::travellingCableLift) - return; - - // Slow down if travelling faster than 4mph - if (velocity > kBlockBrakeBaseSpeed) - { - velocity -= velocity >> 3; - acceleration = 0; - } - // Once moving slow enough, keep moving forward until lined up with the catch car - else if (track_progress <= 18) - { - velocity = kBlockBrakeBaseSpeed; - acceleration = 0; - } - // Once in position, stop, and wait for the catch car - if (velocity > 0 && track_progress >= 18) - { - velocity = 0; - acceleration = 0; - if (!brakeClosed) - SetState(Status::waitingForCableLift, sub_state); - else - _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_VEHICLE_AT_BLOCK_BRAKE; - } -} - -/** - * - * rct2: 0x006DAC43 - */ -void Vehicle::CheckAndApplyBlockSectionStopSite() -{ - auto curRide = GetRide(); - if (curRide == nullptr) - return; - - auto carEntry = Entry(); - if (carEntry == nullptr) - return; - - // Is chair lift type - if (carEntry->flags.has(CarEntryFlag::isChairlift)) - { - velocity = _vehicleBreakdown == 0 ? 0 : curRide->speed << 16; - acceleration = 0; - } - - auto trackType = GetTrackType(); - - TileElement* trackElement = MapGetTrackElementAtOfType(TrackLocation, trackType); - - if (trackElement == nullptr) - { - return; - } - - switch (trackType) - { - case TrackElemType::blockBrakes: - // Check if this brake is the start of a cable lift - if (curRide->lifecycleFlags & RIDE_LIFECYCLE_CABLE_LIFT) - { - CoordsXYE track; - int32_t zUnused; - int32_t direction; - if (TrackBlockGetNextFromZero(TrackLocation, *curRide, GetTrackDirection(), &track, &zUnused, &direction, false) - && track.element != nullptr && track.element->AsTrack()->HasCableLift()) - { - ApplyCableLiftBlockBrake(curRide->isBlockSectioned() && trackElement->AsTrack()->IsBrakeClosed()); - break; - } - } - [[fallthrough]]; - case TrackElemType::diagBlockBrakes: - if (curRide->isBlockSectioned() && trackElement->AsTrack()->IsBrakeClosed()) - ApplyStopBlockBrake(); - else - ApplyNonStopBlockBrake(); - - break; - case TrackElemType::endStation: - if (trackElement->AsTrack()->IsBrakeClosed()) - _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_VEHICLE_AT_BLOCK_BRAKE; - - break; - case TrackElemType::up25ToFlat: - case TrackElemType::up60ToFlat: - case TrackElemType::cableLiftHill: - case TrackElemType::diagUp25ToFlat: - case TrackElemType::diagUp60ToFlat: - if (curRide->isBlockSectioned()) - { - if (trackType == TrackElemType::cableLiftHill || trackElement->AsTrack()->HasChain()) - { - if (trackElement->AsTrack()->IsBrakeClosed()) - { - ApplyStopBlockBrake(); - } - } - } - break; - default: - break; - } -} - -/** - * - * rct2: 0x006DADAE - */ -void Vehicle::UpdateVelocity() -{ - int32_t nextVelocity = acceleration + velocity; - if (HasFlag(VehicleFlags::StoppedBySafetyCutOut)) - { - nextVelocity = 0; - } - if (HasFlag(VehicleFlags::StoppedOnHoldingBrake)) - { - if (vertical_drop_countdown > 0) - { - nextVelocity = 0; - acceleration = 0; - vertical_drop_countdown--; - } - } - velocity = nextVelocity; - - _vehicleVelocityF64E08 = nextVelocity; - _vehicleVelocityF64E0C = (nextVelocity >> 10) * 42; -} - -static void BlockBrakesOpenPreviousSection(const Ride& ride, const CoordsXYZ& vehicleTrackLocation, TileElement* tileElement) -{ - CoordsXYZ location = vehicleTrackLocation; - TrackElement* trackElement = TrackGetPreviousBlock(location, tileElement); - if (trackElement == nullptr) - return; - - SetBrakeClosedMultiTile(*trackElement, location, false); - MapInvalidateElement(location, reinterpret_cast(trackElement)); - - auto trackType = trackElement->GetTrackType(); - if (trackType == TrackElemType::endStation) - { - Play3D(SoundId::blockBrakeClose, location); - } - else if (TrackTypeIsBlockBrakes(trackType)) - { - Play3D(SoundId::blockBrakeClose, location); - BlockBrakeSetLinkedBrakesClosed(location, *trackElement, false); - } -} - /** * * rct2: 0x006DEDB1 @@ -1989,35 +1715,6 @@ void Vehicle::UpdateLandscapeDoors(const int32_t previousTrackHeight) const } } -/** - * - * rct2: 0x006DB38B - */ -static PitchAndRoll PitchAndRollStart(bool useInvertedSprites, TileElement* tileElement) -{ - auto trackType = tileElement->AsTrack()->GetTrackType(); - const auto& ted = GetTrackElementDescriptor(trackType); - return PitchAndRoll{ ted.definition.pitchStart, TrackGetActualBank3(useInvertedSprites, tileElement) }; -} - -void Vehicle::UpdateGoKartAttemptSwitchLanes() -{ - uint16_t probability = 0x8000; - if (HasFlag(VehicleFlags::CurrentlyColliding)) - { - ClearFlag(VehicleFlags::CurrentlyColliding); - } - else - { - probability = 0x0A3D; - } - if ((ScenarioRand() & 0xFFFF) <= probability) - { - // This changes "riding left" to "moving to right lane" and "riding right" to "moving to left lane". - TrackSubposition = VehicleTrackSubposition{ static_cast(static_cast(TrackSubposition) + 2u) }; - } -} - /** * * rct2: 0x006DEDE8 @@ -2034,70 +1731,6 @@ void Vehicle::UpdateSceneryDoorBackwards() const AnimateSceneryDoor({ wallCoords, static_cast(direction) }, TrackLocation, next_vehicle_on_train.IsNull()); } -static void vehicle_update_play_water_splash_sound() -{ - if (_vehicleVelocityF64E08 <= kBlockBrakeBaseSpeed) - { - return; - } - - Play3D(SoundId::waterSplash, { _vehicleCurPosition.x, _vehicleCurPosition.y, _vehicleCurPosition.z }); -} - -/** - * - * rct2: 0x006DB59E - */ -void Vehicle::UpdateHandleWaterSplash() const -{ - const auto* rideEntry = GetRideEntry(); - auto trackType = GetTrackType(); - - if (!rideEntry->flags.has(RideEntryFlag::playSplashSound) && rideEntry->flags.has(RideEntryFlag::playSplashSoundSlide)) - { - if (IsHead()) - { - if (IsOnCoveredTrack()) - { - Vehicle* nextVehicle = getGameState().entities.GetEntity(next_vehicle_on_ride); - if (nextVehicle == nullptr) - return; - - Vehicle* nextNextVehicle = getGameState().entities.GetEntity(nextVehicle->next_vehicle_on_ride); - if (nextNextVehicle == nullptr) - return; - if (!nextNextVehicle->IsOnCoveredTrack()) - { - if (track_progress == 4) - { - vehicle_update_play_water_splash_sound(); - } - } - } - } - } - else - { - if (trackType == TrackElemType::down25ToFlat) - { - if (track_progress == 12) - { - vehicle_update_play_water_splash_sound(); - } - } - } - if (IsHead()) - { - if (trackType == TrackElemType::watersplash) - { - if (track_progress == 48) - { - vehicle_update_play_water_splash_sound(); - } - } - } -} - /** * * rct2: 0x006DB807 @@ -2348,1220 +1981,6 @@ void Vehicle::ReverseReverserCar() previousVehicle->UpdateReverserCarBogies(); } -/** - * - * rct2: 0x006DBF3E - */ -void Vehicle::Sub6DBF3E() -{ - const auto* carEntry = Entry(); - - acceleration /= _vehicleUnkF64E10; - if (TrackSubposition == VehicleTrackSubposition::ChairliftGoingBack) - { - return; - } - - auto trackType = GetTrackType(); - const auto& ted = GetTrackElementDescriptor(trackType); - if (!ted.sequences[0].flags.has(SequenceFlag::trackOrigin)) - { - return; - } - - _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_3; - - TileElement* tileElement = nullptr; - if (MapIsLocationValid(TrackLocation)) - { - tileElement = MapGetTrackElementAtOfTypeSeq(TrackLocation, trackType, 0); - } - - if (tileElement == nullptr) - { - return; - } - - if (_vehicleStationIndex.IsNull()) - { - _vehicleStationIndex = tileElement->AsTrack()->GetStationIndex(); - } - - if (trackType == TrackElemType::towerBase && this == gCurrentVehicle) - { - if (track_progress > 3 && !HasFlag(VehicleFlags::PoweredCarInReverse)) - { - CoordsXYE output; - int32_t outputZ, outputDirection; - - CoordsXYE input = { TrackLocation, tileElement }; - if (!TrackBlockGetNext(&input, &output, &outputZ, &outputDirection)) - { - _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_12; - } - } - - if (track_progress <= 3) - { - _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_VEHICLE_AT_STATION; - } - } - - if (trackType != TrackElemType::endStation || this != gCurrentVehicle) - { - return; - } - - uint16_t ax = track_progress; - if (_vehicleVelocityF64E08 < 0) - { - if (ax <= 22) - { - _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_VEHICLE_AT_STATION; - } - } - else - { - uint16_t cx = 17; - if (carEntry->flags.has(CarEntryFlag::isChairlift)) - { - cx = 6; - } - if (carEntry->flags.has(CarEntryFlag::isGoKart)) - { - // Determine the stop positions for the karts. If in left lane it's further along the track than the right lane. - // Since it's not possible to overtake when the race has ended, this does not check for overtake states (7 and - // 8). - cx = TrackSubposition == VehicleTrackSubposition::GoKartsRightLane ? 18 : 20; - } - - if (ax > cx) - { - _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_VEHICLE_AT_STATION; - } - } -} - -/** - * Determine whether to use block brake speed or brake speed. If block brake is closed or no block brake present, use the - * brake's speed; if block brake is open, use maximum of brake speed or block brake speed. - */ -uint8_t Vehicle::ChooseBrakeSpeed() const -{ - if (!TrackTypeIsBrakes(GetTrackType())) - return brake_speed; - auto trackElement = MapGetTrackElementAtOfTypeSeq(TrackLocation, GetTrackType(), 0); - if (trackElement != nullptr) - { - if (trackElement->AsTrack()->IsBrakeClosed()) - return brake_speed; - else - return std::max(brake_speed, BlockBrakeSpeed); - } - return brake_speed; -} - -/** - * Populate the vehicle's brake_speed and BlockBrakeSpeed values. - */ -void Vehicle::PopulateBrakeSpeed(const CoordsXYZ& vehicleTrackLocation, TrackElement& brake) -{ - auto trackSpeed = brake.GetBrakeBoosterSpeed(); - brake_speed = trackSpeed; - if (!TrackTypeIsBrakes(brake.GetTrackType())) - { - BlockBrakeSpeed = trackSpeed; - return; - } - // As soon as feasible, encode block brake speed into track element so the lookforward can be skipped here. - - CoordsXYE output = CoordsXYE(vehicleTrackLocation.x, vehicleTrackLocation.y, reinterpret_cast(&brake)); - int32_t outputZ = vehicleTrackLocation.z; - uint16_t timeoutCount = 256; - do - { - if (TrackTypeIsBlockBrakes(output.element->AsTrack()->GetTrackType())) - { - BlockBrakeSpeed = output.element->AsTrack()->GetBrakeBoosterSpeed(); - return; - } - if (!TrackTypeIsBrakes(output.element->AsTrack()->GetTrackType())) - { - break; - } - timeoutCount--; - } while (TrackBlockGetNext(&output, &output, &outputZ, nullptr) && timeoutCount); - - // If block brake is not found, use the track's speed - BlockBrakeSpeed = trackSpeed; -} - -/** - * - * rct2: 0x006DB08C - */ -bool Vehicle::UpdateTrackMotionForwardsGetNewTrack( - TrackElemType trackType, const Ride& curRide, const RideObjectEntry& rideEntry) -{ - CoordsXYZD location = {}; - - auto pitchAndRollEnd = TrackPitchAndRollEnd(trackType); - TileElement* tileElement = MapGetTrackElementAtOfTypeSeq(TrackLocation, trackType, 0); - - if (tileElement == nullptr) - { - return false; - } - - if (trackType == TrackElemType::cableLiftHill && this == gCurrentVehicle) - { - _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_11; - } - - if (tileElement->AsTrack()->IsBlockStart()) - { - if (next_vehicle_on_train.IsNull()) - { - SetBrakeClosedMultiTile(*tileElement->AsTrack(), TrackLocation, true); - if (TrackTypeIsBlockBrakes(trackType) || trackType == TrackElemType::endStation) - { - if (!rideEntry.Cars[0].flags.has(CarEntryFlag::isPowered)) - { - Play3D(SoundId::blockBrakeRelease, TrackLocation); - } - } - MapInvalidateElement(TrackLocation, tileElement); - BlockBrakesOpenPreviousSection(curRide, TrackLocation, tileElement); - if (TrackTypeIsBlockBrakes(trackType)) - { - BlockBrakeSetLinkedBrakesClosed(TrackLocation, *tileElement->AsTrack(), true); - } - } - } - - // Change from original: this used to check if the vehicle allowed doors. - UpdateSceneryDoor(); - - bool isGoingBack = false; - switch (TrackSubposition) - { - case VehicleTrackSubposition::ChairliftGoingBack: - case VehicleTrackSubposition::ChairliftEndBullwheel: - TrackSubposition = VehicleTrackSubposition::ChairliftGoingBack; - isGoingBack = true; - break; - case VehicleTrackSubposition::ChairliftStartBullwheel: - TrackSubposition = VehicleTrackSubposition::ChairliftGoingOut; - break; - case VehicleTrackSubposition::GoKartsMovingToRightLane: - TrackSubposition = VehicleTrackSubposition::GoKartsRightLane; - break; - case VehicleTrackSubposition::GoKartsMovingToLeftLane: - TrackSubposition = VehicleTrackSubposition::GoKartsLeftLane; - break; - default: - break; - } - - if (isGoingBack) - { - TrackBeginEnd trackBeginEnd; - if (!TrackBlockGetPrevious({ TrackLocation, tileElement }, &trackBeginEnd)) - { - return false; - } - location.x = trackBeginEnd.begin_x; - location.y = trackBeginEnd.begin_y; - location.z = trackBeginEnd.begin_z; - location.direction = trackBeginEnd.begin_direction; - tileElement = trackBeginEnd.begin_element; - } - else - { - { - int32_t curZ, direction; - CoordsXYE xyElement = { TrackLocation, tileElement }; - if (!TrackBlockGetNext(&xyElement, &xyElement, &curZ, &direction)) - { - return false; - } - tileElement = xyElement.element; - location = { xyElement, curZ, static_cast(direction) }; - } - if (tileElement->AsTrack()->GetTrackType() == TrackElemType::leftReverser - || tileElement->AsTrack()->GetTrackType() == TrackElemType::rightReverser) - { - if (IsHead() && velocity <= 3.0_mph) - { - velocity = 0; - } - } - - if (PitchAndRollStart(HasFlag(VehicleFlags::CarIsInverted), tileElement) != pitchAndRollEnd) - { - return false; - } - - // Update VehicleFlags::CarIsInverted flag - const auto previousCarIsInverted = HasFlag(VehicleFlags::CarIsInverted); - ClearFlag(VehicleFlags::CarIsInverted); - { - auto rideType = ::GetRide(tileElement->AsTrack()->GetRideIndex())->type; - if (GetRideTypeDescriptor(rideType).flags.has(RtdFlag::hasInvertedVariant)) - { - if (tileElement->AsTrack()->IsInverted()) - { - SetFlag(VehicleFlags::CarIsInverted); - } - } - if (previousCarIsInverted != HasFlag(VehicleFlags::CarIsInverted)) - { - EntityTweener::Get().RemoveEntity(this); - } - } - } - - const int32_t previousTrackHeight = TrackLocation.z; - TrackLocation = location; - - // TODO check if getting the vehicle entry again is necessary - const auto* carEntry = Entry(); - if (carEntry == nullptr) - { - return false; - } - if (carEntry->flags.has(CarEntryFlag::isGoKart) && TrackSubposition < VehicleTrackSubposition::GoKartsMovingToRightLane) - { - trackType = tileElement->AsTrack()->GetTrackType(); - if (trackType == TrackElemType::flat || trackType == TrackElemType::leftQuarterTurn3Tiles - || trackType == TrackElemType::rightQuarterTurn3Tiles || trackType == TrackElemType::leftQuarterTurn5Tiles - || trackType == TrackElemType::rightQuarterTurn5Tiles || trackType == TrackElemType::leftEighthToDiag - || trackType == TrackElemType::rightEighthToDiag || trackType == TrackElemType::leftEighthToOrthogonal - || trackType == TrackElemType::rightEighthToOrthogonal || trackType == TrackElemType::diagFlat - || trackType == TrackElemType::sBendLeft || trackType == TrackElemType::sBendRight - || ((curRide.lifecycleFlags & RIDE_LIFECYCLE_PASS_STATION_NO_STOPPING) && tileElement->AsTrack()->IsStation())) - { - UpdateGoKartAttemptSwitchLanes(); - } - } - - if (TrackSubposition >= VehicleTrackSubposition::ChairliftGoingOut - && TrackSubposition <= VehicleTrackSubposition::ChairliftStartBullwheel) - { - TileCoordsXYZ curLocation{ TrackLocation }; - - if (curLocation == curRide.chairliftBullwheelLocation[1]) - { - TrackSubposition = VehicleTrackSubposition::ChairliftEndBullwheel; - } - else if (curLocation == curRide.chairliftBullwheelLocation[0]) - { - TrackSubposition = VehicleTrackSubposition::ChairliftStartBullwheel; - } - } - - // Loc6DB500 - // Update VehicleFlags::OnLiftHill - ClearFlag(VehicleFlags::OnLiftHill); - if (tileElement->AsTrack()->HasChain()) - { - SetFlag(VehicleFlags::OnLiftHill); - } - - trackType = tileElement->AsTrack()->GetTrackType(); - if (trackType != TrackElemType::brakes) - { - target_seat_rotation = tileElement->AsTrack()->GetSeatRotation(); - } - SetTrackDirection(location.direction); - SetTrackType(trackType); - PopulateBrakeSpeed(TrackLocation, *tileElement->AsTrack()); - if (HasFlag(VehicleFlags::StoppedOnHoldingBrake) && vertical_drop_countdown <= 0) - { - ClearFlag(VehicleFlags::StoppedOnHoldingBrake); - } - if (trackType == TrackElemType::onRidePhoto) - { - tileElement->AsTrack()->SetPhotoTimeout(); - MapAnimations::CreateTemporary(TrackLocation, MapAnimations::TemporaryType::onRidePhoto); - } - if (trackType == TrackElemType::rotationControlToggle) - { - Flags ^= VehicleFlags::SpinningIsLocked; - } - // Change from original: this used to check if the vehicle allowed doors. - UpdateSceneryDoorBackwards(); - UpdateLandscapeDoors(previousTrackHeight); - - return true; -} - -/** - * - * rct2: 0x006DAEB9 - */ -bool Vehicle::UpdateTrackMotionForwards(const CarEntry* carEntry, const Ride& curRide, const RideObjectEntry& rideEntry) -{ - EntityId otherVehicleIndex = EntityId::GetNull(); - while (true) - { - auto trackType = GetTrackType(); - if (trackType == TrackElemType::heartLineTransferUp || trackType == TrackElemType::heartLineTransferDown) - { - if (track_progress == 80) - { - vehicle_type ^= 1; - carEntry = Entry(); - } - if (_vehicleVelocityF64E08 >= 0x40000) - { - acceleration = -_vehicleVelocityF64E08 * 8; - } - else if (_vehicleVelocityF64E08 < 0x20000) - { - acceleration = 0x50000; - } - } - else if (TrackTypeIsBrakes(trackType)) - { - bool hasBrakesFailure = curRide.lifecycleFlags & RIDE_LIFECYCLE_BROKEN_DOWN - && curRide.breakdownReasonPending == BREAKDOWN_BRAKES_FAILURE; - if (!hasBrakesFailure || curRide.mechanicStatus == MechanicStatus::hasFixedStationBrakes) - { - auto brakeSpeed = ChooseBrakeSpeed() << kTrackSpeedShiftAmount; - - if ((brakeSpeed) < _vehicleVelocityF64E08) - { - acceleration = -_vehicleVelocityF64E08 * 16; - } - else if (!(getGameState().currentTicks & 0x0F)) - { - if (_vehicleF64E2C == 0) - { - _vehicleF64E2C++; - Play3D(SoundId::brakeRelease, { x, y, z }); - } - } - } - } - else if (TrackTypeIsBooster(trackType)) - { - auto boosterSpeed = GetUnifiedBoosterSpeed(curRide.type, brake_speed) << kTrackSpeedShiftAmount; - if (boosterSpeed > _vehicleVelocityF64E08) - { - acceleration = GetRideTypeDescriptor(curRide.type).LegacyBoosterSettings.BoosterAcceleration - << kBoosterAccelerationShiftAmount; - } - } - else if (rideEntry.flags.has(RideEntryFlag::riderControlsSpeed) && num_peeps > 0) - { - acceleration += CalculateRiderBraking(); - } - - if ((trackType == TrackElemType::flat && curRide.getRideTypeDescriptor().flags.has(RtdFlag::hasLsmBehaviourOnFlat)) - || (trackType == TrackElemType::poweredLift)) - { - acceleration = GetRideTypeDescriptor(curRide.type).LegacyBoosterSettings.PoweredLiftAcceleration - << kBoosterAccelerationShiftAmount; - } - if (trackType == TrackElemType::brakeForDrop) - { - if (IsHead()) - { - if (!HasFlag(VehicleFlags::StoppedOnHoldingBrake)) - { - if (track_progress >= 8) - { - acceleration = -_vehicleVelocityF64E08 * 16; - if (track_progress >= 24) - { - SetFlag(VehicleFlags::StoppedOnHoldingBrake); - vertical_drop_countdown = 90; - } - } - } - } - } - if (trackType == TrackElemType::logFlumeReverser) - { - if (track_progress != 16 || velocity < 4.0_mph) - { - if (track_progress == 32) - { - vehicle_type = carEntry->ReversedCarIndex; - carEntry = Entry(); - } - } - else - { - track_progress += 17; - } - } - - uint16_t newTrackProgress = track_progress + 1; - - // Track Total Progress is in the two bytes before the move info list - uint16_t trackTotalProgress = GetTrackProgress(); - if (newTrackProgress >= trackTotalProgress) - { - UpdateCrossings(); - - if (!UpdateTrackMotionForwardsGetNewTrack(trackType, curRide, rideEntry)) - { - _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_5; - _vehicleVelocityF64E0C -= remaining_distance + 1; - remaining_distance = -1; - return false; - } - newTrackProgress = 0; - } - - track_progress = newTrackProgress; - UpdateHandleWaterSplash(); - - // Loc6DB706 - const auto moveInfo = GetMoveInfo(); - trackType = GetTrackType(); - { - auto nextVehiclePosition = TrackLocation - + CoordsXYZ{ moveInfo->x, moveInfo->y, - moveInfo->z + GetRideTypeDescriptor(curRide.type).Heights.VehicleZOffset }; - - bool useReverserDistance = TrackSubposition == VehicleTrackSubposition::ReverserRCFrontBogie - && (trackType == TrackElemType::leftReverser || trackType == TrackElemType::rightReverser) - && track_progress >= 30 && track_progress <= 66; - - // Loc6DB8A5 - remaining_distance -= Geometry::getTranslationDistance( - nextVehiclePosition - _vehicleCurPosition, useReverserDistance); - - if (TrackSubposition == VehicleTrackSubposition::ReverserRCRearBogie - && (trackType == TrackElemType::leftReverser || trackType == TrackElemType::rightReverser) - && track_progress == 96) - { - ReverseReverserCar(); - - const VehicleInfo* moveInfo2 = GetMoveInfo(); - nextVehiclePosition.x = x + moveInfo2->x; - nextVehiclePosition.y = y + moveInfo2->y; - } - - _vehicleCurPosition = nextVehiclePosition; - Orientation = moveInfo->yaw; - roll = moveInfo->roll; - pitch = moveInfo->pitch; - - if (carEntry->flags.has(CarEntryFlag::useWoodenWildMouseSwing) && moveInfo->pitch != VehiclePitch::flat) - { - SwingSprite = 0; - SwingPosition = 0; - SwingSpeed = 0; - } - - // this == frontVehicle - if (this == _vehicleFrontVehicle) - { - if (_vehicleVelocityF64E08 >= 0) - { - otherVehicleIndex = prev_vehicle_on_ride; - if (UpdateMotionCollisionDetection(nextVehiclePosition, &otherVehicleIndex)) - { - _vehicleVelocityF64E0C -= remaining_distance + 1; - remaining_distance = -1; - - // Might need to be bp rather than this, but hopefully not - auto otherVeh = getGameState().entities.GetEntity(otherVehicleIndex); - if (otherVeh == nullptr) - { - // This can never happen as prev_vehicle_on_ride will always be set to a vehicle - LOG_ERROR("Failed to get next vehicle during update!"); - return true; - } - auto head = otherVeh->TrainHead(); - - auto velocityDelta = abs(velocity - head->velocity); - if (!rideEntry.flags.has(RideEntryFlag::disableCollisionCrashes)) - { - if (velocityDelta > 14.0_mph) - { - if (!carEntry->flags.has(CarEntryFlag::useBoatHireCollisionDetection)) - { - _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_VEHICLE_COLLISION; - } - } - } - - if (carEntry->flags.has(CarEntryFlag::isGoKart)) - { - velocity -= velocity >> 2; - } - else - { - int32_t newHeadVelocity = velocity >> 1; - velocity = head->velocity >> 1; - head->velocity = newHeadVelocity; - } - _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_1; - return false; - } - } - } - } - - // Loc6DB928 - if (remaining_distance < 0x368A) - { - return true; - } - - acceleration += Geometry::getAccelerationFromPitch(pitch); - _vehicleUnkF64E10++; - } -} - -static PitchAndRoll PitchAndRollEnd( - const Ride& curRide, bool useInvertedSprites, TrackElemType trackType, TileElement* tileElement) -{ - bool isInverted = useInvertedSprites ^ tileElement->AsTrack()->IsInverted(); - const auto& ted = GetTrackElementDescriptor(trackType); - return { ted.definition.pitchEnd, TrackGetActualBank2(curRide.type, isInverted, ted.definition.rollEnd) }; -} - -/** - * - * rct2: 0x006DBAA6 - */ -bool Vehicle::UpdateTrackMotionBackwardsGetNewTrack(TrackElemType trackType, const Ride& curRide, uint16_t* progress) -{ - auto pitchAndRollStart = TrackPitchAndRollStart(trackType); - TileElement* tileElement = MapGetTrackElementAtOfTypeSeq(TrackLocation, trackType, 0); - - if (tileElement == nullptr) - return false; - - bool nextTileBackwards = true; - int32_t direction = 0; - // Loc6DBB08:; - auto trackPos = CoordsXYZ{ TrackLocation.x, TrackLocation.y, 0 }; - - switch (TrackSubposition) - { - case VehicleTrackSubposition::ChairliftEndBullwheel: - TrackSubposition = VehicleTrackSubposition::ChairliftGoingOut; - break; - case VehicleTrackSubposition::GoKartsMovingToRightLane: - TrackSubposition = VehicleTrackSubposition::GoKartsLeftLane; - break; - case VehicleTrackSubposition::GoKartsMovingToLeftLane: - TrackSubposition = VehicleTrackSubposition::GoKartsRightLane; - break; - case VehicleTrackSubposition::ChairliftGoingBack: - case VehicleTrackSubposition::ChairliftStartBullwheel: - TrackSubposition = VehicleTrackSubposition::ChairliftGoingBack; - nextTileBackwards = false; - break; - default: - break; - } - - if (nextTileBackwards) - { - // Loc6DBB7E:; - TrackBeginEnd trackBeginEnd; - if (!TrackBlockGetPrevious({ trackPos, tileElement }, &trackBeginEnd)) - { - return false; - } - tileElement = trackBeginEnd.begin_element; - - trackType = tileElement->AsTrack()->GetTrackType(); - if (trackType == TrackElemType::leftReverser || trackType == TrackElemType::rightReverser) - { - return false; - } - - if (PitchAndRollEnd(curRide, HasFlag(VehicleFlags::CarIsInverted), trackType, tileElement) != pitchAndRollStart) - { - return false; - } - - // Update VehicleFlags::CarIsInverted - const auto previousCarIsInverted = HasFlag(VehicleFlags::CarIsInverted); - ClearFlag(VehicleFlags::CarIsInverted); - if (GetRideTypeDescriptor(curRide.type).flags.has(RtdFlag::hasInvertedVariant)) - { - if (tileElement->AsTrack()->IsInverted()) - { - SetFlag(VehicleFlags::CarIsInverted); - } - if (previousCarIsInverted != HasFlag(VehicleFlags::CarIsInverted)) - { - EntityTweener::Get().RemoveEntity(this); - } - } - - trackPos = { trackBeginEnd.begin_x, trackBeginEnd.begin_y, trackBeginEnd.begin_z }; - direction = trackBeginEnd.begin_direction; - } - else - { - // Loc6DBB4F:; - CoordsXYE input; - CoordsXYE output; - int32_t outputZ{}; - - input.x = trackPos.x; - input.y = trackPos.y; - input.element = tileElement; - if (!TrackBlockGetNext(&input, &output, &outputZ, &direction)) - { - return false; - } - tileElement = output.element; - trackPos = { output, outputZ }; - } - - // Loc6DBC3B: - TrackLocation = trackPos; - - if (TrackSubposition >= VehicleTrackSubposition::ChairliftGoingOut - && TrackSubposition <= VehicleTrackSubposition::ChairliftStartBullwheel) - { - TileCoordsXYZ curLocation{ TrackLocation }; - - if (curLocation == curRide.chairliftBullwheelLocation[1]) - { - TrackSubposition = VehicleTrackSubposition::ChairliftEndBullwheel; - } - else if (curLocation == curRide.chairliftBullwheelLocation[0]) - { - TrackSubposition = VehicleTrackSubposition::ChairliftStartBullwheel; - } - } - - if (tileElement->AsTrack()->HasChain()) - { - if (_vehicleVelocityF64E08 < 0) - { - if (next_vehicle_on_train.IsNull()) - { - trackType = tileElement->AsTrack()->GetTrackType(); - const auto& ted = GetTrackElementDescriptor(trackType); - if (!ted.flags.has(TrackElementFlag::down)) - { - _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_9; - } - } - SetFlag(VehicleFlags::OnLiftHill); - } - } - else - { - if (HasFlag(VehicleFlags::OnLiftHill)) - { - ClearFlag(VehicleFlags::OnLiftHill); - if (next_vehicle_on_train.IsNull()) - { - if (_vehicleVelocityF64E08 < 0) - { - _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_8; - } - } - } - } - - trackType = tileElement->AsTrack()->GetTrackType(); - if (trackType != TrackElemType::brakes) - { - target_seat_rotation = tileElement->AsTrack()->GetSeatRotation(); - } - direction &= 3; - SetTrackType(trackType); - SetTrackDirection(direction); - PopulateBrakeSpeed(TrackLocation, *tileElement->AsTrack()); - if (HasFlag(VehicleFlags::StoppedOnHoldingBrake) && vertical_drop_countdown <= 0) - { - ClearFlag(VehicleFlags::StoppedOnHoldingBrake); - } - // There are two bytes before the move info list - uint16_t trackTotalProgress = GetTrackProgress(); - *progress = trackTotalProgress - 1; - return true; -} - -/** - * - * rct2: 0x006DBA33 - */ -bool Vehicle::UpdateTrackMotionBackwards(const CarEntry* carEntry, const Ride& curRide, const RideObjectEntry& rideEntry) -{ - EntityId otherVehicleIndex = EntityId::GetNull(); - - while (true) - { - auto trackType = GetTrackType(); - if (trackType == TrackElemType::flat && curRide.getRideTypeDescriptor().flags.has(RtdFlag::hasLsmBehaviourOnFlat)) - { - int32_t unkVelocity = _vehicleVelocityF64E08; - if (unkVelocity < -524288) - { - unkVelocity = abs(unkVelocity); - acceleration = unkVelocity * 2; - } - } - - if (TrackTypeIsBrakes(trackType)) - { - auto brakeSpeed = ChooseBrakeSpeed(); - - if (-(brakeSpeed << kTrackSpeedShiftAmount) > _vehicleVelocityF64E08) - { - acceleration = _vehicleVelocityF64E08 * -16; - } - } - - uint16_t newTrackProgress = track_progress - 1; - if (newTrackProgress == 0xFFFF) - { - UpdateCrossings(); - - if (!UpdateTrackMotionBackwardsGetNewTrack(trackType, curRide, &newTrackProgress)) - { - _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_5; - _vehicleVelocityF64E0C -= remaining_distance - 0x368A; - remaining_distance = 0x368A; - return false; - } - } - - // Loc6DBD42 - track_progress = newTrackProgress; - { - const VehicleInfo* moveInfo = GetMoveInfo(); - auto nextVehiclePosition = TrackLocation - + CoordsXYZ{ moveInfo->x, moveInfo->y, - moveInfo->z + GetRideTypeDescriptor(curRide.type).Heights.VehicleZOffset }; - - remaining_distance += Geometry::getTranslationDistance(nextVehiclePosition - _vehicleCurPosition, false); - - _vehicleCurPosition = nextVehiclePosition; - Orientation = moveInfo->yaw; - roll = moveInfo->roll; - pitch = moveInfo->pitch; - - if (carEntry->flags.has(CarEntryFlag::useWoodenWildMouseSwing) && pitch != VehiclePitch::flat) - { - SwingSprite = 0; - SwingPosition = 0; - SwingSpeed = 0; - } - - if (this == _vehicleFrontVehicle) - { - if (_vehicleVelocityF64E08 < 0) - { - otherVehicleIndex = next_vehicle_on_ride; - if (UpdateMotionCollisionDetection(nextVehiclePosition, &otherVehicleIndex)) - { - _vehicleVelocityF64E0C -= remaining_distance - 0x368A; - remaining_distance = 0x368A; - - Vehicle* v3 = getGameState().entities.GetEntity(otherVehicleIndex); - Vehicle* v4 = gCurrentVehicle; - if (v3 == nullptr) - { - return false; - } - - if (!rideEntry.flags.has(RideEntryFlag::disableCollisionCrashes)) - { - if (abs(v4->velocity - v3->velocity) > 14.0_mph) - { - if (!carEntry->flags.has(CarEntryFlag::useBoatHireCollisionDetection)) - { - _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_VEHICLE_COLLISION; - } - } - } - - if (carEntry->flags.has(CarEntryFlag::isGoKart)) - { - velocity -= velocity >> 2; - _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_2; - } - else - { - int32_t v3Velocity = v3->velocity; - v3->velocity = v4->velocity >> 1; - v4->velocity = v3Velocity >> 1; - _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_2; - } - - return false; - } - } - } - } - - // Loc6DBE3F - if (remaining_distance >= 0) - { - return true; - } - acceleration += Geometry::getAccelerationFromPitch(pitch); - _vehicleUnkF64E10++; - } -} - -static constexpr int32_t GetAccelerationDecrease2(const int32_t velocity, const int32_t totalMass) -{ - int32_t accelerationDecrease2 = velocity >> 8; - accelerationDecrease2 *= accelerationDecrease2; - if (velocity < 0) - { - accelerationDecrease2 = -accelerationDecrease2; - } - accelerationDecrease2 >>= 4; - // OpenRCT2: vehicles from different track types can have 0 mass. - if (totalMass != 0) - { - return accelerationDecrease2 / totalMass; - } - - return accelerationDecrease2; -} - -/** - * - * rct2: 0x006DC1E4 - */ -static uint8_t modified_speed(TrackElemType trackType, VehicleTrackSubposition trackSubposition, uint8_t speed) -{ - enum - { - FULL_SPEED, - THREE_QUARTER_SPEED, - HALF_SPEED - }; - - uint8_t speedModifier = FULL_SPEED; - - if (trackType == TrackElemType::leftQuarterTurn1Tile) - { - speedModifier = (trackSubposition == VehicleTrackSubposition::GoKartsLeftLane) ? HALF_SPEED : THREE_QUARTER_SPEED; - } - else if (trackType == TrackElemType::rightQuarterTurn1Tile) - { - speedModifier = (trackSubposition == VehicleTrackSubposition::GoKartsRightLane) ? HALF_SPEED : THREE_QUARTER_SPEED; - } - - switch (speedModifier) - { - case HALF_SPEED: - return speed >> 1; - case THREE_QUARTER_SPEED: - return speed - (speed >> 2); - } - return speed; -} - -int32_t Vehicle::UpdateTrackMotionPoweredRideAcceleration( - const CarEntry* carEntry, uint32_t totalMass, const int32_t curAcceleration) -{ - if (carEntry->flags.has(CarEntryFlag::isPoweredRideWithUnrestrictedGravity)) - { - if (velocity > (speed * 0x4000)) - { - // Same code as none powered rides - if (curAcceleration <= 0 && curAcceleration >= -500 && velocity <= 0.5_mph) - { - return curAcceleration + 400; - } - return curAcceleration; - } - } - uint8_t modifiedSpeed = modified_speed(GetTrackType(), TrackSubposition, speed); - int32_t poweredAcceleration = modifiedSpeed << 14; - int32_t quarterForce = (modifiedSpeed * totalMass) >> 2; - if (HasFlag(VehicleFlags::PoweredCarInReverse)) - { - poweredAcceleration = -poweredAcceleration; - } - poweredAcceleration -= velocity; - poweredAcceleration *= powered_acceleration << 1; - if (quarterForce != 0) - { - poweredAcceleration /= quarterForce; - } - - if (carEntry->flags.has(CarEntryFlag::isLift)) - { - poweredAcceleration *= 4; - } - - if (carEntry->flags.has(CarEntryFlag::isWaterRide)) - { - if (poweredAcceleration < 0) - { - poweredAcceleration >>= 4; - } - - if (carEntry->flags.has(CarEntryFlag::hasSpinning)) - { - spin_speed = std::clamp(spin_speed, kVehicleMinSpinSpeedWaterRide, kVehicleMaxSpinSpeedWaterRide); - } - - if (pitch != VehiclePitch::flat) - { - if (poweredAcceleration < 0) - { - poweredAcceleration = 0; - } - - if (carEntry->flags.has(CarEntryFlag::hasSpinning)) - { - // If the vehicle is on the up slope kill the spin speedModifier - if (pitch == VehiclePitch::up25) - { - spin_speed = 0; - } - } - return curAcceleration + poweredAcceleration; - } - } - - if (std::abs(velocity) <= 1.0_mph) - { - return poweredAcceleration; - } - - return curAcceleration + poweredAcceleration; -} - -void Vehicle::UpdateTrackMotionPreUpdate( - Vehicle& car, const Ride& curRide, const RideObjectEntry& rideEntry, const CarEntry* carEntry) -{ - if (carEntry->flags.has(CarEntryFlag::hasSwinging)) - { - car.UpdateSwingingCar(); - } - if (carEntry->flags.has(CarEntryFlag::hasSpinning)) - { - car.UpdateSpinningCar(); - } - if (carEntry->flags.hasAny(CarEntryFlag::hasVehicleAnimation, CarEntryFlag::hasRiderAnimation)) - { - car.UpdateAdditionalAnimation(); - } - car.acceleration = Geometry::getAccelerationFromPitch(car.pitch); - _vehicleUnkF64E10 = 1; - - if (!car.HasFlag(VehicleFlags::MoveSingleCar)) - { - car.remaining_distance += _vehicleVelocityF64E0C; - } - - car.sound2_flags &= ~VEHICLE_SOUND2_FLAGS_LIFT_HILL; - _vehicleCurPosition.x = car.x; - _vehicleCurPosition.y = car.y; - _vehicleCurPosition.z = car.z; - car.Invalidate(); - - while (true) - { - if (car.remaining_distance < 0) - { - // Backward loop - if (car.UpdateTrackMotionBackwards(carEntry, curRide, rideEntry)) - { - break; - } - - if (car.remaining_distance < 0x368A) - { - break; - } - car.acceleration += Geometry::getAccelerationFromPitch(car.pitch); - _vehicleUnkF64E10++; - continue; - } - if (car.remaining_distance < 0x368A) - { - // Location found - return; - } - if (car.UpdateTrackMotionForwards(carEntry, curRide, rideEntry)) - { - break; - } - - if (car.remaining_distance >= 0) - { - break; - } - car.acceleration = Geometry::getAccelerationFromPitch(car.pitch); - _vehicleUnkF64E10++; - } - // Loc6DBF20 - car.MoveTo(_vehicleCurPosition); -} - -/** - * - * rct2: 0x006DAB4C - */ -int32_t Vehicle::UpdateTrackMotion(int32_t* outStation) -{ - auto curRide = GetRide(); - if (curRide == nullptr) - return 0; - - const auto* rideEntry = GetRideEntry(); - const auto* carEntry = Entry(); - - if (carEntry == nullptr) - { - return 0; - } - - if (carEntry->flags.has(CarEntryFlag::isMiniGolf)) - { - return UpdateTrackMotionMiniGolf(outStation); - } - - _vehicleF64E2C = 0; - gCurrentVehicle = this; - _vehicleMotionTrackFlags = 0; - _vehicleStationIndex = StationIndex::GetNull(); - - UpdateTrackMotionUpStopCheck(); - CheckAndApplyBlockSectionStopSite(); - UpdateVelocity(); - - Vehicle* vehicle = this; - if (_vehicleVelocityF64E08 < 0 && !vehicle->HasFlag(VehicleFlags::MoveSingleCar)) - { - vehicle = vehicle->TrainTail(); - } - // This will be the front vehicle even when traveling - // backwards. - _vehicleFrontVehicle = vehicle; - - auto spriteId = vehicle->Id; - while (!spriteId.IsNull()) - { - Vehicle* car = getGameState().entities.GetEntity(spriteId); - if (car == nullptr) - { - break; - } - carEntry = car->Entry(); - if (carEntry != nullptr) - { - UpdateTrackMotionPreUpdate(*car, *curRide, *rideEntry, carEntry); - } - - car->Sub6DBF3E(); - - // Loc6DC0F7 - if (car->HasFlag(VehicleFlags::OnLiftHill)) - { - _vehicleMotionTrackFlags |= VEHICLE_UPDATE_MOTION_TRACK_FLAG_VEHICLE_ON_LIFT_HILL; - } - if (car->HasFlag(VehicleFlags::MoveSingleCar)) - { - if (outStation != nullptr) - *outStation = _vehicleStationIndex.ToUnderlying(); - return _vehicleMotionTrackFlags; - } - if (_vehicleVelocityF64E08 >= 0) - { - spriteId = car->next_vehicle_on_train; - } - else - { - if (car == gCurrentVehicle) - { - break; - } - spriteId = car->prev_vehicle_on_ride; - } - } - // Loc6DC144 - vehicle = gCurrentVehicle; - - carEntry = vehicle->Entry(); - // eax - int32_t totalAcceleration = 0; - // ebp - int32_t totalMass = 0; - // ebx - int32_t numVehicles = 0; - - for (; vehicle != nullptr; vehicle = getGameState().entities.GetEntity(vehicle->next_vehicle_on_train)) - { - numVehicles++; - totalMass += vehicle->mass; - totalAcceleration += vehicle->acceleration; - } - - vehicle = gCurrentVehicle; - int32_t newAcceleration = (totalAcceleration / numVehicles) * 21; - if (newAcceleration < 0) - { - newAcceleration += 511; - } - newAcceleration >>= 9; - - int32_t curAcceleration = newAcceleration; - curAcceleration -= vehicle->velocity / 4096; - curAcceleration -= GetAccelerationDecrease2(vehicle->velocity, totalMass); - - if (carEntry->flags.has(CarEntryFlag::isPowered)) - { - curAcceleration = vehicle->UpdateTrackMotionPoweredRideAcceleration(carEntry, totalMass, curAcceleration); - } - else if (curAcceleration <= 0 && curAcceleration >= -500) - { - // Probably moving slowly on a flat track piece, low rolling resistance and drag. - - if (vehicle->velocity <= 0.5_mph && vehicle->velocity >= 0) - { - // Vehicle is creeping forwards very slowly (less than ~2km/h), boost speed a bit. - curAcceleration += 400; - } - } - - if (vehicle->GetTrackType() == TrackElemType::watersplash) - { - if (vehicle->track_progress >= 48 && vehicle->track_progress <= 128) - { - curAcceleration -= vehicle->velocity >> 6; - } - } - - if (rideEntry->flags.has(RideEntryFlag::playSplashSoundSlide)) - { - if (vehicle->IsHead()) - { - if (vehicle->IsOnCoveredTrack()) - { - if (vehicle->velocity > 2.0_mph) - { - curAcceleration -= vehicle->velocity >> 6; - } - } - } - } - - vehicle->acceleration = curAcceleration; - - // hook_setreturnregisters(®s); - if (outStation != nullptr) - *outStation = _vehicleStationIndex.ToUnderlying(); - return _vehicleMotionTrackFlags; -} - const RideObjectEntry* Vehicle::GetRideEntry() const { return GetRideEntryByIndex(ride_subtype); From e68f249c0e8dbc9744760a2813deeea9aa873e16 Mon Sep 17 00:00:00 2001 From: frozensnowy Date: Wed, 11 Feb 2026 21:09:09 +0100 Subject: [PATCH 2/6] Refactor: Move collision logic to Vehicle.Collision.cpp Moves collision detection and reverser car functions to a dedicated file for it --- src/openrct2/libopenrct2.vcxproj | 1 + src/openrct2/ride/Vehicle.Collision.cpp | 276 ++++++++++++++++++++++++ src/openrct2/ride/Vehicle.cpp | 245 --------------------- 3 files changed, 277 insertions(+), 245 deletions(-) create mode 100644 src/openrct2/ride/Vehicle.Collision.cpp diff --git a/src/openrct2/libopenrct2.vcxproj b/src/openrct2/libopenrct2.vcxproj index 17c8ef39aa..2c47f86cfc 100644 --- a/src/openrct2/libopenrct2.vcxproj +++ b/src/openrct2/libopenrct2.vcxproj @@ -1127,6 +1127,7 @@ + diff --git a/src/openrct2/ride/Vehicle.Collision.cpp b/src/openrct2/ride/Vehicle.Collision.cpp new file mode 100644 index 0000000000..dc0000bff0 --- /dev/null +++ b/src/openrct2/ride/Vehicle.Collision.cpp @@ -0,0 +1,276 @@ +/***************************************************************************** + * 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 "../GameState.h" +#include "../entity/EntityList.h" +#include "../entity/EntityRegistry.h" +#include "../entity/Yaw.hpp" +#include "../math/Trigonometry.hpp" + +using namespace OpenRCT2; + +/** rct2: 0x009A37C4 */ +static constexpr CoordsXY kSurroundingTiles[] = { + { 0, 0 }, + { 0, +kCoordsXYStep }, + { +kCoordsXYStep, 0 }, + { 0, -kCoordsXYStep }, + { 0, -kCoordsXYStep }, + { -kCoordsXYStep, 0 }, + { -kCoordsXYStep, 0 }, + { 0, +kCoordsXYStep }, + { 0, +kCoordsXYStep }, +}; + +/** + * + * rct2: 0x006DB807 + */ +void Vehicle::UpdateReverserCarBogies() +{ + const auto moveInfo = GetMoveInfo(); + MoveTo({ TrackLocation.x + moveInfo->x, TrackLocation.y + moveInfo->y, z }); +} + +/** + * Collision Detection + * rct2: 0x006DD078 + * @param vehicle (esi) + * @param x (ax) + * @param y (cx) + * @param z (dx) + * @param otherVehicleIndex (bp) + */ +bool Vehicle::UpdateMotionCollisionDetection(const CoordsXYZ& loc, EntityId* otherVehicleIndex) +{ + if (HasFlag(VehicleFlags::CollisionDisabled)) + return false; + + auto carEntry = Entry(); + if (carEntry == nullptr) + { + return false; + } + + if (!carEntry->flags.has(CarEntryFlag::useBoatHireCollisionDetection)) + { + CollisionDetectionTimer = 0; + + // If hacking boat hire rides you can end up here + if (otherVehicleIndex == nullptr) + return false; + + Vehicle* collideVehicle = getGameState().entities.GetEntity(*otherVehicleIndex); + if (collideVehicle == nullptr) + return false; + + if (this == collideVehicle) + return false; + + int32_t x_diff = abs(loc.x - collideVehicle->x); + if (x_diff > 0x7FFF) + return false; + + int32_t y_diff = abs(loc.y - collideVehicle->y); + if (y_diff > 0x7FFF) + return false; + + int32_t z_diff = abs(loc.z - collideVehicle->z); + if (x_diff + y_diff + z_diff > 0xFFFF) + return false; + + uint16_t ecx = std::min(var_44 + collideVehicle->var_44, 560); + ecx = ((ecx >> 1) * 30) >> 8; + + if (x_diff + y_diff + z_diff >= ecx) + return false; + + uint8_t direction = (Orientation - collideVehicle->Orientation + 7) & 0x1F; + return direction < 0xF; + } + + CoordsXY location = loc; + + bool mayCollide = false; + Vehicle* collideVehicle = nullptr; + for (auto xy_offset : kSurroundingTiles) + { + location += xy_offset; + + for (auto vehicle2 : EntityTileList(location)) + { + if (vehicle2 == this) + continue; + + int32_t z_diff = abs(vehicle2->z - loc.z); + + if (z_diff > 16) + continue; + + if (vehicle2->IsCableLift()) + continue; + + auto collideCarEntry = vehicle2->Entry(); + if (collideCarEntry == nullptr) + continue; + + if (!collideCarEntry->flags.has(CarEntryFlag::useBoatHireCollisionDetection)) + continue; + + uint32_t x_diff = abs(vehicle2->x - loc.x); + if (x_diff > 0x7FFF) + continue; + + uint32_t y_diff = abs(vehicle2->y - loc.y); + if (y_diff > 0x7FFF) + continue; + + VehicleTrackSubposition cl = std::min(TrackSubposition, vehicle2->TrackSubposition); + VehicleTrackSubposition ch = std::max(TrackSubposition, vehicle2->TrackSubposition); + if (cl != ch) + { + if (cl == VehicleTrackSubposition::GoKartsLeftLane && ch == VehicleTrackSubposition::GoKartsRightLane) + continue; + } + + uint32_t ecx = var_44 + vehicle2->var_44; + ecx = ((ecx >> 1) * 30) >> 8; + + if (x_diff + y_diff >= ecx) + continue; + + if (!collideCarEntry->flags.has(CarEntryFlag::isGoKart)) + { + collideVehicle = vehicle2; + mayCollide = true; + break; + } + + uint8_t direction = (Orientation - vehicle2->Orientation - 6) & 0x1F; + + if (direction < 0x14) + continue; + + const CoordsXY directionVector = Math::Trigonometry::YawToDirectionVector[Entity::Yaw::YawTo64(Orientation)]; + + const CoordsXY directionVectorToVehicle2 = { vehicle2->x - loc.x, vehicle2->y - loc.y }; + const int32_t directionVectorToVehicle2Length = (directionVectorToVehicle2.x * directionVectorToVehicle2.x) + + (directionVectorToVehicle2.y * directionVectorToVehicle2.y); + + const int32_t dotProduct = (directionVector.x * directionVectorToVehicle2.x) + + (directionVector.y * directionVectorToVehicle2.y); + + static constexpr int32_t threshold = []() consteval { + const constexpr float originalThreshold = 0.35f; + const constexpr float directionVectorLength = 256.0f; + const constexpr float thresholdLength = originalThreshold * directionVectorLength; + return static_cast(thresholdLength * thresholdLength); + }(); + static_assert(threshold == 8028); + + if (dotProduct > 0 && dotProduct * dotProduct > threshold * directionVectorToVehicle2Length) + { + collideVehicle = vehicle2; + mayCollide = true; + break; + } + } + if (mayCollide) + { + break; + } + } + + if (!mayCollide || collideVehicle == nullptr) + { + CollisionDetectionTimer = 0; + return false; + } + + if (collideVehicle->status == Status::travellingBoat && sub_state == BoatHireSubState::EnteringReturnPosition) + { + return false; + } + + CollisionDetectionTimer++; + if (CollisionDetectionTimer < 200) + { + SetFlag(VehicleFlags::CurrentlyColliding); + if (otherVehicleIndex != nullptr) + *otherVehicleIndex = collideVehicle->Id; + return true; + } + + if (status == Status::movingToEndOfStation) + { + if (Orientation == 0) + { + if (x <= collideVehicle->x) + { + return false; + } + } + else if (Orientation == 8) + { + if (y >= collideVehicle->y) + { + return false; + } + } + else if (Orientation == 16) + { + if (x >= collideVehicle->x) + { + return false; + } + } + else if (Orientation == 24) + { + if (y <= collideVehicle->y) + { + return false; + } + } + } + + if ((collideVehicle->status == Status::travellingBoat) && (status != Status::arriving) && (status != Status::travelling)) + { + return false; + } + + SetFlag(VehicleFlags::CurrentlyColliding); + if (otherVehicleIndex != nullptr) + *otherVehicleIndex = collideVehicle->Id; + return true; +} + +/** + * + * rct2: 0x006DB7D6 + */ +void Vehicle::ReverseReverserCar() +{ + Vehicle* previousVehicle = getGameState().entities.GetEntity(prev_vehicle_on_ride); + Vehicle* nextVehicle = getGameState().entities.GetEntity(next_vehicle_on_ride); + if (previousVehicle == nullptr || nextVehicle == nullptr) + { + return; + } + + track_progress = 168; + vehicle_type ^= 1; + + previousVehicle->track_progress = 86; + nextVehicle->track_progress = 158; + + nextVehicle->UpdateReverserCarBogies(); + previousVehicle->UpdateReverserCarBogies(); +} diff --git a/src/openrct2/ride/Vehicle.cpp b/src/openrct2/ride/Vehicle.cpp index dcf2277d7e..07a1e5181e 100644 --- a/src/openrct2/ride/Vehicle.cpp +++ b/src/openrct2/ride/Vehicle.cpp @@ -1731,256 +1731,11 @@ void Vehicle::UpdateSceneryDoorBackwards() const AnimateSceneryDoor({ wallCoords, static_cast(direction) }, TrackLocation, next_vehicle_on_train.IsNull()); } -/** - * - * rct2: 0x006DB807 - */ -void Vehicle::UpdateReverserCarBogies() -{ - const auto moveInfo = GetMoveInfo(); - MoveTo({ TrackLocation.x + moveInfo->x, TrackLocation.y + moveInfo->y, z }); -} - bool Vehicle::IsCableLift() const { return ride_subtype == kObjectEntryIndexNull; } -/** - * Collision Detection - * rct2: 0x006DD078 - * @param vehicle (esi) - * @param x (ax) - * @param y (cx) - * @param z (dx) - * @param otherVehicleIndex (bp) - */ -bool Vehicle::UpdateMotionCollisionDetection(const CoordsXYZ& loc, EntityId* otherVehicleIndex) -{ - if (HasFlag(VehicleFlags::CollisionDisabled)) - return false; - - auto carEntry = Entry(); - if (carEntry == nullptr) - { - return false; - } - - if (!carEntry->flags.has(CarEntryFlag::useBoatHireCollisionDetection)) - { - CollisionDetectionTimer = 0; - - // If hacking boat hire rides you can end up here - if (otherVehicleIndex == nullptr) - return false; - - Vehicle* collideVehicle = getGameState().entities.GetEntity(*otherVehicleIndex); - if (collideVehicle == nullptr) - return false; - - if (this == collideVehicle) - return false; - - int32_t x_diff = abs(loc.x - collideVehicle->x); - if (x_diff > 0x7FFF) - return false; - - int32_t y_diff = abs(loc.y - collideVehicle->y); - if (y_diff > 0x7FFF) - return false; - - int32_t z_diff = abs(loc.z - collideVehicle->z); - if (x_diff + y_diff + z_diff > 0xFFFF) - return false; - - uint16_t ecx = std::min(var_44 + collideVehicle->var_44, 560); - ecx = ((ecx >> 1) * 30) >> 8; - - if (x_diff + y_diff + z_diff >= ecx) - return false; - - uint8_t direction = (Orientation - collideVehicle->Orientation + 7) & 0x1F; - return direction < 0xF; - } - - CoordsXY location = loc; - - bool mayCollide = false; - Vehicle* collideVehicle = nullptr; - for (auto xy_offset : kSurroundingTiles) - { - location += xy_offset; - - for (auto vehicle2 : EntityTileList(location)) - { - if (vehicle2 == this) - continue; - - int32_t z_diff = abs(vehicle2->z - loc.z); - - if (z_diff > 16) - continue; - - if (vehicle2->IsCableLift()) - continue; - - auto collideCarEntry = vehicle2->Entry(); - if (collideCarEntry == nullptr) - continue; - - if (!collideCarEntry->flags.has(CarEntryFlag::useBoatHireCollisionDetection)) - continue; - - uint32_t x_diff = abs(vehicle2->x - loc.x); - if (x_diff > 0x7FFF) - continue; - - uint32_t y_diff = abs(vehicle2->y - loc.y); - if (y_diff > 0x7FFF) - continue; - - VehicleTrackSubposition cl = std::min(TrackSubposition, vehicle2->TrackSubposition); - VehicleTrackSubposition ch = std::max(TrackSubposition, vehicle2->TrackSubposition); - if (cl != ch) - { - if (cl == VehicleTrackSubposition::GoKartsLeftLane && ch == VehicleTrackSubposition::GoKartsRightLane) - continue; - } - - uint32_t ecx = var_44 + vehicle2->var_44; - ecx = ((ecx >> 1) * 30) >> 8; - - if (x_diff + y_diff >= ecx) - continue; - - if (!collideCarEntry->flags.has(CarEntryFlag::isGoKart)) - { - collideVehicle = vehicle2; - mayCollide = true; - break; - } - - uint8_t direction = (Orientation - vehicle2->Orientation - 6) & 0x1F; - - if (direction < 0x14) - continue; - - const CoordsXY directionVector = Math::Trigonometry::YawToDirectionVector[Entity::Yaw::YawTo64(Orientation)]; - - const CoordsXY directionVectorToVehicle2 = { vehicle2->x - loc.x, vehicle2->y - loc.y }; - const int32_t directionVectorToVehicle2Length = (directionVectorToVehicle2.x * directionVectorToVehicle2.x) - + (directionVectorToVehicle2.y * directionVectorToVehicle2.y); - - const int32_t dotProduct = (directionVector.x * directionVectorToVehicle2.x) - + (directionVector.y * directionVectorToVehicle2.y); - - static constexpr int32_t threshold = []() consteval { - const constexpr float originalThreshold = 0.35f; - const constexpr float directionVectorLength = 256.0f; - const constexpr float thresholdLength = originalThreshold * directionVectorLength; - return static_cast(thresholdLength * thresholdLength); - }(); - static_assert(threshold == 8028); - - if (dotProduct > 0 && dotProduct * dotProduct > threshold * directionVectorToVehicle2Length) - { - collideVehicle = vehicle2; - mayCollide = true; - break; - } - } - if (mayCollide) - { - break; - } - } - - if (!mayCollide || collideVehicle == nullptr) - { - CollisionDetectionTimer = 0; - return false; - } - - if (collideVehicle->status == Status::travellingBoat && sub_state == BoatHireSubState::EnteringReturnPosition) - { - return false; - } - - CollisionDetectionTimer++; - if (CollisionDetectionTimer < 200) - { - SetFlag(VehicleFlags::CurrentlyColliding); - if (otherVehicleIndex != nullptr) - *otherVehicleIndex = collideVehicle->Id; - return true; - } - - if (status == Status::movingToEndOfStation) - { - if (Orientation == 0) - { - if (x <= collideVehicle->x) - { - return false; - } - } - else if (Orientation == 8) - { - if (y >= collideVehicle->y) - { - return false; - } - } - else if (Orientation == 16) - { - if (x >= collideVehicle->x) - { - return false; - } - } - else if (Orientation == 24) - { - if (y <= collideVehicle->y) - { - return false; - } - } - } - - if ((collideVehicle->status == Status::travellingBoat) && (status != Status::arriving) && (status != Status::travelling)) - { - return false; - } - - SetFlag(VehicleFlags::CurrentlyColliding); - if (otherVehicleIndex != nullptr) - *otherVehicleIndex = collideVehicle->Id; - return true; -} - -/** - * - * rct2: 0x006DB7D6 - */ -void Vehicle::ReverseReverserCar() -{ - Vehicle* previousVehicle = getGameState().entities.GetEntity(prev_vehicle_on_ride); - Vehicle* nextVehicle = getGameState().entities.GetEntity(next_vehicle_on_ride); - if (previousVehicle == nullptr || nextVehicle == nullptr) - { - return; - } - - track_progress = 168; - vehicle_type ^= 1; - - previousVehicle->track_progress = 86; - nextVehicle->track_progress = 158; - - nextVehicle->UpdateReverserCarBogies(); - previousVehicle->UpdateReverserCarBogies(); -} - const RideObjectEntry* Vehicle::GetRideEntry() const { return GetRideEntryByIndex(ride_subtype); From 68274c31f57478a8aff89877bf123d85370519ac Mon Sep 17 00:00:00 2001 From: frozensnowy Date: Wed, 11 Feb 2026 21:51:27 +0100 Subject: [PATCH 3/6] Fix: Removed unused constants --- src/openrct2/ride/Vehicle.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/openrct2/ride/Vehicle.cpp b/src/openrct2/ride/Vehicle.cpp index 07a1e5181e..3470fb1932 100644 --- a/src/openrct2/ride/Vehicle.cpp +++ b/src/openrct2/ride/Vehicle.cpp @@ -66,13 +66,8 @@ using namespace OpenRCT2::TrackMetaData; using namespace OpenRCT2::RideVehicle; constexpr int16_t kVehicleMaxSpinSpeedForStopping = 700; -constexpr int16_t kVehicleMaxSpinSpeedWaterRide = 512; -constexpr int16_t kVehicleMinSpinSpeedWaterRide = -kVehicleMaxSpinSpeedWaterRide; constexpr int16_t kVehicleStoppingSpinSpeed = 600; -constexpr uint8_t kTrackSpeedShiftAmount = 16; -constexpr uint8_t kBoosterAccelerationShiftAmount = 16; - Vehicle* gCurrentVehicle; uint8_t _vehicleBreakdown; From 1140f441affa536f087ebe8d618ce7555753d853 Mon Sep 17 00:00:00 2001 From: frozensnowy Date: Wed, 11 Feb 2026 22:24:37 +0100 Subject: [PATCH 4/6] Refactor: Move dodgems logic to Vehicle.Dodgems.cpp Moves dodgems mode, motion, and collision detection to a dedicated file --- src/openrct2/libopenrct2.vcxproj | 1 + src/openrct2/ride/Vehicle.Dodgems.cpp | 320 ++++++++++++++++++++++++++ src/openrct2/ride/Vehicle.cpp | 277 ---------------------- 3 files changed, 321 insertions(+), 277 deletions(-) create mode 100644 src/openrct2/ride/Vehicle.Dodgems.cpp diff --git a/src/openrct2/libopenrct2.vcxproj b/src/openrct2/libopenrct2.vcxproj index 2c47f86cfc..f320e59d8e 100644 --- a/src/openrct2/libopenrct2.vcxproj +++ b/src/openrct2/libopenrct2.vcxproj @@ -1129,6 +1129,7 @@ + diff --git a/src/openrct2/ride/Vehicle.Dodgems.cpp b/src/openrct2/ride/Vehicle.Dodgems.cpp new file mode 100644 index 0000000000..39ed07dc2b --- /dev/null +++ b/src/openrct2/ride/Vehicle.Dodgems.cpp @@ -0,0 +1,320 @@ +/***************************************************************************** + * 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 "../core/Speed.hpp" +#include "../GameState.h" +#include "../entity/EntityList.h" +#include "../entity/EntityRegistry.h" +#include "../scenario/Scenario.h" +#include "Ride.h" +#include "TrackData.h" +#include "VehicleGeometry.h" + +using namespace OpenRCT2; +using namespace OpenRCT2::TrackMetaData; +using namespace OpenRCT2::RideVehicle; + +// External globals from Vehicle.cpp +extern uint32_t _vehicleMotionTrackFlags; +extern int32_t _vehicleVelocityF64E08; +extern int32_t _vehicleVelocityF64E0C; +extern int32_t _vehicleUnkF64E10; +extern CoordsXYZ _vehicleCurPosition; + +/** rct2: 0x009A37C4 */ +static constexpr CoordsXY kSurroundingTiles[] = { + { 0, 0 }, + { 0, +kCoordsXYStep }, + { +kCoordsXYStep, 0 }, + { 0, -kCoordsXYStep }, + { 0, -kCoordsXYStep }, + { -kCoordsXYStep, 0 }, + { -kCoordsXYStep, 0 }, + { 0, +kCoordsXYStep }, + { 0, +kCoordsXYStep }, +}; + +/** + * + * rct2: 0x006D91BF + */ +void Vehicle::UpdateDodgemsMode() +{ + auto curRide = GetRide(); + if (curRide == nullptr) + return; + + const auto* rideEntry = GetRideEntry(); + if (rideEntry == nullptr) + { + return; + } + const auto& carEntry = rideEntry->Cars[vehicle_type]; + + // Mark the dodgem as in use. + if (carEntry.flags.has(CarEntryFlag::hasDodgemInUseLights) && animation_frame != 1) + { + animation_frame = 1; + Invalidate(); + } + + UpdateMotionDodgems(); + + // Update the length of time vehicle has been in dodgems mode + if (sub_state++ == 0xFF) + { + TimeActive++; + } + + if (curRide->lifecycleFlags & RIDE_LIFECYCLE_PASS_STATION_NO_STOPPING) + return; + + // Mark the dodgem as not in use. + animation_frame = 0; + Invalidate(); + velocity = 0; + acceleration = 0; + SetState(Status::unloadingPassengers); +} + +/** + * + * rct2: 0x006DA44E + */ +int32_t Vehicle::UpdateMotionDodgems() +{ + _vehicleMotionTrackFlags = 0; + + auto curRide = GetRide(); + if (curRide == nullptr) + return _vehicleMotionTrackFlags; + + int32_t nextVelocity = velocity + acceleration; + if (curRide->lifecycleFlags & (RIDE_LIFECYCLE_BREAKDOWN_PENDING | RIDE_LIFECYCLE_BROKEN_DOWN) + && curRide->breakdownReasonPending == BREAKDOWN_SAFETY_CUT_OUT) + { + nextVelocity = 0; + } + velocity = nextVelocity; + + _vehicleVelocityF64E08 = nextVelocity; + _vehicleVelocityF64E0C = (nextVelocity / 1024) * 42; + _vehicleUnkF64E10 = 1; + + acceleration = 0; + if (!(curRide->lifecycleFlags & (RIDE_LIFECYCLE_BREAKDOWN_PENDING | RIDE_LIFECYCLE_BROKEN_DOWN)) + || curRide->breakdownReasonPending != BREAKDOWN_SAFETY_CUT_OUT) + { + if ((getGameState().currentTicks & 1) && var_34 != 0) + { + if (var_34 > 0) + { + var_34--; + Orientation += 2; + } + else + { + var_34++; + Orientation -= 2; + } + Orientation &= 0x1E; + Invalidate(); + } + else if ((ScenarioRand() & 0xFFFF) <= 2849) + { + if (var_35 & (1 << 6)) + Orientation -= 2; + else + Orientation += 2; + Orientation &= 0x1E; + Invalidate(); + } + } + + std::optional collideSprite; + if (DodgemsCollisionDirection != 0) + { + uint8_t oldCollisionDirection = DodgemsCollisionDirection & 0x1E; + DodgemsCollisionDirection = 0; + + CoordsXYZ location = { x, y, z }; + + location.x += Geometry::getFreeroamVehicleMovementData(oldCollisionDirection).x; + location.y += Geometry::getFreeroamVehicleMovementData(oldCollisionDirection).y; + location.x += Geometry::getFreeroamVehicleMovementData(oldCollisionDirection + 1).x; + location.y += Geometry::getFreeroamVehicleMovementData(oldCollisionDirection + 1).y; + + if (collideSprite = DodgemsCarWouldCollideAt(location); !collideSprite.has_value()) + { + MoveTo(location); + } + } + + remaining_distance += _vehicleVelocityF64E0C; + + if (remaining_distance >= 13962) + { + sound2_flags &= ~VEHICLE_SOUND2_FLAGS_LIFT_HILL; + _vehicleCurPosition.x = x; + _vehicleCurPosition.y = y; + _vehicleCurPosition.z = z; + + while (true) + { + var_35++; + uint8_t direction = Orientation; + direction |= var_35 & 1; + + CoordsXY location = _vehicleCurPosition; + location.x += Geometry::getFreeroamVehicleMovementData(direction).x; + location.y += Geometry::getFreeroamVehicleMovementData(direction).y; + + if (collideSprite = DodgemsCarWouldCollideAt(location); collideSprite.has_value()) + { + break; + } + + remaining_distance -= Geometry::getFreeroamVehicleMovementData(direction).distance; + _vehicleCurPosition.x = location.x; + _vehicleCurPosition.y = location.y; + if (remaining_distance < 13962) + { + break; + } + _vehicleUnkF64E10++; + } + + if (remaining_distance >= 13962) + { + int32_t oldVelocity = velocity; + remaining_distance = 0; + velocity = 0; + uint8_t direction = Orientation | 1; + + Vehicle* collideVehicle = getGameState().entities.GetEntity(collideSprite.value()); + if (collideVehicle != nullptr) + { + var_34 = (ScenarioRand() & 1) ? 1 : -1; + + if (oldVelocity >= 2.0_mph) + { + collideVehicle->DodgemsCollisionDirection = direction; + DodgemsCollisionDirection = direction ^ (1 << 4); + } + } + else + { + var_34 = (ScenarioRand() & 1) ? 6 : -6; + + if (oldVelocity >= 2.0_mph) + { + DodgemsCollisionDirection = direction ^ (1 << 4); + } + } + } + + MoveTo(_vehicleCurPosition); + } + + int32_t eax = velocity / 2; + int32_t edx = velocity >> 8; + edx *= edx; + if (velocity < 0) + edx = -edx; + edx >>= 5; + eax += edx; + if (mass != 0) + { + eax /= mass; + } + const auto* rideEntry = GetRideEntry(); + const auto& carEntry = rideEntry->Cars[vehicle_type]; + + if (!carEntry.flags.has(CarEntryFlag::isPowered)) + { + acceleration = -eax; + return _vehicleMotionTrackFlags; + } + + int32_t momentum = (speed * mass) >> 2; + int32_t _eax = speed << 14; + if (HasFlag(VehicleFlags::PoweredCarInReverse)) + { + _eax = -_eax; + } + _eax -= velocity; + _eax *= powered_acceleration * 2; + if (momentum != 0) + _eax /= momentum; + + acceleration = _eax - eax; + return _vehicleMotionTrackFlags; +} + +/** + * + * rct2: 0x006DD365 + */ +static bool wouldCollideWithDodgemsTrackEdge( + const CoordsXY& coords, const CoordsXY& trackLocation, TrackElemType trackType, uint16_t dodgemsCarRadius) +{ + int16_t rideLeft = trackLocation.x + GetDodgemsTrackSize(trackType).left; + int16_t rideRight = trackLocation.x + GetDodgemsTrackSize(trackType).right; + int16_t rideTop = trackLocation.y + GetDodgemsTrackSize(trackType).top; + int16_t rideBottom = trackLocation.y + GetDodgemsTrackSize(trackType).bottom; + + return coords.x - dodgemsCarRadius < rideLeft || coords.y - dodgemsCarRadius < rideTop + || coords.x + dodgemsCarRadius > rideRight || coords.y + dodgemsCarRadius > rideBottom; +} + +std::optional Vehicle::DodgemsCarWouldCollideAt(const CoordsXY& coords) const +{ + auto trackType = GetTrackType(); + + if (wouldCollideWithDodgemsTrackEdge(coords, TrackLocation, trackType, (var_44 * 30) >> 9)) + { + return EntityId::GetNull(); + } + + auto location = coords; + + RideId rideIndex = ride; + for (auto xy_offset : kSurroundingTiles) + { + location += xy_offset; + + for (auto vehicle2 : EntityTileList(location)) + { + if (vehicle2 == this) + continue; + if (vehicle2->ride != rideIndex) + continue; + + int32_t distX = abs(coords.x - vehicle2->x); + if (distX > 32768) + continue; + + int32_t distY = abs(coords.y - vehicle2->y); + if (distY > 32768) + continue; + + int32_t ecx = (var_44 + vehicle2->var_44) / 2; + ecx *= 30; + ecx >>= 8; + if (std::max(distX, distY) < ecx) + { + return vehicle2->Id; + } + } + } + + return std::nullopt; +} diff --git a/src/openrct2/ride/Vehicle.cpp b/src/openrct2/ride/Vehicle.cpp index 3470fb1932..4d8b67860f 100644 --- a/src/openrct2/ride/Vehicle.cpp +++ b/src/openrct2/ride/Vehicle.cpp @@ -904,49 +904,6 @@ std::optional ride_get_train_index_from_vehicle(const Ride& ride, Enti return { trainIndex }; } -/** - * - * rct2: 0x006D91BF - */ -void Vehicle::UpdateDodgemsMode() -{ - auto curRide = GetRide(); - if (curRide == nullptr) - return; - - const auto* rideEntry = GetRideEntry(); - if (rideEntry == nullptr) - { - return; - } - const auto& carEntry = rideEntry->Cars[vehicle_type]; - - // Mark the dodgem as in use. - if (carEntry.flags.has(CarEntryFlag::hasDodgemInUseLights) && animation_frame != 1) - { - animation_frame = 1; - Invalidate(); - } - - UpdateMotionDodgems(); - - // Update the length of time vehicle has been in dodgems mode - if (sub_state++ == 0xFF) - { - TimeActive++; - } - - if (curRide->lifecycleFlags & RIDE_LIFECYCLE_PASS_STATION_NO_STOPPING) - return; - - // Mark the dodgem as not in use. - animation_frame = 0; - Invalidate(); - velocity = 0; - acceleration = 0; - SetState(Status::unloadingPassengers); -} - /** * * rct2: 0x006D9EB0 @@ -1313,240 +1270,6 @@ int32_t Vehicle::IsUsedInPairs() const return num_seats & kVehicleSeatPairFlag; } -/** - * - * rct2: 0x006DA44E - */ -int32_t Vehicle::UpdateMotionDodgems() -{ - _vehicleMotionTrackFlags = 0; - - auto curRide = GetRide(); - if (curRide == nullptr) - return _vehicleMotionTrackFlags; - - int32_t nextVelocity = velocity + acceleration; - if (curRide->lifecycleFlags & (RIDE_LIFECYCLE_BREAKDOWN_PENDING | RIDE_LIFECYCLE_BROKEN_DOWN) - && curRide->breakdownReasonPending == BREAKDOWN_SAFETY_CUT_OUT) - { - nextVelocity = 0; - } - velocity = nextVelocity; - - _vehicleVelocityF64E08 = nextVelocity; - _vehicleVelocityF64E0C = (nextVelocity / 1024) * 42; - _vehicleUnkF64E10 = 1; - - acceleration = 0; - if (!(curRide->lifecycleFlags & (RIDE_LIFECYCLE_BREAKDOWN_PENDING | RIDE_LIFECYCLE_BROKEN_DOWN)) - || curRide->breakdownReasonPending != BREAKDOWN_SAFETY_CUT_OUT) - { - if ((getGameState().currentTicks & 1) && var_34 != 0) - { - if (var_34 > 0) - { - var_34--; - Orientation += 2; - } - else - { - var_34++; - Orientation -= 2; - } - Orientation &= 0x1E; - Invalidate(); - } - else if ((ScenarioRand() & 0xFFFF) <= 2849) - { - if (var_35 & (1 << 6)) - Orientation -= 2; - else - Orientation += 2; - Orientation &= 0x1E; - Invalidate(); - } - } - - std::optional collideSprite; - if (DodgemsCollisionDirection != 0) - { - uint8_t oldCollisionDirection = DodgemsCollisionDirection & 0x1E; - DodgemsCollisionDirection = 0; - - CoordsXYZ location = { x, y, z }; - - location.x += Geometry::getFreeroamVehicleMovementData(oldCollisionDirection).x; - location.y += Geometry::getFreeroamVehicleMovementData(oldCollisionDirection).y; - location.x += Geometry::getFreeroamVehicleMovementData(oldCollisionDirection + 1).x; - location.y += Geometry::getFreeroamVehicleMovementData(oldCollisionDirection + 1).y; - - if (collideSprite = DodgemsCarWouldCollideAt(location); !collideSprite.has_value()) - { - MoveTo(location); - } - } - - remaining_distance += _vehicleVelocityF64E0C; - - if (remaining_distance >= 13962) - { - sound2_flags &= ~VEHICLE_SOUND2_FLAGS_LIFT_HILL; - _vehicleCurPosition.x = x; - _vehicleCurPosition.y = y; - _vehicleCurPosition.z = z; - - while (true) - { - var_35++; - uint8_t direction = Orientation; - direction |= var_35 & 1; - - CoordsXY location = _vehicleCurPosition; - location.x += Geometry::getFreeroamVehicleMovementData(direction).x; - location.y += Geometry::getFreeroamVehicleMovementData(direction).y; - - if (collideSprite = DodgemsCarWouldCollideAt(location); collideSprite.has_value()) - { - break; - } - - remaining_distance -= Geometry::getFreeroamVehicleMovementData(direction).distance; - _vehicleCurPosition.x = location.x; - _vehicleCurPosition.y = location.y; - if (remaining_distance < 13962) - { - break; - } - _vehicleUnkF64E10++; - } - - if (remaining_distance >= 13962) - { - int32_t oldVelocity = velocity; - remaining_distance = 0; - velocity = 0; - uint8_t direction = Orientation | 1; - - Vehicle* collideVehicle = getGameState().entities.GetEntity(collideSprite.value()); - if (collideVehicle != nullptr) - { - var_34 = (ScenarioRand() & 1) ? 1 : -1; - - if (oldVelocity >= 2.0_mph) - { - collideVehicle->DodgemsCollisionDirection = direction; - DodgemsCollisionDirection = direction ^ (1 << 4); - } - } - else - { - var_34 = (ScenarioRand() & 1) ? 6 : -6; - - if (oldVelocity >= 2.0_mph) - { - DodgemsCollisionDirection = direction ^ (1 << 4); - } - } - } - - MoveTo(_vehicleCurPosition); - } - - int32_t eax = velocity / 2; - int32_t edx = velocity >> 8; - edx *= edx; - if (velocity < 0) - edx = -edx; - edx >>= 5; - eax += edx; - if (mass != 0) - { - eax /= mass; - } - const auto* rideEntry = GetRideEntry(); - const auto& carEntry = rideEntry->Cars[vehicle_type]; - - if (!carEntry.flags.has(CarEntryFlag::isPowered)) - { - acceleration = -eax; - return _vehicleMotionTrackFlags; - } - - int32_t momentum = (speed * mass) >> 2; - int32_t _eax = speed << 14; - if (HasFlag(VehicleFlags::PoweredCarInReverse)) - { - _eax = -_eax; - } - _eax -= velocity; - _eax *= powered_acceleration * 2; - if (momentum != 0) - _eax /= momentum; - - acceleration = _eax - eax; - return _vehicleMotionTrackFlags; -} - -/** - * - * rct2: 0x006DD365 - */ -static bool wouldCollideWithDodgemsTrackEdge( - const CoordsXY& coords, const CoordsXY& trackLocation, TrackElemType trackType, uint16_t dodgemsCarRadius) -{ - int16_t rideLeft = trackLocation.x + GetDodgemsTrackSize(trackType).left; - int16_t rideRight = trackLocation.x + GetDodgemsTrackSize(trackType).right; - int16_t rideTop = trackLocation.y + GetDodgemsTrackSize(trackType).top; - int16_t rideBottom = trackLocation.y + GetDodgemsTrackSize(trackType).bottom; - - return coords.x - dodgemsCarRadius < rideLeft || coords.y - dodgemsCarRadius < rideTop - || coords.x + dodgemsCarRadius > rideRight || coords.y + dodgemsCarRadius > rideBottom; -} - -std::optional Vehicle::DodgemsCarWouldCollideAt(const CoordsXY& coords) const -{ - auto trackType = GetTrackType(); - - if (wouldCollideWithDodgemsTrackEdge(coords, TrackLocation, trackType, (var_44 * 30) >> 9)) - { - return EntityId::GetNull(); - } - - auto location = coords; - - RideId rideIndex = ride; - for (auto xy_offset : kSurroundingTiles) - { - location += xy_offset; - - for (auto vehicle2 : EntityTileList(location)) - { - if (vehicle2 == this) - continue; - if (vehicle2->ride != rideIndex) - continue; - - int32_t distX = abs(coords.x - vehicle2->x); - if (distX > 32768) - continue; - - int32_t distY = abs(coords.y - vehicle2->y); - if (distY > 32768) - continue; - - int32_t ecx = (var_44 + vehicle2->var_44) / 2; - ecx *= 30; - ecx >>= 8; - if (std::max(distX, distY) < ecx) - { - return vehicle2->Id; - } - } - } - - return std::nullopt; -} - /** * * rct2: 0x006DEDB1 From e00c8aade8940e4dbe89a6592277bfb0ee7822b8 Mon Sep 17 00:00:00 2001 From: frozensnowy Date: Wed, 11 Feb 2026 22:33:46 +0100 Subject: [PATCH 5/6] Refactor: Move door animation logic to Vehicle.SceneryDoors.cpp Moves scenery and landscape door functions to a dedicated file --- src/openrct2/libopenrct2.vcxproj | 1 + src/openrct2/ride/Vehicle.SceneryDoors.cpp | 219 +++++++++++++++++++++ src/openrct2/ride/Vehicle.cpp | 193 ------------------ 3 files changed, 220 insertions(+), 193 deletions(-) create mode 100644 src/openrct2/ride/Vehicle.SceneryDoors.cpp diff --git a/src/openrct2/libopenrct2.vcxproj b/src/openrct2/libopenrct2.vcxproj index f320e59d8e..cfb8a4958a 100644 --- a/src/openrct2/libopenrct2.vcxproj +++ b/src/openrct2/libopenrct2.vcxproj @@ -1130,6 +1130,7 @@ + diff --git a/src/openrct2/ride/Vehicle.SceneryDoors.cpp b/src/openrct2/ride/Vehicle.SceneryDoors.cpp new file mode 100644 index 0000000000..ea15a1cf94 --- /dev/null +++ b/src/openrct2/ride/Vehicle.SceneryDoors.cpp @@ -0,0 +1,219 @@ +/***************************************************************************** + * 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 "../audio/Audio.h" +#include "../core/EnumUtils.hpp" +#include "../world/Map.h" +#include "../world/MapAnimation.h" +#include "../world/tile_element/SurfaceElement.h" +#include "../world/tile_element/TileElement.h" +#include "../world/tile_element/TrackElement.h" +#include "../world/tile_element/WallElement.h" +#include "Ride.h" +#include "RideData.h" +#include "TrackData.h" + +using namespace OpenRCT2; +using namespace OpenRCT2::Audio; +using namespace OpenRCT2::TrackMetaData; + +static constexpr SoundId kDoorOpenSoundIds[] = { + SoundId::null, // DoorSoundType::none + SoundId::doorOpen, // DoorSoundType::door + SoundId::portcullis, // DoorSoundType::portcullis +}; +static_assert(std::size(kDoorOpenSoundIds) == kDoorSoundTypeCount); + +static constexpr SoundId kDoorCloseSoundIds[] = { + SoundId::null, // DoorSoundType::none + SoundId::doorClose, // DoorSoundType::door + SoundId::portcullis, // DoorSoundType::portcullis +}; +static_assert(std::size(kDoorCloseSoundIds) == kDoorSoundTypeCount); + +/** + * + * rct2: 0x006DEDB1 + */ +static void play_scenery_door_open_sound(const CoordsXYZ& loc, WallElement* tileElement) +{ + auto* wallEntry = tileElement->GetEntry(); + if (wallEntry == nullptr) + return; + + auto doorSoundType = wallEntry->getDoorSoundType(); + if (doorSoundType == DoorSoundType::none) + return; + + auto soundId = kDoorOpenSoundIds[EnumValue(doorSoundType)]; + Play3D(soundId, loc); +} + +/** + * + * rct2: 0x006DED7A + */ +static void play_scenery_door_close_sound(const CoordsXYZ& loc, WallElement* tileElement) +{ + auto* wallEntry = tileElement->GetEntry(); + if (wallEntry == nullptr) + return; + + auto doorSoundType = wallEntry->getDoorSoundType(); + if (doorSoundType == DoorSoundType::none) + return; + + auto soundId = kDoorCloseSoundIds[EnumValue(doorSoundType)]; + Play3D(soundId, loc); +} + +template +static void AnimateSceneryDoor(const CoordsXYZD& doorLocation, const CoordsXYZ& trackLocation, bool isLastVehicle) +{ + auto door = MapGetWallElementAt(doorLocation); + if (door == nullptr) + { + return; + } + + if (!isLastVehicle && (door->GetAnimationFrame() == 0)) + { + door->SetAnimationIsBackwards(isBackwards); + door->SetAnimationFrame(1); + door->SetIsAnimating(true); + play_scenery_door_open_sound(trackLocation, door); + + MapAnimations::MarkTileForUpdate(TileCoordsXY(doorLocation)); + } + + if (isLastVehicle) + { + door->SetAnimationIsBackwards(isBackwards); + door->SetAnimationFrame(6); + door->SetIsAnimating(true); + play_scenery_door_close_sound(trackLocation, door); + + MapAnimations::MarkTileForUpdate(TileCoordsXY(doorLocation)); + } +} + +/** + * + * rct2: 0x006DEE93 + */ +void Vehicle::UpdateSceneryDoor() const +{ + auto trackType = GetTrackType(); + const auto& ted = GetTrackElementDescriptor(trackType); + const auto& trackBlock = ted.sequences[ted.numSequences - 1].clearance; + const TrackCoordinates* trackCoordinates = &ted.coordinates; + auto wallCoords = CoordsXYZ{ x, y, TrackLocation.z - trackBlock.z + trackCoordinates->zEnd }.ToTileStart(); + int32_t direction = (GetTrackDirection() + trackCoordinates->rotationEnd) & 3; + + AnimateSceneryDoor({ wallCoords, static_cast(direction) }, TrackLocation, next_vehicle_on_train.IsNull()); +} + +template +static void AnimateLandscapeDoor( + const CoordsXYZ& doorLocation, TrackElement& trackElement, const bool isLastVehicle, const DoorSoundType doorSound, + const CoordsXYZ& soundLocation) +{ + const auto doorState = isBackwards ? trackElement.GetDoorAState() : trackElement.GetDoorBState(); + if (!isLastVehicle && doorState == kLandEdgeDoorFrameClosed) + { + if (isBackwards) + trackElement.SetDoorAState(kLandEdgeDoorFrameOpening); + else + trackElement.SetDoorBState(kLandEdgeDoorFrameOpening); + + MapAnimations::CreateTemporary(doorLocation, MapAnimations::TemporaryType::landEdgeDoor); + Play3D(kDoorOpenSoundIds[EnumValue(doorSound)], soundLocation); + } + + if (isLastVehicle) + { + if (isBackwards) + trackElement.SetDoorAState(kLandEdgeDoorFrameClosing); + else + trackElement.SetDoorBState(kLandEdgeDoorFrameClosing); + + MapAnimations::CreateTemporary(doorLocation, MapAnimations::TemporaryType::landEdgeDoor); + Play3D(kDoorCloseSoundIds[EnumValue(doorSound)], soundLocation); + } +} + +static const SurfaceElement* GetSurfaceElementAfterElement(const TileElement* tileElement) +{ + while (tileElement->GetType() != TileElementType::Surface) + { + if (tileElement->IsLastForTile()) + return nullptr; + tileElement++; + } + return tileElement->AsSurface(); +} + +void Vehicle::UpdateLandscapeDoors(const int32_t previousTrackHeight) const +{ + const auto* currentRide = GetRide(); + if (currentRide == nullptr || !currentRide->getRideTypeDescriptor().flags.has(RtdFlag::hasLandscapeDoors)) + { + return; + } + + const CoordsXYZ previousTrackLocation = CoordsXYZ(x, y, previousTrackHeight).ToTileStart(); + auto* const previousTrackElement = MapGetTrackElementAtBeforeSurfaceFromRide(previousTrackLocation, ride); + auto* const currentTrackElement = MapGetTrackElementAtBeforeSurfaceFromRide(TrackLocation, ride); + if (previousTrackElement != nullptr && currentTrackElement == nullptr) + { + const auto* const surfaceElement = GetSurfaceElementAfterElement(previousTrackElement); + if (surfaceElement != nullptr && surfaceElement->GetBaseZ() > previousTrackLocation.z) + { + const auto* const edgeObject = surfaceElement->GetEdgeObject(); + if (edgeObject != nullptr && edgeObject->HasDoors) + { + AnimateLandscapeDoor( + previousTrackLocation, *previousTrackElement->AsTrack(), next_vehicle_on_train.IsNull(), + edgeObject->doorSound, TrackLocation); + } + } + } + else if (previousTrackElement == nullptr && currentTrackElement != nullptr) + { + const auto* const surfaceElement = GetSurfaceElementAfterElement(currentTrackElement); + if (surfaceElement != nullptr && surfaceElement->GetBaseZ() > TrackLocation.z) + { + const auto* const edgeObject = surfaceElement->GetEdgeObject(); + if (edgeObject != nullptr && edgeObject->HasDoors) + { + AnimateLandscapeDoor( + TrackLocation, *currentTrackElement->AsTrack(), next_vehicle_on_train.IsNull(), edgeObject->doorSound, + previousTrackLocation); + } + } + } +} + +/** + * + * rct2: 0x006DEDE8 + */ +void Vehicle::UpdateSceneryDoorBackwards() const +{ + auto trackType = GetTrackType(); + const auto& ted = GetTrackElementDescriptor(trackType); + const TrackCoordinates* trackCoordinates = &ted.coordinates; + auto wallCoords = CoordsXYZ{ TrackLocation, TrackLocation.z - ted.sequences[0].clearance.z + trackCoordinates->zBegin }; + int32_t direction = (GetTrackDirection() + trackCoordinates->rotationBegin) & 3; + direction = DirectionReverse(direction); + + AnimateSceneryDoor({ wallCoords, static_cast(direction) }, TrackLocation, next_vehicle_on_train.IsNull()); +} diff --git a/src/openrct2/ride/Vehicle.cpp b/src/openrct2/ride/Vehicle.cpp index 4d8b67860f..d660e780b7 100644 --- a/src/openrct2/ride/Vehicle.cpp +++ b/src/openrct2/ride/Vehicle.cpp @@ -93,20 +93,6 @@ static constexpr CoordsXY kSurroundingTiles[] = { { 0, +kCoordsXYStep }, }; -static constexpr SoundId kDoorOpenSoundIds[] = { - SoundId::null, // DoorSoundType::none - SoundId::doorOpen, // DoorSoundType::door - SoundId::portcullis, // DoorSoundType::portcullis -}; -static_assert(std::size(kDoorOpenSoundIds) == kDoorSoundTypeCount); - -static constexpr SoundId kDoorCloseSoundIds[] = { - SoundId::null, // DoorSoundType::none - SoundId::doorClose, // DoorSoundType::door - SoundId::portcullis, // DoorSoundType::portcullis -}; -static_assert(std::size(kDoorCloseSoundIds) == kDoorSoundTypeCount); - template<> bool EntityBase::Is() const { @@ -1270,185 +1256,6 @@ int32_t Vehicle::IsUsedInPairs() const return num_seats & kVehicleSeatPairFlag; } -/** - * - * rct2: 0x006DEDB1 - */ -static void play_scenery_door_open_sound(const CoordsXYZ& loc, WallElement* tileElement) -{ - auto* wallEntry = tileElement->GetEntry(); - if (wallEntry == nullptr) - return; - - auto doorSoundType = wallEntry->getDoorSoundType(); - if (doorSoundType == DoorSoundType::none) - return; - - auto soundId = kDoorOpenSoundIds[EnumValue(doorSoundType)]; - Play3D(soundId, loc); -} - -/** - * - * rct2: 0x006DED7A - */ -static void play_scenery_door_close_sound(const CoordsXYZ& loc, WallElement* tileElement) -{ - auto* wallEntry = tileElement->GetEntry(); - if (wallEntry == nullptr) - return; - - auto doorSoundType = wallEntry->getDoorSoundType(); - if (doorSoundType == DoorSoundType::none) - return; - - auto soundId = kDoorCloseSoundIds[EnumValue(doorSoundType)]; - Play3D(soundId, loc); -} - -template -static void AnimateSceneryDoor(const CoordsXYZD& doorLocation, const CoordsXYZ& trackLocation, bool isLastVehicle) -{ - auto door = MapGetWallElementAt(doorLocation); - if (door == nullptr) - { - return; - } - - if (!isLastVehicle && (door->GetAnimationFrame() == 0)) - { - door->SetAnimationIsBackwards(isBackwards); - door->SetAnimationFrame(1); - door->SetIsAnimating(true); - play_scenery_door_open_sound(trackLocation, door); - - MapAnimations::MarkTileForUpdate(TileCoordsXY(doorLocation)); - } - - if (isLastVehicle) - { - door->SetAnimationIsBackwards(isBackwards); - door->SetAnimationFrame(6); - door->SetIsAnimating(true); - play_scenery_door_close_sound(trackLocation, door); - - MapAnimations::MarkTileForUpdate(TileCoordsXY(doorLocation)); - } -} - -/** - * - * rct2: 0x006DEE93 - */ -void Vehicle::UpdateSceneryDoor() const -{ - auto trackType = GetTrackType(); - const auto& ted = GetTrackElementDescriptor(trackType); - const auto& trackBlock = ted.sequences[ted.numSequences - 1].clearance; - const TrackCoordinates* trackCoordinates = &ted.coordinates; - auto wallCoords = CoordsXYZ{ x, y, TrackLocation.z - trackBlock.z + trackCoordinates->zEnd }.ToTileStart(); - int32_t direction = (GetTrackDirection() + trackCoordinates->rotationEnd) & 3; - - AnimateSceneryDoor({ wallCoords, static_cast(direction) }, TrackLocation, next_vehicle_on_train.IsNull()); -} - -template -static void AnimateLandscapeDoor( - const CoordsXYZ& doorLocation, TrackElement& trackElement, const bool isLastVehicle, const DoorSoundType doorSound, - const CoordsXYZ& soundLocation) -{ - const auto doorState = isBackwards ? trackElement.GetDoorAState() : trackElement.GetDoorBState(); - if (!isLastVehicle && doorState == kLandEdgeDoorFrameClosed) - { - if (isBackwards) - trackElement.SetDoorAState(kLandEdgeDoorFrameOpening); - else - trackElement.SetDoorBState(kLandEdgeDoorFrameOpening); - - MapAnimations::CreateTemporary(doorLocation, MapAnimations::TemporaryType::landEdgeDoor); - Play3D(kDoorOpenSoundIds[EnumValue(doorSound)], soundLocation); - } - - if (isLastVehicle) - { - if (isBackwards) - trackElement.SetDoorAState(kLandEdgeDoorFrameClosing); - else - trackElement.SetDoorBState(kLandEdgeDoorFrameClosing); - - MapAnimations::CreateTemporary(doorLocation, MapAnimations::TemporaryType::landEdgeDoor); - Play3D(kDoorCloseSoundIds[EnumValue(doorSound)], soundLocation); - } -} - -static const SurfaceElement* GetSurfaceElementAfterElement(const TileElement* tileElement) -{ - while (tileElement->GetType() != TileElementType::Surface) - { - if (tileElement->IsLastForTile()) - return nullptr; - tileElement++; - } - return tileElement->AsSurface(); -} - -void Vehicle::UpdateLandscapeDoors(const int32_t previousTrackHeight) const -{ - const auto* currentRide = GetRide(); - if (currentRide == nullptr || !currentRide->getRideTypeDescriptor().flags.has(RtdFlag::hasLandscapeDoors)) - { - return; - } - - const CoordsXYZ previousTrackLocation = CoordsXYZ(x, y, previousTrackHeight).ToTileStart(); - auto* const previousTrackElement = MapGetTrackElementAtBeforeSurfaceFromRide(previousTrackLocation, ride); - auto* const currentTrackElement = MapGetTrackElementAtBeforeSurfaceFromRide(TrackLocation, ride); - if (previousTrackElement != nullptr && currentTrackElement == nullptr) - { - const auto* const surfaceElement = GetSurfaceElementAfterElement(previousTrackElement); - if (surfaceElement != nullptr && surfaceElement->GetBaseZ() > previousTrackLocation.z) - { - const auto* const edgeObject = surfaceElement->GetEdgeObject(); - if (edgeObject != nullptr && edgeObject->HasDoors) - { - AnimateLandscapeDoor( - previousTrackLocation, *previousTrackElement->AsTrack(), next_vehicle_on_train.IsNull(), - edgeObject->doorSound, TrackLocation); - } - } - } - else if (previousTrackElement == nullptr && currentTrackElement != nullptr) - { - const auto* const surfaceElement = GetSurfaceElementAfterElement(currentTrackElement); - if (surfaceElement != nullptr && surfaceElement->GetBaseZ() > TrackLocation.z) - { - const auto* const edgeObject = surfaceElement->GetEdgeObject(); - if (edgeObject != nullptr && edgeObject->HasDoors) - { - AnimateLandscapeDoor( - TrackLocation, *currentTrackElement->AsTrack(), next_vehicle_on_train.IsNull(), edgeObject->doorSound, - previousTrackLocation); - } - } - } -} - -/** - * - * rct2: 0x006DEDE8 - */ -void Vehicle::UpdateSceneryDoorBackwards() const -{ - auto trackType = GetTrackType(); - const auto& ted = GetTrackElementDescriptor(trackType); - const TrackCoordinates* trackCoordinates = &ted.coordinates; - auto wallCoords = CoordsXYZ{ TrackLocation, TrackLocation.z - ted.sequences[0].clearance.z + trackCoordinates->zBegin }; - int32_t direction = (GetTrackDirection() + trackCoordinates->rotationBegin) & 3; - direction = DirectionReverse(direction); - - AnimateSceneryDoor({ wallCoords, static_cast(direction) }, TrackLocation, next_vehicle_on_train.IsNull()); -} - bool Vehicle::IsCableLift() const { return ride_subtype == kObjectEntryIndexNull; From 7c5caea558085fdad8d0e23b3859f084ccd14380 Mon Sep 17 00:00:00 2001 From: frozensnowy Date: Wed, 11 Feb 2026 22:37:39 +0100 Subject: [PATCH 6/6] Fix: Remove unused constants + fixed include order --- src/openrct2/ride/Vehicle.Dodgems.cpp | 2 +- src/openrct2/ride/Vehicle.cpp | 13 ------------- 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/src/openrct2/ride/Vehicle.Dodgems.cpp b/src/openrct2/ride/Vehicle.Dodgems.cpp index 39ed07dc2b..537852fabe 100644 --- a/src/openrct2/ride/Vehicle.Dodgems.cpp +++ b/src/openrct2/ride/Vehicle.Dodgems.cpp @@ -9,8 +9,8 @@ #include "Vehicle.h" -#include "../core/Speed.hpp" #include "../GameState.h" +#include "../core/Speed.hpp" #include "../entity/EntityList.h" #include "../entity/EntityRegistry.h" #include "../scenario/Scenario.h" diff --git a/src/openrct2/ride/Vehicle.cpp b/src/openrct2/ride/Vehicle.cpp index d660e780b7..71c15d22a2 100644 --- a/src/openrct2/ride/Vehicle.cpp +++ b/src/openrct2/ride/Vehicle.cpp @@ -80,19 +80,6 @@ uint8_t _vehicleF64E2C; Vehicle* _vehicleFrontVehicle; CoordsXYZ _vehicleCurPosition; -/** rct2: 0x009A37C4 */ -static constexpr CoordsXY kSurroundingTiles[] = { - { 0, 0 }, - { 0, +kCoordsXYStep }, - { +kCoordsXYStep, 0 }, - { 0, -kCoordsXYStep }, - { 0, -kCoordsXYStep }, - { -kCoordsXYStep, 0 }, - { -kCoordsXYStep, 0 }, - { 0, +kCoordsXYStep }, - { 0, +kCoordsXYStep }, -}; - template<> bool EntityBase::Is() const {