From 022f029db1ef03d900fb54bad0807939c9695720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Tue, 23 Nov 2021 23:13:40 +0100 Subject: [PATCH] ksys/eco: Match Ecosystem::getMapArea --- data/uking_functions.csv | 2 +- src/KingSystem/Ecosystem/ecoSystem.cpp | 32 +++++++++++--------------- src/KingSystem/Ecosystem/ecoSystem.h | 2 +- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/data/uking_functions.csv b/data/uking_functions.csv index 4d42f3df..f2bdc4a4 100644 --- a/data/uking_functions.csv +++ b/data/uking_functions.csv @@ -77857,7 +77857,7 @@ Address,Quality,Size,Name 0x0000007100e4174c,O,000208,_ZN4ksys3eco9Ecosystem14createInstanceEPN4sead4HeapE 0x0000007100e4181c,O,001056,_ZN4ksys3eco9Ecosystem4initEPN4sead4HeapE 0x0000007100e41c3c,O,000004,_ZN4ksys3eco9Ecosystem4calcEv -0x0000007100e41c40,M,000292,_ZNK4ksys3eco9Ecosystem10getMapAreaERKNS0_10EcoMapInfoEff +0x0000007100e41c40,O,000292,_ZNK4ksys3eco9Ecosystem10getMapAreaERKNS0_10EcoMapInfoEff 0x0000007100e41d64,O,000636,_ZNK4ksys3eco9Ecosystem12getAreaItemsEiNS0_12AreaItemTypeEPNS0_11AreaItemSetE 0x0000007100e41fe0,m,000384,_ZNK4ksys3eco9Ecosystem19getStatusEffectInfoENS0_12StatusEffectEiPNS0_16StatusEffectInfoE 0x0000007100e42160,O,000120,_ZNK4ksys3eco9Ecosystem16getAreaNameByNumEiPPKc diff --git a/src/KingSystem/Ecosystem/ecoSystem.cpp b/src/KingSystem/Ecosystem/ecoSystem.cpp index fd08443c..7a5f2156 100644 --- a/src/KingSystem/Ecosystem/ecoSystem.cpp +++ b/src/KingSystem/Ecosystem/ecoSystem.cpp @@ -48,7 +48,7 @@ SEAD_SINGLETON_DISPOSER_IMPL(Ecosystem) static void setEcoMapInfo(EcoMapInfo& info, const u8* data) { info.mHeader = reinterpret_cast(data); - info.mRowOffsets = reinterpret_cast(info.mHeader + 1); + info.mRowOffsets = reinterpret_cast(info.mHeader + 1); info.mRows = reinterpret_cast(info.mRowOffsets) + sizeof(int) * info.mHeader->num_rows; } @@ -97,41 +97,35 @@ void Ecosystem::init(sead::Heap* heap) { void Ecosystem::calc() {} -// FP instructions rearranged. -#ifdef NON_MATCHING s32 Ecosystem::getMapArea(const EcoMapInfo& info, f32 posX, f32 posZ) const { - posX = sead::Mathf::clamp(posX, -5000.0F, 4999.0F); - posZ = sead::Mathf::clamp(posZ, -4000.0F, 4000.0F); - f32 epsilon1 = (posX + 5000.0F >= 0.0F) ? 0.5F : -0.5F; - f32 epsilon2 = (posZ + 4000.0F >= 0.0F) ? 0.5F : -0.5F; - s32 x = posX + 5000.0F + epsilon1; - s32 z = (posZ + 4000.0F + epsilon2) / info.mHeader->divisor; + posX = sead::Mathf::clamp(posX, -5000.0f, 4999.0f); + posZ = sead::Mathf::clamp(posZ, -4000.0f, 4000.0f); + const auto epsilon = [](float n) { return n >= 0.0f ? 0.5f : -0.5f; }; + + s32 x = s32(posX + 5000.0f + epsilon(posX + 5000.0f)); + s32 z = s32(posZ + 4000.0f + epsilon(posZ + 4000.0f)) / info.mHeader->divisor; s32 row = sead::Mathi::clamp(z, 0, info.mHeader->num_rows - 2); if (info.mHeader->divisor == 10) - x = x / 10; + x /= 10; - s32* offsets = (s32*)info.mRowOffsets + row; - s32 val0 = offsets[0]; - s32 val1 = offsets[1]; - - if (val0 >= val1) + if (info.mRowOffsets[row] >= info.mRowOffsets[row + 1]) return -1; - auto* segmentEnd = reinterpret_cast(info.mRows + 2 * val1); - auto* segment = reinterpret_cast(info.mRows + 2 * val0); + auto* segmentEnd = reinterpret_cast(info.mRows + 2 * info.mRowOffsets[row + 1]); + auto* segment = reinterpret_cast(info.mRows + 2 * info.mRowOffsets[row]); s32 totalLength = 0; while (true) { totalLength += segment->length; if (x < totalLength) - return segment->value; + break; ++segment; if (segment >= segmentEnd) return -1; } + return segment->value; } -#endif void Ecosystem::getAreaItems(s32 areaNum, AreaItemType type, AreaItemSet* out) const { out->count = 0; diff --git a/src/KingSystem/Ecosystem/ecoSystem.h b/src/KingSystem/Ecosystem/ecoSystem.h index 4b63bf2e..02c942d7 100644 --- a/src/KingSystem/Ecosystem/ecoSystem.h +++ b/src/KingSystem/Ecosystem/ecoSystem.h @@ -30,7 +30,7 @@ struct Segment { class EcoMapInfo { public: const EcoMapHeader* mHeader; - const u32* mRowOffsets; + const s32* mRowOffsets; const char* mRows; };