diff --git a/src/server/game/AI/CoreAI/UnitAI.h b/src/server/game/AI/CoreAI/UnitAI.h
index 1f1a44c429d..a38bf60e632 100644
--- a/src/server/game/AI/CoreAI/UnitAI.h
+++ b/src/server/game/AI/CoreAI/UnitAI.h
@@ -329,6 +329,9 @@ class TC_GAME_API UnitAI
// Called when the dialog status between a player and the creature is requested.
virtual uint32 GetDialogStatus(Player* player);
+ virtual void WaypointStarted(uint32 /*nodeId*/, uint32 /*pathId*/) { }
+ virtual void WaypointReached(uint32 /*nodeId*/, uint32 /*pathId*/) { }
+
private:
UnitAI(UnitAI const& right) = delete;
UnitAI& operator=(UnitAI const& right) = delete;
diff --git a/src/server/game/AI/ScriptedAI/ScriptedEscortAI.cpp b/src/server/game/AI/ScriptedAI/ScriptedEscortAI.cpp
index 6acb4cd446a..b9da6840474 100644
--- a/src/server/game/AI/ScriptedAI/ScriptedEscortAI.cpp
+++ b/src/server/game/AI/ScriptedAI/ScriptedEscortAI.cpp
@@ -15,21 +15,16 @@
* with this program. If not, see .
*/
-/* ScriptData
-SDName: Npc_EscortAI
-SD%Complete: 100
-SDComment:
-SDCategory: Npc
-EndScriptData */
-
#include "ScriptedEscortAI.h"
#include "Creature.h"
#include "Group.h"
#include "Log.h"
#include "Map.h"
#include "MotionMaster.h"
+#include "MovementGenerator.h"
#include "ObjectAccessor.h"
#include "Player.h"
+#include "ScriptSystem.h"
#include "World.h"
enum Points
@@ -38,101 +33,62 @@ enum Points
POINT_HOME = 0xFFFFFE
};
-npc_escortAI::npc_escortAI(Creature* creature) : ScriptedAI(creature),
- m_uiWPWaitTimer(2500),
- m_uiPlayerCheckTimer(1000),
- m_uiEscortState(STATE_ESCORT_NONE),
- MaxPlayerDistance(DEFAULT_MAX_PLAYER_DISTANCE),
- m_pQuestForEscort(nullptr),
- m_bIsActiveAttacker(true),
- m_bIsRunning(false),
- m_bCanInstantRespawn(false),
- m_bCanReturnToStart(false),
- DespawnAtEnd(true),
- DespawnAtFar(true),
- ScriptWP(false),
- HasImmuneToNPCFlags(false)
-{ }
-
-void npc_escortAI::AttackStart(Unit* who)
+EscortAI::EscortAI(Creature* creature) : ScriptedAI(creature), _pauseTimer(2500), _playerCheckTimer(1000), _escortState(STATE_ESCORT_NONE), _maxPlayerDistance(DEFAULT_MAX_PLAYER_DISTANCE),
+ _escortQuest(nullptr), _activeAttacker(true), _running(false), _instantRespawn(false), _returnToStart(false), _despawnAtEnd(true), _despawnAtFar(true), _manualPath(false),
+ _hasImmuneToNPCFlags(false), _started(false), _ended(false), _resume(false)
{
- if (!who)
- return;
-
- if (me->Attack(who, true))
- {
- if (me->GetMotionMaster()->GetCurrentMovementGeneratorType() == POINT_MOTION_TYPE)
- me->GetMotionMaster()->MovementExpired();
-
- if (IsCombatMovementAllowed())
- me->GetMotionMaster()->MoveChase(who);
- }
}
-Player* npc_escortAI::GetPlayerForEscort()
+Player* EscortAI::GetPlayerForEscort()
{
- return ObjectAccessor::GetPlayer(*me, m_uiPlayerGUID);
+ return ObjectAccessor::GetPlayer(*me, _playerGUID);
}
-//see followerAI
-bool npc_escortAI::AssistPlayerInCombatAgainst(Unit* who)
+// see followerAI
+bool EscortAI::AssistPlayerInCombatAgainst(Unit* who)
{
if (!who || !who->GetVictim())
return false;
- //experimental (unknown) flag not present
+ if (me->HasReactState(REACT_PASSIVE))
+ return false;
+
+ // experimental (unknown) flag not present
if (!(me->GetCreatureTemplate()->type_flags & CREATURE_TYPE_FLAG_CAN_ASSIST))
return false;
- //not a player
+ // not a player
if (!who->EnsureVictim()->GetCharmerOrOwnerPlayerOrPlayerItself())
return false;
- //never attack friendly
- if (me->IsFriendlyTo(who))
+ // never attack friendly
+ if (!me->IsValidAssistTarget(who->GetVictim()))
return false;
- //too far away and no free sight?
+ // too far away and no free sight?
if (me->IsWithinDistInMap(who, GetMaxPlayerDistance()) && me->IsWithinLOSInMap(who))
{
- //already fighting someone?
- if (!me->GetVictim())
- {
- AttackStart(who);
- return true;
- }
- else
- {
- me->EngageWithTarget(who);
- return true;
- }
+ me->EngageWithTarget(who);
+ return true;
}
return false;
}
-void npc_escortAI::MoveInLineOfSight(Unit* who)
+void EscortAI::MoveInLineOfSight(Unit* who)
{
- if (me->HasReactState(REACT_AGGRESSIVE) && !me->HasUnitState(UNIT_STATE_STUNNED) && who->isTargetableForAttack() && who->isInAccessiblePlaceFor(me))
- {
- if (HasEscortState(STATE_ESCORT_ESCORTING) && AssistPlayerInCombatAgainst(who))
- return;
+ if (!who)
+ return;
- if (!me->CanFly() && me->GetDistanceZ(who) > CREATURE_Z_ATTACK_RANGE)
- return;
+ if (HasEscortState(STATE_ESCORT_ESCORTING) && AssistPlayerInCombatAgainst(who))
+ return;
- if (me->IsHostileTo(who))
- {
- float fAttackRadius = me->GetAttackDistance(who);
- if (me->IsWithinDistInMap(who, fAttackRadius) && me->IsWithinLOSInMap(who))
- me->EngageWithTarget(who);
- }
- }
+ ScriptedAI::MoveInLineOfSight(who);
}
-void npc_escortAI::JustDied(Unit* /*killer*/)
+void EscortAI::JustDied(Unit* /*killer*/)
{
- if (!HasEscortState(STATE_ESCORT_ESCORTING) || !m_uiPlayerGUID || !m_pQuestForEscort)
+ if (!HasEscortState(STATE_ESCORT_ESCORTING) || !_playerGUID || !_escortQuest)
return;
if (Player* player = GetPlayerForEscort())
@@ -140,24 +96,26 @@ void npc_escortAI::JustDied(Unit* /*killer*/)
if (Group* group = player->GetGroup())
{
for (GroupReference* groupRef = group->GetFirstMember(); groupRef != nullptr; groupRef = groupRef->next())
+ {
if (Player* member = groupRef->GetSource())
if (member->IsInMap(player))
- member->FailQuest(m_pQuestForEscort->GetQuestId());
+ member->FailQuest(_escortQuest->GetQuestId());
+ }
}
else
- player->FailQuest(m_pQuestForEscort->GetQuestId());
+ player->FailQuest(_escortQuest->GetQuestId());
}
}
-void npc_escortAI::JustAppeared()
+void EscortAI::JustAppeared()
{
- m_uiEscortState = STATE_ESCORT_NONE;
+ _escortState = STATE_ESCORT_NONE;
if (!IsCombatMovementAllowed())
SetCombatMovement(true);
- //add a small delay before going to first waypoint, normal in near all cases
- m_uiWPWaitTimer = 2500;
+ // add a small delay before going to first waypoint, normal in near all cases
+ _pauseTimer = 2000;
if (me->GetFaction() != me->GetCreatureTemplate()->faction)
me->RestoreFaction();
@@ -165,14 +123,12 @@ void npc_escortAI::JustAppeared()
Reset();
}
-void npc_escortAI::ReturnToLastPoint()
+void EscortAI::ReturnToLastPoint()
{
- float x, y, z, o;
- me->GetHomePosition(x, y, z, o);
- me->GetMotionMaster()->MovePoint(POINT_LAST_POINT, x, y, z);
+ me->GetMotionMaster()->MovePoint(POINT_LAST_POINT, me->GetHomePosition());
}
-void npc_escortAI::EnterEvadeMode(EvadeReason /*why*/)
+void EscortAI::EnterEvadeMode(EvadeReason /*why*/)
{
me->RemoveAllAuras();
me->GetThreatManager().ClearAllThreat();
@@ -183,27 +139,29 @@ void npc_escortAI::EnterEvadeMode(EvadeReason /*why*/)
{
AddEscortState(STATE_ESCORT_RETURNING);
ReturnToLastPoint();
- TC_LOG_DEBUG("scripts", "EscortAI has left combat and is now returning to last point");
+ TC_LOG_DEBUG("scripts", "EscortAI::EnterEvadeMode: left combat and is now returning to last point");
}
else
{
me->GetMotionMaster()->MoveTargetedHome();
- if (HasImmuneToNPCFlags)
+ if (_hasImmuneToNPCFlags)
me->SetImmuneToNPC(true);
Reset();
}
}
-bool npc_escortAI::IsPlayerOrGroupInRange()
+bool EscortAI::IsPlayerOrGroupInRange()
{
if (Player* player = GetPlayerForEscort())
{
if (Group* group = player->GetGroup())
{
for (GroupReference* groupRef = group->GetFirstMember(); groupRef != nullptr; groupRef = groupRef->next())
+ {
if (Player* member = groupRef->GetSource())
if (me->IsWithinDistInMap(member, GetMaxPlayerDistance()))
return true;
+ }
}
else if (me->IsWithinDistInMap(player, GetMaxPlayerDistance()))
return true;
@@ -212,88 +170,77 @@ bool npc_escortAI::IsPlayerOrGroupInRange()
return false;
}
-void npc_escortAI::UpdateAI(uint32 diff)
+void EscortAI::UpdateAI(uint32 diff)
{
- //Waypoint Updating
- if (HasEscortState(STATE_ESCORT_ESCORTING) && !me->GetVictim() && m_uiWPWaitTimer && !HasEscortState(STATE_ESCORT_RETURNING))
+ // Waypoint Updating
+ if (HasEscortState(STATE_ESCORT_ESCORTING) && !me->IsEngaged() && !HasEscortState(STATE_ESCORT_RETURNING))
{
- if (m_uiWPWaitTimer <= diff)
+ if (_pauseTimer <= diff)
{
- //End of the line
- if (CurrentWP == WaypointList.end())
- {
- if (DespawnAtEnd)
- {
- TC_LOG_DEBUG("scripts", "EscortAI reached end of waypoints");
-
- if (m_bCanReturnToStart)
- {
- float fRetX, fRetY, fRetZ;
- me->GetRespawnPosition(fRetX, fRetY, fRetZ);
-
- me->GetMotionMaster()->MovePoint(POINT_HOME, fRetX, fRetY, fRetZ);
-
- m_uiWPWaitTimer = 0;
-
- TC_LOG_DEBUG("scripts", "EscortAI are returning home to spawn location: %u, %f, %f, %f", POINT_HOME, fRetX, fRetY, fRetZ);
- return;
- }
-
- if (m_bCanInstantRespawn && !sWorld->getBoolConfig(CONFIG_RESPAWN_DYNAMIC_ESCORTNPC))
- {
- me->setDeathState(JUST_DIED);
- me->Respawn();
- }
- else
- {
- if (sWorld->getBoolConfig(CONFIG_RESPAWN_DYNAMIC_ESCORTNPC))
- me->GetMap()->RemoveRespawnTime(SPAWN_TYPE_CREATURE, me->GetSpawnId(), true);
- me->DespawnOrUnsummon();
- }
-
- return;
- }
- else
- {
- TC_LOG_DEBUG("scripts", "EscortAI reached end of waypoints with Despawn off");
-
- return;
- }
- }
-
if (!HasEscortState(STATE_ESCORT_PAUSED))
{
- me->GetMotionMaster()->MovePoint(CurrentWP->id, CurrentWP->x, CurrentWP->y, CurrentWP->z);
- TC_LOG_DEBUG("scripts", "EscortAI start waypoint %u (%f, %f, %f).", CurrentWP->id, CurrentWP->x, CurrentWP->y, CurrentWP->z);
+ _pauseTimer = 0;
- WaypointStart(CurrentWP->id);
+ if (_ended)
+ {
+ _ended = false;
+ me->GetMotionMaster()->MoveIdle();
- m_uiWPWaitTimer = 0;
+ if (_despawnAtEnd)
+ {
+ TC_LOG_DEBUG("scripts", "EscortAI::UpdateAI: reached end of waypoints, despawning at end");
+ if (_returnToStart)
+ {
+ Position respawnPosition;
+ float orientation = 0.f;
+ me->GetRespawnPosition(respawnPosition.m_positionX, respawnPosition.m_positionY, respawnPosition.m_positionZ, &orientation);
+ respawnPosition.SetOrientation(orientation);
+ me->GetMotionMaster()->MovePoint(POINT_HOME, respawnPosition);
+ TC_LOG_DEBUG("scripts", "EscortAI::UpdateAI: returning to spawn location: %s", respawnPosition.ToString().c_str());
+ }
+ else if (_instantRespawn)
+ me->Respawn(true);
+ else
+ me->DespawnOrUnsummon();
+ }
+ TC_LOG_DEBUG("scripts", "EscortAI::UpdateAI: reached end of waypoints");
+ RemoveEscortState(STATE_ESCORT_ESCORTING);
+ return;
+ }
+
+ if (!_started)
+ {
+ _started = true;
+ me->GetMotionMaster()->MovePath(_path, false);
+ }
+ else if (_resume)
+ {
+ _resume = false;
+ if (MovementGenerator* movementGenerator = me->GetMotionMaster()->GetMotionSlot(MOTION_SLOT_IDLE))
+ movementGenerator->Resume(0);
+ }
}
}
else
- m_uiWPWaitTimer -= diff;
+ _pauseTimer -= diff;
}
- //Check if player or any member of his group is within range
- if (HasEscortState(STATE_ESCORT_ESCORTING) && !m_uiPlayerGUID.IsEmpty() && !me->GetVictim() && !HasEscortState(STATE_ESCORT_RETURNING))
+ // Check if player or any member of his group is within range
+ if (_despawnAtFar && HasEscortState(STATE_ESCORT_ESCORTING) && !_playerGUID.IsEmpty() && !me->GetVictim() && !HasEscortState(STATE_ESCORT_RETURNING))
{
- if (m_uiPlayerCheckTimer <= diff)
+ if (_playerCheckTimer <= diff)
{
- if (DespawnAtFar && !IsPlayerOrGroupInRange())
+ if (!IsPlayerOrGroupInRange())
{
- TC_LOG_DEBUG("scripts", "EscortAI failed because player/group was to far away or not found");
+ TC_LOG_DEBUG("scripts", "EscortAI::UpdateAI: failed because player/group was to far away or not found");
bool isEscort = false;
- if (CreatureData const* cdata = me->GetCreatureData())
- isEscort = (sWorld->getBoolConfig(CONFIG_RESPAWN_DYNAMIC_ESCORTNPC) && (cdata->spawnGroupData->flags & SPAWNGROUP_FLAG_ESCORTQUESTNPC));
+ if (CreatureData const* creatureData = me->GetCreatureData())
+ isEscort = (sWorld->getBoolConfig(CONFIG_RESPAWN_DYNAMIC_ESCORTNPC) && (creatureData->spawnGroupData->flags & SPAWNGROUP_FLAG_ESCORTQUESTNPC));
- if (m_bCanInstantRespawn && !isEscort)
- {
- me->setDeathState(JUST_DIED);
- me->Respawn();
- }
- else if (m_bCanInstantRespawn && isEscort)
+ if (_instantRespawn && !isEscort)
+ me->DespawnOrUnsummon(0, Seconds(1));
+ else if (_instantRespawn && isEscort)
me->GetMap()->RemoveRespawnTime(SPAWN_TYPE_CREATURE, me->GetSpawnId(), true);
else
me->DespawnOrUnsummon();
@@ -301,16 +248,16 @@ void npc_escortAI::UpdateAI(uint32 diff)
return;
}
- m_uiPlayerCheckTimer = 1000;
+ _playerCheckTimer = 1000;
}
else
- m_uiPlayerCheckTimer -= diff;
+ _playerCheckTimer -= diff;
}
UpdateEscortAI(diff);
}
-void npc_escortAI::UpdateEscortAI(uint32 /*diff*/)
+void EscortAI::UpdateEscortAI(uint32 /*diff*/)
{
if (!UpdateVictim())
return;
@@ -318,121 +265,103 @@ void npc_escortAI::UpdateEscortAI(uint32 /*diff*/)
DoMeleeAttackIfReady();
}
-void npc_escortAI::MovementInform(uint32 moveType, uint32 pointId)
+void EscortAI::MovementInform(uint32 type, uint32 id)
{
- if (moveType != POINT_MOTION_TYPE || !HasEscortState(STATE_ESCORT_ESCORTING))
+ // no action allowed if there is no escort
+ if (!HasEscortState(STATE_ESCORT_ESCORTING))
return;
- //Combat start position reached, continue waypoint movement
- if (pointId == POINT_LAST_POINT)
+ if (type == POINT_MOTION_TYPE)
{
- TC_LOG_DEBUG("scripts", "EscortAI has returned to original position before combat");
+ if (!_pauseTimer)
+ _pauseTimer = 2000;
- me->SetWalk(!m_bIsRunning);
- RemoveEscortState(STATE_ESCORT_RETURNING);
-
- if (!m_uiWPWaitTimer)
- m_uiWPWaitTimer = 1;
- }
- else if (pointId == POINT_HOME)
- {
- TC_LOG_DEBUG("scripts", "EscortAI has returned to original home location and will continue from beginning of waypoint list.");
-
- CurrentWP = WaypointList.begin();
- m_uiWPWaitTimer = 1;
- }
- else
- {
- //Make sure that we are still on the right waypoint
- if (CurrentWP->id != pointId)
+ // continue waypoint movement
+ if (id == POINT_LAST_POINT)
{
- TC_LOG_ERROR("misc", "TSCR ERROR: EscortAI reached waypoint out of order %u, expected %u, creature entry %u", pointId, CurrentWP->id, me->GetEntry());
- return;
+ TC_LOG_DEBUG("scripts", "EscortAI::MovementInform: returned to before combat position");
+ me->SetWalk(!_running);
+ RemoveEscortState(STATE_ESCORT_RETURNING);
}
+ else if (id == POINT_HOME)
+ {
+ TC_LOG_DEBUG("scripts", "EscortAI::MovementInform: returned to home location and restarting waypoint path");
+ _started = false;
+ }
+ }
+ else if (type == WAYPOINT_MOTION_TYPE)
+ {
+ ASSERT(id < _path.nodes.size(), "EscortAI::MovementInform: referenced movement id (%u) points to non-existing node in loaded path", id);
+ WaypointNode waypoint = _path.nodes[id];
- TC_LOG_DEBUG("scripts", "EscortAI Waypoint %u reached", CurrentWP->id);
+ TC_LOG_DEBUG("scripts", "EscortAI::MovementInform: waypoint node %u reached", waypoint.id);
- //Call WP function
- WaypointReached(CurrentWP->id);
-
- m_uiWPWaitTimer = CurrentWP->WaitTimeMs + 1;
-
- ++CurrentWP;
+ // last point
+ if (id == _path.nodes.size() - 1)
+ {
+ _started = false;
+ _ended = true;
+ _pauseTimer = 1000;
+ }
}
}
+///@todo investigate whether if its necessary to handle anything on charm
/*
-void npc_escortAI::OnPossess(bool apply)
+void EscortAI::OnCharmed(bool apply)
{
- // We got possessed in the middle of being escorted, store the point
- // where we left off to come back to when possess is removed
- if (HasEscortState(STATE_ESCORT_ESCORTING))
- {
- if (apply)
- me->GetPosition(LastPos.x, LastPos.y, LastPos.z);
- else
- {
- Returning = true;
- me->GetMotionMaster()->MovementExpired();
- me->GetMotionMaster()->MovePoint(WP_LAST_POINT, LastPos.x, LastPos.y, LastPos.z);
- }
- }
}
*/
-void npc_escortAI::AddWaypoint(uint32 id, float x, float y, float z, uint32 waitTime)
+void EscortAI::AddWaypoint(uint32 id, float x, float y, float z, float orientation/* = 0*/, uint32 waitTime/* = 0*/)
{
- Escort_Waypoint t(id, x, y, z, waitTime);
+ Trinity::NormalizeMapCoord(x);
+ Trinity::NormalizeMapCoord(y);
- WaypointList.push_back(t);
+ WaypointNode waypoint;
+ waypoint.id = id;
+ waypoint.x = x;
+ waypoint.y = y;
+ waypoint.z = z;
+ waypoint.orientation = orientation;
+ waypoint.moveType = _running ? WAYPOINT_MOVE_TYPE_RUN : WAYPOINT_MOVE_TYPE_WALK;
+ waypoint.delay = waitTime;
+ waypoint.eventId = 0;
+ waypoint.eventChance = 100;
+ _path.nodes.push_back(std::move(waypoint));
- // i think SD2 no longer uses this function
- ScriptWP = true;
- /*PointMovement wp;
- wp.m_uiCreatureEntry = me->GetEntry();
- wp.m_uiPointId = id;
- wp.m_fX = x;
- wp.m_fY = y;
- wp.m_fZ = z;
- wp.m_uiWaitTime = WaitTimeMs;
- PointMovementMap[wp.m_uiCreatureEntry].push_back(wp);*/
+ _manualPath = true;
}
-void npc_escortAI::FillPointMovementListForCreature()
+void EscortAI::FillPointMovementListForCreature()
{
- ScriptPointVector const* movePoints = sScriptSystemMgr->GetPointMoveList(me->GetEntry());
- if (!movePoints)
+ WaypointPath const* path = sScriptSystemMgr->GetPath(me->GetEntry());
+ if (!path)
return;
- for (ScriptPointVector::const_iterator itr = movePoints->begin(); itr != movePoints->end(); ++itr)
+ for (WaypointNode const& value : path->nodes)
{
- Escort_Waypoint point(itr->uiPointId, itr->fX, itr->fY, itr->fZ, itr->uiWaitTime);
- WaypointList.push_back(point);
+ WaypointNode node = value;
+ Trinity::NormalizeMapCoord(node.x);
+ Trinity::NormalizeMapCoord(node.y);
+ node.moveType = _running ? WAYPOINT_MOVE_TYPE_RUN : WAYPOINT_MOVE_TYPE_WALK;
+
+ _path.nodes.push_back(std::move(node));
}
}
-void npc_escortAI::SetRun(bool on)
+void EscortAI::SetRun(bool on)
{
- if (on)
- {
- if (!m_bIsRunning)
- me->SetWalk(false);
- else
- TC_LOG_DEBUG("scripts", "EscortAI attempt to set run mode, but is already running.");
- }
- else
- {
- if (m_bIsRunning)
- me->SetWalk(true);
- else
- TC_LOG_DEBUG("scripts", "EscortAI attempt to set walk mode, but is already walking.");
- }
+ if (on && !_running)
+ me->SetWalk(false);
+ else if (!on && _running)
+ me->SetWalk(true);
- m_bIsRunning = on;
+ _running = on;
}
/// @todo get rid of this many variables passed in function.
-void npc_escortAI::Start(bool isActiveAttacker /* = true*/, bool run /* = false */, ObjectGuid playerGUID /* = 0 */, Quest const* quest /* = nullptr */, bool instantRespawn /* = false */, bool canLoopPath /* = false */, bool resetWaypoints /* = true */)
+void EscortAI::Start(bool isActiveAttacker /* = true*/, bool run /* = false */, ObjectGuid playerGUID /* = 0 */, Quest const* quest /* = nullptr */, bool instantRespawn /* = false */, bool canLoopPath /* = false */, bool resetWaypoints /* = true */)
{
// Queue respawn from the point it starts
if (Map* map = me->GetMap())
@@ -452,150 +381,76 @@ void npc_escortAI::Start(bool isActiveAttacker /* = true*/, bool run /* = false
if (me->GetVictim())
{
- TC_LOG_ERROR("scripts.escortai", "TSCR ERROR: EscortAI (script: %s, creature entry: %u) attempts to Start while in combat", me->GetScriptName().c_str(), me->GetEntry());
+ TC_LOG_ERROR("scripts", "EscortAI::Start: (script: %s, creature entry: %u) attempts to Start while in combat", me->GetScriptName().c_str(), me->GetEntry());
return;
}
if (HasEscortState(STATE_ESCORT_ESCORTING))
{
- TC_LOG_ERROR("scripts.escortai", "EscortAI (script: %s, creature entry: %u) attempts to Start while already escorting", me->GetScriptName().c_str(), me->GetEntry());
+ TC_LOG_ERROR("scripts", "EscortAI::Start: (script: %s, creature entry: %u) attempts to Start while already escorting", me->GetScriptName().c_str(), me->GetEntry());
return;
}
- if (!ScriptWP && resetWaypoints) // sd2 never adds wp in script, but tc does
- {
- if (!WaypointList.empty())
- WaypointList.clear();
+ if (!_manualPath && resetWaypoints)
FillPointMovementListForCreature();
- }
- if (WaypointList.empty())
+ if (_path.nodes.empty())
{
- TC_LOG_ERROR("scripts", "EscortAI (script: %s, creature entry: %u) starts with 0 waypoints (possible missing entry in script_waypoint. Quest: %u).",
- me->GetScriptName().c_str(), me->GetEntry(), quest ? quest->GetQuestId() : 0);
+ TC_LOG_ERROR("scripts", "EscortAI::Start: (script: %s, creature entry: %u) starts with 0 waypoints (possible missing entry in script_waypoint. Quest: %u).", me->GetScriptName().c_str(), me->GetEntry(), quest ? quest->GetQuestId() : 0);
return;
}
- //set variables
- m_bIsActiveAttacker = isActiveAttacker;
- m_bIsRunning = run;
+ // set variables
+ _activeAttacker = isActiveAttacker;
+ _running = run;
+ _playerGUID = playerGUID;
+ _escortQuest = quest;
+ _instantRespawn = instantRespawn;
+ _returnToStart = canLoopPath;
- m_uiPlayerGUID = playerGUID;
- m_pQuestForEscort = quest;
+ if (_returnToStart && _instantRespawn)
+ TC_LOG_DEBUG("scripts", "EscortAI::Start: (script: %s, creature entry: %u) is set to return home after waypoint end and instant respawn at waypoint end. Creature will never despawn.", me->GetScriptName().c_str(), me->GetEntry());
- m_bCanInstantRespawn = instantRespawn;
- m_bCanReturnToStart = canLoopPath;
+ me->GetMotionMaster()->MoveIdle();
+ me->GetMotionMaster()->Clear(MOTION_SLOT_ACTIVE);
- if (m_bCanReturnToStart && m_bCanInstantRespawn)
- TC_LOG_DEBUG("scripts", "EscortAI is set to return home after waypoint end and instant respawn at waypoint end. Creature will never despawn.");
-
- if (me->GetMotionMaster()->GetCurrentMovementGeneratorType() == WAYPOINT_MOTION_TYPE)
- {
- me->GetMotionMaster()->MovementExpired();
- me->GetMotionMaster()->MoveIdle();
- TC_LOG_DEBUG("scripts", "EscortAI start with WAYPOINT_MOTION_TYPE, changed to MoveIdle.");
- }
-
- //disable npcflags
+ // disable npcflags
me->SetNpcFlags(UNIT_NPC_FLAG_NONE);
me->SetNpcFlags2(UNIT_NPC_FLAG_2_NONE);
if (me->IsImmuneToNPC())
{
- HasImmuneToNPCFlags = true;
+ _hasImmuneToNPCFlags = true;
me->SetImmuneToNPC(false);
}
- TC_LOG_DEBUG("scripts", "EscortAI started with " UI64FMTD " waypoints. ActiveAttacker = %d, Run = %d, %s", uint64(WaypointList.size()), m_bIsActiveAttacker, m_bIsRunning, m_uiPlayerGUID.ToString().c_str());
+ TC_LOG_DEBUG("scripts", "EscortAI::Start: (script: %s, creature entry: %u) started with %u waypoints. ActiveAttacker = %d, Run = %d, Player = %s", me->GetScriptName().c_str(), me->GetEntry(), uint32(_path.nodes.size()), _activeAttacker, _running, _playerGUID.ToString().c_str());
- CurrentWP = WaypointList.begin();
-
- //Set initial speed
- if (m_bIsRunning)
- me->SetWalk(false);
- else
- me->SetWalk(true);
+ // set initial speed
+ me->SetWalk(!_running);
+ _started = false;
AddEscortState(STATE_ESCORT_ESCORTING);
}
-void npc_escortAI::SetEscortPaused(bool on)
+void EscortAI::SetEscortPaused(bool on)
{
if (!HasEscortState(STATE_ESCORT_ESCORTING))
return;
if (on)
+ {
AddEscortState(STATE_ESCORT_PAUSED);
+ if (MovementGenerator* movementGenerator = me->GetMotionMaster()->GetMotionSlot(MOTION_SLOT_IDLE))
+ movementGenerator->Pause(0);
+ }
else
+ {
RemoveEscortState(STATE_ESCORT_PAUSED);
-}
-
-bool npc_escortAI::SetNextWaypoint(uint32 pointId, float x, float y, float z, float orientation)
-{
- me->UpdatePosition(x, y, z, orientation);
- return SetNextWaypoint(pointId, false, true);
-}
-
-bool npc_escortAI::SetNextWaypoint(uint32 pointId, bool setPosition, bool resetWaypointsOnFail)
-{
- if (!WaypointList.empty())
- WaypointList.clear();
-
- FillPointMovementListForCreature();
-
- if (WaypointList.empty())
- return false;
-
- size_t const size = WaypointList.size();
- Escort_Waypoint waypoint(0, 0, 0, 0, 0);
- do
- {
- waypoint = WaypointList.front();
- WaypointList.pop_front();
- if (waypoint.id == pointId)
- {
- if (setPosition)
- me->UpdatePosition(waypoint.x, waypoint.y, waypoint.z, me->GetOrientation());
-
- CurrentWP = WaypointList.begin();
- return true;
- }
+ _resume = true;
}
- while (!WaypointList.empty());
-
- // we failed.
- // we reset the waypoints in the start; if we pulled any, reset it again
- if (resetWaypointsOnFail && size != WaypointList.size())
- {
- if (!WaypointList.empty())
- WaypointList.clear();
-
- FillPointMovementListForCreature();
- }
-
- return false;
}
-bool npc_escortAI::GetWaypointPosition(uint32 pointId, float& x, float& y, float& z)
-{
- ScriptPointVector const* waypoints = sScriptSystemMgr->GetPointMoveList(me->GetEntry());
- if (!waypoints)
- return false;
-
- for (ScriptPointVector::const_iterator itr = waypoints->begin(); itr != waypoints->end(); ++itr)
- {
- if (itr->uiPointId == pointId)
- {
- x = itr->fX;
- y = itr->fY;
- z = itr->fZ;
- return true;
- }
- }
-
- return false;
-}
-
-bool npc_escortAI::IsEscortNPC(bool onlyIfActive) const
+bool EscortAI::IsEscortNPC(bool onlyIfActive) const
{
if (!onlyIfActive)
return true;
diff --git a/src/server/game/AI/ScriptedAI/ScriptedEscortAI.h b/src/server/game/AI/ScriptedAI/ScriptedEscortAI.h
index b91bdd21005..4020d90815e 100644
--- a/src/server/game/AI/ScriptedAI/ScriptedEscortAI.h
+++ b/src/server/game/AI/ScriptedAI/ScriptedEscortAI.h
@@ -19,93 +19,59 @@
#define SC_ESCORTAI_H
#include "ScriptedCreature.h"
-#include "ScriptSystem.h"
+#include "WaypointDefines.h"
class Quest;
#define DEFAULT_MAX_PLAYER_DISTANCE 50
-struct Escort_Waypoint
+enum EscortState : uint32
{
- Escort_Waypoint(uint32 _id, float _x, float _y, float _z, uint32 _w)
- {
- id = _id;
- x = _x;
- y = _y;
- z = _z;
- WaitTimeMs = _w;
- }
-
- uint32 id;
- float x;
- float y;
- float z;
- uint32 WaitTimeMs;
+ STATE_ESCORT_NONE = 0x00, // nothing in progress
+ STATE_ESCORT_ESCORTING = 0x01, // escort is in progress
+ STATE_ESCORT_RETURNING = 0x02, // escort is returning after being in combat
+ STATE_ESCORT_PAUSED = 0x04 // escort is paused, wont continue with next waypoint
};
-enum eEscortState
-{
- STATE_ESCORT_NONE = 0x000, //nothing in progress
- STATE_ESCORT_ESCORTING = 0x001, //escort are in progress
- STATE_ESCORT_RETURNING = 0x002, //escort is returning after being in combat
- STATE_ESCORT_PAUSED = 0x004 //will not proceed with waypoints before state is removed
-};
-
-struct TC_GAME_API npc_escortAI : public ScriptedAI
+struct TC_GAME_API EscortAI : public ScriptedAI
{
public:
- explicit npc_escortAI(Creature* creature);
- ~npc_escortAI() { }
-
- // CreatureAI functions
- void AttackStart(Unit* who) override;
+ explicit EscortAI(Creature* creature);
+ ~EscortAI() { }
+ void UpdateAI(uint32 diff) override; // the "internal" update, calls UpdateEscortAI()
void MoveInLineOfSight(Unit* who) override;
-
void JustDied(Unit*) override;
-
void JustAppeared() override;
-
void ReturnToLastPoint();
-
void EnterEvadeMode(EvadeReason /*why*/ = EVADE_REASON_OTHER) override;
-
- void UpdateAI(uint32 diff) override; // the "internal" update, calls UpdateEscortAI()
- virtual void UpdateEscortAI(uint32 diff); // used when it's needed to add code in update (abilities, scripted events, etc)
-
void MovementInform(uint32, uint32) override;
- // EscortAI functions
- void AddWaypoint(uint32 id, float x, float y, float z, uint32 waitTime = 0); // waitTime is in ms
+ virtual void UpdateEscortAI(uint32 diff); // used when it's needed to add code in update (abilities, scripted events, etc)
- //this will set the current position to x/y/z/o, and the current WP to pointId.
- bool SetNextWaypoint(uint32 pointId, float x, float y, float z, float orientation);
-
- //this will set the current position to WP start position (if setPosition == true),
- //and the current WP to pointId
- bool SetNextWaypoint(uint32 pointId, bool setPosition = true, bool resetWaypointsOnFail = true);
-
- bool GetWaypointPosition(uint32 pointId, float& x, float& y, float& z);
-
- virtual void WaypointReached(uint32 pointId) = 0;
- virtual void WaypointStart(uint32 /*pointId*/) { }
+ void AddWaypoint(uint32 id, float x, float y, float z, float orientation = 0.f, uint32 waitTime = 0); // waitTime is in ms
void Start(bool isActiveAttacker = true, bool run = false, ObjectGuid playerGUID = ObjectGuid::Empty, Quest const* quest = nullptr, bool instantRespawn = false, bool canLoopPath = false, bool resetWaypoints = true);
void SetRun(bool on = true);
+
void SetEscortPaused(bool on);
+ void SetPauseTimer(uint32 Timer) { _pauseTimer = Timer; }
- bool HasEscortState(uint32 escortState) { return (m_uiEscortState & escortState) != 0; }
- virtual bool IsEscorted() const override { return (m_uiEscortState & STATE_ESCORT_ESCORTING); }
+ bool HasEscortState(uint32 escortState) { return (_escortState & escortState) != 0; }
+ virtual bool IsEscorted() const override { return (_escortState & STATE_ESCORT_ESCORTING); }
- void SetMaxPlayerDistance(float newMax) { MaxPlayerDistance = newMax; }
- float GetMaxPlayerDistance() const { return MaxPlayerDistance; }
+ void SetMaxPlayerDistance(float newMax) { _maxPlayerDistance = newMax; }
+ float GetMaxPlayerDistance() const { return _maxPlayerDistance; }
+
+ void SetDespawnAtEnd(bool despawn) { _despawnAtEnd = despawn; }
+ void SetDespawnAtFar(bool despawn) { _despawnAtFar = despawn; }
+
+ bool GetAttack() const { return _activeAttacker; } // used in EnterEvadeMode override
+ void SetCanAttack(bool attack) { _activeAttacker = attack; }
+
+ ObjectGuid GetEventStarterGUID() const { return _playerGUID; }
- void SetDespawnAtEnd(bool despawn) { DespawnAtEnd = despawn; }
- void SetDespawnAtFar(bool despawn) { DespawnAtFar = despawn; }
- bool GetAttack() const { return m_bIsActiveAttacker; }//used in EnterEvadeMode override
- void SetCanAttack(bool attack) { m_bIsActiveAttacker = attack; }
- ObjectGuid GetEventStarterGUID() const { return m_uiPlayerGUID; }
virtual bool IsEscortNPC(bool isEscorting) const override;
protected:
@@ -116,27 +82,29 @@ struct TC_GAME_API npc_escortAI : public ScriptedAI
bool IsPlayerOrGroupInRange();
void FillPointMovementListForCreature();
- void AddEscortState(uint32 escortState) { m_uiEscortState |= escortState; }
- void RemoveEscortState(uint32 escortState) { m_uiEscortState &= ~escortState; }
+ void AddEscortState(uint32 escortState) { _escortState |= escortState; }
+ void RemoveEscortState(uint32 escortState) { _escortState &= ~escortState; }
- ObjectGuid m_uiPlayerGUID;
- uint32 m_uiWPWaitTimer;
- uint32 m_uiPlayerCheckTimer;
- uint32 m_uiEscortState;
- float MaxPlayerDistance;
+ ObjectGuid _playerGUID;
+ uint32 _pauseTimer;
+ uint32 _playerCheckTimer;
+ uint32 _escortState;
+ float _maxPlayerDistance;
- Quest const* m_pQuestForEscort; //generally passed in Start() when regular escort script.
+ Quest const* _escortQuest; // generally passed in Start() when regular escort script.
- std::list WaypointList;
- std::list::iterator CurrentWP;
+ WaypointPath _path;
- bool m_bIsActiveAttacker; //obsolete, determined by faction.
- bool m_bIsRunning; //all creatures are walking by default (has flag MOVEMENTFLAG_WALK)
- bool m_bCanInstantRespawn; //if creature should respawn instantly after escort over (if not, database respawntime are used)
- bool m_bCanReturnToStart; //if creature can walk same path (loop) without despawn. Not for regular escort quests.
- bool DespawnAtEnd;
- bool DespawnAtFar;
- bool ScriptWP;
- bool HasImmuneToNPCFlags;
+ bool _activeAttacker; // obsolete, determined by faction.
+ bool _running; // all creatures are walking by default (has flag MOVEMENTFLAG_WALK)
+ bool _instantRespawn; // if creature should respawn instantly after escort over (if not, database respawntime are used)
+ bool _returnToStart; // if creature can walk same path (loop) without despawn. Not for regular escort quests.
+ bool _despawnAtEnd;
+ bool _despawnAtFar;
+ bool _manualPath;
+ bool _hasImmuneToNPCFlags;
+ bool _started;
+ bool _ended;
+ bool _resume;
};
#endif
diff --git a/src/server/game/AI/ScriptedAI/ScriptedFollowerAI.h b/src/server/game/AI/ScriptedAI/ScriptedFollowerAI.h
index 1a35f9c77ad..0212d8e61d3 100644
--- a/src/server/game/AI/ScriptedAI/ScriptedFollowerAI.h
+++ b/src/server/game/AI/ScriptedAI/ScriptedFollowerAI.h
@@ -40,8 +40,6 @@ class TC_GAME_API FollowerAI : public ScriptedAI
explicit FollowerAI(Creature* creature);
~FollowerAI() { }
- //virtual void WaypointReached(uint32 uiPointId) = 0;
-
void MovementInform(uint32 motionType, uint32 pointId) override;
void AttackStart(Unit*) override;
diff --git a/src/server/game/AI/SmartScripts/SmartAI.cpp b/src/server/game/AI/SmartScripts/SmartAI.cpp
index ff2aede2097..9bd9fa7d3cc 100644
--- a/src/server/game/AI/SmartScripts/SmartAI.cpp
+++ b/src/server/game/AI/SmartScripts/SmartAI.cpp
@@ -16,8 +16,9 @@
*/
#include "SmartAI.h"
-#include "DB2Structure.h"
#include "Creature.h"
+#include "CreatureGroups.h"
+#include "DB2Structure.h"
#include "GameObject.h"
#include "Group.h"
#include "Log.h"
@@ -28,54 +29,12 @@
#include "ScriptMgr.h"
#include "Vehicle.h"
-SmartAI::SmartAI(Creature* c) : CreatureAI(c)
+SmartAI::SmartAI(Creature* creature) : CreatureAI(creature), mIsCharmed(false), mFollowCreditType(0), mFollowArrivedTimer(0), mFollowCredit(0), mFollowArrivedEntry(0), mFollowDist(0.f), mFollowAngle(0.f),
+ _escortState(SMART_ESCORT_NONE), _escortNPCFlags(0), _escortInvokerCheckTimer(1000), _currentWaypointNode(0), _waypointReached(false), _waypointPauseTimer(0), _waypointPauseForced(false), _repeatWaypointPath(false),
+ _OOCReached(false), _waypointPathEnded(false), mRun(true), mEvadeDisabled(false), mCanAutoAttack(true), mCanCombatMove(true), mInvincibilityHpLevel(0), mDespawnTime(0), mDespawnState(0), mJustReset(false),
+ mConditionsTimer(0), _gossipReturn(false)
{
- mIsCharmed = false;
- // copy script to local (protection for table reload)
-
- mWayPoints = nullptr;
- mEscortState = SMART_ESCORT_NONE;
- mCurrentWPID = 0;//first wp id is 1 !!
- mWPReached = false;
- mWPPauseTimer = 0;
- mEscortNPCFlags = 0;
- mLastWP = nullptr;
-
- mCanRepeatPath = false;
-
- // spawn in run mode
- me->SetWalk(false);
- mRun = false;
- mEvadeDisabled = false;
-
- mLastOOCPos = me->GetPosition();
-
- mCanAutoAttack = true;
- mCanCombatMove = true;
-
- mForcedPaused = false;
- mLastWPIDReached = 0;
-
- mEscortQuestID = 0;
-
- mDespawnTime = 0;
- mDespawnState = 0;
-
- mEscortInvokerCheckTimer = 1000;
- mFollowGuid.Clear();
- mFollowDist = 0;
- mFollowAngle = 0;
- mFollowCredit = 0;
- mFollowArrivedEntry = 0;
- mFollowCreditType = 0;
- mFollowArrivedTimer = 0;
- mInvincibilityHpLevel = 0;
-
- mJustReset = false;
- mConditionsTimer = 0;
- mHasConditions = sConditionMgr->HasConditionsForNotGroupedEntry(CONDITION_SOURCE_TYPE_CREATURE_TEMPLATE_VEHICLE, c->GetEntry());
-
- _gossipReturn = false;
+ mHasConditions = sConditionMgr->HasConditionsForNotGroupedEntry(CONDITION_SOURCE_TYPE_CREATURE_TEMPLATE_VEHICLE, creature->GetEntry());
}
bool SmartAI::IsAIControlled() const
@@ -101,74 +60,68 @@ void SmartAI::UpdateDespawn(uint32 diff)
} else mDespawnTime -= diff;
}
-WayPoint* SmartAI::GetNextWayPoint()
+void SmartAI::StartPath(bool run/* = false*/, uint32 pathId/* = 0*/, bool repeat/* = false*/, Unit* invoker/* = nullptr*/, uint32 nodeId/* = 1*/)
{
- if (!mWayPoints || mWayPoints->empty())
- return nullptr;
-
- mCurrentWPID++;
- WPPath::const_iterator itr = mWayPoints->find(mCurrentWPID);
- if (itr != mWayPoints->end())
+ if (me->IsInCombat()) // no wp movement in combat
{
- mLastWP = (*itr).second;
- if (mLastWP->id != mCurrentWPID)
- {
- TC_LOG_ERROR("misc", "SmartAI::GetNextWayPoint: Got not expected waypoint id %u, expected %u", mLastWP->id, mCurrentWPID);
- }
- return (*itr).second;
- }
- return nullptr;
-}
-
-void SmartAI::StartPath(bool run, uint32 path, bool repeat, Unit* invoker)
-{
- if (me->IsInCombat())// no wp movement in combat
- {
- TC_LOG_ERROR("misc", "SmartAI::StartPath: Creature entry %u wanted to start waypoint movement while in combat, ignoring.", me->GetEntry());
+ TC_LOG_ERROR("misc", "SmartAI::StartPath: Creature entry %u wanted to start waypoint movement (%u) while in combat, ignoring.", me->GetEntry(), pathId);
return;
}
if (HasEscortState(SMART_ESCORT_ESCORTING))
StopPath();
- if (path)
+ SetRun(run);
+
+ if (pathId)
{
- if (!LoadPath(path))
+ if (!LoadPath(pathId))
return;
}
- if (!mWayPoints || mWayPoints->empty())
+ if (_path.nodes.empty())
return;
- if (WayPoint* wp = GetNextWayPoint())
+ _currentWaypointNode = nodeId;
+ _waypointPathEnded = false;
+
+ _repeatWaypointPath = repeat;
+
+ // Do not use AddEscortState, removing everything from previous
+ _escortState = SMART_ESCORT_ESCORTING;
+
+ if (invoker && invoker->GetTypeId() == TYPEID_PLAYER)
{
- AddEscortState(SMART_ESCORT_ESCORTING);
- mCanRepeatPath = repeat;
-
- SetRun(run);
-
- if (invoker && invoker->GetTypeId() == TYPEID_PLAYER)
- {
- mEscortNPCFlags = me->m_unitData->NpcFlags[0];
- me->SetNpcFlags((NPCFlags)0);
- }
-
- mLastOOCPos = me->GetPosition();
- me->GetMotionMaster()->MovePoint(wp->id, wp->x, wp->y, wp->z);
- GetScript()->ProcessEventsFor(SMART_EVENT_WAYPOINT_START, nullptr, wp->id, GetScript()->GetPathId());
+ _escortNPCFlags = me->m_unitData->NpcFlags[0];
+ me->SetNpcFlags((NPCFlags)0);
}
+
+ GetScript()->ProcessEventsFor(SMART_EVENT_WAYPOINT_START, nullptr, _currentWaypointNode, GetScript()->GetPathId());
+
+ me->GetMotionMaster()->MovePath(_path, _repeatWaypointPath);
}
bool SmartAI::LoadPath(uint32 entry)
{
if (HasEscortState(SMART_ESCORT_ESCORTING))
return false;
- mWayPoints = sSmartWaypointMgr->GetPath(entry);
- if (!mWayPoints)
+
+ WaypointPath const* path = sSmartWaypointMgr->GetPath(entry);
+ if (!path || path->nodes.empty())
{
GetScript()->SetPathId(0);
return false;
}
+
+ _path.id = path->id;
+ _path.nodes = path->nodes;
+ for (WaypointNode& waypoint : _path.nodes)
+ {
+ Trinity::NormalizeMapCoord(waypoint.x);
+ Trinity::NormalizeMapCoord(waypoint.y);
+ waypoint.moveType = mRun ? WAYPOINT_MOVE_TYPE_RUN : WAYPOINT_MOVE_TYPE_WALK;
+ }
+
GetScript()->SetPathId(entry);
return true;
}
@@ -177,22 +130,26 @@ void SmartAI::PausePath(uint32 delay, bool forced)
{
if (!HasEscortState(SMART_ESCORT_ESCORTING))
return;
+
if (HasEscortState(SMART_ESCORT_PAUSED))
{
- TC_LOG_ERROR("misc", "SmartAI::PausePath: Creature entry %u wanted to pause waypoint movement while already paused, ignoring.", me->GetEntry());
+ TC_LOG_ERROR("misc", "SmartAI::PausePath: Creature entry %u wanted to pause waypoint (current waypoint: %u) movement while already paused, ignoring.", me->GetEntry(), _currentWaypointNode);
return;
}
- mForcedPaused = forced;
- mLastOOCPos = me->GetPosition();
- AddEscortState(SMART_ESCORT_PAUSED);
- mWPPauseTimer = delay;
+
+ _waypointPauseTimer = delay;
+
if (forced)
{
+ _waypointPauseForced = forced;
SetRun(mRun);
- me->StopMoving();//force stop
- me->GetMotionMaster()->MoveIdle();//force stop
+ me->PauseMovement();
}
- GetScript()->ProcessEventsFor(SMART_EVENT_WAYPOINT_PAUSED, nullptr, mLastWP->id, GetScript()->GetPathId());
+ else
+ _waypointReached = false;
+
+ AddEscortState(SMART_ESCORT_PAUSED);
+ GetScript()->ProcessEventsFor(SMART_EVENT_WAYPOINT_PAUSED, nullptr, _currentWaypointNode, GetScript()->GetPathId());
}
void SmartAI::StopPath(uint32 DespawnTime, uint32 quest, bool fail)
@@ -202,40 +159,29 @@ void SmartAI::StopPath(uint32 DespawnTime, uint32 quest, bool fail)
if (quest)
mEscortQuestID = quest;
- SetDespawnTime(DespawnTime);
- //mDespawnTime = DespawnTime;
- mLastOOCPos = me->GetPosition();
- me->StopMoving();//force stop
+ if (mDespawnState != 2)
+ SetDespawnTime(DespawnTime);
+
me->GetMotionMaster()->MoveIdle();
- GetScript()->ProcessEventsFor(SMART_EVENT_WAYPOINT_STOPPED, nullptr, mLastWP->id, GetScript()->GetPathId());
+
+ GetScript()->ProcessEventsFor(SMART_EVENT_WAYPOINT_STOPPED, nullptr, _currentWaypointNode, GetScript()->GetPathId());
+
EndPath(fail);
}
void SmartAI::EndPath(bool fail)
{
- GetScript()->ProcessEventsFor(SMART_EVENT_WAYPOINT_ENDED, nullptr, mLastWP->id, GetScript()->GetPathId());
-
RemoveEscortState(SMART_ESCORT_ESCORTING | SMART_ESCORT_PAUSED | SMART_ESCORT_RETURNING);
- mWayPoints = nullptr;
- mCurrentWPID = 0;
- mWPPauseTimer = 0;
- mLastWP = nullptr;
+ _path.nodes.clear();
+ _waypointPauseTimer = 0;
- if (mEscortNPCFlags)
+ if (_escortNPCFlags)
{
- me->SetNpcFlags((NPCFlags)mEscortNPCFlags);
- mEscortNPCFlags = 0;
+ me->SetNpcFlags((NPCFlags)_escortNPCFlags);
+ _escortNPCFlags = 0;
}
- if (mCanRepeatPath)
- {
- if (IsAIControlled())
- StartPath(mRun, GetScript()->GetPathId(), true);
- }
- else
- GetScript()->SetPathId(0);
-
ObjectVector const* targets = GetScript()->GetStoredTargetVector(SMART_ESCORT_TARGETS, *me);
if (targets && mEscortQuestID)
{
@@ -279,15 +225,36 @@ void SmartAI::EndPath(bool fail)
}
}
+ // End Path events should be only processed if it was SUCCESSFUL stop or stop called by SMART_ACTION_WAYPOINT_STOP
+ if (fail)
+ return;
+
+ GetScript()->ProcessEventsFor(SMART_EVENT_WAYPOINT_ENDED, nullptr, _currentWaypointNode, GetScript()->GetPathId());
+
+ if (_repeatWaypointPath)
+ {
+ if (IsAIControlled())
+ StartPath(mRun, GetScript()->GetPathId(), _repeatWaypointPath);
+ }
+ else
+ GetScript()->SetPathId(0);
+
if (mDespawnState == 1)
StartDespawn();
}
void SmartAI::ResumePath()
{
+ GetScript()->ProcessEventsFor(SMART_EVENT_WAYPOINT_RESUMED, nullptr, _currentWaypointNode, GetScript()->GetPathId());
+
+ RemoveEscortState(SMART_ESCORT_PAUSED);
+
+ _waypointPauseForced = false;
+ _waypointReached = false;
+ _waypointPauseTimer = 0;
+
SetRun(mRun);
- if (mLastWP)
- me->GetMotionMaster()->MovePoint(mLastWP->id, mLastWP->x, mLastWP->y, mLastWP->z);
+ me->ResumeMovement();
}
void SmartAI::ReturnToLastOOCPos()
@@ -295,81 +262,57 @@ void SmartAI::ReturnToLastOOCPos()
if (!IsAIControlled())
return;
- SetRun(mRun);
- me->GetMotionMaster()->MovePoint(SMART_ESCORT_LAST_OOC_POINT, mLastOOCPos);
+ me->SetWalk(false);
+ me->GetMotionMaster()->MovePoint(SMART_ESCORT_LAST_OOC_POINT, me->GetHomePosition());
}
void SmartAI::UpdatePath(const uint32 diff)
{
if (!HasEscortState(SMART_ESCORT_ESCORTING))
return;
- if (mEscortInvokerCheckTimer < diff)
+
+ if (_escortInvokerCheckTimer < diff)
{
- // Escort failed, no players in range
if (!IsEscortInvokerInRange())
{
StopPath(0, mEscortQuestID, true);
// allow to properly hook out of range despawn action, which in most cases should perform the same operation as dying
GetScript()->ProcessEventsFor(SMART_EVENT_DEATH, me);
- me->DespawnOrUnsummon(1);
+ me->DespawnOrUnsummon();
return;
}
- mEscortInvokerCheckTimer = 1000;
+ _escortInvokerCheckTimer = 1000;
}
else
- mEscortInvokerCheckTimer -= diff;
+ _escortInvokerCheckTimer -= diff;
// handle pause
- if (HasEscortState(SMART_ESCORT_PAUSED))
+ if (HasEscortState(SMART_ESCORT_PAUSED) && (_waypointReached || _waypointPauseForced))
{
- if (mWPPauseTimer < diff)
+ if (_waypointPauseTimer <= diff)
{
- if (!me->IsInCombat() && !HasEscortState(SMART_ESCORT_RETURNING) && (mWPReached || mLastWPIDReached == SMART_ESCORT_LAST_OOC_POINT || mForcedPaused))
- {
- GetScript()->ProcessEventsFor(SMART_EVENT_WAYPOINT_RESUMED, nullptr, mLastWP->id, GetScript()->GetPathId());
- RemoveEscortState(SMART_ESCORT_PAUSED);
- if (mForcedPaused)// if paused between 2 wps resend movement
- {
- ResumePath();
- mWPReached = false;
- mForcedPaused = false;
- }
- if (mLastWPIDReached == SMART_ESCORT_LAST_OOC_POINT)
- mWPReached = true;
- }
- mWPPauseTimer = 0;
+ if (!me->IsInCombat() && !HasEscortState(SMART_ESCORT_RETURNING))
+ ResumePath();
}
else
- mWPPauseTimer -= diff;
+ _waypointPauseTimer -= diff;
+ }
+ else if (_waypointPathEnded) // end path
+ {
+ _waypointPathEnded = false;
+ StopPath();
+ return;
}
if (HasEscortState(SMART_ESCORT_RETURNING))
{
- if (mWPReached)//reached OOC WP
+ if (_OOCReached) // reached OOC WP
{
+ _OOCReached = false;
RemoveEscortState(SMART_ESCORT_RETURNING);
if (!HasEscortState(SMART_ESCORT_PAUSED))
ResumePath();
- mWPReached = false;
- }
- }
-
- if ((!me->HasReactState(REACT_PASSIVE) && me->IsInCombat()) || HasEscortState(SMART_ESCORT_PAUSED | SMART_ESCORT_RETURNING))
- return;
-
- // handle next wp
- if (mWPReached)//reached WP
- {
- mWPReached = false;
- if (mCurrentWPID == GetWPCount())
- {
- EndPath();
- }
- else if (WayPoint* wp = GetNextWayPoint())
- {
- SetRun(mRun);
- me->GetMotionMaster()->MovePoint(wp->id, wp->x, wp->y, wp->z);
}
}
}
@@ -449,24 +392,43 @@ bool SmartAI::IsEscortInvokerInRange()
return true;
}
-void SmartAI::MovepointReached(uint32 id)
+///@todo Implement new smart event SMART_EVENT_WAYPOINT_STARTED
+void SmartAI::WaypointStarted(uint32 /*nodeId*/, uint32 /*pathId*/)
{
- if (id != SMART_ESCORT_LAST_OOC_POINT && mLastWPIDReached != id)
- GetScript()->ProcessEventsFor(SMART_EVENT_WAYPOINT_REACHED, nullptr, id);
-
- mLastWPIDReached = id;
- mWPReached = true;
}
-void SmartAI::MovementInform(uint32 MovementType, uint32 Data)
+void SmartAI::WaypointReached(uint32 nodeId, uint32 pathId)
{
- if ((MovementType == POINT_MOTION_TYPE && Data == SMART_ESCORT_LAST_OOC_POINT) || MovementType == FOLLOW_MOTION_TYPE)
+ _currentWaypointNode = nodeId;
+
+ GetScript()->ProcessEventsFor(SMART_EVENT_WAYPOINT_REACHED, nullptr, _currentWaypointNode, pathId);
+
+ if (_waypointPauseTimer && !_waypointPauseForced)
+ {
+ _waypointReached = true;
+ me->PauseMovement();
+ }
+ else if (HasEscortState(SMART_ESCORT_ESCORTING) && me->GetMotionMaster()->GetCurrentMovementGeneratorType() == WAYPOINT_MOTION_TYPE)
+ {
+ if (_currentWaypointNode == _path.nodes.size())
+ _waypointPathEnded = true;
+ else
+ SetRun(mRun);
+ }
+}
+
+void SmartAI::MovementInform(uint32 type, uint32 id)
+{
+ if (type == POINT_MOTION_TYPE && id == SMART_ESCORT_LAST_OOC_POINT)
me->ClearUnitState(UNIT_STATE_EVADE);
- GetScript()->ProcessEventsFor(SMART_EVENT_MOVEMENTINFORM, nullptr, MovementType, Data);
- if (MovementType != POINT_MOTION_TYPE || !HasEscortState(SMART_ESCORT_ESCORTING))
+ GetScript()->ProcessEventsFor(SMART_EVENT_MOVEMENTINFORM, nullptr, type, id);
+
+ if (!HasEscortState(SMART_ESCORT_ESCORTING))
return;
- MovepointReached(Data);
+
+ if (type == POINT_MOTION_TYPE && id == SMART_ESCORT_LAST_OOC_POINT)
+ _OOCReached = true;
}
void SmartAI::EnterEvadeMode(EvadeReason /*why*/)
@@ -510,8 +472,8 @@ void SmartAI::EnterEvadeMode(EvadeReason /*why*/)
else
me->GetMotionMaster()->MoveTargetedHome();
- if (!HasEscortState(SMART_ESCORT_ESCORTING)) //dont mess up escort movement after combat
- SetRun(mRun);
+ if (!me->HasUnitState(UNIT_STATE_EVADE))
+ GetScript()->OnReset();
}
void SmartAI::MoveInLineOfSight(Unit* who)
@@ -543,19 +505,19 @@ bool SmartAI::AssistPlayerInCombatAgainst(Unit* who)
if (!who || !who->GetVictim())
return false;
- //experimental (unknown) flag not present
+ // experimental (unknown) flag not present
if (!(me->GetCreatureTemplate()->type_flags & CREATURE_TYPE_FLAG_CAN_ASSIST))
return false;
- //not a player
+ // not a player
if (!who->EnsureVictim()->GetCharmerOrOwnerPlayerOrPlayerItself())
return false;
- //never attack friendly
+ // never attack friendly
if (!me->IsValidAssistTarget(who->GetVictim()))
return false;
- //too far away and no free sight?
+ // too far away and no free sight?
if (me->IsWithinDistInMap(who, SMART_MAX_AID_DIST) && me->IsWithinLOSInMap(who))
{
me->EngageWithTarget(who);
@@ -570,14 +532,14 @@ void SmartAI::JustAppeared()
mDespawnTime = 0;
mRespawnTime = 0;
mDespawnState = 0;
- mEscortState = SMART_ESCORT_NONE;
+ _escortState = SMART_ESCORT_NONE;
me->SetVisible(true);
if (me->GetFaction() != me->GetCreatureTemplate()->faction)
me->RestoreFaction();
mJustReset = true;
JustReachedHome();
GetScript()->ProcessEventsFor(SMART_EVENT_RESPAWN);
- mFollowGuid.Clear();//do not reset follower on Reset(), we need it after combat evade
+ mFollowGuid.Clear(); // do not reset follower on Reset(), we need it after combat evade
mFollowDist = 0;
mFollowAngle = 0;
mFollowCredit = 0;
@@ -594,8 +556,16 @@ void SmartAI::JustReachedHome()
{
GetScript()->ProcessEventsFor(SMART_EVENT_REACHED_HOME);
- if (!UpdateVictim() && me->GetMotionMaster()->GetCurrentMovementGeneratorType() == IDLE_MOTION_TYPE && me->GetWaypointPath())
- me->GetMotionMaster()->MovePath(me->GetWaypointPath(), true);
+ CreatureGroup* formation = me->GetFormation();
+ if (!formation || formation->getLeader() == me || !formation->isFormed())
+ {
+ if (me->GetMotionMaster()->GetCurrentMovementGeneratorType() == IDLE_MOTION_TYPE && me->GetWaypointPath())
+ me->GetMotionMaster()->MovePath(me->GetWaypointPath(), true);
+ else
+ me->ResumeMovement();
+ }
+ else if (formation->isFormed())
+ me->GetMotionMaster()->MoveIdle(); // wait the order of leader
}
mJustReset = false;
@@ -607,24 +577,14 @@ void SmartAI::EnterCombat(Unit* enemy)
me->InterruptNonMeleeSpells(false); // must be before ProcessEvents
GetScript()->ProcessEventsFor(SMART_EVENT_AGGRO, enemy);
-
- if (!IsAIControlled())
- return;
- mLastOOCPos = me->GetPosition();
- SetRun(mRun);
- if (me->GetMotionMaster()->GetMotionSlotType(MOTION_SLOT_ACTIVE) == POINT_MOTION_TYPE)
- me->GetMotionMaster()->MovementExpired();
}
void SmartAI::JustDied(Unit* killer)
{
- GetScript()->ProcessEventsFor(SMART_EVENT_DEATH, killer);
if (HasEscortState(SMART_ESCORT_ESCORTING))
- {
EndPath(true);
- me->StopMoving();//force stop
- me->GetMotionMaster()->MoveIdle();
- }
+
+ GetScript()->ProcessEventsFor(SMART_EVENT_DEATH, killer);
}
void SmartAI::KilledUnit(Unit* victim)
@@ -642,15 +602,21 @@ void SmartAI::AttackStart(Unit* who)
// dont allow charmed npcs to act on their own
if (!IsAIControlled())
{
- if (who && mCanAutoAttack)
- me->Attack(who, true);
+ if (who)
+ me->Attack(who, mCanAutoAttack);
return;
}
- if (who && me->Attack(who, me->IsWithinMeleeRange(who)))
+ if (who && me->Attack(who, mCanAutoAttack))
{
+ me->GetMotionMaster()->Clear(MOTION_SLOT_ACTIVE);
+ me->PauseMovement();
+
if (mCanCombatMove)
+ {
+ SetRun(mRun);
me->GetMotionMaster()->MoveChase(who);
+ }
}
}
@@ -713,10 +679,10 @@ void SmartAI::PassengerBoarded(Unit* who, int8 seatId, bool apply)
void SmartAI::InitializeAI()
{
GetScript()->OnInitialize(me);
+
if (!me->isDead())
{
- mJustReset = true;
- JustReachedHome();
+ GetScript()->OnReset();
GetScript()->ProcessEventsFor(SMART_EVENT_RESPAWN);
}
}
@@ -727,13 +693,13 @@ void SmartAI::OnCharmed(bool apply)
{
if (HasEscortState(SMART_ESCORT_ESCORTING | SMART_ESCORT_PAUSED | SMART_ESCORT_RETURNING))
EndPath(true);
- me->StopMoving();
}
+
mIsCharmed = apply;
if (!apply && !me->IsInEvadeMode())
{
- if (mCanRepeatPath)
+ if (_repeatWaypointPath)
StartPath(mRun, GetScript()->GetPathId(), true);
else
me->SetWalk(!mRun);
@@ -826,30 +792,21 @@ void SmartAI::SetCombatMove(bool on)
{
if (mCanCombatMove == on)
return;
+
mCanCombatMove = on;
+
if (!IsAIControlled())
return;
- if (!HasEscortState(SMART_ESCORT_ESCORTING))
- {
- if (on && me->GetVictim())
- {
- if (me->GetMotionMaster()->GetCurrentMovementGeneratorType() == IDLE_MOTION_TYPE)
- {
- SetRun(mRun);
- me->GetMotionMaster()->MoveChase(me->GetVictim());
- me->CastStop();
- }
- }
- else
- {
- if (me->HasUnitState(UNIT_STATE_CONFUSED_MOVE | UNIT_STATE_FLEEING_MOVE))
- return;
- me->GetMotionMaster()->MovementExpired();
- me->GetMotionMaster()->Clear(true);
- me->StopMoving();
- me->GetMotionMaster()->MoveIdle();
+ if (me->IsEngaged())
+ {
+ if (on && !me->HasReactState(REACT_PASSIVE) && me->GetVictim() && me->GetMotionMaster()->GetMotionSlotType(MOTION_SLOT_ACTIVE) == MAX_MOTION_TYPE)
+ {
+ SetRun(mRun);
+ me->GetMotionMaster()->MoveChase(me->GetVictim());
}
+ else if (!on && me->GetMotionMaster()->GetMotionSlotType(MOTION_SLOT_ACTIVE) == CHASE_MOTION_TYPE)
+ me->GetMotionMaster()->Clear(MOTION_SLOT_ACTIVE);
}
}
@@ -881,7 +838,6 @@ void SmartAI::StopFollow(bool complete)
mFollowArrivedTimer = 1000;
mFollowArrivedEntry = 0;
mFollowCreditType = 0;
- me->StopMoving();
me->GetMotionMaster()->MoveIdle();
if (!complete)
diff --git a/src/server/game/AI/SmartScripts/SmartAI.h b/src/server/game/AI/SmartScripts/SmartAI.h
index b892d5705b1..357020f80b2 100644
--- a/src/server/game/AI/SmartScripts/SmartAI.h
+++ b/src/server/game/AI/SmartScripts/SmartAI.h
@@ -23,8 +23,7 @@
#include "GameObjectAI.h"
#include "Position.h"
#include "SmartScript.h"
-
-struct WayPoint;
+#include "WaypointDefines.h"
enum SmartEscortState
{
@@ -43,32 +42,37 @@ enum SmartEscortVars
class TC_GAME_API SmartAI : public CreatureAI
{
public:
- ~SmartAI(){ }
+ ~SmartAI() { }
explicit SmartAI(Creature* c);
+ // core related
+ static int32 Permissible(Creature const* /*creature*/) { return PERMIT_BASE_NO; }
+
// Check whether we are currently permitted to make the creature take action
bool IsAIControlled() const;
// Start moving to the desired MovePoint
- void StartPath(bool run = false, uint32 path = 0, bool repeat = false, Unit* invoker = nullptr);
+ void StartPath(bool run = false, uint32 pathId = 0, bool repeat = false, Unit* invoker = nullptr, uint32 nodeId = 1);
bool LoadPath(uint32 entry);
void PausePath(uint32 delay, bool forced = false);
void StopPath(uint32 DespawnTime = 0, uint32 quest = 0, bool fail = false);
void EndPath(bool fail = false);
void ResumePath();
- WayPoint* GetNextWayPoint();
- bool HasEscortState(uint32 uiEscortState) const { return (mEscortState & uiEscortState) != 0; }
- void AddEscortState(uint32 uiEscortState) { mEscortState |= uiEscortState; }
- void RemoveEscortState(uint32 uiEscortState) { mEscortState &= ~uiEscortState; }
+ bool HasEscortState(uint32 uiEscortState) const { return (_escortState & uiEscortState) != 0; }
+ void AddEscortState(uint32 uiEscortState) { _escortState |= uiEscortState; }
+ void RemoveEscortState(uint32 uiEscortState) { _escortState &= ~uiEscortState; }
void SetAutoAttack(bool on) { mCanAutoAttack = on; }
void SetCombatMove(bool on);
bool CanCombatMove() { return mCanCombatMove; }
void SetFollow(Unit* target, float dist = 0.0f, float angle = 0.0f, uint32 credit = 0, uint32 end = 0, uint32 creditType = 0);
void StopFollow(bool complete);
+ bool IsEscortInvokerInRange();
+
+ void WaypointStarted(uint32 nodeId, uint32 pathId) override;
+ void WaypointReached(uint32 nodeId, uint32 pathId) override;
void SetScript9(SmartScriptHolder& e, uint32 entry, Unit* invoker);
SmartScript* GetScript() { return &mScript; }
- bool IsEscortInvokerInRange();
// Called when creature is spawned or respawned
void JustAppeared() override;
@@ -157,12 +161,6 @@ class TC_GAME_API SmartAI : public CreatureAI
// Used in scripts to share variables
ObjectGuid GetGUID(int32 id = 0) const override;
- //core related
- static int32 Permissible(Creature const* /*creature*/) { return PERMIT_BASE_NO; }
-
- // Called at movepoint reached
- void MovepointReached(uint32 id);
-
// Makes the creature run/walk
void SetRun(bool run = true);
@@ -196,11 +194,20 @@ class TC_GAME_API SmartAI : public CreatureAI
void OnSpellClick(Unit* clicker, bool& result) override;
- void SetWPPauseTimer(uint32 time) { mWPPauseTimer = time; }
+ void SetWPPauseTimer(uint32 time) { _waypointPauseTimer = time; }
void SetGossipReturn(bool val) { _gossipReturn = val; }
private:
+ bool AssistPlayerInCombatAgainst(Unit* who);
+ void ReturnToLastOOCPos();
+ void UpdatePath(const uint32 diff);
+ void UpdateDespawn(uint32 diff);
+ // Vehicle conditions
+ void CheckConditions(uint32 diff);
+
+ SmartScript mScript;
+
bool mIsCharmed;
uint32 mFollowCreditType;
uint32 mFollowArrivedTimer;
@@ -210,37 +217,30 @@ class TC_GAME_API SmartAI : public CreatureAI
float mFollowDist;
float mFollowAngle;
- void ReturnToLastOOCPos();
- void UpdatePath(const uint32 diff);
- SmartScript mScript;
- WPPath* mWayPoints;
- uint32 mEscortState;
- uint32 mCurrentWPID;
- uint32 mLastWPIDReached;
- bool mWPReached;
- uint32 mWPPauseTimer;
- uint32 mEscortNPCFlags;
- WayPoint* mLastWP;
- Position mLastOOCPos;//set on enter combat
- uint32 GetWPCount() const { return mWayPoints ? uint32(mWayPoints->size()) : 0; }
- bool mCanRepeatPath;
+ uint32 _escortState;
+ uint32 _escortNPCFlags;
+ uint32 _escortInvokerCheckTimer;
+ WaypointPath _path;
+ uint32 _currentWaypointNode;
+ bool _waypointReached;
+ uint32 _waypointPauseTimer;
+ bool _waypointPauseForced;
+ bool _repeatWaypointPath;
+ bool _OOCReached;
+ bool _waypointPathEnded;
+
bool mRun;
bool mEvadeDisabled;
bool mCanAutoAttack;
bool mCanCombatMove;
- bool mForcedPaused;
uint32 mInvincibilityHpLevel;
- bool AssistPlayerInCombatAgainst(Unit* who);
uint32 mDespawnTime;
uint32 mRespawnTime;
uint32 mDespawnState;
- void UpdateDespawn(uint32 diff);
- uint32 mEscortInvokerCheckTimer;
bool mJustReset;
// Vehicle conditions
- void CheckConditions(uint32 diff);
bool mHasConditions;
uint32 mConditionsTimer;
diff --git a/src/server/game/AI/SmartScripts/SmartScript.cpp b/src/server/game/AI/SmartScripts/SmartScript.cpp
index 8b6616a2ee3..44414d5b5ad 100644
--- a/src/server/game/AI/SmartScripts/SmartScript.cpp
+++ b/src/server/game/AI/SmartScripts/SmartScript.cpp
@@ -41,6 +41,7 @@
#include "SpellMgr.h"
#include "TemporarySummon.h"
#include "Vehicle.h"
+#include "WaypointDefines.h"
SmartScript::SmartScript()
{
@@ -2044,7 +2045,7 @@ void SmartScript::ProcessAction(SmartScriptHolder& e, Unit* unit, uint32 var0, u
std::back_inserter(waypoints), [](uint32 wp) { return wp != 0; });
float distanceToClosest = std::numeric_limits::max();
- WayPoint* closestWp = nullptr;
+ std::pair closest = { 0, 0 };
for (WorldObject* target : targets)
{
@@ -2052,29 +2053,27 @@ void SmartScript::ProcessAction(SmartScriptHolder& e, Unit* unit, uint32 var0, u
{
if (IsSmart(creature))
{
- for (uint32 wp : waypoints)
+ for (uint32 pathId : waypoints)
{
- WPPath* path = sSmartWaypointMgr->GetPath(wp);
- if (!path || path->empty())
+ WaypointPath const* path = sSmartWaypointMgr->GetPath(pathId);
+ if (!path || path->nodes.empty())
continue;
- auto itrWp = path->find(0);
- if (itrWp != path->end())
+ for (auto itr = path->nodes.begin(); itr != path->nodes.end(); ++itr)
{
- if (WayPoint* wp = itrWp->second)
+ WaypointNode const waypoint = *itr;
+ float distamceToThisNode = creature->GetDistance(waypoint.x, waypoint.y, waypoint.z);
+ if (distamceToThisNode < distanceToClosest)
{
- float distToThisPath = creature->GetDistance(wp->x, wp->y, wp->z);
- if (distToThisPath < distanceToClosest)
- {
- distanceToClosest = distToThisPath;
- closestWp = wp;
- }
+ distanceToClosest = distamceToThisNode;
+ closest.first = pathId;
+ closest.second = waypoint.id;
}
}
}
- if (closestWp)
- CAST_AI(SmartAI, creature->AI())->StartPath(false, closestWp->id, true);
+ if (closest.first != 0)
+ CAST_AI(SmartAI, creature->AI())->StartPath(false, closest.first, true, nullptr, closest.second);
}
}
}
diff --git a/src/server/game/AI/SmartScripts/SmartScriptMgr.cpp b/src/server/game/AI/SmartScripts/SmartScriptMgr.cpp
index f1240950f1e..3f0962a20b8 100644
--- a/src/server/game/AI/SmartScripts/SmartScriptMgr.cpp
+++ b/src/server/game/AI/SmartScripts/SmartScriptMgr.cpp
@@ -29,6 +29,7 @@
#include "SpellMgr.h"
#include "Timer.h"
#include "UnitDefines.h"
+#include "WaypointDefines.h"
#include
SmartWaypointMgr* SmartWaypointMgr::instance()
@@ -41,15 +42,7 @@ void SmartWaypointMgr::LoadFromDB()
{
uint32 oldMSTime = getMSTime();
- for (std::unordered_map::iterator itr = waypoint_map.begin(); itr != waypoint_map.end(); ++itr)
- {
- for (WPPath::iterator pathItr = itr->second->begin(); pathItr != itr->second->end(); ++pathItr)
- delete pathItr->second;
-
- delete itr->second;
- }
-
- waypoint_map.clear();
+ _waypointStore.clear();
WorldDatabasePreparedStatement* stmt = WorldDatabase.GetPreparedStatement(WORLD_SEL_SMARTAI_WP);
PreparedQueryResult result = WorldDatabase.Query(stmt);
@@ -63,49 +56,47 @@ void SmartWaypointMgr::LoadFromDB()
uint32 count = 0;
uint32 total = 0;
- uint32 last_entry = 0;
- uint32 last_id = 1;
+ uint32 lastEntry = 0;
+ uint32 lastId = 1;
do
{
Field* fields = result->Fetch();
uint32 entry = fields[0].GetUInt32();
uint32 id = fields[1].GetUInt32();
- float x, y, z;
- x = fields[2].GetFloat();
- y = fields[3].GetFloat();
- z = fields[4].GetFloat();
+ float x = fields[2].GetFloat();
+ float y = fields[3].GetFloat();
+ float z = fields[4].GetFloat();
- if (last_entry != entry)
+ if (lastEntry != entry)
{
- waypoint_map[entry] = new WPPath();
- last_id = 1;
- count++;
+ lastId = 1;
+ ++count;
}
- if (last_id != id)
- TC_LOG_ERROR("sql.sql", "SmartWaypointMgr::LoadFromDB: Path entry %u, unexpected point id %u, expected %u.", entry, id, last_id);
+ if (lastId != id)
+ TC_LOG_ERROR("sql.sql", "SmartWaypointMgr::LoadFromDB: Path entry %u, unexpected point id %u, expected %u.", entry, id, lastId);
- last_id++;
- (*waypoint_map[entry])[id] = new WayPoint(id, x, y, z);
+ ++lastId;
- last_entry = entry;
- total++;
+ WaypointPath& path = _waypointStore[entry];
+ path.id = entry;
+ path.nodes.emplace_back(id, x, y, z);
+
+ lastEntry = entry;
+ ++total;
}
while (result->NextRow());
TC_LOG_INFO("server.loading", ">> Loaded %u SmartAI waypoint paths (total %u waypoints) in %u ms", count, total, GetMSTimeDiffToNow(oldMSTime));
}
-SmartWaypointMgr::~SmartWaypointMgr()
+WaypointPath const* SmartWaypointMgr::GetPath(uint32 id)
{
- for (std::unordered_map::iterator itr = waypoint_map.begin(); itr != waypoint_map.end(); ++itr)
- {
- for (WPPath::iterator pathItr = itr->second->begin(); pathItr != itr->second->end(); ++pathItr)
- delete pathItr->second;
-
- delete itr->second;
- }
+ auto itr = _waypointStore.find(id);
+ if (itr != _waypointStore.end())
+ return &itr->second;
+ return nullptr;
}
SmartAIMgr* SmartAIMgr::instance()
@@ -1363,21 +1354,22 @@ bool SmartAIMgr::IsEventValid(SmartScriptHolder& e)
return false;
break;
case SMART_ACTION_WP_START:
+ {
+ WaypointPath const* path = sSmartWaypointMgr->GetPath(e.action.wpStart.pathID);
+ if (!path || path->nodes.empty())
{
- if (!sSmartWaypointMgr->GetPath(e.action.wpStart.pathID))
- {
- TC_LOG_ERROR("sql.sql", "SmartAIMgr: Creature " SI64FMTD " Event %u Action %u uses non-existent WaypointPath id %u, skipped.", e.entryOrGuid, e.event_id, e.GetActionType(), e.action.wpStart.pathID);
- return false;
- }
- if (e.action.wpStart.quest && !IsQuestValid(e, e.action.wpStart.quest))
- return false;
- if (e.action.wpStart.reactState > REACT_AGGRESSIVE)
- {
- TC_LOG_ERROR("sql.sql", "SmartAIMgr: Creature " SI64FMTD " Event %u Action %u uses invalid React State %u, skipped.", e.entryOrGuid, e.event_id, e.GetActionType(), e.action.wpStart.reactState);
- return false;
- }
- break;
+ TC_LOG_ERROR("sql.sql", "SmartAIMgr: Creature " SI64FMTD " Event %u Action %u uses non-existent WaypointPath id %u, skipped.", e.entryOrGuid, e.event_id, e.GetActionType(), e.action.wpStart.pathID);
+ return false;
}
+ if (e.action.wpStart.quest && !IsQuestValid(e, e.action.wpStart.quest))
+ return false;
+ if (e.action.wpStart.reactState > REACT_AGGRESSIVE)
+ {
+ TC_LOG_ERROR("sql.sql", "SmartAIMgr: Creature " SI64FMTD " Event %u Action %u uses invalid React State %u, skipped.", e.entryOrGuid, e.event_id, e.GetActionType(), e.action.wpStart.reactState);
+ return false;
+ }
+ break;
+ }
case SMART_ACTION_CREATE_TIMED_EVENT:
{
if (!IsMinMaxValid(e, e.action.timeEvent.min, e.action.timeEvent.max))
diff --git a/src/server/game/AI/SmartScripts/SmartScriptMgr.h b/src/server/game/AI/SmartScripts/SmartScriptMgr.h
index 6285726bb33..f358ea81c90 100644
--- a/src/server/game/AI/SmartScripts/SmartScriptMgr.h
+++ b/src/server/game/AI/SmartScripts/SmartScriptMgr.h
@@ -20,6 +20,7 @@
#include "Define.h"
#include "ObjectGuid.h"
+#include "WaypointDefines.h"
#include