Refactor DodgemsCarWouldCollideAt to return optional (#16695)

This commit is contained in:
GalBr 2022-02-24 23:29:11 +02:00 committed by GitHub
parent 501328bc9a
commit f44d944b26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 15 deletions

View File

@ -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 });
}

View File

@ -6262,7 +6262,7 @@ int32_t Vehicle::UpdateMotionDodgems()
}
}
auto collideSprite = EntityId::GetNull();
std::optional<EntityId> 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<Vehicle>(collideSprite);
Vehicle* collideVehicle = GetEntity<Vehicle>(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<EntityId>
bool Vehicle::DodgemsCarWouldCollideAt(const CoordsXY& coords, EntityId* collidedWith) const
std::optional<EntityId> 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;
}
/**

View File

@ -220,7 +220,7 @@ struct Vehicle : EntityBase
void SetState(Vehicle::Status vehicleStatus, uint8_t subState = 0);
bool IsGhost() const;
void UpdateSoundParams(std::vector<OpenRCT2::Audio::VehicleSoundParams>& vehicleSoundParamsList) const;
bool DodgemsCarWouldCollideAt(const CoordsXY& coords, EntityId* spriteId) const;
std::optional<EntityId> DodgemsCarWouldCollideAt(const CoordsXY& coords) const;
int32_t UpdateTrackMotion(int32_t* outStation);
int32_t CableLiftUpdateTrackMotion();
GForces GetGForces() const;