Files
DarkflameServer/dScripts/ai/LuaAI/LuaAIMachine.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

230 lines
7.2 KiB
C++

#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";
case State::WayPointEvent: return "WayPointEvent";
case State::Follow: return "Follow";
}
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));
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;
}
}
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:
GetVarables();
CancelTemplateTimers();
break;
case State::WayPointEvent: WayPointEventEnter(); break;
case State::Follow: FollowEnter(); 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);
// Way Point Event: attached_path is the one loadOnce took from Set.WayPointSet
if ((!m_Set.wayPointSet.empty() || !m_AttachedPath.empty() || m_Set.hasGroupID) && !m_Set.suspendLuaMovementAI) {
m_HasWayPointState = true;
// A grouped object waits for a startpath event; the path loadOnce started is followed again
if (!m_Set.hasGroupID) FollowWaypoints();
}
}
void LuaAI::Machine::LoadOnce() {
m_AttackingTarget = LWOOBJID_EMPTY;
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;
if (m_Set.overrideName) m_Host.SetName(m_Set.name);
if (m_Set.overrideHealth) m_Host.SetMaxHealth(m_Set.health);
}
void LuaAI::Machine::GetVarables() {
m_ReadyToAttack = true;
m_WPEventNum = 1;
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) {
if (!m_AttachedPath.empty()) {
FollowWaypoints();
return;
}
if (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(const Arrived& msg) {
switch (m_State) {
case State::Meander: MeanderArrived(); break;
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;
}
}
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);
} else if (name == Timers::DELAY_ACTION || name == Timers::DELAY_ACTION_EMOTE) {
if (!m_Set.suspendLuaMovementAI) NextWayPointEvent();
}
}
void LuaAI::Machine::OnLeftTetherRadius() {
m_AttackingTarget = LWOOBJID_EMPTY;
CancelTemplateTimers();
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);
CancelTemplateTimers();
m_AiDisabled = true;
m_Host.StopPathing();
}
void LuaAI::Machine::AiEnableEnter() {
m_AiDisabled = false;
SetResetState();
}