Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
ScriptedCreature.h
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#ifndef SCRIPTEDCREATURE_H_
7#define SCRIPTEDCREATURE_H_
8
9#include "Creature.h"
10#include "CreatureAI.h"
11#include "CreatureAIImpl.h"
12#include "InstanceScript.h"
13
14#define CAST_AI(a, b) (dynamic_cast<a*>(b))
15
16class InstanceScript;
17
19{
20public:
21 typedef std::list<uint64> StorageType;
22 typedef StorageType::iterator iterator;
23 typedef StorageType::const_iterator const_iterator;
24 typedef StorageType::size_type size_type;
25 typedef StorageType::value_type value_type;
26
27 explicit SummonList(Creature* creature)
28 : me(creature)
29 { }
30
31 // And here we see a problem of original inheritance approach. People started
32 // to exploit presence of std::list members, so I have to provide wrappers
33
35 {
36 return storage_.begin();
37 }
38
40 {
41 return storage_.begin();
42 }
43
45 {
46 return storage_.end();
47 }
48
50 {
51 return storage_.end();
52 }
53
55 {
56 return storage_.erase(i);
57 }
58
59 bool empty() const
60 {
61 return storage_.empty();
62 }
63
65 {
66 return storage_.size();
67 }
68
69 void Summon(Creature const* summon)
70 {
71 storage_.push_back(summon->GetGUID());
72 }
73 void Despawn(Creature const* summon)
74 {
75 storage_.remove(summon->GetGUID());
76 }
77 void DespawnEntry(uint32 entry);
78 void DespawnAll();
79
80 template <typename T>
81 void DespawnIf(T const& predicate)
82 {
83 storage_.remove_if(predicate);
84 }
85
86 template <class Predicate>
87 void DoAction(int32 info, Predicate& predicate, uint16 max = 0)
88 {
89 // We need to use a copy of SummonList here, otherwise original SummonList would be modified
90 StorageType listCopy = storage_;
92 for (StorageType::iterator i = listCopy.begin(); i != listCopy.end();)
93 {
94 Creature* summon = Unit::GetCreature(*me, *i++);
95 if (summon && summon->IsAIEnabled)
96 summon->AI()->DoAction(info);
97 }
98 }
99
100 void DoZoneInCombat(uint32 entry = 0);
101 void RemoveNotExisting();
102 bool HasEntry(uint32 entry) const;
103
104private:
107};
108
110{
111public:
113 { }
115 {
116 return GUID_ENPART(guid) == _entry;
117 }
118
119private:
121};
122
124{
125public:
127 {
128 return true;
129 }
130};
131
132struct ScriptedAI : public CreatureAI
133{
134 explicit ScriptedAI(Creature* creature);
135 virtual ~ScriptedAI()
136 { }
137
138 // *************
139 //CreatureAI Functions
140 // *************
141
142 void AttackStartNoMove(Unit* target);
143
144 // Called at any Damage from any attacker (before damage apply)
145 void DamageTaken(Unit* /*attacker*/, uint32& /*damage*/) OVERRIDE { }
146
147 //Called at World update tick
148 void UpdateAI(uint32 diff) OVERRIDE;
149
150 void ExecuteEvent(uint32 /*eventId*/) OVERRIDE { }
151
152 //Called at creature death
153 void JustDied(Unit* /*killer*/) OVERRIDE { }
154
155 //Called at creature killing another unit
156 void KilledUnit(Unit* /*victim*/) OVERRIDE { }
157
158 // Called when the creature summon successfully other creature
159 void JustSummoned(Creature* /*summon*/) OVERRIDE { }
160
161 // Called when a summoned creature is despawned
163
164 // Called when hit by a spell
165 void SpellHit(Unit* /*caster*/, SpellInfo const* /*spell*/) OVERRIDE { }
166
167 // Called when spell hits a target
168 void SpellHitTarget(Unit* /*target*/, SpellInfo const* /*spell*/) OVERRIDE { }
169
170 //Called at waypoint reached or PointMovement end
171 void MovementInform(uint32 /*type*/, uint32 /*id*/) OVERRIDE { }
172
173 // Called when AI is temporarily replaced or put back when possess is applied or removed
174 void OnPossess(bool /*apply*/)
175 { }
176
177 // *************
178 // Variables
179 // *************
180
181 //Pointer to creature we are manipulating
183
184 //For fleeing
186
187 // *************
188 //Pure virtual functions
189 // *************
190
191 //Called at creature reset either by death or evade
192 void Reset() OVERRIDE { }
193
194 //Called at creature aggro either by MoveInLOS or Attack Start
195 void EnterCombat(Unit* /*victim*/) OVERRIDE { }
196
197 // Called before EnterCombat even before the creature is in combat.
198 void AttackStart(Unit* /*target*/) OVERRIDE;
199
200 // *************
201 //AI Helper Functions
202 // *************
203
204 //Start movement toward victim
205 void DoStartMovement(Unit* target, float distance = 0.0f, float angle = 0.0f);
206
207 //Start no movement on victim
208 void DoStartNoMovement(Unit* target);
209
210 //Stop attack of current victim
211 void DoStopAttack();
212
213 //Cast spell by spell info
214 void DoCastSpell(Unit* target, SpellInfo const* spellInfo, bool triggered = false);
215
216 //Plays a sound to all nearby players
217 void DoPlaySoundToSet(WorldObject* source, uint32 soundId);
218
219 //Drops all threat to 0%. Does not remove players from the threat list
220 void DoResetThreat();
221
222 float DoGetThreat(Unit* unit);
223 void DoModifyThreatPercent(Unit* unit, int32 pct);
224
225 void DoTeleportTo(float x, float y, float z, uint32 time = 0);
226 void DoTeleportTo(float const pos[4]);
227
228 //Teleports a player without dropping threat (only teleports to same map)
229 void DoTeleportPlayer(Unit* unit, float x, float y, float z, float o);
230 void DoTeleportAll(float x, float y, float z, float o);
231
232 //Returns friendly unit with the most amount of hp missing from max hp
233 Unit* DoSelectLowestHpFriendly(float range, uint32 minHPDiff = 1);
234
235 //Returns a list of friendly CC'd units within range
236 std::list<Creature*> DoFindFriendlyCC(float range);
237
238 //Returns a list of all friendly units missing a specific buff within range
239 std::list<Creature*> DoFindFriendlyMissingBuff(float range, uint32 spellId);
240
241 //Return a player with at least minimumRange from me
242 Player* GetPlayerAtMinimumRange(float minRange);
243
244 //Spawns a creature relative to me
245 Creature* DoSpawnCreature(uint32 entry, float offsetX, float offsetY, float offsetZ, float angle, TempSummonType type, uint32 despawntime);
246
247 bool HealthBelowPct(uint32 pct) const
248 {
249 return me->HealthBelowPct(pct);
250 }
251 bool HealthAbovePct(uint32 pct) const
252 {
253 return me->HealthAbovePct(pct);
254 }
255
256 //Returns spells that meet the specified criteria from the creatures spell list
257 SpellInfo const* SelectSpell(Unit* target, uint32 school, uint32 mechanic, SelectTargetType targets, uint32 powerCostMin, uint32 powerCostMax, float rangeMin, float rangeMax, SelectEffect effect);
258
259 void SetEquipmentSlots(bool loadDefault, int32 mainHand = EQUIP_NO_CHANGE, int32 offHand = EQUIP_NO_CHANGE, int32 ranged = EQUIP_NO_CHANGE);
260
261 // Used to control if MoveChase() is to be used or not in AttackStart(). Some creatures does not chase victims
262 // NOTE: If you use SetCombatMovement while the creature is in combat, it will do NOTHING - This only affects AttackStart
263 // You should make the necessary to make it happen so.
264 // Remember that if you modified _isCombatMovementAllowed (e.g: using SetCombatMovement) it will not be reset at Reset().
265 // It will keep the last value you set.
266 void SetCombatMovement(bool allowMovement);
268 {
270 }
271
272 bool EnterEvadeIfOutOfCombatArea(uint32 const diff);
273
274 // return true for heroic mode. i.e.
275 // - for dungeon in mode 10-heroic,
276 // - for raid in mode 10-Heroic
277 // - for raid in mode 25-heroic
278 // DO NOT USE to check raid in mode 25-normal.
279 bool IsHeroic() const
280 {
281 return _isHeroic;
282 }
283
284 // return the dungeon or raid difficulty
286 {
287 return _difficulty;
288 }
289
290 // return true for 25 man or 25 man heroic mode
291 bool Is25ManRaid() const;
292
293 template<class T> inline
294 const T& DUNGEON_MODE(const T& normal5, const T& heroic5) const
295 {
296 switch (_difficulty)
297 {
299 return normal5;
301 return heroic5;
302 default:
303 break;
304 }
305
306 return heroic5;
307 }
308
309 template<class T> inline
310 const T& RAID_MODE(const T& normal10, const T& normal25) const
311 {
312 switch (_difficulty)
313 {
315 return normal10;
317 return normal25;
318 default:
319 break;
320 }
321
322 return normal25;
323 }
324
325 template<class T> inline
326 const T& RAID_MODE(const T& normal10, const T& normal25, const T& flex, const T& lfr) const
327 {
328 switch (_difficulty)
329 {
331 return normal10;
333 return normal25;
334 case DIFFICULTY_FLEX:
335 return flex;
337 return lfr;
338 default:
339 break;
340 }
341
342 return normal25;
343 }
344
345 template<class T> inline
346 const T& RAID_MODE(const T& normal10, const T& normal25, const T& heroic10, const T& heroic25, const T& flex, const T& lfr) const
347 {
348 switch (_difficulty)
349 {
351 return normal10;
353 return normal25;
355 return heroic10;
357 return heroic25;
358 case DIFFICULTY_FLEX:
359 return flex;
361 return lfr;
362 default:
363 break;
364 }
365
366 return heroic25;
367 }
368
369private:
374};
375
376class BossAI : public ScriptedAI
377{
378public:
379 BossAI(Creature* creature, uint32 bossId);
380 virtual ~BossAI()
381 { }
382
385 {
386 return _boundary;
387 }
388
389 void JustSummoned(Creature* summon) OVERRIDE;
391
392 void UpdateAI(uint32 diff) OVERRIDE;
393
394 // Hook used to execute events scheduled into EventMap without the need
395 // to override UpdateAI
396 // note: You must re-schedule the event within this method if the event
397 // is supposed to run more than once
398 void ExecuteEvent(uint32 /*eventId*/) OVERRIDE { }
399
401 {
402 _Reset();
403 }
404 void EnterCombat(Unit* /*who*/) OVERRIDE
405 {
406 _EnterCombat();
407 }
408 void JustDied(Unit* /*killer*/) OVERRIDE
409 {
410 _JustDied();
411 }
413 {
415 }
416
417protected:
418 void _Reset();
419 void _EnterCombat();
420 void _JustDied();
422 {
423 me->setActive(false);
424 }
425 void _DespawnAtEvade();
426
428 {
429 if (CheckBoundary(me))
430 return true;
431
433 return false;
434 }
435
436 bool CheckBoundary(Unit* who);
437 void TeleportCheaters();
438
441
442private:
445};
446
448{
449public:
450 WorldBossAI(Creature* creature);
451 virtual ~WorldBossAI()
452 { }
453
454 void JustSummoned(Creature* summon) OVERRIDE;
456
457 void UpdateAI(uint32 diff) OVERRIDE;
458
459 // Hook used to execute events scheduled into EventMap without the need
460 // to override UpdateAI
461 // note: You must re-schedule the event within this method if the event
462 // is supposed to run more than once
463 void ExecuteEvent(uint32 /*eventId*/) OVERRIDE { }
464
466 {
467 _Reset();
468 }
469 void EnterCombat(Unit* /*who*/) OVERRIDE
470 {
471 _EnterCombat();
472 }
473 void JustDied(Unit* /*killer*/) OVERRIDE
474 {
475 _JustDied();
476 }
477
478protected:
479 void _Reset();
480 void _EnterCombat();
481 void _JustDied();
482
485};
486
487// SD2 grid searchers.
488Creature* GetClosestCreatureWithEntry(WorldObject* source, uint32 entry, float maxSearchRange, bool alive = true);
489GameObject* GetClosestGameObjectWithEntry(WorldObject* source, uint32 entry, float maxSearchRange);
490void GetCreatureListWithEntryInGrid(std::list<Creature*>& list, WorldObject* source, uint32 entry, float maxSearchRange);
491void GetGameObjectListWithEntryInGrid(std::list<GameObject*>& list, WorldObject* source, uint32 entry, float maxSearchRange);
492void GetPlayerListInGrid(std::list<Player*>& list, WorldObject* source, float maxSearchRange);
493void GetPositionWithDistInOrientation(Unit* pUnit, float dist, float orientation, float& x, float& y);
494void GetRandPosFromCenterInDist(float centerX, float centerY, float dist, float& x, float& y);
495#endif // SCRIPTEDCREATURE_H_
SelectTargetType
Definition CreatureAI.h:24
@ EQUIP_NO_CHANGE
Definition CreatureAI.h:49
SelectEffect
Definition CreatureAI.h:40
DifficultyID
Definition DBCEnums.h:330
@ DIFFICULTY_10MAN_HEROIC
Definition DBCEnums.h:336
@ DIFFICULTY_25MAN_NORMAL
Definition DBCEnums.h:335
@ DIFFICULTY_NORMAL
Definition DBCEnums.h:332
@ DIFFICULTY_HEROIC
Definition DBCEnums.h:333
@ DIFFICULTY_25MAN_LFR
Definition DBCEnums.h:338
@ DIFFICULTY_FLEX
Definition DBCEnums.h:343
@ DIFFICULTY_10MAN_NORMAL
Definition DBCEnums.h:334
@ DIFFICULTY_25MAN_HEROIC
Definition DBCEnums.h:337
std::int32_t int32
Definition Define.h:73
std::uint32_t uint32
Definition Define.h:77
#define OVERRIDE
Definition Define.h:60
std::uint64_t uint64
Definition Define.h:76
std::uint16_t uint16
Definition Define.h:78
std::map< BoundaryType, float > BossBoundaryMap
TempSummonType
Definition Object.h:67
uint32 GUID_ENPART(uint64 x)
void GetPlayerListInGrid(std::list< Player * > &list, WorldObject *source, float maxSearchRange)
GameObject * GetClosestGameObjectWithEntry(WorldObject *source, uint32 entry, float maxSearchRange)
void GetPositionWithDistInOrientation(Unit *pUnit, float dist, float orientation, float &x, float &y)
Creature * GetClosestCreatureWithEntry(WorldObject *source, uint32 entry, float maxSearchRange, bool alive=true)
void GetCreatureListWithEntryInGrid(std::list< Creature * > &list, WorldObject *source, uint32 entry, float maxSearchRange)
void GetRandPosFromCenterInDist(float centerX, float centerY, float dist, float &x, float &y)
void GetGameObjectListWithEntryInGrid(std::list< GameObject * > &list, WorldObject *source, uint32 entry, float maxSearchRange)
InstanceScript *const instance
void _JustReachedHome()
BossBoundaryMap const * GetBoundary() const
void SummonedCreatureDespawn(Creature *summon) OVERRIDE
void JustSummoned(Creature *summon) OVERRIDE
BossBoundaryMap const *const _boundary
virtual ~BossAI()
void TeleportCheaters()
void JustReachedHome() OVERRIDE
void ExecuteEvent(uint32) OVERRIDE
void _DespawnAtEvade()
void UpdateAI(uint32 diff) OVERRIDE
== Triggered Actions Requested ==================
uint32 const _bossId
bool CheckInRoom()
SummonList summons
EventMap events
void Reset() OVERRIDE
BossAI(Creature *creature, uint32 bossId)
void EnterCombat(Unit *) OVERRIDE
void _EnterCombat()
void JustDied(Unit *) OVERRIDE
bool CheckBoundary(Unit *who)
CreatureAI(Creature *creature)
Definition CreatureAI.h:69
virtual void EnterEvadeMode()
CreatureAI * AI() const
Definition Creature.h:483
bool operator()(uint64)
bool operator()(uint64 guid)
EntryCheckPredicate(uint32 entry)
uint32 _entry
uint64 GetGUID() const
Definition Object.h:119
Creature * me
bool empty() const
iterator erase(iterator i)
bool HasEntry(uint32 entry) const
void Despawn(Creature const *summon)
iterator begin()
StorageType::const_iterator const_iterator
void DespawnEntry(uint32 entry)
void DespawnIf(T const &predicate)
void RemoveNotExisting()
void Summon(Creature const *summon)
StorageType::size_type size_type
iterator end()
SummonList(Creature *creature)
StorageType storage_
StorageType::value_type value_type
size_type size() const
void DoZoneInCombat(uint32 entry=0)
const_iterator begin() const
std::list< uint64 > StorageType
const_iterator end() const
StorageType::iterator iterator
void DoAction(int32 info, Predicate &predicate, uint16 max=0)
virtual void DoAction(int32)
Definition UnitAI.h:126
Definition Unit.h:1367
static Creature * GetCreature(WorldObject &object, uint64 guid)
Definition Unit.cpp:4905
bool IsAIEnabled
Definition Unit.h:2735
void SummonedCreatureDespawn(Creature *summon) OVERRIDE
void UpdateAI(uint32 diff) OVERRIDE
== Triggered Actions Requested ==================
void EnterCombat(Unit *) OVERRIDE
virtual ~WorldBossAI()
WorldBossAI(Creature *creature)
void ExecuteEvent(uint32) OVERRIDE
void Reset() OVERRIDE
void JustSummoned(Creature *summon) OVERRIDE
SummonList summons
void JustDied(Unit *) OVERRIDE
void RandomResizeList(std::list< T > &list, uint32 size)
Definition Containers.h:19
void DamageTaken(Unit *, uint32 &) OVERRIDE
bool IsHeroic() const
void KilledUnit(Unit *) OVERRIDE
void SpellHit(Unit *, SpellInfo const *) OVERRIDE
void DoStartNoMovement(Unit *target)
void Reset() OVERRIDE
void SummonedCreatureDespawn(Creature *) OVERRIDE
void JustSummoned(Creature *) OVERRIDE
const T & RAID_MODE(const T &normal10, const T &normal25, const T &flex, const T &lfr) const
void SetEquipmentSlots(bool loadDefault, int32 mainHand=EQUIP_NO_CHANGE, int32 offHand=EQUIP_NO_CHANGE, int32 ranged=EQUIP_NO_CHANGE)
DifficultyID GetDifficulty() const
void SetCombatMovement(bool allowMovement)
uint32 _evadeCheckCooldown
Creature * me
bool IsCombatMovementAllowed() const
void SpellHitTarget(Unit *, SpellInfo const *) OVERRIDE
bool HealthAbovePct(uint32 pct) const
Unit * DoSelectLowestHpFriendly(float range, uint32 minHPDiff=1)
float DoGetThreat(Unit *unit)
void DoTeleportTo(float x, float y, float z, uint32 time=0)
void OnPossess(bool)
void EnterCombat(Unit *) OVERRIDE
Player * GetPlayerAtMinimumRange(float minRange)
void MovementInform(uint32, uint32) OVERRIDE
void UpdateAI(uint32 diff) OVERRIDE
== Triggered Actions Requested ==================
bool _isCombatMovementAllowed
void DoTeleportPlayer(Unit *unit, float x, float y, float z, float o)
void DoTeleportAll(float x, float y, float z, float o)
virtual ~ScriptedAI()
void ExecuteEvent(uint32) OVERRIDE
const T & RAID_MODE(const T &normal10, const T &normal25, const T &heroic10, const T &heroic25, const T &flex, const T &lfr) const
bool HealthBelowPct(uint32 pct) const
SpellInfo const * SelectSpell(Unit *target, uint32 school, uint32 mechanic, SelectTargetType targets, uint32 powerCostMin, uint32 powerCostMax, float rangeMin, float rangeMax, SelectEffect effect)
bool EnterEvadeIfOutOfCombatArea(uint32 const diff)
void DoCastSpell(Unit *target, SpellInfo const *spellInfo, bool triggered=false)
const T & RAID_MODE(const T &normal10, const T &normal25) const
void JustDied(Unit *) OVERRIDE
void AttackStartNoMove(Unit *target)
std::list< Creature * > DoFindFriendlyMissingBuff(float range, uint32 spellId)
DifficultyID _difficulty
Creature * DoSpawnCreature(uint32 entry, float offsetX, float offsetY, float offsetZ, float angle, TempSummonType type, uint32 despawntime)
std::list< Creature * > DoFindFriendlyCC(float range)
ScriptedAI(Creature *creature)
bool Is25ManRaid() const
void DoModifyThreatPercent(Unit *unit, int32 pct)
void DoPlaySoundToSet(WorldObject *source, uint32 soundId)
const T & DUNGEON_MODE(const T &normal5, const T &heroic5) const
void AttackStart(Unit *) OVERRIDE
void DoStartMovement(Unit *target, float distance=0.0f, float angle=0.0f)