[7921] Set pet and for creature owner in Spell::EffectSummon similar other summon...
[getmangos.git] / src / game / CreatureEventAI.h
blob852d47576d0686da539bd9520623fe662e16ef12
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef MANGOS_CREATURE_EAI_H
20 #define MANGOS_CREATURE_EAI_H
22 #include "Common.h"
23 #include "Creature.h"
24 #include "CreatureAI.h"
25 #include "Unit.h"
27 class Player;
28 class WorldObject;
30 #define EVENT_UPDATE_TIME 500
31 #define MAX_ACTIONS 3
32 #define MAX_PHASE 32
34 enum EventAI_Type
36 EVENT_T_TIMER = 0, // InitialMin, InitialMax, RepeatMin, RepeatMax
37 EVENT_T_TIMER_OOC = 1, // InitialMin, InitialMax, RepeatMin, RepeatMax
38 EVENT_T_HP = 2, // HPMax%, HPMin%, RepeatMin, RepeatMax
39 EVENT_T_MANA = 3, // ManaMax%,ManaMin% RepeatMin, RepeatMax
40 EVENT_T_AGGRO = 4, // NONE
41 EVENT_T_KILL = 5, // RepeatMin, RepeatMax
42 EVENT_T_DEATH = 6, // NONE
43 EVENT_T_EVADE = 7, // NONE
44 EVENT_T_SPELLHIT = 8, // SpellID, School, RepeatMin, RepeatMax
45 EVENT_T_RANGE = 9, // MinDist, MaxDist, RepeatMin, RepeatMax
46 EVENT_T_OOC_LOS = 10, // NoHostile, MaxRnage, RepeatMin, RepeatMax
47 EVENT_T_SPAWNED = 11, // Condition, CondValue1
48 EVENT_T_TARGET_HP = 12, // HPMax%, HPMin%, RepeatMin, RepeatMax
49 EVENT_T_TARGET_CASTING = 13, // RepeatMin, RepeatMax
50 EVENT_T_FRIENDLY_HP = 14, // HPDeficit, Radius, RepeatMin, RepeatMax
51 EVENT_T_FRIENDLY_IS_CC = 15, // DispelType, Radius, RepeatMin, RepeatMax
52 EVENT_T_FRIENDLY_MISSING_BUFF = 16, // SpellId, Radius, RepeatMin, RepeatMax
53 EVENT_T_SUMMONED_UNIT = 17, // CreatureId, RepeatMin, RepeatMax
54 EVENT_T_TARGET_MANA = 18, // ManaMax%, ManaMin%, RepeatMin, RepeatMax
55 EVENT_T_QUEST_ACCEPT = 19, // QuestID
56 EVENT_T_QUEST_COMPLETE = 20, //
57 EVENT_T_REACHED_HOME = 21, // NONE
58 EVENT_T_RECEIVE_EMOTE = 22, // EmoteId, Condition, CondValue1, CondValue2
60 EVENT_T_END,
63 enum EventAI_ActionType
65 ACTION_T_NONE = 0, // No action
66 ACTION_T_TEXT = 1, // TextId1, optionally -TextId2, optionally -TextId3(if -TextId2 exist). If more than just -TextId1 is defined, randomize. Negative values.
67 ACTION_T_SET_FACTION = 2, // FactionId (or 0 for default)
68 ACTION_T_MORPH_TO_ENTRY_OR_MODEL = 3, // Creature_template entry(param1) OR ModelId (param2) (or 0 for both to demorph)
69 ACTION_T_SOUND = 4, // SoundId
70 ACTION_T_EMOTE = 5, // EmoteId
71 ACTION_T_RANDOM_SAY = 6, // UNUSED
72 ACTION_T_RANDOM_YELL = 7, // UNUSED
73 ACTION_T_RANDOM_TEXTEMOTE = 8, // UNUSED
74 ACTION_T_RANDOM_SOUND = 9, // SoundId1, SoundId2, SoundId3 (-1 in any field means no output if randomed that field)
75 ACTION_T_RANDOM_EMOTE = 10, // EmoteId1, EmoteId2, EmoteId3 (-1 in any field means no output if randomed that field)
76 ACTION_T_CAST = 11, // SpellId, Target, CastFlags
77 ACTION_T_SUMMON = 12, // CreatureID, Target, Duration in ms
78 ACTION_T_THREAT_SINGLE_PCT = 13, // Threat%, Target
79 ACTION_T_THREAT_ALL_PCT = 14, // Threat%
80 ACTION_T_QUEST_EVENT = 15, // QuestID, Target
81 ACTION_T_CAST_EVENT = 16, // QuestID, SpellId, Target - must be removed as hack?
82 ACTION_T_SET_UNIT_FIELD = 17, // Field_Number, Value, Target
83 ACTION_T_SET_UNIT_FLAG = 18, // Flags (may be more than one field OR'd together), Target
84 ACTION_T_REMOVE_UNIT_FLAG = 19, // Flags (may be more than one field OR'd together), Target
85 ACTION_T_AUTO_ATTACK = 20, // AllowAttackState (0 = stop attack, anything else means continue attacking)
86 ACTION_T_COMBAT_MOVEMENT = 21, // AllowCombatMovement (0 = stop combat based movement, anything else continue attacking)
87 ACTION_T_SET_PHASE = 22, // Phase
88 ACTION_T_INC_PHASE = 23, // Value (may be negative to decrement phase, should not be 0)
89 ACTION_T_EVADE = 24, // No Params
90 ACTION_T_FLEE_FOR_ASSIST = 25, // No Params
91 ACTION_T_QUEST_EVENT_ALL = 26, // QuestID
92 ACTION_T_CAST_EVENT_ALL = 27, // CreatureId, SpellId
93 ACTION_T_REMOVEAURASFROMSPELL = 28, // Target, Spellid
94 ACTION_T_RANGED_MOVEMENT = 29, // Distance, Angle
95 ACTION_T_RANDOM_PHASE = 30, // PhaseId1, PhaseId2, PhaseId3
96 ACTION_T_RANDOM_PHASE_RANGE = 31, // PhaseMin, PhaseMax
97 ACTION_T_SUMMON_ID = 32, // CreatureId, Target, SpawnId
98 ACTION_T_KILLED_MONSTER = 33, // CreatureId, Target
99 ACTION_T_SET_INST_DATA = 34, // Field, Data
100 ACTION_T_SET_INST_DATA64 = 35, // Field, Target
101 ACTION_T_UPDATE_TEMPLATE = 36, // Entry, Team
102 ACTION_T_DIE = 37, // No Params
103 ACTION_T_ZONE_COMBAT_PULSE = 38, // No Params
104 ACTION_T_CALL_FOR_HELP = 39, // Radius
105 ACTION_T_SET_SHEATH = 40, // Sheath (0-passive,1-melee,2-ranged)
106 ACTION_T_END,
109 enum Target
111 //Self (m_creature)
112 TARGET_T_SELF = 0, //Self cast
114 //Hostile targets (if pet then returns pet owner)
115 TARGET_T_HOSTILE, //Our current target (ie: highest aggro)
116 TARGET_T_HOSTILE_SECOND_AGGRO, //Second highest aggro (generaly used for cleaves and some special attacks)
117 TARGET_T_HOSTILE_LAST_AGGRO, //Dead last on aggro (no idea what this could be used for)
118 TARGET_T_HOSTILE_RANDOM, //Just any random target on our threat list
119 TARGET_T_HOSTILE_RANDOM_NOT_TOP, //Any random target except top threat
121 //Invoker targets (if pet then returns pet owner)
122 TARGET_T_ACTION_INVOKER, //Unit who caused this Event to occur (only works for EVENT_T_AGGRO, EVENT_T_KILL, EVENT_T_DEATH, EVENT_T_SPELLHIT, EVENT_T_OOC_LOS, EVENT_T_FRIENDLY_HP, EVENT_T_FRIENDLY_IS_CC, EVENT_T_FRIENDLY_MISSING_BUFF)
124 //Hostile targets (including pets)
125 TARGET_T_HOSTILE_WPET, //Current target (can be a pet)
126 TARGET_T_HOSTILE_WPET_SECOND_AGGRO, //Second highest aggro (generaly used for cleaves and some special attacks)
127 TARGET_T_HOSTILE_WPET_LAST_AGGRO, //Dead last on aggro (no idea what this could be used for)
128 TARGET_T_HOSTILE_WPET_RANDOM, //Just any random target on our threat list
129 TARGET_T_HOSTILE_WPET_RANDOM_NOT_TOP, //Any random target except top threat
131 TARGET_T_ACTION_INVOKER_WPET,
133 TARGET_T_END
136 enum CastFlags
138 CAST_INTURRUPT_PREVIOUS = 0x01, //Interrupt any spell casting
139 CAST_TRIGGERED = 0x02, //Triggered (this makes spell cost zero mana and have no cast time)
140 CAST_FORCE_CAST = 0x04, //Forces cast even if creature is out of mana or out of range
141 CAST_NO_MELEE_IF_OOM = 0x08, //Prevents creature from entering melee if out of mana or out of range
142 CAST_FORCE_TARGET_SELF = 0x10, //Forces the target to cast this spell on itself
143 CAST_AURA_NOT_PRESENT = 0x20, //Only casts the spell if the target does not have an aura from the spell
146 enum EventFlags
148 EFLAG_REPEATABLE = 0x01, //Event repeats
149 EFLAG_NORMAL = 0x02, //Event only occurs in Normal instance difficulty
150 EFLAG_HEROIC = 0x04, //Event only occurs in Heroic instance difficulty
151 EFLAG_RESERVED_3 = 0x08,
152 EFLAG_RESERVED_4 = 0x10,
153 EFLAG_RESERVED_5 = 0x20,
154 EFLAG_RESERVED_6 = 0x40,
155 EFLAG_DEBUG_ONLY = 0x80, //Event only occurs in debug build
158 enum SpawnedEventMode
160 SPAWNED_EVENT_ALWAY = 0,
161 SPAWNED_EVENT_MAP = 1,
162 SPAWNED_EVENT_ZONE = 2
165 // String text additional data, used in (CreatureEventAI)
166 struct StringTextData
168 uint32 SoundId;
169 uint8 Type;
170 uint32 Language;
171 uint32 Emote;
173 // Text Maps
174 typedef UNORDERED_MAP<int32, StringTextData> CreatureEventAI_TextMap;
176 struct CreatureEventAI_Action
178 EventAI_ActionType type: 16;
179 union
181 // ACTION_T_TEXT = 1
182 struct
184 int32 TextId1;
185 int32 TextId2;
186 int32 TextId3;
187 } text;
188 // ACTION_T_SET_FACTION = 2
189 struct
191 uint32 factionId; // faction or 0 for default)
192 } set_faction;
193 // ACTION_T_MORPH_TO_ENTRY_OR_MODEL = 3
194 struct
196 uint32 creatureId; // set one from fields (or 0 for both to demorph)
197 uint32 modelId;
198 } morph;
199 // ACTION_T_SOUND = 4
200 struct
202 uint32 soundId;
203 } sound;
204 // ACTION_T_EMOTE = 5
205 struct
207 uint32 emoteId;
208 } emote;
209 // ACTION_T_RANDOM_SOUND = 9
210 struct
212 int32 soundId1; // (-1 in any field means no output if randomed that field)
213 int32 soundId2;
214 int32 soundId3;
215 } random_sound;
216 // ACTION_T_RANDOM_EMOTE = 10
217 struct
219 int32 emoteId1; // (-1 in any field means no output if randomed that field)
220 int32 emoteId2;
221 int32 emoteId3;
222 } random_emote;
223 // ACTION_T_CAST = 11
224 struct
226 uint32 spellId;
227 uint32 target;
228 uint32 castFlags;
229 } cast;
230 // ACTION_T_SUMMON = 12
231 struct
233 uint32 creatureId;
234 uint32 target;
235 uint32 duration;
236 } summon;
237 // ACTION_T_THREAT_SINGLE_PCT = 13
238 struct
240 int32 percent;
241 uint32 target;
242 } threat_single_pct;
243 // ACTION_T_THREAT_ALL_PCT = 14
244 struct
246 int32 percent;
247 } threat_all_pct;
248 // ACTION_T_QUEST_EVENT = 15
249 struct
251 uint32 questId;
252 uint32 target;
253 } quest_event;
254 // ACTION_T_CAST_EVENT = 16
255 struct
257 uint32 creatureId;
258 uint32 spellId;
259 uint32 target;
260 } cast_event;
261 // ACTION_T_SET_UNIT_FIELD = 17
262 struct
264 uint32 field;
265 uint32 value;
266 uint32 target;
267 } set_unit_field;
268 // ACTION_T_SET_UNIT_FLAG = 18, // value provided mask bits that will be set
269 // ACTION_T_REMOVE_UNIT_FLAG = 19, // value provided mask bits that will be clear
270 struct
272 uint32 value;
273 uint32 target;
274 } unit_flag;
275 // ACTION_T_AUTO_ATTACK = 20
276 struct
278 uint32 state; // 0 = stop attack, anything else means continue attacking
279 } auto_attack;
280 // ACTION_T_COMBAT_MOVEMENT = 21
281 struct
283 uint32 state; // 0 = stop combat based movement, anything else continue attacking
284 } combat_movement;
285 // ACTION_T_SET_PHASE = 22
286 struct
288 uint32 phase;
289 } set_phase;
290 // ACTION_T_INC_PHASE = 23
291 struct
293 int32 step;
294 } set_inc_phase;
295 // ACTION_T_QUEST_EVENT_ALL = 26
296 struct
298 uint32 questId;
299 } quest_event_all;
300 // ACTION_T_CAST_EVENT_ALL = 27
301 struct
303 uint32 creatureId;
304 uint32 spellId;
305 } cast_event_all;
306 // ACTION_T_REMOVEAURASFROMSPELL = 28
307 struct
309 uint32 target;
310 uint32 spellId;
311 } remove_aura;
312 // ACTION_T_RANGED_MOVEMENT = 29
313 struct
315 uint32 distance;
316 int32 angle;
317 } ranged_movement;
318 // ACTION_T_RANDOM_PHASE = 30
319 struct
321 uint32 phase1;
322 uint32 phase2;
323 uint32 phase3;
324 } random_phase;
325 // ACTION_T_RANDOM_PHASE_RANGE = 31
326 struct
328 uint32 phaseMin;
329 uint32 phaseMax;
330 } random_phase_range;
331 // ACTION_T_SUMMON_ID = 32
332 struct
334 uint32 creatureId;
335 uint32 target;
336 uint32 spawnId;
337 } summon_id;
338 // ACTION_T_KILLED_MONSTER = 33
339 struct
341 uint32 creatureId;
342 uint32 target;
343 } killed_monster;
344 // ACTION_T_SET_INST_DATA = 34
345 struct
347 uint32 field;
348 uint32 value;
349 } set_inst_data;
350 // ACTION_T_SET_INST_DATA64 = 35
351 struct
353 uint32 field;
354 uint32 target;
355 } set_inst_data64;
356 // ACTION_T_UPDATE_TEMPLATE = 36
357 struct
359 uint32 creatureId;
360 uint32 team;
361 } update_template;
362 // ACTION_T_CALL_FOR_HELP = 39
363 struct
365 uint32 radius;
366 } call_for_help;
367 // ACTION_T_SET_SHEATH = 40
368 struct
370 uint32 sheath;
371 } set_sheath;
372 // RAW
373 struct
375 uint32 param1;
376 uint32 param2;
377 uint32 param3;
378 } raw;
382 struct CreatureEventAI_Event
384 uint32 event_id;
386 uint32 creature_id;
388 uint32 event_inverse_phase_mask;
390 EventAI_Type event_type : 16;
391 uint8 event_chance : 8;
392 uint8 event_flags : 8;
394 union
396 // EVENT_T_TIMER = 0
397 // EVENT_T_TIMER_OOC = 1
398 struct
400 uint32 initialMin;
401 uint32 initialMax;
402 uint32 repeatMin;
403 uint32 repeatMax;
404 } timer;
405 // EVENT_T_HP = 2
406 // EVENT_T_MANA = 3
407 // EVENT_T_TARGET_HP = 12
408 // EVENT_T_TARGET_MANA = 18
409 struct
411 uint32 percentMax;
412 uint32 percentMin;
413 uint32 repeatMin;
414 uint32 repeatMax;
415 } percent_range;
416 // EVENT_T_KILL = 5
417 struct
419 uint32 repeatMin;
420 uint32 repeatMax;
421 } kill;
422 // EVENT_T_SPELLHIT = 8
423 struct
425 uint32 spellId;
426 uint32 schoolMask; // -1 (==0xffffffff) is ok value for full mask, or must be more limited mask like (0 < 1) = 1 for normal/physical school
427 uint32 repeatMin;
428 uint32 repeatMax;
429 } spell_hit;
430 // EVENT_T_RANGE = 9
431 struct
433 uint32 minDist;
434 uint32 maxDist;
435 uint32 repeatMin;
436 uint32 repeatMax;
437 } range;
438 // EVENT_T_OOC_LOS = 10
439 struct
441 uint32 noHostile;
442 uint32 maxRange;
443 uint32 repeatMin;
444 uint32 repeatMax;
445 } ooc_los;
446 // EVENT_T_SPAWNED = 11
447 struct
449 uint32 condition;
450 uint32 conditionValue1;
451 } spawned;
452 // EVENT_T_TARGET_CASTING = 13
453 struct
455 uint32 repeatMin;
456 uint32 repeatMax;
457 } target_casting;
458 // EVENT_T_FRIENDLY_HP = 14
459 struct
461 uint32 hpDeficit;
462 uint32 radius;
463 uint32 repeatMin;
464 uint32 repeatMax;
465 } friendly_hp;
466 // EVENT_T_FRIENDLY_IS_CC = 15
467 struct
469 uint32 dispelType; // unused ?
470 uint32 radius;
471 uint32 repeatMin;
472 uint32 repeatMax;
473 } friendly_is_cc;
474 // EVENT_T_FRIENDLY_MISSING_BUFF = 16
475 struct
477 uint32 spellId;
478 uint32 radius;
479 uint32 repeatMin;
480 uint32 repeatMax;
481 } friendly_buff;
482 // EVENT_T_SUMMONED_UNIT = 17
483 struct
485 uint32 creatureId;
486 uint32 repeatMin;
487 uint32 repeatMax;
488 } summon_unit;
489 // EVENT_T_QUEST_ACCEPT = 19
490 // EVENT_T_QUEST_COMPLETE = 20
491 struct
493 uint32 questId;
494 } quest;
495 // EVENT_T_RECEIVE_EMOTE = 22
496 struct
498 uint32 emoteId;
499 uint32 condition;
500 uint32 conditionValue1;
501 uint32 conditionValue2;
502 } receive_emote;
504 // RAW
505 struct
507 uint32 param1;
508 uint32 param2;
509 uint32 param3;
510 uint32 param4;
511 } raw;
514 CreatureEventAI_Action action[MAX_ACTIONS];
516 //Event_Map
517 typedef UNORDERED_MAP<uint32, std::vector<CreatureEventAI_Event> > CreatureEventAI_Event_Map;
519 struct CreatureEventAI_Summon
521 uint32 id;
523 float position_x;
524 float position_y;
525 float position_z;
526 float orientation;
527 uint32 SpawnTimeSecs;
530 //EventSummon_Map
531 typedef UNORDERED_MAP<uint32, CreatureEventAI_Summon> CreatureEventAI_Summon_Map;
533 struct CreatureEventAIHolder
535 CreatureEventAIHolder(CreatureEventAI_Event p) : Event(p), Time(0), Enabled(true){}
537 CreatureEventAI_Event Event;
538 uint32 Time;
539 bool Enabled;
541 // helper
542 bool UpdateRepeatTimer(Creature* creature, uint32 repeatMin, uint32 repeatMax);
545 class MANGOS_DLL_SPEC CreatureEventAI : public CreatureAI
548 public:
549 explicit CreatureEventAI(Creature *c);
550 ~CreatureEventAI()
552 CreatureEventAIList.clear();
554 void JustRespawned();
555 void Reset();
556 void JustReachedHome();
557 void EnterCombat(Unit *enemy);
558 void EnterEvadeMode();
559 void JustDied(Unit* killer);
560 void KilledUnit(Unit* victim);
561 void JustSummoned(Creature* pUnit);
562 void AttackStart(Unit *who);
563 void MoveInLineOfSight(Unit *who);
564 void SpellHit(Unit* pUnit, const SpellEntry* pSpell);
565 void UpdateAI(const uint32 diff);
566 bool IsVisible(Unit *) const;
567 void ReceiveEmote(Player* pPlayer, uint32 text_emote);
568 static int Permissible(const Creature *);
570 bool ProcessEvent(CreatureEventAIHolder& pHolder, Unit* pActionInvoker = NULL);
571 void ProcessAction(CreatureEventAI_Action const& action, uint32 rnd, uint32 EventId, Unit* pActionInvoker);
572 inline uint32 GetRandActionParam(uint32 rnd, uint32 param1, uint32 param2, uint32 param3);
573 inline int32 GetRandActionParam(uint32 rnd, int32 param1, int32 param2, int32 param3);
574 inline Unit* GetTargetByType(uint32 Target, Unit* pActionInvoker);
575 inline Unit* SelectUnit(AttackingTarget target, uint32 position);
577 void DoScriptText(int32 textEntry, WorldObject* pSource, Unit* target);
578 void DoZoneInCombat(Unit* pUnit);
579 void DoMeleeAttackIfReady();
580 bool CanCast(Unit* Target, SpellEntry const *Spell, bool Triggered);
582 bool SpawnedEventConditionsCheck(CreatureEventAI_Event const& event);
584 Unit* DoSelectLowestHpFriendly(float range, uint32 MinHPDiff);
585 void DoFindFriendlyMissingBuff(std::list<Creature*>& _list, float range, uint32 spellid);
586 void DoFindFriendlyCC(std::list<Creature*>& _list, float range);
588 //Holder for events (stores enabled, time, and eventid)
589 std::list<CreatureEventAIHolder> CreatureEventAIList;
590 uint32 EventUpdateTime; //Time between event updates
591 uint32 EventDiff; //Time between the last event call
592 bool bEmptyList;
594 //Variables used by Events themselves
595 uint8 Phase; //Current phase, max 32 phases
596 bool CombatMovementEnabled; //If we allow targeted movment gen (movement twoards top threat)
597 bool MeleeEnabled; //If we allow melee auto attack
598 uint32 AttackDistance; //Distance to attack from
599 float AttackAngle; //Angle of attack
601 #endif