forked from insider42/scriptdev2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathescort_ai.cpp
More file actions
520 lines (420 loc) · 14.9 KB
/
escort_ai.cpp
File metadata and controls
520 lines (420 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/* Copyright (C) 2006 - 2011 ScriptDev2 <http://www.scriptdev2.com/>
* This program is free software licensed under GPL version 2
* Please see the included DOCS/LICENSE.TXT for more information */
/* ScriptData
SDName: EscortAI
SD%Complete: 100
SDComment:
SDCategory: Npc
EndScriptData */
#include "precompiled.h"
#include "escort_ai.h"
const float MAX_PLAYER_DISTANCE = 66.0f;
enum
{
POINT_LAST_POINT = 0xFFFFFF,
POINT_HOME = 0xFFFFFE
};
npc_escortAI::npc_escortAI(Creature* pCreature) : ScriptedAI(pCreature),
m_uiPlayerGUID(0),
m_uiPlayerCheckTimer(1000),
m_uiWPWaitTimer(2500),
m_uiEscortState(STATE_ESCORT_NONE),
m_bIsRunning(false),
m_pQuestForEscort(NULL),
m_bCanInstantRespawn(false),
m_bCanReturnToStart(false)
{}
bool npc_escortAI::IsVisible(Unit* pWho) const
{
if (!pWho)
return false;
return m_creature->IsWithinDist(pWho, VISIBLE_RANGE) && pWho->isVisibleForOrDetect(m_creature, m_creature, true);
}
void npc_escortAI::AttackStart(Unit* pWho)
{
if (!pWho)
return;
if (m_creature->Attack(pWho, true))
{
m_creature->AddThreat(pWho);
m_creature->SetInCombatWith(pWho);
pWho->SetInCombatWith(m_creature);
if (m_creature->GetMotionMaster()->GetCurrentMovementGeneratorType() == POINT_MOTION_TYPE)
m_creature->GetMotionMaster()->MovementExpired();
if (IsCombatMovement())
m_creature->GetMotionMaster()->MoveChase(pWho);
}
}
void npc_escortAI::EnterCombat(Unit* pEnemy)
{
if (!pEnemy)
return;
Aggro(pEnemy);
}
void npc_escortAI::Aggro(Unit* pEnemy)
{
}
//see followerAI
bool npc_escortAI::AssistPlayerInCombat(Unit* pWho)
{
if (!pWho->getVictim())
return false;
// experimental (unknown) flag not present
if (!(m_creature->GetCreatureInfo()->type_flags & CREATURE_TYPEFLAGS_CAN_ASSIST))
return false;
// unit state prevents (similar check is done in CanInitiateAttack which also include checking unit_flags. We skip those here)
if (m_creature->hasUnitState(UNIT_STAT_STUNNED | UNIT_STAT_DIED))
return false;
// victim of pWho is not a player
if (!pWho->getVictim()->GetCharmerOrOwnerPlayerOrPlayerItself())
return false;
// never attack friendly
if (m_creature->IsFriendlyTo(pWho))
return false;
// too far away and no free sight?
if (m_creature->IsWithinDistInMap(pWho, MAX_PLAYER_DISTANCE) && m_creature->IsWithinLOSInMap(pWho))
{
// already fighting someone?
if (!m_creature->getVictim())
{
AttackStart(pWho);
return true;
}
else
{
pWho->SetInCombatWith(m_creature);
m_creature->AddThreat(pWho);
return true;
}
}
return false;
}
void npc_escortAI::MoveInLineOfSight(Unit* pWho)
{
if (pWho->isTargetableForAttack() && pWho->isInAccessablePlaceFor(m_creature))
{
// AssistPlayerInCombat can start attack, so return if true
if (HasEscortState(STATE_ESCORT_ESCORTING) && AssistPlayerInCombat(pWho))
return;
if (!m_creature->CanInitiateAttack())
return;
if (!m_creature->CanFly() && m_creature->GetDistanceZ(pWho) > CREATURE_Z_ATTACK_RANGE)
return;
if (m_creature->IsHostileTo(pWho))
{
float fAttackRadius = m_creature->GetAttackDistance(pWho);
if (m_creature->IsWithinDistInMap(pWho, fAttackRadius) && m_creature->IsWithinLOSInMap(pWho))
{
if (!m_creature->getVictim())
{
pWho->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH);
AttackStart(pWho);
}
else if (m_creature->GetMap()->IsDungeon())
{
pWho->SetInCombatWith(m_creature);
m_creature->AddThreat(pWho);
}
}
}
}
}
void npc_escortAI::JustDied(Unit* pKiller)
{
if (!HasEscortState(STATE_ESCORT_ESCORTING) || !m_uiPlayerGUID || !m_pQuestForEscort)
return;
if (Player* pPlayer = GetPlayerForEscort())
{
if (Group* pGroup = pPlayer->GetGroup())
{
for(GroupReference* pRef = pGroup->GetFirstMember(); pRef != NULL; pRef = pRef->next())
{
if (Player* pMember = pRef->getSource())
{
if (pMember->GetQuestStatus(m_pQuestForEscort->GetQuestId()) == QUEST_STATUS_INCOMPLETE)
pMember->FailQuest(m_pQuestForEscort->GetQuestId());
}
}
}
else
{
if (pPlayer->GetQuestStatus(m_pQuestForEscort->GetQuestId()) == QUEST_STATUS_INCOMPLETE)
pPlayer->FailQuest(m_pQuestForEscort->GetQuestId());
}
}
}
void npc_escortAI::JustRespawned()
{
m_uiEscortState = STATE_ESCORT_NONE;
if (!IsCombatMovement())
SetCombatMovement(true);
//add a small delay before going to first waypoint, normal in near all cases
m_uiWPWaitTimer = 2500;
if (m_creature->getFaction() != m_creature->GetCreatureInfo()->faction_A)
m_creature->setFaction(m_creature->GetCreatureInfo()->faction_A);
Reset();
}
void npc_escortAI::EnterEvadeMode()
{
m_creature->RemoveAllAuras();
m_creature->DeleteThreatList();
m_creature->CombatStop(true);
m_creature->SetLootRecipient(NULL);
if (HasEscortState(STATE_ESCORT_ESCORTING))
{
debug_log("SD2: EscortAI has left combat and is now returning to CombatStartPosition.");
if (m_creature->GetMotionMaster()->GetCurrentMovementGeneratorType() == CHASE_MOTION_TYPE)
{
AddEscortState(STATE_ESCORT_RETURNING);
float fPosX, fPosY, fPosZ;
m_creature->GetCombatStartPosition(fPosX, fPosY, fPosZ);
m_creature->GetMotionMaster()->MovePoint(POINT_LAST_POINT, fPosX, fPosY, fPosZ);
}
}
else
{
if (m_creature->GetMotionMaster()->GetCurrentMovementGeneratorType() == CHASE_MOTION_TYPE)
m_creature->GetMotionMaster()->MoveTargetedHome();
}
Reset();
}
bool npc_escortAI::IsPlayerOrGroupInRange()
{
if (Player* pPlayer = GetPlayerForEscort())
{
if (Group* pGroup = pPlayer->GetGroup())
{
for(GroupReference* pRef = pGroup->GetFirstMember(); pRef != NULL; pRef = pRef->next())
{
Player* pMember = pRef->getSource();
if (pMember && m_creature->IsWithinDistInMap(pMember, MAX_PLAYER_DISTANCE))
{
return true;
break;
}
}
}
else
{
if (m_creature->IsWithinDistInMap(pPlayer, MAX_PLAYER_DISTANCE))
return true;
}
}
return false;
}
void npc_escortAI::UpdateAI(const uint32 uiDiff)
{
//Waypoint Updating
if (HasEscortState(STATE_ESCORT_ESCORTING) && !m_creature->getVictim() && m_uiWPWaitTimer && !HasEscortState(STATE_ESCORT_RETURNING))
{
if (m_uiWPWaitTimer <= uiDiff)
{
//End of the line
if (CurrentWP == WaypointList.end())
{
debug_log("SD2: EscortAI reached end of waypoints");
if (m_bCanReturnToStart)
{
float fRetX, fRetY, fRetZ;
m_creature->GetRespawnCoord(fRetX, fRetY, fRetZ);
m_creature->GetMotionMaster()->MovePoint(POINT_HOME, fRetX, fRetY, fRetZ);
m_uiWPWaitTimer = 0;
debug_log("SD2: EscortAI are returning home to spawn location: %u, %f, %f, %f", POINT_HOME, fRetX, fRetY, fRetZ);
return;
}
if (m_bCanInstantRespawn)
{
m_creature->SetDeathState(JUST_DIED);
m_creature->Respawn();
}
else
m_creature->ForcedDespawn();
return;
}
if (!HasEscortState(STATE_ESCORT_PAUSED))
{
m_creature->GetMotionMaster()->MovePoint(CurrentWP->uiId, CurrentWP->fX, CurrentWP->fY, CurrentWP->fZ);
debug_log("SD2: EscortAI start waypoint %u (%f, %f, %f).", CurrentWP->uiId, CurrentWP->fX, CurrentWP->fY, CurrentWP->fZ);
WaypointStart(CurrentWP->uiId);
m_uiWPWaitTimer = 0;
}
}
else
m_uiWPWaitTimer -= uiDiff;
}
//Check if player or any member of his group is within range
if (HasEscortState(STATE_ESCORT_ESCORTING) && m_uiPlayerGUID && !m_creature->getVictim() && !HasEscortState(STATE_ESCORT_RETURNING))
{
if (m_uiPlayerCheckTimer < uiDiff)
{
if (!HasEscortState(STATE_ESCORT_PAUSED) && !IsPlayerOrGroupInRange())
{
debug_log("SD2: EscortAI failed because player/group was to far away or not found");
if (m_bCanInstantRespawn)
{
m_creature->SetDeathState(JUST_DIED);
m_creature->Respawn();
}
else
m_creature->ForcedDespawn();
return;
}
m_uiPlayerCheckTimer = 1000;
}
else
m_uiPlayerCheckTimer -= uiDiff;
}
UpdateEscortAI(uiDiff);
}
void npc_escortAI::UpdateEscortAI(const uint32 uiDiff)
{
//Check if we have a current target
if (!m_creature->SelectHostileTarget() || !m_creature->getVictim())
return;
DoMeleeAttackIfReady();
}
void npc_escortAI::MovementInform(uint32 uiMoveType, uint32 uiPointId)
{
if (uiMoveType != POINT_MOTION_TYPE || !HasEscortState(STATE_ESCORT_ESCORTING))
return;
//Combat start position reached, continue waypoint movement
if (uiPointId == POINT_LAST_POINT)
{
debug_log("SD2: EscortAI has returned to original position before combat");
if (m_bIsRunning && m_creature->HasSplineFlag(SPLINEFLAG_WALKMODE))
m_creature->RemoveSplineFlag(SPLINEFLAG_WALKMODE);
else if (!m_bIsRunning && !m_creature->HasSplineFlag(SPLINEFLAG_WALKMODE))
m_creature->AddSplineFlag(SPLINEFLAG_WALKMODE);
RemoveEscortState(STATE_ESCORT_RETURNING);
if (!m_uiWPWaitTimer)
m_uiWPWaitTimer = 1;
}
else if (uiPointId == POINT_HOME)
{
debug_log("SD2: 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->uiId != uiPointId)
{
error_log("SD2: EscortAI reached waypoint out of order %u, expected %u.", uiPointId, CurrentWP->uiId);
return;
}
debug_log("SD2: EscortAI waypoint %u reached.", CurrentWP->uiId);
//Call WP function
WaypointReached(CurrentWP->uiId);
m_uiWPWaitTimer = CurrentWP->uiWaitTime + 1;
++CurrentWP;
}
}
/*void npc_escortAI::AddWaypoint(uint32 id, float x, float y, float z, uint32 WaitTimeMs)
{
Escort_Waypoint t(id, x, y, z, WaitTimeMs);
WaypointList.push_back(t);
}*/
void npc_escortAI::FillPointMovementListForCreature()
{
std::vector<ScriptPointMove> const &pPointsEntries = pSystemMgr.GetPointMoveList(m_creature->GetEntry());
if (pPointsEntries.empty())
return;
std::vector<ScriptPointMove>::const_iterator itr;
for (itr = pPointsEntries.begin(); itr != pPointsEntries.end(); ++itr)
{
Escort_Waypoint pPoint(itr->uiPointId, itr->fX, itr->fY, itr->fZ, itr->uiWaitTime);
WaypointList.push_back(pPoint);
}
}
void npc_escortAI::SetCurrentWaypoint(uint32 uiPointId)
{
if (!(HasEscortState(STATE_ESCORT_PAUSED))) // only when paused
return;
if (uiPointId >= WaypointList.size()) // too high number
return;
if (uiPointId == CurrentWP->uiId) // already here
return;
CurrentWP = WaypointList.begin(); // set to begin (can't -- backwards in itr list)
while(uiPointId != CurrentWP->uiId)
{
++CurrentWP;
if (CurrentWP == WaypointList.end())
break;
}
m_uiWPWaitTimer = 1;
debug_log("SD2: EscortAI current waypoint set to id %u", CurrentWP->uiId);
}
void npc_escortAI::SetRun(bool bRun)
{
if (bRun)
{
if (!m_bIsRunning)
m_creature->RemoveSplineFlag(SPLINEFLAG_WALKMODE);
else
debug_log("SD2: EscortAI attempt to set run mode, but is already running.");
}
else
{
if (m_bIsRunning)
m_creature->AddSplineFlag(SPLINEFLAG_WALKMODE);
else
debug_log("SD2: EscortAI attempt to set walk mode, but is already walking.");
}
m_bIsRunning = bRun;
}
//TODO: get rid of this many variables passed in function.
void npc_escortAI::Start(bool bRun, uint64 uiPlayerGUID, const Quest* pQuest, bool bInstantRespawn, bool bCanLoopPath)
{
if (m_creature->getVictim())
{
error_log("SD2: EscortAI attempt to Start while in combat.");
return;
}
if (HasEscortState(STATE_ESCORT_ESCORTING))
{
error_log("SD2: EscortAI attempt to Start while already escorting.");
return;
}
if (!WaypointList.empty())
WaypointList.clear();
FillPointMovementListForCreature();
if (WaypointList.empty())
{
error_db_log("SD2: EscortAI Start with 0 waypoints (possible missing entry in script_waypoint).");
return;
}
//set variables
m_bIsRunning = bRun;
m_uiPlayerGUID = uiPlayerGUID;
m_pQuestForEscort = pQuest;
m_bCanInstantRespawn = bInstantRespawn;
m_bCanReturnToStart = bCanLoopPath;
if (m_bCanReturnToStart && m_bCanInstantRespawn)
debug_log("SD2: EscortAI is set to return home after waypoint end and instant respawn at waypoint end. Creature will never despawn.");
if (m_creature->GetMotionMaster()->GetCurrentMovementGeneratorType() == WAYPOINT_MOTION_TYPE)
{
m_creature->GetMotionMaster()->MovementExpired();
m_creature->GetMotionMaster()->MoveIdle();
debug_log("SD2: EscortAI start with WAYPOINT_MOTION_TYPE, changed to MoveIdle.");
}
//disable npcflags
m_creature->SetUInt32Value(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_NONE);
debug_log("SD2: EscortAI started with " SIZEFMTD " waypoints. Run = %d, PlayerGUID = " UI64FMTD, WaypointList.size(), m_bIsRunning, m_uiPlayerGUID);
CurrentWP = WaypointList.begin();
//Set initial speed
if (m_bIsRunning)
m_creature->RemoveSplineFlag(SPLINEFLAG_WALKMODE);
AddEscortState(STATE_ESCORT_ESCORTING);
JustStartedEscort();
}
void npc_escortAI::SetEscortPaused(bool bPaused)
{
if (!HasEscortState(STATE_ESCORT_ESCORTING))
return;
if (bPaused)
AddEscortState(STATE_ESCORT_PAUSED);
else
RemoveEscortState(STATE_ESCORT_PAUSED);
}