diff --git a/dGame/dComponents/MovingPlatformComponent.cpp b/dGame/dComponents/MovingPlatformComponent.cpp index 047a40802..0f2bac7b5 100644 --- a/dGame/dComponents/MovingPlatformComponent.cpp +++ b/dGame/dComponents/MovingPlatformComponent.cpp @@ -21,6 +21,7 @@ #include "ObjectLoadState.h" #include "MovementAIComponent.h" #include "QuickBuildComponent.h" +#include "PhantomPhysicsComponent.h" #include "eQuickBuildState.h" void PlatformSubComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) { @@ -509,6 +510,22 @@ void MovingPlatformComponent::MoverHandleWaypointArrived(const uint32_t index, c if ((mover->mState & ePlatformStateFlag::Stopped) == 0) Game::entityManager->SerializeEntity(m_Parent); } +// LWOPlatform moves its object with SetPosition and SetRotation ignoring the dirty flags (ProcessStateChange 0x00cce380, +// Resync 0x00be9310), and its physics carry it between waypoints: live did not serialize a mover's physics for it, each +// client moves its own. The server's object goes with the mover, turned between the two waypoints' rotations. +void MovingPlatformComponent::MoveObject() { + const auto* const mover = GetMoverSubComponent(); + const auto& from = m_Path->pathWaypoints[mover->mCurrentWaypointIndex].rotation; + const auto& to = m_Path->pathWaypoints[mover->mNextWaypointIndex].rotation; + const auto rotation = from == to ? from : glm::slerp(from, to, std::min(mover->mPercentBetweenPoints, 1.0f)); + if (auto* const simplePhysics = m_Parent->GetComponent()) { + simplePhysics->SetPositionAndRotationIgnoringDirtyFlags(mover->mPosition, rotation); + } + if (auto* const phantomPhysics = m_Parent->GetComponent()) { + phantomPhysics->SetPositionAndRotationIgnoringDirtyFlags(mover->mPosition, rotation); + } +} + // LWOPlatform::Run 0x00c87080 and RunPlatform 0x00c86e10. The client's platform is moved by its physics, at the // velocity SetLinearVelocity 0x00be9200 gives it; the server moves it here by the same speed. void MovingPlatformComponent::Run(const float deltaTime) { @@ -550,6 +567,8 @@ void MovingPlatformComponent::Run(const float deltaTime) { mover->mPosition = from + (to - from) * mover->mPercentBetweenPoints; } + MoveObject(); + const bool stopped = (mover->mState & ePlatformStateFlag::Stopped) != 0; // LWOPlatform::IncrementWaitingTime 0x00bd4370 bool doneWaiting = false; @@ -576,6 +595,7 @@ void MovingPlatformComponent::Run(const float deltaTime) { // Live serialized a platform leaving its waypoint if (doneWaiting) Game::entityManager->SerializeEntity(m_Parent); } + if (IsMover()) MoveObject(); } void MovingPlatformComponent::FillResync(GameMessages::PlatformResync& resync) const { diff --git a/dGame/dComponents/MovingPlatformComponent.h b/dGame/dComponents/MovingPlatformComponent.h index fd030d82d..aae1bca82 100644 --- a/dGame/dComponents/MovingPlatformComponent.h +++ b/dGame/dComponents/MovingPlatformComponent.h @@ -320,6 +320,8 @@ private: // LWOPlatform::ProcessStateChange: index becomes the current waypoint (the next from the path's behavior and direction) void MoverProcessStateChange(uint32_t index); + // Puts the object where the mover is, without a serialization of its physics + void MoveObject(); // LWOPlatform::SetWaitingTime: starts a wait of at least 0.1 s void MoverSetWaitingTime(float waitTime, float idleTimeElapsed); // LWOPlatform::ResumeTravelling: stopped between two waypoints it travels on; at a waypoint it first waits there diff --git a/dGame/dComponents/Physics/PhysicsComponent.h b/dGame/dComponents/Physics/PhysicsComponent.h index da10e3146..df7a5ceeb 100644 --- a/dGame/dComponents/Physics/PhysicsComponent.h +++ b/dGame/dComponents/Physics/PhysicsComponent.h @@ -37,6 +37,14 @@ public: const NiQuaternion& GetRotation() const { return m_Rotation; } virtual void SetRotation(const NiQuaternion& rot) { if (m_Rotation == rot) return; m_Rotation = rot; m_DirtyPosition = true; } + // As the client's SetPosition and SetRotation with bIgnoreDirtyFlags: moved without being serialized for it + void SetPositionAndRotationIgnoringDirtyFlags(const NiPoint3& pos, const NiQuaternion& rot) { + const bool dirty = m_DirtyPosition; + SetPosition(pos); + SetRotation(rot); + m_DirtyPosition = dirty; + } + int32_t GetCollisionGroup() const noexcept { return m_CollisionGroup; } void SetCollisionGroup(int32_t group) noexcept { m_CollisionGroup = group; } diff --git a/tests/dGameTests/dComponentsTests/MovingPlatformComponentTests.cpp b/tests/dGameTests/dComponentsTests/MovingPlatformComponentTests.cpp index 883b5f29d..db210ca28 100644 --- a/tests/dGameTests/dComponentsTests/MovingPlatformComponentTests.cpp +++ b/tests/dGameTests/dComponentsTests/MovingPlatformComponentTests.cpp @@ -299,3 +299,41 @@ TEST_F(MovingPlatformComponentTest, MoverOnceLikeLiveFallingTile) { EXPECT_EQ(mover->mNextWaypointIndex, 1u); EXPECT_FALSE(mover->mInReverse); } + +// The object goes with its mover on the server (LWOPlatform moves it ignoring the dirty flags), turned between the +// waypoints' rotations, and its physics are not serialized for it: live sent a moving target's mover data only +TEST_F(MovingPlatformComponentTest, MoverCarriesItsObject) { + Path path; + path.pathType = PathType::MovingPlatform; + path.pathName = "TestCarryPath"; + path.pathBehavior = PathBehavior::Once; + PathWaypoint first; + first.position = NiPoint3(0.0f, 0.0f, 0.0f); + first.speed = 5.0f; + PathWaypoint second = first; + second.position = NiPoint3(10.0f, 0.0f, 0.0f); + second.rotation = NiQuaternion(0.0f, 0.0f, 1.0f, 0.0f); + path.pathWaypoints = { first, second }; + const_cast(Game::zoneManager->GetZone())->AddPath(path); + + info.pos = NiPoint3(0.0f, 0.0f, 0.0f); + Entity entity(288300744895900110, info); + auto* const simplePhysics = entity.AddComponent(-1); + auto* const platform = entity.AddComponent(-1, "TestCarryPath"); + + RakNet::BitStream before; + simplePhysics->Serialize(before, false); + platform->Run(0.11f); + platform->Run(1.0f); + EXPECT_NEAR(entity.GetPosition().x, 5.0f, 0.01f); + EXPECT_NEAR(std::abs(entity.GetRotation().y), 0.7071f, 0.001f); + + RakNet::BitStream moved; + simplePhysics->Serialize(moved, false); + // No position and rotation (seven floats) in it + EXPECT_LT(moved.GetNumberOfBitsUsed(), 7u * 32u); + + platform->Run(1.1f); + EXPECT_EQ(entity.GetPosition(), NiPoint3(10.0f, 0.0f, 0.0f)); + EXPECT_NEAR(std::abs(entity.GetRotation().y), 1.0f, 0.001f); +}