From bff3fe63635483f29e541ee23489890a7cb28c78 Mon Sep 17 00:00:00 2001 From: Aaron Kimbrell Date: Thu, 1 Oct 2026 15:59:45 -0500 Subject: [PATCH] refactor(component): moving platform subcomponents like the client's PlatformSubComponent (LWOPlatform: the state between waypoints and its serialization) is the base of MoverSubComponent (4), SimpleMoverSubComponent (5) and the new RotatorSubComponent (6), as LWOPlatformMover, LWOPlatformSimpleMover and LWOPlatformRotator are built on LWOPlatform. The platform keeps one subcomponent per type the client makes (LWOMovingPlatformComponent::SetupPlatform: one per platformIs* setting that is true) and sends them all; the first is the one the server runs. Same bytes for every platform with one subcomponent. Co-Authored-By: Claude Opus 5.5 --- dGame/dComponents/MovingPlatformComponent.cpp | 120 ++++++++-------- dGame/dComponents/MovingPlatformComponent.h | 129 +++++++++++------- .../MovingPlatformComponentTests.cpp | 11 ++ 3 files changed, 145 insertions(+), 115 deletions(-) diff --git a/dGame/dComponents/MovingPlatformComponent.cpp b/dGame/dComponents/MovingPlatformComponent.cpp index b2d61f30e..db16d0894 100644 --- a/dGame/dComponents/MovingPlatformComponent.cpp +++ b/dGame/dComponents/MovingPlatformComponent.cpp @@ -18,15 +18,7 @@ #include -MoverSubComponent::MoverSubComponent(const NiPoint3& startPos) { - mPosition = startPos; -} - -MoverSubComponent::~MoverSubComponent() = default; - -// LWOPlatform::Deserialize 0x00c870e0: a dirty bit, the state, desired waypoint, whether to stop there, reverse, the -// percent between the waypoints, where it is, the current and next waypoints, the time waited and the time travelled -void MoverSubComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) { +void PlatformSubComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) { outBitStream.Write(true); outBitStream.Write(mState); @@ -68,7 +60,7 @@ void SimpleMoverSubComponent::Serialize(RakNet::BitStream& outBitStream, bool bI outBitStream.Write(bIsInitialUpdate || mDirtyState); if (bIsInitialUpdate || mDirtyState) { outBitStream.Write(mState); - outBitStream.Write(mCurrentWaypointIndex); + outBitStream.Write(static_cast(mCurrentWaypointIndex)); outBitStream.Write(mInReverse); if (!bIsInitialUpdate) mDirtyState = false; } @@ -80,19 +72,25 @@ NiPoint3 SimpleMoverSubComponent::GetWaypointPosition(const int32_t index) const return mStartPosition + NiPoint3(moved.x, moved.y, moved.z); } -eMoverSubComponentType ChooseMoverSubComponentType(const int32_t componentID, const Entity& entity) { +std::vector ChooseSubComponentTypes(const int32_t componentID, const Entity& entity) { const bool setRotater = entity.HasVar(u"platformIsRotater"); const bool setMover = entity.HasVar(u"platformIsMover"); const bool setSimpleMover = entity.HasVar(u"platformIsSimpleMover"); if (!setRotater && !setMover && !setSimpleMover) { - return componentID < 1 ? eMoverSubComponentType::mover : eMoverSubComponentType::simpleMover; + return { componentID < 1 ? eMoverSubComponentType::mover : eMoverSubComponentType::simpleMover }; } - if (setMover && entity.GetVar(u"platformIsMover")) return eMoverSubComponentType::mover; - if (setSimpleMover && entity.GetVar(u"platformIsSimpleMover")) return eMoverSubComponentType::simpleMover; - if (setRotater && entity.GetVar(u"platformIsRotater")) return eMoverSubComponentType::rotater; - return eMoverSubComponentType::none; + std::vector types; + if (setMover && entity.GetVar(u"platformIsMover")) types.push_back(eMoverSubComponentType::mover); + if (setSimpleMover && entity.GetVar(u"platformIsSimpleMover")) types.push_back(eMoverSubComponentType::simpleMover); + if (setRotater && entity.GetVar(u"platformIsRotater")) types.push_back(eMoverSubComponentType::rotater); + return types; +} + +eMoverSubComponentType ChooseMoverSubComponentType(const int32_t componentID, const Entity& entity) { + const auto types = ChooseSubComponentTypes(componentID, entity); + return types.empty() ? eMoverSubComponentType::none : types.front(); } //------------- MovingPlatformComponent below -------------- @@ -102,39 +100,45 @@ MovingPlatformComponent::MovingPlatformComponent(Entity* parent, const int32_t c m_NoAutoStart = false; } -// The mover type, and a simple mover's MovingPlatforms row +// The subcomponents (LWOMovingPlatformComponent::SetupPlatform: one per type, in the client's order), and a simple +// mover's MovingPlatforms row void MovingPlatformComponent::LoadDataFromTemplate() { - m_MoverSubComponentType = ChooseMoverSubComponentType(m_ComponentID, *m_Parent); - if (m_MoverSubComponentType == eMoverSubComponentType::simpleMover) { - auto* const simpleMover = new SimpleMoverSubComponent(); - m_MoverSubComponent = simpleMover; - simpleMover->mStartPosition = m_Parent->GetDefaultPosition(); - simpleMover->mStartRotation = m_Parent->GetDefaultRotation(); + for (const auto type : ChooseSubComponentTypes(m_ComponentID, *m_Parent)) { + if (type == eMoverSubComponentType::simpleMover) { + auto simpleMover = std::make_unique(); + simpleMover->mStartPosition = m_Parent->GetDefaultPosition(); + simpleMover->mStartRotation = m_Parent->GetDefaultRotation(); - // LWOPlatformSimpleMover::LoadTemplateData: the MovingPlatforms row, unless the object sets dbonly false - const bool dbOnly = m_Parent->GetVar(u"dbonly"); - if (m_ComponentID > 0 && (!m_Parent->HasVar(u"dbonly") || dbOnly)) { - auto query = CDClientDatabase::CreatePreppedStmt( - "SELECT platformMoveX, platformMoveY, platformMoveZ, platformMoveTime, platformStartAtEnd FROM MovingPlatforms WHERE id = ?;"); - query.bind(1, m_ComponentID); - auto result = query.execQuery(); - if (!result.eof()) { - simpleMover->mMove = NiPoint3(result.getFloatField(0), result.getFloatField(1), result.getFloatField(2)); - simpleMover->mMoveTime = result.getFloatField(3); - simpleMover->mStartAtEnd = result.getIntField(4) != 0; - } else { - LOG("LOT %i couldn't find simple mover %i in MovingPlatforms", m_Parent->GetLOT(), m_ComponentID); + // LWOPlatformSimpleMover::LoadTemplateData: the MovingPlatforms row, unless the object sets dbonly false + const bool dbOnly = m_Parent->GetVar(u"dbonly"); + if (m_ComponentID > 0 && (!m_Parent->HasVar(u"dbonly") || dbOnly)) { + auto query = CDClientDatabase::CreatePreppedStmt( + "SELECT platformMoveX, platformMoveY, platformMoveZ, platformMoveTime, platformStartAtEnd FROM MovingPlatforms WHERE id = ?;"); + query.bind(1, m_ComponentID); + auto result = query.execQuery(); + if (!result.eof()) { + simpleMover->mMove = NiPoint3(result.getFloatField(0), result.getFloatField(1), result.getFloatField(2)); + simpleMover->mMoveTime = result.getFloatField(3); + simpleMover->mStartAtEnd = result.getIntField(4) != 0; + } else { + LOG("LOT %i couldn't find simple mover %i in MovingPlatforms", m_Parent->GetLOT(), m_ComponentID); + } } + m_SubComponents.push_back(std::move(simpleMover)); + } else if (type == eMoverSubComponentType::rotater) { + m_SubComponents.push_back(std::make_unique(m_Parent->GetDefaultPosition())); + } else { + m_SubComponents.push_back(std::make_unique(m_Parent->GetDefaultPosition())); } - } else { - m_MoverSubComponent = new MoverSubComponent(m_Parent->GetDefaultPosition()); } + m_MoverSubComponentType = m_SubComponents.empty() ? eMoverSubComponentType::none : m_SubComponents.front()->GetType(); } // The level config: a simple mover's settings, the path and where and whether a mover starts void MovingPlatformComponent::LoadConfigData() { - if (m_MoverSubComponentType == eMoverSubComponentType::simpleMover) { - auto* const simpleMover = GetSimpleMoverSubComponent(); + for (const auto& subComponent : m_SubComponents) { + if (subComponent->GetType() != eMoverSubComponentType::simpleMover) continue; + auto* const simpleMover = static_cast(subComponent.get()); const bool dbOnly = m_Parent->GetVar(u"dbonly"); // LWOPlatformSimpleMover::LoadConfigData: the object's settings over the table's, unless it is db only @@ -178,15 +182,12 @@ void MovingPlatformComponent::LoadConfigData() { if (simplePhysics && !m_Parent->HasVar(u"motionType")) simplePhysics->SetPhysicsMotionState(SimplePhysicsComponent::MOTION_TYPE_KEYFRAMED); } -MovingPlatformComponent::~MovingPlatformComponent() { - if (m_MoverSubComponentType == eMoverSubComponentType::simpleMover) delete static_cast(m_MoverSubComponent); - else delete static_cast(m_MoverSubComponent); -} +MovingPlatformComponent::~MovingPlatformComponent() = default; void MovingPlatformComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) { // Live always constructed a simple mover with its starting point (the client builds its path from it), and // serialized it only when its state changed: no path, then the one subcomponent and the 0 bit ending the list - if (m_MoverSubComponentType == eMoverSubComponentType::simpleMover) { + if (m_SubComponents.size() == 1 && m_MoverSubComponentType == eMoverSubComponentType::simpleMover) { auto* const simpleMover = GetSimpleMoverSubComponent(); const bool dirty = bIsInitialUpdate || simpleMover->mDirtyStartingPoint || simpleMover->mDirtyState; outBitStream.Write(dirty); @@ -220,19 +221,14 @@ void MovingPlatformComponent::Serialize(RakNet::BitStream& outBitStream, bool bI } } - // A platform the client made no subcomponent for must not get one: it could not read it - const auto hasPlatform = m_MoverSubComponentType != eMoverSubComponentType::none; - outBitStream.Write(hasPlatform); - - if (hasPlatform) { - // A rotater's data is a mover's (both read by LWOPlatform::Deserialize) - auto* mover = static_cast(m_MoverSubComponent); - outBitStream.Write(m_MoverSubComponentType); - mover->Serialize(outBitStream, bIsInitialUpdate); - - // The client reads subcomponents while a 1 bit comes before one: a 0 bit ends the list - outBitStream.Write0(); + // The subcomponents, each after a 1 bit with its type (a rotater sends a mover's data, both LWOPlatform's); a 0 bit + // ends the list. A platform the client made none for gets none: it could not read one. + for (const auto& subComponent : m_SubComponents) { + outBitStream.Write1(); + outBitStream.Write(subComponent->GetType()); + subComponent->Serialize(outBitStream, bIsInitialUpdate); } + outBitStream.Write0(); } void MovingPlatformComponent::OnQuickBuildInitilized() { @@ -549,14 +545,14 @@ size_t MovingPlatformComponent::GetLastWaypointIndex() const { return m_Path->pathWaypoints.size() - 1; } -MoverSubComponent* MovingPlatformComponent::GetMoverSubComponent() const { - if (m_MoverSubComponentType == eMoverSubComponentType::simpleMover) return nullptr; - return static_cast(m_MoverSubComponent); +PlatformSubComponent* MovingPlatformComponent::GetMoverSubComponent() const { + if (m_SubComponents.empty() || m_MoverSubComponentType == eMoverSubComponentType::simpleMover) return nullptr; + return m_SubComponents.front().get(); } SimpleMoverSubComponent* MovingPlatformComponent::GetSimpleMoverSubComponent() const { - if (m_MoverSubComponentType != eMoverSubComponentType::simpleMover) return nullptr; - return static_cast(m_MoverSubComponent); + if (m_SubComponents.empty() || m_MoverSubComponentType != eMoverSubComponentType::simpleMover) return nullptr; + return static_cast(m_SubComponents.front().get()); } // The client runs the two point path at |platformMove| / platformMoveTime, so it takes platformMoveTime seconds from diff --git a/dGame/dComponents/MovingPlatformComponent.h b/dGame/dComponents/MovingPlatformComponent.h index 20d541853..8c5d0ce53 100644 --- a/dGame/dComponents/MovingPlatformComponent.h +++ b/dGame/dComponents/MovingPlatformComponent.h @@ -6,6 +6,9 @@ #ifndef MOVINGPLATFORMCOMPONENT_H #define MOVINGPLATFORMCOMPONENT_H +#include +#include + #include "RakNetTypes.h" #include "NiPoint3.h" #include "NiQuaternion.h" @@ -54,60 +57,29 @@ namespace ePlatformStateFlag { }; /** - * How the client picks the subcomponent (LWOMovingPlatformComponent::LoadConfigData): the platformIsRotater, - * platformIsMover and platformIsSimpleMover settings, and when none is set a mover for a platform with no registry - * component (set up from its attached path) or a simple mover for one with a registry component. - * The client makes one subcomponent per flag set (mover, simple mover, rotater, LWOMovingPlatformComponent::SetupPlatform); - * DLU keeps one, the first in that order. + * How the client picks the subcomponents (LWOMovingPlatformComponent::LoadConfigData and SetupPlatform): one for each + * of the platformIsMover, platformIsSimpleMover and platformIsRotater settings that is true, in that order, and when + * none is set a mover for a platform with no registry component (set up from its attached path) or a simple mover for + * one with a registry component. */ +std::vector ChooseSubComponentTypes(int32_t componentID, const Entity& entity); + +// The first of ChooseSubComponentTypes (none when there are none): the one the server runs eMoverSubComponentType ChooseMoverSubComponentType(int32_t componentID, const Entity& entity); /** - * A mover between a start point and that point moved by platformMove (in the start rotation), taking - * platformMoveTime seconds, as LWOPlatformSimpleMover::GenerateSimpleMoverPath builds its two waypoint path. + * The base of the platform subcomponents, as the client's LWOPlatform (mover, simple mover and rotator are built on + * it): the platform's state between the waypoints of its path, which LWOPlatform::Deserialize 0x00c870e0 reads. */ -class SimpleMoverSubComponent { +class PlatformSubComponent { public: - void Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate); + virtual ~PlatformSubComponent() = default; - NiPoint3 mStartPosition{}; - NiQuaternion mStartRotation{}; - NiPoint3 mMove{}; - float mMoveTime = 0.0f; - bool mStartAtEnd = false; + virtual eMoverSubComponentType GetType() const = 0; - /** - * ePlatformStateFlag bits. Live constructed simple movers stopped at their desired waypoint. - */ - uint32_t mState = ePlatformStateFlag::Stopped | ePlatformStateFlag::ReachedDesiredWaypoint; - int32_t mCurrentWaypointIndex = 0; - bool mInReverse = false; - - bool mDirtyStartingPoint = false; - bool mDirtyState = false; - - /** - * The world position of a waypoint (0 the start, 1 the end) - */ - NiPoint3 GetWaypointPosition(int32_t index) const; -}; - -/** - * Sub component for moving platforms that determine the actual current movement state - */ -/** - * A mover (or rotater) along its path, run by the server as live ran it: the client's LWOPlatform logic (ArrivedAtWaypoint - * 0x00c86c30, ProcessStateChange 0x00cce380, GetNextWaypoint 0x00bd4490 / GetNextReversedWaypoint 0x00bd4420, the speeds - * of GetWaypointSpeed 0x00be90d0), with live's states: it travels (2), and at a waypoint it is stopped (1) with - * ReachedDesiredWaypoint (8), and ReachedFinalDestination (16) at either end, while it waits the waypoint's wait time; - * each arrival and departure is serialized (NT movers in the live captures: 25 on arrival, 2 on departure). - */ -class MoverSubComponent { -public: - MoverSubComponent(const NiPoint3& startPos); - ~MoverSubComponent(); - - void Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate); + // LWOPlatform::Deserialize 0x00c870e0: a dirty bit, the state, desired waypoint, whether to stop there, reverse, the + // percent between the waypoints, where it is, the current and next waypoints, the time waited and the time travelled + virtual void Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate); // ePlatformStateFlag bits uint32_t mState = ePlatformStateFlag::Stopped | ePlatformStateFlag::ReachedDesiredWaypoint; @@ -135,6 +107,58 @@ public: bool mTimeBased = false; }; +/** + * A mover along its path (4), as the client's LWOPlatformMover, run by the server as live ran it: the client's + * LWOPlatform logic (ArrivedAtWaypoint 0x00c86c30, ProcessStateChange 0x00cce380, GetNextWaypoint 0x00bd4490 / + * GetNextReversedWaypoint 0x00bd4420, the speeds of GetWaypointSpeed 0x00be90d0), with live's states: it travels (2), + * and at a waypoint it is stopped (1) with ReachedDesiredWaypoint (8), and ReachedFinalDestination (16) at either end, + * while it waits the waypoint's wait time; each arrival and departure is serialized (NT movers in the live captures: + * 25 on arrival, 2 on departure). + */ +class MoverSubComponent final : public PlatformSubComponent { +public: + MoverSubComponent(const NiPoint3& startPos) { mPosition = startPos; } + eMoverSubComponentType GetType() const override { return eMoverSubComponentType::mover; } +}; + +/** + * A rotator (6), as the client's LWOPlatformRotator: it only spins (rotX/rotY/rotZ at rate) while travelling; it sends + * the LWOPlatform data a mover sends. + */ +class RotatorSubComponent final : public PlatformSubComponent { +public: + RotatorSubComponent(const NiPoint3& startPos) { mPosition = startPos; } + eMoverSubComponentType GetType() const override { return eMoverSubComponentType::rotater; } +}; + +/** + * A simple mover (5), as the client's LWOPlatformSimpleMover: between a start point and that point moved by + * platformMove (in the start rotation), taking platformMoveTime seconds, as GenerateSimpleMoverPath builds its two + * waypoint path. Live constructed simple movers stopped at their desired waypoint. + */ +class SimpleMoverSubComponent final : public PlatformSubComponent { +public: + eMoverSubComponentType GetType() const override { return eMoverSubComponentType::simpleMover; } + + // LWOPlatformSimpleMover::Deserialize: a dirty bit, then whether there is a starting point with its position and + // rotation (w, x, y, z); a dirty bit, then the state, the current waypoint and whether it is reversing + void Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) override; + + NiPoint3 mStartPosition{}; + NiQuaternion mStartRotation{}; + NiPoint3 mMove{}; + float mMoveTime = 0.0f; + bool mStartAtEnd = false; + + bool mDirtyStartingPoint = false; + bool mDirtyState = false; + + /** + * The world position of a waypoint (0 the start, 1 the end) + */ + NiPoint3 GetWaypointPosition(int32_t index) const; +}; + /** * Represents entities that may be moving platforms, indicating how they should move through the world. @@ -219,10 +243,9 @@ public: size_t GetLastWaypointIndex() const; /** - * Returns the sub component that actually defines how the platform moves around (speeds, etc). - * @return the sub component that actually defines how the platform moves around + * The mover or rotator the server runs (the first subcomponent), if it is one */ - MoverSubComponent* GetMoverSubComponent() const; + PlatformSubComponent* GetMoverSubComponent() const; /** * Returns the simple mover sub component, if this platform is a simple mover @@ -258,14 +281,14 @@ private: uint32_t m_StartWaypoint = 0; /** - * The type of the subcomponent + * The type of the first subcomponent, the one the server runs (none without one) */ - eMoverSubComponentType m_MoverSubComponentType; + eMoverSubComponentType m_MoverSubComponentType = eMoverSubComponentType::none; /** - * The mover sub component that belongs to this platform + * The subcomponents, one per type the client makes (ChooseSubComponentTypes), in its order */ - void* m_MoverSubComponent; + std::vector> m_SubComponents; /** * Counts sends so an arrival timer from an older send does nothing diff --git a/tests/dGameTests/dComponentsTests/MovingPlatformComponentTests.cpp b/tests/dGameTests/dComponentsTests/MovingPlatformComponentTests.cpp index 796326ce1..04b8fcd89 100644 --- a/tests/dGameTests/dComponentsTests/MovingPlatformComponentTests.cpp +++ b/tests/dGameTests/dComponentsTests/MovingPlatformComponentTests.cpp @@ -77,6 +77,17 @@ TEST_F(MovingPlatformComponentTest, SubcomponentTypeFromLevelConfig) { info.settings.values.clear(); } +// LWOMovingPlatformComponent::SetupPlatform: one subcomponent per setting that is true, mover, simple mover, rotater +TEST_F(MovingPlatformComponentTest, OneSubcomponentPerSetting) { + info.settings.Insert(u"platformIsRotater", true); + info.settings.Insert(u"platformIsMover", true); + Entity both(288300744895900107, info); + const std::vector expected{ eMoverSubComponentType::mover, eMoverSubComponentType::rotater }; + EXPECT_EQ(ChooseSubComponentTypes(-1, both), expected); + EXPECT_EQ(ChooseMoverSubComponentType(-1, both), eMoverSubComponentType::mover); + info.settings.values.clear(); +} + // Live: 1 (subcomponents), 1 (dirty), 0 (no path), 1, type 5, the starting point (the object's position and // rotation, w first), state 9 (stopped at its desired waypoint), waypoint 0, not reversing, then the ending 0 bit TEST_F(MovingPlatformComponentTest, SimpleMoverConstructionMatchesLive) {