From 9c05983b2a03eb6d89575f9a1d75454a8bce69e7 Mon Sep 17 00:00:00 2001 From: sogladev Date: Sun, 22 Mar 2026 21:45:31 +0100 Subject: [PATCH] fix(Core/SmartAI): despawn follow if player goes out of range / offline (#24450) --- src/server/game/AI/SmartScripts/SmartAI.cpp | 57 +++++++++++++++------ src/server/game/AI/SmartScripts/SmartAI.h | 2 + 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/server/game/AI/SmartScripts/SmartAI.cpp b/src/server/game/AI/SmartScripts/SmartAI.cpp index 2c165c5266..fc4481d505 100644 --- a/src/server/game/AI/SmartScripts/SmartAI.cpp +++ b/src/server/game/AI/SmartScripts/SmartAI.cpp @@ -109,6 +109,45 @@ void SmartAI::UpdateDespawn(const uint32 diff) mDespawnTime -= diff; } +void SmartAI::UpdateFollow(const uint32 diff) +{ + if (!mFollowGuid) + return; + + if (mFollowArrivedTimer < diff) + { + if (me->FindNearestCreature(mFollowArrivedEntry, INTERACTION_DISTANCE, mFollowArrivedAlive)) + StopFollow(true); + else + mFollowArrivedTimer = 1000; + } + else + mFollowArrivedTimer -= diff; + + if (mFollowGuid.IsPlayer()) + { + if (_followCheckTimer < diff) + { + bool shouldDespawn = false; + if (Player* player = ObjectAccessor::FindPlayer(mFollowGuid)) + { + float checkDist = me->GetInstanceScript() ? SMART_ESCORT_MAX_PLAYER_DIST * 2 : SMART_ESCORT_MAX_PLAYER_DIST; + if (!me->IsWithinDistInMap(player, checkDist)) + shouldDespawn = true; + } + else + shouldDespawn = true; + + if (shouldDespawn) + me->DespawnOrUnsummon(); + + _followCheckTimer = 1000; + } + else + _followCheckTimer -= diff; + } +} + WaypointData const* SmartAI::GetNextWayPoint() { if (!mWayPoints || mWayPoints->empty()) @@ -536,23 +575,7 @@ void SmartAI::UpdateAI(uint32 diff) GetScript()->OnUpdate(diff); UpdatePath(diff); UpdateDespawn(diff); - - //TODO move to void - if (mFollowGuid) - { - if (mFollowArrivedTimer < diff) - { - if (me->FindNearestCreature(mFollowArrivedEntry, INTERACTION_DISTANCE, mFollowArrivedAlive)) - { - StopFollow(true); - return; - } - - mFollowArrivedTimer = 1000; - } - else - mFollowArrivedTimer -= diff; - } + UpdateFollow(diff); if (!IsAIControlled()) { diff --git a/src/server/game/AI/SmartScripts/SmartAI.h b/src/server/game/AI/SmartScripts/SmartAI.h index 5165058bb6..34e6aa6586 100644 --- a/src/server/game/AI/SmartScripts/SmartAI.h +++ b/src/server/game/AI/SmartScripts/SmartAI.h @@ -222,6 +222,7 @@ private: bool mIsCharmed; uint32 mFollowCreditType; uint32 mFollowArrivedTimer; + uint32 _followCheckTimer; uint32 mFollowCredit; uint32 mFollowArrivedEntry; bool mFollowArrivedAlive; @@ -253,6 +254,7 @@ private: uint32 mDespawnTime; uint32 mDespawnState; void UpdateDespawn(const uint32 diff); + void UpdateFollow(const uint32 diff); uint32 mEscortInvokerCheckTimer; bool mJustReset;