summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLéo Lam <leo@leolam.fr>2021-11-24 02:12:05 +0100
committerLéo Lam <leo@leolam.fr>2021-11-24 02:12:21 +0100
commit0f3cb6e1ecb63e1f514d227ace38944a2685edac (patch)
treea1148595223230a49608ceb60b44fcb0fb01a9f7 /src
parentc0a79e67eeb8bc7f41889cc0fc9ea1808cf6341e (diff)
ksys: Add act::getRandomAreaItem
Diffstat (limited to 'src')
-rw-r--r--src/KingSystem/ActorSystem/actActorUtil.cpp30
-rw-r--r--src/KingSystem/ActorSystem/actActorUtil.h6
2 files changed, 36 insertions, 0 deletions
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 <container/seadSafeArray.h>
#include <math/seadMathCalcCommon.h>
+#include <random/seadGlobalRandom.h>
#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;
}
diff --git a/src/KingSystem/ActorSystem/actActorUtil.h b/src/KingSystem/ActorSystem/actActorUtil.h
index e940f301..2e0a576e 100644
--- a/src/KingSystem/ActorSystem/actActorUtil.h
+++ b/src/KingSystem/ActorSystem/actActorUtil.h
@@ -7,6 +7,10 @@ namespace al {
class ByamlIter;
}
+namespace ksys::eco {
+enum class AreaItemType;
+}
+
namespace ksys::map {
class Object;
}
@@ -167,6 +171,8 @@ bool getSameGroupActorName(sead::SafeString* name, const sead::SafeString& actor
s32 getSelectedChoiceIdx(s32 max, const char* query_name);
+bool getRandomAreaItem(sead::SafeString* item, const eco::AreaItemType& type,
+ const sead::Vector3f& pos);
bool isInSatoriMountainArea(const sead::Vector3f& pos);
} // namespace ksys::act