feat(ai): shared Lua AI custom state

Port o_Main's customState: a script that defines
onTemplateCustomStateEnter or onTemplateCustomStateArrived gets the
state, and its enter and arrived hooks run as LuaAIScript virtuals.
Pets and helpers stay unported: no original script turns them on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-11-27 23:50:19 -06:00
parent 9df7deb468
commit c96d645b94
5 changed files with 54 additions and 3 deletions

View File

@@ -14,6 +14,7 @@ const char* LuaAI::StateName(const State state) {
case State::Dead: return "Dead";
case State::WayPointEvent: return "WayPointEvent";
case State::Follow: return "Follow";
case State::CustomState: return "customState";
}
return "None";
}
@@ -28,6 +29,7 @@ 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::CustomState: return m_Set.hasCustomState;
case State::Follow:
return m_Set.followActive && luaMovement && (m_Set.movementType == MovementType::Wander || m_Set.movementType == MovementType::Guard);
default: return true;
@@ -63,6 +65,7 @@ void LuaAI::Machine::EnterState(const State state) {
break;
case State::WayPointEvent: WayPointEventEnter(); break;
case State::Follow: FollowEnter(); break;
case State::CustomState: m_Host.CustomStateEnter(); break;
}
}
@@ -168,6 +171,7 @@ void LuaAI::Machine::OnArrived(const Arrived& msg) {
case State::GoHome: GoHomeArrived(); break;
// Follow.onArrived does nothing
case State::Follow: break;
case State::CustomState: m_Host.CustomStateArrived(); break;
default: WayPointArrived(msg); break;
}
}

View File

@@ -8,8 +8,8 @@
// 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), 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.
// (Set.FollowActive), the name override (Set.OverRideName) and the custom state.
// Not ported: pets (Set.Pet_Active) and helpers (Set.Helper), which no original script turns on.
#include "dCommonVars.h"
#include "NiPoint3.h"
@@ -25,7 +25,7 @@ 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, Follow };
enum class State : uint8_t { None, Idle, Meander, Aggro, Tether, GoHome, AiDisable, AiEnable, Combat, Dead, WayPointEvent, Follow, CustomState };
const char* StateName(State state);
@@ -131,6 +131,9 @@ namespace LuaAI {
// Set.OverRideName / Set.Name
bool overrideName = false;
std::u16string name;
// The script defines onTemplateCustomStateEnter or onTemplateCustomStateArrived: the customState state
bool hasCustomState = false;
};
// What the machine needs from the object it runs on: the script API calls the shared Lua makes
@@ -195,6 +198,9 @@ namespace LuaAI {
virtual bool IsFollowingDone(LWOOBJID object) const = 0;
// self:SetVar("Imfollowing", true)
virtual void SetImFollowing() = 0;
// The script's onTemplateCustomStateEnter and onTemplateCustomStateArrived
virtual void CustomStateEnter() = 0;
virtual void CustomStateArrived() = 0;
};
class Machine {

View File

@@ -198,6 +198,16 @@ namespace {
void SetImFollowing() override { m_Self->SetVar<std::u16string>(u"Imfollowing", u"true"); }
void CustomStateEnter() override {
auto* const script = dynamic_cast<LuaAIScript*>(m_Self->GetScript());
if (script) script->OnTemplateCustomStateEnter(m_Self);
}
void CustomStateArrived() override {
auto* const script = dynamic_cast<LuaAIScript*>(m_Self->GetScript());
if (script) script->OnTemplateCustomStateArrived(m_Self);
}
// A GoTo or FollowTarget is under way: the arrival is the first frame its movement is done
bool moving = false;

View File

@@ -34,4 +34,10 @@ protected:
virtual void OnLuaTimerDone(Entity* self, const std::string& timerName);
// The global onDie: o_main's does nothing
virtual void OnLuaDie(Entity* self, Entity* killer) {}
public:
// onTemplateCustomStateEnter / onTemplateCustomStateArrived: a script with either sets
// Settings::hasCustomState and overrides them
virtual void OnTemplateCustomStateEnter(Entity* self) {}
virtual void OnTemplateCustomStateArrived(Entity* self) {}
};

View File

@@ -62,6 +62,8 @@ namespace {
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; }
void CustomStateEnter() override { customEnters++; }
void CustomStateArrived() override { customArrivals++; }
int32_t Random(const int32_t min, const int32_t max) override {
randomRanges.push_back({ min, max });
if (rolls.empty()) return min;
@@ -101,6 +103,8 @@ namespace {
std::set<LWOOBJID> pointInLine;
std::set<LWOOBJID> followingDone;
bool imFollowing = false;
int32_t customEnters = 0;
int32_t customArrivals = 0;
};
// A Guard on the path "patrol"
@@ -699,3 +703,24 @@ TEST(LuaAITest, OverRideNameRenamesItOnLoad) {
machine.Start();
EXPECT_EQ(host.names, std::vector<std::u16string>{ u"Bob" });
}
TEST(LuaAITest, TheCustomStateRunsTheScriptsHooks) {
FakeHost host;
Machine plain(host, Wanderer());
plain.Start();
plain.SetState(State::CustomState);
EXPECT_EQ(plain.GetState(), State::Meander);
EXPECT_EQ(host.customEnters, 0);
auto set = Wanderer();
set.hasCustomState = true;
Machine machine(host, set);
machine.Start();
machine.SetState(State::CustomState);
EXPECT_EQ(machine.GetState(), State::CustomState);
EXPECT_EQ(host.customEnters, 1);
// Its onArrived, not the waypoints
machine.OnArrived(AtWaypoint(1));
EXPECT_EQ(host.customArrivals, 1);
EXPECT_FALSE(machine.GetSavedWayPoint().has_value());
}