// 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 #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(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(-1); platform->AddComponent(-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(-1, &character, ClientAddress()); auto* const physics = player.AddComponent(-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(); 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(-1, &character, ClientAddress()); auto* const physics = player.AddComponent(-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(-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(); 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(-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); }