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

60 lines
2.1 KiB
C++

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