#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"; } 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)); 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: // Dead.onEnter: getVarables and CancelTemplateTimers (only the waypoint timers are template timers) GetVarables(); 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); } void LuaAI::Machine::LoadOnce() { m_AttackingTarget = LWOOBJID_EMPTY; // 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; // Set.OverRideName renames it; names come from the client's locale here, so it is not ported if (m_Set.overrideHealth) m_Host.SetMaxHealth(m_Set.health); } void LuaAI::Machine::GetVarables() { m_ReadyToAttack = true; 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 && 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() { switch (m_State) { case State::Meander: MeanderArrived(); break; case State::Aggro: DoAttack(); break; case State::Tether: TetherArrived(); break; case State::GoHome: GoHomeArrived(); break; // The global onArrived only handles waypoint paths default: 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); } } void LuaAI::Machine::OnLeftTetherRadius() { m_AttackingTarget = LWOOBJID_EMPTY; 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); m_AiDisabled = true; m_Host.StopPathing(); } void LuaAI::Machine::AiEnableEnter() { m_AiDisabled = false; SetResetState(); }