Files
DarkflameServer/tests/dGameTests/LuaAITests.cpp
Aaron Kimbrell 9df7deb468 feat(ai): shared Lua AI Follow state and name override
Port o_Movement's Follow state (Set.FollowActive): the Followers
proximity, the faction 7 line and its PointInLine / Imfollowing vars,
the flip emote and the follow at radius 3, speed 3. loadOnce now applies
Set.OverRideName; the binding only logs SetName, which DLU does not send
yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 23:46:28 -06:00

702 lines
26 KiB
C++

// 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 <gtest/gtest.h>
#include <deque>
#include <map>
#include <set>
using namespace LuaAI;
namespace {
constexpr LWOOBJID PLAYER = 100;
constexpr LWOOBJID OTHER_PLAYER = 101;
constexpr LWOOBJID FRIEND = 102;
constexpr LWOOBJID SELF = 103;
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<LWOOBJID> GetProximityObjects(const std::string& name) const override {
if (name == FOLLOWERS_PROXIMITY) return followers;
return name == AGGRO_PROXIMITY ? inAggroRadius : std::vector<LWOOBJID>{};
}
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<uint32_t> 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; }
void FollowWaypoints(const std::string& path, const uint32_t start) override { paths.push_back({ path, start }); }
void ContinueWaypoints() override { continues++; }
float GetAnimationTime(const std::u16string&) const override { return animationTime; }
void RunWaypointAction(const std::string& name, const std::string& value, std::optional<uint32_t>) override { actions.push_back({ name, value }); }
LWOOBJID GetObjectID() const override { return SELF; }
void SetName(const std::u16string& value) override { names.push_back(value); }
bool BelongsToFaction(const LWOOBJID object, const int32_t faction) const override { return faction == FOLLOW_FACTION && lineFaction.contains(object); }
bool GetPointInLine(const LWOOBJID object) const override { return pointInLine.contains(object); }
void SetPointInLine(const LWOOBJID object, const bool value) override { if (value) pointInLine.insert(object); else pointInLine.erase(object); }
bool IsFollowingDone(const LWOOBJID object) const override { return followingDone.contains(object); }
void SetImFollowing() override { imFollowing = 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<GoToCall> goTos;
std::vector<FollowCall> follows;
std::map<std::string, float> timers;
std::map<std::string, float> proximities;
std::vector<LWOOBJID> inAggroRadius;
std::map<LWOOBJID, std::string> types{ { PLAYER, "NPC" }, { OTHER_PLAYER, "NPC" }, { FRIEND, "Smashables" } };
std::set<LWOOBJID> enemies{ PLAYER, OTHER_PLAYER, FRIEND };
std::set<LWOOBJID> dead;
bool selfDead = false;
std::vector<uint32_t> skills{ FIRST_SKILL, LAST_SKILL };
std::vector<std::pair<uint32_t, LWOOBJID>> casts;
bool immune = false;
int32_t healed = 0;
int32_t maxHealth = 0;
std::vector<std::u16string> emotes;
bool diedSilently = false;
bool stopped = false;
std::deque<int32_t> rolls;
std::vector<std::pair<int32_t, int32_t>> randomRanges;
std::vector<std::pair<std::string, uint32_t>> paths;
int32_t continues = 0;
float animationTime = 0.5f;
std::vector<std::pair<std::string, std::string>> actions;
std::vector<std::u16string> names;
std::vector<LWOOBJID> followers;
std::set<LWOOBJID> lineFaction;
std::set<LWOOBJID> pointInLine;
std::set<LWOOBJID> followingDone;
bool imFollowing = false;
};
// A Guard on the path "patrol"
Settings Patroller() {
Settings set{};
set.aggroRadius = 15.0f;
set.tetherRadius = 50.0f;
set.aggression = Aggression::Aggressive;
set.movementType = MovementType::Guard;
set.attachedPath = "patrol";
return set;
}
Arrived AtWaypoint(const uint32_t wayPoint, std::optional<std::vector<WaypointAction>> actions = std::nullopt, const bool last = false) {
Arrived msg{};
msg.wayPoint = wayPoint;
msg.pathName = "patrol";
msg.isLastPoint = last;
msg.actions = std::move(actions);
return msg;
}
// 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<float>(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<std::u16string>{ 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<std::u16string>{ 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);
}
TEST(LuaAITest, APathIsFollowedFromLoadOnceAndOStart) {
FakeHost host;
Machine machine(host, Patroller());
machine.Start();
EXPECT_EQ(machine.GetState(), State::Idle);
ASSERT_EQ(host.paths.size(), 2u);
EXPECT_EQ(host.paths[0], std::make_pair(std::string("patrol"), 0u));
EXPECT_EQ(host.paths[1], host.paths[0]);
// A waypoint without actions: remembered, then on along the path
machine.OnArrived(AtWaypoint(2));
EXPECT_EQ(machine.GetSavedWayPoint(), 2u);
EXPECT_EQ(machine.GetPathName(), "patrol");
EXPECT_EQ(host.continues, 1);
EXPECT_EQ(machine.GetState(), State::Idle);
}
TEST(LuaAITest, SetWayPointSetAttachesItsPath) {
FakeHost host;
auto set = Patroller();
set.attachedPath.clear();
set.wayPointSet = "lua_path";
Machine machine(host, set);
machine.Start();
ASSERT_FALSE(host.paths.empty());
EXPECT_EQ(host.paths[0].first, "lua_path");
EXPECT_EQ(machine.GetAttachedPath(), "lua_path");
}
TEST(LuaAITest, AGroupedObjectWaitsForStartPath) {
FakeHost host;
auto set = Patroller();
set.attachedPath.clear();
set.hasGroupID = true;
Machine machine(host, set);
machine.Start();
EXPECT_TRUE(host.paths.empty());
machine.OnFireEvent("startpath,gate_path,3");
ASSERT_EQ(host.paths.size(), 1u);
EXPECT_EQ(host.paths[0], std::make_pair(std::string("gate_path"), 3u));
EXPECT_EQ(machine.GetAttachedPath(), "gate_path");
}
TEST(LuaAITest, SuspendedMovementFollowsNoPath) {
FakeHost host;
auto set = Patroller();
set.suspendLuaMovementAI = true;
Machine machine(host, set);
machine.Start();
EXPECT_TRUE(host.paths.empty());
machine.OnArrived(AtWaypoint(1, std::vector<WaypointAction>{ { "castskill", NO_VALUE } }));
EXPECT_TRUE(host.casts.empty());
EXPECT_EQ(machine.GetState(), State::Idle);
}
TEST(LuaAITest, WaypointActionsRunInOrderAndWaitOnDelays) {
FakeHost host;
Machine machine(host, Patroller());
machine.Start();
machine.OnArrived(AtWaypoint(1, std::vector<WaypointAction>{
{ "castskill", NO_VALUE }, { "femote", "wave" }, { "pathspeed", "2" }, { "delay", "3" }, { "castskill", "500" } }));
EXPECT_EQ(machine.GetState(), State::WayPointEvent);
ASSERT_EQ(host.casts.size(), 1u);
EXPECT_EQ(host.casts[0].first, FIRST_SKILL);
ASSERT_EQ(host.emotes.size(), 1u);
EXPECT_EQ(host.emotes[0], u"wave");
ASSERT_EQ(host.actions.size(), 1u);
EXPECT_EQ(host.actions[0], std::make_pair(std::string("pathspeed"), std::string("2")));
// The delay stops it and stores its timer
EXPECT_TRUE(host.stopped);
EXPECT_EQ(host.timers.at(Timers::DELAY_ACTION), 3.0f);
EXPECT_EQ(machine.GetWayPointEventNum(), 4);
EXPECT_EQ(machine.GetTemplateTimers(), std::vector<std::string>{ Timers::DELAY_ACTION });
EXPECT_EQ(host.continues, 0);
machine.OnTimerDone(Timers::DELAY_ACTION);
ASSERT_EQ(host.casts.size(), 2u);
EXPECT_EQ(host.casts[1].first, 500u);
// All run: back to the first action, on along the path
EXPECT_EQ(machine.GetWayPointEventNum(), 1);
EXPECT_EQ(host.continues, 1);
}
TEST(LuaAITest, AnEmoteActionWaitsAtLeastASecond) {
FakeHost host;
Machine machine(host, Patroller());
machine.Start();
machine.OnArrived(AtWaypoint(1, std::vector<WaypointAction>{ { "emote", "salute" } }));
EXPECT_EQ(host.emotes.back(), u"salute");
EXPECT_EQ(host.timers.at(Timers::DELAY_ACTION_EMOTE), 1.0f);
host.animationTime = 2.5f;
machine.OnTimerDone(Timers::DELAY_ACTION_EMOTE);
EXPECT_EQ(host.continues, 1);
}
TEST(LuaAITest, ChangeWPAndKillSelf) {
FakeHost host;
Machine machine(host, Patroller());
machine.Start();
host.paths.clear();
machine.OnArrived(AtWaypoint(4, std::vector<WaypointAction>{ { "changeWP", "other,3" } }));
ASSERT_EQ(host.paths.size(), 1u);
EXPECT_EQ(host.paths[0], std::make_pair(std::string("other"), 3u));
EXPECT_EQ(machine.GetAttachedPath(), "other");
machine.OnArrived(AtWaypoint(5, std::vector<WaypointAction>{ { "changeWP", "third" } }));
EXPECT_EQ(host.paths.back(), std::make_pair(std::string("third"), 0u));
machine.OnArrived(AtWaypoint(1, std::vector<WaypointAction>{ { "killself", "2" } }));
EXPECT_EQ(host.timers.at(Timers::DELAY_KILL_SELF), 2.0f);
EXPECT_FALSE(host.diedSilently);
machine.OnTimerDone(Timers::DELAY_KILL_SELF);
EXPECT_TRUE(host.diedSilently);
}
TEST(LuaAITest, TheLastWaypointDetachesThePath) {
FakeHost host;
Machine machine(host, Patroller());
machine.Start();
machine.OnArrived(AtWaypoint(3));
host.paths.clear();
// A reset goes on along the path from the waypoint after the last one
machine.ResetState();
ASSERT_EQ(host.paths.size(), 1u);
EXPECT_EQ(host.paths[0], std::make_pair(std::string("patrol"), 4u));
machine.OnArrived(AtWaypoint(5, std::nullopt, true));
EXPECT_TRUE(machine.GetAttachedPath().empty());
EXPECT_FALSE(machine.GetSavedWayPoint().has_value());
host.paths.clear();
machine.ResetState();
EXPECT_TRUE(host.paths.empty());
EXPECT_EQ(machine.GetState(), State::Idle);
}
TEST(LuaAITest, AFightCancelsTheWaypointTimers) {
FakeHost host;
host.types[PLAYER] = "NPC";
Machine machine(host, Patroller());
machine.Start();
machine.OnArrived(AtWaypoint(1, std::vector<WaypointAction>{ { "delay", "5" } }));
ASSERT_TRUE(host.timers.contains(Timers::DELAY_ACTION));
machine.OnProximityEnter(PLAYER, AGGRO_PROXIMITY);
EXPECT_FALSE(host.timers.contains(Timers::DELAY_ACTION));
EXPECT_EQ(machine.GetState(), State::Aggro);
}
TEST(LuaAITest, AStateWithItsOwnOnArrivedGetsTheWaypoints) {
FakeHost host;
auto set = Wanderer();
set.attachedPath = "patrol";
Machine machine(host, set);
machine.Start();
ASSERT_EQ(machine.GetState(), State::Meander);
machine.OnArrived(AtWaypoint(2, std::vector<WaypointAction>{ { "castskill", NO_VALUE } }));
// Meander.onArrived: the wander pause, not the waypoint actions
EXPECT_TRUE(host.casts.empty());
EXPECT_FALSE(machine.GetSavedWayPoint().has_value());
EXPECT_TRUE(host.timers.contains(Timers::MEANDER_PAUSE));
}
TEST(LuaAITest, FollowWithoutALineBecomesItsEnd) {
FakeHost host;
auto set = Patroller();
set.attachedPath.clear();
set.followActive = true;
Machine machine(host, set);
machine.Start();
EXPECT_FALSE(host.pointInLine.contains(SELF));
machine.SetState(State::Follow);
EXPECT_EQ(machine.GetState(), State::Follow);
EXPECT_EQ(host.names, std::vector<std::u16string>{ FOLLOW_NAME });
EXPECT_EQ(host.emotes, std::vector<std::u16string>{ FOLLOW_EMOTE });
EXPECT_TRUE(host.pointInLine.contains(SELF));
EXPECT_TRUE(host.imFollowing);
ASSERT_EQ(host.follows.size(), 1u);
EXPECT_EQ(host.follows[0].target, LWOOBJID_EMPTY);
EXPECT_EQ(host.follows[0].radius, FOLLOW_RADIUS);
EXPECT_EQ(host.follows[0].speed, FOLLOW_SPEED);
EXPECT_TRUE(host.follows[0].keepFollowing);
// Follow.onArrived does nothing: no waypoint bookkeeping
machine.OnArrived(AtWaypoint(1));
EXPECT_EQ(host.continues, 0);
}
TEST(LuaAITest, FollowTakesOverTheEndOfTheLine) {
FakeHost host;
auto set = Patroller();
set.attachedPath.clear();
set.followActive = true;
Machine machine(host, set);
machine.Start();
host.followers = { OTHER_PLAYER, FRIEND };
host.lineFaction = { FRIEND };
host.pointInLine = { OTHER_PLAYER, FRIEND };
machine.SetState(State::Follow);
// Only the faction 7 object counts
EXPECT_TRUE(host.pointInLine.contains(SELF));
EXPECT_FALSE(host.pointInLine.contains(FRIEND));
EXPECT_TRUE(host.pointInLine.contains(OTHER_PLAYER));
ASSERT_EQ(host.follows.size(), 1u);
EXPECT_EQ(host.follows[0].target, FRIEND);
// The end of the line it found stays, even once it is done following
host.followers.clear();
host.followingDone = { FRIEND };
machine.SetState(State::Follow);
ASSERT_EQ(host.follows.size(), 2u);
EXPECT_EQ(host.follows[1].target, LWOOBJID_EMPTY);
}
TEST(LuaAITest, FollowNeedsFollowActive) {
FakeHost host;
Machine machine(host, Patroller());
machine.Start();
machine.SetState(State::Follow);
EXPECT_EQ(machine.GetState(), State::Idle);
EXPECT_TRUE(host.follows.empty());
}
TEST(LuaAITest, OverRideNameRenamesItOnLoad) {
FakeHost host;
auto set = Wanderer();
set.overrideName = true;
set.name = u"Bob";
Machine machine(host, set);
machine.Start();
EXPECT_EQ(host.names, std::vector<std::u16string>{ u"Bob" });
}