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>
This commit is contained in:
Aaron Kimbrell
2026-11-27 23:46:28 -06:00
parent 06e9778076
commit 9df7deb468
5 changed files with 184 additions and 5 deletions

View File

@@ -13,6 +13,7 @@ const char* LuaAI::StateName(const State state) {
case State::Combat: return "combat";
case State::Dead: return "Dead";
case State::WayPointEvent: return "WayPointEvent";
case State::Follow: return "Follow";
}
return "None";
}
@@ -27,6 +28,8 @@ bool LuaAI::Machine::HasState(const State state) const {
return m_Set.movementType == MovementType::Frozen
|| (luaMovement && (m_Set.movementType == MovementType::Wander || m_Set.movementType == MovementType::Guard));
case State::WayPointEvent: return m_HasWayPointState;
case State::Follow:
return m_Set.followActive && luaMovement && (m_Set.movementType == MovementType::Wander || m_Set.movementType == MovementType::Guard);
default: return true;
}
}
@@ -59,6 +62,7 @@ void LuaAI::Machine::EnterState(const State state) {
CancelTemplateTimers();
break;
case State::WayPointEvent: WayPointEventEnter(); break;
case State::Follow: FollowEnter(); break;
}
}
@@ -99,13 +103,14 @@ void LuaAI::Machine::LoadOnce() {
m_AttachedPath = m_Set.attachedPath;
if (!m_Set.wayPointSet.empty()) m_AttachedPath = m_Set.wayPointSet;
if (!m_Set.suspendLuaMovementAI && !m_AttachedPath.empty()) FollowWaypoints();
m_Host.SetPointInLine(m_Host.GetObjectID(), false);
// SkillTime: the cooldown of the first skill, the attack heartbeat
const auto skills = m_Host.GetSkills();
if (!skills.empty() && skills.front() != 0) m_SkillTime = m_Host.GetSkillCooldown(skills.front());
m_Host.RestoreHealth();
m_InPursuit = false;
m_AggroTarget = 0;
// Set.OverRideName renames it; names come from the client's locale here, so it is not ported
if (m_Set.overrideName) m_Host.SetName(m_Set.name);
if (m_Set.overrideHealth) m_Host.SetMaxHealth(m_Set.health);
}
@@ -161,6 +166,8 @@ void LuaAI::Machine::OnArrived(const Arrived& msg) {
case State::Aggro: DoAttack(); break;
case State::Tether: TetherArrived(); break;
case State::GoHome: GoHomeArrived(); break;
// Follow.onArrived does nothing
case State::Follow: break;
default: WayPointArrived(msg); break;
}
}

View File

