feat(ai): shared Lua AI state machine ported from the original scripts

The State/o_StateCreate/o_Main/o_mis/o_onEvent/o_Movement AI that the
original server scripts run with loadOnce, getVarables, CreateStates and
oStart: Idle, Meander, aggro, tether, GoHome, combat, AiDisable, AiEnable
and Dead, fed by the scripts' Set table and driven through a Host
interface so it runs without an entity.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-11-27 22:50:36 -06:00
parent 549e8821dd
commit 5334953fe6
6 changed files with 641 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ add_subdirectory(AG)
add_subdirectory(FV)
add_subdirectory(GENERAL)
add_subdirectory(GF)
add_subdirectory(LuaAI)
add_subdirectory(MINIGAME)
add_subdirectory(NP)
add_subdirectory(NS)
@@ -21,6 +22,7 @@ target_sources(dScriptsAI INTERFACE
$<TARGET_OBJECTS:dScriptsAiFV>
$<TARGET_OBJECTS:dScriptsAiGeneral>
$<TARGET_OBJECTS:dScriptsAiGF>
$<TARGET_OBJECTS:dScriptsAiLuaAI>
$<TARGET_OBJECTS:dScriptsAiMinigame>
$<TARGET_OBJECTS:dScriptsAiNP>
$<TARGET_OBJECTS:dScriptsAiNS>
@@ -37,6 +39,7 @@ target_include_directories(dScriptsAI INTERFACE
$<TARGET_PROPERTY:dScriptsAiFV,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:dScriptsAiGeneral,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:dScriptsAiGF,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:dScriptsAiLuaAI,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:dScriptsAiMinigame,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:dScriptsAiNP,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:dScriptsAiNS,INTERFACE_INCLUDE_DIRECTORIES>

View File

@@ -0,0 +1,8 @@
set(DSCRIPTS_SOURCES_AI_LUAAI
"LuaAICombat.cpp"
"LuaAIMachine.cpp"
"LuaAIMovement.cpp")
add_library(dScriptsAiLuaAI OBJECT ${DSCRIPTS_SOURCES_AI_LUAAI})
target_include_directories(dScriptsAiLuaAI PUBLIC ".")
target_precompile_headers(dScriptsAiLuaAI REUSE_FROM dScriptsBase)

View File

@@ -0,0 +1,130 @@
// The combat side of the shared Lua AI: the aggroRadius proximity and hits (o_onEvent), the aggro state and its
// attack heartbeat (o_main AggroState, DoAttack, CanAttackTarget)
#include "LuaAIMachine.h"
#include <algorithm>
#include <array>
namespace {
// isAttackable / isOBJAttackable: the Objects table types it fights (players are "NPC")
constexpr std::array<std::string_view, 3> ATTACKABLE_TYPES{ "Enemies", "NPC", "Rebuildables" };
};
bool LuaAI::Machine::IsAttackable(const LWOOBJID object) const {
const auto type = m_Host.GetObjectType(object);
if (std::find(ATTACKABLE_TYPES.begin(), ATTACKABLE_TYPES.end(), type) == ATTACKABLE_TYPES.end()) return false;
if (!m_Host.IsEnemy(object) || m_Host.IsDead(object)) return false;
if (m_Set.aggression != Aggression::Aggressive && m_Set.aggression != Aggression::PassiveAggres) return false;
return m_AggroTarget != 2 && m_AttackingTarget == LWOOBJID_EMPTY && !m_AiDisabled;
}
void LuaAI::Machine::AggroEmote(const LWOOBJID target) {
m_Host.FaceTarget(target);
m_Host.PlayEmote(m_Set.aggroEmoteType);
m_Host.AddTimer(Timers::AGGRO_EMOTE, m_Set.aggroEmoteDelay);
m_AggroEmoteDelay = false;
}
// o_onEvent onProximityUpdate, status ENTER
void LuaAI::Machine::OnProximityEnter(const LWOOBJID object, const std::string& name) {
if (name != AGGRO_PROXIMITY || !IsAttackable(object)) return;
if (m_Set.suspendLuaAI) {
SetState(State::Combat);
return;
}
m_AttackingTarget = object;
StoreHomePoint();
// The original reads "tetherRadius", not "Set.tetherRadius": nil, so no tether until aggro.onEnter sets one
SetTetherPoint(m_Host.GetPosition(), 0.0f);
m_AggroTarget = 1;
if (m_Set.aggroEmote && m_AggroEmoteDelay) {
AggroEmote(object);
SetState(State::Idle);
return;
}
SetState(State::Aggro);
}
// ProximityPuls: the next target in the aggro radius, or back home
void LuaAI::Machine::ProximityPuls() {
m_AttackingTarget = LWOOBJID_EMPTY;
for (const auto object : m_Host.GetProximityObjects(AGGRO_PROXIMITY)) {
if (!IsAttackable(object)) continue;
m_AttackingTarget = object;
m_AggroTarget = 1;
if (m_Set.aggroEmote && m_AggroEmoteDelay) {
AggroEmote(object);
SetState(State::Idle);
}
SetState(m_Set.suspendLuaAI ? State::Combat : State::Aggro);
return;
}
SetState(State::Tether);
}
void LuaAI::Machine::OnHit(const LWOOBJID attacker) {
if (m_Set.suspendLuaAI) {
SetState(State::Combat);
return;
}
if (m_Set.aggression == Aggression::Passive || m_AiDisabled) return;
m_AttackingTarget = attacker;
if (!m_InPursuit) {
// Each hit moves the home point here; the tether goes round the global myPos the Meander state set
StoreHomePoint();
if (m_GlobalMyPos) SetTetherPoint(*m_GlobalMyPos, m_Set.tetherRadius);
}
if (!m_Host.IsEnemy(attacker) || m_Host.IsDead(attacker) || m_Host.IsSelfDead()) return;
if (m_AggroTarget != 2) SetState(State::Aggro);
}
void LuaAI::Machine::AggroEnter() {
m_ReadyToAttack = false;
m_Host.AddTimer(Timers::ATTACK_HEART_BEAT, INITIAL_ATTACK_TIME);
m_Host.CancelTimer(Timers::MEANDER_PAUSE);
const auto target = m_AttackingTarget;
if (m_Host.IsDead(target)) {
m_Aggroed = false;
ProximityPuls();
return;
}
if (m_Set.movementType == MovementType::Frozen) return;
SetTetherPoint(m_Home, m_Set.tetherRadius);
if (m_Set.aggroDist && m_Set.aggroSpeed) {
m_Host.FollowTarget(target, *m_Set.aggroDist, *m_Set.aggroSpeed * 2.0f, true);
m_Aggroed = true;
} else {
m_Host.FollowTarget(target, DEFAULT_AGGRO_DIST, DEFAULT_AGGRO_SPEED, true);
}
}
void LuaAI::Machine::AggroTimerDone(const std::string& name) {
m_ReadyToAttack = true;
DoAttack();
// Re-added even when the attack left the aggro state; without a skill the original errors here and stops
if (m_SkillTime) m_Host.AddTimer(Timers::ATTACK_HEART_BEAT, *m_SkillTime);
}
// Casts whenever the heartbeat (or an arrival) finds it ready, however far the target is
void LuaAI::Machine::DoAttack() {
if (!m_ReadyToAttack) return;
if (!CanAttackTarget()) return;
m_ReadyToAttack = false;
if (m_Set.aggression == Aggression::PassiveAggres && m_AggroOnce == 0) m_AggroOnce = 1;
// The last of its skills, or Set.AttackSkill with Set.OverRideAttackSkill
const auto skills = m_Host.GetSkills();
uint32_t skill = skills.empty() ? 0 : skills.back();
if (m_Set.overrideAttackSkill && m_Set.attackSkill != 0) skill = m_Set.attackSkill;
if (skill == 0) return;
m_Host.CastSkill(skill, m_Set.useOptionalTargetOnAttack ? m_AttackingTarget : LWOOBJID_EMPTY);
}
bool LuaAI::Machine::CanAttackTarget() {
const auto target = m_AttackingTarget;
if (m_Host.Exists(target) && !m_Host.IsDead(target) && m_Host.IsEnemy(target)) return true;
m_AttackingTarget = LWOOBJID_EMPTY;
m_Aggroed = false;
ProximityPuls();
return false;
}

View File

@@ -0,0 +1,200 @@
#include "LuaAIMachine.h"
const char* LuaAI::StateName(const State state) {
switch (state) {
case State::None: return "None";
case State::Idle: return "Idle";
case State::Meander: return "Meander";
case State::Aggro: return "aggro";
case State::Tether: return "tether";
case State::GoHome: return "GoHome";
case State::AiDisable: return "AiDisable";
case State::AiEnable: return "AiEnable";
case State::Combat: return "combat";
case State::Dead: return "Dead";
}
return "None";
}
// oStart's addState calls: the states this one has
bool LuaAI::Machine::HasState(const State state) const {
const bool luaMovement = !m_Set.suspendLuaMovementAI;
switch (state) {
case State::None: return false;
case State::Meander: return luaMovement && m_Set.movementType == MovementType::Wander;
case State::Aggro:
return m_Set.movementType == MovementType::Frozen
|| (luaMovement && (m_Set.movementType == MovementType::Wander || m_Set.movementType == MovementType::Guard));
default: return true;
}
}
void LuaAI::Machine::SetState(const State state) {
// setState to a state it never added does nothing
if (!HasState(state)) return;
const auto previous = m_State;
m_State = state;
ExitState(previous);
EnterState(state);
}
void LuaAI::Machine::EnterState(const State state) {
switch (state) {
case State::None: break;
// Idle.onEnter only starts pets and rebuild breaks, neither of which is ported
case State::Idle: break;
case State::Meander: MeanderEnter(); break;
case State::Aggro: AggroEnter(); break;
case State::Tether: TetherEnter(); break;
case State::GoHome: GoHomeEnter(); break;
case State::AiDisable: AiDisableEnter(); break;
case State::AiEnable: AiEnableEnter(); break;
case State::Combat:
m_Host.CancelTimer(Timers::MEANDER_PAUSE);
break;
case State::Dead:
// Dead.onEnter: getVarables and CancelTemplateTimers (only the waypoint timers are template timers)
GetVarables();
break;
}
}
void LuaAI::Machine::ExitState(const State state) {
if (state == State::Aggro) m_Host.CancelTimer(Timers::ATTACK_HEART_BEAT);
}
void LuaAI::Machine::Start() {
LoadOnce();
GetVarables();
// CreateStates
StoreHomePoint();
SetTetherPoint(m_Host.GetPosition(), 0.0f);
// MeanderState: storeMeanderPoint, the centre it wanders round for good
if (m_Set.movementType == MovementType::Wander) m_MeanderPoint = m_Host.GetPosition();
// oStart
m_AiDisabled = false;
if (m_Set.aggression == Aggression::Aggressive) m_Host.SetProximityRadius(m_Set.aggroRadius, AGGRO_PROXIMITY);
if (!m_Set.suspendLuaMovementAI) {
if (m_Set.movementType == MovementType::Wander) SetState(State::Meander);
if (m_Set.movementType == MovementType::Guard) SetState(State::Idle);
} else {
SetState(State::Idle);
}
if (m_Set.movementType == MovementType::Frozen) SetState(State::Idle);
}
void LuaAI::Machine::LoadOnce() {
m_AttackingTarget = LWOOBJID_EMPTY;
// 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.overrideHealth) m_Host.SetMaxHealth(m_Set.health);
}
void LuaAI::Machine::GetVarables() {
m_ReadyToAttack = true;
m_AggroEmoteDelay = true;
m_AggroTarget = 0;
m_InPursuit = false;
m_AggroOnce = 0;
}
void LuaAI::Machine::SetResetState() {
GetVarables();
m_InPursuit = false;
m_AttackingTarget = LWOOBJID_EMPTY;
SetTetherPoint(m_Host.GetPosition(), 0.0f);
if (!m_Set.suspendLuaMovementAI && m_Set.movementType == MovementType::Wander) {
SetState(State::Meander);
return;
}
if (m_Set.movementType == MovementType::Guard) SetState(State::Idle);
}
void LuaAI::Machine::StoreHomePoint() {
m_Home = m_Host.GetPosition();
m_HomeRotation = m_Host.GetRotation();
}
void LuaAI::Machine::SetTetherPoint(const NiPoint3& point, const float radius) {
m_TetherPoint = point;
m_TetherRadius = radius;
m_InsideTether = radius <= 0.0f || NiPoint3::DistanceSquared(m_Host.GetPosition(), point) <= radius * radius;
}
void LuaAI::Machine::Update() {
if (m_TetherRadius <= 0.0f || m_State == State::Dead) return;
const bool inside = NiPoint3::DistanceSquared(m_Host.GetPosition(), m_TetherPoint) <= m_TetherRadius * m_TetherRadius;
const bool left = m_InsideTether && !inside;
m_InsideTether = inside;
if (left) OnLeftTetherRadius();
}
void LuaAI::Machine::OnArrived() {
switch (m_State) {
case State::Meander: MeanderArrived(); break;
case State::Aggro: DoAttack(); break;
case State::Tether: TetherArrived(); break;
case State::GoHome: GoHomeArrived(); break;
// The global onArrived only handles waypoint paths
default: break;
}
}
void LuaAI::Machine::OnCancelled() {
if (m_State != State::Aggro) return;
m_Aggroed = false;
ProximityPuls();
}
bool LuaAI::Machine::OnStateTimerDone(const std::string& name) {
if (m_State != State::Aggro || name != Timers::ATTACK_HEART_BEAT) return false;
AggroTimerDone(name);
return true;
}
void LuaAI::Machine::OnTimerDone(const std::string& name) {
if (m_AiDisabled) return;
if (name == Timers::DELAY_KILL_SELF) {
if (!m_Set.suspendLuaMovementAI) m_Host.DieSilently();
} else if (name == Timers::DELETE_OBJECT) {
m_Host.DieSilently();
} else if (name == Timers::AGGRO_EMOTE) {
SetState(m_Set.suspendLuaAI ? State::Combat : State::Aggro);
} else if (name == Timers::MEANDER_PAUSE) {
if (!m_Set.suspendLuaMovementAI) SetState(State::Meander);
}
}
void LuaAI::Machine::OnLeftTetherRadius() {
m_AttackingTarget = LWOOBJID_EMPTY;
GetVarables();
m_Host.RestoreHealth();
SetState(State::Tether);
}
void LuaAI::Machine::OnDeath() {
SetState(State::Dead);
}
void LuaAI::Machine::OnNotifyObject(const std::string& name) {
if (name == "GoHome") SetState(State::GoHome);
}
void LuaAI::Machine::AiDisableEnter() {
StoreHomePoint();
SetTetherPoint(m_Host.GetPosition(), 0.0f);
m_AiDisabled = true;
m_Host.StopPathing();
}
void LuaAI::Machine::AiEnableEnter() {
m_AiDisabled = false;
SetResetState();
}

View File

@@ -0,0 +1,241 @@
#pragma once
// The shared Lua AI of the original server scripts (State, o_StateCreate, o_Main, o_mis, o_onEvent, o_Movement):
// a script fills its Set table, then runs loadOnce, getVarables, CreateStates and oStart. This is that state
// machine, fed by the same Set table and driven through a Host, so it can be tested without an entity and bound to
// one by LuaAIScript.
//
// Ported: the Idle, Meander, aggro, tether, GoHome, combat, AiDisable, AiEnable and Dead states, the aggroRadius
// proximity, hits, the tether, the wander and aggro emotes and the attack heartbeat.
// Not ported yet: waypoint paths (Set.WayPointSet, attached_path, groupID: o_WayPointEvents), pets (Set.Pet_Active),
// the Follow state (Set.FollowActive), helpers (Set.Helper) and the custom state.
#include "dCommonVars.h"
#include "NiPoint3.h"
#include "NiQuaternion.h"
#include <optional>
#include <string>
#include <vector>
namespace LuaAI {
// Set['Aggression']; a script that sets none acts as Neutral
enum class Aggression : uint8_t { Neutral, Aggressive, Passive, PassiveAggres };
// 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 };
const char* StateName(State state);
// The proximity oStart makes for an Aggressive one
constexpr const char* AGGRO_PROXIMITY = "aggroRadius";
// aggro.onEnter: INITIAL_ATTACK_TIME
constexpr float INITIAL_ATTACK_TIME = 1.0f;
// The FollowTarget of the aggro state without AggroDist and AggroSpeed
constexpr float DEFAULT_AGGRO_DIST = 3.0f;
constexpr float DEFAULT_AGGRO_SPEED = 1.0f;
// GoHome.onEnter: GoTo { speed = 1 }
constexpr float GO_HOME_SPEED = 1.0f;
// The timers the shared Lua uses
namespace Timers {
constexpr const char* ATTACK_HEART_BEAT = "AttackHeartBeat";
constexpr const char* MEANDER_PAUSE = "MeanderPause";
constexpr const char* AGGRO_EMOTE = "AggroEmote";
constexpr const char* DELAY_KILL_SELF = "DealyKillSelf";
constexpr const char* DELETE_OBJECT = "TimerDeleteObject";
};
// The Set table keys the shared AI reads. Keys a script leaves nil keep these defaults.
struct Settings {
// RADIUS
float aggroRadius = 0.0f;
float tetherRadius = 0.0f;
// tether.onEnter: GoTo { speed = Set.tetherSpeed }
float tetherSpeed = 1.0f;
// Meander: math.random(-r, r) on x and z, so it is an integer
int32_t wanderRadius = 0;
// AGGRO
Aggression aggression = Aggression::Neutral;
// FollowTarget { radius = AggroDist, speed = AggroSpeed * 2 } when both are set
std::optional<float> aggroDist;
std::optional<float> aggroSpeed;
bool aggroEmote = false;
std::u16string aggroEmoteType;
float aggroEmoteDelay = 1.0f;
// MOVEMENT
MovementType movementType = MovementType::None;
int32_t wanderChance = 0;
// math.random(WanderDelayMin, WanderDelayMax): integers
int32_t wanderDelayMin = 0;
int32_t wanderDelayMax = 0;
float wanderSpeed = 1.0f;
bool wanderEmote = false;
// WEmote_2 set: two weighted emotes, WEmote_1 is the first one's weight
bool wanderEmoteTwo = false;
int32_t wanderEmoteWeight = 0;
std::u16string wanderEmoteType1;
std::u16string wanderEmoteType2;
// Hand fights to the object's own combat AI (the combat state) / no Lua movement states
bool suspendLuaAI = false;
bool suspendLuaMovementAI = false;
// SKILLS: the attack is the object's last skill unless overridden
bool overrideAttackSkill = false;
uint32_t attackSkill = 0;
bool useOptionalTargetOnAttack = false;
// SKILLS block: loadOnce
bool overrideHealth = false;
int32_t health = 1;
};
// What the machine needs from the object it runs on: the script API calls the shared Lua makes
class Host {
public:
virtual ~Host() = default;
virtual NiPoint3 GetPosition() const = 0;
virtual NiQuaternion GetRotation() const = 0;
virtual void SetRotation(const NiQuaternion& rotation) = 0;
// self:GoTo { speed, target }
virtual void GoTo(const NiPoint3& target, float speed) = 0;
// self:FollowTarget { targetID, radius, speed, keepFollowing }
virtual void FollowTarget(LWOOBJID target, float radius, float speed, bool keepFollowing) = 0;
// self:FaceTarget { target, keepFacingTarget = true }
virtual void FaceTarget(LWOOBJID target) = 0;
virtual void StopPathing() = 0;
virtual void AddTimer(const std::string& name, float time) = 0;
virtual void CancelTimer(const std::string& name) = 0;
virtual void SetProximityRadius(float radius, const std::string& name) = 0;
virtual std::vector<LWOOBJID> GetProximityObjects(const std::string& name) const = 0;
virtual bool Exists(LWOOBJID object) const = 0;
// True for an object that does not exist
virtual bool IsDead(LWOOBJID object) const = 0;
virtual bool IsSelfDead() const = 0;
virtual bool IsEnemy(LWOOBJID object) const = 0;
// obj:GetType().objType: the Objects table type ("Enemies", "NPC" for players, "Rebuildables", ...)
virtual std::string GetObjectType(LWOOBJID object) const = 0;
// self:GetSkills().skills, in order
virtual std::vector<uint32_t> GetSkills() const = 0;
// self:GetSkillInfo { skillID }.cooldown
virtual float GetSkillCooldown(uint32_t skill) const = 0;
virtual void CastSkill(uint32_t skill, LWOOBJID optionalTarget) = 0;
virtual void SetImmunity(bool immune) = 0;
// self:SetHealth { health = self:GetMaxHealth{}.health }
virtual void RestoreHealth() = 0;
// Set.OverRideHealth: SetMaxHealth and SetHealth
virtual void SetMaxHealth(int32_t health) = 0;
// PlayFXEffect { priority = 1.5, effectType }
virtual void PlayEmote(const std::u16string& effectType) = 0;
// self:Die { killerID = self, killType = "SILENT" }
virtual void DieSilently() = 0;
// math.random(min, max), both inclusive
virtual int32_t Random(int32_t min, int32_t max) = 0;
};
class Machine {
public:
Machine(Host& host, const Settings& settings) : m_Host(host), m_Set(settings) {}
// loadOnce, getVarables, CreateStates and oStart
void Start();
// setState: onExit of the current state, then onEnter of the new one (again for the same state)
void SetState(State state);
State GetState() const { return m_State; }
// The messages the states and o_onEvent handle
void OnProximityEnter(LWOOBJID object, const std::string& name);
void OnHit(LWOOBJID attacker);
// The state's onTimerDone; false when it has none for the timer (the script's onTimerDone runs then)
bool OnStateTimerDone(const std::string& name);
// o_onEvent onTimerDone, the script's one unless it has its own
void OnTimerDone(const std::string& name);
void OnArrived();
void OnCancelled();
void OnLeftTetherRadius();
void OnDeath();
// o_onEvent onNotifyObject
void OnNotifyObject(const std::string& name);
// onResetScriptedAIState
void ResetState() { SetResetState(); }
// The tether check, every frame: onLeftTetherRadius once it leaves the radius
void Update();
// self:SetTetherPoint { tetherPt, radius }; radius 0 turns it off
void SetTetherPoint(const NiPoint3& point, float radius);
const Settings& GetSettings() const { return m_Set; }
LWOOBJID GetAttackingTarget() const { return m_AttackingTarget; }
const NiPoint3& GetHomePoint() const { return m_Home; }
const NiPoint3& GetMeanderPoint() const { return m_MeanderPoint; }
const NiPoint3& GetTetherPoint() const { return m_TetherPoint; }
float GetTetherRadius() const { return m_TetherRadius; }
int32_t GetAggroTarget() const { return m_AggroTarget; }
bool GetReadyToAttack() const { return m_ReadyToAttack; }
bool GetAggroed() const { return m_Aggroed; }
bool GetAiDisabled() const { return m_AiDisabled; }
std::optional<float> GetSkillTime() const { return m_SkillTime; }
private:
bool HasState(State state) const;
void EnterState(State state);
void ExitState(State state);
// o_mis
void LoadOnce();
void GetVarables();
void SetResetState();
void StoreHomePoint();
// o_Movement / o_main
void MeanderEnter();
void MeanderArrived();
void TetherEnter();
void TetherArrived();
void GoHomeEnter();
void GoHomeArrived();
void AiDisableEnter();
void AiEnableEnter();
// o_main / o_onEvent
void AggroEnter();
void AggroTimerDone(const std::string& name);
void DoAttack();
bool CanAttackTarget();
void ProximityPuls();
bool IsAttackable(LWOOBJID object) const;
void AggroEmote(LWOOBJID target);
Host& m_Host;
Settings m_Set;
State m_State = State::None;
// storeHomePoint: myx/myy/myz and rot
NiPoint3 m_Home = NiPoint3Constant::ZERO;
NiQuaternion m_HomeRotation = QuatUtils::IDENTITY;
// storeMeanderPoint: Meadx/Meady/Meadz
NiPoint3 m_MeanderPoint = NiPoint3Constant::ZERO;
NiPoint3 m_TetherPoint = NiPoint3Constant::ZERO;
float m_TetherRadius = 0.0f;
bool m_InsideTether = true;
// The global myPos the Meander state leaves behind, which onOnHit tethers to
std::optional<NiPoint3> m_GlobalMyPos;
// The vars: AttackingTarget ("NoTarget" is LWOOBJID_EMPTY), aggrotarget (0 none, 1 fighting, 2 tethering)
LWOOBJID m_AttackingTarget = LWOOBJID_EMPTY;
int32_t m_AggroTarget = 0;
bool m_ReadyToAttack = true;
bool m_AggroEmoteDelay = true;
bool m_InPursuit = false;
bool m_AiDisabled = false;
bool m_Aggroed = false;
int32_t m_AggroOnce = 0;
std::optional<float> m_SkillTime;
};
};

View File

@@ -0,0 +1,59 @@
// The movement states of the shared Lua AI: Meander (o_Movement MeanderState), tether and GoHome (o_main)
#include "LuaAIMachine.h"
void LuaAI::Machine::MeanderEnter() {
// getRandomPos: math.random(-radius, radius) on x and z round the meander point, at its height
const auto radius = m_Set.wanderRadius;
const auto& centre = m_MeanderPoint;
m_GlobalMyPos = centre;
NiPoint3 target = centre;
target.x += static_cast<float>(m_Host.Random(-radius, radius));
target.z += static_cast<float>(m_Host.Random(-radius, radius));
m_Host.GoTo(target, m_Set.wanderSpeed);
}
void LuaAI::Machine::MeanderArrived() {
// getRandomDelay: math.random(WanderDelayMin, WanderDelayMax), an empty interval is an error there
if (m_Set.wanderDelayMin > m_Set.wanderDelayMax) return;
const auto delay = m_Host.Random(m_Set.wanderDelayMin, m_Set.wanderDelayMax);
m_Host.CancelTimer(Timers::MEANDER_PAUSE);
// The original's "if ran < 1 then local ran = 1 end" sets a new local, so a delay under 1 stays
m_Host.AddTimer(Timers::MEANDER_PAUSE, static_cast<float>(delay));
if (!m_Set.wanderEmote) return;
if (m_Host.Random(1, 100) > m_Set.wanderChance) return;
// With one emote the original calls self:GwetVar, an error before it plays anything
if (!m_Set.wanderEmoteTwo) return;
const auto roll = m_Host.Random(1, 100);
const auto weight = m_Set.wanderEmoteWeight;
const auto secondWeight = 100 - weight;
if (weight < 50) {
m_Host.PlayEmote(roll > 0 && roll <= weight ? m_Set.wanderEmoteType1 : m_Set.wanderEmoteType2);
} else if (secondWeight <= 50) {
m_Host.PlayEmote(roll > secondWeight && roll < 100 ? m_Set.wanderEmoteType1 : m_Set.wanderEmoteType2);
}
}
void LuaAI::Machine::TetherEnter() {
m_AggroTarget = 2;
m_Host.SetImmunity(true);
m_Host.GoTo(m_Home, m_Set.tetherSpeed);
}
void LuaAI::Machine::TetherArrived() {
m_AggroTarget = 0;
m_Host.RestoreHealth();
m_Host.SetImmunity(false);
GetVarables();
SetResetState();
}
void LuaAI::Machine::GoHomeEnter() {
m_Host.GoTo(m_Home, GO_HOME_SPEED);
}
void LuaAI::Machine::GoHomeArrived() {
m_Host.SetRotation(m_HomeRotation);
SetResetState();
}