From 0f3cb6e1ecb63e1f514d227ace38944a2685edac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Wed, 24 Nov 2021 02:12:05 +0100 Subject: ksys: Add act::getRandomAreaItem --- src/KingSystem/ActorSystem/actActorUtil.cpp | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src/KingSystem/ActorSystem/actActorUtil.cpp') diff --git a/src/KingSystem/ActorSystem/actActorUtil.cpp b/src/KingSystem/ActorSystem/actActorUtil.cpp index 95062d8e..798506b8 100644 --- a/src/KingSystem/ActorSystem/actActorUtil.cpp +++ b/src/KingSystem/ActorSystem/actActorUtil.cpp @@ -1,6 +1,7 @@ #include "KingSystem/ActorSystem/actActorUtil.h" #include #include +#include #include "KingSystem/ActorSystem/Profiles/actRopeBase.h" #include "KingSystem/ActorSystem/actActor.h" #include "KingSystem/ActorSystem/actActorConstDataAccess.h" @@ -586,6 +587,35 @@ bool getSameGroupActorName(sead::SafeString* name, const sead::SafeString& actor return true; } +bool getRandomAreaItem(sead::SafeString* item, const eco::AreaItemType& type, + const sead::Vector3f& pos) { + auto* eco = eco::Ecosystem::instance(); + if (!eco) + return false; + + const int area = eco->getFieldMapArea(pos.x, pos.z); + eco::AreaItemSet items; + eco->getAreaItems(area, type, &items); + + int chosen_idx = -1; + int running_total = 0; + + for (int idx = 0; idx < items.count; ++idx) { + const int num = items.items[idx].num; + running_total += num; + if (int(sead::GlobalRandom::instance()->getU32(running_total)) < num) + chosen_idx = idx; + } + + if (chosen_idx < 0) { + *item = sead::SafeString::cEmptyString; + return false; + } + + *item = items.items[chosen_idx].name; + return true; +} + bool isInSatoriMountainArea(const sead::Vector3f& pos) { return eco::Ecosystem::instance()->getFieldMapArea(pos.x, pos.z) == 64; } -- cgit v1.2.3