@@ -7,9 +7,9 @@
//
// Ported: the Idle, Meander, aggro, tether, GoHome, combat, AiDisable, AiEnable, Dead and WayPointEvent states, the
// aggroRadius proximity, hits, the tether, the wander and aggro emotes, the attack heartbeat, waypoint paths
// (Set.WayPointSet, attached_path, groupID: o_WayPointEvents) and the template timers.
// Not ported yet: pets (Set.Pet_Active), the Follow state (Set.FollowActive), helpers (Set.Helper) and the custom
// state.
// (Set.WayPointSet, attached_path, groupID: o_WayPointEvents), the template timers, the Follow state
// (Set.FollowActive) and the name override (Set.OverRideName).
// Not ported yet: pets (Set.Pet_Active), helpers (Set.Helper) and the custom state.
#include "dCommonVars.h"
#include "NiPoint3.h"
@@ -25,12 +25,20 @@ namespace LuaAI {
// Set['MovementType']; a script that sets none gets no movement state at all
enum class MovementType : uint8_t { None, Guard, Wander, Frozen };
enum class State : uint8_t { None, Idle, Meander, Aggro, Tether, GoHome, AiDisable, AiEnable, Combat, Dead, WayPointEvent };
enum class State : uint8_t { None, Idle, Meander, Aggro, Tether, GoHome, AiDisable, AiEnable, Combat, Dead, WayPointEvent, Follow };
const char* StateName(State state);
// The proximity oStart makes for an Aggressive one
constexpr const char* AGGRO_PROXIMITY = "aggroRadius";
// The proximity Follow.onEnter looks in for the end of the line (a script of its own makes it)
constexpr const char* FOLLOWERS_PROXIMITY = "Followers";
// Follow.onEnter: the faction whose objects are the line, its FollowTarget and the name it takes
constexpr int32_t FOLLOW_FACTION = 7;
constexpr float FOLLOW_RADIUS = 3.0f;
constexpr float FOLLOW_SPEED = 3.0f;
constexpr const char16_t* FOLLOW_NAME = u"scared";
constexpr const char16_t* FOLLOW_EMOTE = u"flip";
// aggro.onEnter: INITIAL_ATTACK_TIME
constexpr float INITIAL_ATTACK_TIME = 1.0f;
// The FollowTarget of the aggro state without AggroDist and AggroSpeed
@@ -117,6 +125,12 @@ namespace LuaAI {
std::string wayPointSet;
std::string attachedPath;
bool hasGroupID = false;
// Set.FollowActive: the Follow state
bool followActive = false;
// Set.OverRideName / Set.Name
bool overrideName = false;
std::u16string name;
};
// What the machine needs from the object it runs on: the script API calls the shared Lua makes
@@ -169,6 +183,18 @@ namespace LuaAI {
// The waypoint actions that act on other objects or the world (bounce, groupemote, setvar, eqInvent, unInvent,
// teleport, pathspeed, removeNPC, DeleteSelf, removeself, spawnOBJ); the machine runs the rest
virtual void RunWaypointAction(const std::string& name, const std::string& value, std::optional<uint32_t> savedWayPoint) = 0;
virtual LWOOBJID GetObjectID() const = 0;
// self:SetName { name }
virtual void SetName(const std::u16string& name) = 0;
// obj:BelongsToFaction { factionID }.bIsInFaction
virtual bool BelongsToFaction(LWOOBJID object, int32_t faction) const = 0;
// The PointInLine and Imfollowing vars of the Follow state, which other objects of the line read and set
virtual bool GetPointInLine(LWOOBJID object) const = 0;
virtual void SetPointInLine(LWOOBJID object, bool pointInLine) = 0;
// obj:GetVar("Imfollowing") == "done"
virtual bool IsFollowingDone(LWOOBJID object) const = 0;
// self:SetVar("Imfollowing", true)
virtual void SetImFollowing() = 0;
};
class Machine {
@@ -244,6 +270,7 @@ namespace LuaAI {
void GoHomeArrived();
void AiDisableEnter();
void AiEnableEnter();
void FollowEnter();
// o_main / o_onEvent
void AggroEnter();
@@ -302,5 +329,8 @@ namespace LuaAI {
int32_t m_WPEventNum = 1;
// The Timers var: the names SetStoreTimmer started (its "blank" first entry left out)
std::vector<std::string> m_TemplateTimers;
// EndOfLineTarget: the object of the line Follow.onEnter found last
LWOOBJID m_EndOfLineTarget = LWOOBJID_EMPTY;
};
};

View File

@@ -35,6 +35,35 @@ void LuaAI::Machine::MeanderArrived() {
}
}
// Follow.onEnter: follow the end of the line of faction 7 objects in the Followers proximity, or be its end
void LuaAI::Machine::FollowEnter() {
const auto self = m_Host.GetObjectID();
for (const auto object : m_Host.GetProximityObjects(FOLLOWERS_PROXIMITY)) {
if (!m_Host.BelongsToFaction(object, FOLLOW_FACTION) || !m_Host.GetPointInLine(object)) continue;
m_Host.SetPointInLine(self, true);
m_Host.SetPointInLine(object, false);
m_EndOfLineTarget = object;
}
m_Host.SetName(FOLLOW_NAME);
// getMyTarget: the myTarget var, which only o_Proximity (not part of this AI) sets, so nil here
constexpr LWOOBJID myTarget = LWOOBJID_EMPTY;
if (m_EndOfLineTarget != LWOOBJID_EMPTY) {
m_Host.SetImFollowing();
m_Host.PlayEmote(FOLLOW_EMOTE);
if (!m_Host.IsFollowingDone(m_EndOfLineTarget)) {
m_Host.FollowTarget(m_EndOfLineTarget, FOLLOW_RADIUS, FOLLOW_SPEED, true);
} else {
m_Host.SetImFollowing();
m_Host.FollowTarget(myTarget, FOLLOW_RADIUS, FOLLOW_SPEED, true);
}
} else {
m_Host.PlayEmote(FOLLOW_EMOTE);
m_Host.SetPointInLine(self, true);
m_Host.SetImFollowing();
m_Host.FollowTarget(myTarget, FOLLOW_RADIUS, FOLLOW_SPEED, true);
}
}
void LuaAI::Machine::TetherEnter() {
m_AggroTarget = 2;
m_Host.SetImmunity(true);

View File

@@ -168,6 +168,35 @@ namespace {
LOG_DEBUG("Lua AI waypoint action %s on %llu is run by the movement AI", name.c_str(), m_Self->GetObjectID());
}
LWOOBJID GetObjectID() const override { return m_Self->GetObjectID(); }
// SetName (game message 72) has no DLU message yet, and names otherwise come from the locale
void SetName(const std::u16string& name) override {
LOG_DEBUG("Lua AI SetName %s on %llu is not sent", GeneralUtils::UTF16ToWTF8(name).c_str(), m_Self->GetObjectID());
}
bool BelongsToFaction(const LWOOBJID object, const int32_t faction) const override {
const auto* const other = Game::entityManager->GetEntity(object);
const auto* const destroyable = other ? other->GetComponent<DestroyableComponent>() : nullptr;
return destroyable && destroyable->HasFaction(faction);
}
bool GetPointInLine(const LWOOBJID object) const override {
const auto* const other = Game::entityManager->GetEntity(object);
return other && other->GetVar<bool>(u"PointInLine");
}
void SetPointInLine(const LWOOBJID object, const bool pointInLine) override {
auto* const other = object == m_Self->GetObjectID() ? m_Self : Game::entityManager->GetEntity(object);
if (other) other->SetVar<bool>(u"PointInLine", pointInLine);
}
bool IsFollowingDone(const LWOOBJID object) const override {
const auto* const other = Game::entityManager->GetEntity(object);
return other && other->GetVarAsString(u"Imfollowing") == "done";
}
void SetImFollowing() override { m_Self->SetVar<std::u16string>(u"Imfollowing", u"true"); }
// A GoTo or FollowTarget is under way: the arrival is the first frame its movement is done
bool moving = false;

View File

@@ -15,6 +15,7 @@ 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;
@@ -34,6 +35,7 @@ namespace {
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); }
@@ -53,6 +55,13 @@ namespace {
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;
@@ -86,6 +95,12 @@ namespace {
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"
@@ -615,3 +630,72 @@ TEST(LuaAITest, AStateWithItsOwnOnArrivedGetsTheWaypoints) {
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" });
}