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

89 lines
3.2 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);
}
}
// Follow.onEnter: follow the end of the line of faction 7 objects in the Followers proximity, or be its end
void LuaAI::Machine::FollowEnter() {
const auto self = m_Host.GetObjectID();
for (const auto object : m_Host.GetProximityObjects(FOLLOWERS_PROXIMITY)) {
if (!m_Host.BelongsToFaction(object, FOLLOW_FACTION) || !m_Host.GetPointInLine(object)) continue;
m_Host.SetPointInLine(self, true);
m_Host.SetPointInLine(object, false);
m_EndOfLineTarget = object;
}
m_Host.SetName(FOLLOW_NAME);
// getMyTarget: the myTarget var, which only o_Proximity (not part of this AI) sets, so nil here
constexpr LWOOBJID myTarget = LWOOBJID_EMPTY;
if (m_EndOfLineTarget != LWOOBJID_EMPTY) {
m_Host.SetImFollowing();
m_Host.PlayEmote(FOLLOW_EMOTE);
if (!m_Host.IsFollowingDone(m_EndOfLineTarget)) {
m_Host.FollowTarget(m_EndOfLineTarget, FOLLOW_RADIUS, FOLLOW_SPEED, true);
} else {
m_Host.SetImFollowing();
m_Host.FollowTarget(myTarget, FOLLOW_RADIUS, FOLLOW_SPEED, true);
}
} else {
m_Host.PlayEmote(FOLLOW_EMOTE);
m_Host.SetPointInLine(self, true);
m_Host.SetImFollowing();
m_Host.FollowTarget(myTarget, FOLLOW_RADIUS, FOLLOW_SPEED, true);
}
}
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();
}