1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-22 14:24:33 +01:00

Merge pull request #24035 from ZehMatt/code-overhaul-2

Some code cleanup
This commit is contained in:
Matt
2025-03-20 22:00:09 +02:00
committed by GitHub
7 changed files with 69 additions and 121 deletions

View File

@@ -9,6 +9,7 @@
#pragma once
#include <compare>
#include <cstdint>
#include <cstdio>
@@ -62,43 +63,5 @@ public:
return _handle == ValueType::Null;
}
constexpr bool operator==(const ValueType other) const noexcept
{
return _handle == other;
}
constexpr bool operator!=(const ValueType other) const noexcept
{
return _handle != other;
}
constexpr bool operator==(const TIdentifier& other) const noexcept
{
return _handle == other._handle;
}
constexpr bool operator!=(const TIdentifier& other) const noexcept
{
return _handle != other._handle;
}
constexpr bool operator<(const TIdentifier& other) const noexcept
{
return ToUnderlying() < other.ToUnderlying();
}
constexpr bool operator<=(const TIdentifier& other) const noexcept
{
return ToUnderlying() <= other.ToUnderlying();
}
constexpr bool operator>(const TIdentifier& other) const noexcept
{
return ToUnderlying() > other.ToUnderlying();
}
constexpr bool operator>=(const TIdentifier& other) const noexcept
{
return ToUnderlying() >= other.ToUnderlying();
}
auto operator<=>(const TIdentifier&) const = default;
};

View File

@@ -45,7 +45,7 @@ public:
while (iter != end && Entity == nullptr)
{
Entity = GetEntity<T>(*iter++);
Entity = TryGetEntity<T>(*iter++);
}
return *this;
}
@@ -119,7 +119,7 @@ public:
while (iter != end && Entity == nullptr)
{
Entity = GetEntity<T>(*iter++);
Entity = TryGetEntity<T>(*iter++);
}
return *this;
}

View File

