From 5219172a07d6d694707f832beefd75336bfe58b8 Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Thu, 5 Dec 2024 15:02:23 +0100 Subject: [PATCH] Introduce functions to get a (random) peep anim object by type --- src/openrct2/peep/PeepAnimations.cpp | 62 ++++++++++++++++++++++++++++ src/openrct2/peep/PeepAnimations.h | 8 ++++ 2 files changed, 70 insertions(+) diff --git a/src/openrct2/peep/PeepAnimations.cpp b/src/openrct2/peep/PeepAnimations.cpp index e2f66a407e..4682be8d9c 100644 --- a/src/openrct2/peep/PeepAnimations.cpp +++ b/src/openrct2/peep/PeepAnimations.cpp @@ -13,6 +13,7 @@ #include "../entity/Peep.h" #include +#include namespace OpenRCT2 { @@ -104,6 +105,67 @@ namespace OpenRCT2 } } + ObjectEntryIndex findPeepAnimationsIndexForType(const AnimationPeepType type) + { + auto& objManager = GetContext()->GetObjectManager(); + for (auto i = 0u; i < kMaxPeepAnimationsObjects; i++) + { + auto* animObj = objManager.GetLoadedObject(i); + if (animObj != nullptr && animObj->GetPeepType() == type) + return i; + } + return OBJECT_ENTRY_INDEX_NULL; + } + + PeepAnimationsObject* findPeepAnimationsObjectForType(const AnimationPeepType type) + { + auto& objManager = GetContext()->GetObjectManager(); + for (auto i = 0u; i < kMaxPeepAnimationsObjects; i++) + { + auto* animObj = objManager.GetLoadedObject(i); + if (animObj != nullptr && animObj->GetPeepType() == type) + return animObj; + } + return nullptr; + } + + std::vector findAllPeepAnimationsIndexesForType(const AnimationPeepType type) + { + std::vector output{}; + auto& objManager = GetContext()->GetObjectManager(); + for (auto i = 0u; i < kMaxPeepAnimationsObjects; i++) + { + auto* animObj = objManager.GetLoadedObject(i); + if (animObj != nullptr && animObj->GetPeepType() == type) + output.push_back(i); + } + return output; + } + + std::vector findAllPeepAnimationsObjectForType(const AnimationPeepType type) + { + std::vector output{}; + auto& objManager = GetContext()->GetObjectManager(); + for (auto i = 0u; i < kMaxPeepAnimationsObjects; i++) + { + auto* animObj = objManager.GetLoadedObject(i); + if (animObj != nullptr && animObj->GetPeepType() == type) + output.push_back(animObj); + } + return output; + } + + ObjectEntryIndex findRandomPeepAnimationsIndexForType(const AnimationPeepType type) + { + auto costumes = findAllPeepAnimationsIndexesForType(type); + if (costumes.empty()) + return OBJECT_ENTRY_INDEX_NULL; + + std::vector out{}; + std::sample(costumes.begin(), costumes.end(), std::back_inserter(out), 1, std::mt19937{ std::random_device{}() }); + return !out.empty() ? out[0] : OBJECT_ENTRY_INDEX_NULL; + } + // Adapted from CarEntry.cpp SpriteBounds inferMaxAnimationDimensions(const PeepAnimation& anim) { diff --git a/src/openrct2/peep/PeepAnimations.h b/src/openrct2/peep/PeepAnimations.h index 46632180ed..b7e072be1a 100644 --- a/src/openrct2/peep/PeepAnimations.h +++ b/src/openrct2/peep/PeepAnimations.h @@ -71,5 +71,13 @@ namespace OpenRCT2 PeepAnimation animations[37]{}; }; + ObjectEntryIndex findPeepAnimationsIndexForType(const AnimationPeepType type); + PeepAnimationsObject* findPeepAnimationsObjectForType(const AnimationPeepType type); + + std::vector findAllPeepAnimationsIndexesForType(const AnimationPeepType type); + std::vector findAllPeepAnimationsObjectForType(const AnimationPeepType type); + + ObjectEntryIndex findRandomPeepAnimationsIndexForType(const AnimationPeepType type); + SpriteBounds inferMaxAnimationDimensions(const PeepAnimation& anim); } // namespace OpenRCT2