diff --git a/src/server/game/Movement/MovementGenerators/WaypointMovementGenerator.cpp b/src/server/game/Movement/MovementGenerators/WaypointMovementGenerator.cpp index 926a090ae..03fb5b5e8 100644 --- a/src/server/game/Movement/MovementGenerators/WaypointMovementGenerator.cpp +++ b/src/server/game/Movement/MovementGenerators/WaypointMovementGenerator.cpp @@ -143,18 +143,9 @@ void WaypointMovementGenerator::ProcessWaypointArrival(Creature* creat _waypointDelay = waypoint.Delay; } - // Check if the waypoint path has reached its end and may not repeat. Inform AI. - if ((i_currentNode == i_path->Nodes.size() - 1) && !_repeating && !_done) - { + bool pathEnded = (i_currentNode == i_path->Nodes.size() - 1) && !_repeating && !_done; + if (pathEnded) _done = true; - creature->UpdateCurrentWaypointInfo(0, 0); - - if (CreatureAI* AI = creature->AI()) - { - AI->PathEndReached(i_path->Id); - AI->WaypointPathEnded(waypoint.Id, i_path->Id); - } - } UpdateHomePosition(creature, waypoint); @@ -189,6 +180,20 @@ void WaypointMovementGenerator::ProcessWaypointArrival(Creature* creat AI->SummonMovementInform(creature, WAYPOINT_MOTION_TYPE, waypoint.Id); } + // Path end notifications fire after WaypointReached so that m_path_id + // is still valid when SmartAI checks it for SMART_EVENT_WAYPOINT_REACHED. + if (pathEnded) + { + creature->UpdateCurrentWaypointInfo(0, 0); + + if (CreatureAI* AI = creature->AI()) + AI->PathEndReached(i_path->Id); + + // Re-fetch AI — PathEndReached may have despawned the creature or swapped its AI + if (CreatureAI* AI = creature->AI()) + AI->WaypointPathEnded(waypoint.Id, i_path->Id); + } + // All hooks called and infos updated. Time to increment the waypoint node id if (i_path && !i_path->Nodes.empty()) // ensure that the path has not been changed in one of the hooks. i_currentNode = (i_currentNode + 1) % i_path->Nodes.size(); @@ -422,10 +427,11 @@ bool WaypointMovementGenerator::DoUpdate(Creature* creature, uint32 di _smoothSplineLaunched = false; creature->UpdateCurrentWaypointInfo(0, 0); if (CreatureAI* AI = creature->AI()) - { AI->PathEndReached(i_path->Id); + + // Re-fetch AI — PathEndReached may have despawned the creature or swapped its AI + if (CreatureAI* AI = creature->AI()) AI->WaypointPathEnded(i_path->Nodes.at(i_currentNode).Id, i_path->Id); - } } else {