diff --git a/src/openrct2/ride/Ride.cpp b/src/openrct2/ride/Ride.cpp index d99fe87b3e..3844d7a15c 100644 --- a/src/openrct2/ride/Ride.cpp +++ b/src/openrct2/ride/Ride.cpp @@ -3194,7 +3194,7 @@ static Vehicle* vehicle_create_car( vehicle->sprite_direction = scenario_rand() & 0x1E; chosenLoc.y = dodgemPos.y + (scenario_rand() & 0xFF); chosenLoc.x = dodgemPos.x + (scenario_rand() & 0xFF); - } while (vehicle->DodgemsCarWouldCollideAt(chosenLoc, nullptr)); + } while (vehicle->DodgemsCarWouldCollideAt(chosenLoc).has_value()); vehicle->MoveTo({ chosenLoc, dodgemPos.z }); } diff --git a/src/openrct2/ride/Vehicle.cpp b/src/openrct2/ride/Vehicle.cpp index 08a7067fbb..6c0ce6b62e 100644 --- a/src/openrct2/ride/Vehicle.cpp +++ b/src/openrct2/ride/Vehicle.cpp @@ -6262,7 +6262,7 @@ int32_t Vehicle::UpdateMotionDodgems() } } - auto collideSprite = EntityId::GetNull(); + std::optional collideSprite; if (DodgemsCollisionDirection != 0) { uint8_t oldCollisionDirection = DodgemsCollisionDirection & 0x1E; @@ -6275,7 +6275,7 @@ int32_t Vehicle::UpdateMotionDodgems() location.x += Unk9A36C4[oldCollisionDirection + 1].x; location.y += Unk9A36C4[oldCollisionDirection + 1].y; - if (!DodgemsCarWouldCollideAt(location, &collideSprite)) + if (collideSprite = DodgemsCarWouldCollideAt(location); !collideSprite.has_value()) { MoveTo(location); } @@ -6300,8 +6300,10 @@ int32_t Vehicle::UpdateMotionDodgems() location.x += Unk9A36C4[direction].x; location.y += Unk9A36C4[direction].y; - if (DodgemsCarWouldCollideAt(location, &collideSprite)) + if (collideSprite = DodgemsCarWouldCollideAt(location); collideSprite.has_value()) + { break; + } remaining_distance -= Unk9A36C4[direction].distance; unk_F64E20.x = location.x; @@ -6320,7 +6322,7 @@ int32_t Vehicle::UpdateMotionDodgems() velocity = 0; uint8_t direction = sprite_direction | 1; - Vehicle* collideVehicle = GetEntity(collideSprite); + Vehicle* collideVehicle = GetEntity(collideSprite.value()); if (collideVehicle != nullptr) { var_34 = (scenario_rand() & 1) ? 1 : -1; @@ -6396,16 +6398,13 @@ static bool wouldCollideWithDodgemsTrackEdge( || coords.x + dodgemsCarRadius > rideRight || coords.y + dodgemsCarRadius > rideBottom; } -// TODO: Return optional -bool Vehicle::DodgemsCarWouldCollideAt(const CoordsXY& coords, EntityId* collidedWith) const +std::optional Vehicle::DodgemsCarWouldCollideAt(const CoordsXY& coords) const { auto trackType = GetTrackType(); if (wouldCollideWithDodgemsTrackEdge(coords, TrackLocation, trackType, (var_44 * 30) >> 9)) { - if (collidedWith != nullptr) - *collidedWith = EntityId::GetNull(); - return true; + return EntityId::GetNull(); } auto location = coords; @@ -6435,14 +6434,12 @@ bool Vehicle::DodgemsCarWouldCollideAt(const CoordsXY& coords, EntityId* collide ecx >>= 8; if (std::max(distX, distY) < ecx) { - if (collidedWith != nullptr) - *collidedWith = vehicle2->sprite_index; - return true; + return vehicle2->sprite_index; } } } - return false; + return std::nullopt; } /** diff --git a/src/openrct2/ride/Vehicle.h b/src/openrct2/ride/Vehicle.h index 7792939d66..e07a8f1d91 100644 --- a/src/openrct2/ride/Vehicle.h +++ b/src/openrct2/ride/Vehicle.h @@ -220,7 +220,7 @@ struct Vehicle : EntityBase void SetState(Vehicle::Status vehicleStatus, uint8_t subState = 0); bool IsGhost() const; void UpdateSoundParams(std::vector& vehicleSoundParamsList) const; - bool DodgemsCarWouldCollideAt(const CoordsXY& coords, EntityId* spriteId) const; + std::optional DodgemsCarWouldCollideAt(const CoordsXY& coords) const; int32_t UpdateTrackMotion(int32_t* outStation); int32_t CableLiftUpdateTrackMotion(); GForces GetGForces() const;