From 154f53004f8297214c538307e5499396578b0602 Mon Sep 17 00:00:00 2001 From: Aaron Kimbrell Date: Fri, 27 Nov 2026 22:50:50 -0600 Subject: [PATCH] test(ai): shared Lua AI transitions, radii, tether and wander timing Co-Authored-By: Claude Opus 5.5 --- tests/dGameTests/CMakeLists.txt | 1 + tests/dGameTests/LuaAITests.cpp | 425 ++++++++++++++++++++++++++++++++ 2 files changed, 426 insertions(+) create mode 100644 tests/dGameTests/LuaAITests.cpp diff --git a/tests/dGameTests/CMakeLists.txt b/tests/dGameTests/CMakeLists.txt index 1939749c6..94f6ffd1e 100644 --- a/tests/dGameTests/CMakeLists.txt +++ b/tests/dGameTests/CMakeLists.txt @@ -14,6 +14,7 @@ set(DGAMETEST_SOURCES "KnockbackTests.cpp" "ShootingGalleryTests.cpp" "MedusaScriptsTests.cpp" + "LuaAITests.cpp" "CollisionFilterTests.cpp" "MovementBlockerTests.cpp" "ItemSetPassiveAbilityTests.cpp" diff --git a/tests/dGameTests/LuaAITests.cpp b/tests/dGameTests/LuaAITests.cpp new file mode 100644 index 000000000..e8d4820c0 --- /dev/null +++ b/tests/dGameTests/LuaAITests.cpp @@ -0,0 +1,425 @@ +// The shared Lua AI state machine (State, o_StateCreate, o_Main, o_mis, o_onEvent, o_Movement) on a fake object: +// the states and their transitions, the aggro radius and tether, and the wander timing. + +#include "LuaAIMachine.h" + +#include + +#include +#include +#include + +using namespace LuaAI; + +namespace { + constexpr LWOOBJID PLAYER = 100; + constexpr LWOOBJID OTHER_PLAYER = 101; + constexpr LWOOBJID FRIEND = 102; + constexpr uint32_t FIRST_SKILL = 11; + constexpr uint32_t LAST_SKILL = 239; + + struct GoToCall { NiPoint3 target; float speed; }; + struct FollowCall { LWOOBJID target; float radius; float speed; bool keepFollowing; }; + + class FakeHost final : public Host { + public: + NiPoint3 GetPosition() const override { return position; } + NiQuaternion GetRotation() const override { return rotation; } + void SetRotation(const NiQuaternion& value) override { rotation = value; } + void GoTo(const NiPoint3& target, const float speed) override { goTos.push_back({ target, speed }); } + void FollowTarget(const LWOOBJID target, const float radius, const float speed, const bool keep) override { follows.push_back({ target, radius, speed, keep }); } + void FaceTarget(LWOOBJID) override {} + void StopPathing() override { stopped = true; } + void AddTimer(const std::string& name, const float time) override { timers[name] = time; } + void CancelTimer(const std::string& name) override { timers.erase(name); } + void SetProximityRadius(const float radius, const std::string& name) override { proximities[name] = radius; } + std::vector GetProximityObjects(const std::string& name) const override { return name == AGGRO_PROXIMITY ? inAggroRadius : std::vector{}; } + bool Exists(const LWOOBJID object) const override { return types.contains(object); } + bool IsDead(const LWOOBJID object) const override { return !Exists(object) || dead.contains(object); } + bool IsSelfDead() const override { return selfDead; } + bool IsEnemy(const LWOOBJID object) const override { return enemies.contains(object); } + std::string GetObjectType(const LWOOBJID object) const override { const auto it = types.find(object); return it == types.end() ? "" : it->second; } + std::vector GetSkills() const override { return skills; } + float GetSkillCooldown(const uint32_t skill) const override { return skill == FIRST_SKILL ? 1.5f : 3.0f; } + void CastSkill(const uint32_t skill, const LWOOBJID target) override { casts.push_back({ skill, target }); } + void SetImmunity(const bool value) override { immune = value; } + void RestoreHealth() override { healed++; } + void SetMaxHealth(const int32_t value) override { maxHealth = value; } + void PlayEmote(const std::u16string& type) override { emotes.push_back(type); } + void DieSilently() override { diedSilently = true; } + int32_t Random(const int32_t min, const int32_t max) override { + randomRanges.push_back({ min, max }); + if (rolls.empty()) return min; + const auto roll = rolls.front(); + rolls.pop_front(); + return roll; + } + + NiPoint3 position{ 10.0f, 2.0f, 20.0f }; + NiQuaternion rotation = QuatUtils::IDENTITY; + std::vector goTos; + std::vector follows; + std::map timers; + std::map proximities; + std::vector inAggroRadius; + std::map types{ { PLAYER, "NPC" }, { OTHER_PLAYER, "NPC" }, { FRIEND, "Smashables" } }; + std::set enemies{ PLAYER, OTHER_PLAYER, FRIEND }; + std::set dead; + bool selfDead = false; + std::vector skills{ FIRST_SKILL, LAST_SKILL }; + std::vector> casts; + bool immune = false; + int32_t healed = 0; + int32_t maxHealth = 0; + std::vector emotes; + bool diedSilently = false; + bool stopped = false; + std::deque rolls; + std::vector> randomRanges; + }; + + // L_WILD_MEDUSA.lua's Set table + Settings Wanderer() { + Settings set{}; + set.aggroRadius = 15.0f; + set.tetherRadius = 50.0f; + set.tetherSpeed = 1.0f; + set.wanderRadius = 25; + set.aggression = Aggression::Aggressive; + set.aggroDist = 8.0f; + set.aggroSpeed = 1.0f; + set.movementType = MovementType::Wander; + set.wanderChance = 100; + set.wanderDelayMin = 1; + set.wanderDelayMax = 10; + set.wanderSpeed = 0.65f; + return set; + } + + // A machine started on the host, in the aggro state on PLAYER + void Aggro(FakeHost& host, Machine& machine) { + machine.Start(); + machine.OnProximityEnter(PLAYER, AGGRO_PROXIMITY); + ASSERT_EQ(machine.GetState(), State::Aggro); + host.goTos.clear(); + host.follows.clear(); + } +}; + +TEST(LuaAITest, WandererStartsMeanderingWithItsAggroProximity) { + FakeHost host; + host.rolls = { 7, -25 }; + Machine machine(host, Wanderer()); + machine.Start(); + EXPECT_EQ(machine.GetState(), State::Meander); + EXPECT_FLOAT_EQ(host.proximities.at(AGGRO_PROXIMITY), 15.0f); + // math.random(-r, r) on x, then z, round the spawn point at its height + ASSERT_EQ(host.goTos.size(), 1u); + EXPECT_EQ(host.goTos[0].target, NiPoint3(17.0f, 2.0f, -5.0f)); + EXPECT_FLOAT_EQ(host.goTos[0].speed, 0.65f); + ASSERT_EQ(host.randomRanges.size(), 2u); + EXPECT_EQ(host.randomRanges[0], std::make_pair(-25, 25)); + EXPECT_EQ(host.randomRanges[1], std::make_pair(-25, 25)); + // SkillTime: the first skill's cooldown; full health from loadOnce; no tether yet + EXPECT_EQ(machine.GetSkillTime(), std::optional(1.5f)); + EXPECT_EQ(host.healed, 1); + EXPECT_FLOAT_EQ(machine.GetTetherRadius(), 0.0f); + EXPECT_EQ(machine.GetMeanderPoint(), NiPoint3(10.0f, 2.0f, 20.0f)); +} + +TEST(LuaAITest, PassiveGuardStaysIdleWithoutProximity) { + FakeHost host; + auto set = Wanderer(); + set.aggression = Aggression::Passive; + set.movementType = MovementType::Guard; + Machine machine(host, set); + machine.Start(); + EXPECT_EQ(machine.GetState(), State::Idle); + EXPECT_TRUE(host.proximities.empty()); + EXPECT_TRUE(host.goTos.empty()); + // Guards have no Meander state: a MeanderPause does nothing + machine.OnTimerDone(Timers::MEANDER_PAUSE); + EXPECT_EQ(machine.GetState(), State::Idle); + // Passive: a hit does not start a fight + machine.OnHit(PLAYER); + EXPECT_EQ(machine.GetState(), State::Idle); +} + +TEST(LuaAITest, ArrivingPausesForARandomWholeDelayThenWandersOn) { + FakeHost host; + Machine machine(host, Wanderer()); + machine.Start(); + host.goTos.clear(); + host.randomRanges.clear(); + host.rolls = { 4 }; + machine.OnArrived(); + EXPECT_EQ(host.randomRanges.at(0), std::make_pair(1, 10)); + EXPECT_FLOAT_EQ(host.timers.at(Timers::MEANDER_PAUSE), 4.0f); + EXPECT_TRUE(host.goTos.empty()); + // The pause ends: Meander again, a new point round the same meander point + host.position = NiPoint3(30.0f, 2.0f, 30.0f); + host.rolls = { 1, 2 }; + machine.OnTimerDone(Timers::MEANDER_PAUSE); + EXPECT_EQ(machine.GetState(), State::Meander); + ASSERT_EQ(host.goTos.size(), 1u); + EXPECT_EQ(host.goTos[0].target, NiPoint3(11.0f, 2.0f, 22.0f)); +} + +TEST(LuaAITest, ADelayUnderOneIsKept) { + FakeHost host; + auto set = Wanderer(); + set.wanderDelayMin = 0; + set.wanderDelayMax = 0; + Machine machine(host, set); + machine.Start(); + machine.OnArrived(); + EXPECT_FLOAT_EQ(host.timers.at(Timers::MEANDER_PAUSE), 0.0f); +} + +TEST(LuaAITest, TwoWeightedWanderEmotes) { + FakeHost host; + auto set = Wanderer(); + set.wanderEmote = true; + set.wanderEmoteTwo = true; + set.wanderEmoteWeight = 30; + set.wanderEmoteType1 = u"salute"; + set.wanderEmoteType2 = u"wave"; + Machine machine(host, set); + machine.Start(); + // delay, chance roll, emote roll + host.rolls = { 3, 50, 30 }; + machine.OnArrived(); + host.rolls = { 3, 50, 31 }; + machine.OnArrived(); + EXPECT_EQ(host.emotes, (std::vector{ u"salute", u"wave" })); + // One emote: the original errors before playing it + set.wanderEmoteTwo = false; + FakeHost single; + Machine one(single, set); + one.Start(); + one.OnArrived(); + EXPECT_TRUE(single.emotes.empty()); +} + +TEST(LuaAITest, AnEnemyInTheAggroRadiusIsChased) { + FakeHost host; + Machine machine(host, Wanderer()); + machine.Start(); + machine.OnArrived(); + ASSERT_TRUE(host.timers.contains(Timers::MEANDER_PAUSE)); + host.position = NiPoint3(5.0f, 2.0f, 5.0f); + machine.OnProximityEnter(PLAYER, AGGRO_PROXIMITY); + EXPECT_EQ(machine.GetState(), State::Aggro); + EXPECT_EQ(machine.GetAttackingTarget(), PLAYER); + EXPECT_EQ(machine.GetAggroTarget(), 1); + // The fight starts from here: home and a tetherRadius tether round it + EXPECT_EQ(machine.GetHomePoint(), NiPoint3(5.0f, 2.0f, 5.0f)); + EXPECT_EQ(machine.GetTetherPoint(), NiPoint3(5.0f, 2.0f, 5.0f)); + EXPECT_FLOAT_EQ(machine.GetTetherRadius(), 50.0f); + // FollowTarget { radius = AggroDist, speed = AggroSpeed * 2, keepFollowing = true } + ASSERT_EQ(host.follows.size(), 1u); + EXPECT_EQ(host.follows[0].target, PLAYER); + EXPECT_FLOAT_EQ(host.follows[0].radius, 8.0f); + EXPECT_FLOAT_EQ(host.follows[0].speed, 2.0f); + EXPECT_TRUE(host.follows[0].keepFollowing); + EXPECT_FLOAT_EQ(host.timers.at(Timers::ATTACK_HEART_BEAT), INITIAL_ATTACK_TIME); + EXPECT_FALSE(host.timers.contains(Timers::MEANDER_PAUSE)); + EXPECT_FALSE(machine.GetReadyToAttack()); +} + +TEST(LuaAITest, OnlyAttackableObjectsInTheAggroProximityStartAFight) { + FakeHost host; + Machine machine(host, Wanderer()); + machine.Start(); + // Another proximity, a type it does not fight, a friend, a dead one + machine.OnProximityEnter(PLAYER, "conductRadius"); + machine.OnProximityEnter(FRIEND, AGGRO_PROXIMITY); + host.enemies.erase(OTHER_PLAYER); + machine.OnProximityEnter(OTHER_PLAYER, AGGRO_PROXIMITY); + host.dead.insert(PLAYER); + machine.OnProximityEnter(PLAYER, AGGRO_PROXIMITY); + EXPECT_EQ(machine.GetState(), State::Meander); + // Without the default aggro FollowTarget values + auto set = Wanderer(); + set.aggroDist.reset(); + FakeHost other; + Machine noDist(other, set); + noDist.Start(); + noDist.OnProximityEnter(PLAYER, AGGRO_PROXIMITY); + ASSERT_EQ(other.follows.size(), 1u); + EXPECT_FLOAT_EQ(other.follows[0].radius, DEFAULT_AGGRO_DIST); + EXPECT_FLOAT_EQ(other.follows[0].speed, DEFAULT_AGGRO_SPEED); +} + +TEST(LuaAITest, TheHeartBeatCastsTheLastSkillEverySkillTime) { + FakeHost host; + Machine machine(host, Wanderer()); + Aggro(host, machine); + // Not ready before the first heartbeat + machine.OnArrived(); + EXPECT_TRUE(host.casts.empty()); + EXPECT_FALSE(machine.OnStateTimerDone(Timers::MEANDER_PAUSE)); + EXPECT_TRUE(machine.OnStateTimerDone(Timers::ATTACK_HEART_BEAT)); + ASSERT_EQ(host.casts.size(), 1u); + EXPECT_EQ(host.casts[0], std::make_pair(LAST_SKILL, LWOOBJID_EMPTY)); + EXPECT_FLOAT_EQ(host.timers.at(Timers::ATTACK_HEART_BEAT), 1.5f); + EXPECT_FALSE(machine.GetReadyToAttack()); + // An override, and the target passed on + auto set = Wanderer(); + set.overrideAttackSkill = true; + set.attackSkill = 55; + set.useOptionalTargetOnAttack = true; + FakeHost other; + Machine overridden(other, set); + Aggro(other, overridden); + overridden.OnStateTimerDone(Timers::ATTACK_HEART_BEAT); + EXPECT_EQ(other.casts.at(0), std::make_pair(55u, PLAYER)); +} + +TEST(LuaAITest, ALostTargetMovesOnToTheNextOneInTheRadius) { + FakeHost host; + Machine machine(host, Wanderer()); + Aggro(host, machine); + host.dead.insert(PLAYER); + host.inAggroRadius = { PLAYER, FRIEND, OTHER_PLAYER }; + machine.OnStateTimerDone(Timers::ATTACK_HEART_BEAT); + EXPECT_TRUE(host.casts.empty()); + EXPECT_EQ(machine.GetState(), State::Aggro); + EXPECT_EQ(machine.GetAttackingTarget(), OTHER_PLAYER); + ASSERT_EQ(host.follows.size(), 1u); + EXPECT_EQ(host.follows[0].target, OTHER_PLAYER); +} + +TEST(LuaAITest, WithNoTargetLeftItTethersHomeAndWandersAgain) { + FakeHost host; + Machine machine(host, Wanderer()); + host.position = NiPoint3(0.0f, 0.0f, 0.0f); + Aggro(host, machine); + host.position = NiPoint3(20.0f, 0.0f, 0.0f); + host.dead.insert(PLAYER); + const auto healed = host.healed; + machine.OnStateTimerDone(Timers::ATTACK_HEART_BEAT); + EXPECT_EQ(machine.GetState(), State::Tether); + EXPECT_EQ(machine.GetAggroTarget(), 2); + EXPECT_TRUE(host.immune); + ASSERT_EQ(host.goTos.size(), 1u); + EXPECT_EQ(host.goTos[0].target, NiPoint3(0.0f, 0.0f, 0.0f)); + EXPECT_FLOAT_EQ(host.goTos[0].speed, 1.0f); + // While it tethers nothing starts a fight + host.dead.clear(); + machine.OnProximityEnter(OTHER_PLAYER, AGGRO_PROXIMITY); + machine.OnHit(OTHER_PLAYER); + EXPECT_EQ(machine.GetState(), State::Tether); + // Home: full health, no immunity, the tether off, Meander again + host.position = NiPoint3(0.0f, 0.0f, 0.0f); + machine.OnArrived(); + EXPECT_EQ(host.healed, healed + 1); + EXPECT_FALSE(host.immune); + EXPECT_EQ(machine.GetAggroTarget(), 0); + EXPECT_EQ(machine.GetAttackingTarget(), LWOOBJID_EMPTY); + EXPECT_FLOAT_EQ(machine.GetTetherRadius(), 0.0f); + EXPECT_EQ(machine.GetState(), State::Meander); +} + +TEST(LuaAITest, LeavingTheTetherRadiusSendsItHome) { + FakeHost host; + Machine machine(host, Wanderer()); + host.position = NiPoint3(0.0f, 0.0f, 0.0f); + Aggro(host, machine); + host.position = NiPoint3(49.0f, 0.0f, 0.0f); + machine.Update(); + EXPECT_EQ(machine.GetState(), State::Aggro); + const auto healed = host.healed; + host.position = NiPoint3(50.5f, 0.0f, 0.0f); + machine.Update(); + EXPECT_EQ(machine.GetState(), State::Tether); + EXPECT_EQ(host.healed, healed + 1); + EXPECT_EQ(machine.GetAttackingTarget(), LWOOBJID_EMPTY); + EXPECT_FALSE(host.timers.contains(Timers::ATTACK_HEART_BEAT)); + // Once: still outside on the next frame + host.goTos.clear(); + machine.Update(); + EXPECT_TRUE(host.goTos.empty()); +} + +TEST(LuaAITest, AHitStartsAFightFromWhereItIs) { + FakeHost host; + Machine machine(host, Wanderer()); + machine.Start(); + host.position = NiPoint3(3.0f, 0.0f, 4.0f); + machine.OnHit(PLAYER); + EXPECT_EQ(machine.GetState(), State::Aggro); + EXPECT_EQ(machine.GetAttackingTarget(), PLAYER); + EXPECT_EQ(machine.GetHomePoint(), NiPoint3(3.0f, 0.0f, 4.0f)); + EXPECT_EQ(machine.GetTetherPoint(), NiPoint3(3.0f, 0.0f, 4.0f)); + // A hit in the fight starts the aggro state over: the attack waits for a new first heartbeat + machine.OnStateTimerDone(Timers::ATTACK_HEART_BEAT); + EXPECT_EQ(host.casts.size(), 1u); + machine.OnHit(PLAYER); + EXPECT_FLOAT_EQ(host.timers.at(Timers::ATTACK_HEART_BEAT), INITIAL_ATTACK_TIME); + EXPECT_FALSE(machine.GetReadyToAttack()); +} + +TEST(LuaAITest, SuspendLuaAIHandsFightsToCombat) { + FakeHost host; + auto set = Wanderer(); + set.suspendLuaAI = true; + Machine machine(host, set); + machine.Start(); + machine.OnProximityEnter(PLAYER, AGGRO_PROXIMITY); + EXPECT_EQ(machine.GetState(), State::Combat); +} + +TEST(LuaAITest, AggroEmotePlaysBeforeTheFight) { + FakeHost host; + auto set = Wanderer(); + set.aggroEmote = true; + set.aggroEmoteType = u"roar"; + set.aggroEmoteDelay = 2.0f; + Machine machine(host, set); + machine.Start(); + machine.OnProximityEnter(PLAYER, AGGRO_PROXIMITY); + EXPECT_EQ(machine.GetState(), State::Idle); + EXPECT_EQ(host.emotes, std::vector{ u"roar" }); + EXPECT_FLOAT_EQ(host.timers.at(Timers::AGGRO_EMOTE), 2.0f); + machine.OnTimerDone(Timers::AGGRO_EMOTE); + EXPECT_EQ(machine.GetState(), State::Aggro); + EXPECT_EQ(machine.GetAttackingTarget(), PLAYER); +} + +TEST(LuaAITest, DeathEndsTheFight) { + FakeHost host; + Machine machine(host, Wanderer()); + Aggro(host, machine); + machine.OnDeath(); + EXPECT_EQ(machine.GetState(), State::Dead); + EXPECT_FALSE(host.timers.contains(Timers::ATTACK_HEART_BEAT)); + // No tether once dead + host.position = NiPoint3(1000.0f, 0.0f, 0.0f); + machine.Update(); + EXPECT_EQ(machine.GetState(), State::Dead); +} + +TEST(LuaAITest, GoHomeAndAiDisable) { + FakeHost host; + Machine machine(host, Wanderer()); + machine.Start(); + host.goTos.clear(); + machine.OnNotifyObject("GoHome"); + EXPECT_EQ(machine.GetState(), State::GoHome); + ASSERT_EQ(host.goTos.size(), 1u); + EXPECT_FLOAT_EQ(host.goTos[0].speed, GO_HOME_SPEED); + machine.OnArrived(); + EXPECT_EQ(machine.GetState(), State::Meander); + + machine.SetState(State::AiDisable); + EXPECT_TRUE(machine.GetAiDisabled()); + EXPECT_TRUE(host.stopped); + machine.OnProximityEnter(PLAYER, AGGRO_PROXIMITY); + machine.OnHit(PLAYER); + machine.OnTimerDone(Timers::MEANDER_PAUSE); + EXPECT_EQ(machine.GetState(), State::AiDisable); + machine.SetState(State::AiEnable); + EXPECT_FALSE(machine.GetAiDisabled()); + EXPECT_EQ(machine.GetState(), State::Meander); +}