diff --git a/dGame/dComponents/MovingPlatformComponent.cpp b/dGame/dComponents/MovingPlatformComponent.cpp index 50622d5c5..345e4ffad 100644 --- a/dGame/dComponents/MovingPlatformComponent.cpp +++ b/dGame/dComponents/MovingPlatformComponent.cpp @@ -47,8 +47,8 @@ void PlatformSubComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsIn //------------- SimpleMoverSubComponent below -------------- -// 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 +// 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 SimpleMoverSubComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) { outBitStream.Write(bIsInitialUpdate || mDirtyStartingPoint); if (bIsInitialUpdate || mDirtyStartingPoint) { @@ -78,6 +78,30 @@ NiPoint3 SimpleMoverSubComponent::GetWaypointPosition(const int32_t index) const return mStartPosition + NiPoint3(moved.x, moved.y, moved.z); } +SimpleMoverSubComponent::SimpleMoverSubComponent() = default; +SimpleMoverSubComponent::~SimpleMoverSubComponent() = default; + +// As the client builds it from the starting point it is constructed with: a path run once, from the start to the start +// moved by platformMove (in the start's rotation), both waypoints with the start's rotation and the speed that covers +// the way in platformMoveTime (none without a way or a time) +void SimpleMoverSubComponent::GeneratePath() { + mOwnPath = std::make_unique(); + mOwnPath->pathType = PathType::MovingPlatform; + mOwnPath->pathName = "SimpleMoverPath"; + mOwnPath->pathBehavior = PathBehavior::Once; + const float distance = mMove.Length(); + const float speed = distance != 0.0f && mMoveTime != 0.0f ? distance / mMoveTime : 0.0f; + for (int32_t index = 0; index < 2; index++) { + PathWaypoint waypoint; + waypoint.position = GetWaypointPosition(index); + waypoint.rotation = mStartRotation; + waypoint.speed = speed; + mOwnPath->pathWaypoints.push_back(waypoint); + } + mOwnPath->waypointCount = 2; + mPath = mOwnPath.get(); +} + std::vector ChooseSubComponentTypes(const int32_t componentID, const Entity& entity) { const bool setRotater = entity.HasVar(u"platformIsRotater"); const bool setMover = entity.HasVar(u"platformIsMover"); @@ -106,8 +130,7 @@ MovingPlatformComponent::MovingPlatformComponent(Entity* parent, const int32_t c m_NoAutoStart = false; } -// The subcomponents (LWOMovingPlatformComponent::SetupPlatform: one per type, in the client's order), and a simple -// mover's MovingPlatforms row +// The subcomponents (one per type, in the client's order), and a simple mover's MovingPlatforms row void MovingPlatformComponent::LoadDataFromTemplate() { for (const auto type : ChooseSubComponentTypes(m_ComponentID, *m_Parent)) { if (type == eMoverSubComponentType::simpleMover) { @@ -115,7 +138,7 @@ void MovingPlatformComponent::LoadDataFromTemplate() { simpleMover->mStartPosition = m_Parent->GetDefaultPosition(); simpleMover->mStartRotation = m_Parent->GetDefaultRotation(); - // LWOPlatformSimpleMover::LoadTemplateData: the MovingPlatforms row, unless the object sets dbonly false + // 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( @@ -140,37 +163,28 @@ void MovingPlatformComponent::LoadDataFromTemplate() { 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() { - 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"); +using namespace ePlatformStateFlag; - // LWOPlatformSimpleMover::LoadConfigData: the object's settings over the table's, unless it is db only - if (m_Parent->HasVar(u"attached_path_start")) simpleMover->mStartAtEnd = m_Parent->GetVar(u"attached_path_start") != 0; - if (m_Parent->HasVar(u"platformStartAtEnd")) simpleMover->mStartAtEnd = m_Parent->GetVar(u"platformStartAtEnd"); - if (!dbOnly) { - if (m_Parent->HasVar(u"platformMoveX")) simpleMover->mMove.x = m_Parent->GetVar(u"platformMoveX"); - if (m_Parent->HasVar(u"platformMoveY")) simpleMover->mMove.y = m_Parent->GetVar(u"platformMoveY"); - if (m_Parent->HasVar(u"platformMoveZ")) simpleMover->mMove.z = m_Parent->GetVar(u"platformMoveZ"); - if (m_Parent->HasVar(u"platformMoveTime")) simpleMover->mMoveTime = m_Parent->GetVar(u"platformMoveTime"); - } - simpleMover->mCurrentWaypointIndex = simpleMover->mStartAtEnd ? 1 : 0; - - // As a mover on its path (LWOPlatform::ResumeTravelling and SetWaitingTime): unless - // startPathingOnLoad is false it waits at its waypoint (9), for the shortest wait as its waypoints have none. - // Live's scripts for the NJ spinners, which are simple movers that only move when used, stop them when they - // start up (L_NJ_SPINNER_SERVER: StopPathing), and the simple movers that wait for an event without such a script - // have startPathingOnLoad 0. - const bool startOnLoad = !m_Parent->HasVar(u"startPathingOnLoad") || m_Parent->GetVar(u"startPathingOnLoad"); - if (startOnLoad) { - simpleMover->mState = ePlatformStateFlag::Waiting | ePlatformStateFlag::ReachedDesiredWaypoint; - simpleMover->mTimeToWait = 0.1f; - simpleMover->mWaitSet = true; - } +namespace { + void StartTravelling(PlatformSubComponent& platform) { + platform.mState = (platform.mState & ~(Waiting | Stopped)) | Travelling; } + void StopTravelling(PlatformSubComponent& platform) { + platform.mState = (platform.mState & ~(Waiting | Travelling)) | Stopped; + } + + void StartWaiting(PlatformSubComponent& platform) { + platform.mState = (platform.mState & ~(Travelling | Stopped)) | Waiting; + } + + bool IsSimpleMover(const PlatformSubComponent& platform) { + return platform.GetType() == eMoverSubComponentType::simpleMover; + } +} + +// The level config: the path, a simple mover's settings, and where and whether each platform starts +void MovingPlatformComponent::LoadConfigData() { const auto pathName = GeneralUtils::UTF16ToWTF8(m_PathName); m_Path = Game::zoneManager->GetZone()->GetPath(pathName); @@ -178,14 +192,14 @@ void MovingPlatformComponent::LoadConfigData() { LOG("Path not found: %s", pathName.c_str()); } - if (IsMover()) { - const auto start = m_Parent->HasVar(u"attached_path_start") ? m_Parent->GetVar(u"attached_path_start") : 0; - m_StartWaypoint = start >= 0 && static_cast(start) < m_Path->pathWaypoints.size() ? start : 0; - SetupMoverPath(); - } else if (m_MoverSubComponentType == eMoverSubComponentType::rotater) { - const auto start = m_Parent->HasVar(u"attached_path_start") ? m_Parent->GetVar(u"attached_path_start") : 0; - m_StartWaypoint = start >= 0 ? start : 0; - SetupRotator(); + const auto start = m_Parent->HasVar(u"attached_path_start") ? m_Parent->GetVar(u"attached_path_start") : 0; + m_StartWaypoint = start >= 0 ? start : 0; + if (m_Path != nullptr && static_cast(m_StartWaypoint) >= m_Path->pathWaypoints.size()) m_StartWaypoint = 0; + + for (const auto& subComponent : m_SubComponents) { + if (IsSimpleMover(*subComponent)) SetupSimpleMover(static_cast(*subComponent)); + else if (subComponent->GetType() == eMoverSubComponentType::rotater) SetupRotator(static_cast(*subComponent)); + else SetupMoverPath(*subComponent); } // Live constructed every moving platform with simple physics as keyframed @@ -193,67 +207,91 @@ void MovingPlatformComponent::LoadConfigData() { if (simplePhysics && !m_Parent->HasVar(u"motionType")) simplePhysics->SetPhysicsMotionState(SimplePhysicsComponent::MOTION_TYPE_KEYFRAMED); } -// LWOPlatform::SetupPath: a mover is at its starting waypoint, stopped (12, as live constructed movers nothing -// had started). Unless startPathingOnLoad is false it then waits the waypoint's wait and leaves (live: 9). -void MovingPlatformComponent::SetupMoverPath() { - auto* const mover = GetMoverSubComponent(); - mover->mTimeBased = m_Path->movingPlatform.timeBasedMovement != 0; - mover->mInReverse = m_PathReverse; - MoverProcessStateChange(m_StartWaypoint); - const bool startOnLoad = !m_Parent->HasVar(u"startPathingOnLoad") || m_Parent->GetVar(u"startPathingOnLoad"); - if (startOnLoad) MoverResumeTravelling(); +bool MovingPlatformComponent::StartsOnLoad() const { + return !m_Parent->HasVar(u"startPathingOnLoad") || m_Parent->GetVar(u"startPathingOnLoad"); } -// LWOPlatformRotator: on its path it only turns (rotX/rotY/rotZ at rate; its HandleStoppedLogic only keeps -// the angular velocity, so it never arrives anywhere), while travelling. Only one on a path runs -// (LWOMovingPlatformComponent::SetupPlatform), and the client reads a platform that is not at a waypoint -// from its path (LWOPlatform::Deserialize), so one without a path stays as it was made (12). The client's -// rotator starts turning when it is sent travelling (Deserialize: StartTravelling, ResumeTravelling, UpdateVelocities). -void MovingPlatformComponent::SetupRotator() { - auto* const rotator = static_cast(GetMoverSubComponent()); - // LWOPlatformRotator::LoadConfigData +// The object's settings go over the table's, unless it is db only. The simple mover then gets its own path, between +// its two ends, and is at the end it starts at: a platform like any other on a path run once. Made stopped at its +// waypoint (12), it waits there (9, as live constructed the NJ counterweights) unless startPathingOnLoad is false, +// for the shortest wait as its waypoints have none, and sets off for its other end. Live's scripts for the NJ +// spinners, which are simple movers that only move when used, stop them when they start up, and the simple movers +// that wait for an event without such a script have startPathingOnLoad 0. +void MovingPlatformComponent::SetupSimpleMover(SimpleMoverSubComponent& simpleMover) { + const bool dbOnly = m_Parent->GetVar(u"dbonly"); + if (m_Parent->HasVar(u"attached_path_start")) simpleMover.mStartAtEnd = m_Parent->GetVar(u"attached_path_start") != 0; + if (m_Parent->HasVar(u"platformStartAtEnd")) simpleMover.mStartAtEnd = m_Parent->GetVar(u"platformStartAtEnd"); + if (!dbOnly) { + if (m_Parent->HasVar(u"platformMoveX")) simpleMover.mMove.x = m_Parent->GetVar(u"platformMoveX"); + if (m_Parent->HasVar(u"platformMoveY")) simpleMover.mMove.y = m_Parent->GetVar(u"platformMoveY"); + if (m_Parent->HasVar(u"platformMoveZ")) simpleMover.mMove.z = m_Parent->GetVar(u"platformMoveZ"); + if (m_Parent->HasVar(u"platformMoveTime")) simpleMover.mMoveTime = m_Parent->GetVar(u"platformMoveTime"); + } + + simpleMover.GeneratePath(); + // One that starts at its end goes back from there: the client takes the direction it is constructed with + simpleMover.mInReverse = simpleMover.mStartAtEnd; + PlatformProcessStateChange(simpleMover, simpleMover.mStartAtEnd ? 1 : 0); + if (StartsOnLoad()) PlatformResumeTravelling(simpleMover, false); +} + +// A mover is at its starting waypoint, stopped (12, as live constructed movers nothing had started). Unless +// startPathingOnLoad is false it then waits the waypoint's wait and leaves (live: 9). +void MovingPlatformComponent::SetupMoverPath(PlatformSubComponent& mover) { + mover.mPath = m_Path != nullptr && !m_Path->pathWaypoints.empty() ? m_Path : nullptr; + if (mover.mPath == nullptr) return; + mover.mTimeBased = m_Path->movingPlatform.timeBasedMovement != 0; + mover.mInReverse = m_PathReverse; + PlatformProcessStateChange(mover, m_StartWaypoint); + if (StartsOnLoad()) PlatformResumeTravelling(mover, false); +} + +// A rotator does not move along its path: it only turns (rotX/rotY/rotZ in half turns, at rate) while travelling, and +// never arrives anywhere. Only one on a path runs, and the client reads a platform that is not at a waypoint from its +// path, so one without a path stays as it was made (12). The client's rotator starts turning when it is sent +// travelling. +void MovingPlatformComponent::SetupRotator(RotatorSubComponent& rotator) { constexpr float pi = 3.14159265358979323846f; - if (m_Parent->HasVar(u"rotX")) rotator->mRotationX = m_Parent->GetVarAs(u"rotX") * pi; - if (m_Parent->HasVar(u"rotY")) rotator->mRotationY = m_Parent->GetVarAs(u"rotY") * pi; - if (m_Parent->HasVar(u"rotZ")) rotator->mRotationZ = m_Parent->GetVarAs(u"rotZ") * pi; - rotator->mRate = m_Parent->GetVarAs(u"rate"); + if (m_Parent->HasVar(u"rotX")) rotator.mRotationX = m_Parent->GetVarAs(u"rotX") * pi; + if (m_Parent->HasVar(u"rotY")) rotator.mRotationY = m_Parent->GetVarAs(u"rotY") * pi; + if (m_Parent->HasVar(u"rotZ")) rotator.mRotationZ = m_Parent->GetVarAs(u"rotZ") * pi; + rotator.mRate = m_Parent->GetVarAs(u"rate"); - if (m_Path == nullptr || m_Path->pathWaypoints.empty()) return; - // LWOPlatform::SetupPath - rotator->mTimeBased = m_Path->movingPlatform.timeBasedMovement != 0; - rotator->mInReverse = m_PathReverse; - if (static_cast(m_StartWaypoint) >= m_Path->pathWaypoints.size()) m_StartWaypoint = 0; - MoverProcessStateChange(m_StartWaypoint); - const bool startOnLoad = !m_Parent->HasVar(u"startPathingOnLoad") || m_Parent->GetVar(u"startPathingOnLoad"); - if (startOnLoad) rotator->mState = ePlatformStateFlag::Travelling; + rotator.mPath = m_Path != nullptr && !m_Path->pathWaypoints.empty() ? m_Path : nullptr; + if (rotator.mPath == nullptr) return; + rotator.mTimeBased = m_Path->movingPlatform.timeBasedMovement != 0; + rotator.mInReverse = m_PathReverse; + PlatformProcessStateChange(rotator, m_StartWaypoint); + if (StartsOnLoad()) rotator.mState = Travelling; } -// LWOMovingPlatformComponent::SetCurrentPath (the SetCurrentPath game message): the path, the -// waypoint it starts at and its direction, then SetupPlatform makes the subcomponents anew and a mover takes -// the path. Live wrote the path to the clients with the mover (the GF shooting gallery's targets), whose -// LWOMovingPlatformComponent::Deserialize does the same there. +// The SetCurrentPath game message: the path, the waypoint it starts at and its direction, then the client makes its +// subcomponents anew and a mover takes the path. Live wrote the path to the clients with the mover (the GF shooting +// gallery's targets), which do the same there. A simple mover keeps its own path. bool MovingPlatformComponent::SetCurrentPath(const std::string& pathName, const uint32_t startPoint, const bool reverse) { m_PathName = GeneralUtils::ASCIIToUTF16(pathName); m_Path = Game::zoneManager->GetZone()->GetPath(pathName); m_PathReverse = reverse; m_DirtyPath = true; - m_PathingStopped = false; + // Whatever is running a platform of before now stops looking at it + m_SetupGeneration++; + // The new platforms of a quick build run once it is built + m_CheckedQuickBuild = false; + m_StartWaypoint = m_Path != nullptr && static_cast(startPoint) >= m_Path->pathWaypoints.size() ? 0 : startPoint; for (auto& subComponent : m_SubComponents) { - if (subComponent->GetType() == eMoverSubComponentType::mover) subComponent = std::make_unique(m_Parent->GetPosition()); - else if (subComponent->GetType() == eMoverSubComponentType::rotater) subComponent = std::make_unique(m_Parent->GetPosition()); + if (subComponent->GetType() == eMoverSubComponentType::mover) { + subComponent = std::make_unique(m_Parent->GetPosition()); + SetupMoverPath(*subComponent); + } else if (subComponent->GetType() == eMoverSubComponentType::rotater) { + subComponent = std::make_unique(m_Parent->GetPosition()); + SetupRotator(static_cast(*subComponent)); + } } const bool mover = IsMover(); - if (mover) { - m_StartWaypoint = static_cast(startPoint) < m_Path->pathWaypoints.size() ? startPoint : 0; - SetupMoverPath(); - MoveObject(); - } else { - m_StartWaypoint = startPoint; - if (m_Path == nullptr) LOG("Cannot setup platform on path %s", pathName.c_str()); - else if (m_MoverSubComponentType == eMoverSubComponentType::rotater) SetupRotator(); - } + if (mover) MoveObject(*GetMoverSubComponent()); + else if (m_Path == nullptr) LOG("Cannot setup platform on path %s", pathName.c_str()); Game::entityManager->SerializeEntity(m_Parent); return mover; } @@ -278,9 +316,9 @@ void MovingPlatformComponent::Serialize(RakNet::BitStream& outBitStream, bool bI return; } - // LWOMovingPlatformComponent::Deserialize: whether subcomponents follow, whether the path changed (then whether - // there is one: its name, starting waypoint and reverse), then the subcomponents. Live wrote the path with the - // construction and the mover's state with every serialization; a path set later (SetCurrentPath) is written once. + // Whether subcomponents follow, whether the path changed (then whether there is one: its name, starting waypoint + // and reverse), then the subcomponents. Live wrote the path with the construction and the mover's state with every + // serialization; a path set later (SetCurrentPath) is written once. outBitStream.Write(true); const bool writePath = bIsInitialUpdate || m_DirtyPath; @@ -299,7 +337,7 @@ void MovingPlatformComponent::Serialize(RakNet::BitStream& outBitStream, bool bI } } - // The subcomponents, each after a 1 bit with its type (a rotater sends a mover's data, both LWOPlatform's); a 0 bit + // The subcomponents, each after a 1 bit with its type (a rotater sends a mover's data); 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(); @@ -310,8 +348,7 @@ 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: scripts - // send it on + // 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; @@ -323,9 +360,8 @@ void MovingPlatformComponent::OnCompleteQuickBuild() { if (m_MoverSubComponentType == eMoverSubComponentType::simpleMover) { // 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. It is the client's LWOPlatform at work: active once its quick build - // is complete (SendMessage), it waits the shortest wait (SetWaitingTime) and sets off - // (RunPlatform): RunSimpleMover. + // 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; } @@ -336,145 +372,149 @@ void MovingPlatformComponent::OnCompleteQuickBuild() { StartPathing(); } -void MovingPlatformComponent::SetMovementState(eMovementPlatformState value) { - if (m_MoverSubComponentType == eMoverSubComponentType::simpleMover) { - auto* const simpleMover = GetSimpleMoverSubComponent(); - simpleMover->mState = static_cast(value); - simpleMover->mDirtyState = true; - Game::entityManager->SerializeEntity(m_Parent); - return; - } - if (!IsMover()) return; - GetMoverSubComponent()->mState = static_cast(value); +// A serialization with the platform's state in it: a simple mover only writes its state when it changed +void MovingPlatformComponent::SerializePlatform(PlatformSubComponent& platform) { + if (IsSimpleMover(platform)) static_cast(platform).mDirtyState = true; Game::entityManager->SerializeEntity(m_Parent); } -// Sends the mover to a waypoint, stopping there when stopAtWaypoint: it turns round if the waypoint is behind it. Live +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); +} + +void MovingPlatformComponent::GotoWaypoint(uint32_t index, bool stopAtWaypoint) { + if (m_SubComponents.empty()) return; + PlatformGotoWaypoint(*m_SubComponents.front(), index, stopAtWaypoint); +} + +// Sends a platform to a waypoint, stopping there when stopAtWaypoint: it turns round if the waypoint is behind it. Live // (the NJ falling tiles): a mover at a waypoint first waits there (9, or 25 at an end, with the desired waypoint), then // leaves (2). -void MovingPlatformComponent::GotoWaypoint(uint32_t index, bool stopAtWaypoint) { - if (m_MoverSubComponentType == eMoverSubComponentType::simpleMover) { - SimpleMoverGotoWaypoint(static_cast(index)); - return; - } - if (!IsMover() || index >= m_Path->pathWaypoints.size()) return; - auto* const mover = GetMoverSubComponent(); - const bool atWaypoint = (mover->mState & ePlatformStateFlag::ReachedDesiredWaypoint) != 0; - if (atWaypoint && mover->mCurrentWaypointIndex == index) { - // Already there - mover->mDesiredWaypointIndex = -1; - mover->mShouldStopAtDesiredWaypoint = false; +void MovingPlatformComponent::PlatformGotoWaypoint(PlatformSubComponent& platform, const uint32_t index, const bool stopAtWaypoint) { + if (platform.GetType() == eMoverSubComponentType::rotater) return; + if (platform.mPath == nullptr || index >= platform.mPath->pathWaypoints.size()) return; + const auto& waypoints = platform.mPath->pathWaypoints; + const bool atWaypoint = (platform.mState & ReachedDesiredWaypoint) != 0; + if (atWaypoint && platform.mCurrentWaypointIndex == index) { + // Already there. A simple mover stays as it is. + if (IsSimpleMover(platform)) return; + platform.mDesiredWaypointIndex = -1; + platform.mShouldStopAtDesiredWaypoint = false; if (stopAtWaypoint) { - mover->mState = (mover->mState & ~(ePlatformStateFlag::Waiting | ePlatformStateFlag::Travelling)) | ePlatformStateFlag::Stopped; - mover->mVelocity = NiPoint3Constant::ZERO; + StopTravelling(platform); + platform.mVelocity = NiPoint3Constant::ZERO; } - FireArrivalHooks(index, true, index == m_Path->pathWaypoints.size() - 1); + FireArrivalHooks(index, true, index == waypoints.size() - 1); return; } - mover->mDesiredWaypointIndex = static_cast(index); - mover->mShouldStopAtDesiredWaypoint = stopAtWaypoint; + platform.mDesiredWaypointIndex = static_cast(index); + platform.mShouldStopAtDesiredWaypoint = stopAtWaypoint; - // LWOPlatform::SetMovingForwards / SetMovingBackwards: at a waypoint it sets off in the waypoint's direction; between - // two it carries on if the waypoint is ahead, else turns round (the waypoint it was heading to becomes the one it - // comes from) + // At a waypoint it sets off in the waypoint's direction. Between two a mover carries on if the waypoint is ahead, + // else turns round (the waypoint it was heading to becomes the one it comes from). if (atWaypoint) { - const bool reverse = index < mover->mCurrentWaypointIndex; - if (reverse != mover->mInReverse) { - mover->mInReverse = reverse; - MoverProcessStateChange(mover->mCurrentWaypointIndex); + const bool reverse = index < platform.mCurrentWaypointIndex; + if (reverse != platform.mInReverse) { + platform.mInReverse = reverse; + PlatformProcessStateChange(platform, platform.mCurrentWaypointIndex); } + } else if (IsSimpleMover(platform)) { + // The client's simple mover only takes a new direction at a waypoint: on its way it goes on to the end it was + // heading for, and sets off for the waypoint it was sent to once it has stopped there + return; } else { - const bool ahead = mover->mInReverse ? index <= mover->mNextWaypointIndex : index >= mover->mNextWaypointIndex; + const bool ahead = platform.mInReverse ? index <= platform.mNextWaypointIndex : index >= platform.mNextWaypointIndex; if (!ahead) { - const float percent = mover->mPercentBetweenPoints; - const auto heading = mover->mNextWaypointIndex; - mover->mInReverse = !mover->mInReverse; - mover->mNextWaypointIndex = mover->mCurrentWaypointIndex; - mover->mCurrentWaypointIndex = heading; - mover->mPercentBetweenPoints = 1.0f - std::min(percent, 1.0f); - mover->mSegmentLength = Vector3::Distance(m_Path->pathWaypoints[mover->mCurrentWaypointIndex].position, m_Path->pathWaypoints[mover->mNextWaypointIndex].position); - mover->mWaitTime = m_Path->pathWaypoints[mover->mCurrentWaypointIndex].movingPlatform.wait; - if (mover->mTimeBased) mover->mMoveTimeElapsed = mover->mCurrentSpeed * mover->mPercentBetweenPoints; - else std::swap(mover->mCurrentSpeed, mover->mNextSpeed); + const float percent = platform.mPercentBetweenPoints; + const auto heading = platform.mNextWaypointIndex; + platform.mInReverse = !platform.mInReverse; + platform.mNextWaypointIndex = platform.mCurrentWaypointIndex; + platform.mCurrentWaypointIndex = heading; + platform.mPercentBetweenPoints = 1.0f - std::min(percent, 1.0f); + platform.mSegmentLength = Vector3::Distance(waypoints[platform.mCurrentWaypointIndex].position, waypoints[platform.mNextWaypointIndex].position); + platform.mWaitTime = waypoints[platform.mCurrentWaypointIndex].movingPlatform.wait; + if (platform.mTimeBased) platform.mMoveTimeElapsed = platform.mCurrentSpeed * platform.mPercentBetweenPoints; + else std::swap(platform.mCurrentSpeed, platform.mNextSpeed); } } - MoverResumeTravelling(); - Game::entityManager->SerializeEntity(m_Parent); + PlatformResumeTravelling(platform, false); + SerializePlatform(platform); } void MovingPlatformComponent::StartPathing() { - if (m_MoverSubComponentType == eMoverSubComponentType::simpleMover) { - // A simple mover's path is start to end: starting sends it to the other end - SimpleMoverGotoWaypoint(GetSimpleMoverSubComponent()->mCurrentWaypointIndex == 0 ? 1 : 0); - return; - } - if (m_MoverSubComponentType == eMoverSubComponentType::rotater) { - // LWOPlatformRotator::StartLinearMovement: spinning again (only on a path, as above) - if (m_Path == nullptr) return; - GetMoverSubComponent()->mState = ePlatformStateFlag::Travelling; + if (m_SubComponents.empty()) return; + auto& platform = *m_SubComponents.front(); + 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 (!IsMover()) return; - m_PathingStopped = false; - auto* const mover = GetMoverSubComponent(); - if ((mover->mState & ePlatformStateFlag::Travelling) != 0) return; - // LWOPlatform::ResumeTravelling: 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) - MoverResumeTravelling(); - Game::entityManager->SerializeEntity(m_Parent); + 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 + // it sets off for the other. + PlatformResumeTravelling(platform, false); + SerializePlatform(platform); } void MovingPlatformComponent::StopPathing() { - if (m_MoverSubComponentType == eMoverSubComponentType::simpleMover) { - // LWOPlatformSimpleMover::StopPlatform: at an end it is stopped; on its way it goes on to the end it - // was heading for, where its path (run once) stops it anyway - auto* const simpleMover = GetSimpleMoverSubComponent(); - if ((simpleMover->mState & ePlatformStateFlag::ReachedDesiredWaypoint) == 0) return; - simpleMover->mState = (simpleMover->mState & ~(ePlatformStateFlag::Waiting | ePlatformStateFlag::Travelling)) | ePlatformStateFlag::Stopped; - simpleMover->mDirtyState = true; - Game::entityManager->SerializeEntity(m_Parent); + if (m_SubComponents.empty()) return; + auto& platform = *m_SubComponents.front(); + 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) + if ((platform.mState & ReachedDesiredWaypoint) == 0) { + platform.mDesiredWaypointIndex = static_cast(platform.mNextWaypointIndex); + platform.mShouldStopAtDesiredWaypoint = true; + return; + } + StopTravelling(platform); + platform.mVelocity = NiPoint3Constant::ZERO; + SerializePlatform(platform); return; } - if (m_MoverSubComponentType == eMoverSubComponentType::rotater) { - // LWOPlatformRotator::StopPlatform: no longer spinning - auto* const rotator = GetMoverSubComponent(); - rotator->mState = (rotator->mState & ~(ePlatformStateFlag::Waiting | ePlatformStateFlag::Travelling)) | ePlatformStateFlag::Stopped; + if (platform.GetType() == eMoverSubComponentType::rotater) { + // No longer spinning + StopTravelling(platform); Game::entityManager->SerializeEntity(m_Parent); return; } if (!IsMover()) return; - m_PathingStopped = true; - auto* const mover = GetMoverSubComponent(); - // LWOPlatform::StopPlatform: stopped where it is (the client stops on the stopped bit, 4) - mover->mState = (mover->mState & ~(ePlatformStateFlag::Waiting | ePlatformStateFlag::Travelling)) | ePlatformStateFlag::Stopped; - mover->mVelocity = NiPoint3Constant::ZERO; - mover->mDesiredWaypointIndex = -1; - mover->mShouldStopAtDesiredWaypoint = false; + // Stopped where it is (the client stops on the stopped bit, 4) + StopTravelling(platform); + platform.mVelocity = NiPoint3Constant::ZERO; + platform.mDesiredWaypointIndex = -1; + platform.mShouldStopAtDesiredWaypoint = false; Game::entityManager->SerializeEntity(m_Parent); } bool MovingPlatformComponent::IsMover() const { - return m_MoverSubComponentType == eMoverSubComponentType::mover && m_Path != nullptr && !m_Path->pathWaypoints.empty(); + return m_MoverSubComponentType == eMoverSubComponentType::mover && m_SubComponents.front()->mPath != nullptr; } -// LWOPlatform::ProcessStateChange: the waypoint becomes the current one, the next follows from the path's behavior and -// the direction (a bounce turns round at either end), with the two waypoints' speeds (GetWaypointSpeed) -void MovingPlatformComponent::MoverProcessStateChange(const uint32_t index) { - auto* const mover = GetMoverSubComponent(); - const auto& waypoints = m_Path->pathWaypoints; +// The waypoint becomes the platform's current one, where it now is. The next follows from the path's behavior and the +// direction (a bounce turns round at either end), with the two waypoints' speeds. +void MovingPlatformComponent::PlatformProcessStateChange(PlatformSubComponent& platform, const uint32_t index) { + const auto& waypoints = platform.mPath->pathWaypoints; const auto count = static_cast(waypoints.size()); - const auto behavior = static_cast(m_Path->pathBehavior); + const auto behavior = static_cast(platform.mPath->pathBehavior); const auto current = std::min(static_cast(index), count - 1); int32_t next{}; - if (!mover->mInReverse) { + if (!platform.mInReverse) { next = current + 1; if (next >= count) { if (behavior == PathBehavior::Once) next = count - 1; else if (behavior == PathBehavior::Bounce) { next = count - 2; - mover->mInReverse = true; + platform.mInReverse = true; } else next = 0; } } else { @@ -483,178 +523,191 @@ void MovingPlatformComponent::MoverProcessStateChange(const uint32_t index) { if (behavior == PathBehavior::Once) next = 0; else if (behavior == PathBehavior::Bounce) { next = 1; - mover->mInReverse = false; + platform.mInReverse = false; } else next = count - 1; } } next = std::clamp(next, 0, count - 1); - mover->mCurrentWaypointIndex = current; - mover->mNextWaypointIndex = next; - mover->mWaitTime = waypoints[current].movingPlatform.wait; - mover->mPercentBetweenPoints = 0.0f; - mover->mMoveTimeElapsed = 0.0f; - mover->mPosition = waypoints[current].position; - mover->mSegmentLength = Vector3::Distance(waypoints[current].position, waypoints[next].position); + platform.mCurrentWaypointIndex = current; + platform.mNextWaypointIndex = next; + platform.mWaitTime = waypoints[current].movingPlatform.wait; + platform.mPercentBetweenPoints = 0.0f; + platform.mMoveTimeElapsed = 0.0f; + platform.mPosition = waypoints[current].position; + platform.mSegmentLength = Vector3::Distance(waypoints[current].position, waypoints[next].position); if (current == next) { - mover->mCurrentSpeed = mover->mNextSpeed = 0.0f; - } else if (mover->mTimeBased) { - mover->mCurrentSpeed = waypoints[std::min(current, next)].speed; - mover->mNextSpeed = 0.0f; + platform.mCurrentSpeed = platform.mNextSpeed = 0.0f; + } else if (platform.mTimeBased) { + platform.mCurrentSpeed = waypoints[std::min(current, next)].speed; + platform.mNextSpeed = 0.0f; } else { - mover->mCurrentSpeed = waypoints[current].speed; - mover->mNextSpeed = waypoints[next].speed; + platform.mCurrentSpeed = waypoints[current].speed; + platform.mNextSpeed = waypoints[next].speed; } } -void MovingPlatformComponent::MoverSetWaitingTime(const float waitTime, const float idleTimeElapsed) { - auto* const mover = GetMoverSubComponent(); - mover->mTimeToWait = std::max(0.1f, waitTime); - mover->mIdleTimeElapsed = idleTimeElapsed; - mover->mWaitSet = true; +// Starts a wait of at least 0.1 s +void MovingPlatformComponent::PlatformSetWaitingTime(PlatformSubComponent& platform, const float waitTime, const float idleTimeElapsed) { + platform.mTimeToWait = std::max(0.1f, waitTime); + platform.mIdleTimeElapsed = idleTimeElapsed; + platform.mWaitSet = true; // Only at an end of the path, or stopped, does the object lose its velocities here. Waiting at a waypoint in between // it keeps them: live's platforms went on as they came for the wait (the GF shooting gallery subs 2 units past each // waypoint, the FV dragons 5.33; a dragon met 2.2 s into a 5 s wait was 88 past its waypoint). - if ((mover->mState & (ePlatformStateFlag::ReachedFinalDestination | ePlatformStateFlag::Stopped)) != 0) mover->mVelocity = NiPoint3Constant::ZERO; + if ((platform.mState & (ReachedFinalDestination | Stopped)) != 0) platform.mVelocity = NiPoint3Constant::ZERO; } -void MovingPlatformComponent::MoverUpdateVelocities() { - auto* const mover = GetMoverSubComponent(); - auto direction = m_Path->pathWaypoints[mover->mNextWaypointIndex].position - m_Path->pathWaypoints[mover->mCurrentWaypointIndex].position; +// The velocity towards the next waypoint at the speed for where the platform is; setting off drops the two "reached" +// bits of its state +void MovingPlatformComponent::PlatformUpdateVelocities(PlatformSubComponent& platform) { + const auto& waypoints = platform.mPath->pathWaypoints; + auto direction = waypoints[platform.mNextWaypointIndex].position - waypoints[platform.mCurrentWaypointIndex].position; const float distance = direction.Length(); if (distance > 0.0f) direction = direction / distance; - // LWOPlatform::GetSpeed: a time based path's mCurrentSpeed is the seconds to the next waypoint; else from - // the waypoint's speed to the next one's along the way + // A time based path's mCurrentSpeed is the seconds to the next waypoint; else the speed goes from the waypoint's to + // the next one's along the way float speed{}; - if (mover->mTimeBased) speed = mover->mCurrentSpeed > 0.0f ? distance / mover->mCurrentSpeed : 0.0f; - else speed = mover->mCurrentSpeed + (mover->mNextSpeed - mover->mCurrentSpeed) * mover->mPercentBetweenPoints; - mover->mVelocity = direction * speed; - mover->mState &= ePlatformStateFlag::MovementMask; + if (platform.mTimeBased) speed = platform.mCurrentSpeed > 0.0f ? distance / platform.mCurrentSpeed : 0.0f; + else speed = platform.mCurrentSpeed + (platform.mNextSpeed - platform.mCurrentSpeed) * platform.mPercentBetweenPoints; + platform.mVelocity = direction * speed; + platform.mState &= MovementMask; } -void MovingPlatformComponent::MoverResumeTravelling() { - auto* const mover = GetMoverSubComponent(); - if ((mover->mState & ePlatformStateFlag::Stopped) != 0 && (mover->mState & ePlatformStateFlag::ReachedDesiredWaypoint) == 0) { - // LWOPlatform::StartTravelling - mover->mState = (mover->mState & ~(ePlatformStateFlag::Waiting | ePlatformStateFlag::Stopped)) | ePlatformStateFlag::Travelling; - } - if ((mover->mState & ePlatformStateFlag::Travelling) != 0) { - MoverUpdateVelocities(); +// Stopped between two waypoints the platform travels on; at a waypoint it first waits there. A simple mover only does +// either at a waypoint or stopped, unless forced. +void MovingPlatformComponent::PlatformResumeTravelling(PlatformSubComponent& platform, const bool force) { + if (IsSimpleMover(platform) && !force && (platform.mState & (ReachedDesiredWaypoint | Stopped)) == 0) return; + if ((platform.mState & Stopped) != 0 && (platform.mState & ReachedDesiredWaypoint) == 0) StartTravelling(platform); + if ((platform.mState & Travelling) != 0) { + PlatformUpdateVelocities(platform); } else { - mover->mState = (mover->mState & ~(ePlatformStateFlag::Travelling | ePlatformStateFlag::Stopped)) | ePlatformStateFlag::Waiting; - MoverSetWaitingTime(mover->mWaitTime, 0.0f); + StartWaiting(platform); + PlatformSetWaitingTime(platform, platform.mWaitTime, 0.0f); } } -void MovingPlatformComponent::MoverArrivedAtWaypoint() { - auto* const mover = GetMoverSubComponent(); +// Once the platform is at its next waypoint: it is there (8), at an end of the path (16) if it is one, where a path +// run once turns it round. It stops there if it was sent there to stop or turned at the end of a path run once; else +// it travels on, after the waypoint's wait. False when a script told of it put the platform on another path. +bool MovingPlatformComponent::PlatformArrivedAtWaypoint(PlatformSubComponent& platform) { // A platform with nowhere to go (one waypoint, or at the end of a path run once) is not arriving - if (mover->mCurrentWaypointIndex == mover->mNextWaypointIndex) return; + if (platform.mCurrentWaypointIndex == platform.mNextWaypointIndex) return true; - const auto& next = m_Path->pathWaypoints[mover->mNextWaypointIndex].position; + const auto& waypoints = platform.mPath->pathWaypoints; + const auto& next = waypoints[platform.mNextWaypointIndex].position; bool closeEnough{}; - if (mover->mTimeBased) { - // LWOPlatform::CheckEnoughTimeElapsedAtWaypoint - closeEnough = std::abs(mover->mCurrentSpeed - mover->mMoveTimeElapsed) < 0.02f; + if (platform.mTimeBased) { + // The time to the next waypoint is up + closeEnough = std::abs(platform.mCurrentSpeed - platform.mMoveTimeElapsed) < 0.02f; } else { - // LWOPlatform::CloseToNextWaypoint: within 0.01 of it, or past it - const auto offset = next - mover->mPosition; - const auto direction = next - m_Path->pathWaypoints[mover->mCurrentWaypointIndex].position; + // Within 0.01 of the next waypoint, or past it + const auto offset = next - platform.mPosition; + const auto direction = next - waypoints[platform.mCurrentWaypointIndex].position; closeEnough = offset.SquaredLength() <= 0.0001f || direction.DotProduct(offset) <= 0.0f; } - if (!closeEnough) return; + if (!closeEnough) return true; - const auto arrived = mover->mNextWaypointIndex; - const bool atLastWaypoint = arrived == static_cast(m_Path->pathWaypoints.size() - 1); + const auto arrived = platform.mNextWaypointIndex; + const bool atLastWaypoint = arrived == static_cast(waypoints.size() - 1); bool atDesiredWaypoint = false; bool stopAtDesiredWaypoint = false; bool turnedAtEnd = false; - mover->mState = ePlatformStateFlag::ReachedDesiredWaypoint; - if (mover->mDesiredWaypointIndex == static_cast(arrived)) { + platform.mState = ReachedDesiredWaypoint; + if (platform.mDesiredWaypointIndex == static_cast(arrived)) { atDesiredWaypoint = true; - stopAtDesiredWaypoint = mover->mShouldStopAtDesiredWaypoint; - mover->mShouldStopAtDesiredWaypoint = false; - mover->mDesiredWaypointIndex = -1; + stopAtDesiredWaypoint = platform.mShouldStopAtDesiredWaypoint; + platform.mShouldStopAtDesiredWaypoint = false; + platform.mDesiredWaypointIndex = -1; } if (arrived == 0 || atLastWaypoint) { - mover->mState |= ePlatformStateFlag::ReachedFinalDestination; - if (static_cast(m_Path->pathBehavior) == PathBehavior::Once) { + platform.mState |= ReachedFinalDestination; + if (static_cast(platform.mPath->pathBehavior) == PathBehavior::Once) { turnedAtEnd = true; - mover->mInReverse = !mover->mInReverse; + platform.mInReverse = !platform.mInReverse; } } if (stopAtDesiredWaypoint || turnedAtEnd) { - mover->mState = (mover->mState & ~(ePlatformStateFlag::Waiting | ePlatformStateFlag::Travelling)) | ePlatformStateFlag::Stopped; - mover->mVelocity = NiPoint3Constant::ZERO; + StopTravelling(platform); + platform.mVelocity = NiPoint3Constant::ZERO; } else { - mover->mState = (mover->mState & ~(ePlatformStateFlag::Waiting | ePlatformStateFlag::Stopped)) | ePlatformStateFlag::Travelling; + StartTravelling(platform); } - MoverProcessStateChange(arrived); - MoverHandleWaypointArrived(arrived, atDesiredWaypoint, atLastWaypoint); + PlatformProcessStateChange(platform, arrived); + 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 + if (IsSimpleMover(platform) && (platform.mState & Stopped) != 0 && platform.mDesiredWaypointIndex >= 0 + && platform.mDesiredWaypointIndex != static_cast(platform.mCurrentWaypointIndex)) { + PlatformGotoWaypoint(platform, static_cast(platform.mDesiredWaypointIndex), platform.mShouldStopAtDesiredWaypoint); + } + return true; } -void MovingPlatformComponent::MoverHandleWaypointArrived(const uint32_t index, const bool atDesiredWaypoint, const bool atLastWaypoint) { +// Tells the scripts, then a platform that travels on waits at the waypoint. False when a script put the platform on +// another path. +bool MovingPlatformComponent::PlatformHandleWaypointArrived(PlatformSubComponent& platform, const uint32_t index, const bool atDesiredWaypoint, const bool atLastWaypoint) { + const auto generation = m_SetupGeneration; FireArrivalHooks(index, atDesiredWaypoint, atLastWaypoint); + if (generation != m_SetupGeneration) return false; - // The scripts may have taken the platform or its object away - auto* const mover = GetMoverSubComponent(); - if (!mover || !IsMover()) return; - if ((mover->mState & ePlatformStateFlag::Travelling) != 0) { - mover->mState = (mover->mState & ~(ePlatformStateFlag::Travelling | ePlatformStateFlag::Stopped)) | ePlatformStateFlag::Waiting; - MoverSetWaitingTime(mover->mWaitTime, 0.0f); + if ((platform.mState & Travelling) != 0) { + StartWaiting(platform); + PlatformSetWaitingTime(platform, platform.mWaitTime, 0.0f); } // Live serialized a platform that waits at the waypoint it arrived at (it leaves again), not one that stopped there - if ((mover->mState & ePlatformStateFlag::Stopped) == 0) Game::entityManager->SerializeEntity(m_Parent); + if ((platform.mState & Stopped) == 0) SerializePlatform(platform); + return true; } -// LWOPlatform moves its object with SetPosition and SetRotation ignoring the dirty flags (ProcessStateChange, -// Resync), 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)); +// 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) { + const auto& from = platform.mPath->pathWaypoints[platform.mCurrentWaypointIndex].rotation; + 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); +} + +void MovingPlatformComponent::SetObjectTransform(const NiPoint3& position, const NiQuaternion& rotation) { if (auto* const simplePhysics = m_Parent->GetComponent()) { - simplePhysics->SetPositionAndRotationIgnoringDirtyFlags(mover->mPosition, rotation); + simplePhysics->SetPositionAndRotationIgnoringDirtyFlags(position, rotation); } if (auto* const phantomPhysics = m_Parent->GetComponent()) { - phantomPhysics->SetPositionAndRotationIgnoringDirtyFlags(mover->mPosition, rotation); + phantomPhysics->SetPositionAndRotationIgnoringDirtyFlags(position, rotation); } } -// LWOPlatform::SetupPath and SendMessage (RebuildNotifyState): a quick build's platform does not -// run until it is built, nor once the quick build is open again -bool MovingPlatformComponent::UpdateActive(PlatformSubComponent& platform) { +// A quick build's platforms do not run until it is built, nor once the quick build is open again +void MovingPlatformComponent::UpdateActive() { if (!m_CheckedQuickBuild) { m_CheckedQuickBuild = true; m_QuickBuild = m_Parent->GetComponent(); - if (m_QuickBuild) platform.mActive = false; + if (m_QuickBuild) for (const auto& subComponent : m_SubComponents) subComponent->mActive = false; } - if (m_QuickBuild) { - const auto quickBuildState = m_QuickBuild->GetState(); - if (quickBuildState == eQuickBuildState::OPEN) platform.mActive = false; - else if (quickBuildState == eQuickBuildState::COMPLETED) platform.mActive = true; - } - return platform.mActive; + if (!m_QuickBuild) return; + const auto quickBuildState = m_QuickBuild->GetState(); + if (quickBuildState != eQuickBuildState::OPEN && quickBuildState != eQuickBuildState::COMPLETED) return; + for (const auto& subComponent : m_SubComponents) subComponent->mActive = quickBuildState == eQuickBuildState::COMPLETED; } NiPoint3 CalcRotatorAngularVelocity(const float rotX, const float rotY, const float rotZ, const float rate, const bool reverse) { - // NiMatrix3::MakeXRotation, MakeYRotation and MakeZRotation + // The client's rotations about x, y and z: each turns the other way round than the usual right-handed one const float sx = static_cast(std::sin(static_cast(rotX))), cx = static_cast(std::cos(static_cast(rotX))); const float sy = static_cast(std::sin(static_cast(rotY))), cy = static_cast(std::cos(static_cast(rotY))); const float sz = static_cast(std::sin(static_cast(rotZ))), cz = static_cast(std::cos(static_cast(rotZ))); const float x[3][3] = { { 1.0f, 0.0f, 0.0f }, { 0.0f, cx, sx }, { 0.0f, -sx, cx } }; const float y[3][3] = { { cy, 0.0f, -sy }, { 0.0f, 1.0f, 0.0f }, { sy, 0.0f, cy } }; const float z[3][3] = { { cz, sz, 0.0f }, { -sz, cz, 0.0f }, { 0.0f, 0.0f, 1.0f } }; - // NiMatrix3::FromEulerAnglesXYZ: x * (y * z) + // The three as one: x * (y * z) float yz[3][3]{}; float m[3][3]{}; for (int32_t i = 0; i < 3; i++) for (int32_t j = 0; j < 3; j++) for (int32_t k = 0; k < 3; k++) yz[i][j] += y[i][k] * z[k][j]; for (int32_t i = 0; i < 3; i++) for (int32_t j = 0; j < 3; j++) for (int32_t k = 0; k < 3; k++) m[i][j] += x[i][k] * yz[k][j]; - // NiQuaternion::FromRotation + // The matrix as a quaternion float q[4]{}; // x, y, z, w const float trace = m[0][0] + m[1][1] + m[2][2]; if (trace > 0.0f) { @@ -679,7 +732,7 @@ NiPoint3 CalcRotatorAngularVelocity(const float rotX, const float rotY, const fl q[k] = (m[k][i] + m[i][k]) * root; } - // hkpKeyFrameUtility::KeyFrameInfo::setUsingPositionOrientationPair: the rotation from the first to the + // The velocity that turns from one rotation to another in a time: the rotation from the first to the // second (the rotation itself, or its inverse in reverse), normalized; without an axis there is no velocity; else // the axis (the other way for a negative w) times twice the arc cosine of |w| over the time if (reverse) { @@ -697,38 +750,22 @@ NiPoint3 CalcRotatorAngularVelocity(const float rotX, const float rotY, const fl return NiPoint3(q[0] * scale, q[1] * scale, q[2] * scale); } -// LWOPlatform::RunPlatform for a simple mover waiting at its waypoint: once the wait is over it sets off for -// its other end (LWOPlatform::IncrementWaitingTime, StartTravelling). A quick build's does not -// run until it is built (LWOPlatform::SendMessage). Live: a built counterweight was serialized waiting (9) -// and about 0.1 s later travelling (2). -void MovingPlatformComponent::RunSimpleMover(const float deltaTime) { - auto* const simpleMover = GetSimpleMoverSubComponent(); - if (!UpdateActive(*simpleMover)) return; - if ((simpleMover->mState & ePlatformStateFlag::Waiting) == 0 || !simpleMover->mWaitSet) return; - simpleMover->mIdleTimeElapsed += deltaTime; - if (simpleMover->mIdleTimeElapsed < simpleMover->mTimeToWait) return; - simpleMover->mIdleTimeElapsed = 0.0f; - SimpleMoverGotoWaypoint(simpleMover->mCurrentWaypointIndex == 0 ? 1 : 0); -} - -// LWOPlatform::RunPlatform for a rotator: its wait never ends (IncrementWaitingTime) and it never -// arrives (HandleStoppedLogic keeps the object's angular velocity at the rotator's); it has no linear -// velocity (SetLinearVelocity). The client's physics turn the object at that velocity; the server turns its -// object here, without a serialization of its physics, as a mover's. -void MovingPlatformComponent::RunRotator(const float deltaTime) { - auto* const rotator = static_cast(GetMoverSubComponent()); - if (!rotator || m_Path == nullptr || m_Path->pathWaypoints.empty()) return; +// A rotator's wait never ends and it never arrives; it has no linear velocity. The client's physics turn the object at +// the rotator's angular velocity; the server turns its object here, without a serialization of its physics, as a +// mover's. +void MovingPlatformComponent::RunRotator(RotatorSubComponent& rotator, const float deltaTime) { + if (rotator.mPath == nullptr) return; auto rotation = m_Parent->GetRotation(); bool moved = false; - if (!rotator->mPlaced) { - // LWOPlatform::ProcessStateChange from SetupPath: the object is put at its waypoint - rotator->mPlaced = true; - rotation = m_Path->pathWaypoints[rotator->mCurrentWaypointIndex].rotation; + if (!rotator.mPlaced) { + // The object is put at its waypoint when the rotator is set up on its path + rotator.mPlaced = true; + rotation = rotator.mPath->pathWaypoints[rotator.mCurrentWaypointIndex].rotation; moved = true; } - if (UpdateActive(*rotator) && (rotator->mState & ePlatformStateFlag::Travelling) != 0) { - const auto velocity = rotator->GetAngularVelocity(); + if (rotator.mActive && (rotator.mState & Travelling) != 0) { + const auto velocity = rotator.GetAngularVelocity(); const float speed = velocity.Length(); if (speed > 0.0f && std::isfinite(speed)) { const glm::vec3 axis(velocity.x / speed, velocity.y / speed, velocity.z / speed); @@ -736,83 +773,72 @@ void MovingPlatformComponent::RunRotator(const float deltaTime) { moved = true; } } - if (!moved) return; - if (auto* const simplePhysics = m_Parent->GetComponent()) { - simplePhysics->SetPositionAndRotationIgnoringDirtyFlags(rotator->mPosition, rotation); - } - if (auto* const phantomPhysics = m_Parent->GetComponent()) { - phantomPhysics->SetPositionAndRotationIgnoringDirtyFlags(rotator->mPosition, rotation); - } + if (moved) SetObjectTransform(rotator.mPosition, rotation); } -// LWOPlatform::Run and RunPlatform. The client's platform is moved by its physics, at the -// velocity SetLinearVelocity gives it; the server moves it here by the same speed. -void MovingPlatformComponent::Run(const float deltaTime) { - if (m_MoverSubComponentType == eMoverSubComponentType::rotater) { - RunRotator(deltaTime); - return; +// A platform on its path. The client's is moved by its physics, at the velocity the platform gives its object; the +// server moves it here by the same speed. False when a script told of an arrival put the platform on another path. +bool MovingPlatformComponent::RunPlatform(PlatformSubComponent& platform, const float deltaTime) { + if (platform.mPath == nullptr) return true; + if (!platform.mActive) { + // An open quick build's platform loses its velocities + platform.mVelocity = NiPoint3Constant::ZERO; + return true; } - if (m_MoverSubComponentType == eMoverSubComponentType::simpleMover) { - RunSimpleMover(deltaTime); - return; - } - if (!IsMover()) return; - auto* mover = GetMoverSubComponent(); - UpdateActive(*mover); - if (!mover->mActive) { - // RebuildNotifyState: an open quick build's platform loses its velocities - mover->mVelocity = NiPoint3Constant::ZERO; - return; - } - if (mover->mState == 0) return; + if (platform.mState == 0) return true; - const bool travelling = (mover->mState & ePlatformStateFlag::Travelling) != 0; - if (mover->mTimeBased && travelling) mover->mMoveTimeElapsed += deltaTime; + const bool travelling = (platform.mState & Travelling) != 0; + if (platform.mTimeBased && travelling) platform.mMoveTimeElapsed += deltaTime; // The object's physics: it goes at its velocity, travelling or not - mover->mPosition = mover->mPosition + mover->mVelocity * deltaTime; + platform.mPosition = platform.mPosition + platform.mVelocity * deltaTime; - if (mover->mTimeBased) { - // LWOPlatform::TimeBasedCalculatePercentToNextWaypoint: mCurrentSpeed is the seconds to the next waypoint - mover->mMoveTimeElapsed = std::min(mover->mMoveTimeElapsed, mover->mCurrentSpeed); - mover->mPercentBetweenPoints = mover->mCurrentSpeed > 0.0f ? mover->mMoveTimeElapsed / mover->mCurrentSpeed : 1.0f; + if (platform.mTimeBased) { + // mCurrentSpeed is the seconds to the next waypoint + platform.mMoveTimeElapsed = std::min(platform.mMoveTimeElapsed, platform.mCurrentSpeed); + platform.mPercentBetweenPoints = platform.mCurrentSpeed > 0.0f ? platform.mMoveTimeElapsed / platform.mCurrentSpeed : 1.0f; } else { - // LWOPlatform::CalculatePercentTravelledToWaypoint: how far the object is from the current waypoint, - // over the way to the next (live sent 1.219 for a dragon that far past its waypoint) - const auto& from = m_Path->pathWaypoints[mover->mCurrentWaypointIndex].position; - mover->mPercentBetweenPoints = mover->mSegmentLength > 0.0f ? Vector3::Distance(from, mover->mPosition) / mover->mSegmentLength : 0.0f; + // How far the object is from the current waypoint, over the way to the next (live sent 1.219 for a dragon that + // far past its waypoint) + const auto& from = platform.mPath->pathWaypoints[platform.mCurrentWaypointIndex].position; + platform.mPercentBetweenPoints = platform.mSegmentLength > 0.0f ? Vector3::Distance(from, platform.mPosition) / platform.mSegmentLength : 0.0f; } - MoveObject(); + MoveObject(platform); - const bool stopped = (mover->mState & ePlatformStateFlag::Stopped) != 0; - // LWOPlatform::IncrementWaitingTime + const bool stopped = (platform.mState & Stopped) != 0; bool doneWaiting = false; - if ((mover->mState & ePlatformStateFlag::Waiting) != 0 && mover->mWaitSet) { - mover->mIdleTimeElapsed += deltaTime; - if (mover->mIdleTimeElapsed >= mover->mTimeToWait) { - mover->mIdleTimeElapsed = 0.0f; + if ((platform.mState & Waiting) != 0 && platform.mWaitSet) { + platform.mIdleTimeElapsed += deltaTime; + if (platform.mIdleTimeElapsed >= platform.mTimeToWait) { + platform.mIdleTimeElapsed = 0.0f; doneWaiting = true; } } if (doneWaiting) { - mover->mState = (mover->mState & ~(ePlatformStateFlag::Waiting | ePlatformStateFlag::Stopped)) | ePlatformStateFlag::Travelling; - } else if (!stopped && (travelling || mover->mSegmentLength > 0.0f)) { - // LWOPlatform::HandleStoppedLogic: a platform that has not stopped looks whether it arrived, also while - // it waits. Live's FV dragons, going on as they came through the 5 s wait at their waypoint 4, were at waypoint 5 - // (waiting there, then going on in the direction they came to 4 in) without ever having left 4. Where two - // waypoints are in one place the client arrives every frame of the wait; here only a travelling one arrives there. - MoverArrivedAtWaypoint(); - // A script told of the arrival may have put the platform on another path - mover = GetMoverSubComponent(); - if (!IsMover()) return; + StartTravelling(platform); + } else if (!stopped && (travelling || platform.mSegmentLength > 0.0f)) { + // A platform that has not stopped looks whether it arrived, also while it waits. Live's FV dragons, going on as + // they came through the 5 s wait at their waypoint 4, were at waypoint 5 (waiting there, then going on in the + // direction they came to 4 in) without ever having left 4. Where two waypoints are in one place the client + // arrives every frame of the wait; here only a travelling one arrives there. + if (!PlatformArrivedAtWaypoint(platform)) return false; } - if ((mover->mState & ePlatformStateFlag::Travelling) != 0 && (doneWaiting || mover->mCurrentSpeed != mover->mNextSpeed)) { - MoverUpdateVelocities(); + if ((platform.mState & Travelling) != 0 && (doneWaiting || platform.mCurrentSpeed != platform.mNextSpeed)) { + PlatformUpdateVelocities(platform); // Live serialized a platform leaving its waypoint, where it then was: as far past the waypoint as the wait took it - if (doneWaiting) Game::entityManager->SerializeEntity(m_Parent); + if (doneWaiting) SerializePlatform(platform); } - if (IsMover()) MoveObject(); + MoveObject(platform); + return true; +} + +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); } void MovingPlatformComponent::FillResync(GameMessages::PlatformResync& resync) const { @@ -827,8 +853,8 @@ void MovingPlatformComponent::FillResync(GameMessages::PlatformResync& resync) c resync.iIndex = static_cast(mover->mCurrentWaypointIndex); resync.iNextIndex = static_cast(mover->mNextWaypointIndex); resync.ptUnexpectedLocation = mover->mPosition; - const auto& from = m_Path->pathWaypoints[mover->mCurrentWaypointIndex].rotation; - const auto& to = m_Path->pathWaypoints[mover->mNextWaypointIndex].rotation; + const auto& from = mover->mPath->pathWaypoints[mover->mCurrentWaypointIndex].rotation; + const auto& to = mover->mPath->pathWaypoints[mover->mNextWaypointIndex].rotation; const auto rotation = glm::slerp(glm::quat(from.w, from.x, from.y, from.z), glm::quat(to.w, to.x, to.y, to.z), resync.fPercentBetweenPoints); resync.qUnexpectedRotation = NiQuaternion(rotation.w, rotation.x, rotation.y, rotation.z); } @@ -846,34 +872,30 @@ void MovingPlatformComponent::SetNoAutoStart(const bool value) { } void MovingPlatformComponent::WarpToWaypoint(size_t index) { - if (m_MoverSubComponentType == eMoverSubComponentType::simpleMover) { - auto* const simpleMover = GetSimpleMoverSubComponent(); - m_MoveGeneration++; - simpleMover->mCurrentWaypointIndex = index == 0 ? 0 : 1; - simpleMover->mInReverse = simpleMover->mCurrentWaypointIndex == 1; - simpleMover->mState = ePlatformStateFlag::Stopped | ePlatformStateFlag::ReachedDesiredWaypoint | ePlatformStateFlag::ReachedFinalDestination; - simpleMover->mDirtyState = true; - m_Parent->SetPosition(simpleMover->GetWaypointPosition(simpleMover->mCurrentWaypointIndex)); - Game::entityManager->SerializeEntity(m_Parent); - return; + if (m_SubComponents.empty()) return; + auto& platform = *m_SubComponents.front(); + if (platform.GetType() == eMoverSubComponentType::rotater || platform.mPath == nullptr) return; + const auto& waypoints = platform.mPath->pathWaypoints; + if (IsSimpleMover(platform)) { + // Either end: at its end it goes back from there + index = index == 0 ? 0 : 1; + platform.mInReverse = index == 1; } - if (!IsMover() || index >= m_Path->pathWaypoints.size()) return; - const auto& waypoint = m_Path->pathWaypoints[index]; - MoverProcessStateChange(static_cast(index)); - auto* const mover = GetMoverSubComponent(); - const bool atEnd = index == 0 || index == m_Path->pathWaypoints.size() - 1; - mover->mState = ePlatformStateFlag::Stopped | ePlatformStateFlag::ReachedDesiredWaypoint | (atEnd ? ePlatformStateFlag::ReachedFinalDestination : 0); - mover->mVelocity = NiPoint3Constant::ZERO; + if (index >= waypoints.size()) return; + PlatformProcessStateChange(platform, static_cast(index)); + const bool atEnd = index == 0 || index == waypoints.size() - 1; + platform.mState = Stopped | ReachedDesiredWaypoint | (atEnd ? ReachedFinalDestination : 0); + platform.mVelocity = NiPoint3Constant::ZERO; - m_Parent->SetPosition(waypoint.position); - m_Parent->SetRotation(waypoint.rotation); + m_Parent->SetPosition(waypoints[index].position); + m_Parent->SetRotation(waypoints[index].rotation); - Game::entityManager->SerializeEntity(m_Parent); + SerializePlatform(platform); } size_t MovingPlatformComponent::GetLastWaypointIndex() const { - if (m_MoverSubComponentType == eMoverSubComponentType::simpleMover) return 1; - return m_Path->pathWaypoints.size() - 1; + if (m_SubComponents.empty() || m_SubComponents.front()->mPath == nullptr) return 0; + return m_SubComponents.front()->mPath->pathWaypoints.size() - 1; } PlatformSubComponent* MovingPlatformComponent::GetMoverSubComponent() const { @@ -886,49 +908,8 @@ SimpleMoverSubComponent* MovingPlatformComponent::GetSimpleMoverSubComponent() c return static_cast(m_SubComponents.front().get()); } -// The client runs the two point path at |platformMove| / platformMoveTime, so it takes platformMoveTime seconds from -// either end. Sending it travelling from its current waypoint towards the other one is all the client needs. -void MovingPlatformComponent::SimpleMoverGotoWaypoint(int32_t index) { - auto* const simpleMover = GetSimpleMoverSubComponent(); - index = index <= 0 ? 0 : 1; - const bool travelling = (simpleMover->mState & ePlatformStateFlag::Travelling) != 0; - if (!travelling && index == simpleMover->mCurrentWaypointIndex) return; - - simpleMover->mCurrentWaypointIndex = index == 1 ? 0 : 1; - simpleMover->mInReverse = index == 0; - simpleMover->mState = ePlatformStateFlag::Travelling; - simpleMover->mDirtyState = true; - Game::entityManager->SerializeEntity(m_Parent); - - const auto generation = ++m_MoveGeneration; - m_Parent->AddCallbackTimer(simpleMover->mMoveTime, [this, generation, index] { - if (generation == m_MoveGeneration) OnSimpleMoverArrived(index); - }); -} - -void MovingPlatformComponent::OnSimpleMoverArrived(const int32_t index) { - auto* const simpleMover = GetSimpleMoverSubComponent(); - if (!simpleMover) return; - - // LWOPlatform::ArrivedAtWaypoint: both ends are the path's ends, and a path run once stops there (28) and turns - // round. Live did not serialize it (each client's simple mover arrives by itself), and the object is moved ignoring - // the dirty flags (ProcessStateChange). - simpleMover->mCurrentWaypointIndex = index; - simpleMover->mState = ePlatformStateFlag::Stopped | ePlatformStateFlag::ReachedDesiredWaypoint | ePlatformStateFlag::ReachedFinalDestination; - simpleMover->mInReverse = index == 1; - const auto position = simpleMover->GetWaypointPosition(index); - if (auto* const simplePhysics = m_Parent->GetComponent()) { - simplePhysics->SetPositionAndRotationIgnoringDirtyFlags(position, simpleMover->mStartRotation); - } - if (auto* const phantomPhysics = m_Parent->GetComponent()) { - phantomPhysics->SetPositionAndRotationIgnoringDirtyFlags(position, simpleMover->mStartRotation); - } - - FireArrivalHooks(index, true, index == 1); -} - -// LWOPlatform::HandleWaypointArrived: Arrived, then ArrivedAtDesiredWaypoint at the waypoint it was sent to, then -// PlatformAtLastWaypoint at either end of the path +// The client's arrival messages: Arrived, then ArrivedAtDesiredWaypoint at the waypoint it was sent to, then +// PlatformAtLastWaypoint at the last waypoint of the path void MovingPlatformComponent::FireArrivalHooks(const uint32_t index, const bool atDesiredWaypoint, const bool atLastWaypoint) { auto* const script = m_Parent->GetScript(); script->OnWaypointReached(m_Parent, index); diff --git a/dGame/dComponents/MovingPlatformComponent.h b/dGame/dComponents/MovingPlatformComponent.h index adb98bfd7..d8a0d2cdd 100644 --- a/dGame/dComponents/MovingPlatformComponent.h +++ b/dGame/dComponents/MovingPlatformComponent.h @@ -23,7 +23,7 @@ struct ObjectLoadState; -class Path; +struct Path; namespace GameMessages { struct PlatformResync; } /** @@ -49,7 +49,7 @@ enum class eMoverSubComponentType : uint32_t { }; /** - * The client's platform state flags (LWOPlatform), as live sent them: a platform that was never started is stopped at + * The client's platform state flags, as live sent them: a platform that was never started is stopped at * its waypoint (12), one waiting out a waypoint's wait is 9 (25 at either end of its path), one on its way is 2 and * one that stopped at an end of a path run once is 28. */ @@ -59,12 +59,12 @@ namespace ePlatformStateFlag { constexpr uint32_t Stopped = 4; constexpr uint32_t ReachedDesiredWaypoint = 8; constexpr uint32_t ReachedFinalDestination = 16; - // What LWOPlatform::UpdateVelocities keeps when the platform sets off (it drops the two "reached" bits) + // What a platform keeps when it sets off (it drops the two "reached" bits) constexpr uint32_t MovementMask = Waiting | Travelling | Stopped; }; /** - * How the client picks the subcomponents (LWOMovingPlatformComponent::LoadConfigData and SetupPlatform): one for each + * How the client picks the subcomponents: 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. @@ -75,8 +75,8 @@ std::vector ChooseSubComponentTypes(int32_t componentID, eMoverSubComponentType ChooseMoverSubComponentType(int32_t componentID, const Entity& entity); /** - * 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 reads. + * The base of the platform subcomponents, as the client's (mover, simple mover and rotator are built on it): the + * platform's state between the waypoints of its path. */ class PlatformSubComponent { public: @@ -84,10 +84,12 @@ public: virtual eMoverSubComponentType GetType() const = 0; - // LWOPlatform::Deserialize: 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 + // 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); + // The path it runs on: the object's attached path, or a simple mover's own between its two ends + const Path* mPath = nullptr; // ePlatformStateFlag bits uint32_t mState = ePlatformStateFlag::Stopped | ePlatformStateFlag::ReachedDesiredWaypoint; int32_t mDesiredWaypointIndex = -1; @@ -97,21 +99,20 @@ public: float mPercentBetweenPoints = 0; // Where it is now: its object's position, which its linear velocity moves NiPoint3 mPosition{}; - // The linear velocity LWOPlatform::SetLinearVelocity gave its object when it last set off (or last looked, - // between waypoints of different speeds). It is only taken away where the platform stops and at either end of the - // path (SetWaitingTime), so through a wait at a waypoint in between the object goes on as it came. + // The linear velocity the platform gave its object when it last set off (or last looked, between waypoints of + // different speeds). It is only taken away where the platform stops and at either end of the path, so through a + // wait at a waypoint in between the object goes on as it came. NiPoint3 mVelocity{}; uint32_t mCurrentWaypointIndex = 0; uint32_t mNextWaypointIndex = 0; - // Time spent at the waypoint, and how long it waits there (LWOPlatform::SetWaitingTime: the waypoint's - // wait, at least 0.1 s); the wait only counts once it was set + // Time spent at the waypoint, and how long it waits there (the waypoint's wait, at least 0.1 s); the wait only + // counts once it was set float mIdleTimeElapsed = 0; float mTimeToWait = 0; bool mWaitSet = false; // The current waypoint's wait float mWaitTime = 0; // Whether it runs: a quick build's platform does not until it is built, nor while the quick build is open again - // (LWOPlatform::SendMessage, RebuildNotifyState) bool mActive = true; // Time travelled since the current waypoint (time based paths only) float mMoveTimeElapsed = 0; @@ -124,11 +125,10 @@ public: }; /** - * A mover along its path (4), as the client's LWOPlatformMover, run by the server as live ran it: the client's - * LWOPlatform logic (RunPlatform, ArrivedAtWaypoint, HandleWaypointArrived, - * ResumeTravelling, ProcessStateChange, GetNextWaypoint / GetNextReversedWaypoint - * the speeds of GetWaypointSpeed). Live serialized it when it began waiting at a waypoint and - * when it left one, not when it stopped (the client stops by itself). + * A mover along its path (4), run by the server as live ran it, with the client's platform logic: it waits at a + * waypoint, travels to the next, arrives, and stops or turns round as the path and what it was sent to do say. Live + * serialized it when it began waiting at a waypoint and when it left one, not when it stopped (the client stops by + * itself). */ class MoverSubComponent final : public PlatformSubComponent { public: @@ -137,49 +137,48 @@ public: }; /** - * The angular velocity of a rotator, as the client's LWOPlatformRotator::SetAngularVelocity works it out: - * the rotation NiMatrix3::FromEulerAnglesXYZ makes of the three angles (radians; the matrix about x times - * the one about y times the one about z, each turning the other way round than the usual right-handed one), as a - * quaternion (NiQuaternion::FromRotation), and the velocity that turns from no rotation to it (from it to none in - * reverse) in half the rate, by the shortest way (hkpKeyFrameUtility::KeyFrameInfo::setUsingPositionOrientationPair - * the axis times the angle, at most half a turn, over the time). Angles that add up to whole turns give - * no velocity. The client divides by a rate of 0; here that gives none. + * The angular velocity of a rotator, as the client works it out: the rotation made of the three angles (radians; the + * matrix about x times the one about y times the one about z, each turning the other way round than the usual + * right-handed one), as a quaternion, and the velocity that turns from no rotation to it (from it to none in reverse) + * in half the rate, by the shortest way (the axis times the angle, at most half a turn, over the time). Angles that + * add up to whole turns give no velocity. The client divides by a rate of 0; here that gives none. */ NiPoint3 CalcRotatorAngularVelocity(float rotX, float rotY, float rotZ, float rate, bool reverse); /** - * A rotator (6), as the client's LWOPlatformRotator: on its path it does not move, it turns at its angular velocity - * while travelling (rotX/rotY/rotZ in half turns, at rate), and never waits or arrives (IncrementWaitingTime - * HandleStoppedLogic); it sends the LWOPlatform data a mover sends. + * A rotator (6), as the client's: on its path it does not move, it turns at its angular velocity while travelling + * (rotX/rotY/rotZ in half turns, at rate), and never waits or arrives; it sends the data a mover sends. */ class RotatorSubComponent final : public PlatformSubComponent { public: RotatorSubComponent(const NiPoint3& startPos) { mPosition = startPos; } eMoverSubComponentType GetType() const override { return eMoverSubComponentType::rotater; } - // LWOPlatformRotator::LoadConfigData: rotX, rotY and rotZ times pi, and rate (seconds) + // rotX, rotY and rotZ times pi, and rate (seconds) float mRotationX = 0.0f; float mRotationY = 0.0f; float mRotationZ = 0.0f; float mRate = 0.0f; - // Whether its object was put at its waypoint (LWOPlatform::ProcessStateChange, from SetupPath) + // Whether its object was put at its waypoint bool mPlaced = false; NiPoint3 GetAngularVelocity() const { return CalcRotatorAngularVelocity(mRotationX, mRotationY, mRotationZ, mRate, mInReverse); } }; /** - * 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. At an end it has stopped there (28), as any platform on a path run once. Like a mover it is made - * stopped at its waypoint (12) and, unless startPathingOnLoad is false, waits there (9, as live constructed the NJ - * counterweights) the shortest wait before it sets off for its other end, a quick build's once it is built. + * A simple mover (5), as the client's: a platform on a path of its own, run once, between a start point and that + * point moved by platformMove (in the start rotation), which takes it platformMoveTime seconds. At an end it has + * stopped there (28), as any platform on a path run once. Like a mover it is made stopped at its waypoint (12) and, + * unless startPathingOnLoad is false, waits there (9, as live constructed the NJ counterweights) the shortest wait + * before it sets off for its other end, a quick build's once it is built. */ class SimpleMoverSubComponent final : public PlatformSubComponent { public: + SimpleMoverSubComponent(); + ~SimpleMoverSubComponent() override; eMoverSubComponentType GetType() const override { return eMoverSubComponentType::simpleMover; } - // LWOPlatformSimpleMover::Deserialize: a dirty bit, then whether there is a starting point with its position and + // 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; @@ -196,6 +195,14 @@ public: * The world position of a waypoint (0 the start, 1 the end) */ NiPoint3 GetWaypointPosition(int32_t index) const; + + /** + * Builds its path from the start point, platformMove and platformMoveTime + */ + void GeneratePath(); + +private: + std::unique_ptr mOwnPath; }; @@ -308,12 +315,6 @@ public: */ void AddPlatformAtLastWaypointCallback(const std::function& callback); - /** - * Called when a simple mover gets to the waypoint it was sent to: the client's LWOPlatform::ArrivedAtWaypoint - * and HandleWaypointArrived (Arrived, then ArrivedAtDesiredWaypoint and PlatformAtLastWaypoint) - */ - void OnSimpleMoverArrived(int32_t index); - private: /** @@ -326,11 +327,6 @@ private: */ std::u16string m_PathName; - /** - * Whether the platform has stopped pathing - */ - bool m_PathingStopped = false; - // The waypoint the path starts at (attached_path_start), written with the path uint32_t m_StartWaypoint = 0; // Whether it runs the path backwards from there (SetCurrentPath), written with the path @@ -354,39 +350,43 @@ private: class QuickBuildComponent* m_QuickBuild = nullptr; bool m_CheckedQuickBuild = false; - /** - * Counts sends so an arrival timer from an older send does nothing - */ - uint32_t m_MoveGeneration = 0; - - void SimpleMoverGotoWaypoint(int32_t index); + // Counts the times the platforms were made anew (SetCurrentPath), so what was running one of before stops + uint32_t m_SetupGeneration = 0; void FireArrivalHooks(uint32_t index, bool atDesiredWaypoint, bool atLastWaypoint); - // LWOPlatform::ProcessStateChange: index becomes the current waypoint (the next from the path's behavior and direction) - void MoverProcessStateChange(uint32_t index); + // Whether the platform starts by itself (startPathingOnLoad, true unless set false) + bool StartsOnLoad() const; + // A simple mover's settings from the level config, its path, and where it is on it + void SetupSimpleMover(SimpleMoverSubComponent& simpleMover); // The mover at its starting waypoint on the path, started unless startPathingOnLoad is false - void SetupMoverPath(); - // Puts the object where the mover is, without a serialization of its physics - void MoveObject(); - // A simple mover's wait at its waypoint before it sets off by itself - void RunSimpleMover(float deltaTime); + void SetupMoverPath(PlatformSubComponent& mover); // A rotator's settings from the level config, and where it is on its path - void SetupRotator(); + void SetupRotator(RotatorSubComponent& rotator); + // Serializes the object with the platform's state + void SerializePlatform(PlatformSubComponent& platform); + // Index becomes the platform's current waypoint (the next from the path's behavior and direction) + void PlatformProcessStateChange(PlatformSubComponent& platform, uint32_t index); + // Starts a wait of at least 0.1 s + void PlatformSetWaitingTime(PlatformSubComponent& platform, float waitTime, float idleTimeElapsed); + // The velocity towards the next waypoint at the speed for where it is + void PlatformUpdateVelocities(PlatformSubComponent& platform); + // Stopped between two waypoints it travels on; at a waypoint it first waits there + void PlatformResumeTravelling(PlatformSubComponent& platform, bool force); + // Once it is at its next waypoint + bool PlatformArrivedAtWaypoint(PlatformSubComponent& platform); + // 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); + // Puts the object where the platform is, without a serialization of its physics + void MoveObject(const PlatformSubComponent& platform); + void SetObjectTransform(const NiPoint3& position, const NiQuaternion& rotation); + // A platform (mover or simple mover) on its path + bool RunPlatform(PlatformSubComponent& platform, float deltaTime); // Turns a travelling rotator's object by its angular velocity - void RunRotator(float deltaTime); - // LWOPlatform::SetupPath and SendMessage (RebuildNotifyState): whether the platform of a quick build runs yet - bool UpdateActive(PlatformSubComponent& platform); - // LWOPlatform::SetWaitingTime: starts a wait of at least 0.1 s - void MoverSetWaitingTime(float waitTime, float idleTimeElapsed); - // LWOPlatform::UpdateVelocities: the velocity towards the next waypoint at the speed for where it is - void MoverUpdateVelocities(); - // LWOPlatform::ResumeTravelling: stopped between two waypoints it travels on; at a waypoint it first waits there - void MoverResumeTravelling(); - // LWOPlatform::ArrivedAtWaypoint: once it is at its next waypoint - void MoverArrivedAtWaypoint(); - // LWOPlatform::HandleWaypointArrived: tells the scripts, then a platform that travels on waits at the waypoint - void MoverHandleWaypointArrived(uint32_t index, bool atDesiredWaypoint, bool atLastWaypoint); + void RunRotator(RotatorSubComponent& rotator, float deltaTime); + // Whether the platforms of a quick build run yet + void UpdateActive(); public: void Run(float deltaTime) override; diff --git a/tests/dGameTests/dComponentsTests/MovingPlatformComponentTests.cpp b/tests/dGameTests/dComponentsTests/MovingPlatformComponentTests.cpp index 0f1f01fed..af47069b2 100644 --- a/tests/dGameTests/dComponentsTests/MovingPlatformComponentTests.cpp +++ b/tests/dGameTests/dComponentsTests/MovingPlatformComponentTests.cpp @@ -113,8 +113,9 @@ TEST_F(MovingPlatformComponentTest, SimpleMoverConstructionMatchesLive) { EXPECT_EQ(Bits(idle), "00"); } -// Sent to its end it serializes travelling from waypoint 0, and after platformMoveTime it has arrived: stopped at its -// desired and final waypoint 1, moved by platformMove +// A simple mover is a platform on its own two waypoint path, run once. Sent to its end it waits the shortest wait at +// waypoint 0 (9), serializes travelling from it (2), and after platformMoveTime it has arrived: stopped at its final +// waypoint 1 (28), moved by platformMove, and turned round TEST_F(MovingPlatformComponentTest, SimpleMoverTripTiming) { info.lot = 16141; info.pos = NiPoint3(10.0f, 20.0f, 30.0f); @@ -123,32 +124,125 @@ TEST_F(MovingPlatformComponentTest, SimpleMoverTripTiming) { counterweight.AddComponent(-1); auto* const platform = counterweight.AddComponent(41, ""); auto* const mover = platform->GetSimpleMoverSubComponent(); + ASSERT_NE(mover->mPath, nullptr); + ASSERT_EQ(mover->mPath->pathWaypoints.size(), 2u); + EXPECT_EQ(mover->mPath->pathBehavior, PathBehavior::Once); + EXPECT_EQ(mover->mPath->pathWaypoints[1].position, NiPoint3(10.0f, 17.5f, 30.0f)); + EXPECT_FLOAT_EQ(mover->mPath->pathWaypoints[0].speed, 2.5f); platform->GotoWaypoint(1); - counterweight.Update(0.0f); // timers added now start on the next update - EXPECT_EQ(mover->mState, ePlatformStateFlag::Travelling); - EXPECT_EQ(mover->mCurrentWaypointIndex, 0); - EXPECT_FALSE(mover->mInReverse); + EXPECT_EQ(mover->mState, 9u); + EXPECT_EQ(mover->mDesiredWaypointIndex, 1); + RakNet::BitStream waiting; + platform->Serialize(waiting, false); + // 1, not dirty, 1, type 5, no starting point, the state: waiting at 0 forwards, then the ending 0 + EXPECT_EQ(Bits(waiting), "101" "00000101000000000000000000000000" "0" "1" "00001001000000000000000000000000" "00000000000000000000000000000000" "0" "0"); + platform->Run(0.11f); + EXPECT_EQ(mover->mState, ePlatformStateFlag::Travelling); + EXPECT_EQ(mover->mCurrentWaypointIndex, 0u); + EXPECT_FALSE(mover->mInReverse); RakNet::BitStream sent; platform->Serialize(sent, false); - // 1, not dirty, 1, type 5, no starting point, the state: travelling from 0 forwards, then the ending 0 EXPECT_EQ(Bits(sent), "101" "00000101000000000000000000000000" "0" "1" "00000010000000000000000000000000" "00000000000000000000000000000000" "0" "0"); - counterweight.Update(0.5f); + platform->Run(0.5f); EXPECT_EQ(mover->mState, ePlatformStateFlag::Travelling); - counterweight.Update(0.6f); + EXPECT_NEAR(counterweight.GetPosition().y, 18.75f, 0.001f); + EXPECT_NEAR(mover->mPercentBetweenPoints, 0.5f, 0.001f); + platform->Run(0.6f); EXPECT_EQ(mover->mState, ePlatformStateFlag::Stopped | ePlatformStateFlag::ReachedDesiredWaypoint | ePlatformStateFlag::ReachedFinalDestination); - EXPECT_EQ(mover->mCurrentWaypointIndex, 1); - EXPECT_EQ(counterweight.GetPosition(), NiPoint3(10.0f, 17.5f, 30.0f)); - - // And back - platform->GotoWaypoint(0); - counterweight.Update(0.0f); - EXPECT_EQ(mover->mCurrentWaypointIndex, 1); + EXPECT_EQ(mover->mCurrentWaypointIndex, 1u); + EXPECT_EQ(mover->mNextWaypointIndex, 0u); EXPECT_TRUE(mover->mInReverse); - counterweight.Update(1.1f); - EXPECT_EQ(mover->mCurrentWaypointIndex, 0); + EXPECT_EQ(mover->mDesiredWaypointIndex, -1); + EXPECT_EQ(counterweight.GetPosition(), NiPoint3(10.0f, 17.5f, 30.0f)); + // Stopped there it was not serialized: each client's arrives by itself + RakNet::BitStream arrived; + platform->Serialize(arrived, false); + EXPECT_EQ(Bits(arrived), "00"); + + // And back: it waits at its end (25), reversed + platform->GotoWaypoint(0); + EXPECT_EQ(mover->mState, 25u); + EXPECT_EQ(mover->mCurrentWaypointIndex, 1u); + EXPECT_TRUE(mover->mInReverse); + platform->Run(0.11f); + EXPECT_EQ(mover->mState, 2u); + platform->Run(1.1f); + EXPECT_EQ(mover->mState, 28u); + EXPECT_EQ(mover->mCurrentWaypointIndex, 0u); + EXPECT_FALSE(mover->mInReverse); + EXPECT_EQ(counterweight.GetPosition(), NiPoint3(10.0f, 20.0f, 30.0f)); +} + +// The client's simple mover only turns at a waypoint. Sent back while on its way it goes on to the end it was heading +// for, stops there, and then sets off for the waypoint it was sent to. Stopped on its way it stops at that end. +TEST_F(MovingPlatformComponentTest, SimpleMoverOnlyTurnsAtAWaypoint) { + info.lot = 16141; + info.pos = NiPoint3(10.0f, 20.0f, 30.0f); + info.rot = QuatUtils::IDENTITY; + info.settings.Insert(u"startPathingOnLoad", false); + Entity counterweight(288300744895900120, info); + info.settings.values.clear(); + counterweight.AddComponent(-1); + auto* const platform = counterweight.AddComponent(41, ""); + auto* const mover = platform->GetSimpleMoverSubComponent(); + EXPECT_EQ(mover->mState, 12u); + + platform->GotoWaypoint(1); + platform->Run(0.11f); + platform->Run(0.4f); + EXPECT_EQ(mover->mState, 2u); + platform->GotoWaypoint(0); + EXPECT_EQ(mover->mState, 2u); + EXPECT_FALSE(mover->mInReverse); + EXPECT_EQ(mover->mNextWaypointIndex, 1u); + platform->Run(0.4f); + EXPECT_NEAR(counterweight.GetPosition().y, 18.0f, 0.001f); + // At its end it waits to go back + platform->Run(0.3f); + EXPECT_EQ(mover->mState, 25u); + EXPECT_EQ(mover->mCurrentWaypointIndex, 1u); + EXPECT_EQ(mover->mDesiredWaypointIndex, 0); + platform->Run(0.11f); + EXPECT_EQ(mover->mState, 2u); + platform->Run(0.5f); + // Stopped on its way: it goes on to the end it was heading for + platform->StopPathing(); + EXPECT_EQ(mover->mState, 2u); + platform->Run(0.6f); + EXPECT_EQ(mover->mState, 28u); + EXPECT_EQ(mover->mCurrentWaypointIndex, 0u); + EXPECT_EQ(mover->mDesiredWaypointIndex, -1); + EXPECT_EQ(counterweight.GetPosition(), NiPoint3(10.0f, 20.0f, 30.0f)); + platform->Run(1.0f); + EXPECT_EQ(mover->mState, 28u); +} + +// One that starts at its end (platformStartAtEnd) is there, turned round: the client takes the direction it is +// constructed with, and goes back from there +TEST_F(MovingPlatformComponentTest, SimpleMoverStartsAtItsEnd) { + info.lot = 16141; + info.pos = NiPoint3(10.0f, 20.0f, 30.0f); + info.rot = QuatUtils::IDENTITY; + info.settings.Insert(u"platformStartAtEnd", true); + Entity counterweight(288300744895900121, info); + info.settings.values.clear(); + counterweight.AddComponent(-1); + auto* const platform = counterweight.AddComponent(41, ""); + auto* const mover = platform->GetSimpleMoverSubComponent(); + EXPECT_EQ(mover->mState, 9u); + EXPECT_EQ(mover->mCurrentWaypointIndex, 1u); + EXPECT_EQ(mover->mNextWaypointIndex, 0u); + EXPECT_TRUE(mover->mInReverse); + platform->Run(0.05f); + EXPECT_EQ(counterweight.GetPosition(), NiPoint3(10.0f, 17.5f, 30.0f)); + platform->Run(0.06f); + EXPECT_EQ(mover->mState, 2u); + platform->Run(1.1f); + EXPECT_EQ(mover->mState, 28u); + EXPECT_EQ(mover->mCurrentWaypointIndex, 0u); EXPECT_EQ(counterweight.GetPosition(), NiPoint3(10.0f, 20.0f, 30.0f)); } @@ -641,6 +735,9 @@ TEST_F(MovingPlatformComponentTest, OnceOnlyMoverGoesOnItsSpinnersEvent) { script.OnFireEventServerSide(&entity, &entity, "blades_on", 0, 0, 0); EXPECT_EQ(mover->mState, 12u); script.OnFireEventServerSide(&entity, &entity, "blades_off", 0, 0, 0); + EXPECT_EQ(mover->mState, 9u); + EXPECT_EQ(mover->mDesiredWaypointIndex, 1); + platform->Run(0.11f); EXPECT_EQ(mover->mState, 2u); EXPECT_EQ(mover->mCurrentWaypointIndex, 0u); }