Files
DarkflameServer/dScripts/ai/LuaAI/LuaAICombat.cpp
Aaron Kimbrell 5334953fe6 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>
2026-11-27 22:50:36 -06:00

131 lines
4.6 KiB
C++

// 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;
}