mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-11-30 09:51:38 +00:00
The live captures show platform movement in whole 1/15 s steps in every zone (departure drift past a waypoint is always a whole number of them, exact to 0.1 ms on about 260 departures), whatever the server loop ran at, while the waypoint waits count the loop's time. Platforms now move their object by the whole steps that fall in a frame; timers keep the frame's time. The world server's frame rate is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
465 lines
18 KiB
C++
465 lines
18 KiB
C++
/*
|
|
* Darkflame Universe
|
|
* Copyright 2019
|
|
*/
|
|
|
|
#ifndef MOVINGPLATFORMCOMPONENT_H
|
|
#define MOVINGPLATFORMCOMPONENT_H
|
|
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <vector>
|
|
|
|
#include "RakNetTypes.h"
|
|
#include "NiPoint3.h"
|
|
#include "NiQuaternion.h"
|
|
#include <string>
|
|
|
|
#include "dCommonVars.h"
|
|
#include "EntityManager.h"
|
|
#include "Component.h"
|
|
#include "eMovementPlatformState.h"
|
|
#include "eReplicaComponentType.h"
|
|
|
|
struct ObjectLoadState;
|
|
|
|
struct Path;
|
|
namespace GameMessages { struct PlatformResync; }
|
|
|
|
/**
|
|
* Different types of available platforms
|
|
*/
|
|
enum class eMoverSubComponentType : uint32_t {
|
|
mover = 4,
|
|
|
|
/**
|
|
* A two point mover set up from the MovingPlatforms table or the platformMove* settings (used in NJ)
|
|
*/
|
|
simpleMover = 5,
|
|
|
|
/**
|
|
* A mover that turns between rotations along its path (same data as a mover)
|
|
*/
|
|
rotater = 6,
|
|
|
|
/**
|
|
* The level config set the platform flags all false: the client makes no subcomponent
|
|
*/
|
|
none = 0,
|
|
};
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
namespace ePlatformStateFlag {
|
|
constexpr uint32_t Waiting = 1;
|
|
constexpr uint32_t Travelling = 2;
|
|
constexpr uint32_t Stopped = 4;
|
|
constexpr uint32_t ReachedDesiredWaypoint = 8;
|
|
constexpr uint32_t ReachedFinalDestination = 16;
|
|
// 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: one for each
|
|
* of the platformIsMover, platformIsSimpleMover and platformIsRotater settings that is true, in that order, and when
|
|
* none is set a mover for a platform with no registry component (set up from its attached path) or a simple mover for
|
|
* one with a registry component.
|
|
*/
|
|
std::vector<eMoverSubComponentType> ChooseSubComponentTypes(int32_t componentID, const Entity& entity);
|
|
|
|
// The first of ChooseSubComponentTypes (none when there are none)
|
|
eMoverSubComponentType ChooseMoverSubComponentType(int32_t componentID, const Entity& entity);
|
|
|
|
/**
|
|
* 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:
|
|
virtual ~PlatformSubComponent() = default;
|
|
|
|
virtual eMoverSubComponentType GetType() const = 0;
|
|
|
|
// 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;
|
|
bool mInReverse = false;
|
|
bool mShouldStopAtDesiredWaypoint = false;
|
|
// From the current waypoint (0) to the next (1)
|
|
float mPercentBetweenPoints = 0;
|
|
// Where it is now: its object's position, which its linear velocity moves. Nowhere (0, 0, 0) without a path.
|
|
NiPoint3 mPosition{};
|
|
// 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 (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
|
|
bool mActive = true;
|
|
// Time travelled since the current waypoint (time based paths only)
|
|
float mMoveTimeElapsed = 0;
|
|
// The current and next waypoints' speeds; time based paths: the seconds from one to the next in mCurrentSpeed
|
|
float mCurrentSpeed = 0;
|
|
float mNextSpeed = 0;
|
|
// From the current waypoint to the next
|
|
float mSegmentLength = 0;
|
|
bool mTimeBased = false;
|
|
};
|
|
|
|
/**
|
|
* 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:
|
|
eMoverSubComponentType GetType() const override { return eMoverSubComponentType::mover; }
|
|
};
|
|
|
|
/**
|
|
* 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);
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
class RotatorSubComponent final : public PlatformSubComponent {
|
|
public:
|
|
eMoverSubComponentType GetType() const override { return eMoverSubComponentType::rotater; }
|
|
|
|
// 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
|
|
bool mPlaced = false;
|
|
|
|
NiPoint3 GetAngularVelocity() const { return CalcRotatorAngularVelocity(mRotationX, mRotationY, mRotationZ, mRate, mInReverse); }
|
|
};
|
|
|
|
/**
|
|
* 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; }
|
|
|
|
// A dirty bit, then whether there is a starting point with its position and
|
|
// rotation (w, x, y, z); a dirty bit, then the state, the current waypoint and whether it is reversing
|
|
void Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) override;
|
|
|
|
NiPoint3 mStartPosition{};
|
|
NiQuaternion mStartRotation{};
|
|
NiPoint3 mMove{};
|
|
float mMoveTime = 0.0f;
|
|
bool mStartAtEnd = false;
|
|
|
|
bool mDirtyStartingPoint = false;
|
|
bool mDirtyState = false;
|
|
|
|
/**
|
|
* The world position of a waypoint (0 the start, 1 the end)
|
|
*/
|
|
NiPoint3 GetWaypointPosition(int32_t index) const;
|
|
|
|
/**
|
|
* Builds its path from the start point, platformMove and platformMoveTime
|
|
*/
|
|
void GeneratePath();
|
|
|
|
private:
|
|
std::unique_ptr<Path> mOwnPath;
|
|
};
|
|
|
|
|
|
/**
|
|
* Represents entities that may be moving platforms, indicating how they should move through the world.
|
|
* NOTE: the logic in this component hardly does anything, apparently the client can figure most of this stuff out
|
|
* if you just serialize it correctly, resulting in smoother results anyway. Don't be surprised if the exposed APIs
|
|
* don't at all do what you expect them to as we don't instruct the client of changes made here.
|
|
* ^^^ Trivia: This made the red blocks platform and property platforms a pain to implement.
|
|
*/
|
|
class MovingPlatformComponent final : public Component {
|
|
public:
|
|
static constexpr eReplicaComponentType ComponentType = eReplicaComponentType::MOVING_PLATFORM;
|
|
|
|
// The client's allocator for this type (ObjectLoader2::DoObjectComponentLoad calls it in load order)
|
|
static void Allocator(Entity& entity, ObjectLoadState& state);
|
|
|
|
MovingPlatformComponent(Entity* parent, const int32_t componentID, const std::string& pathName);
|
|
|
|
void LoadDataFromTemplate() override;
|
|
void LoadConfigData() override;
|
|
~MovingPlatformComponent() override;
|
|
|
|
void Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) override;
|
|
|
|
/**
|
|
* Stops all pathing, called when an entity starts a quick build associated with this platform
|
|
*/
|
|
void OnQuickBuildInitilized();
|
|
|
|
/**
|
|
* Starts the pathing, called when an entity completed a quick build associated with this platform
|
|
*/
|
|
void OnCompleteQuickBuild();
|
|
|
|
/**
|
|
* Updates the movement state for the moving platform
|
|
* @param value the movement state to set
|
|
*/
|
|
void SetMovementState(eMovementPlatformState value);
|
|
|
|
/**
|
|
* Instructs the moving platform to go to some waypoint
|
|
* @param index the index of the waypoint
|
|
* @param stopAtWaypoint determines if the platform should stop at the waypoint
|
|
*/
|
|
void GotoWaypoint(uint32_t index, bool stopAtWaypoint = true);
|
|
|
|
/**
|
|
* Puts the platform on a path of the zone, at one of its waypoints (the SetCurrentPath game message): a mover
|
|
* starts over on it. False when the platform is not a mover or the zone has no such path.
|
|
*/
|
|
bool SetCurrentPath(const std::string& pathName, uint32_t startPoint = 0, bool reverse = false);
|
|
|
|
/**
|
|
* Starts the pathing of this platform, setting appropriate waypoints and speeds
|
|
*/
|
|
void StartPathing();
|
|
|
|
/**
|
|
* Stops the platform from moving, waiting for it to be activated again.
|
|
*/
|
|
void StopPathing();
|
|
|
|
/**
|
|
* Determines if the entity should be serialized on the next update
|
|
* @param value whether to serialize the entity or not
|
|
*/
|
|
void SetSerialized(bool value);
|
|
|
|
/**
|
|
* Returns if this platform will start automatically after spawn
|
|
* @return if this platform will start automatically after spawn
|
|
*/
|
|
bool GetNoAutoStart() const;
|
|
|
|
/**
|
|
* Sets the auto start value for this platform
|
|
* @param value the auto start value to set
|
|
*/
|
|
void SetNoAutoStart(bool value);
|
|
|
|
/**
|
|
* Warps the platform to a waypoint index, skipping its current path
|
|
* @param index the index to go to
|
|
*/
|
|
void WarpToWaypoint(size_t index);
|
|
|
|
/**
|
|
* Returns the waypoint this platform was previously at
|
|
* @return the waypoint this platform was previously at
|
|
*/
|
|
size_t GetLastWaypointIndex() const;
|
|
|
|
/**
|
|
* The subcomponent of a type, if the platform has one
|
|
*/
|
|
PlatformSubComponent* GetSubComponent(eMoverSubComponentType type) const;
|
|
|
|
/**
|
|
* The mover, or without one the rotator, if the platform has one
|
|
*/
|
|
PlatformSubComponent* GetMoverSubComponent() const;
|
|
|
|
/**
|
|
* The simple mover, if the platform has one
|
|
*/
|
|
SimpleMoverSubComponent* GetSimpleMoverSubComponent() const;
|
|
|
|
eMoverSubComponentType GetMoverSubComponentType() const { return m_MoverSubComponentType; }
|
|
|
|
/**
|
|
* Calls back whenever the platform is at the last waypoint of its path (the PlatformAtLastWaypoint game message,
|
|
* which live's scripts ask to be notified of on the NJ counterweights)
|
|
*/
|
|
void AddPlatformAtLastWaypointCallback(const std::function<void()>& callback);
|
|
|
|
private:
|
|
|
|
/**
|
|
* The path this platform is currently on
|
|
*/
|
|
const Path* m_Path = nullptr;
|
|
|
|
/**
|
|
* The name of the path this platform is currently on
|
|
*/
|
|
std::u16string m_PathName;
|
|
|
|
// 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
|
|
bool m_PathReverse = false;
|
|
// The path changed since it was last written
|
|
bool m_DirtyPath = false;
|
|
|
|
/**
|
|
* The type of the first subcomponent (none without one)
|
|
*/
|
|
eMoverSubComponentType m_MoverSubComponentType = eMoverSubComponentType::none;
|
|
|
|
/**
|
|
* The subcomponents, one per type the client makes (ChooseSubComponentTypes), in its order
|
|
*/
|
|
std::vector<std::unique_ptr<PlatformSubComponent>> m_SubComponents;
|
|
|
|
std::vector<std::function<void()>> m_AtLastWaypointCallbacks;
|
|
|
|
// The object's quick build, looked up when the platform first runs
|
|
class QuickBuildComponent* m_QuickBuild = nullptr;
|
|
bool m_CheckedQuickBuild = false;
|
|
|
|
// 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);
|
|
|
|
// 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(PlatformSubComponent& mover);
|
|
// A rotator's settings from the level config, and where it is on its path
|
|
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);
|
|
// Sends a rotator forwards or backwards
|
|
void RotatorSetMoving(RotatorSubComponent& rotator, bool reverse);
|
|
void PlatformStart(PlatformSubComponent& platform);
|
|
void PlatformStop(PlatformSubComponent& platform);
|
|
void PlatformWarpToWaypoint(PlatformSubComponent& platform, size_t index);
|
|
// Puts the object where the platform is, without a serialization of its physics
|
|
void MoveObject(const PlatformSubComponent& platform, bool atWaypoint = false);
|
|
void SetObjectTransform(const NiPoint3& position, const NiQuaternion& rotation);
|
|
// A platform (mover or simple mover) on its path: its timers count the frame's time, its object moves by the
|
|
// physics steps run this frame
|
|
bool RunPlatform(PlatformSubComponent& platform, float deltaTime, float stepTime);
|
|
// Turns a travelling rotator's object by its angular velocity
|
|
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{};
|
|
|
|
// Frame time not yet run as a physics step (PhysicsStep)
|
|
float m_StepTimeLeft = 0.0f;
|
|
|
|
public:
|
|
// Live's world servers moved platforms in fixed physics steps of 1/15 s whatever their loop rate (departure drift
|
|
// past a waypoint is always a whole number of them), while the waits count the loop's time
|
|
static constexpr float PhysicsStep = 1.0f / 15.0f;
|
|
|
|
void Run(float deltaTime) override;
|
|
|
|
// The platform's state as a PlatformResync gives it (the answer to RequestPlatformResync); false without a platform
|
|
bool FillResync(GameMessages::PlatformResync& resync) const;
|
|
|
|
// 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; }
|
|
|
|
private:
|
|
|
|
/**
|
|
* Whether the platform shouldn't auto start
|
|
*/
|
|
bool m_NoAutoStart;
|
|
|
|
/**
|
|
* Whether to serialize the entity on the next update
|
|
*/
|
|
bool m_Serialize = false;
|
|
};
|
|
|
|
#endif // MOVINGPLATFORMCOMPONENT_H
|