feat(moving platform): mover turns with a stopped rotator, Departed, arrivals in one place

Three changes to the platform logic, each as the client's platform does it:

- Mover and rotator on one object: the object now turns at an angular
  velocity. A mover setting off (or going on between waypoints of
  different speeds) puts the object in the rotation for where it is and
  gives it the angular velocity that brings it to the next waypoint's
  rotation as it gets there. A rotator that has not stopped puts its own
  angular velocity back every frame, so the mover only turns the object
  while the rotator is stopped. Objects without a rotator still turn
  between the two waypoints' rotations as before.
- Departed: a new script hook, OnDeparted, called when a platform leaves
  a waypoint it waited at, before it sets off.
- Two waypoints in one place: a platform that has not stopped looks for
  its arrival every frame, also while it waits, and with no special case
  for a next waypoint in the same place. It is there at once, and on a
  path that keeps it at one waypoint it arrives every frame (without
  resending an unchanged state).

ScriptComponent::SetScript can now take a script itself (used by the
tests).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-11-27 17:04:15 -06:00
parent 53dfa66cf7
commit b63f65ee5d
5 changed files with 354 additions and 47 deletions

View File

@@ -167,6 +167,12 @@ namespace {
}
}
// The client's platform takes away both of its object's velocities
void MovingPlatformComponent::ResetVelocities(PlatformSubComponent& platform) {
platform.mVelocity = NiPoint3Constant::ZERO;
m_AngularVelocity = NiPoint3Constant::ZERO;
}
// 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);
@@ -416,7 +422,7 @@ void MovingPlatformComponent::PlatformGotoWaypoint(PlatformSubComponent& platfor
platform.mShouldStopAtDesiredWaypoint = false;
if (stopAtWaypoint) {
StopTravelling(platform);
platform.mVelocity = NiPoint3Constant::ZERO;
ResetVelocities(platform);
}
FireArrivalHooks(index, true, index == waypoints.size() - 1);
return;
@@ -515,20 +521,21 @@ void MovingPlatformComponent::PlatformStop(PlatformSubComponent& platform) {
return;
}
StopTravelling(platform);
platform.mVelocity = NiPoint3Constant::ZERO;
ResetVelocities(platform);
SerializePlatform(platform);
return;
}
if (platform.GetType() == eMoverSubComponentType::rotater) {
// No longer spinning
StopTravelling(platform);
m_AngularVelocity = NiPoint3Constant::ZERO;
Game::entityManager->SerializeEntity(m_Parent);
return;
}
if (platform.mPath == nullptr) return;
// Stopped where it is (the client stops on the stopped bit, 4)
StopTravelling(platform);
platform.mVelocity = NiPoint3Constant::ZERO;
ResetVelocities(platform);
platform.mDesiredWaypointIndex = -1;
platform.mShouldStopAtDesiredWaypoint = false;
Game::entityManager->SerializeEntity(m_Parent);
@@ -595,7 +602,7 @@ void MovingPlatformComponent::PlatformSetWaitingTime(PlatformSubComponent& platf
// 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 ((platform.mState & (ReachedFinalDestination | Stopped)) != 0) platform.mVelocity = NiPoint3Constant::ZERO;
if ((platform.mState & (ReachedFinalDestination | Stopped)) != 0) ResetVelocities(platform);
}
// The velocity towards the next waypoint at the speed for where the platform is; setting off drops the two "reached"
@@ -611,6 +618,9 @@ void MovingPlatformComponent::PlatformUpdateVelocities(PlatformSubComponent& pla
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;
// The object turns at its angular velocity only where it has a rotator on a path; else it is turned between the
// two waypoints' rotations as it goes (MoveObject)
if (platform.GetType() != eMoverSubComponentType::rotater && HasRotatorOnPath()) PlatformSetAngularVelocity(platform);
platform.mState &= MovementMask;
}
@@ -631,9 +641,6 @@ void MovingPlatformComponent::PlatformResumeTravelling(PlatformSubComponent& pla
// 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 (platform.mCurrentWaypointIndex == platform.mNextWaypointIndex) return true;
const auto& waypoints = platform.mPath->pathWaypoints;
const auto& next = waypoints[platform.mNextWaypointIndex].position;
bool closeEnough{};
@@ -648,6 +655,12 @@ bool MovingPlatformComponent::PlatformArrivedAtWaypoint(PlatformSubComponent& pl
}
if (!closeEnough) return true;
// What the clients were last sent, to know whether an arrival while waiting changes it
const auto sentState = platform.mState;
const auto sentCurrent = platform.mCurrentWaypointIndex;
const auto sentNext = platform.mNextWaypointIndex;
const bool sentReverse = platform.mInReverse;
const auto arrived = platform.mNextWaypointIndex;
const bool atLastWaypoint = arrived == static_cast<uint32_t>(waypoints.size() - 1);
bool atDesiredWaypoint = false;
@@ -670,13 +683,19 @@ bool MovingPlatformComponent::PlatformArrivedAtWaypoint(PlatformSubComponent& pl
}
if (stopAtDesiredWaypoint || turnedAtEnd) {
StopTravelling(platform);
platform.mVelocity = NiPoint3Constant::ZERO;
ResetVelocities(platform);
} else {
StartTravelling(platform);
}
PlatformProcessStateChange(platform, arrived);
MoveObject(platform, true);
if (!PlatformHandleWaypointArrived(platform, arrived, atDesiredWaypoint, atLastWaypoint)) return false;
// Live serialized a platform that waits at the waypoint it arrived at (it leaves again), not one that stopped there.
// A platform kept at one waypoint by its path (a path of one waypoint) arrives every frame of its wait without
// anything to send.
const bool unchanged = (sentState & Waiting) != 0 && platform.mState == sentState && platform.mCurrentWaypointIndex == sentCurrent
&& platform.mNextWaypointIndex == sentNext && platform.mInReverse == sentReverse;
if ((platform.mState & Stopped) == 0 && !unchanged) SerializePlatform(platform);
// 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
@@ -697,8 +716,6 @@ bool MovingPlatformComponent::PlatformHandleWaypointArrived(PlatformSubComponent
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 ((platform.mState & Stopped) == 0) SerializePlatform(platform);
return true;
}
@@ -782,51 +799,114 @@ NiPoint3 CalcRotatorAngularVelocity(const float rotX, const float rotY, const fl
q[k] = (m[k][i] + m[i][k]) * root;
}
// 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) {
q[0] = -q[0];
q[1] = -q[1];
q[2] = -q[2];
}
const float length = std::sqrt(q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]);
if (length <= 0.0f || rate == 0.0f) return NiPoint3Constant::ZERO;
for (auto& part : q) part /= length;
const float axisLength = std::sqrt(q[0] * q[0] + q[1] * q[1] + q[2] * q[2]);
// From no rotation to it (from it to none in reverse) in half the rate
const NiQuaternion rotation(q[3], q[0], q[1], q[2]);
if (rate == 0.0f) return NiPoint3Constant::ZERO;
const float time = rate * 0.5f;
return reverse ? CalcAngularVelocity(rotation, QuatUtils::IDENTITY, time) : CalcAngularVelocity(QuatUtils::IDENTITY, rotation, time);
}
// The rotation from the first to the second (the second times the inverse of the first), 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
NiPoint3 CalcAngularVelocity(const NiQuaternion& from, const NiQuaternion& to, const float time) {
if (!(time > 0.0f)) return NiPoint3Constant::ZERO;
auto diff = to * glm::inverse(from);
const float length = std::sqrt(diff.x * diff.x + diff.y * diff.y + diff.z * diff.z + diff.w * diff.w);
if (!(length > 0.0f)) return NiPoint3Constant::ZERO;
diff = NiQuaternion(diff.w / length, diff.x / length, diff.y / length, diff.z / length);
const float axisLength = std::sqrt(diff.x * diff.x + diff.y * diff.y + diff.z * diff.z);
if (!(axisLength > std::numeric_limits<float>::epsilon())) return NiPoint3Constant::ZERO;
const float angle = 2.0f * std::acos(std::min(std::abs(q[3]), 1.0f));
const float scale = (q[3] < 0.0f ? -1.0f : 1.0f) / axisLength * angle * (1.0f / (rate * 0.5f));
return NiPoint3(q[0] * scale, q[1] * scale, q[2] * scale);
const float angle = 2.0f * std::acos(std::min(std::abs(diff.w), 1.0f));
const float scale = (diff.w < 0.0f ? -1.0f : 1.0f) / axisLength * angle * (1.0f / time);
return NiPoint3(diff.x * scale, diff.y * scale, diff.z * scale);
}
// Even acceleration from one speed to the other over the way; the time at that acceleration, or the way over the
// speed without one
float CalcPlatformTravelTime(const NiPoint3& start, const NiPoint3& end, const float startSpeed, const float endSpeed) {
constexpr float minDistance = 0.00001f;
const float distance = Vector3::Distance(start, end);
if (distance < minDistance) return 0.0f;
float acceleration = 0.0f;
if (startSpeed != endSpeed) acceleration = static_cast<float>((endSpeed * endSpeed - startSpeed * startSpeed) / (distance * 2.0));
if (acceleration != 0.0f) {
const double root = std::sqrt(startSpeed * startSpeed + acceleration * 2.0 * distance);
return static_cast<float>(-startSpeed + root) / acceleration;
}
return startSpeed == 0.0f ? 0.0f : distance / startSpeed;
}
// 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.
// its angular velocity, which a rotator that has not stopped puts back to its own whenever it differs (so a mover on
// the same object only turns it while the rotator is stopped). The server turns its object in TurnObject, without a
// serialization of its physics, as a mover's.
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;
SetObjectTransform(rotator.mPosition, rotator.mPath->pathWaypoints[rotator.mCurrentWaypointIndex].rotation);
}
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);
rotation = glm::normalize(glm::angleAxis(speed * deltaTime, axis) * rotation);
moved = true;
// An open quick build's platforms do not turn it
if (!rotator.mActive) m_AngularVelocity = NiPoint3Constant::ZERO;
else if (rotator.mState != 0 && (rotator.mState & Stopped) == 0) m_AngularVelocity = rotator.GetAngularVelocity();
}
bool MovingPlatformComponent::HasRotatorOnPath() const {
const auto* const rotator = GetSubComponent(eMoverSubComponentType::rotater);
return rotator != nullptr && rotator->mPath != nullptr;
}
void MovingPlatformComponent::TurnObject(const float deltaTime) {
const float speed = m_AngularVelocity.Length();
if (!(speed > 0.0f) || !std::isfinite(speed)) return;
const glm::vec3 axis(m_AngularVelocity.x / speed, m_AngularVelocity.y / speed, m_AngularVelocity.z / speed);
SetObjectTransform(m_Parent->GetPosition(), glm::normalize(glm::angleAxis(speed * deltaTime, axis) * m_Parent->GetRotation()));
}
// The client's mover sets off (and, between waypoints of different speeds, goes on) turned towards the next
// waypoint's rotation: at a waypoint from that waypoint's rotation; between two from the rotation for how far it is
// (or its own, when that is within 8 degrees of it, then timed from where it is at the speed for there), at the
// angular velocity that gets it there with the next waypoint. With both waypoints in one rotation it is put in it and
// does not turn.
void MovingPlatformComponent::PlatformSetAngularVelocity(PlatformSubComponent& platform) {
constexpr float closeAngle = 0.13962634f;
constexpr float minStretchSquared = 0.00001f;
const auto& waypoints = platform.mPath->pathWaypoints;
const auto& currentRotation = waypoints[platform.mCurrentWaypointIndex].rotation;
const auto& nextRotation = waypoints[platform.mNextWaypointIndex].rotation;
if (currentRotation == nextRotation) {
SetObjectTransform(platform.mPosition, currentRotation);
m_AngularVelocity = NiPoint3Constant::ZERO;
return;
}
bool closeToRotation = false;
auto rotation = currentRotation;
if ((platform.mState & ReachedDesiredWaypoint) == 0) {
rotation = glm::slerp(currentRotation, nextRotation, platform.mPercentBetweenPoints);
const auto current = m_Parent->GetRotation();
const float dot = std::abs(glm::dot(rotation, current));
if (std::acos(std::min(dot, 1.0f)) < closeAngle) {
closeToRotation = true;
rotation = current;
}
}
if (moved) SetObjectTransform(position, rotation);
if (!closeToRotation) SetObjectTransform(platform.mPosition, rotation);
float time = platform.mTimeBased ? platform.mCurrentSpeed - platform.mMoveTimeElapsed : 0.0f;
if (!platform.mTimeBased) {
auto start = waypoints[platform.mCurrentWaypointIndex].position;
auto startSpeed = platform.mCurrentSpeed;
if (closeToRotation) {
start = platform.mPosition;
startSpeed = platform.mCurrentSpeed + (platform.mNextSpeed - platform.mCurrentSpeed) * platform.mPercentBetweenPoints;
}
const auto& end = waypoints[platform.mNextWaypointIndex].position;
if ((start - end).SquaredLength() > minStretchSquared) time = CalcPlatformTravelTime(start, end, startSpeed, platform.mNextSpeed);
}
if (time > 0.0f) m_AngularVelocity = CalcAngularVelocity(rotation, nextRotation, time);
}
// A platform on its path. The client's is moved by its physics, at the velocity the platform gives its object; the
@@ -835,7 +915,7 @@ bool MovingPlatformComponent::RunPlatform(PlatformSubComponent& platform, const
if (platform.mPath == nullptr) return true;
if (!platform.mActive) {
// An open quick build's platform loses its velocities
platform.mVelocity = NiPoint3Constant::ZERO;
ResetVelocities(platform);
return true;
}
if (platform.mState == 0) return true;
@@ -869,15 +949,18 @@ bool MovingPlatformComponent::RunPlatform(PlatformSubComponent& platform, const
}
if (doneWaiting) {
StartTravelling(platform);
} else if (!stopped && (travelling || platform.mSegmentLength > 0.0f)) {
} else if (!stopped) {
// 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.
// direction they came to 4 in) without ever having left 4. Where the next waypoint is in the same place (two
// waypoints in one place, a path of one waypoint) it is there at once: it arrives in the first frame of the
// wait, and on a path that keeps it there, every frame, as the client's.
if (!PlatformArrivedAtWaypoint(platform)) return false;
}
if ((platform.mState & Travelling) != 0 && (doneWaiting || platform.mCurrentSpeed != platform.mNextSpeed)) {
// The client's platform sends Departed as it leaves a waypoint it waited at
if (doneWaiting && !FireDeparted(platform.mCurrentWaypointIndex)) return false;
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) SerializePlatform(platform);
@@ -898,6 +981,7 @@ void MovingPlatformComponent::Run(const float deltaTime) {
// A script told of an arrival may have put the platforms on another path
if (generation != m_SetupGeneration) return;
}
if (HasRotatorOnPath()) TurnObject(deltaTime);
}
// What live answered a client's request with: the state of the platform (a mover's, a simple mover's on its own path,
@@ -954,7 +1038,7 @@ void MovingPlatformComponent::PlatformWarpToWaypoint(PlatformSubComponent& platf
PlatformProcessStateChange(platform, static_cast<uint32_t>(index));
const bool atEnd = index == 0 || index == waypoints.size() - 1;
platform.mState = Stopped | ReachedDesiredWaypoint | (atEnd ? ReachedFinalDestination : 0);
platform.mVelocity = NiPoint3Constant::ZERO;
ResetVelocities(platform);
m_Parent->SetPosition(waypoints[index].position);
m_Parent->SetRotation(waypoints[index].rotation);
@@ -999,6 +1083,12 @@ void MovingPlatformComponent::FireArrivalHooks(const uint32_t index, const bool
}
}
bool MovingPlatformComponent::FireDeparted(const uint32_t index) {
const auto generation = m_SetupGeneration;
m_Parent->GetScript()->OnDeparted(m_Parent, index);
return generation == m_SetupGeneration;
}
void MovingPlatformComponent::AddPlatformAtLastWaypointCallback(const std::function<void()>& callback) {
m_AtLastWaypointCallbacks.push_back(callback);
}

