mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-11-30 01:41:40 +00:00
Merge exp/platforms-7: platform riders, 1/15 s platform steps
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -16,7 +16,11 @@ struct RemoteInputInfo {
|
||||
bool m_IsModified = false;
|
||||
};
|
||||
|
||||
// The object a character stands on (the client's local space): position and linear velocity as offsets from that
|
||||
// object's position and velocity, not turned with it
|
||||
struct LocalSpaceInfo {
|
||||
bool operator==(const LocalSpaceInfo& other) const = default;
|
||||
|
||||
LWOOBJID objectId = LWOOBJID_EMPTY;
|
||||
NiPoint3 position = NiPoint3Constant::ZERO;
|
||||
NiPoint3 linearVelocity = NiPoint3Constant::ZERO;
|
||||
@@ -29,6 +33,7 @@ struct PositionUpdate {
|
||||
bool onRail = false;
|
||||
NiPoint3 velocity = NiPoint3Constant::ZERO;
|
||||
NiPoint3 angularVelocity = NiPoint3Constant::ZERO;
|
||||
bool hasLocalSpaceInfo = false;
|
||||
LocalSpaceInfo localSpaceInfo;
|
||||
RemoteInputInfo remoteInputInfo;
|
||||
};
|
||||
|
||||
@@ -1711,6 +1711,12 @@ void Entity::ProcessPositionUpdate(PositionUpdate& update) {
|
||||
update.angularVelocity = NiPoint3Constant::ZERO;
|
||||
}
|
||||
|
||||
// Standing on something (the client's local space): the server's position is that object's position here plus
|
||||
// the offset, the way the other clients place the rider; the block is kept and written back
|
||||
if (update.hasLocalSpaceInfo) {
|
||||
if (const auto world = ControllablePhysicsComponent::LocalSpaceToWorld(update.localSpaceInfo)) update.position = *world;
|
||||
}
|
||||
|
||||
// Handle statistics
|
||||
auto* characterComponent = GetComponent<CharacterComponent>();
|
||||
if (characterComponent) {
|
||||
@@ -1723,6 +1729,7 @@ void Entity::ProcessPositionUpdate(PositionUpdate& update) {
|
||||
controllablePhysicsComponent->SetIsOnRail(update.onRail);
|
||||
controllablePhysicsComponent->SetVelocity(update.velocity);
|
||||
controllablePhysicsComponent->SetAngularVelocity(update.angularVelocity);
|
||||
controllablePhysicsComponent->SetLocalSpace(update.hasLocalSpaceInfo, update.localSpaceInfo);
|
||||
|
||||
auto* ghostComponent = GetComponent<GhostComponent>();
|
||||
if (ghostComponent) ghostComponent->SetGhostReferencePoint(update.position);
|
||||
|
||||
@@ -4,6 +4,9 @@
|
||||
*/
|
||||
|
||||
#include "MovingPlatformComponent.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include "BitStream.h"
|
||||
#include "GeneralUtils.h"
|
||||
#include "dZoneManager.h"
|
||||
@@ -911,7 +914,7 @@ void MovingPlatformComponent::PlatformSetAngularVelocity(PlatformSubComponent& p
|
||||
|
||||
// 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) {
|
||||
bool MovingPlatformComponent::RunPlatform(PlatformSubComponent& platform, const float deltaTime, const float stepTime) {
|
||||
if (platform.mPath == nullptr) return true;
|
||||
if (!platform.mActive) {
|
||||
// An open quick build's platform loses its velocities
|
||||
@@ -922,8 +925,8 @@ bool MovingPlatformComponent::RunPlatform(PlatformSubComponent& platform, const
|
||||
|
||||
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
|
||||
platform.mPosition = platform.mPosition + platform.mVelocity * deltaTime;
|
||||
// The object's physics: it goes at its velocity, travelling or not, in whole physics steps
|
||||
platform.mPosition = platform.mPosition + platform.mVelocity * stepTime;
|
||||
|
||||
if (platform.mTimeBased) {
|
||||
// mCurrentSpeed is the seconds to the next waypoint
|
||||
@@ -972,16 +975,22 @@ bool MovingPlatformComponent::RunPlatform(PlatformSubComponent& platform, const
|
||||
// Every platform of the object runs, in the client's order: a mover or simple mover moves the object, a rotator turns it
|
||||
void MovingPlatformComponent::Run(const float deltaTime) {
|
||||
if (m_SubComponents.empty()) return;
|
||||
// The physics steps that fall in this frame (several in a long frame, none in most short ones)
|
||||
m_StepTimeLeft += deltaTime;
|
||||
// (a frame of exactly one step must not lose it to rounding)
|
||||
const float steps = std::floor(m_StepTimeLeft / PhysicsStep + 0.0001f);
|
||||
const float stepTime = steps * PhysicsStep;
|
||||
m_StepTimeLeft = std::max(0.0f, m_StepTimeLeft - stepTime);
|
||||
UpdateActive();
|
||||
const auto generation = m_SetupGeneration;
|
||||
for (size_t i = 0; i < m_SubComponents.size(); i++) {
|
||||
auto& platform = *m_SubComponents[i];
|
||||
if (platform.GetType() == eMoverSubComponentType::rotater) RunRotator(static_cast<RotatorSubComponent&>(platform), deltaTime);
|
||||
else if (!RunPlatform(platform, deltaTime)) return;
|
||||
else if (!RunPlatform(platform, deltaTime, stepTime)) return;
|
||||
// A script told of an arrival may have put the platforms on another path
|
||||
if (generation != m_SetupGeneration) return;
|
||||
}
|
||||
if (HasRotatorOnPath()) TurnObject(deltaTime);
|
||||
if (HasRotatorOnPath() && stepTime > 0.0f) TurnObject(stepTime);
|
||||
}
|
||||
|
||||
// What live answered a client's request with: the state of the platform (a mover's, a simple mover's on its own path,
|
||||
|
||||
@@ -402,8 +402,9 @@ private:
|
||||
// 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
|
||||
bool RunPlatform(PlatformSubComponent& platform, float deltaTime);
|
||||
// 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
|
||||
@@ -425,7 +426,14 @@ private:
|
||||
// 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
|
||||
|
||||
@@ -20,6 +20,9 @@
|
||||
#include "StringifiedEnum.h"
|
||||
#include "Amf3.h"
|
||||
#include "ObjectLoadState.h"
|
||||
#include "GhostComponent.h"
|
||||
|
||||
#include <limits>
|
||||
|
||||
ControllablePhysicsComponent::ControllablePhysicsComponent(Entity* entity, const int32_t componentID) : PhysicsComponent(entity, componentID) {
|
||||
RegisterMsg(&ControllablePhysicsComponent::OnGetObjectReportInfo);
|
||||
@@ -85,8 +88,36 @@ ControllablePhysicsComponent::~ControllablePhysicsComponent() {
|
||||
}
|
||||
}
|
||||
|
||||
// A rider goes with the object it stands on between its client's position updates, so the server's position
|
||||
// (proximity, phantom volumes, ghosting) stays on the platform. Not serialized for it: each client places the rider on
|
||||
// its own view of the object from the local space block.
|
||||
void ControllablePhysicsComponent::Run(float deltaTime) {
|
||||
if (!m_HasLocalSpaceInfo || m_Static) return;
|
||||
// Put somewhere else by the server since (a teleport): off the object until the client says otherwise
|
||||
if (m_Position != m_LocalSpaceWorldPosition) {
|
||||
SetLocalSpace(false, {});
|
||||
return;
|
||||
}
|
||||
const auto world = LocalSpaceToWorld(m_LocalSpaceInfo);
|
||||
if (!world || *world == m_Position) return;
|
||||
const bool dirty = m_DirtyPosition;
|
||||
SetPosition(*world);
|
||||
m_DirtyPosition = dirty;
|
||||
m_LocalSpaceWorldPosition = m_Position;
|
||||
if (auto* const ghostComponent = m_Parent->GetComponent<GhostComponent>()) ghostComponent->SetGhostReferencePoint(*world);
|
||||
}
|
||||
|
||||
void ControllablePhysicsComponent::SetLocalSpace(const bool hasLocalSpaceInfo, const LocalSpaceInfo& info) {
|
||||
m_HasLocalSpaceInfo = hasLocalSpaceInfo;
|
||||
m_LocalSpaceInfo = hasLocalSpaceInfo ? info : LocalSpaceInfo{};
|
||||
m_LocalSpaceWorldPosition = m_Position;
|
||||
}
|
||||
|
||||
std::optional<NiPoint3> ControllablePhysicsComponent::LocalSpaceToWorld(const LocalSpaceInfo& info) {
|
||||
if (info.objectId == LWOOBJID_EMPTY || !Game::entityManager) return std::nullopt;
|
||||
const auto* const object = Game::entityManager->GetEntity(info.objectId);
|
||||
if (!object) return std::nullopt;
|
||||
return object->GetPosition() + info.position;
|
||||
}
|
||||
|
||||
void ControllablePhysicsComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsInitialUpdate) {
|
||||
@@ -176,7 +207,22 @@ void ControllablePhysicsComponent::Serialize(RakNet::BitStream& outBitStream, bo
|
||||
outBitStream.Write(m_AngularVelocity.z);
|
||||
}
|
||||
|
||||
outBitStream.Write0(); // local_space_info, always zero for now.
|
||||
// The local space block as the client writes it (velocity only when its square length is over float epsilon)
|
||||
outBitStream.Write(m_HasLocalSpaceInfo);
|
||||
if (m_HasLocalSpaceInfo) {
|
||||
outBitStream.Write(m_LocalSpaceInfo.objectId);
|
||||
outBitStream.Write(m_LocalSpaceInfo.position.x);
|
||||
outBitStream.Write(m_LocalSpaceInfo.position.y);
|
||||
outBitStream.Write(m_LocalSpaceInfo.position.z);
|
||||
const auto& velocity = m_LocalSpaceInfo.linearVelocity;
|
||||
const bool writeVelocity = velocity.SquaredLength() > std::numeric_limits<float>::epsilon();
|
||||
outBitStream.Write(writeVelocity);
|
||||
if (writeVelocity) {
|
||||
outBitStream.Write(velocity.x);
|
||||
outBitStream.Write(velocity.y);
|
||||
outBitStream.Write(velocity.z);
|
||||
}
|
||||
}
|
||||
|
||||
if (!bIsInitialUpdate) {
|
||||
m_DirtyPosition = false;
|
||||
|
||||
@@ -9,6 +9,9 @@
|
||||
#include "NiQuaternion.h"
|
||||
#include "dpCollisionChecks.h"
|
||||
#include "eBubbleType.h"
|
||||
#include "PositionUpdate.h"
|
||||
|
||||
#include <optional>
|
||||
|
||||
struct ObjectLoadState;
|
||||
|
||||
@@ -310,6 +313,22 @@ public:
|
||||
void ApplyStun(const GameMessages::SetStunned& stun);
|
||||
bool GetStunnedFromUsingItems() const { return m_StunnedUseItemCount > 0; }
|
||||
|
||||
/**
|
||||
* The object this entity stands on and where on it, as the client's position updates give it (the client's local
|
||||
* space). While it is set the entity goes with that object: its world position is the object's position plus the
|
||||
* offset. Written back in this component's serialization, so other clients place the rider on their own view of
|
||||
* the object. Call it after the position is set: a later move by anything else (a teleport) takes the entity off.
|
||||
*/
|
||||
void SetLocalSpace(const bool hasLocalSpaceInfo, const LocalSpaceInfo& info);
|
||||
bool GetHasLocalSpaceInfo() const noexcept { return m_HasLocalSpaceInfo; }
|
||||
const LocalSpaceInfo& GetLocalSpaceInfo() const noexcept { return m_LocalSpaceInfo; }
|
||||
|
||||
/**
|
||||
* Where a local space position is in the world: the object's position plus the offset (not turned with the
|
||||
* object, as the client). Nothing when the object is not there.
|
||||
*/
|
||||
static std::optional<NiPoint3> LocalSpaceToWorld(const LocalSpaceInfo& info);
|
||||
|
||||
private:
|
||||
// The save's physics state (LoadFromSaveData), then where the character comes into the world
|
||||
void ReadSaveData(const tinyxml2::XMLDocument& doc);
|
||||
@@ -327,6 +346,12 @@ private:
|
||||
*/
|
||||
NiPoint3 m_Velocity;
|
||||
|
||||
// The object the entity stands on and where on it (SetLocalSpace)
|
||||
bool m_HasLocalSpaceInfo = false;
|
||||
LocalSpaceInfo m_LocalSpaceInfo;
|
||||
// Where the entity was when its local space was last set or it was last carried
|
||||
NiPoint3 m_LocalSpaceWorldPosition;
|
||||
|
||||
/**
|
||||
* The current angular velocity of the entity
|
||||
*/
|
||||
|
||||
@@ -1684,6 +1684,7 @@ json ToJson(const PositionUpdate& m) {
|
||||
j["onRail"] = ToJson(m.onRail);
|
||||
j["velocity"] = ToJson(m.velocity);
|
||||
j["angularVelocity"] = ToJson(m.angularVelocity);
|
||||
j["hasLocalSpaceInfo"] = ToJson(m.hasLocalSpaceInfo);
|
||||
j["localSpaceInfo"] = ToJson(m.localSpaceInfo);
|
||||
j["remoteInputInfo"] = ToJson(m.remoteInputInfo);
|
||||
return j;
|
||||
|
||||
@@ -1551,6 +1551,7 @@ namespace {
|
||||
struct PositionUpdatePacket final : public WorldPackets::PositionUpdate {
|
||||
void Handle() override {
|
||||
auto positionUpdate = update;
|
||||
positionUpdate.hasLocalSpaceInfo = hasLocalSpaceInfo;
|
||||
|
||||
User* user = UserManager::Instance()->GetUser(sysAddr);
|
||||
if (!user) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
set(DCOMPONENTS_TESTS
|
||||
"DeletionRestrictionTests.cpp"
|
||||
"MovingPlatformComponentTests.cpp"
|
||||
"PlatformRiderTests.cpp"
|
||||
"ScriptComponentTests.cpp"
|
||||
"InventorySaveTests.cpp"
|
||||
"EquipMessagesTests.cpp"
|
||||
|
||||
@@ -186,8 +186,9 @@ TEST_F(MovingPlatformComponentTest, ResyncAnswersLikeLive) {
|
||||
EXPECT_EQ(static_cast<uint32_t>(travelling.eState), 2u);
|
||||
EXPECT_EQ(travelling.iIndex, 0);
|
||||
EXPECT_EQ(travelling.iNextIndex, 1);
|
||||
EXPECT_NEAR(travelling.fPercentBetweenPoints, 0.5f, 0.001f);
|
||||
EXPECT_NEAR(travelling.ptUnexpectedLocation.y, info.pos.y - 1.25f, 0.001f);
|
||||
// 8 physics steps of 1/15 s since it left
|
||||
EXPECT_NEAR(travelling.fPercentBetweenPoints, 8.0f / 15.0f, 0.001f);
|
||||
EXPECT_NEAR(travelling.ptUnexpectedLocation.y, info.pos.y - 2.5f * 8.0f / 15.0f, 0.001f);
|
||||
|
||||
info.lot = 2349;
|
||||
info.pos = NiPoint3(10.0f, 20.0f, 30.0f);
|
||||
@@ -239,8 +240,9 @@ TEST_F(MovingPlatformComponentTest, SimpleMoverTripTiming) {
|
||||
|
||||
platform->Run(0.5f);
|
||||
EXPECT_EQ(mover->mState, ePlatformStateFlag::Travelling);
|
||||
EXPECT_NEAR(counterweight.GetPosition().y, 18.75f, 0.001f);
|
||||
EXPECT_NEAR(mover->mPercentBetweenPoints, 0.5f, 0.001f);
|
||||
// 8 physics steps of 1/15 s since it left
|
||||
EXPECT_NEAR(counterweight.GetPosition().y, 20.0f - 2.5f * 8.0f / 15.0f, 0.001f);
|
||||
EXPECT_NEAR(mover->mPercentBetweenPoints, 8.0f / 15.0f, 0.001f);
|
||||
platform->Run(0.6f);
|
||||
EXPECT_EQ(mover->mState, ePlatformStateFlag::Stopped | ePlatformStateFlag::ReachedDesiredWaypoint | ePlatformStateFlag::ReachedFinalDestination);
|
||||
EXPECT_EQ(mover->mCurrentWaypointIndex, 1u);
|
||||
@@ -823,7 +825,7 @@ TEST_F(MovingPlatformComponentTest, MoverAndRotatorBothRun) {
|
||||
platform->Run(0.11f);
|
||||
EXPECT_EQ(mover->mState, 2u);
|
||||
platform->Run(0.89f);
|
||||
EXPECT_NEAR(entity.GetPosition().x, 4.45f, 0.001f);
|
||||
EXPECT_NEAR(entity.GetPosition().x, 5.0f * 14.0f / 15.0f, 0.001f); // 14 physics steps since it left
|
||||
EXPECT_NEAR(entity.GetRotation().w, 0.7071f, 0.001f);
|
||||
EXPECT_NEAR(entity.GetRotation().y, -0.7071f, 0.001f);
|
||||
|
||||
@@ -904,6 +906,60 @@ TEST_F(MovingPlatformComponentTest, MoverKeepsItsVelocityThroughAWaitLikeLive) {
|
||||
EXPECT_EQ(entity.GetPosition(), NiPoint3(30.0f, 0.0f, 30.0f));
|
||||
}
|
||||
|
||||
// Live moved platforms in fixed physics steps of 1/15 s while its loop ran faster or slower: on a 60 a second loop
|
||||
// the platform moves every fourth frame, a whole step at a time, and the waypoint wait counts the loop's frames, so it
|
||||
// leaves a waypoint (after the 0.1 s wait) a whole number of steps past it: 2 here, as live's subs at speed 15 (2.0)
|
||||
TEST_F(MovingPlatformComponentTest, MoverMovesInLivesPhysicsSteps) {
|
||||
Path path;
|
||||
path.pathType = PathType::MovingPlatform;
|
||||
path.pathName = "TestStepPath";
|
||||
path.pathBehavior = PathBehavior::Once;
|
||||
PathWaypoint first;
|
||||
first.position = NiPoint3(0.0f, 0.0f, 0.0f);
|
||||
first.speed = 15.0f;
|
||||
PathWaypoint second = first;
|
||||
second.position = NiPoint3(3.0f, 0.0f, 0.0f);
|
||||
PathWaypoint third = first;
|
||||
third.position = NiPoint3(3.0f, 0.0f, 30.0f);
|
||||
path.pathWaypoints = { first, second, third };
|
||||
const_cast<Zone*>(Game::zoneManager->GetZone())->AddPath(path);
|
||||
|
||||
info.pos = NiPoint3(0.0f, 0.0f, 0.0f);
|
||||
Entity entity(288300744895900130, info);
|
||||
entity.AddComponent<SimplePhysicsComponent>(-1);
|
||||
auto* const platform = entity.AddComponent<MovingPlatformComponent>(-1, "TestStepPath");
|
||||
auto* const mover = platform->GetMoverSubComponent();
|
||||
constexpr float frame = 1.0f / 60.0f;
|
||||
int frames = 0;
|
||||
while (mover->mState != 2u && frames < 60) { platform->Run(frame); frames++; }
|
||||
ASSERT_EQ(mover->mState, 2u);
|
||||
|
||||
// Travelling: the object moves only on the frames a step falls in, by speed / 15
|
||||
std::vector<float> xs;
|
||||
for (int i = 0; i < 8; i++) {
|
||||
platform->Run(frame);
|
||||
xs.push_back(mover->mPosition.x);
|
||||
}
|
||||
int moves = 0;
|
||||
for (size_t i = 1; i < xs.size(); i++) {
|
||||
if (xs[i] == xs[i - 1]) continue;
|
||||
moves++;
|
||||
EXPECT_NEAR(xs[i] - xs[i - 1], 1.0f, 0.001f);
|
||||
}
|
||||
EXPECT_EQ(moves, 2);
|
||||
|
||||
// At waypoint 1 (3 units on): waiting, then off the next stretch's way from a whole number of steps past it
|
||||
frames = 0;
|
||||
while (!(mover->mState == 2u && mover->mCurrentWaypointIndex == 1u) && frames < 120) { platform->Run(frame); frames++; }
|
||||
ASSERT_EQ(mover->mCurrentWaypointIndex, 1u);
|
||||
ASSERT_EQ(mover->mState, 2u);
|
||||
const float drift = mover->mPosition.x - 3.0f;
|
||||
const float steps = drift / 1.0f;
|
||||
EXPECT_NEAR(steps, std::round(steps), 0.001f);
|
||||
EXPECT_GE(std::round(steps), 1.0f);
|
||||
EXPECT_LE(std::round(steps), 3.0f);
|
||||
}
|
||||
|
||||
// A simple mover without a quick build runs from when it is loaded, as a mover does: waiting at its waypoint (9) for the
|
||||
// shortest wait, then off to its other end, where it has stopped (28). With startPathingOnLoad false it stays as it was
|
||||
// made (12), and so does one its script stops when it starts up (live's NJ spinner script, L_NJ_SPINNER_SERVER).
|
||||
@@ -1049,7 +1105,7 @@ TEST_F(MovingPlatformComponentTest, MoverTurnsItsObjectWhileItsRotatorIsStopped)
|
||||
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.GetPosition().x, 5.0f * 14.0f / 15.0f, 0.001f); // 14 physics steps since it left
|
||||
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);
|
||||
|
||||
|
||||
186
tests/dGameTests/dComponentsTests/PlatformRiderTests.cpp
Normal file
186
tests/dGameTests/dComponentsTests/PlatformRiderTests.cpp
Normal file
@@ -0,0 +1,186 @@
|
||||
// Characters standing on moving platforms: the client's local space block in its position updates (the object it
|
||||
// stands on, and its position and velocity as offsets from that object's) is kept, written back in the controllable
|
||||
// physics serialization as live did, and the server's position goes with the platform.
|
||||
#include "GameDependencies.h"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "BitStream.h"
|
||||
#include "CDClientDatabase.h"
|
||||
#include "Character.h"
|
||||
#include "CharacterComponent.h"
|
||||
#include "ControllablePhysicsComponent.h"
|
||||
#include "Entity.h"
|
||||
#include "EntityManager.h"
|
||||
#include "MovingPlatformComponent.h"
|
||||
#include "PositionUpdate.h"
|
||||
#include "SimplePhysicsComponent.h"
|
||||
#include "Zone.h"
|
||||
#include "dZoneManager.h"
|
||||
|
||||
class PlatformRiderTest : public GameDependenciesTest {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
SetUpDependencies();
|
||||
CDClientDatabase::Connect(":memory:");
|
||||
CDClientDatabase::ExecuteDML("CREATE TABLE ComponentsRegistry (id INTEGER, component_type INTEGER, component_id INTEGER);");
|
||||
CDClientDatabase::ExecuteDML("CREATE TABLE Objects (id INTEGER, name TEXT, type TEXT, interactionDistance REAL);");
|
||||
CDClientDatabase::ExecuteDML("CREATE TABLE MovingPlatforms (id INTEGER, platformIsSimpleMover INTEGER, platformMoveX REAL, platformMoveY REAL, platformMoveZ REAL, platformMoveTime REAL, platformStartAtEnd INTEGER, description TEXT);");
|
||||
}
|
||||
void TearDown() override { TearDownDependencies(); }
|
||||
|
||||
// A mover at (100, 0, 0) going +x at 5 units a second after its 0.1 s wait at the first waypoint
|
||||
Entity* MakePlatform() {
|
||||
Path path;
|
||||
path.pathType = PathType::MovingPlatform;
|
||||
path.pathName = "RiderTestPath";
|
||||
path.pathBehavior = PathBehavior::Once;
|
||||
PathWaypoint first;
|
||||
first.position = NiPoint3(100.0f, 0.0f, 0.0f);
|
||||
first.speed = 5.0f;
|
||||
PathWaypoint second = first;
|
||||
second.position = NiPoint3(150.0f, 0.0f, 0.0f);
|
||||
path.pathWaypoints = { first, second };
|
||||
const_cast<Zone*>(Game::zoneManager->GetZone())->AddPath(path);
|
||||
|
||||
auto platformInfo = info;
|
||||
platformInfo.pos = first.position;
|
||||
auto* const platform = Game::entityManager->CreateEntity(platformInfo, nullptr, nullptr, false, 0x40040000000aa01LL);
|
||||
platform->AddComponent<SimplePhysicsComponent>(-1);
|
||||
platform->AddComponent<MovingPlatformComponent>(-1, "RiderTestPath");
|
||||
return platform;
|
||||
}
|
||||
|
||||
static SystemAddress ClientAddress() {
|
||||
SystemAddress address;
|
||||
address.binaryAddress = 0x7f000001;
|
||||
address.port = 2003;
|
||||
return address;
|
||||
}
|
||||
|
||||
static PositionUpdate Update(const NiPoint3& world, const LWOOBJID platform, const NiPoint3& offset) {
|
||||
PositionUpdate update;
|
||||
update.position = world;
|
||||
update.onGround = true;
|
||||
update.hasLocalSpaceInfo = platform != LWOOBJID_EMPTY;
|
||||
update.localSpaceInfo.objectId = platform;
|
||||
update.localSpaceInfo.position = offset;
|
||||
return update;
|
||||
}
|
||||
};
|
||||
|
||||
// The server's position of a rider is the platform's here plus the offset, and goes with the platform between updates
|
||||
TEST_F(PlatformRiderTest, RiderGoesWithThePlatform) {
|
||||
auto* const platform = MakePlatform();
|
||||
auto playerInfo = info;
|
||||
playerInfo.lot = 1;
|
||||
Entity player(32, playerInfo);
|
||||
Character character(1, nullptr);
|
||||
player.AddComponent<CharacterComponent>(-1, &character, ClientAddress());
|
||||
auto* const physics = player.AddComponent<ControllablePhysicsComponent>(-1);
|
||||
ASSERT_TRUE(player.IsPlayer());
|
||||
|
||||
// The client's world position is a little behind its own view of the platform; the server places it on its own
|
||||
auto update = Update(NiPoint3(99.0f, 2.0f, 1.0f), platform->GetObjectID(), NiPoint3(-1.0f, 2.0f, 1.0f));
|
||||
player.ProcessPositionUpdate(update);
|
||||
EXPECT_EQ(physics->GetPosition(), NiPoint3(99.0f, 2.0f, 1.0f));
|
||||
EXPECT_TRUE(physics->GetHasLocalSpaceInfo());
|
||||
|
||||
auto* const mover = platform->GetComponent<MovingPlatformComponent>();
|
||||
mover->Run(0.11f);
|
||||
mover->Run(1.0f);
|
||||
const auto platformAt = platform->GetPosition();
|
||||
ASSERT_GT(platformAt.x, 103.0f);
|
||||
physics->Run(0.0f);
|
||||
EXPECT_EQ(physics->GetPosition(), platformAt + NiPoint3(-1.0f, 2.0f, 1.0f));
|
||||
|
||||
// Off the platform: the update's own world position
|
||||
auto off = Update(NiPoint3(10.0f, 0.0f, 10.0f), LWOOBJID_EMPTY, NiPoint3Constant::ZERO);
|
||||
player.ProcessPositionUpdate(off);
|
||||
EXPECT_FALSE(physics->GetHasLocalSpaceInfo());
|
||||
mover->Run(1.0f);
|
||||
physics->Run(0.0f);
|
||||
EXPECT_EQ(physics->GetPosition(), NiPoint3(10.0f, 0.0f, 10.0f));
|
||||
}
|
||||
|
||||
// The platform is not on this server (not loaded, or gone): the client's world position is used
|
||||
TEST_F(PlatformRiderTest, UnknownPlatformKeepsTheClientsPosition) {
|
||||
auto playerInfo = info;
|
||||
playerInfo.lot = 1;
|
||||
Entity player(32, playerInfo);
|
||||
Character character(1, nullptr);
|
||||
player.AddComponent<CharacterComponent>(-1, &character, ClientAddress());
|
||||
auto* const physics = player.AddComponent<ControllablePhysicsComponent>(-1);
|
||||
ASSERT_TRUE(player.IsPlayer());
|
||||
auto update = Update(NiPoint3(5.0f, 6.0f, 7.0f), 0x40040000000aa99LL, NiPoint3(1.0f, 1.0f, 1.0f));
|
||||
player.ProcessPositionUpdate(update);
|
||||
EXPECT_EQ(physics->GetPosition(), NiPoint3(5.0f, 6.0f, 7.0f));
|
||||
physics->Run(0.0f);
|
||||
EXPECT_EQ(physics->GetPosition(), NiPoint3(5.0f, 6.0f, 7.0f));
|
||||
EXPECT_TRUE(physics->GetHasLocalSpaceInfo());
|
||||
}
|
||||
|
||||
// Moved by the server (a teleport) while riding: it comes off the platform
|
||||
TEST_F(PlatformRiderTest, TeleportTakesTheRiderOff) {
|
||||
auto* const platform = MakePlatform();
|
||||
Entity npc(0x40040000000aa02LL, info);
|
||||
auto* const physics = npc.AddComponent<ControllablePhysicsComponent>(-1);
|
||||
LocalSpaceInfo onPlatform;
|
||||
onPlatform.objectId = platform->GetObjectID();
|
||||
onPlatform.position = NiPoint3(0.0f, 1.0f, 0.0f);
|
||||
physics->SetPosition(NiPoint3(100.0f, 1.0f, 0.0f));
|
||||
physics->SetLocalSpace(true, onPlatform);
|
||||
|
||||
auto* const mover = platform->GetComponent<MovingPlatformComponent>();
|
||||
mover->Run(0.11f);
|
||||
mover->Run(1.0f);
|
||||
physics->Run(0.0f);
|
||||
EXPECT_EQ(physics->GetPosition(), platform->GetPosition() + NiPoint3(0.0f, 1.0f, 0.0f));
|
||||
|
||||
physics->SetPosition(NiPoint3(-50.0f, 0.0f, 0.0f));
|
||||
physics->Run(0.0f);
|
||||
EXPECT_FALSE(physics->GetHasLocalSpaceInfo());
|
||||
mover->Run(1.0f);
|
||||
physics->Run(0.0f);
|
||||
EXPECT_EQ(physics->GetPosition(), NiPoint3(-50.0f, 0.0f, 0.0f));
|
||||
}
|
||||
|
||||
// The block as the client writes it, after the angular velocity bit: 1, object id, offset, then the velocity bit and
|
||||
// the velocity only when its square length is over float epsilon
|
||||
TEST_F(PlatformRiderTest, LocalSpaceBlockSerializedAsTheClientWritesIt) {
|
||||
Entity entity(0x40040000000aa03LL, info);
|
||||
auto* const physics = entity.AddComponent<ControllablePhysicsComponent>(-1);
|
||||
RakNet::BitStream none;
|
||||
physics->Serialize(none, true);
|
||||
|
||||
LocalSpaceInfo block;
|
||||
block.objectId = 0x40040000000aa01LL;
|
||||
block.position = NiPoint3(-4.78f, 1.88f, -0.65f);
|
||||
block.linearVelocity = NiPoint3(1e-5f, 0.0f, 0.0f); // square length under epsilon: not written
|
||||
physics->SetLocalSpace(true, block);
|
||||
RakNet::BitStream still;
|
||||
physics->Serialize(still, true);
|
||||
EXPECT_EQ(still.GetNumberOfBitsUsed(), none.GetNumberOfBitsUsed() + 64 + 3 * 32 + 1);
|
||||
|
||||
block.linearVelocity = NiPoint3(11.44f, 10.55f, -3.0f);
|
||||
physics->SetLocalSpace(true, block);
|
||||
RakNet::BitStream moving;
|
||||
physics->Serialize(moving, true);
|
||||
ASSERT_EQ(moving.GetNumberOfBitsUsed(), none.GetNumberOfBitsUsed() + 64 + 6 * 32 + 1);
|
||||
|
||||
// Read back from where the block starts (a construction ends with it, so the no-block one ends with its 0 bit)
|
||||
moving.SetReadOffset(none.GetNumberOfBitsUsed() - 1);
|
||||
bool has{};
|
||||
LWOOBJID id{};
|
||||
NiPoint3 offset, velocity;
|
||||
bool hasVelocity{};
|
||||
ASSERT_TRUE(moving.Read(has));
|
||||
EXPECT_TRUE(has);
|
||||
moving.Read(id);
|
||||
moving.Read(offset.x); moving.Read(offset.y); moving.Read(offset.z);
|
||||
moving.Read(hasVelocity);
|
||||
moving.Read(velocity.x); moving.Read(velocity.y); moving.Read(velocity.z);
|
||||
EXPECT_EQ(id, block.objectId);
|
||||
EXPECT_EQ(offset, block.position);
|
||||
EXPECT_TRUE(hasVelocity);
|
||||
EXPECT_EQ(velocity, block.linearVelocity);
|
||||
}
|
||||
Reference in New Issue
Block a user