Files
DarkflameServer/dScripts/ai/LuaAI/LuaAIWayPoints.cpp
Aaron Kimbrell 06e9778076 feat(ai): shared Lua AI waypoint paths and template timers
Port o_WayPointEvents and the waypoint parts of o_mis, o_StateCreate and
o_onEvent: Set.WayPointSet / attached_path / groupID, the WayPointEvent
state and its actions, SavedWP and the last point detaching the path, the
startpath fire event and SetStoreTimmer / CancelTemplateTimers. The
binding reports MovementAIComponent waypoints as arrivals without
actions, since the movement AI already runs the waypoint commands.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 23:42:38 -06:00

119 lines
4.2 KiB
C++

#include "LuaAIMachine.h"
#include "GeneralUtils.h"
// o_WayPointEvents and the waypoint parts of o_mis, o_StateCreate and o_onEvent
// The global onArrived: the states without an onArrived of their own get the waypoints of the path
void LuaAI::Machine::WayPointArrived(const Arrived& msg) {
if (m_Set.suspendLuaMovementAI || m_AiDisabled) return;
if (!msg.isLastPoint) {
m_SavedWP = msg.wayPoint;
m_PathName = msg.pathName;
} else {
m_SavedWP.reset();
m_PathName.clear();
m_AttachedPath.clear();
}
if (msg.actions) {
m_Actions = *msg.actions;
SetState(State::WayPointEvent);
} else {
m_Host.ContinueWaypoints();
}
}
// WayPointEvent.onEnter: run action WPEvent_NUM, or go on along the path once all have run
void LuaAI::Machine::WayPointEventEnter() {
if (m_WPEventNum <= static_cast<int32_t>(m_Actions.size())) {
const auto& action = m_Actions[m_WPEventNum - 1];
WayPointEventFunc(action.name, action.value);
} else {
m_WPEventNum = 1;
m_Host.ContinueWaypoints();
}
}
void LuaAI::Machine::NextWayPointEvent() {
m_WPEventNum++;
SetState(State::WayPointEvent);
}
// WayPointEventFunc. An action it does not know, and stop, leave it standing in the state.
void LuaAI::Machine::WayPointEventFunc(const std::string& name, const std::string& value) {
if (m_Set.suspendLuaMovementAI) return;
const bool noValue = value == NO_VALUE;
if (name == "bounce" || name == "groupemote" || name == "setvar" || name == "eqInvent" || name == "unInvent"
|| name == "teleport" || name == "pathspeed" || name == "removeNPC" || name == "spawnOBJ") {
m_Host.RunWaypointAction(name, value, m_SavedWP);
NextWayPointEvent();
} else if (name == "stop") {
m_Host.StopPathing();
} else if (name == "castskill") {
uint32_t skill = 0;
if (!noValue) {
skill = GeneralUtils::TryParse<uint32_t>(value).value_or(0);
} else {
const auto skills = m_Host.GetSkills();
if (!skills.empty()) skill = skills.front();
}
m_Host.CastSkill(skill, LWOOBJID_EMPTY);
NextWayPointEvent();
} else if (name == "delay") {
m_Host.StopPathing();
SetStoreTimer(GeneralUtils::TryParse<float>(value).value_or(0.0f), Timers::DELAY_ACTION);
} else if (name == "femote") {
m_Host.PlayEmote(GeneralUtils::ASCIIToUTF16(value));
NextWayPointEvent();
} else if (name == "emote") {
const auto emote = GeneralUtils::ASCIIToUTF16(value);
m_Host.PlayEmote(emote);
SetStoreTimer(std::max(m_Host.GetAnimationTime(emote), 1.0f), Timers::DELAY_ACTION_EMOTE);
} else if (name == "changeWP") {
// "path" or "path,start"; onTemplateChangeWaypoints is a script's own hook
const auto comma = value.find(',');
m_WPEventNum = 1;
if (comma != std::string::npos) {
FollowNewPath(value.substr(0, comma), GeneralUtils::TryParse<uint32_t>(value.substr(comma + 1)).value_or(0));
} else {
FollowNewPath(value, 0);
}
} else if (name == "DeleteSelf" || name == "removeself") {
m_Host.RunWaypointAction(name, value, m_SavedWP);
} else if (name == "killself") {
if (noValue) {
m_Host.DieSilently();
} else {
const auto delay = GeneralUtils::TryParse<float>(value).value_or(0.0f);
if (delay >= 1.0f) SetStoreTimer(delay, Timers::DELAY_KILL_SELF);
}
}
}
// self:FollowWaypoints {}: on along the attached path, from the waypoint after the last one reached
void LuaAI::Machine::FollowWaypoints() {
m_Host.FollowWaypoints(m_AttachedPath, m_SavedWP ? *m_SavedWP + 1 : m_AttachedPathStart);
}
void LuaAI::Machine::FollowNewPath(const std::string& path, const uint32_t start) {
m_AttachedPath = path;
m_AttachedPathStart = start;
m_SavedWP.reset();
m_Host.FollowWaypoints(path, start);
}
void LuaAI::Machine::OnFireEvent(const std::string& args) {
const auto parts = GeneralUtils::SplitString(args, ',');
if (parts.empty() || parts[0] != "startpath") return;
FollowNewPath(parts.size() > 1 ? parts[1] : "", parts.size() > 2 ? GeneralUtils::TryParse<uint32_t>(parts[2]).value_or(0) : 0);
}
void LuaAI::Machine::SetStoreTimer(const float time, const std::string& name) {
m_Host.AddTimer(name, time);
if (std::ranges::find(m_TemplateTimers, name) == m_TemplateTimers.end()) m_TemplateTimers.push_back(name);
}
void LuaAI::Machine::CancelTemplateTimers() {
for (const auto& name : m_TemplateTimers) m_Host.CancelTimer(name);
}