From 04c75f6f8b05f7333b88aa37a577c7a3ba04845a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Wed, 21 May 2025 17:28:05 +0300 Subject: [PATCH] Remove code duplication for stepping --- src/openrct2/entity/Guest.cpp | 20 +++----------------- src/openrct2/entity/Peep.cpp | 27 ++++++++++++++++++++++++--- src/openrct2/entity/Peep.h | 7 ++++--- src/openrct2/entity/Staff.cpp | 20 +++----------------- 4 files changed, 34 insertions(+), 40 deletions(-) diff --git a/src/openrct2/entity/Guest.cpp b/src/openrct2/entity/Guest.cpp index f6159f1057..2c01fa4a63 100644 --- a/src/openrct2/entity/Guest.cpp +++ b/src/openrct2/entity/Guest.cpp @@ -5299,24 +5299,10 @@ void Guest::Update() GuestUpdateThoughts(this); // Walking speed logic - uint32_t stepsToTake = Energy; - if (stepsToTake < 95 && State == PeepState::Queuing) - stepsToTake = 95; - if ((PeepFlags & PEEP_FLAGS_SLOW_WALK) && State != PeepState::Queuing) - stepsToTake /= 2; - if (IsActionWalking() && GetNextIsSloped()) - { - stepsToTake /= 2; - if (State == PeepState::Queuing) - stepsToTake += stepsToTake / 2; - } - // Ensure guests make it across a level crossing in time - constexpr auto minStepsForCrossing = 55; - if (stepsToTake < minStepsForCrossing && IsOnPathBlockedByVehicle()) - stepsToTake = minStepsForCrossing; - - uint32_t carryCheck = StepProgress + stepsToTake; + const auto stepsToTake = GetStepsToTake(); + const auto carryCheck = StepProgress + stepsToTake; StepProgress = carryCheck; + if (carryCheck <= 255) { UpdateEasterEggInteractions(); diff --git a/src/openrct2/entity/Peep.cpp b/src/openrct2/entity/Peep.cpp index 3db2b02654..b578412cbb 100644 --- a/src/openrct2/entity/Peep.cpp +++ b/src/openrct2/entity/Peep.cpp @@ -300,7 +300,7 @@ bool Peep::CheckForPath() return false; } -bool Peep::ShouldWaitForLevelCrossing() +bool Peep::ShouldWaitForLevelCrossing() const { if (IsOnPathBlockedByVehicle()) { @@ -318,13 +318,13 @@ bool Peep::ShouldWaitForLevelCrossing() return false; } -bool Peep::IsOnLevelCrossing() +bool Peep::IsOnLevelCrossing() const { auto trackElement = MapGetTrackElementAt(GetLocation()); return trackElement != nullptr; } -bool Peep::IsOnPathBlockedByVehicle() +bool Peep::IsOnPathBlockedByVehicle() const { auto curPos = TileCoordsXYZ(GetLocation()); return FootpathIsBlockedByVehicle(curPos); @@ -928,6 +928,27 @@ void Peep::UpdatePicked() } } +uint32_t Peep::GetStepsToTake() const +{ + uint32_t stepsToTake = Energy; + if (stepsToTake < 95 && State == PeepState::Queuing) + stepsToTake = 95; + if ((PeepFlags & PEEP_FLAGS_SLOW_WALK) && State != PeepState::Queuing) + stepsToTake /= 2; + if (IsActionWalking() && GetNextIsSloped()) + { + stepsToTake /= 2; + if (State == PeepState::Queuing) + stepsToTake += stepsToTake / 2; + } + // Ensure guests make it across a level crossing in time + constexpr auto minStepsForCrossing = 55; + if (stepsToTake < minStepsForCrossing && IsOnPathBlockedByVehicle()) + stepsToTake = minStepsForCrossing; + + return stepsToTake; +} + /** * * rct2: 0x0069BF41 diff --git a/src/openrct2/entity/Peep.h b/src/openrct2/entity/Peep.h index 197592134d..85c287cdf1 100644 --- a/src/openrct2/entity/Peep.h +++ b/src/openrct2/entity/Peep.h @@ -399,9 +399,9 @@ public: // Peep // TODO: Make these private again when done refactoring public: // Peep [[nodiscard]] bool CheckForPath(); - bool ShouldWaitForLevelCrossing(); - bool IsOnLevelCrossing(); - bool IsOnPathBlockedByVehicle(); + bool ShouldWaitForLevelCrossing() const; + bool IsOnLevelCrossing() const; + bool IsOnPathBlockedByVehicle() const; std::pair PerformNextAction(); [[nodiscard]] int32_t GetZOnSlope(int32_t tile_x, int32_t tile_y); void SwitchNextAnimationType(); @@ -411,6 +411,7 @@ protected: void UpdateFalling(); void Update1(); void UpdatePicked(); + uint32_t GetStepsToTake() const; }; enum diff --git a/src/openrct2/entity/Staff.cpp b/src/openrct2/entity/Staff.cpp index ea0adaac78..9a4579025c 100644 --- a/src/openrct2/entity/Staff.cpp +++ b/src/openrct2/entity/Staff.cpp @@ -1707,24 +1707,10 @@ void Staff::Update() } // Walking speed logic - uint32_t stepsToTake = Energy; - if (stepsToTake < 95 && State == PeepState::Queuing) - stepsToTake = 95; - if ((PeepFlags & PEEP_FLAGS_SLOW_WALK) && State != PeepState::Queuing) - stepsToTake /= 2; - if (IsActionWalking() && GetNextIsSloped()) - { - stepsToTake /= 2; - if (State == PeepState::Queuing) - stepsToTake += stepsToTake / 2; - } - // Ensure guests make it across a level crossing in time - constexpr auto minStepsForCrossing = 55; - if (stepsToTake < minStepsForCrossing && IsOnPathBlockedByVehicle()) - stepsToTake = minStepsForCrossing; - - uint32_t carryCheck = StepProgress + stepsToTake; + const auto stepsToTake = GetStepsToTake(); + const auto carryCheck = StepProgress + stepsToTake; StepProgress = carryCheck; + if (carryCheck <= 255) { // No-op: Keep replay working for now, can be eliminate with a replay update.