From cac21504cdea40b90854b273dd4ef49791ba3d56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Thu, 20 Mar 2025 18:48:26 +0200 Subject: [PATCH] Refactor PerformNextAction, we don't need a wrapper --- src/openrct2/entity/Guest.cpp | 25 ++++++--------- src/openrct2/entity/Peep.cpp | 57 ++++++++++++++++++----------------- src/openrct2/entity/Peep.h | 4 +-- src/openrct2/entity/Staff.cpp | 19 +++--------- test/tests/Pathfinding.cpp | 3 +- 5 files changed, 46 insertions(+), 62 deletions(-) diff --git a/src/openrct2/entity/Guest.cpp b/src/openrct2/entity/Guest.cpp index c1fc65b194..1568d70efd 100644 --- a/src/openrct2/entity/Guest.cpp +++ b/src/openrct2/entity/Guest.cpp @@ -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; diff --git a/src/openrct2/entity/Peep.cpp b/src/openrct2/entity/Peep.cpp index 53f4917738..ce29dba81a 100644 --- a/src/openrct2/entity/Peep.cpp +++ b/src/openrct2/entity/Peep.cpp @@ -2442,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 Peep::PerformNextAction() { - pathing_result = 0; + uint8_t pathingResult = 0; + TileElement* tileResult = nullptr; + PeepActionType previousAction = Action; if (Action == PeepActionType::Idle) @@ -2464,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 loc; if (loc = UpdateAction(); !loc.has_value()) { - pathing_result |= PATHING_DESTINATION_REACHED; + pathingResult |= PATHING_DESTINATION_REACHED; uint8_t result = 0; if (guest != nullptr) @@ -2484,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; @@ -2496,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; @@ -2527,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()); @@ -2564,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(); @@ -2588,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 }; } } @@ -2598,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 }; } /** diff --git a/src/openrct2/entity/Peep.h b/src/openrct2/entity/Peep.h index 0365ed88e2..8e207d3fa4 100644 --- a/src/openrct2/entity/Peep.h +++ b/src/openrct2/entity/Peep.h @@ -20,6 +20,7 @@ #include #include #include +#include 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 PerformNextAction(); [[nodiscard]] int32_t GetZOnSlope(int32_t tile_x, int32_t tile_y); void SwitchNextAnimationType(); [[nodiscard]] PeepAnimationType GetAnimationType(); diff --git a/src/openrct2/entity/Staff.cpp b/src/openrct2/entity/Staff.cpp index bf729b7966..c5b89611ca 100644 --- a/src/openrct2/entity/Staff.cpp +++ b/src/openrct2/entity/Staff.cpp @@ -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; diff --git a/test/tests/Pathfinding.cpp b/test/tests/Pathfinding.cpp index 0865c48cc5..49665add66 100644 --- a/test/tests/Pathfinding.cpp +++ b/test/tests/Pathfinding.cpp @@ -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());