View File

@@ -144,6 +144,19 @@ public:
*/
NiPoint3 CalcRotatorAngularVelocity(float rotX, float rotY, float rotZ, float rate, bool reverse);
/**
* The angular velocity that turns from one rotation to another in a time, as the client's physics work it out: the
* rotation from the first to the second, normalized, by the shortest way (at most half a turn). None without an axis
* (the same rotation, or whole turns) or without time.
*/
NiPoint3 CalcAngularVelocity(const NiQuaternion& from, const NiQuaternion& to, float time);
/**
* The time to go from start to end, speeding up or slowing down evenly from startSpeed to endSpeed, as the client
* works it out (none for a way under 0.00001 or without speed)
*/
float CalcPlatformTravelTime(const NiPoint3& start, const NiPoint3& end, float startSpeed, float endSpeed);
/**
* 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.
@@ -395,6 +408,22 @@ private:
void RunRotator(RotatorSubComponent& rotator, float deltaTime);
// Whether the platforms of a quick build run yet
void UpdateActive();
// Whether the object has a rotator on a path: then the object turns at its angular velocity
bool HasRotatorOnPath() const;
// A mover's turn towards its next waypoint's rotation, as the client's mover gives it when it sets off: put in the
// rotation for where it is (the waypoint's at a waypoint, else between the two unless it is within 8 degrees of
// that), and the angular velocity that gets it to the next waypoint's rotation as it gets there
void PlatformSetAngularVelocity(PlatformSubComponent& platform);
// Turns the object by its angular velocity
void TurnObject(float deltaTime);
// Takes away the object's linear and angular velocities
void ResetVelocities(PlatformSubComponent& platform);
// Tells the scripts the platform leaves a waypoint after waiting there; false when one put it on another path
bool FireDeparted(uint32_t index);
// The object's angular velocity (world), only used while it has a rotator on a path: the mover's turn towards the
// next waypoint's rotation, or the rotator's, which puts its own back whenever it is turning
NiPoint3 m_AngularVelocity{};
public:
void Run(float deltaTime) override;
@@ -405,6 +434,9 @@ public:
// Whether the platform has a mover on a path
bool IsMover() const;
// The object's angular velocity while it has a rotator on a path
const NiPoint3& GetAngularVelocity() const { return m_AngularVelocity; }
// Whether the platform follows a path (its attached_path); a simple mover without one has no waypoints
bool HasPath() const { return m_Path != nullptr; }

View File

@@ -58,6 +58,11 @@ public:
*/
void SetScript(const std::string& scriptName);
/**
* Sets the script itself (it is not owned: scripts are shared singletons)
*/
void SetScript(CppScripts::Script* const script) { m_Script = script; }
bool OnGetObjectReportInfo(GameMessages::GetObjectReportInfo& reportInfo);
// Registers a message from a script to be listened for on the parent object

