Skip to content

Commit 3a8d8e1

Browse files
committed
[3097] Use WaypointMovement for Escort-Scripting. Requires CMaNGOS [12839]
1 parent 60d2555 commit 3a8d8e1

8 files changed

+72
-302
lines changed

ScriptMgr.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ void LoadDatabase()
3737
outstring_log("SD2: ScriptDev2 database initialized.");
3838
outstring_log("");
3939

40+
// Extract DB-Name
41+
std::string::size_type n = strSD2DBinfo.rfind(';');
42+
std::string dbname;
43+
if (n != std::string::npos && n + 1 != std::string::npos)
44+
dbname = strSD2DBinfo.substr(n + 1);
45+
else
46+
dbname = "SD2_Database";
47+
dbname.append(".script_waypoint");
48+
SetExternalWaypointTable(dbname.c_str());
49+
50+
// Load content
4051
pSystemMgr.LoadVersion();
4152
pSystemMgr.LoadScriptTexts();
4253
pSystemMgr.LoadScriptTextsCustom();

base/escort_ai.cpp

+31-229
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,7 @@ EndScriptData */
1515

1616
const float MAX_PLAYER_DISTANCE = 66.0f;
1717

18-
enum
19-
{
20-
POINT_LAST_POINT = 0xFFFFFF,
21-
POINT_HOME = 0xFFFFFE
22-
};
23-
2418
npc_escortAI::npc_escortAI(Creature* pCreature) : ScriptedAI(pCreature),
25-
m_uiWPWaitTimer(2500),
2619
m_uiPlayerCheckTimer(1000),
2720
m_uiEscortState(STATE_ESCORT_NONE),
2821
m_pQuestForEscort(NULL),
@@ -43,10 +36,8 @@ void npc_escortAI::GetAIInformation(ChatHandler& reader)
4336

4437
if (HasEscortState(STATE_ESCORT_ESCORTING))
4538
{
46-
oss << "\nEscortFlags: Escorting" << (HasEscortState(STATE_ESCORT_RETURNING) ? ", Returning" : "") << (HasEscortState(STATE_ESCORT_PAUSED) ? ", Paused" : "");
47-
48-
if (CurrentWP != WaypointList.end())
49-
oss << "\nNext Waypoint Id = " << CurrentWP->uiId << " Position: " << CurrentWP->fX << " " << CurrentWP->fY << " " << CurrentWP->fZ;
39+
oss << "\nEscortFlags: Escorting" << (HasEscortState(STATE_ESCORT_RETURNING) ? ", Returning" : "") << (HasEscortState(STATE_ESCORT_PAUSED) ? ", Paused" : "") << "\n";
40+
m_creature->GetMotionMaster()->GetWaypointPathInformation(oss);
5041
}
5142

5243
reader.PSendSysMessage(oss.str().c_str());
@@ -60,36 +51,6 @@ bool npc_escortAI::IsVisible(Unit* pWho) const
6051
return m_creature->IsWithinDist(pWho, VISIBLE_RANGE) && pWho->isVisibleForOrDetect(m_creature, m_creature, true);
6152
}
6253

63-
void npc_escortAI::AttackStart(Unit* pWho)
64-
{
65-
if (!pWho)
66-
return;
67-
68-
if (m_creature->Attack(pWho, true))
69-
{
70-
m_creature->AddThreat(pWho);
71-
m_creature->SetInCombatWith(pWho);
72-
pWho->SetInCombatWith(m_creature);
73-
74-
if (m_creature->GetMotionMaster()->GetCurrentMovementGeneratorType() == POINT_MOTION_TYPE)
75-
m_creature->GetMotionMaster()->MovementExpired();
76-
77-
if (IsCombatMovement())
78-
m_creature->GetMotionMaster()->MoveChase(pWho);
79-
}
80-
}
81-
82-
void npc_escortAI::EnterCombat(Unit* pEnemy)
83-
{
84-
// Store combat start position
85-
m_creature->SetCombatStartPosition(m_creature->GetPositionX(), m_creature->GetPositionY(), m_creature->GetPositionZ());
86-
87-
if (!pEnemy)
88-
return;
89-
90-
Aggro(pEnemy);
91-
}
92-
9354
void npc_escortAI::Aggro(Unit* /*pEnemy*/) {}
9455

