diff --git a/dGame/dComponents/MovingPlatformComponent.cpp b/dGame/dComponents/MovingPlatformComponent.cpp index 345e4ffad..7aec9bf2d 100644 --- a/dGame/dComponents/MovingPlatformComponent.cpp +++ b/dGame/dComponents/MovingPlatformComponent.cpp @@ -290,7 +290,7 @@ bool MovingPlatformComponent::SetCurrentPath(const std::string& pathName, const } const bool mover = IsMover(); - if (mover) MoveObject(*GetMoverSubComponent()); + if (mover) MoveObject(*GetSubComponent(eMoverSubComponentType::mover)); else if (m_Path == nullptr) LOG("Cannot setup platform on path %s", pathName.c_str()); Game::entityManager->SerializeEntity(m_Parent); return mover; @@ -348,28 +348,24 @@ void MovingPlatformComponent::Serialize(RakNet::BitStream& outBitStream, bool bI } void MovingPlatformComponent::OnQuickBuildInitilized() { - // Live left a quick built simple mover (the NJ counterweights) where it was, only resending its state - if (m_MoverSubComponentType == eMoverSubComponentType::simpleMover) { - GetSimpleMoverSubComponent()->mDirtyState = true; - return; + for (size_t i = 0; i < m_SubComponents.size(); i++) { + auto& platform = *m_SubComponents[i]; + // Live left a quick built simple mover (the NJ counterweights) where it was, only resending its state + if (IsSimpleMover(platform)) static_cast(platform).mDirtyState = true; + else PlatformStop(platform); } - StopPathing(); } void MovingPlatformComponent::OnCompleteQuickBuild() { - if (m_MoverSubComponentType == eMoverSubComponentType::simpleMover) { + for (size_t i = 0; i < m_SubComponents.size(); i++) { + auto& platform = *m_SubComponents[i]; // Live (the NJ counterweights, LOT 16141): the built simple mover is serialized waiting at its waypoint (9) and // about 0.1 s later travelling (2) to its other end, where it stops without a serialization and // PlatformAtLastWaypoint tells the scripts. A quick build's platform runs once the quick build is complete: it // then waits the shortest wait and sets off. - GetSimpleMoverSubComponent()->mDirtyState = true; - return; + if (IsSimpleMover(platform)) static_cast(platform).mDirtyState = true; + else if (!m_NoAutoStart) PlatformStart(platform); } - - if (m_NoAutoStart) - return; - - StartPathing(); } // A serialization with the platform's state in it: a simple mover only writes its state when it changed @@ -379,16 +375,20 @@ void MovingPlatformComponent::SerializePlatform(PlatformSubComponent& platform) } void MovingPlatformComponent::SetMovementState(eMovementPlatformState value) { - if (m_SubComponents.empty()) return; - auto& platform = *m_SubComponents.front(); - if (!IsSimpleMover(platform) && !IsMover()) return; - platform.mState = static_cast(value); - SerializePlatform(platform); + for (const auto& subComponent : m_SubComponents) { + if (subComponent->GetType() == eMoverSubComponentType::rotater || subComponent->mPath == nullptr) continue; + subComponent->mState = static_cast(value); + SerializePlatform(*subComponent); + } } +// What the object is told goes to each of its platforms, as the client's component passes its messages on to every +// subcomponent void MovingPlatformComponent::GotoWaypoint(uint32_t index, bool stopAtWaypoint) { - if (m_SubComponents.empty()) return; - PlatformGotoWaypoint(*m_SubComponents.front(), index, stopAtWaypoint); + const auto generation = m_SetupGeneration; + for (size_t i = 0; i < m_SubComponents.size() && generation == m_SetupGeneration; i++) { + PlatformGotoWaypoint(*m_SubComponents[i], index, stopAtWaypoint); + } } // Sends a platform to a waypoint, stopping there when stopAtWaypoint: it turns round if the waypoint is behind it. Live @@ -446,16 +446,17 @@ void MovingPlatformComponent::PlatformGotoWaypoint(PlatformSubComponent& platfor } void MovingPlatformComponent::StartPathing() { - if (m_SubComponents.empty()) return; - auto& platform = *m_SubComponents.front(); + for (size_t i = 0; i < m_SubComponents.size(); i++) PlatformStart(*m_SubComponents[i]); +} + +void MovingPlatformComponent::PlatformStart(PlatformSubComponent& platform) { + if (platform.mPath == nullptr) return; if (platform.GetType() == eMoverSubComponentType::rotater) { // A rotator on a path spins again - if (platform.mPath == nullptr) return; platform.mState = Travelling; Game::entityManager->SerializeEntity(m_Parent); return; } - if (platform.mPath == nullptr) return; if ((platform.mState & Travelling) != 0) return; // Stopped between two waypoints it carries on; at a waypoint it first waits there for the waypoint's wait (issue // 964: the AG monument elevator waited about 2 s before rising). A simple mover's path is start to end: from one @@ -465,8 +466,10 @@ void MovingPlatformComponent::StartPathing() { } void MovingPlatformComponent::StopPathing() { - if (m_SubComponents.empty()) return; - auto& platform = *m_SubComponents.front(); + for (size_t i = 0; i < m_SubComponents.size(); i++) PlatformStop(*m_SubComponents[i]); +} + +void MovingPlatformComponent::PlatformStop(PlatformSubComponent& platform) { if (IsSimpleMover(platform)) { // At an end it is stopped; on its way it goes on to the end it was heading for, to stop there (where its path, // run once, stops it anyway) @@ -486,7 +489,7 @@ void MovingPlatformComponent::StopPathing() { Game::entityManager->SerializeEntity(m_Parent); return; } - if (!IsMover()) return; + if (platform.mPath == nullptr) return; // Stopped where it is (the client stops on the stopped bit, 4) StopTravelling(platform); platform.mVelocity = NiPoint3Constant::ZERO; @@ -496,7 +499,8 @@ void MovingPlatformComponent::StopPathing() { } bool MovingPlatformComponent::IsMover() const { - return m_MoverSubComponentType == eMoverSubComponentType::mover && m_SubComponents.front()->mPath != nullptr; + const auto* const mover = GetSubComponent(eMoverSubComponentType::mover); + return mover != nullptr && mover->mPath != nullptr; } // The waypoint becomes the platform's current one, where it now is. The next follows from the path's behavior and the @@ -635,6 +639,7 @@ bool MovingPlatformComponent::PlatformArrivedAtWaypoint(PlatformSubComponent& pl StartTravelling(platform); } PlatformProcessStateChange(platform, arrived); + MoveObject(platform, true); if (!PlatformHandleWaypointArrived(platform, arrived, atDesiredWaypoint, atLastWaypoint)) return false; // A simple mover sent elsewhere on its way (it cannot turn between its ends) sets off from where it stopped @@ -663,9 +668,18 @@ bool MovingPlatformComponent::PlatformHandleWaypointArrived(PlatformSubComponent // The client's platform moves its object ignoring the dirty flags, 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 platform, -// turned between the two waypoints' rotations. -void MovingPlatformComponent::MoveObject(const PlatformSubComponent& platform) { +// turned between the two waypoints' rotations. Where the object has a rotator on the path as well, the rotator turns +// it (the client's rotator puts its own angular velocity back on the object every frame), so the object keeps the +// rotation it has; only at a waypoint it arrives at is it put in the waypoint's rotation. +void MovingPlatformComponent::MoveObject(const PlatformSubComponent& platform, const bool atWaypoint) { const auto& from = platform.mPath->pathWaypoints[platform.mCurrentWaypointIndex].rotation; + if (!atWaypoint) { + const auto* const rotator = GetSubComponent(eMoverSubComponentType::rotater); + if (rotator != nullptr && rotator->mPath != nullptr) { + SetObjectTransform(platform.mPosition, m_Parent->GetRotation()); + return; + } + } const auto& to = platform.mPath->pathWaypoints[platform.mNextWaypointIndex].rotation; const auto rotation = from == to ? from : glm::slerp(from, to, std::min(platform.mPercentBetweenPoints, 1.0f)); SetObjectTransform(platform.mPosition, rotation); @@ -756,11 +770,14 @@ NiPoint3 CalcRotatorAngularVelocity(const float rotX, const float rotY, const fl void MovingPlatformComponent::RunRotator(RotatorSubComponent& rotator, const float deltaTime) { if (rotator.mPath == nullptr) return; + // It turns the object where the object is: a mover on the same object may have moved it + auto position = m_Parent->GetPosition(); auto rotation = m_Parent->GetRotation(); bool moved = false; if (!rotator.mPlaced) { // The object is put at its waypoint when the rotator is set up on its path rotator.mPlaced = true; + position = rotator.mPosition; rotation = rotator.mPath->pathWaypoints[rotator.mCurrentWaypointIndex].rotation; moved = true; } @@ -773,7 +790,7 @@ void MovingPlatformComponent::RunRotator(RotatorSubComponent& rotator, const flo moved = true; } } - if (moved) SetObjectTransform(rotator.mPosition, rotation); + if (moved) SetObjectTransform(position, rotation); } // A platform on its path. The client's is moved by its physics, at the velocity the platform gives its object; the @@ -833,16 +850,22 @@ bool MovingPlatformComponent::RunPlatform(PlatformSubComponent& platform, const return true; } +// Every platform of the object runs, in the client's order: a mover or simple mover moves the object, a rotator turns it void MovingPlatformComponent::Run(const float deltaTime) { if (m_SubComponents.empty()) return; UpdateActive(); - auto& platform = *m_SubComponents.front(); - if (platform.GetType() == eMoverSubComponentType::rotater) RunRotator(static_cast(platform), deltaTime); - else RunPlatform(platform, deltaTime); + const auto generation = m_SetupGeneration; + for (size_t i = 0; i < m_SubComponents.size(); i++) { + auto& platform = *m_SubComponents[i]; + if (platform.GetType() == eMoverSubComponentType::rotater) RunRotator(static_cast(platform), deltaTime); + else if (!RunPlatform(platform, deltaTime)) return; + // A script told of an arrival may have put the platforms on another path + if (generation != m_SetupGeneration) return; + } } void MovingPlatformComponent::FillResync(GameMessages::PlatformResync& resync) const { - auto* const mover = GetMoverSubComponent(); + auto* const mover = GetSubComponent(eMoverSubComponentType::mover); resync.bReverse = mover->mInReverse; resync.bStopAtDesiredWaypoint = mover->mShouldStopAtDesiredWaypoint; resync.eState = static_cast(mover->mState); @@ -872,8 +895,10 @@ void MovingPlatformComponent::SetNoAutoStart(const bool value) { } void MovingPlatformComponent::WarpToWaypoint(size_t index) { - if (m_SubComponents.empty()) return; - auto& platform = *m_SubComponents.front(); + for (size_t i = 0; i < m_SubComponents.size(); i++) PlatformWarpToWaypoint(*m_SubComponents[i], index); +} + +void MovingPlatformComponent::PlatformWarpToWaypoint(PlatformSubComponent& platform, size_t index) { if (platform.GetType() == eMoverSubComponentType::rotater || platform.mPath == nullptr) return; const auto& waypoints = platform.mPath->pathWaypoints; if (IsSimpleMover(platform)) { @@ -894,18 +919,26 @@ void MovingPlatformComponent::WarpToWaypoint(size_t index) { } size_t MovingPlatformComponent::GetLastWaypointIndex() const { - if (m_SubComponents.empty() || m_SubComponents.front()->mPath == nullptr) return 0; - return m_SubComponents.front()->mPath->pathWaypoints.size() - 1; + for (const auto& subComponent : m_SubComponents) { + if (subComponent->mPath != nullptr) return subComponent->mPath->pathWaypoints.size() - 1; + } + return 0; +} + +PlatformSubComponent* MovingPlatformComponent::GetSubComponent(const eMoverSubComponentType type) const { + for (const auto& subComponent : m_SubComponents) { + if (subComponent->GetType() == type) return subComponent.get(); + } + return nullptr; } PlatformSubComponent* MovingPlatformComponent::GetMoverSubComponent() const { - if (m_SubComponents.empty() || m_MoverSubComponentType == eMoverSubComponentType::simpleMover) return nullptr; - return m_SubComponents.front().get(); + auto* const mover = GetSubComponent(eMoverSubComponentType::mover); + return mover != nullptr ? mover : GetSubComponent(eMoverSubComponentType::rotater); } SimpleMoverSubComponent* MovingPlatformComponent::GetSimpleMoverSubComponent() const { - if (m_SubComponents.empty() || m_MoverSubComponentType != eMoverSubComponentType::simpleMover) return nullptr; - return static_cast(m_SubComponents.front().get()); + return static_cast(GetSubComponent(eMoverSubComponentType::simpleMover)); } // The client's arrival messages: Arrived, then ArrivedAtDesiredWaypoint at the waypoint it was sent to, then diff --git a/dGame/dComponents/MovingPlatformComponent.h b/dGame/dComponents/MovingPlatformComponent.h index d8a0d2cdd..82717f30f 100644 --- a/dGame/dComponents/MovingPlatformComponent.h +++ b/dGame/dComponents/MovingPlatformComponent.h @@ -71,7 +71,7 @@ namespace ePlatformStateFlag { */ std::vector ChooseSubComponentTypes(int32_t componentID, const Entity& entity); -// The first of ChooseSubComponentTypes (none when there are none): the one the server runs +// The first of ChooseSubComponentTypes (none when there are none) eMoverSubComponentType ChooseMoverSubComponentType(int32_t componentID, const Entity& entity); /** @@ -298,12 +298,17 @@ public: size_t GetLastWaypointIndex() const; /** - * The mover or rotator the server runs (the first subcomponent), if it is one + * The subcomponent of a type, if the platform has one + */ + PlatformSubComponent* GetSubComponent(eMoverSubComponentType type) const; + + /** + * The mover, or without one the rotator, if the platform has one */ PlatformSubComponent* GetMoverSubComponent() const; /** - * Returns the simple mover sub component, if this platform is a simple mover + * The simple mover, if the platform has one */ SimpleMoverSubComponent* GetSimpleMoverSubComponent() const; @@ -335,7 +340,7 @@ private: bool m_DirtyPath = false; /** - * The type of the first subcomponent, the one the server runs (none without one) + * The type of the first subcomponent (none without one) */ eMoverSubComponentType m_MoverSubComponentType = eMoverSubComponentType::none; @@ -378,8 +383,11 @@ private: // Tells the scripts, then a platform that travels on waits at the waypoint bool PlatformHandleWaypointArrived(PlatformSubComponent& platform, uint32_t index, bool atDesiredWaypoint, bool atLastWaypoint); void PlatformGotoWaypoint(PlatformSubComponent& platform, uint32_t index, bool stopAtWaypoint); + void PlatformStart(PlatformSubComponent& platform); + void PlatformStop(PlatformSubComponent& platform); + void PlatformWarpToWaypoint(PlatformSubComponent& platform, size_t index); // Puts the object where the platform is, without a serialization of its physics - void MoveObject(const PlatformSubComponent& platform); + void MoveObject(const PlatformSubComponent& platform, bool atWaypoint = false); void SetObjectTransform(const NiPoint3& position, const NiQuaternion& rotation); // A platform (mover or simple mover) on its path bool RunPlatform(PlatformSubComponent& platform, float deltaTime); @@ -394,7 +402,7 @@ public: // The mover's state as a PlatformResync gives it (RequestPlatformResync) void FillResync(GameMessages::PlatformResync& resync) const; - // A mover on a path, run by the server (a rotater only spins: no waypoints) + // Whether the platform has a mover on a path bool IsMover() const; // Whether the platform follows a path (its attached_path); a simple mover without one has no waypoints diff --git a/tests/dGameTests/dComponentsTests/MovingPlatformComponentTests.cpp b/tests/dGameTests/dComponentsTests/MovingPlatformComponentTests.cpp index af47069b2..37f68b7c4 100644 --- a/tests/dGameTests/dComponentsTests/MovingPlatformComponentTests.cpp +++ b/tests/dGameTests/dComponentsTests/MovingPlatformComponentTests.cpp @@ -615,6 +615,77 @@ TEST_F(MovingPlatformComponentTest, RotatorTurnsItsObject) { EXPECT_EQ(entity.GetPosition(), NiPoint3(1.0f, 2.0f, 3.0f)); } +// An object with both a mover and a rotator (platformIsMover and platformIsRotater): the client makes both and runs +// both. The mover carries the object along the path while the rotator turns it; where the mover arrives the object is +// put in the waypoint's rotation, and the rotator turns it on from there. +TEST_F(MovingPlatformComponentTest, MoverAndRotatorBothRun) { + Path path; + path.pathType = PathType::MovingPlatform; + path.pathName = "TestMoverRotatorPath"; + 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); + path.pathWaypoints = { first, second }; + const_cast(Game::zoneManager->GetZone())->AddPath(path); + + info.pos = NiPoint3(0.0f, 0.0f, 0.0f); + info.rot = QuatUtils::IDENTITY; + info.settings.Insert(u"platformIsMover", true); + info.settings.Insert(u"platformIsRotater", true); + info.settings.Insert(u"rotY", 0.5f); + info.settings.Insert(u"rate", 2.0f); + Entity entity(288300744895900122, info); + info.settings.values.clear(); + entity.AddComponent(-1); + auto* const platform = entity.AddComponent(-1, "TestMoverRotatorPath"); + auto* const mover = platform->GetSubComponent(eMoverSubComponentType::mover); + auto* const rotator = platform->GetSubComponent(eMoverSubComponentType::rotater); + ASSERT_NE(mover, nullptr); + ASSERT_NE(rotator, nullptr); + EXPECT_TRUE(platform->IsMover()); + EXPECT_EQ(mover->mState, 9u); + EXPECT_EQ(rotator->mState, 2u); + + // Both are written: the path, then type 4 and type 6 with a platform's data each + RakNet::BitStream stream; + platform->Serialize(stream, true); + const auto bits = Bits(stream); + const std::string moverType = "1" "00000100000000000000000000000000"; + const std::string rotatorType = "1" "00000110000000000000000000000000"; + const auto moverAt = bits.find(moverType); + ASSERT_NE(moverAt, std::string::npos); + EXPECT_NE(bits.find(rotatorType, moverAt + moverType.size()), std::string::npos); + + // The mover waits its 0.1 s; the rotator turns from the start: a quarter turn a second + platform->Run(0.11f); + EXPECT_EQ(mover->mState, 2u); + platform->Run(0.89f); + EXPECT_NEAR(entity.GetPosition().x, 4.45f, 0.001f); + EXPECT_NEAR(entity.GetRotation().w, 0.7071f, 0.001f); + EXPECT_NEAR(entity.GetRotation().y, -0.7071f, 0.001f); + + // At the end of the path the mover stops and the object is in the waypoint's rotation; the rotator goes on from it + platform->Run(1.2f); + EXPECT_EQ(mover->mState, 28u); + EXPECT_EQ(rotator->mState, 2u); + EXPECT_EQ(entity.GetPosition(), NiPoint3(10.0f, 0.0f, 0.0f)); + const float angle = 1.2f * 3.14159265f / 2.0f; + EXPECT_NEAR(entity.GetRotation().w, std::cos(angle / 2.0f), 0.001f); + EXPECT_NEAR(entity.GetRotation().y, -std::sin(angle / 2.0f), 0.001f); + platform->Run(1.0f); + EXPECT_EQ(entity.GetPosition(), NiPoint3(10.0f, 0.0f, 0.0f)); + + // Stopped, neither goes on + platform->StopPathing(); + EXPECT_EQ(rotator->mState & 7u, 4u); + const auto rotation = entity.GetRotation(); + platform->Run(1.0f); + EXPECT_EQ(entity.GetRotation(), rotation); +} + // Live's movers at a waypoint between the ends of their path: the object is put at the waypoint and serialized waiting // there (9), keeps the velocity it came with for the wait (LWOPlatform::SetWaitingTime only takes it away at // an end or when stopped), and is serialized leaving (2) from where that took it, with the part of the next stretch it