From c7fd5093d307e98335dc7bdb279e205dc3148ec7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Fri, 21 Mar 2025 02:16:14 +0200 Subject: [PATCH] Further cleanup the code, restrict maximum iterations --- src/openrct2/entity/Peep.cpp | 60 ++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/src/openrct2/entity/Peep.cpp b/src/openrct2/entity/Peep.cpp index 20733a6960..6e54c584dd 100644 --- a/src/openrct2/entity/Peep.cpp +++ b/src/openrct2/entity/Peep.cpp @@ -2116,46 +2116,60 @@ static void PeepFootpathMoveForward(Peep* peep, const CoordsXYE& coords, bool va guest->VandalismSeen = (vandalThoughtTimeout << 6) | vandalisedTiles; - uint16_t crowded = 0; + constexpr uint16_t kThresholdCrowdCount = 10; + constexpr uint8_t kThresholdLitterCount = 3; + constexpr uint8_t kThresholdVomitCount = 3; + + // Don't allow this loop to become too expensive, lets just have enough to potentially satisfy + // all the conditions. + constexpr uint32_t kMaxIterations = kThresholdCrowdCount + kThresholdLitterCount + kThresholdVomitCount; + + uint16_t crowdCount = 0; uint8_t litterCount = 0; uint8_t vomitCount = 0; + uint32_t iterations = 0; auto quad = EntityTileList(coords); - for (auto entity : quad) + for (auto* otherEnt : quad) { - if (auto other_peep = entity->As(); other_peep != nullptr) + if (iterations++ >= kMaxIterations) { - if (other_peep->State != PeepState::Walking) + break; + } + + if (std::abs(otherEnt->z - guest->NextLoc.z) > 16) + continue; + + if (const auto* otherPeep = otherEnt->As(); otherPeep != nullptr) + { + if (otherPeep->State != PeepState::Walking) continue; - if (abs(other_peep->z - guest->NextLoc.z) > 16) - continue; - crowded++; + crowdCount++; continue; } - if (auto litter = entity->As(); litter != nullptr) + if (const auto* litter = otherEnt->As(); litter != nullptr) { - if (abs(litter->z - guest->NextLoc.z) > 16) - continue; - - litterCount++; if (litter->SubType != Litter::Type::Vomit && litter->SubType != Litter::Type::VomitAlt) - continue; - - litterCount--; - vomitCount++; + { + litterCount++; + } + else + { + vomitCount++; + } } } - if (crowded >= 10 && guest->State == PeepState::Walking && (ScenarioRand() & 0xFFFF) <= 21845) + if (crowdCount >= kThresholdCrowdCount && guest->State == PeepState::Walking && (ScenarioRand() & 0xFFFF) <= 21845) { guest->InsertNewThought(PeepThoughtType::Crowded); guest->HappinessTarget = std::max(0, guest->HappinessTarget - 14); } - litterCount = std::min(static_cast(3), litterCount); - vomitCount = std::min(static_cast(3), vomitCount); + litterCount = std::min(kThresholdLitterCount, litterCount); + vomitCount = std::min(kThresholdVomitCount, vomitCount); uint8_t disgustingTime = guest->DisgustingCount & 0xC0; uint8_t disgustingCount = ((guest->DisgustingCount & 0xF) << 2) | vomitCount; @@ -2168,13 +2182,13 @@ static void PeepFootpathMoveForward(Peep* peep, const CoordsXYE& coords, bool va } else { - uint8_t totalSick = 0; + uint8_t totalDisgustingCount = 0; for (uint8_t time = 0; time < 3; time++) { - totalSick += (disgustingCount >> (2 * time)) & 0x3; + totalDisgustingCount += (disgustingCount >> (2 * time)) & 0x3; } - if (totalSick >= 3 && (ScenarioRand() & 0xFFFF) <= 10922) + if (totalDisgustingCount >= kThresholdVomitCount && (ScenarioRand() & 0xFFFF) <= 10922) { guest->InsertNewThought(PeepThoughtType::PathDisgusting); guest->HappinessTarget = std::max(0, guest->HappinessTarget - 17); @@ -2200,7 +2214,7 @@ static void PeepFootpathMoveForward(Peep* peep, const CoordsXYE& coords, bool va totalLitter += (litterCount >> (2 * time)) & 0x3; } - if (totalLitter >= 3 && (ScenarioRand() & 0xFFFF) <= 10922) + if (totalLitter >= kThresholdLitterCount && (ScenarioRand() & 0xFFFF) <= 10922) { guest->InsertNewThought(PeepThoughtType::BadLitter); guest->HappinessTarget = std::max(0, guest->HappinessTarget - 17);