9556
// see followerAI
@@ -201,36 +162,6 @@ void npc_escortAI::JustRespawned()
201162
if (!IsCombatMovement())
202163
SetCombatMovement(true);
203164

204-
// add a small delay before going to first waypoint, normal in near all cases
205-
m_uiWPWaitTimer = 2500;
206-
207-
Reset();
208-
}
209-
210-
void npc_escortAI::EnterEvadeMode()
211-
{
212-
m_creature->RemoveAllAurasOnEvade();
213-
m_creature->DeleteThreatList();
214-
m_creature->CombatStop(true);
215-
m_creature->SetLootRecipient(NULL);
216-
217-
if (HasEscortState(STATE_ESCORT_ESCORTING))
218-
{
219-
// We have left our path
220-
if (m_creature->GetMotionMaster()->GetCurrentMovementGeneratorType() != POINT_MOTION_TYPE)
221-
{
222-
debug_log("SD2: EscortAI has left combat and is now returning to CombatStartPosition.");
223-
224-
AddEscortState(STATE_ESCORT_RETURNING);
225-
226-
float fPosX, fPosY, fPosZ;
227-
m_creature->GetCombatStartPosition(fPosX, fPosY, fPosZ);
228-
m_creature->GetMotionMaster()->MovePoint(POINT_LAST_POINT, fPosX, fPosY, fPosZ);
229-
}
230-
}
231-
else
232-
m_creature->GetMotionMaster()->MoveTargetedHome();
233-
234165
Reset();
235166
}
236167

@@ -256,66 +187,8 @@ bool npc_escortAI::IsPlayerOrGroupInRange()
256187
return false;
257188
}
258189

