Files
DarkflameServer/tests/dGameTests/dComponentsTests/MovementAIPathTests.cpp
Aaron Kimbrell e158fc4ede fix(movement): a bounce path turns round at its ends without reaching them twice
PathFollowWaypointsHelper goes on to waypoint 1, or to the one before the last, when it runs off an
end of a bounce path. Here the turned path started with the waypoint the object stood on, so that waypoint was
reached again at once and its commands ran twice.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-10 20:36:57 -06:00

139 lines
4.9 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 "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);");
}
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].commands.push_back({ eWaypointCommandType::CHANGE_WAYPOINT, "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].commands.push_back({ eWaypointCommandType::CHANGE_WAYPOINT, "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;
}