Files
DarkflameServer/tests/dGameTests/dComponentsTests/MovementAIPathTests.cpp
Aaron Kimbrell f5a4dfab9a test(movement): movement AI and pathfinding serialize nothing
The client's movement AI and pathfinding control components read no
replica data; NPC movement reaches it through the physics component.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 05:17:19 -06:00

361 lines
14 KiB
C++

#include "GameDependencies.h"
#include <gtest/gtest.h>
#include "CDClientDatabase.h"
#include "CDComponentsRegistryTable.h"
#include "dZoneManager.h"
#include "Entity.h"
#include "MovementAIComponent.h"
#include "PathfindingComponent.h"
#include "BitStream.h"
#include "SimplePhysicsComponent.h"
#include "Zone.h"
#include "ZoneFile.h"
#include <vector>
// An object walking a level path, as the client's PathFollowWaypointsHelper and LWOMovementAIComponent do it
class MovementAIPathTest : public GameDependenciesTest {
protected:
void SetUp() override {
SetUpDependencies();
if (!CDClientDatabase::isConnected) {
CDClientDatabase::Connect(":memory:");
CDClientDatabase::ExecuteDML("CREATE TABLE ComponentsRegistry (id INTEGER, component_type INTEGER, component_id INTEGER);");
}
// No rows: the default gravity (the bounce action's arc)
CDClientDatabase::ExecuteDML("CREATE TABLE IF NOT EXISTS WorldConfig (pegravityvalue REAL);");
CDClientManager::GetEntriesMutable<CDComponentsRegistryTable>().insert_or_assign(static_cast<uint64_t>(info.lot), 0);
}
void TearDown() override {
TearDownDependencies();
}
// A path along x: waypoint i is at x = 10 * (i + 1)
static Path MakePath(const std::string& name, const uint32_t count, const PathBehavior behavior) {
Path path{};
path.pathName = name;
path.pathType = PathType::Movement;
path.pathBehavior = behavior;
for (uint32_t i = 0; i < count; i++) {
auto& waypoint = path.pathWaypoints.emplace_back();
waypoint.position = { 10.0f * static_cast<float>(i + 1), 0.0f, 0.0f };
}
path.waypointCount = count;
return path;
}
static void AddPath(const Path& path) {
const_cast<Zone*>(Game::zoneManager->GetZone())->AddPath(path);
}
// The walker, telling which waypoints it reached and where
MovementAIComponent* Walker(Entity& entity) {
entity.AddComponent<SimplePhysicsComponent>(-1);
auto* const movement = entity.AddComponent<MovementAIComponent>(-1, MovementAIInfo{});
movement->AddWaypointReachedCallback([this, &entity](const uint32_t waypoint) {
reached.push_back(waypoint);
reachedX.push_back(entity.GetPosition().x);
});
return movement;
}
static void Walk(MovementAIComponent& movement, const float seconds) {
for (float time = 0.0f; time < seconds; time += 0.05f) movement.Run(0.05f);
}
std::vector<uint32_t> reached;
std::vector<float> reachedX;
};
TEST_F(MovementAIPathTest, PathIsWalkedFromItsStartWaypoint) {
AddPath(MakePath("StartPath", 4, PathBehavior::Once));
Entity entity(300, info);
auto* const movement = Walker(entity);
movement->SetPath(Game::zoneManager->GetZone()->GetPath("StartPath"), 2);
Walk(*movement, 30.0f);
ASSERT_EQ(reached, (std::vector<uint32_t>{ 2, 3 }));
EXPECT_NEAR(reachedX[0], 30.0f, 0.01f);
EXPECT_NEAR(reachedX[1], 40.0f, 0.01f);
}
TEST_F(MovementAIPathTest, StartWaypointPastTheEndWalksTheWholePath) {
AddPath(MakePath("ShortPath", 2, PathBehavior::Once));
Entity entity(301, info);
auto* const movement = Walker(entity);
movement->SetPath(Game::zoneManager->GetZone()->GetPath("ShortPath"), 9);
Walk(*movement, 30.0f);
EXPECT_EQ(reached, (std::vector<uint32_t>{ 0, 1 }));
}
// Avant Gardens Survival sends its enemies on with "RaceTrack_01,6": the other path, from its waypoint 6
TEST_F(MovementAIPathTest, ChangeWaypointCommandGoesToTheNamedWaypointOfTheOtherPath) {
auto first = MakePath("FirstPath", 2, PathBehavior::Once);
first.pathWaypoints[1].actions.push_back({ "changeWP", "SecondPath,2" });
AddPath(first);
auto second = MakePath("SecondPath", 4, PathBehavior::Once);
for (auto& waypoint : second.pathWaypoints) waypoint.position.z = 50.0f;
AddPath(second);
Entity entity(302, info);
auto* const movement = Walker(entity);
movement->SetPath("FirstPath");
Walk(*movement, 60.0f);
ASSERT_EQ(reached, (std::vector<uint32_t>{ 0, 1, 2, 3 }));
// Waypoints 2 and 3 are the second path's
EXPECT_NEAR(reachedX[2], 30.0f, 0.01f);
EXPECT_NEAR(reachedX[3], 40.0f, 0.01f);
EXPECT_NEAR(entity.GetPosition().z, 50.0f, 0.01f);
}
TEST_F(MovementAIPathTest, ChangeWaypointCommandWithoutAWaypointStartsAtTheFirst) {
auto first = MakePath("LonePath", 1, PathBehavior::Once);
first.pathWaypoints[0].actions.push_back({ "changeWP", "OtherPath" });
AddPath(first);
AddPath(MakePath("OtherPath", 3, PathBehavior::Once));
Entity entity(303, info);
auto* const movement = Walker(entity);
movement->SetPath("LonePath");
Walk(*movement, 60.0f);
EXPECT_EQ(reached, (std::vector<uint32_t>{ 0, 0, 1, 2 }));
}
// PathFollowWaypointsHelper turns round at an end: the end waypoint is reached once, then the one before it
TEST_F(MovementAIPathTest, BouncePathReachesEachEndOnce) {
AddPath(MakePath("BouncePath", 3, PathBehavior::Bounce));
Entity entity(304, info);
auto* const movement = Walker(entity);
movement->SetPath("BouncePath");
Walk(*movement, 120.0f);
ASSERT_GE(reached.size(), 9u);
reached.resize(9);
EXPECT_EQ(reached, (std::vector<uint32_t>{ 0, 1, 2, 1, 0, 1, 2, 1, 0 }));
const std::vector<float> expectedX{ 10.0f, 20.0f, 30.0f, 20.0f, 10.0f, 20.0f, 30.0f, 20.0f, 10.0f };
for (size_t i = 0; i < expectedX.size(); i++) EXPECT_NEAR(reachedX[i], expectedX[i], 0.01f) << i;
}
// LWOMovementAIComponent::ProcessDelayAction: the object stands at the waypoint for the delay, and the actions after
// the delay wait for it
TEST_F(MovementAIPathTest, DelayCommandHoldsTheObjectAtTheWaypoint) {
auto path = MakePath("DelayPath", 2, PathBehavior::Once);
path.pathWaypoints[0].actions.push_back({ "delay", "5" });
path.pathWaypoints[0].actions.push_back({ "changeWP", "AfterDelay,1" });
AddPath(path);
auto after = MakePath("AfterDelay", 2, PathBehavior::Once);
for (auto& waypoint : after.pathWaypoints) waypoint.position.z = 50.0f;
AddPath(after);
Entity entity(305, info);
auto* const movement = Walker(entity);
movement->SetPath("DelayPath");
while (reached.empty()) movement->Run(0.05f);
ASSERT_EQ(reached, (std::vector<uint32_t>{ 0 }));
// Four seconds on it has not moved: the command after the delay has not run either
Walk(*movement, 4.0f);
EXPECT_NEAR(entity.GetPosition().x, 10.0f, 0.01f);
EXPECT_NEAR(entity.GetPosition().z, 0.0f, 0.01f);
EXPECT_EQ(movement->GetVelocity(), NiPoint3Constant::ZERO);
// The delay over, the next command sends it to the other path; the waypoint is not reached a second time
Walk(*movement, 60.0f);
EXPECT_EQ(reached, (std::vector<uint32_t>{ 0, 1 }));
EXPECT_NEAR(entity.GetPosition().x, 20.0f, 0.01f);
EXPECT_NEAR(entity.GetPosition().z, 50.0f, 0.01f);
}
TEST_F(MovementAIPathTest, DelayAtEachEndOfALoopPath) {
auto path = MakePath("DelayLoop", 2, PathBehavior::Loop);
path.pathWaypoints[1].actions.push_back({ "delay", "2" });
AddPath(path);
Entity entity(306, info);
auto* const movement = Walker(entity);
movement->SetPath("DelayLoop");
Walk(*movement, 120.0f);
ASSERT_GE(reached.size(), 4u);
reached.resize(4);
EXPECT_EQ(reached, (std::vector<uint32_t>{ 0, 1, 0, 1 }));
}
// A waypoint with any actions holds it until ContinueWaypoints; here its script runs them (the Lua AI)
TEST_F(MovementAIPathTest, WaypointWithActionsWaitsForContinueWaypoints) {
auto path = MakePath("HoldPath", 3, PathBehavior::Once);
path.pathWaypoints[1].actions.push_back({ "setvar", "" });
AddPath(path);
Entity entity(307, info);
auto* const movement = Walker(entity);
movement->SetScriptRunsWaypointActions(true);
movement->SetPath("HoldPath");
Walk(*movement, 30.0f);
ASSERT_EQ(reached, (std::vector<uint32_t>{ 0, 1 }));
EXPECT_TRUE(movement->IsWaitingAtWaypoint());
EXPECT_NEAR(entity.GetPosition().x, 20.0f, 0.01f);
movement->ContinueWaypoints();
// A second one does nothing
movement->ContinueWaypoints();
Walk(*movement, 30.0f);
EXPECT_EQ(reached, (std::vector<uint32_t>{ 0, 1, 2 }));
EXPECT_NEAR(entity.GetPosition().x, 30.0f, 0.01f);
}
// The movement AI runs them itself and sends ContinueWaypoints once all of them continue
TEST_F(MovementAIPathTest, ContinuingActionsGoOnAtOnce) {
auto path = MakePath("ContinuePath", 3, PathBehavior::Once);
path.pathWaypoints[1].actions.push_back({ "setvar", "x" });
path.pathWaypoints[1].actions.push_back({ "castskill", "" });
AddPath(path);
Entity entity(308, info);
auto* const movement = Walker(entity);
movement->SetPath("ContinuePath");
Walk(*movement, 30.0f);
EXPECT_EQ(reached, (std::vector<uint32_t>{ 0, 1, 2 }));
EXPECT_FALSE(movement->IsWaitingAtWaypoint());
}
// A name the client does not know pauses the list until the next frame
TEST_F(MovementAIPathTest, UnknownActionPausesOneFrame) {
Entity entity(309, info);
auto* const movement = Walker(entity);
EXPECT_FALSE(movement->RunWaypointAction("notAnAction", "1"));
EXPECT_FLOAT_EQ(movement->GetWaypointActionDelay(), 0.0f);
}
TEST_F(MovementAIPathTest, ActionsThatContinue) {
Entity entity(310, info);
auto* const movement = Walker(entity);
for (const auto* const name : { "setvar", "castskill", "eqInvent", "unInvent", "pathspeed", "removeNPC", "changeWP", "playSound", "groupemote" }) {
EXPECT_TRUE(movement->RunWaypointAction(name, "")) << name;
}
}
TEST_F(MovementAIPathTest, DelayAndEmoteActionsPause) {
Entity entity(311, info);
auto* const movement = Walker(entity);
EXPECT_FALSE(movement->RunWaypointAction("delay", "2.5"));
EXPECT_FLOAT_EQ(movement->GetWaypointActionDelay(), 2.5f);
// The emote's wait is its animation's time (none without a render component)
EXPECT_FALSE(movement->RunWaypointAction("emote", "5"));
EXPECT_FLOAT_EQ(movement->GetWaypointActionDelay(), 0.0f);
EXPECT_FALSE(movement->RunWaypointAction("femote", "5"));
}
// stop: StopMoving, and the path is not followed on
TEST_F(MovementAIPathTest, StopActionLeavesItStanding) {
auto path = MakePath("StopPath", 3, PathBehavior::Once);
path.pathWaypoints[1].actions.push_back({ "stop", "" });
AddPath(path);
Entity entity(312, info);
auto* const movement = Walker(entity);
movement->SetPath("StopPath");
Walk(*movement, 30.0f);
EXPECT_EQ(reached, (std::vector<uint32_t>{ 0, 1 }));
EXPECT_TRUE(movement->AtFinalWaypoint());
EXPECT_NEAR(entity.GetPosition().x, 20.0f, 0.01f);
}
TEST_F(MovementAIPathTest, TeleportActionMovesIt) {
Entity entity(313, info);
auto* const movement = Walker(entity);
EXPECT_TRUE(movement->RunWaypointAction("teleport", "1,2;3"));
EXPECT_EQ(entity.GetPosition(), (NiPoint3{ 1.0f, 2.0f, 3.0f }));
}
// SetPathingSpeed: below 0 is 0, and at 0 it stands
TEST_F(MovementAIPathTest, PathSpeedActionBelowZeroStandsIt) {
auto path = MakePath("SlowPath", 3, PathBehavior::Once);
path.pathWaypoints[0].actions.push_back({ "pathspeed", "-2" });
AddPath(path);
Entity entity(314, info);
auto* const movement = Walker(entity);
movement->SetPath("SlowPath");
Walk(*movement, 30.0f);
EXPECT_EQ(reached, (std::vector<uint32_t>{ 0 }));
EXPECT_NEAR(entity.GetPosition().x, 10.0f, 0.01f);
}
TEST_F(MovementAIPathTest, PathSpeedActionScalesTheSpeed) {
auto path = MakePath("FastPath", 2, PathBehavior::Once);
path.pathWaypoints[0].actions.push_back({ "pathspeed", "2" });
AddPath(path);
Entity slowEntity(315, info);
auto* const slow = Walker(slowEntity);
slow->SetPath("FastPath");
while (reached.empty()) slow->Run(0.05f);
Walk(*slow, 0.5f);
const auto fastX = slowEntity.GetPosition().x;
AddPath(MakePath("PlainPath", 2, PathBehavior::Once));
reached.clear();
Entity plainEntity(316, info);
auto* const plain = Walker(plainEntity);
plain->SetPath("PlainPath");
while (reached.empty()) plain->Run(0.05f);
Walk(*plain, 0.5f);
EXPECT_GT(fastX - 10.0f, plainEntity.GetPosition().x - 10.0f);
}
// bounce: a path of one waypoint pauses without a wait; otherwise it is thrown two waypoints on and waits 5 s
TEST_F(MovementAIPathTest, BounceActionThrowsItTwoWaypointsOn) {
auto lone = MakePath("LoneBounce", 1, PathBehavior::Once);
lone.pathWaypoints[0].actions.push_back({ "bounce", "10" });
AddPath(lone);
Entity loneEntity(317, info);
auto* const loneWalker = Walker(loneEntity);
loneWalker->SetPath("LoneBounce");
while (reached.empty()) loneWalker->Run(0.05f);
EXPECT_FLOAT_EQ(loneWalker->GetWaypointActionDelay(), 0.0f);
auto path = MakePath("BounceAction", 4, PathBehavior::Loop);
path.pathWaypoints[0].actions.push_back({ "bounce", "10" });
AddPath(path);
reached.clear();
Entity entity(318, info);
auto* const movement = Walker(entity);
movement->SetPath("BounceAction");
while (reached.empty()) movement->Run(0.05f);
EXPECT_FLOAT_EQ(movement->GetWaypointActionDelay(), MovementAIComponent::BOUNCE_ACTION_DELAY);
EXPECT_TRUE(movement->IsKnockedBack());
while (movement->IsKnockedBack()) movement->Run(0.05f);
// Waypoint 2
EXPECT_NEAR(entity.GetPosition().x, 30.0f, 1.0f);
}
// The last waypoint of a once path: its actions do not send it on
TEST_F(MovementAIPathTest, OncePathEndActionsStopIt) {
auto path = MakePath("EndPath", 2, PathBehavior::Once);
path.pathWaypoints[1].actions.push_back({ "delay", "1" });
AddPath(path);
Entity entity(319, info);
auto* const movement = Walker(entity);
movement->SetPath("EndPath");
Walk(*movement, 30.0f);
EXPECT_EQ(reached, (std::vector<uint32_t>{ 0, 1 }));
EXPECT_TRUE(movement->AtFinalWaypoint());
EXPECT_FALSE(movement->IsWaitingAtWaypoint());
}
// The client's movement AI and pathfinding control components read nothing from the replica: NPC movement goes
// through the physics component
TEST_F(MovementAIPathTest, MovementAIAndPathfindingSerializeNothing) {
Entity entity(320, info);
auto* const movement = Walker(entity);
auto* const pathfinding = entity.AddComponent<PathfindingComponent>(-1);
for (const bool construction : { true, false }) {
RakNet::BitStream movementBits;
movement->Serialize(movementBits, construction);
EXPECT_EQ(movementBits.GetNumberOfBitsUsed(), 0u);
RakNet::BitStream pathfindingBits;
pathfinding->Serialize(pathfindingBits, construction);
EXPECT_EQ(pathfindingBits.GetNumberOfBitsUsed(), 0u);
}
}