259-
// Returns false, if npc is despawned
260-
bool npc_escortAI::MoveToNextWaypoint()
261-
{
262-
// Do nothing if escorting is paused
263-
if (HasEscortState(STATE_ESCORT_PAUSED))
264-
return true;
265-
266-
// Final Waypoint reached (and final wait time waited)
267-
if (CurrentWP == WaypointList.end())
268-
{
269-
debug_log("SD2: EscortAI reached end of waypoints");
270-
271-
if (m_bCanReturnToStart)
272-
{
273-
float fRetX, fRetY, fRetZ;
274-
m_creature->GetRespawnCoord(fRetX, fRetY, fRetZ);
275-
276-
m_creature->GetMotionMaster()->MovePoint(POINT_HOME, fRetX, fRetY, fRetZ);
277-
278-
m_uiWPWaitTimer = 0;
279-
280-
debug_log("SD2: EscortAI are returning home to spawn location: %u, %f, %f, %f", POINT_HOME, fRetX, fRetY, fRetZ);
281-
return true;
282-
}
283-
284-
if (m_bCanInstantRespawn)
285-
{
286-
m_creature->SetDeathState(JUST_DIED);
287-
m_creature->Respawn();
288-
}
289-
else
290-
m_creature->ForcedDespawn();
291-
292-
return false;
293-
}
294-
295-
m_creature->GetMotionMaster()->MovePoint(CurrentWP->uiId, CurrentWP->fX, CurrentWP->fY, CurrentWP->fZ);
296-
debug_log("SD2: EscortAI start waypoint %u (%f, %f, %f).", CurrentWP->uiId, CurrentWP->fX, CurrentWP->fY, CurrentWP->fZ);
297-
298-
WaypointStart(CurrentWP->uiId);
299-
300-
m_uiWPWaitTimer = 0;
301-
302-
return true;
303-
}
304-
305190
void npc_escortAI::UpdateAI(const uint32 uiDiff)
306191
{
307-
// Waypoint Updating
308-
if (HasEscortState(STATE_ESCORT_ESCORTING) && !m_creature->getVictim() && m_uiWPWaitTimer && !HasEscortState(STATE_ESCORT_RETURNING))
309-
{
310-
if (m_uiWPWaitTimer <= uiDiff)
311-
{
312-
if (!MoveToNextWaypoint())
313-
return;
314-
}
315-
else
316-
m_uiWPWaitTimer -= uiDiff;
317-
}
318-
319192
// Check if player or any member of his group is within range
320193
if (HasEscortState(STATE_ESCORT_ESCORTING) && m_playerGuid && !m_creature->getVictim() && !HasEscortState(STATE_ESCORT_RETURNING))
321194
{
@@ -356,107 +229,40 @@ void npc_escortAI::UpdateEscortAI(const uint32 /*uiDiff*/)
356229

357230
void npc_escortAI::MovementInform(uint32 uiMoveType, uint32 uiPointId)
358231
{
359-
if (uiMoveType != POINT_MOTION_TYPE || !HasEscortState(STATE_ESCORT_ESCORTING))
232+
if (uiMoveType < EXTERNAL_WAYPOINT_MOVE || !HasEscortState(STATE_ESCORT_ESCORTING))
360233
return;
361234

362-
// Combat start position reached, continue waypoint movement
363-
if (uiPointId == POINT_LAST_POINT)
364-
{
365-
debug_log("SD2: EscortAI has returned to original position before combat");
235+
//uint32 pathId = uiMoveType & 0xFF;
366236

367-
m_creature->SetWalk(!m_bIsRunning);
368-
RemoveEscortState(STATE_ESCORT_RETURNING);
369-
}
370-
else if (uiPointId == POINT_HOME)
237+
if (uiMoveType < EXTERNAL_WAYPOINT_MOVE_START)
238+
WaypointReached(uiPointId);
239+
else if (uiMoveType < EXTERNAL_WAYPOINT_FINISHED_LAST)
240+
WaypointStart(uiPointId);
241+
else // Last WP Reached
371242
{
372-
debug_log("SD2: EscortAI has returned to original home location and will continue from beginning of waypoint list.");
373-
374-
CurrentWP = WaypointList.begin();
375-
m_uiWPWaitTimer = 0;
376-
}
377-
else
378-
{
379-
// Make sure that we are still on the right waypoint
380-
if (CurrentWP->uiId != uiPointId)
243+
if (m_bCanInstantRespawn)
381244
{
382-
script_error_log("EscortAI for Npc %u reached waypoint out of order %u, expected %u.", m_creature->GetEntry(), uiPointId, CurrentWP->uiId);
383-
return;
245+
m_creature->SetDeathState(JUST_DIED);
246+
m_creature->Respawn();
384247
}
385-
386-
debug_log("SD2: EscortAI waypoint %u reached.", CurrentWP->uiId);
387-
388-
// In case we were moving while in combat, we should evade back to this position
389-
m_creature->SetCombatStartPosition(m_creature->GetPositionX(), m_creature->GetPositionY(), m_creature->GetPositionZ());
390-
391-
// Call WP function
392-
WaypointReached(CurrentWP->uiId);
393-
394-
m_uiWPWaitTimer = CurrentWP->uiWaitTime;
395-
396-
++CurrentWP;
397-
}
398-
399-
if (!m_uiWPWaitTimer)
400-
{
401-
// Continue WP Movement if Can
402-
if (HasEscortState(STATE_ESCORT_ESCORTING) && !HasEscortState(STATE_ESCORT_PAUSED | STATE_ESCORT_RETURNING) && !m_creature->getVictim())
403-
MoveToNextWaypoint();
404248
else
405-
m_uiWPWaitTimer = 1;
406-
}
407-
}
408-
409-
/*void npc_escortAI::AddWaypoint(uint32 id, float x, float y, float z, uint32 WaitTimeMs)
410-
{
411-
Escort_Waypoint t(id, x, y, z, WaitTimeMs);
412-
413-
WaypointList.push_back(t);
414-
}*/
415-
416-
void npc_escortAI::FillPointMovementListForCreature()
417-
{
418-
std::vector<ScriptPointMove> const& pPointsEntries = pSystemMgr.GetPointMoveList(m_creature->GetEntry());
419-
420-
if (pPointsEntries.empty())
421-
return;
422-
423-
std::vector<ScriptPointMove>::const_iterator itr;
424-
425-
for (itr = pPointsEntries.begin(); itr != pPointsEntries.end(); ++itr)
426-
{
427-
Escort_Waypoint pPoint(itr->uiPointId, itr->fX, itr->fY, itr->fZ, itr->uiWaitTime);
428-
WaypointList.push_back(pPoint);
249+
m_creature->ForcedDespawn();
429250
}
430251
}
431252

432253
void npc_escortAI::SetCurrentWaypoint(uint32 uiPointId)
433254
{
434255
if (!(HasEscortState(STATE_ESCORT_PAUSED))) // Only when paused
435-
return;
436-
437-
if (uiPointId == CurrentWP->uiId) // Already here
438-
return;
439-
440-
bool bFoundWaypoint = false;
441-
for (std::list<Escort_Waypoint>::iterator itr = WaypointList.begin(); itr != WaypointList.end(); ++itr)
442256
{
443-
if (itr->uiId == uiPointId)
444-
{
445-
CurrentWP = itr; // Set to found itr
446-
bFoundWaypoint = true;
447-
break;
448-
}
257+
script_error_log("EscortAI for %s tried to set new waypoint %u, but not paused", m_creature->GetGuidStr().c_str(), uiPointId);
258+
return;
449259
}
450260

451-
if (!bFoundWaypoint)
261+
if (!m_creature->GetMotionMaster()->SetNextWaypoint(uiPointId))
452262
{
453-
debug_log("SD2: EscortAI current waypoint tried to set to id %u, but doesn't exist in WaypointList", uiPointId);
263+
script_error_log("EscortAI for %s current waypoint tried to set to id %u, but doesn't exist in this path", m_creature->GetGuidStr().c_str(), uiPointId);
454264
return;
455265
}
456-
457-
m_uiWPWaitTimer = 1;
458-
459-
debug_log("SD2: EscortAI current waypoint set to id %u", CurrentWP->uiId);
460266
}
461267

462268
void npc_escortAI::SetRun(bool bRun)
@@ -493,14 +299,9 @@ void npc_escortAI::Start(bool bRun, const Player* pPlayer, const Quest* pQuest,
493299
return;
494300
}
495301

496-
if (!WaypointList.empty())
497-
WaypointList.clear();
498-
499-
FillPointMovementListForCreature();
500-
501-
if (WaypointList.empty())
302+
if (!pSystemMgr.GetPathInfo(m_creature->GetEntry(), 1))
502303
{
503-
error_db_log("SD2: EscortAI Start with 0 waypoints (possible missing entry in script_waypoint).");
304+
script_error_log("EscortAI attempt to start escorting for %s, but has no waypints loaded", m_creature->GetGuidStr().c_str());
504305
return;
505306
}
506307

@@ -516,24 +317,19 @@ void npc_escortAI::Start(bool bRun, const Player* pPlayer, const Quest* pQuest,
516317
if (m_bCanReturnToStart && m_bCanInstantRespawn)
517318
debug_log("SD2: EscortAI is set to return home after waypoint end and instant respawn at waypoint end. Creature will never despawn.");
518319

519-
if (m_creature->GetMotionMaster()->GetCurrentMovementGeneratorType() == WAYPOINT_MOTION_TYPE)
520-
{
521-
m_creature->GetMotionMaster()->MovementExpired();
522-
m_creature->GetMotionMaster()->MoveIdle();
523-
debug_log("SD2: EscortAI start with WAYPOINT_MOTION_TYPE, changed to MoveIdle.");
524-
}
525-
526320
// disable npcflags
527321
m_creature->SetUInt32Value(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_NONE);
528322

529-
debug_log("SD2: EscortAI started with " SIZEFMTD " waypoints. Run = %d, PlayerGuid = %s", WaypointList.size(), m_bIsRunning, m_playerGuid.GetString().c_str());
530-
531-
CurrentWP = WaypointList.begin();
323+
AddEscortState(STATE_ESCORT_ESCORTING);
532324

533325
// Set initial speed
534326
m_creature->SetWalk(!m_bIsRunning);
535327

536-
AddEscortState(STATE_ESCORT_ESCORTING);
328+
m_creature->StopMoving();
329+
330+
// Start moving along the path with 2500ms delay
331+
m_creature->GetMotionMaster()->Clear(false, true);
332+
m_creature->GetMotionMaster()->MoveWaypoint(1, 3, 2500);
537333

538334
JustStartedEscort();
539335
}
@@ -544,7 +340,13 @@ void npc_escortAI::SetEscortPaused(bool bPaused)
544340
return;
545341

546342
if (bPaused)
343+
{
547344
AddEscortState(STATE_ESCORT_PAUSED);
345+
m_creature->addUnitState(UNIT_STAT_WAYPOINT_PAUSED);
346+
}
548347
else
348+
{
549349
RemoveEscortState(STATE_ESCORT_PAUSED);
350+
m_creature->clearUnitState(UNIT_STAT_WAYPOINT_PAUSED);
351+
}
550352
}

0 commit comments

Comments
 (0)