@@ -1281,8 +1281,7 @@ void Guest::UpdateSitting()
return;
// 691541
uint8_t pathingResult;
PerformNextAction(pathingResult);
const auto [pathingResult, _] = PerformNextAction();
if (!(pathingResult & PATHING_DESTINATION_REACHED))
return;
@@ -5425,8 +5424,7 @@ void Guest::UpdateWalking()
return;
}
uint8_t pathingResult;
PerformNextAction(pathingResult);
const auto [pathingResult, _] = PerformNextAction();
if (!(pathingResult & PATHING_DESTINATION_REACHED))
return;
@@ -5660,8 +5658,7 @@ void Guest::UpdateQueuing()
return;
}
uint8_t pathingResult;
PerformNextAction(pathingResult);
PerformNextAction();
if (!IsActionInterruptable())
return;
if (AnimationGroup == PeepAnimationGroup::Normal)
@@ -5739,8 +5736,7 @@ void Guest::UpdateEnteringPark()
{
if (Var37 != 1)
{
uint8_t pathingResult;
PerformNextAction(pathingResult);
const auto [pathingResult, _] = PerformNextAction();
if ((pathingResult & PATHING_OUTSIDE_PARK))
{
DecrementGuestsHeadingForPark();
@@ -5771,8 +5767,7 @@ void Guest::UpdateLeavingPark()
{
if (Var37 != 0)
{
uint8_t pathingResult;
PerformNextAction(pathingResult);
const auto [pathingResult, _] = PerformNextAction();
if (!(pathingResult & PATHING_OUTSIDE_PARK))
return;
PeepEntityRemove(this);
@@ -5795,8 +5790,7 @@ void Guest::UpdateLeavingPark()
auto* windowMgr = Ui::GetWindowManager();
windowMgr->InvalidateByClass(WindowClass::GuestList);
uint8_t pathingResult;
PerformNextAction(pathingResult);
const auto [pathingResult, _] = PerformNextAction();
if (!(pathingResult & PATHING_OUTSIDE_PARK))
return;
Remove();
@@ -5812,8 +5806,8 @@ void Guest::UpdateWatching()
{
if (!CheckForPath())
return;
uint8_t pathingResult;
PerformNextAction(pathingResult);
const auto [pathingResult, _] = PerformNextAction();
if (!(pathingResult & PATHING_DESTINATION_REACHED))
return;
@@ -5909,8 +5903,7 @@ void Guest::UpdateUsingBin()
if (!CheckForPath())
return;
uint8_t pathingResult;
PerformNextAction(pathingResult);
const auto [pathingResult, _] = PerformNextAction();
if (pathingResult & PATHING_DESTINATION_REACHED)
{
UsingBinSubState = PeepUsingBinSubState::GoingBack;

View File

@@ -2215,15 +2215,16 @@ static void PeepFootpathMoveForward(Peep* peep, const CoordsXYE& coords, bool va
static void PeepInteractWithPath(Peep* peep, const CoordsXYE& coords)
{
// 0x00F1AEE2
auto tile_element = coords.element;
bool vandalism_present = false;
if (tile_element->AsPath()->HasAddition() && (tile_element->AsPath()->IsBroken())
&& (tile_element->AsPath()->GetEdges()) != 0xF)
const auto* pathElement = coords.element->AsPath();
assert(pathElement != nullptr);
bool vandalismPresent = false;
if (pathElement->HasAddition() && pathElement->IsBroken() && (pathElement->GetEdges()) != 0xF)
{
vandalism_present = true;
vandalismPresent = true;
}
int16_t z = tile_element->GetBaseZ();
int16_t z = pathElement->GetBaseZ();
auto* guest = peep->As<Guest>();
if (MapIsLocationOwned({ coords, z }))
{
@@ -2242,9 +2243,9 @@ static void PeepInteractWithPath(Peep* peep, const CoordsXYE& coords)
}
}
if (guest != nullptr && tile_element->AsPath()->IsQueue())
if (guest != nullptr && pathElement->IsQueue())
{
auto rideIndex = tile_element->AsPath()->GetRideIndex();
auto rideIndex = pathElement->GetRideIndex();
if (guest->State == PeepState::Queuing)
{
// Check if this queue is connected to the ride the
@@ -2252,7 +2253,7 @@ static void PeepInteractWithPath(Peep* peep, const CoordsXYE& coords)
// the queue, rebuilt the ride, etc.
if (guest->CurrentRide == rideIndex)
{
PeepFootpathMoveForward(guest, { coords, tile_element }, vandalism_present);
PeepFootpathMoveForward(guest, coords, vandalismPresent);
}
else
{
@@ -2260,23 +2261,23 @@ static void PeepInteractWithPath(Peep* peep, const CoordsXYE& coords)
guest->InteractionRideIndex = RideId::GetNull();
guest->RemoveFromQueue();
guest->SetState(PeepState::One);
PeepFootpathMoveForward(guest, { coords, tile_element }, vandalism_present);
PeepFootpathMoveForward(guest, coords, vandalismPresent);
}
}
else
{
// Peep is not queuing.
guest->TimeLost = 0;
auto stationNum = tile_element->AsPath()->GetStationIndex();
auto stationNum = pathElement->GetStationIndex();
if ((tile_element->AsPath()->HasQueueBanner())
&& (tile_element->AsPath()->GetQueueBannerDirection()
== DirectionReverse(guest->PeepDirection)) // Ride sign is facing the direction the peep is walking
if (pathElement->HasQueueBanner()
&& pathElement->GetQueueBannerDirection()
== DirectionReverse(guest->PeepDirection) // Ride sign is facing the direction the peep is walking
)
{
/* Peep is approaching the entrance of a ride queue.
* Decide whether to go on the ride. */
auto ride = GetRide(rideIndex);
auto* ride = GetRide(rideIndex);
if (ride != nullptr && guest->ShouldGoOnRide(*ride, stationNum, true, false))
{
// Peep has decided to go on the ride at the queue.
@@ -2316,7 +2317,7 @@ static void PeepInteractWithPath(Peep* peep, const CoordsXYE& coords)
.ToTileCentre();
guest->SetDestination(queueTileCentre);
PeepFootpathMoveForward(guest, { coords, tile_element }, vandalism_present);
PeepFootpathMoveForward(guest, coords, vandalismPresent);
}
else
{
@@ -2328,7 +2329,7 @@ static void PeepInteractWithPath(Peep* peep, const CoordsXYE& coords)
{
/* Peep is approaching a queue tile without a ride
* sign facing the peep. */
PeepFootpathMoveForward(guest, { coords, tile_element }, vandalism_present);
PeepFootpathMoveForward(guest, coords, vandalismPresent);
}
}
}
@@ -2340,7 +2341,7 @@ static void PeepInteractWithPath(Peep* peep, const CoordsXYE& coords)
guest->RemoveFromQueue();
guest->SetState(PeepState::One);
}
PeepFootpathMoveForward(peep, { coords, tile_element }, vandalism_present);
PeepFootpathMoveForward(peep, coords, vandalismPresent);
}
}
@@ -2441,19 +2442,15 @@ static bool PeepInteractWithShop(Peep* peep, const CoordsXYE& coords)
return true;
}
void Peep::PerformNextAction(uint8_t& pathing_result)
{
TileElement* tmpTile;
PerformNextAction(pathing_result, tmpTile);
}
/**
*
* rct2: 0x00693C9E
*/
void Peep::PerformNextAction(uint8_t& pathing_result, TileElement*& tile_result)
std::pair<uint8_t, TileElement*> Peep::PerformNextAction()
{
pathing_result = 0;
uint8_t pathingResult = 0;
TileElement* tileResult = nullptr;
PeepActionType previousAction = Action;
if (Action == PeepActionType::Idle)
@@ -2463,13 +2460,15 @@ void Peep::PerformNextAction(uint8_t& pathing_result, TileElement*& tile_result)
if (State == PeepState::Queuing && guest != nullptr)
{
if (guest->UpdateQueuePosition(previousAction))
return;
{
return { pathingResult, tileResult };
}
}
std::optional<CoordsXY> loc;
if (loc = UpdateAction(); !loc.has_value())
{
pathing_result |= PATHING_DESTINATION_REACHED;
pathingResult |= PATHING_DESTINATION_REACHED;
uint8_t result = 0;
if (guest != nullptr)
@@ -2483,10 +2482,10 @@ void Peep::PerformNextAction(uint8_t& pathing_result, TileElement*& tile_result)
}
if (result != 0)
return;
return { pathingResult, tileResult };
if (loc = UpdateAction(); !loc.has_value())
return;
return { pathingResult, tileResult };
}
auto newLoc = *loc;
@@ -2495,22 +2494,23 @@ void Peep::PerformNextAction(uint8_t& pathing_result, TileElement*& tile_result)
{
int16_t height = GetZOnSlope(newLoc.x, newLoc.y);
MoveTo({ newLoc.x, newLoc.y, height });
return;
return { pathingResult, tileResult };
}
if (MapIsEdge(newLoc))
{
if (guest != nullptr && guest->OutsideOfPark)
{
pathing_result |= PATHING_OUTSIDE_PARK;
pathingResult |= PATHING_OUTSIDE_PARK;
}
PeepReturnToCentreOfTile(this);
return;
return { pathingResult, tileResult };
}
TileElement* tileElement = MapGetFirstElementAt(newLoc);
if (tileElement == nullptr)
return;
return { pathingResult, tileResult };
int16_t base_z = std::max(0, (z / 8) - 2);
int16_t top_z = (z / 8) + 1;
@@ -2526,24 +2526,25 @@ void Peep::PerformNextAction(uint8_t& pathing_result, TileElement*& tile_result)
if (tileElement->GetType() == TileElementType::Path)
{
PeepInteractWithPath(this, { newLoc, tileElement });
tile_result = tileElement;
return;
tileResult = tileElement;
return { pathingResult, tileResult };
}
if (tileElement->GetType() == TileElementType::Track)
{
if (PeepInteractWithShop(this, { newLoc, tileElement }))
{
tile_result = tileElement;
return;
tileResult = tileElement;
return { pathingResult, tileResult };
}
}
else if (tileElement->GetType() == TileElementType::Entrance)
{
if (PeepInteractWithEntrance(this, { newLoc, tileElement }, pathing_result))
if (PeepInteractWithEntrance(this, { newLoc, tileElement }, pathingResult))
{
tile_result = tileElement;
return;
tileResult = tileElement;
return { pathingResult, tileResult };
}
}
} while (!(tileElement++)->IsLastForTile());
@@ -2563,21 +2564,21 @@ void Peep::PerformNextAction(uint8_t& pathing_result, TileElement*& tile_result)
if (!MapIsLocationInPark(newLoc))
{
PeepReturnToCentreOfTile(this);
return;
return { pathingResult, tileResult };
}
auto surfaceElement = MapGetSurfaceElementAt(newLoc);
if (surfaceElement == nullptr)
{
PeepReturnToCentreOfTile(this);
return;
return { pathingResult, tileResult };
}
int16_t water_height = surfaceElement->GetWaterHeight();
if (water_height > 0)
{
PeepReturnToCentreOfTile(this);
return;
return { pathingResult, tileResult };
}
auto* staff = As<Staff>();
@@ -2587,7 +2588,7 @@ void Peep::PerformNextAction(uint8_t& pathing_result, TileElement*& tile_result)
if (!((staff->StaffOrders & STAFF_ORDERS_MOWING) && staff->StaffMowingTimeout >= 12))
{
PeepReturnToCentreOfTile(staff);
return;
return { pathingResult, tileResult };
}
}
@@ -2597,11 +2598,12 @@ void Peep::PerformNextAction(uint8_t& pathing_result, TileElement*& tile_result)
height = GetZOnSlope(newLoc.x, newLoc.y);
MoveTo({ newLoc.x, newLoc.y, height });
return;
return { pathingResult, tileResult };
}
}
PeepReturnToCentreOfTile(this);
return { pathingResult, tileResult };
}
/**

View File

@@ -20,6 +20,7 @@
#include <optional>
#include <string>
#include <string_view>
#include <utility>
constexpr uint8_t kPeepMinEnergy = 32;
constexpr uint8_t kPeepMaxEnergy = 128;
@@ -402,8 +403,7 @@ public: // Peep
bool ShouldWaitForLevelCrossing();
bool IsOnLevelCrossing();
bool IsOnPathBlockedByVehicle();
void PerformNextAction(uint8_t& pathing_result);
void PerformNextAction(uint8_t& pathing_result, TileElement*& tile_result);
std::pair<uint8_t, TileElement*> PerformNextAction();
[[nodiscard]] int32_t GetZOnSlope(int32_t tile_x, int32_t tile_y);
void SwitchNextAnimationType();
[[nodiscard]] PeepAnimationType GetAnimationType();

View File

@@ -1053,8 +1053,7 @@ void Staff::UpdateWatering()
if (!CheckForPath())
return;
uint8_t pathingResult;
PerformNextAction(pathingResult);
const auto [pathingResult, _] = PerformNextAction();
if (!(pathingResult & PATHING_DESTINATION_REACHED))
return;
@@ -1117,8 +1116,7 @@ void Staff::UpdateEmptyingBin()
if (!CheckForPath())
return;
uint8_t pathingResult;
PerformNextAction(pathingResult);
const auto [pathingResult, _] = PerformNextAction();
if (!(pathingResult & PATHING_DESTINATION_REACHED))
return;
@@ -1273,10 +1271,7 @@ void Staff::UpdateHeadingToInspect()
if (ShouldWaitForLevelCrossing() && !IsMechanicHeadingToFixRideBlockingPath())
return;
uint8_t pathingResult;
TileElement* rideEntranceExitElement;
PerformNextAction(pathingResult, rideEntranceExitElement);
const auto [pathingResult, rideEntranceExitElement] = PerformNextAction();
if (!(pathingResult & PATHING_RIDE_EXIT) && !(pathingResult & PATHING_RIDE_ENTRANCE))
{
return;
@@ -1381,10 +1376,7 @@ void Staff::UpdateAnswering()
if (ShouldWaitForLevelCrossing() && !IsMechanicHeadingToFixRideBlockingPath())
return;
uint8_t pathingResult;
TileElement* rideEntranceExitElement;
PerformNextAction(pathingResult, rideEntranceExitElement);
const auto [pathingResult, rideEntranceExitElement] = PerformNextAction();
if (!(pathingResult & PATHING_RIDE_EXIT) && !(pathingResult & PATHING_RIDE_ENTRANCE))
{
return;
@@ -1719,8 +1711,7 @@ void Staff::UpdatePatrolling()
if (ShouldWaitForLevelCrossing() && !IsMechanicHeadingToFixRideBlockingPath())
return;
uint8_t pathingResult;
PerformNextAction(pathingResult);
const auto [pathingResult, _] = PerformNextAction();
if (!(pathingResult & PATHING_DESTINATION_REACHED))
return;

View File

@@ -108,8 +108,7 @@ protected:
int step = 0;
while (!(*pos == goal) && step < expectedSteps)
{
uint8_t pathingResult = 0;
peep->PerformNextAction(pathingResult);
peep->PerformNextAction();
++step;
*pos = TileCoordsXYZ(peep->GetLocation());