From 4eb07ca88c2bd9c0c7594028e0149e825f675649 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Sun, 16 Jan 2022 16:43:57 +0100 Subject: [PATCH] ksys/phys: Start adding RigidBodyRequestMgr --- data/uking_functions.csv | 8 +- .../RigidBody/physRigidBodyRequestMgr.cpp | 81 +++++++++++++++++ .../RigidBody/physRigidBodyRequestMgr.h | 91 ++++++++++++++++++- .../Physics/System/physRigidContactPoints.h | 2 + .../Physics/System/physRigidContactPointsEx.h | 10 +- 5 files changed, 185 insertions(+), 7 deletions(-) diff --git a/data/uking_functions.csv b/data/uking_functions.csv index de0e6d8b..26bb3f92 100644 --- a/data/uking_functions.csv +++ b/data/uking_functions.csv @@ -83444,12 +83444,12 @@ Address,Quality,Size,Name 0x0000007100fa54a8,O,000204,_ZNK4ksys4phys20RigidBodyMotionProxy27checkDerivedRuntimeTypeInfoEPKN4sead15RuntimeTypeInfo9InterfaceE 0x0000007100fa5574,O,000092,_ZNK4ksys4phys20RigidBodyMotionProxy18getRuntimeTypeInfoEv 0x0000007100fa55d0,O,000012,_ZN4ksys4phys20RigidBodyMotionProxy16resetFrozenStateEv -0x0000007100fa55dc,U,000332,phys::RigidBodyRequestMgr::ctor +0x0000007100fa55dc,O,000332,_ZN4ksys4phys19RigidBodyRequestMgrC1Ev 0x0000007100fa5728,U,000484,phys::RigidBodyRequestMgr::x -0x0000007100fa590c,U,000352,phys::RigidBodyRequestMgr::dtor +0x0000007100fa590c,O,000352,_ZN4ksys4phys19RigidBodyRequestMgrD1Ev 0x0000007100fa5a6c,U,000004,nullsub_4236 -0x0000007100fa5a70,U,000036,phys::RigidBodyRequestMgr::dtorDelete -0x0000007100fa5a94,U,002468,phys::RigidBodyRequestMgr::init +0x0000007100fa5a70,O,000036,_ZN4ksys4phys19RigidBodyRequestMgrD0Ev +0x0000007100fa5a94,O,002468,_ZN4ksys4phys19RigidBodyRequestMgr4initEPN4sead4HeapE 0x0000007100fa6438,U,001636,phys::RigidBodyRequestMgr::x_0 0x0000007100fa6a9c,U,000020, 0x0000007100fa6ab0,U,000020, diff --git a/src/KingSystem/Physics/RigidBody/physRigidBodyRequestMgr.cpp b/src/KingSystem/Physics/RigidBody/physRigidBodyRequestMgr.cpp index e3cbc936..d603d3a3 100644 --- a/src/KingSystem/Physics/RigidBody/physRigidBodyRequestMgr.cpp +++ b/src/KingSystem/Physics/RigidBody/physRigidBodyRequestMgr.cpp @@ -1 +1,82 @@ #include "KingSystem/Physics/RigidBody/physRigidBodyRequestMgr.h" +#include "KingSystem/Physics/System/physMaterialMask.h" + +namespace ksys::phys { + +RigidBodyRequestMgr::RigidBodyRequestMgr() = default; + +RigidBodyRequestMgr::~RigidBodyRequestMgr() { + for (int i = 0; i < NumRigidBodyBuffers; ++i) { + mRigidBodies1[i].freeBuffer(); + mRigidBodies2[i].freeBuffer(); + } + + _38.freeBuffer(); + _50.freeBuffer(); + _98.freeBuffer(); + _b0.freeBuffer(); + _c8.freeBuffer(); + _e0.freeBuffer(); + _120.freeBuffer(); + _138.freeBuffer(); + _108.freeBuffer(); + _f8.freeBuffer(); + + if (mContactPoints) { + ksys::phys::RigidContactPointsEx::free(mContactPoints); + mContactPoints = nullptr; + } +} + +void RigidBodyRequestMgr::init(sead::Heap* heap) { + constexpr int Buffer138Size = 0x800; + + _108.allocBufferAssert(0x100, heap); + _118 = 0; + + _120.allocBufferAssert(0x100, heap); + _130 = 0; + + for (int i = 0; i < NumRigidBodyBuffers; ++i) { + mRigidBodies1[i].alloc(0x2000, heap); + mRigidBodies2[i].alloc(0x100, heap); + } + + _38.alloc(0x800, heap); + _50.alloc(0x800, heap); + _98.alloc(0x100, heap); + _b0.alloc(0x100, heap); + _f8.allocBuffer(0x400, heap); + _138.allocBufferAssert(Buffer138Size, heap); + _c8.alloc(0x800, heap); + _e0.alloc(Buffer138Size, heap); + + for (int i = 0; i < Buffer138Size; ++i) { + _e0.push(&_138[i]); + } + + _148 = 0; + + mContactPoints = + RigidContactPointsEx::make(heap, 0x1000, 11, "RigidBodyRequestMgr::Water", 0, 0, 0); + + mContactPoints->setCallback(&mCallback); + mContactPoints->set30(1); + mContactPoints->registerLayerPair(ContactLayer::EntityWater, ContactLayer::EntityObject); + mContactPoints->registerLayerPair(ContactLayer::EntityWater, ContactLayer::EntitySmallObject); + mContactPoints->registerLayerPair(ContactLayer::EntityWater, ContactLayer::EntityGroundObject); + mContactPoints->registerLayerPair(ContactLayer::EntityWater, ContactLayer::EntityTree); + mContactPoints->registerLayerPair(ContactLayer::EntityWater, ContactLayer::EntityRope); + mContactPoints->registerLayerPair(ContactLayer::EntityWater, ContactLayer::EntityRagdoll); + mContactPoints->registerLayerPair(ContactLayer::EntityWater, ContactLayer::EntityNPC); + mContactPoints->registerLayerPair(ContactLayer::EntityWater, ContactLayer::EntityPlayer); + mContactPoints->registerLayerPair(ContactLayer::EntityGround, ContactLayer::EntityPlayer); + mContactPoints->registerLayerPair(ContactLayer::EntityGround, ContactLayer::EntityObject); + mContactPoints->registerLayerPair(ContactLayer::EntityGround, ContactLayer::EntitySmallObject); + + mWaterIceSubmatIdx = MaterialMask::getSubMaterialIdx(Material::Water, "Water_Ice"); + mWaterHotSubmatIdx = MaterialMask::getSubMaterialIdx(Material::Water, "Water_Hot"); + mWaterPoisonSubmatIdx = MaterialMask::getSubMaterialIdx(Material::Water, "Water_Poison"); +} + +} // namespace ksys::phys diff --git a/src/KingSystem/Physics/RigidBody/physRigidBodyRequestMgr.h b/src/KingSystem/Physics/RigidBody/physRigidBodyRequestMgr.h index 8d66ec71..190c234d 100644 --- a/src/KingSystem/Physics/RigidBody/physRigidBodyRequestMgr.h +++ b/src/KingSystem/Physics/RigidBody/physRigidBodyRequestMgr.h @@ -1,12 +1,101 @@ #pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "KingSystem/Physics/System/physRigidContactPointsEx.h" +#include "KingSystem/Utils/Container/LockFreeQueue.h" +#include "KingSystem/Utils/Types.h" + namespace ksys::phys { class RigidBody; -class RigidBodyRequestMgr { +class RigidBodyRequestMgr : public sead::hostio::Node { public: + RigidBodyRequestMgr(); + virtual ~RigidBodyRequestMgr(); + + void init(sead::Heap* heap); + void sub_7100FA6C8C(bool, RigidBody*); + +private: + struct Unk1; + struct Unk2; + struct Unk3; + + struct Unk4 { + u8 _0[0x10]; + }; + KSYS_CHECK_SIZE_NX150(Unk4, 0x10); + + struct Unk5 { + u8 _0[0x18]; + }; + KSYS_CHECK_SIZE_NX150(Unk5, 0x18); + + struct Unk6 { + sead::Vector3f _0 = sead::Vector3f::zero; + u32 _c = 0x1000000; + sead::Vector3f _10 = sead::Vector3f::zero; + void* _20{}; + sead::Vector3f _28 = sead::Vector3f::ey; + float _34{}; + sead::Vector3f _38 = sead::Vector3f::zero; + float _44{}; + }; + KSYS_CHECK_SIZE_NX150(Unk6, 0x48); + + struct PointCallback : RigidContactPointsEx::Callback { + explicit PointCallback(RigidBodyRequestMgr* mgr_) : mgr(mgr_) {} + void invoke(void* arg) override { mgr->someFunction2(arg); } + + RigidBodyRequestMgr* mgr; + }; + + // FIXME: rename, implement + void someFunction2(void* arg); + static void someFunction(void* arg); + + static constexpr int NumRigidBodyBuffers = 2; + + sead::SafeArray, NumRigidBodyBuffers> mRigidBodies1; + util::LockFreeQueue _38; + util::LockFreeQueue _50; + sead::SafeArray, NumRigidBodyBuffers> mRigidBodies2; + util::LockFreeQueue _98; + util::LockFreeQueue _b0; + util::LockFreeQueue _c8; + util::LockFreeQueue _e0; + sead::PtrArray _f8; + sead::Buffer _108; + sead::Atomic _118; + sead::Buffer _120; + sead::Atomic _130; + sead::Buffer _138; + u32 _148{}; + RigidContactPointsEx* mContactPoints{}; + sead::SafeArray mCriticalSections; + sead::CriticalSection mCS; + float _218 = 1.0; + float _21c = 1.0 / 30.0; + float _220 = 1.0 / 30.0; + float _224 = 1.0 / 30.0; + float _228 = 1.0 / 30.0; + sead::Atomic _22c; + u32 mWaterIceSubmatIdx{}; + u32 mWaterHotSubmatIdx{}; + u32 mWaterPoisonSubmatIdx{}; + PointCallback mCallback{this}; + sead::Delegate1Func _250{&RigidBodyRequestMgr::someFunction}; }; +KSYS_CHECK_SIZE_NX150(RigidBodyRequestMgr, 0x260); } // namespace ksys::phys diff --git a/src/KingSystem/Physics/System/physRigidContactPoints.h b/src/KingSystem/Physics/System/physRigidContactPoints.h index 499391e5..621555d6 100644 --- a/src/KingSystem/Physics/System/physRigidContactPoints.h +++ b/src/KingSystem/Physics/System/physRigidContactPoints.h @@ -21,6 +21,8 @@ public: virtual ~IRigidContactPoints() = default; virtual void freePoints() = 0; + void set30(u32 value) { _30 = value; } + bool isLinked() const { return mListNode.isLinked(); } static constexpr size_t getListNodeOffset() { return offsetof(IRigidContactPoints, mListNode); } diff --git a/src/KingSystem/Physics/System/physRigidContactPointsEx.h b/src/KingSystem/Physics/System/physRigidContactPointsEx.h index e581699e..04f3bf90 100644 --- a/src/KingSystem/Physics/System/physRigidContactPointsEx.h +++ b/src/KingSystem/Physics/System/physRigidContactPointsEx.h @@ -2,6 +2,7 @@ #include #include +#include #include #include "KingSystem/Physics/System/physDefines.h" #include "KingSystem/Physics/System/physRigidContactPoints.h" @@ -80,6 +81,9 @@ public: const ContactPoint* const* mPointsStart = nullptr; }; + // FIXME: figure out the types + using Callback = sead::IDelegate1; + static RigidContactPointsEx* make(sead::Heap* heap, int num, int num2, const sead::SafeString& name, int a, int b, int c); static void free(RigidContactPointsEx* instance); @@ -89,9 +93,11 @@ public: void freePoints() override; virtual void allocPoints(sead::Heap* heap, int num, int num2); - bool registerLayerPair(ContactLayer layer1, ContactLayer layer2, bool enabled); + bool registerLayerPair(ContactLayer layer1, ContactLayer layer2, bool enabled = true); bool isPairUnknown(ContactLayer layer1, ContactLayer layer2) const; + void setCallback(Callback* callback) { mCallback = callback; } + auto begin() const { return Iterator(mPoints, _18); } auto end() const { return IteratorEnd(mPoints, _18); } @@ -105,7 +111,7 @@ private: Points mPoints{}; sead::ObjArray mLayerEntries; ContactLayerType mLayerType = ContactLayerType::Invalid; - void* _80 = nullptr; + Callback* mCallback = nullptr; }; KSYS_CHECK_SIZE_NX150(RigidContactPointsEx, 0x88);