#include "GameDependencies.h" #include #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 // 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().insert_or_assign(static_cast(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(i + 1), 0.0f, 0.0f }; } path.waypointCount = count; return path; } static void AddPath(const Path& path) { const_cast(Game::zoneManager->GetZone())->AddPath(path); } // The walker, telling which waypoints it reached and where MovementAIComponent* Walker(Entity& entity) { entity.AddComponent(-1); auto* const movement = entity.AddComponent(-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 reached; std::vector 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{ 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{ 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{ 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{ 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{ 0, 1, 2, 1, 0, 1, 2, 1, 0 })); const std::vector 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 commands after // the delay wait for it TEST_F(MovementAIPathTest, DelayCommandHoldsTheObjectAtTheWaypoint) { auto path = MakePath("DelayPath", 2, PathBehavior::Once); path.pathWaypoints[0].commands.push_back({ eWaypointCommandType::DELAY, "5" }); path.pathWaypoints[0].commands.push_back({ eWaypointCommandType::CHANGE_WAYPOINT, "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{ 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{ 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].commands.push_back({ eWaypointCommandType::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{ 0, 1, 0, 1 })); }