forked from rosebudai/OpenRCT2-Web
Merge pull request #26005 from frozensnowy/vehicle-refactor-p2
Refactor: Split more of Vehicle.cpp into focused files
This commit is contained in:
6 files changed
+2450
-2314
No files matched your search
@@ -1127,7 +1127,10 @@
|
||||
<ClCompile Include="ride\TrackStyle.cpp" />
|
||||
<ClCompile Include="ride\TrainManager.cpp" />
|
||||
<ClCompile Include="ride\Vehicle.BoatHire.cpp" />
|
||||
<ClCompile Include="ride\Vehicle.Collision.cpp" />
|
||||
<ClCompile Include="ride\Vehicle.Crash.cpp" />
|
||||
<ClCompile Include="ride\Vehicle.Dodgems.cpp" />
|
||||
<ClCompile Include="ride\Vehicle.SceneryDoors.cpp" />
|
||||
<ClCompile Include="ride\Vehicle.cpp" />
|
||||
<ClCompile Include="ride\Vehicle.Animation.cpp" />
|
||||
<ClCompile Include="ride\Vehicle.MiniGolf.cpp">
|
||||
@@ -1135,6 +1138,7 @@
|
||||
</ClCompile>
|
||||
<ClCompile Include="ride\Vehicle.Sound.cpp" />
|
||||
<ClCompile Include="ride\Vehicle.Station.cpp" />
|
||||
<ClCompile Include="ride\Vehicle.TrackMotion.cpp" />
|
||||
<ClCompile Include="ride\VehicleData.cpp" />
|
||||
<ClCompile Include="ride\VehicleGeometry.cpp" />
|
||||
<ClCompile Include="ride\VehicleRiderControl.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<Vehicle>(*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<Vehicle>(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<int32_t>(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<Vehicle>(prev_vehicle_on_ride);
|
||||
Vehicle* nextVehicle = getGameState().entities.GetEntity<Vehicle>(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();
|
||||
}
|
||||
@@ -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 "../GameState.h"
|
||||
#include "../core/Speed.hpp"
|
||||
#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<EntityId> 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<Vehicle>(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<EntityId> 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<Vehicle>(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;
|
||||
}
|
||||
@@ -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<bool isBackwards>
|
||||
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<false>({ wallCoords, static_cast<Direction>(direction) }, TrackLocation, next_vehicle_on_train.IsNull());
|
||||
}
|
||||
|
||||
template<bool isBackwards>
|
||||
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<false>(
|
||||
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<true>(
|
||||
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<true>({ wallCoords, static_cast<Direction>(direction) }, TrackLocation, next_vehicle_on_train.IsNull());
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user