View File

@@ -307,6 +307,14 @@ namespace CppScripts {
*/
virtual void OnPlatformAtLastWaypoint(Entity* self) {};
/**
* Invoked when a moving platform leaves a waypoint after waiting there (the Departed message, path type
* "movingPlatform"), with the waypoint it leaves.
*
* Equivalent to 'function onDeparted(self, msg)'
*/
virtual void OnDeparted(Entity* self, uint32_t waypointIndex) {};
/**
* Invoked when a player fired a skill event on self.
*

View File

@@ -5,6 +5,8 @@
#include <bit>
#include <glm/gtc/quaternion.hpp>
#include "BitStream.h"
#include "CDClientDatabase.h"
#include "Entity.h"
@@ -14,6 +16,7 @@
#include "MovementMessages.h"
#include "MovingPlatformComponent.h"
#include "02_server/Map/General/SimpleOnceOnlyMoverSpinnerEvent.h"
#include "ScriptComponent.h"
#include "SimplePhysicsComponent.h"
#include "Zone.h"
#include "dZoneManager.h"
@@ -29,6 +32,15 @@ namespace {
return bits;
}
// Counts the platform messages its object's script is told of
class PlatformMessages : public CppScripts::Script {
public:
void OnWaypointReached(Entity* self, const uint32_t waypointIndex) override { arrived.push_back(waypointIndex); }
void OnDeparted(Entity* self, const uint32_t waypointIndex) override { departed.push_back(waypointIndex); }
std::vector<uint32_t> arrived;
std::vector<uint32_t> departed;
};
std::string HexBits(const std::string& hex, const uint32_t count) {
std::string bits;
for (size_t i = 0; i + 1 < hex.size(); i += 2) {
@@ -995,3 +1007,163 @@ TEST_F(MovingPlatformComponentTest, FireTransRockGoesBackDownAfterItsHold) {
EXPECT_EQ(mover->mState, 28u);
EXPECT_EQ(mover->mCurrentWaypointIndex, 0u);
}
// An object with a mover and a rotator: while the rotator is stopped, the mover turns the object as the client's does.
// Setting off from a waypoint it is put in that waypoint's rotation and gets the angular velocity that brings it to the
// next waypoint's rotation as it gets there (here a quarter turn about y over the 2 s of the way); at the end it is put
// in the waypoint's rotation and stops turning. A rotator that turns again wins over the mover.
TEST_F(MovingPlatformComponentTest, MoverTurnsItsObjectWhileItsRotatorIsStopped) {
Path path;
path.pathType = PathType::MovingPlatform;
path.pathName = "TestMoverTurnPath";
path.pathBehavior = PathBehavior::Once;
PathWaypoint first;
first.position = NiPoint3(0.0f, 0.0f, 0.0f);
first.speed = 5.0f;
PathWaypoint second = first;
second.position = NiPoint3(10.0f, 0.0f, 0.0f);
second.rotation = glm::angleAxis(3.14159265f / 2.0f, glm::vec3(0.0f, 1.0f, 0.0f));
path.pathWaypoints = { first, second };
const_cast<Zone*>(Game::zoneManager->GetZone())->AddPath(path);
info.pos = NiPoint3(0.0f, 0.0f, 0.0f);
info.rot = QuatUtils::IDENTITY;
info.settings.Insert<bool>(u"platformIsMover", true);
info.settings.Insert<bool>(u"platformIsRotater", true);
info.settings.Insert<float>(u"rotY", 0.5f);
info.settings.Insert<float>(u"rate", 2.0f);
Entity entity(288300744895900130, info);
info.settings.values.clear();
entity.AddComponent<SimplePhysicsComponent>(-1);
auto* const platform = entity.AddComponent<MovingPlatformComponent>(-1, "TestMoverTurnPath");
auto* const mover = platform->GetSubComponent(eMoverSubComponentType::mover);
auto* const rotator = platform->GetSubComponent(eMoverSubComponentType::rotater);
ASSERT_NE(rotator, nullptr);
// The rotator stopped at its waypoint, as one never started
rotator->mState = 12;
platform->Run(0.11f);
EXPECT_EQ(mover->mState, 2u);
EXPECT_NEAR(platform->GetAngularVelocity().y, 3.14159265f / 4.0f, 0.0001f);
platform->Run(0.89f);
EXPECT_NEAR(entity.GetPosition().x, 4.45f, 0.001f);
EXPECT_NEAR(entity.GetRotation().w, std::cos(3.14159265f / 8.0f), 0.001f);
EXPECT_NEAR(entity.GetRotation().y, std::sin(3.14159265f / 8.0f), 0.001f);
platform->Run(1.2f);
EXPECT_EQ(mover->mState, 28u);
EXPECT_EQ(entity.GetRotation(), second.rotation);
EXPECT_EQ(platform->GetAngularVelocity(), NiPoint3Constant::ZERO);
platform->Run(1.0f);
EXPECT_EQ(entity.GetRotation(), second.rotation);
// The rotator sent turning again: its angular velocity, not the mover's
platform->StartPathing();
platform->Run(0.5f);
EXPECT_NEAR(platform->GetAngularVelocity().y, -3.14159265f / 2.0f, 0.0001f);
}
// The client's angular velocity between two rotations: the shortest way, over the time; none without a turn or a time.
// Its travel time speeds up or slows down evenly between the two speeds.
TEST_F(MovingPlatformComponentTest, AngularVelocityAndTravelTimeAsTheClient) {
const auto quarter = glm::angleAxis(3.14159265f / 2.0f, glm::vec3(0.0f, 0.0f, 1.0f));
const auto velocity = CalcAngularVelocity(QuatUtils::IDENTITY, quarter, 2.0f);
EXPECT_NEAR(velocity.z, 3.14159265f / 4.0f, 0.0001f);
EXPECT_NEAR(CalcAngularVelocity(quarter, QuatUtils::IDENTITY, 2.0f).z, -3.14159265f / 4.0f, 0.0001f);
EXPECT_EQ(CalcAngularVelocity(quarter, quarter, 2.0f), NiPoint3Constant::ZERO);
EXPECT_EQ(CalcAngularVelocity(QuatUtils::IDENTITY, quarter, 0.0f), NiPoint3Constant::ZERO);
EXPECT_FLOAT_EQ(CalcPlatformTravelTime(NiPoint3(0.0f, 0.0f, 0.0f), NiPoint3(10.0f, 0.0f, 0.0f), 5.0f, 5.0f), 2.0f);
// From 0 to 10 over 10: the average speed 5
EXPECT_NEAR(CalcPlatformTravelTime(NiPoint3(0.0f, 0.0f, 0.0f), NiPoint3(10.0f, 0.0f, 0.0f), 0.0f, 10.0f), 2.0f, 0.0001f);
EXPECT_EQ(CalcPlatformTravelTime(NiPoint3(0.0f, 0.0f, 0.0f), NiPoint3(0.0f, 0.0f, 0.0f), 5.0f, 5.0f), 0.0f);
}
// The client's platform sends Departed as it leaves a waypoint it waited at (with that waypoint), not when it stops
TEST_F(MovingPlatformComponentTest, MoverDepartsAfterItsWait) {
Path path;
path.pathType = PathType::MovingPlatform;
path.pathName = "TestDepartedPath";
path.pathBehavior = PathBehavior::Once;
PathWaypoint first;
first.position = NiPoint3(0.0f, 0.0f, 0.0f);
first.speed = 10.0f;
PathWaypoint second = first;
second.position = NiPoint3(10.0f, 0.0f, 0.0f);
second.movingPlatform.wait = 1.0f;
PathWaypoint third = first;
third.position = NiPoint3(20.0f, 0.0f, 0.0f);
path.pathWaypoints = { first, second, third };
const_cast<Zone*>(Game::zoneManager->GetZone())->AddPath(path);
Entity entity(288300744895900131, info);
PlatformMessages messages;
entity.AddComponent<ScriptComponent>(-1, "", false)->SetScript(&messages);
auto* const platform = entity.AddComponent<MovingPlatformComponent>(-1, "TestDepartedPath");
platform->Run(0.05f);
EXPECT_TRUE(messages.departed.empty());
platform->Run(0.06f);
EXPECT_EQ(messages.departed, std::vector<uint32_t>{ 0 });
platform->Run(1.0f);
EXPECT_EQ(messages.arrived, std::vector<uint32_t>{ 1 });
platform->Run(0.5f);
EXPECT_EQ(messages.departed, std::vector<uint32_t>{ 0 });
platform->Run(0.51f);
EXPECT_EQ(messages.departed, (std::vector<uint32_t>{ 0, 1 }));
platform->Run(1.0f);
platform->Run(1.0f);
EXPECT_EQ(messages.arrived, (std::vector<uint32_t>{ 1, 2 }));
EXPECT_EQ(messages.departed.size(), 2u);
}
// Where the next waypoint is in the same place the client's platform is there at once: waiting at waypoint 0 it
// arrives at waypoint 1 in the first frame, and waits there. On a path of one waypoint it arrives every frame.
TEST_F(MovingPlatformComponentTest, WaypointsInOnePlaceArriveAtOnce) {
Path path;
path.pathType = PathType::MovingPlatform;
path.pathName = "TestSamePlacePath";
path.pathBehavior = PathBehavior::Once;
PathWaypoint first;
first.position = NiPoint3(0.0f, 0.0f, 0.0f);
first.speed = 10.0f;
PathWaypoint second = first;
second.movingPlatform.wait = 2.0f;
PathWaypoint third = first;
third.position = NiPoint3(10.0f, 0.0f, 0.0f);
path.pathWaypoints = { first, second, third };
const_cast<Zone*>(Game::zoneManager->GetZone())->AddPath(path);
Entity entity(288300744895900132, info);
PlatformMessages messages;
entity.AddComponent<ScriptComponent>(-1, "", false)->SetScript(&messages);
auto* const platform = entity.AddComponent<MovingPlatformComponent>(-1, "TestSamePlacePath");
auto* const mover = platform->GetMoverSubComponent();
EXPECT_EQ(mover->mState, 9u);
platform->Run(0.05f);
EXPECT_EQ(messages.arrived, std::vector<uint32_t>{ 1 });
EXPECT_EQ(mover->mState, 9u);
EXPECT_EQ(mover->mCurrentWaypointIndex, 1u);
EXPECT_TRUE(messages.departed.empty());
platform->Run(1.0f);
EXPECT_EQ(mover->mState, 9u);
platform->Run(1.0f);
EXPECT_EQ(messages.departed, std::vector<uint32_t>{ 1 });
Path single;
single.pathType = PathType::MovingPlatform;
single.pathName = "TestOneWaypointPath";
single.pathBehavior = PathBehavior::Loop;
single.pathWaypoints = { first };
const_cast<Zone*>(Game::zoneManager->GetZone())->AddPath(single);
Entity alone(288300744895900133, info);
PlatformMessages aloneMessages;
alone.AddComponent<ScriptComponent>(-1, "", false)->SetScript(&aloneMessages);
auto* const alonePlatform = alone.AddComponent<MovingPlatformComponent>(-1, "TestOneWaypointPath");
alonePlatform->Run(0.05f);
alonePlatform->Run(0.05f);
alonePlatform->Run(0.05f);
EXPECT_EQ(aloneMessages.arrived, (std::vector<uint32_t>{ 0, 0, 0 }));
EXPECT_TRUE(aloneMessages.departed.empty());
}