TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ScriptedCreature.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2006-2009 ScriptDev2 <https://scriptdev2.svn.sourceforge.net/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef SCRIPTEDCREATURE_H_
20 #define SCRIPTEDCREATURE_H_
21 
22 #include "Creature.h"
23 #include "CreatureAI.h"
24 #include "CreatureAIImpl.h"
25 #include "InstanceScript.h"
26 #include "TaskScheduler.h"
27 
28 #define CAST_AI(a, b) (dynamic_cast<a*>(b))
29 #define ENSURE_AI(a,b) (EnsureAI<a>(b))
30 
31 template<class T, class U>
32 T* EnsureAI(U* ai)
33 {
34  T* cast_ai = dynamic_cast<T*>(ai);
35  ASSERT(cast_ai);
36  return cast_ai;
37 };
38 
39 class InstanceScript;
40 
42 {
43 public:
45  typedef StorageType::iterator iterator;
46  typedef StorageType::const_iterator const_iterator;
47  typedef StorageType::size_type size_type;
48  typedef StorageType::value_type value_type;
49 
50  explicit SummonList(Creature* creature)
51  : me(creature)
52  { }
53 
54  // And here we see a problem of original inheritance approach. People started
55  // to exploit presence of std::list members, so I have to provide wrappers
56 
57  iterator begin()
58  {
59  return storage_.begin();
60  }
61 
62  const_iterator begin() const
63  {
64  return storage_.begin();
65  }
66 
67  iterator end()
68  {
69  return storage_.end();
70  }
71 
72  const_iterator end() const
73  {
74  return storage_.end();
75  }
76 
77  iterator erase(iterator i)
78  {
79  return storage_.erase(i);
80  }
81 
82  bool empty() const
83  {
84  return storage_.empty();
85  }
86 
87  size_type size() const
88  {
89  return storage_.size();
90  }
91 
92  void Summon(Creature const* summon) { storage_.push_back(summon->GetGUID()); }
93  void Despawn(Creature const* summon) { storage_.remove(summon->GetGUID()); }
94  void DespawnEntry(uint32 entry);
95  void DespawnAll();
96 
97  template <typename T>
98  void DespawnIf(T const &predicate)
99  {
100  storage_.remove_if(predicate);
101  }
102 
103  template <class Predicate>
104  void DoAction(int32 info, Predicate& predicate, uint16 max = 0)
105  {
106  // We need to use a copy of SummonList here, otherwise original SummonList would be modified
107  StorageType listCopy = storage_;
108  Trinity::Containers::RandomResizeList<ObjectGuid, Predicate>(listCopy, predicate, max);
109  for (StorageType::iterator i = listCopy.begin(); i != listCopy.end(); )
110  {
111  Creature* summon = ObjectAccessor::GetCreature(*me, *i++);
112  if (summon && summon->IsAIEnabled)
113  summon->AI()->DoAction(info);
114  }
115  }
116 
117  void DoZoneInCombat(uint32 entry = 0);
118  void RemoveNotExisting();
119  bool HasEntry(uint32 entry) const;
120 
121 private:
123  StorageType storage_;
124 };
125 
127 {
128  public:
129  EntryCheckPredicate(uint32 entry) : _entry(entry) { }
130  bool operator()(ObjectGuid guid) { return guid.GetEntry() == _entry; }
131 
132  private:
134 };
135 
137 {
138  public:
139  bool operator()(ObjectGuid) { return true; }
140 };
141 
143 {
144  explicit ScriptedAI(Creature* creature);
145  virtual ~ScriptedAI() { }
146 
147  // *************
148  //CreatureAI Functions
149  // *************
150 
151  void AttackStartNoMove(Unit* target);
152 
153  // Called at any Damage from any attacker (before damage apply)
154  void DamageTaken(Unit* /*attacker*/, uint32& /*damage*/) override { }
155 
156  //Called at World update tick
157  virtual void UpdateAI(uint32 diff) override;
158 
159  //Called at creature death
160  void JustDied(Unit* /*killer*/) override { }
161 
162  //Called at creature killing another unit
163  void KilledUnit(Unit* /*victim*/) override { }
164 
165  // Called when the creature summon successfully other creature
166  void JustSummoned(Creature* /*summon*/) override { }
167 
168  // Called when a summoned creature is despawned
169  void SummonedCreatureDespawn(Creature* /*summon*/) override { }
170 
171  // Called when hit by a spell
172  void SpellHit(Unit* /*caster*/, SpellInfo const* /*spell*/) override { }
173 
174  // Called when spell hits a target
175  void SpellHitTarget(Unit* /*target*/, SpellInfo const* /*spell*/) override { }
176 
177  // Called when AI is temporarily replaced or put back when possess is applied or removed
178  void OnPossess(bool /*apply*/) { }
179 
180  // *************
181  // Variables
182  // *************
183 
184  //For fleeing
185  bool IsFleeing;
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, uint32 type, uint32 despawntime);
246 
247  bool HealthBelowPct(uint32 pct) const { return me->HealthBelowPct(pct); }
248  bool HealthAbovePct(uint32 pct) const { return me->HealthAbovePct(pct); }
249 
250  //Returns spells that meet the specified criteria from the creatures spell list
251  SpellInfo const* SelectSpell(Unit* target, uint32 school, uint32 mechanic, SelectTargetType targets, float rangeMin, float rangeMax, SelectEffect effect);
252 
253  void SetEquipmentSlots(bool loadDefault, int32 mainHand = EQUIP_NO_CHANGE, int32 offHand = EQUIP_NO_CHANGE, int32 ranged = EQUIP_NO_CHANGE);
254 
255  // Used to control if MoveChase() is to be used or not in AttackStart(). Some creatures does not chase victims
256  // NOTE: If you use SetCombatMovement while the creature is in combat, it will do NOTHING - This only affects AttackStart
257  // You should make the necessary to make it happen so.
258  // Remember that if you modified _isCombatMovementAllowed (e.g: using SetCombatMovement) it will not be reset at Reset().
259  // It will keep the last value you set.
260  void SetCombatMovement(bool allowMovement);
261  bool IsCombatMovementAllowed() const { return _isCombatMovementAllowed; }
262 
263  // return true for heroic mode. i.e.
264  // - for dungeon in mode 10-heroic,
265  // - for raid in mode 10-Heroic
266  // - for raid in mode 25-heroic
267  // DO NOT USE to check raid in mode 25-normal.
268  bool IsHeroic() const { return _isHeroic; }
269 
270  // return the dungeon or raid difficulty
271  Difficulty GetDifficulty() const { return _difficulty; }
272 
273  // return true for 25 man or 25 man heroic mode
274  bool Is25ManRaid() const { return _difficulty == DIFFICULTY_25_N || _difficulty == DIFFICULTY_25_HC; }
275 
276  template<class T> inline
277  const T& DUNGEON_MODE(const T& normal5, const T& heroic10) const
278  {
279  switch (_difficulty)
280  {
281  case DIFFICULTY_NORMAL:
282  return normal5;
283  case DIFFICULTY_HEROIC:
284  return heroic10;
285  default:
286  break;
287  }
288 
289  return heroic10;
290  }
291 
292  template<class T> inline
293  const T& RAID_MODE(const T& normal10, const T& normal25) const
294  {
295  switch (_difficulty)
296  {
297  case DIFFICULTY_10_N:
298  return normal10;
299  case DIFFICULTY_25_N:
300  return normal25;
301  default:
302  break;
303  }
304 
305  return normal25;
306  }
307 
308  template<class T> inline
309  const T& RAID_MODE(const T& normal10, const T& normal25, const T& heroic10, const T& heroic25) const
310  {
311  switch (_difficulty)
312  {
313  case DIFFICULTY_10_N:
314  return normal10;
315  case DIFFICULTY_25_N:
316  return normal25;
317  case DIFFICULTY_10_HC:
318  return heroic10;
319  case DIFFICULTY_25_HC:
320  return heroic25;
321  default:
322  break;
323  }
324 
325  return heroic25;
326  }
327 
328  private:
331  bool _isHeroic;
332 };
333 
335 {
336  public:
337  BossAI(Creature* creature, uint32 bossId);
338  virtual ~BossAI() { }
339 
341 
342  void JustSummoned(Creature* summon) override;
343  void SummonedCreatureDespawn(Creature* summon) override;
344 
345  virtual void UpdateAI(uint32 diff) override;
346 
347  // Hook used to execute events scheduled into EventMap without the need
348  // to override UpdateAI
349  // note: You must re-schedule the event within this method if the event
350  // is supposed to run more than once
351  virtual void ExecuteEvent(uint32 /*eventId*/) { }
352 
353  virtual void ScheduleTasks() { }
354 
355  void Reset() override { _Reset(); }
356  void EnterCombat(Unit* /*who*/) override { _EnterCombat(); }
357  void JustDied(Unit* /*killer*/) override { _JustDied(); }
358  void JustReachedHome() override { _JustReachedHome(); }
359 
360  bool CanAIAttack(Unit const* target) const override { return CheckBoundary(target); }
361 
362  protected:
363  void _Reset();
364  void _EnterCombat();
365  void _JustDied();
366  void _JustReachedHome() { me->setActive(false); }
367  void _DespawnAtEvade();
368 
369  void TeleportCheaters();
370 
374 
375  private:
377 };
378 
380 {
381  public:
382  WorldBossAI(Creature* creature);
383  virtual ~WorldBossAI() { }
384 
385  void JustSummoned(Creature* summon) override;
386  void SummonedCreatureDespawn(Creature* summon) override;
387 
388  virtual void UpdateAI(uint32 diff) override;
389 
390  // Hook used to execute events scheduled into EventMap without the need
391  // to override UpdateAI
392  // note: You must re-schedule the event within this method if the event
393  // is supposed to run more than once
394  virtual void ExecuteEvent(uint32 /*eventId*/) { }
395 
396  void Reset() override { _Reset(); }
397  void EnterCombat(Unit* /*who*/) override { _EnterCombat(); }
398  void JustDied(Unit* /*killer*/) override { _JustDied(); }
399 
400  protected:
401  void _Reset();
402  void _EnterCombat();
403  void _JustDied();
404 
407 };
408 
409 // SD2 grid searchers.
410 TC_GAME_API Creature* GetClosestCreatureWithEntry(WorldObject* source, uint32 entry, float maxSearchRange, bool alive = true);
411 TC_GAME_API GameObject* GetClosestGameObjectWithEntry(WorldObject* source, uint32 entry, float maxSearchRange);
412 TC_GAME_API void GetCreatureListWithEntryInGrid(std::list<Creature*>& list, WorldObject* source, uint32 entry, float maxSearchRange);
413 TC_GAME_API void GetGameObjectListWithEntryInGrid(std::list<GameObject*>& list, WorldObject* source, uint32 entry, float maxSearchRange);
414 TC_GAME_API void GetPlayerListInGrid(std::list<Player*>& list, WorldObject* source, float maxSearchRange);
415 
416 #endif // SCRIPTEDCREATURE_H_
Definition: ScriptedCreature.h:379
Definition: DBCEnums.h:406
void JustReachedHome() override
Definition: ScriptedCreature.h:358
Difficulty
Definition: DBCEnums.h:402
uint32 const _bossId
Definition: ScriptedCreature.h:376
Definition: TaskScheduler.h:49
Definition: DBCEnums.h:407
bool _isHeroic
Definition: ScriptedCreature.h:331
void DamageTaken(Unit *, uint32 &) override
Definition: ScriptedCreature.h:154
void SpellHit(Unit *, SpellInfo const *) override
Definition: ScriptedCreature.h:172
Definition: SpellInfo.h:326
iterator begin()
Definition: ScriptedCreature.h:57
bool HealthBelowPct(uint32 pct) const
Definition: ScriptedCreature.h:247
Definition: ScriptedCreature.h:136
Creature * me
Definition: ScriptedCreature.h:122
StorageType storage_
Definition: ScriptedCreature.h:123
void DoAction(int32 info, Predicate &predicate, uint16 max=0)
Definition: ScriptedCreature.h:104
bool HealthAbovePct(uint32 pct) const
Definition: ScriptedCreature.h:248
void Summon(Creature const *summon)
Definition: ScriptedCreature.h:92
virtual void UpdateAI(uint32 diff) override
Definition: ScriptedCreature.cpp:125
bool IsFleeing
Definition: ScriptedCreature.h:185
virtual void ScheduleTasks()
Definition: ScriptedCreature.h:353
bool IsHeroic() const
Definition: ScriptedCreature.h:268
virtual void UpdateAI(uint32 diff)=0
void JustDied(Unit *) override
Definition: ScriptedCreature.h:160
Definition: Object.h:423
Definition: Creature.h:467
TC_GAME_API void GetCreatureListWithEntryInGrid(std::list< Creature * > &list, WorldObject *source, uint32 entry, float maxSearchRange)
Definition: ScriptedCreature.cpp:621
bool IsCombatMovementAllowed() const
Definition: ScriptedCreature.h:261
void EnterCombat(Unit *) override
Definition: ScriptedCreature.h:356
TaskScheduler scheduler
Definition: ScriptedCreature.h:373
bool IsAIEnabled
Definition: Unit.h:2161
virtual void ExecuteEvent(uint32)
Definition: ScriptedCreature.h:394
uint32 _entry
Definition: ScriptedCreature.h:133
GuidList StorageType
Definition: ScriptedCreature.h:44
double distance(double x, double y)
Definition: g3dmath.h:731
const T & RAID_MODE(const T &normal10, const T &normal25, const T &heroic10, const T &heroic25) const
Definition: ScriptedCreature.h:309
std::list< ObjectGuid > GuidList
Definition: ObjectGuid.h:333
T max(const T &x, const T &y)
Definition: g3dmath.h:320
void Despawn(Creature const *summon)
Definition: ScriptedCreature.h:93
Definition: CreatureAI.h:63
Difficulty GetDifficulty() const
Definition: ScriptedCreature.h:271
bool operator()(ObjectGuid)
Definition: ScriptedCreature.h:139
TC_GAME_API GameObject * GetClosestGameObjectWithEntry(WorldObject *source, uint32 entry, float maxSearchRange)
Definition: ScriptedCreature.cpp:616
void JustSummoned(Creature *) override
Definition: ScriptedCreature.h:166
void EnterCombat(Unit *) override
Definition: ScriptedCreature.h:195
TC_GAME_API void GetGameObjectListWithEntryInGrid(std::list< GameObject * > &list, WorldObject *source, uint32 entry, float maxSearchRange)
Definition: ScriptedCreature.cpp:626
const_iterator end() const
Definition: ScriptedCreature.h:72
Definition: ScriptedCreature.h:142
G3D::int16 z
Definition: Vector3int16.h:46
void _JustReachedHome()
Definition: ScriptedCreature.h:366
Difficulty _difficulty
Definition: ScriptedCreature.h:329
bool CanAIAttack(Unit const *target) const override
Definition: ScriptedCreature.h:360
CreatureAI * AI() const
Definition: Creature.h:525
Definition: EventMap.h:25
int32_t int32
Definition: Define.h:146
Definition: CreatureAI.h:68
uint32_t uint32
Definition: Define.h:150
void SummonedCreatureDespawn(Creature *) override
Definition: ScriptedCreature.h:169
G3D::int16 y
Definition: Vector2int16.h:38
uint16_t uint16
Definition: Define.h:151
void Reset() override
Definition: ScriptedCreature.h:355
Definition: DBCEnums.h:409
Definition: DBCEnums.h:408
Definition: GameObject.h:880
virtual void DoAction(int32)
Definition: UnitAI.h:138
SummonList(Creature *creature)
Definition: ScriptedCreature.h:50
Definition: DBCEnums.h:410
TC_GAME_API void GetPlayerListInGrid(std::list< Player * > &list, WorldObject *source, float maxSearchRange)
Definition: ScriptedCreature.cpp:631
void OnPossess(bool)
Definition: ScriptedCreature.h:178
iterator erase(iterator i)
Definition: ScriptedCreature.h:77
virtual ~WorldBossAI()
Definition: ScriptedCreature.h:383
uint32 GetEntry() const
Definition: ObjectGuid.h:220
SummonList summons
Definition: ScriptedCreature.h:372
TC_GAME_API Creature * GetCreature(WorldObject const &u, ObjectGuid const &guid)
Definition: ObjectAccessor.cpp:174
void Reset() override
Definition: ScriptedCreature.h:192
void EnterCombat(Unit *) override
Definition: ScriptedCreature.h:397
const T & RAID_MODE(const T &normal10, const T &normal25) const
Definition: ScriptedCreature.h:293
Definition: ScriptedCreature.h:126
T * EnsureAI(U *ai)
Definition: ScriptedCreature.h:32
void JustDied(Unit *) override
Definition: ScriptedCreature.h:398
const_iterator begin() const
Definition: ScriptedCreature.h:62
ObjectGuid const & GetGUID() const
Definition: Object.h:105
TC_GAME_API Creature * GetClosestCreatureWithEntry(WorldObject *source, uint32 entry, float maxSearchRange, bool alive=true)
Definition: ScriptedCreature.cpp:611
const T & DUNGEON_MODE(const T &normal5, const T &heroic10) const
Definition: ScriptedCreature.h:277
EntryCheckPredicate(uint32 entry)
Definition: ScriptedCreature.h:129
virtual ~ScriptedAI()
Definition: ScriptedCreature.h:145
void KilledUnit(Unit *) override
Definition: ScriptedCreature.h:163
#define TC_GAME_API
Definition: Define.h:134
void Reset() override
Definition: ScriptedCreature.h:396
SelectEffect
Definition: CreatureAI.h:53
Definition: InstanceScript.h:141
bool _isCombatMovementAllowed
Definition: ScriptedCreature.h:330
StorageType::value_type value_type
Definition: ScriptedCreature.h:48
#define ASSERT
Definition: Errors.h:55
StorageType::const_iterator const_iterator
Definition: ScriptedCreature.h:46
EventMap events
Definition: ScriptedCreature.h:371
Definition: ScriptedCreature.h:41
Definition: ObjectGuid.h:189
G3D::int16 x
Definition: Vector2int16.h:37
Definition: ScriptedCreature.h:334
void SpellHitTarget(Unit *, SpellInfo const *) override
Definition: ScriptedCreature.h:175
void DespawnIf(T const &predicate)
Definition: ScriptedCreature.h:98
StorageType::iterator iterator
Definition: ScriptedCreature.h:45
Definition: Unit.h:1305
bool empty() const
Definition: ScriptedCreature.h:82
virtual void AttackStart(Unit *)
Definition: UnitAI.cpp:29
bool CheckBoundary(Position const *who=nullptr) const
Definition: CreatureAI.cpp:353
void JustDied(Unit *) override
Definition: ScriptedCreature.h:357
virtual void ExecuteEvent(uint32)
Definition: ScriptedCreature.h:351
SummonList summons
Definition: ScriptedCreature.h:406
StorageType::size_type size_type
Definition: ScriptedCreature.h:47
InstanceScript *const instance
Definition: ScriptedCreature.h:340
size_type size() const
Definition: ScriptedCreature.h:87
Definition: DBCEnums.h:405
SelectTargetType
Definition: CreatureAI.h:37
iterator end()
Definition: ScriptedCreature.h:67
virtual ~BossAI()
Definition: ScriptedCreature.h:338
bool operator()(ObjectGuid guid)
Definition: ScriptedCreature.h:130
EventMap events
Definition: ScriptedCreature.h:405
bool Is25ManRaid() const
Definition: ScriptedCreature.h:274