From f10ea9a29fb03ee5fb667eab253d86e061fcaa5a Mon Sep 17 00:00:00 2001 From: Henrique Gemignani Passos Lima Date: Thu, 10 Nov 2022 12:55:04 +0200 Subject: [PATCH] Add CStaticInterference --- asm/MetroidPrime/Enemies/CDrone.s | 6 +- asm/MetroidPrime/Enemies/CJellyZap.s | 2 +- asm/MetroidPrime/Enemies/CMetroidBeta.s | 2 +- asm/MetroidPrime/Player/CStaticInterference.s | 30 +++---- configure.py | 2 +- .../Player/CStaticInterference.hpp | 6 ++ include/rstl/pointer_iterator.hpp | 6 +- include/rstl/vector.hpp | 35 ++++++-- .../Player/CStaticInterference.cpp | 79 +++++++++++++++++++ 9 files changed, 139 insertions(+), 29 deletions(-) create mode 100644 src/MetroidPrime/Player/CStaticInterference.cpp diff --git a/asm/MetroidPrime/Enemies/CDrone.s b/asm/MetroidPrime/Enemies/CDrone.s index f4233c79..c86a04a3 100644 --- a/asm/MetroidPrime/Enemies/CDrone.s +++ b/asm/MetroidPrime/Enemies/CDrone.s @@ -7048,7 +7048,7 @@ lbl_80164CC0: /* 80164D40 00161CA0 B0 01 00 28 */ sth r0, 0x28(r1) /* 80164D44 00161CA4 80 63 00 00 */ lwz r3, 0(r3) /* 80164D48 00161CA8 38 63 01 88 */ addi r3, r3, 0x188 -/* 80164D4C 00161CAC 48 05 BC 69 */ bl sub_801c09b4 +/* 80164D4C 00161CAC 48 05 BC 69 */ bl RemoveSource__19CStaticInterferenceF9TUniqueId /* 80164D50 00161CB0 80 7F 08 B8 */ lwz r3, 0x8b8(r31) /* 80164D54 00161CB4 80 63 00 00 */ lwz r3, 0(r3) /* 80164D58 00161CB8 38 63 01 88 */ addi r3, r3, 0x188 @@ -7089,7 +7089,7 @@ lbl_80164DAC: /* 80164DD8 00161D38 B0 01 00 18 */ sth r0, 0x18(r1) /* 80164DDC 00161D3C 80 63 00 00 */ lwz r3, 0(r3) /* 80164DE0 00161D40 38 63 01 88 */ addi r3, r3, 0x188 -/* 80164DE4 00161D44 48 05 BB D1 */ bl sub_801c09b4 +/* 80164DE4 00161D44 48 05 BB D1 */ bl RemoveSource__19CStaticInterferenceF9TUniqueId /* 80164DE8 00161D48 80 7F 08 B8 */ lwz r3, 0x8b8(r31) /* 80164DEC 00161D4C 80 63 00 00 */ lwz r3, 0(r3) /* 80164DF0 00161D50 38 63 01 88 */ addi r3, r3, 0x188 @@ -8629,7 +8629,7 @@ lbl_801663E0: /* 80166408 00163368 B0 01 00 2C */ sth r0, 0x2c(r1) /* 8016640C 0016336C 80 63 00 00 */ lwz r3, 0(r3) /* 80166410 00163370 38 63 01 88 */ addi r3, r3, 0x188 -/* 80166414 00163374 48 05 A5 A1 */ bl sub_801c09b4 +/* 80166414 00163374 48 05 A5 A1 */ bl RemoveSource__19CStaticInterferenceF9TUniqueId /* 80166418 00163378 A0 7E 05 78 */ lhz r3, 0x578(r30) /* 8016641C 0016337C A0 0D A3 8C */ lhz r0, kInvalidUniqueId@sda21(r13) /* 80166420 00163380 7C 03 00 40 */ cmplw r3, r0 diff --git a/asm/MetroidPrime/Enemies/CJellyZap.s b/asm/MetroidPrime/Enemies/CJellyZap.s index 32de628c..db0d496c 100644 --- a/asm/MetroidPrime/Enemies/CJellyZap.s +++ b/asm/MetroidPrime/Enemies/CJellyZap.s @@ -1044,7 +1044,7 @@ lbl_801D8450: /* 801D8460 001D53C0 B0 01 00 08 */ sth r0, 8(r1) /* 801D8464 001D53C4 80 63 00 00 */ lwz r3, 0(r3) /* 801D8468 001D53C8 38 63 01 88 */ addi r3, r3, 0x188 -/* 801D846C 001D53CC 4B FE 85 49 */ bl sub_801c09b4 +/* 801D846C 001D53CC 4B FE 85 49 */ bl RemoveSource__19CStaticInterferenceF9TUniqueId /* 801D8470 001D53D0 38 60 00 00 */ li r3, 0 /* 801D8474 001D53D4 90 7E 03 2C */ stw r3, 0x32c(r30) /* 801D8478 001D53D8 88 1E 05 B8 */ lbz r0, 0x5b8(r30) diff --git a/asm/MetroidPrime/Enemies/CMetroidBeta.s b/asm/MetroidPrime/Enemies/CMetroidBeta.s index c1e78c97..e87af728 100644 --- a/asm/MetroidPrime/Enemies/CMetroidBeta.s +++ b/asm/MetroidPrime/Enemies/CMetroidBeta.s @@ -3638,7 +3638,7 @@ lbl_801C3C94: /* 801C3CC0 001C0C20 B0 01 00 14 */ sth r0, 0x14(r1) /* 801C3CC4 001C0C24 80 63 00 00 */ lwz r3, 0(r3) /* 801C3CC8 001C0C28 38 63 01 88 */ addi r3, r3, 0x188 -/* 801C3CCC 001C0C2C 4B FF CC E9 */ bl sub_801c09b4 +/* 801C3CCC 001C0C2C 4B FF CC E9 */ bl RemoveSource__19CStaticInterferenceF9TUniqueId /* 801C3CD0 001C0C30 80 1D 03 04 */ lwz r0, 0x304(r29) /* 801C3CD4 001C0C34 2C 00 00 04 */ cmpwi r0, 4 /* 801C3CD8 001C0C38 40 82 00 24 */ bne lbl_801C3CFC diff --git a/asm/MetroidPrime/Player/CStaticInterference.s b/asm/MetroidPrime/Player/CStaticInterference.s index bb084786..2c7301b3 100644 --- a/asm/MetroidPrime/Player/CStaticInterference.s +++ b/asm/MetroidPrime/Player/CStaticInterference.s @@ -41,7 +41,7 @@ Update__19CStaticInterferenceFR13CStateManagerf: /* 801C07E4 001BD744 90 01 00 28 */ stw r0, 0x28(r1) /* 801C07E8 001BD748 90 01 00 2C */ stw r0, 0x2c(r1) /* 801C07EC 001BD74C 80 9F 00 04 */ lwz r4, 4(r31) -/* 801C07F0 001BD750 48 00 04 9D */ bl sub_801c0c8c +/* 801C07F0 001BD750 48 00 04 9D */ bl "reserve__Q24rstl62vector<25CStaticInterferenceSource,Q24rstl17rmemory_allocator>Fi" /* 801C07F4 001BD754 C3 E2 AA 78 */ lfs f31, lbl_805AC798@sda21(r2) /* 801C07F8 001BD758 48 00 00 80 */ b lbl_801C0878 lbl_801C07FC: @@ -58,7 +58,7 @@ lbl_801C07FC: /* 801C0824 001BD784 41 82 00 08 */ beq lbl_801C082C /* 801C0828 001BD788 54 A4 08 3C */ slwi r4, r5, 1 lbl_801C082C: -/* 801C082C 001BD78C 48 00 04 61 */ bl sub_801c0c8c +/* 801C082C 001BD78C 48 00 04 61 */ bl "reserve__Q24rstl62vector<25CStaticInterferenceSource,Q24rstl17rmemory_allocator>Fi" lbl_801C0830: /* 801C0830 001BD790 80 01 00 24 */ lwz r0, 0x24(r1) /* 801C0834 001BD794 80 61 00 2C */ lwz r3, 0x2c(r1) @@ -96,7 +96,7 @@ lbl_801C0898: /* 801C08A0 001BD800 38 81 00 0C */ addi r4, r1, 0xc /* 801C08A4 001BD804 B0 01 00 08 */ sth r0, 8(r1) /* 801C08A8 001BD808 B0 01 00 0C */ sth r0, 0xc(r1) -/* 801C08AC 001BD80C 48 00 01 09 */ bl sub_801c09b4 +/* 801C08AC 001BD80C 48 00 01 09 */ bl RemoveSource__19CStaticInterferenceF9TUniqueId /* 801C08B0 001BD810 3B DE 00 0C */ addi r30, r30, 0xc lbl_801C08B4: /* 801C08B4 001BD814 80 01 00 24 */ lwz r0, 0x24(r1) @@ -177,8 +177,8 @@ lbl_801C09A8: /* 801C09AC 001BD90C 38 21 00 10 */ addi r1, r1, 0x10 /* 801C09B0 001BD910 4E 80 00 20 */ blr -.global sub_801c09b4 -sub_801c09b4: +.global RemoveSource__19CStaticInterferenceF9TUniqueId +RemoveSource__19CStaticInterferenceF9TUniqueId: /* 801C09B4 001BD914 94 21 FF E0 */ stwu r1, -0x20(r1) /* 801C09B8 001BD918 7C 08 02 A6 */ mflr r0 /* 801C09BC 001BD91C 90 01 00 24 */ stw r0, 0x24(r1) @@ -210,15 +210,15 @@ lbl_801C0A04: /* 801C0A18 001BD978 38 61 00 0C */ addi r3, r1, 0xc /* 801C0A1C 001BD97C 38 A1 00 08 */ addi r5, r1, 8 /* 801C0A20 001BD980 90 01 00 08 */ stw r0, 8(r1) -/* 801C0A24 001BD984 48 00 00 15 */ bl sub_801c0a38 +/* 801C0A24 001BD984 48 00 00 15 */ bl "erase__Q24rstl62vector<25CStaticInterferenceSource,Q24rstl17rmemory_allocator>FQ24rstl144pointer_iterator<25CStaticInterferenceSource,Q24rstl62vector<25CStaticInterferenceSource,Q24rstl17rmemory_allocator>,Q24rstl17rmemory_allocator>" lbl_801C0A28: /* 801C0A28 001BD988 80 01 00 24 */ lwz r0, 0x24(r1) /* 801C0A2C 001BD98C 7C 08 03 A6 */ mtlr r0 /* 801C0A30 001BD990 38 21 00 20 */ addi r1, r1, 0x20 /* 801C0A34 001BD994 4E 80 00 20 */ blr -.global sub_801c0a38 -sub_801c0a38: +.global "erase__Q24rstl62vector<25CStaticInterferenceSource,Q24rstl17rmemory_allocator>FQ24rstl144pointer_iterator<25CStaticInterferenceSource,Q24rstl62vector<25CStaticInterferenceSource,Q24rstl17rmemory_allocator>,Q24rstl17rmemory_allocator>" +"erase__Q24rstl62vector<25CStaticInterferenceSource,Q24rstl17rmemory_allocator>FQ24rstl144pointer_iterator<25CStaticInterferenceSource,Q24rstl62vector<25CStaticInterferenceSource,Q24rstl17rmemory_allocator>,Q24rstl17rmemory_allocator>": /* 801C0A38 001BD998 94 21 FF E0 */ stwu r1, -0x20(r1) /* 801C0A3C 001BD99C 7C 08 02 A6 */ mflr r0 /* 801C0A40 001BD9A0 90 01 00 24 */ stw r0, 0x24(r1) @@ -232,15 +232,15 @@ sub_801c0a38: /* 801C0A60 001BD9C0 90 E1 00 08 */ stw r7, 8(r1) /* 801C0A64 001BD9C4 90 E1 00 0C */ stw r7, 0xc(r1) /* 801C0A68 001BD9C8 90 01 00 10 */ stw r0, 0x10(r1) -/* 801C0A6C 001BD9CC 48 00 00 19 */ bl sub_801c0a84 +/* 801C0A6C 001BD9CC 48 00 00 19 */ bl "erase__Q24rstl62vector<25CStaticInterferenceSource,Q24rstl17rmemory_allocator>FQ24rstl144pointer_iterator<25CStaticInterferenceSource,Q24rstl62vector<25CStaticInterferenceSource,Q24rstl17rmemory_allocator>,Q24rstl17rmemory_allocator>Q24rstl144pointer_iterator<25CStaticInterferenceSource,Q24rstl62vector<25CStaticInterferenceSource,Q24rstl17rmemory_allocator>,Q24rstl17rmemory_allocator>" /* 801C0A70 001BD9D0 80 01 00 24 */ lwz r0, 0x24(r1) /* 801C0A74 001BD9D4 83 E1 00 1C */ lwz r31, 0x1c(r1) /* 801C0A78 001BD9D8 7C 08 03 A6 */ mtlr r0 /* 801C0A7C 001BD9DC 38 21 00 20 */ addi r1, r1, 0x20 /* 801C0A80 001BD9E0 4E 80 00 20 */ blr -.global sub_801c0a84 -sub_801c0a84: +.global "erase__Q24rstl62vector<25CStaticInterferenceSource,Q24rstl17rmemory_allocator>FQ24rstl144pointer_iterator<25CStaticInterferenceSource,Q24rstl62vector<25CStaticInterferenceSource,Q24rstl17rmemory_allocator>,Q24rstl17rmemory_allocator>Q24rstl144pointer_iterator<25CStaticInterferenceSource,Q24rstl62vector<25CStaticInterferenceSource,Q24rstl17rmemory_allocator>,Q24rstl17rmemory_allocator>" +"erase__Q24rstl62vector<25CStaticInterferenceSource,Q24rstl17rmemory_allocator>FQ24rstl144pointer_iterator<25CStaticInterferenceSource,Q24rstl62vector<25CStaticInterferenceSource,Q24rstl17rmemory_allocator>,Q24rstl17rmemory_allocator>Q24rstl144pointer_iterator<25CStaticInterferenceSource,Q24rstl62vector<25CStaticInterferenceSource,Q24rstl17rmemory_allocator>,Q24rstl17rmemory_allocator>": /* 801C0A84 001BD9E4 94 21 FF F0 */ stwu r1, -0x10(r1) /* 801C0A88 001BD9E8 80 E5 00 00 */ lwz r7, 0(r5) /* 801C0A8C 001BD9EC 80 06 00 00 */ lwz r0, 0(r6) @@ -345,7 +345,7 @@ lbl_801C0BC4: /* 801C0BE8 001BDB48 41 82 00 08 */ beq lbl_801C0BF0 /* 801C0BEC 001BDB4C 54 A4 08 3C */ slwi r4, r5, 1 lbl_801C0BF0: -/* 801C0BF0 001BDB50 48 00 00 9D */ bl sub_801c0c8c +/* 801C0BF0 001BDB50 48 00 00 9D */ bl "reserve__Q24rstl62vector<25CStaticInterferenceSource,Q24rstl17rmemory_allocator>Fi" lbl_801C0BF4: /* 801C0BF4 001BDB54 80 1F 00 04 */ lwz r0, 4(r31) /* 801C0BF8 001BDB58 80 7F 00 0C */ lwz r3, 0xc(r31) @@ -383,7 +383,7 @@ __ct__19CStaticInterferenceFi: /* 801C0C64 001BDBC4 90 03 00 04 */ stw r0, 4(r3) /* 801C0C68 001BDBC8 90 03 00 08 */ stw r0, 8(r3) /* 801C0C6C 001BDBCC 90 03 00 0C */ stw r0, 0xc(r3) -/* 801C0C70 001BDBD0 48 00 00 1D */ bl sub_801c0c8c +/* 801C0C70 001BDBD0 48 00 00 1D */ bl "reserve__Q24rstl62vector<25CStaticInterferenceSource,Q24rstl17rmemory_allocator>Fi" /* 801C0C74 001BDBD4 80 01 00 14 */ lwz r0, 0x14(r1) /* 801C0C78 001BDBD8 7F E3 FB 78 */ mr r3, r31 /* 801C0C7C 001BDBDC 83 E1 00 0C */ lwz r31, 0xc(r1) @@ -391,8 +391,8 @@ __ct__19CStaticInterferenceFi: /* 801C0C84 001BDBE4 38 21 00 10 */ addi r1, r1, 0x10 /* 801C0C88 001BDBE8 4E 80 00 20 */ blr -.global sub_801c0c8c -sub_801c0c8c: +.global "reserve__Q24rstl62vector<25CStaticInterferenceSource,Q24rstl17rmemory_allocator>Fi" +"reserve__Q24rstl62vector<25CStaticInterferenceSource,Q24rstl17rmemory_allocator>Fi": /* 801C0C8C 001BDBEC 94 21 FF D0 */ stwu r1, -0x30(r1) /* 801C0C90 001BDBF0 7C 08 02 A6 */ mflr r0 /* 801C0C94 001BDBF4 90 01 00 34 */ stw r0, 0x34(r1) diff --git a/configure.py b/configure.py index e9ee8863..ccb022a8 100755 --- a/configure.py +++ b/configure.py @@ -271,7 +271,7 @@ LIBS = [ "MetroidPrime/Weapons/CGunWeapon", ["MetroidPrime/ScriptObjects/CScriptAreaAttributes", False], "MetroidPrime/Weapons/CWaveBuster", - "MetroidPrime/Player/CStaticInterference", + ["MetroidPrime/Player/CStaticInterference", False], "MetroidPrime/Enemies/CMetroidBeta", "MetroidPrime/PathFinding/CPathFindSearch", "MetroidPrime/PathFinding/CPathFindRegion", diff --git a/include/MetroidPrime/Player/CStaticInterference.hpp b/include/MetroidPrime/Player/CStaticInterference.hpp index 0329be3b..98aa2155 100644 --- a/include/MetroidPrime/Player/CStaticInterference.hpp +++ b/include/MetroidPrime/Player/CStaticInterference.hpp @@ -8,6 +8,12 @@ #include "rstl/vector.hpp" struct CStaticInterferenceSource { + CStaticInterferenceSource(TUniqueId id, float magnitude, float timeLeft) + : x0_id(id), x4_magnitude(magnitude), x8_timeLeft(timeLeft) {} + + TUniqueId GetId() const { return x0_id; } + float GetMagnitude() const { return x4_magnitude; } + TUniqueId x0_id; float x4_magnitude; float x8_timeLeft; diff --git a/include/rstl/pointer_iterator.hpp b/include/rstl/pointer_iterator.hpp index 09297608..75d65bbd 100644 --- a/include/rstl/pointer_iterator.hpp +++ b/include/rstl/pointer_iterator.hpp @@ -90,7 +90,9 @@ public: ++this->current; return *this; } - pointer_iterator operator++(int) { return pointer_iterator(this->current++); } + pointer_iterator operator++(int) { + return *this += 1; + } pointer_iterator& operator--() { --this->current; return *this; @@ -131,7 +133,7 @@ inline typename It::difference_type __distance(It first, It last, random_access_ template < typename It > inline typename It::difference_type distance(It first, It last) { - return __distance(first, last, It::iterator_category()); + return __distance(first, last, typename It::iterator_category()); } } // namespace rstl diff --git a/include/rstl/vector.hpp b/include/rstl/vector.hpp index 52a8cb79..0e892c93 100644 --- a/include/rstl/vector.hpp +++ b/include/rstl/vector.hpp @@ -66,7 +66,9 @@ public: iterator insert(iterator it, const T& value); // TODO template < typename from_iterator > iterator insert(iterator it, from_iterator begin, from_iterator end); + iterator erase(iterator it); + iterator erase(iterator first, iterator last); void push_back(const T& in) { if (x4_count >= x8_capacity) { @@ -103,12 +105,12 @@ protected: void insert_into(iterator at, int n, In in) { // int insertAt = xc_items + n; // TODO: finish - if (x8_capacity < n) { - int newCapacity = x8_capacity != 0 ? x8_capacity * 2 : 4; - T* newData; - x0_allocator.allocate(newData, newCapacity); - } - } + if (x8_capacity < n) { + int newCapacity = x8_capacity != 0 ? x8_capacity * 2 : 4; + T* newData; + x0_allocator.allocate(newData, newCapacity); + } + } }; template < typename T, typename Alloc > @@ -163,6 +165,27 @@ vector< T, Alloc >& vector< T, Alloc >::operator=(const vector< T, Alloc >& othe return *this; } +template < typename T, typename Alloc > +typename vector< T, Alloc >::iterator vector< T, Alloc >::erase(iterator it) { + return erase(it, it + 1); +} + +template < typename T, typename Alloc > +typename vector< T, Alloc >::iterator vector< T, Alloc >::erase(iterator first, iterator last) { + destroy(first, last); + iterator start = begin(); + int newCount = rstl::distance(first, start); + + iterator moved = start + newCount; + for (iterator it = last; it != end(); ++it) { + construct(&*moved, *it); + ++moved; + ++newCount; + } + x4_count = newCount; + + return first; +} typedef vector< void > unk_vector; CHECK_SIZEOF(unk_vector, 0x10) diff --git a/src/MetroidPrime/Player/CStaticInterference.cpp b/src/MetroidPrime/Player/CStaticInterference.cpp new file mode 100644 index 00000000..93b9b8c6 --- /dev/null +++ b/src/MetroidPrime/Player/CStaticInterference.cpp @@ -0,0 +1,79 @@ +#include "MetroidPrime/Player/CStaticInterference.hpp" + +#include "Kyoto/Math/CMath.hpp" +#include "rstl/math.hpp" + +CStaticInterference::CStaticInterference(int sourceCount) { sources.reserve(sourceCount); } + +void CStaticInterference::AddSource(TUniqueId id, float magnitude, float duration) { + float clampedMagnitude = CMath::Clamp(0.f, magnitude, 1.f); + + rstl::vector< CStaticInterferenceSource >::iterator search = sources.begin(); + for (; search != sources.end(); ++search) { + if (search->x0_id == id) { + break; + } + } + + if (search != sources.end()) { + search->x4_magnitude = clampedMagnitude; + search->x8_timeLeft = duration; + } else { + if (sources.size() < sources.capacity()) { + sources.push_back(CStaticInterferenceSource(id, clampedMagnitude, duration)); + } + } +} + +void CStaticInterference::RemoveSource(TUniqueId id) { + rstl::vector< CStaticInterferenceSource >::iterator search = sources.begin(); + for (; search != sources.end(); ++search) { + if (search->x0_id == id) { + break; + } + } + + if (search != sources.end()) { + sources.erase(search); + } +} + +float CStaticInterference::GetTotalInterference() const { + float validAccum = 0.f; + float invalidAccum = 0.f; + + rstl::vector< CStaticInterferenceSource >::const_iterator it = sources.begin(); + for (; it != sources.end(); ++it) { + TUniqueId id = it->GetId(); + float magnitude = it->GetMagnitude(); + + if (id == kInvalidUniqueId) { + invalidAccum += magnitude; + } + if (id != kInvalidUniqueId) { + validAccum += magnitude; + } + } + if (validAccum > 0.8f) + validAccum = 0.8f; + + return rstl::min_val(validAccum + invalidAccum, 1.f); +} + +void CStaticInterference::Update(CStateManager&, float dt) { + rstl::vector< CStaticInterferenceSource >::iterator it = sources.begin(); + rstl::vector< CStaticInterferenceSource > toRemove; + toRemove.reserve(sources.size()); + for (; it != sources.end(); ++it) { + if (it->x8_timeLeft < 0.f) { + toRemove.push_back(*it); + } else { + it->x8_timeLeft -= dt; + } + } + + for (rstl::vector< CStaticInterferenceSource >::iterator it = toRemove.begin(); + it != toRemove.end(); ++it) { + RemoveSource(it->GetId()); + } +}