Just a few renames.
[AHbot.git] / src / game / CreatureEventAI.cpp
blob49d5634d4958795b581b60320dcb6b53bd0778f0
1 /*
2 * Copyright (C) 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 #include "Common.h"
20 #include "CreatureEventAI.h"
21 #include "CreatureEventAIMgr.h"
22 #include "ObjectMgr.h"
23 #include "Spell.h"
24 #include "World.h"
25 #include "Cell.h"
26 #include "CellImpl.h"
27 #include "GameEventMgr.h"
28 #include "GridNotifiers.h"
29 #include "GridNotifiersImpl.h"
30 #include "WorldPacket.h"
31 #include "InstanceData.h"
33 int CreatureEventAI::Permissible(const Creature *creature)
35 if( creature->GetAIName() == "EventAI" )
36 return PERMIT_BASE_SPECIAL;
37 return PERMIT_BASE_NO;
40 CreatureEventAI::CreatureEventAI(Creature *c ) : CreatureAI(c)
42 CreatureEventAI_Event_Map::const_iterator CreatureEvents = CreatureEAI_Mgr.GetCreatureEventAIMap().find(m_creature->GetEntry());
43 if (CreatureEvents != CreatureEAI_Mgr.GetCreatureEventAIMap().end())
45 std::vector<CreatureEventAI_Event>::const_iterator i;
46 for (i = (*CreatureEvents).second.begin(); i != (*CreatureEvents).second.end(); ++i)
49 //Debug check
50 #ifndef MANGOS_DEBUG
51 if ((*i).event_flags & EFLAG_DEBUG_ONLY)
52 continue;
53 #endif
54 if (m_creature->GetMap()->IsDungeon())
56 if( (m_creature->GetMap()->IsHeroic() && (*i).event_flags & EFLAG_HEROIC) ||
57 (!m_creature->GetMap()->IsHeroic() && (*i).event_flags & EFLAG_NORMAL))
59 //event flagged for instance mode
60 CreatureEventAIList.push_back(CreatureEventAIHolder(*i));
62 continue;
64 CreatureEventAIList.push_back(CreatureEventAIHolder(*i));
66 //EventMap had events but they were not added because they must be for instance
67 if (CreatureEventAIList.empty())
68 sLog.outError("CreatureEventAI: Creature %u has events but no events added to list because of instance flags.", m_creature->GetEntry());
70 else
71 sLog.outError("CreatureEventAI: EventMap for Creature %u is empty but creature is using CreatureEventAI.", m_creature->GetEntry());
73 bEmptyList = CreatureEventAIList.empty();
74 Phase = 0;
75 CombatMovementEnabled = true;
76 MeleeEnabled = true;
77 AttackDistance = 0;
78 AttackAngle = 0.0f;
80 //Handle Spawned Events
81 if (!bEmptyList)
83 for (std::list<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
85 if ((*i).Event.event_type == EVENT_T_SPAWNED)
86 ProcessEvent(*i);
89 Reset();
92 bool CreatureEventAI::ProcessEvent(CreatureEventAIHolder& pHolder, Unit* pActionInvoker)
94 if (!pHolder.Enabled || pHolder.Time)
95 return false;
97 //Check the inverse phase mask (event doesn't trigger if current phase bit is set in mask)
98 if (pHolder.Event.event_inverse_phase_mask & (1 << Phase))
99 return false;
101 //Store random here so that all random actions match up
102 uint32 rnd = rand();
104 //Return if chance for event is not met
105 if (pHolder.Event.event_chance <= rnd % 100)
106 return false;
108 uint32 param1 = pHolder.Event.event_param1;
109 uint32 param2 = pHolder.Event.event_param2;
110 uint32 param3 = pHolder.Event.event_param3;
111 uint32 param4 = pHolder.Event.event_param4;
113 //Check event conditions based on the event type, also reset events
114 switch (pHolder.Event.event_type)
116 case EVENT_T_TIMER:
118 if (!m_creature->isInCombat())
119 return false;
121 //Repeat Timers
122 if (param3 == param4)
124 pHolder.Time = param3;
126 }else if (param4 > param3)
127 pHolder.Time = urand(param3, param4);
128 else
130 sLog.outErrorDb("CreatureEventAI: Creature %u using Event %u (Type = %u) has RandomMax < RandomMin. Event repeating disabled.", m_creature->GetEntry(), pHolder.Event.event_id, pHolder.Event.event_type);
131 pHolder.Enabled = false;
134 break;
135 case EVENT_T_TIMER_OOC:
137 if (m_creature->isInCombat())
138 return false;
140 //Repeat Timers
141 if (param3 == param4)
143 pHolder.Time = param3;
145 }else if (param4 > param3)
146 pHolder.Time = urand(param3, param4);
147 else
150 sLog.outErrorDb("CreatureEventAI: Creature %u using Event %u (Type = %u) has RandomMax < RandomMin. Event repeating disabled.", m_creature->GetEntry(), pHolder.Event.event_id, pHolder.Event.event_type);
151 pHolder.Enabled = false;
154 break;
155 case EVENT_T_HP:
157 if (!m_creature->isInCombat() || !m_creature->GetMaxHealth())
158 return false;
160 uint32 perc = (m_creature->GetHealth()*100) / m_creature->GetMaxHealth();
162 if (perc > param1 || perc < param2)
163 return false;
165 //Repeat Timers
166 if (param3 == param4)
168 pHolder.Time = param3;
170 }else if (param4 > param3)
171 pHolder.Time = urand(param3, param4);
172 else
175 sLog.outErrorDb("CreatureEventAI: Creature %u using Event %u (Type = %u) has RandomMax < RandomMin. Event repeating disabled.", m_creature->GetEntry(), pHolder.Event.event_id, pHolder.Event.event_type);
176 pHolder.Enabled = false;
179 break;
180 case EVENT_T_MANA:
182 if (!m_creature->isInCombat() || !m_creature->GetMaxPower(POWER_MANA))
183 return false;
185 uint32 perc = (m_creature->GetPower(POWER_MANA)*100) / m_creature->GetMaxPower(POWER_MANA);
187 if (perc > param1 || perc < param2)
188 return false;
190 //Repeat Timers
191 if (param3 == param4)
193 pHolder.Time = param3;
195 }else if (param4 > param3)
196 pHolder.Time = urand(param3, param4);
197 else
200 sLog.outErrorDb("CreatureEventAI: Creature %u using Event %u (Type = %u) has RandomMax < RandomMin. Event repeating disabled.", m_creature->GetEntry(), pHolder.Event.event_id, pHolder.Event.event_type);
201 pHolder.Enabled = false;
204 break;
205 case EVENT_T_AGGRO:
208 break;
209 case EVENT_T_KILL:
211 //Repeat Timers
212 if (param1 == param2)
214 pHolder.Time = param1;
216 }else if (param2 > param1)
217 pHolder.Time = urand(param1, param2);
218 else
221 sLog.outErrorDb("CreatureEventAI: Creature %u using Event %u (Type = %u) has RandomMax < RandomMin. Event repeating disabled.", m_creature->GetEntry(), pHolder.Event.event_id, pHolder.Event.event_type);
222 pHolder.Enabled = false;
225 case EVENT_T_DEATH:
228 break;
229 case EVENT_T_EVADE:
232 break;
233 case EVENT_T_SPELLHIT:
235 //Spell hit is special case, param1 and param2 handled within CreatureEventAI::SpellHit
237 //Repeat Timers
238 if (param3 == param4)
240 pHolder.Time = param3;
242 }else if (param4 > param3)
243 pHolder.Time = urand(param3, param4);
244 else
247 sLog.outErrorDb("CreatureEventAI: Creature %u using Event %u (Type = %u) has RandomMax < RandomMin. Event repeating disabled.", m_creature->GetEntry(), pHolder.Event.event_id, pHolder.Event.event_type);
248 pHolder.Enabled = false;
251 break;
252 case EVENT_T_RANGE:
254 //Repeat Timers
255 if (param3 == param4)
257 pHolder.Time = param3;
259 }else if (param4 > param3)
260 pHolder.Time = urand(param3, param4);
261 else
264 sLog.outErrorDb("CreatureEventAI: Creature %u using Event %u (Type = %u) has RandomMax < RandomMin. Event repeating disabled.", m_creature->GetEntry(), pHolder.Event.event_id, pHolder.Event.event_type);
265 pHolder.Enabled = false;
268 break;
269 case EVENT_T_OOC_LOS:
271 //Repeat Timers
272 if (param3 == param4)
274 pHolder.Time = param3;
276 }else if (param4 > param3)
277 pHolder.Time = urand(param3, param4);
278 else
281 sLog.outErrorDb("CreatureEventAI: Creature %u using Event %u (Type = %u) has RandomMax < RandomMin. Event repeating disabled.", m_creature->GetEntry(), pHolder.Event.event_id, pHolder.Event.event_type);
282 pHolder.Enabled = false;
285 break;
286 case EVENT_T_SPAWNED:
289 break;
290 case EVENT_T_TARGET_HP:
292 if (!m_creature->isInCombat() || !m_creature->getVictim() || !m_creature->getVictim()->GetMaxHealth())
293 return false;
295 uint32 perc = (m_creature->getVictim()->GetHealth()*100) / m_creature->getVictim()->GetMaxHealth();
297 if (perc > param1 || perc < param2)
298 return false;
300 //Repeat Timers
301 if (param3 == param4)
303 pHolder.Time = param3;
305 }else if (param4 > param3)
306 pHolder.Time = urand(param3, param4);
307 else
310 sLog.outErrorDb("CreatureEventAI: Creature %u using Event %u (Type = %u) has RandomMax < RandomMin. Event repeating disabled.", m_creature->GetEntry(), pHolder.Event.event_id, pHolder.Event.event_type);
311 pHolder.Enabled = false;
314 break;
315 case EVENT_T_TARGET_CASTING:
317 if (!m_creature->isInCombat() || !m_creature->getVictim() || !m_creature->getVictim()->IsNonMeleeSpellCasted(false, false, true))
318 return false;
320 //Repeat Timers
321 if (param1 == param2)
323 pHolder.Time = param1;
325 }else if (param2 > param1)
326 pHolder.Time = urand(param1, param2);
327 else
330 sLog.outErrorDb("CreatureEventAI: Creature %u using Event %u (Type = %u) has RandomMax < RandomMin. Event repeating disabled.", m_creature->GetEntry(), pHolder.Event.event_id, pHolder.Event.event_type);
331 pHolder.Enabled = false;
334 break;
335 case EVENT_T_FRIENDLY_HP:
337 if (!m_creature->isInCombat())
338 return false;
340 Unit* pUnit = DoSelectLowestHpFriendly(param2, param1);
342 if (!pUnit)
343 return false;
345 pActionInvoker = pUnit;
347 //Repeat Timers
348 if (param3 == param4)
350 pHolder.Time = param3;
352 }else if (param4 > param3)
353 pHolder.Time = urand(param3, param4);
354 else
357 sLog.outErrorDb("CreatureEventAI: Creature %u using Event %u (Type = %u) has RandomMax < RandomMin. Event repeating disabled.", m_creature->GetEntry(), pHolder.Event.event_id, pHolder.Event.event_type);
358 pHolder.Enabled = false;
361 break;
362 case EVENT_T_FRIENDLY_IS_CC:
364 if (!m_creature->isInCombat())
365 return false;
367 std::list<Creature*> pList;
368 DoFindFriendlyCC(pList, param2);
370 //List is empty
371 if (pList.empty())
372 return false;
374 //We don't really care about the whole list, just return first available
375 pActionInvoker = *(pList.begin());
377 //Repeat Timers
378 if (param3 == param4)
380 pHolder.Time = param3;
382 }else if (param4 > param3)
383 pHolder.Time = urand(param3, param4);
384 else
386 sLog.outErrorDb("CreatureEventAI: Creature %u using Event %u (Type = %u) has RandomMax < RandomMin. Event repeating disabled.", m_creature->GetEntry(), pHolder.Event.event_id, pHolder.Event.event_type);
387 pHolder.Enabled = false;
390 break;
391 case EVENT_T_FRIENDLY_MISSING_BUFF:
393 std::list<Creature*> pList;
394 DoFindFriendlyMissingBuff(pList, param2, param1);
396 //List is empty
397 if (pList.empty())
398 return false;
400 //We don't really care about the whole list, just return first available
401 pActionInvoker = *(pList.begin());
403 //Repeat Timers
404 if (param3 == param4)
406 pHolder.Time = param3;
408 }else if (param4 > param3)
409 pHolder.Time = urand(param3, param4);
410 else
413 sLog.outErrorDb("CreatureEventAI: Creature %u using Event %u (Type = %u) has RandomMax < RandomMin. Event repeating disabled.", m_creature->GetEntry(), pHolder.Event.event_id, pHolder.Event.event_type);
414 pHolder.Enabled = false;
417 break;
418 case EVENT_T_SUMMONED_UNIT:
420 //Prevent event from occuring on no unit or non creatures
421 if (!pActionInvoker || pActionInvoker->GetTypeId()!=TYPEID_UNIT)
422 return false;
424 //Creature id doesn't match up
425 if (param1 && ((Creature*)pActionInvoker)->GetEntry() != param1)
426 return false;
428 //Repeat Timers
429 if (param2 == param3)
431 pHolder.Time = param2;
433 }else if (param3 > param2)
434 pHolder.Time = urand(param2, param3);
435 else
438 sLog.outErrorDb("CreatureEventAI: Creature %u using Event %u (Type = %u) has RandomMax < RandomMin. Event repeating disabled.", m_creature->GetEntry(), pHolder.Event.event_id, pHolder.Event.event_type);
439 pHolder.Enabled = false;
442 break;
443 case EVENT_T_REACHED_HOME:
446 break;
447 case EVENT_T_RECEIVE_EMOTE:
450 break;
451 default:
453 sLog.outErrorDb("CreatureEventAI: Creature %u using Event %u has invalid Event Type(%u), missing from ProcessEvent() Switch.", m_creature->GetEntry(), pHolder.Event.event_id, pHolder.Event.event_type);
454 break;
457 //Disable non-repeatable events
458 if (!(pHolder.Event.event_flags & EFLAG_REPEATABLE))
459 pHolder.Enabled = false;
461 //Process actions
462 for (uint32 j = 0; j < MAX_ACTIONS; j++)
463 ProcessAction(pHolder.Event.action[j].type, pHolder.Event.action[j].param1, pHolder.Event.action[j].param2, pHolder.Event.action[j].param3, rnd, pHolder.Event.event_id, pActionInvoker);
465 return true;
468 void CreatureEventAI::ProcessAction(uint16 type, uint32 param1, uint32 param2, uint32 param3, uint32 rnd, uint32 EventId, Unit* pActionInvoker)
470 switch (type)
472 case ACTION_T_TEXT:
474 if (!param1)
475 return;
477 uint32 temp = 0;
479 if (param2 && param3)
481 switch( rand()%3 )
483 case 0: temp = param1; break;
484 case 2: temp = param2; break;
485 case 3: temp = param3; break;
487 }else if ( param2 && urand(0,1) )
489 temp = param2;
490 }else
492 temp = param1;
495 if (temp)
497 Unit* target = NULL;
499 if (pActionInvoker)
501 if (pActionInvoker->GetTypeId() == TYPEID_PLAYER)
502 target = pActionInvoker;
503 else if (Unit* owner = pActionInvoker->GetOwner())
505 if (owner->GetTypeId() == TYPEID_PLAYER)
506 target = owner;
509 else if (target = m_creature->getVictim())
511 if (target->GetTypeId() != TYPEID_PLAYER)
512 if (Unit* owner = target->GetOwner())
513 if (owner->GetTypeId() == TYPEID_PLAYER)
514 target = owner;
517 DoScriptText(temp, m_creature, target);
520 break;
521 case ACTION_T_SET_FACTION:
523 if (param1)
524 m_creature->setFaction(param1);
525 else
527 if (CreatureInfo const* ci = GetCreatureTemplateStore(m_creature->GetEntry()))
529 //if no id provided, assume reset and then use default
530 if (m_creature->getFaction() != ci->faction_A)
531 m_creature->setFaction(ci->faction_A);
535 break;
536 case ACTION_T_MORPH_TO_ENTRY_OR_MODEL:
538 if (param1 || param2)
540 //set model based on entry from creature_template
541 if (param1)
543 if (CreatureInfo const* ci = GetCreatureTemplateStore(param1))
545 //use default display
546 if (ci->DisplayID_A)
547 m_creature->SetDisplayId(ci->DisplayID_A);
550 //if no param1, then use value from param2 (modelId)
551 else
552 m_creature->SetDisplayId(param2);
554 else
555 m_creature->DeMorph();
557 break;
558 case ACTION_T_SOUND:
559 m_creature->PlayDirectSound(param1);
560 break;
561 case ACTION_T_EMOTE:
562 m_creature->HandleEmoteCommand(param1);
563 break;
564 case ACTION_T_RANDOM_SOUND:
566 uint32 temp = GetRandActionParam(rnd, param1, param2, param3);
568 if (temp != uint32(0xffffffff))
569 m_creature->PlayDirectSound( temp );
571 break;
572 case ACTION_T_RANDOM_EMOTE:
574 uint32 temp = GetRandActionParam(rnd, param1, param2, param3);
576 if (temp != uint32(0xffffffff))
577 m_creature->HandleEmoteCommand(temp);
579 break;
580 case ACTION_T_CAST:
582 Unit* target = GetTargetByType(param2, pActionInvoker);
583 Unit* caster = m_creature;
585 if (!target)
586 return;
588 //Cast is always triggered if target is forced to cast on self
589 if (param3 & CAST_FORCE_TARGET_SELF)
591 param3 |= CAST_TRIGGERED;
592 caster = target;
595 //Allowed to cast only if not casting (unless we interrupt ourself) or if spell is triggered
596 bool canCast = !caster->IsNonMeleeSpellCasted(false) || (param3 & (CAST_TRIGGERED | CAST_INTURRUPT_PREVIOUS));
598 // If cast flag CAST_AURA_NOT_PRESENT is active, check if target already has aura on them
599 if(param3 & CAST_AURA_NOT_PRESENT)
601 for(uint8 i = 0; i < 3; ++i)
602 if(target->HasAura(param1, i))
603 return;
606 if (canCast)
608 const SpellEntry* tSpell = GetSpellStore()->LookupEntry(param1);
610 //Verify that spell exists
611 if (tSpell)
613 //Check if cannot cast spell
614 if (!(param3 & (CAST_FORCE_TARGET_SELF | CAST_FORCE_CAST)) &&
615 !CanCast(target, tSpell, (param3 & CAST_TRIGGERED)))
617 //Melee current victim if flag not set
618 if (!(param3 & CAST_NO_MELEE_IF_OOM))
620 if (m_creature->GetMotionMaster()->GetCurrentMovementGeneratorType() == TARGETED_MOTION_TYPE)
622 AttackDistance = 0;
623 AttackAngle = 0;
625 m_creature->GetMotionMaster()->Clear(false);
626 m_creature->GetMotionMaster()->MoveChase(m_creature->getVictim(), AttackDistance, AttackAngle);
631 else
633 //Interrupt any previous spell
634 if (caster->IsNonMeleeSpellCasted(false) && param3 & CAST_INTURRUPT_PREVIOUS)
635 caster->InterruptNonMeleeSpells(false);
637 caster->CastSpell(target, param1, (param3 & CAST_TRIGGERED));
640 }else
641 sLog.outErrorDb("CreatureEventAI: event %d creature %d attempt to cast spell that doesn't exist %d", EventId, m_creature->GetEntry(), param1);
644 break;
645 case ACTION_T_SUMMON:
647 Unit* target = GetTargetByType(param2, pActionInvoker);
649 Creature* pCreature = NULL;
651 if (param3)
652 pCreature = m_creature->SummonCreature(param1, 0.0f, 0.0f, 0.0f, 0.0f, TEMPSUMMON_TIMED_OR_DEAD_DESPAWN, param3);
653 else
654 pCreature = m_creature->SummonCreature(param1, 0.0f, 0.0f, 0.0f, 0.0f, TEMPSUMMON_TIMED_DESPAWN_OUT_OF_COMBAT, 0);
656 if (!pCreature)
659 sLog.outErrorDb( "CreatureEventAI: failed to spawn creature %u. Spawn event %d is on creature %d", param1, EventId, m_creature->GetEntry());
661 else if (param2 != TARGET_T_SELF && target)
662 pCreature->AI()->AttackStart(target);
664 break;
665 case ACTION_T_THREAT_SINGLE_PCT:
667 Unit* target = GetTargetByType(param2, pActionInvoker);
669 if (target)
670 m_creature->getThreatManager().modifyThreatPercent(target, param1);
672 break;
673 case ACTION_T_THREAT_ALL_PCT:
675 Unit* Temp = NULL;
677 std::list<HostilReference*>::iterator i = m_creature->getThreatManager().getThreatList().begin();
678 for (; i != m_creature->getThreatManager().getThreatList().end(); ++i)
680 Temp = Unit::GetUnit(*m_creature,(*i)->getUnitGuid());
681 if (Temp)
682 m_creature->getThreatManager().modifyThreatPercent(Temp, param1);
685 break;
686 case ACTION_T_QUEST_EVENT:
688 Unit* target = GetTargetByType(param2, pActionInvoker);
690 if (target && target->GetTypeId() == TYPEID_PLAYER)
691 ((Player*)target)->AreaExploredOrEventHappens(param1);
693 break;
694 case ACTION_T_CASTCREATUREGO:
696 Unit* target = GetTargetByType(param3, pActionInvoker);
698 if (target && target->GetTypeId() == TYPEID_PLAYER)
699 ((Player*)target)->CastedCreatureOrGO(param1, m_creature->GetGUID(), param2);
701 break;
702 case ACTION_T_SET_UNIT_FIELD:
704 Unit* target = GetTargetByType(param3, pActionInvoker);
706 if (param1 < OBJECT_END || param1 >= UNIT_END)
707 return;
709 if (target)
710 target->SetUInt32Value(param1, param2);
712 break;
713 case ACTION_T_SET_UNIT_FLAG:
715 Unit* target = GetTargetByType(param2, pActionInvoker);
717 if (target)
718 target->SetFlag(UNIT_FIELD_FLAGS, param1);
720 break;
721 case ACTION_T_REMOVE_UNIT_FLAG:
723 Unit* target = GetTargetByType(param2, pActionInvoker);
725 if (target)
726 target->RemoveFlag(UNIT_FIELD_FLAGS, param1);
728 break;
729 case ACTION_T_AUTO_ATTACK:
731 if (param1)
732 MeleeEnabled = true;
733 else MeleeEnabled = false;
735 break;
736 case ACTION_T_COMBAT_MOVEMENT:
738 CombatMovementEnabled = param1;
740 //Allow movement (create new targeted movement gen only if idle)
741 if (CombatMovementEnabled)
743 if (m_creature->GetMotionMaster()->GetCurrentMovementGeneratorType() == IDLE_MOTION_TYPE)
745 m_creature->GetMotionMaster()->Clear(false);
746 m_creature->GetMotionMaster()->MoveChase(m_creature->getVictim(), AttackDistance, AttackAngle);
749 else
750 if (m_creature->GetMotionMaster()->GetCurrentMovementGeneratorType() == TARGETED_MOTION_TYPE)
752 m_creature->GetMotionMaster()->Clear(false);
753 m_creature->GetMotionMaster()->MoveIdle();
754 m_creature->StopMoving();
757 break;
758 case ACTION_T_SET_PHASE:
760 Phase = param1;
762 break;
763 case ACTION_T_INC_PHASE:
765 Phase += param1;
767 if (Phase > 31)
769 sLog.outErrorDb( "CreatureEventAI: Event %d incremented Phase above 31. Phase mask cannot be used with phases past 31. CreatureEntry = %d", EventId, m_creature->GetEntry());
771 break;
772 case ACTION_T_EVADE:
774 EnterEvadeMode();
776 break;
777 case ACTION_T_FLEE:
779 //TODO: Replace with Flee movement generator
780 m_creature->CastSpell(m_creature, SPELL_RUN_AWAY, true);
782 break;
783 case ACTION_T_QUEST_EVENT_ALL:
785 Unit* Temp = NULL;
786 if( pActionInvoker && pActionInvoker->GetTypeId() == TYPEID_PLAYER )
788 Temp = Unit::GetUnit(*m_creature,pActionInvoker->GetGUID());
789 if( Temp )
790 ((Player*)Temp)->GroupEventHappens(param1,m_creature);
793 break;
794 case ACTION_T_CASTCREATUREGO_ALL:
796 Unit* Temp = NULL;
798 std::list<HostilReference*>::iterator i = m_creature->getThreatManager().getThreatList().begin();
799 for (; i != m_creature->getThreatManager().getThreatList().end(); ++i)
801 Temp = Unit::GetUnit(*m_creature,(*i)->getUnitGuid());
802 if (Temp && Temp->GetTypeId() == TYPEID_PLAYER)
803 ((Player*)Temp)->CastedCreatureOrGO(param1, m_creature->GetGUID(), param2);
806 break;
807 case ACTION_T_REMOVEAURASFROMSPELL:
809 Unit* target = GetTargetByType(param1, pActionInvoker);
811 if (target)
812 target->RemoveAurasDueToSpell(param2);
814 break;
815 case ACTION_T_RANGED_MOVEMENT:
817 AttackDistance = param1;
818 AttackAngle = ((float)param2/180)*M_PI;
820 if (CombatMovementEnabled)
822 if (m_creature->GetMotionMaster()->GetCurrentMovementGeneratorType() == TARGETED_MOTION_TYPE)
824 //Drop current movement gen
825 m_creature->GetMotionMaster()->Clear(false);
826 m_creature->GetMotionMaster()->MoveChase(m_creature->getVictim(), AttackDistance, AttackAngle);
830 break;
831 case ACTION_T_RANDOM_PHASE:
833 uint32 temp = GetRandActionParam(rnd, param1, param2, param3);
835 Phase = temp;
837 break;
838 case ACTION_T_RANDOM_PHASE_RANGE:
840 if (param2 > param1)
842 Phase = param1 + (rnd % (param2 - param1));
844 else
845 sLog.outErrorDb( "CreatureEventAI: ACTION_T_RANDOM_PHASE_RANGE cannot have Param2 <= Param1. Divide by Zero. Event = %d. CreatureEntry = %d", EventId, m_creature->GetEntry());
847 break;
848 case ACTION_T_SUMMON_ID:
850 Unit* target = GetTargetByType(param2, pActionInvoker);
852 //Duration
853 Creature* pCreature = NULL;
855 CreatureEventAI_Summon_Map::const_iterator i = CreatureEAI_Mgr.GetCreatureEventAISummonMap().find(param3);
856 if (i == CreatureEAI_Mgr.GetCreatureEventAISummonMap().end())
859 sLog.outErrorDb( "CreatureEventAI: failed to spawn creature %u. Summon map index %u does not exist. EventID %d. CreatureID %d", param1, param3, EventId, m_creature->GetEntry());
860 return;
863 if ((*i).second.SpawnTimeSecs)
864 pCreature = m_creature->SummonCreature(param1, (*i).second.position_x, (*i).second.position_y, (*i).second.position_z, (*i).second.orientation, TEMPSUMMON_TIMED_OR_DEAD_DESPAWN, (*i).second.SpawnTimeSecs);
865 else pCreature = m_creature->SummonCreature(param1, (*i).second.position_x, (*i).second.position_y, (*i).second.position_z, (*i).second.orientation, TEMPSUMMON_TIMED_DESPAWN_OUT_OF_COMBAT, 0);
867 if (!pCreature)
870 sLog.outErrorDb( "CreatureEventAI: failed to spawn creature %u. EventId %d.Creature %d", param1, EventId, m_creature->GetEntry());
872 else if (param2 != TARGET_T_SELF && target)
873 pCreature->AI()->AttackStart(target);
875 break;
876 case ACTION_T_KILLED_MONSTER:
878 //first attempt player who tapped creature
879 if (Player* pPlayer = m_creature->GetLootRecipient())
880 pPlayer->RewardPlayerAndGroupAtEvent(param1, m_creature);
881 else
883 //if not available, use pActionInvoker
884 if (Unit* pTarget = GetTargetByType(param2, pActionInvoker))
886 if (Player* pPlayer2 = pTarget->GetCharmerOrOwnerPlayerOrPlayerItself())
887 pPlayer2->RewardPlayerAndGroupAtEvent(param1, m_creature);
891 break;
892 case ACTION_T_SET_INST_DATA:
894 InstanceData* pInst = (InstanceData*)m_creature->GetInstanceData();
895 if (!pInst)
897 sLog.outErrorDb("CreatureEventAI: Event %d attempt to set instance data without instance script. Creature %d", EventId, m_creature->GetEntry());
898 return;
901 pInst->SetData(param1, param2);
903 break;
904 case ACTION_T_SET_INST_DATA64:
906 Unit* target = GetTargetByType(param2, pActionInvoker);
907 if (!target)
909 sLog.outErrorDb("CreatureEventAI: Event %d attempt to set instance data64 but Target == NULL. Creature %d", EventId, m_creature->GetEntry());
910 return;
913 InstanceData* pInst = (InstanceData*)m_creature->GetInstanceData();
914 if (!pInst)
916 sLog.outErrorDb("CreatureEventAI: Event %d attempt to set instance data64 without instance script. Creature %d", EventId, m_creature->GetEntry());
917 return;
920 pInst->SetData64(param1, target->GetGUID());
922 break;
923 case ACTION_T_UPDATE_TEMPLATE:
925 if (m_creature->GetEntry() == param1)
928 sLog.outErrorDb("CreatureEventAI: Event %d ACTION_T_UPDATE_TEMPLATE call with param1 == current entry. Creature %d", EventId, m_creature->GetEntry());
929 return;
932 m_creature->UpdateEntry(param1, param2 ? HORDE : ALLIANCE);
934 break;
935 case ACTION_T_DIE:
937 if (m_creature->isDead())
940 sLog.outErrorDb("CreatureEventAI: Event %d ACTION_T_DIE on dead creature. Creature %d", EventId, m_creature->GetEntry());
941 return;
943 m_creature->DealDamage(m_creature, m_creature->GetMaxHealth(),NULL, DIRECT_DAMAGE, SPELL_SCHOOL_MASK_NORMAL, NULL, false);
945 break;
946 case ACTION_T_ZONE_COMBAT_PULSE:
948 if (!m_creature->isInCombat() || !m_creature->GetMap()->IsDungeon())
951 sLog.outErrorDb("CreatureEventAI: Event %d ACTION_T_ZONE_COMBAT_PULSE on creature out of combat or in non-dungeon map. Creature %d", EventId, m_creature->GetEntry());
952 return;
955 DoZoneInCombat(m_creature);
957 break;
961 void CreatureEventAI::JustRespawned()
963 Reset();
965 if (bEmptyList)
966 return;
968 //Handle Spawned Events
969 for (std::list<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
971 if ((*i).Event.event_type == EVENT_T_SPAWNED)
972 ProcessEvent(*i);
976 void CreatureEventAI::Reset()
978 EventUpdateTime = EVENT_UPDATE_TIME;
979 EventDiff = 0;
981 if (bEmptyList)
982 return;
984 //Reset all events to enabled
985 for (std::list<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
987 switch ((*i).Event.event_type)
989 //Reset all out of combat timers
990 case EVENT_T_TIMER_OOC:
992 if ((*i).Event.event_param2 == (*i).Event.event_param1)
994 (*i).Time = (*i).Event.event_param1;
995 (*i).Enabled = true;
997 else if ((*i).Event.event_param2 > (*i).Event.event_param1)
999 (*i).Time = urand((*i).Event.event_param1, (*i).Event.event_param2);
1000 (*i).Enabled = true;
1002 else
1003 sLog.outErrorDb("CreatureEventAI: Creature %u using Event %u (Type = %u) has InitialMax < InitialMin. Event disabled.", m_creature->GetEntry(), (*i).Event.event_id, (*i).Event.event_type);
1005 break;
1006 //default:
1007 //TODO: enable below code line / verify this is correct to enable events previously disabled (ex. aggro yell), instead of enable this in void Aggro()
1008 //(*i).Enabled = true;
1009 //(*i).Time = 0;
1010 //break;
1015 void CreatureEventAI::JustReachedHome()
1017 m_creature->LoadCreaturesAddon();
1019 if (!bEmptyList)
1021 for (std::list<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
1023 if ((*i).Event.event_type == EVENT_T_REACHED_HOME)
1024 ProcessEvent(*i);
1028 Reset();
1031 void CreatureEventAI::EnterEvadeMode()
1033 m_creature->RemoveAllAuras();
1034 m_creature->DeleteThreatList();
1035 m_creature->CombatStop(true);
1037 if (m_creature->isAlive())
1038 m_creature->GetMotionMaster()->MoveTargetedHome();
1040 m_creature->SetLootRecipient(NULL);
1042 if (bEmptyList)
1043 return;
1045 //Handle Evade events
1046 for (std::list<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
1048 if ((*i).Event.event_type == EVENT_T_EVADE)
1049 ProcessEvent(*i);
1053 void CreatureEventAI::JustDied(Unit* killer)
1055 Reset();
1057 if (bEmptyList)
1058 return;
1060 //Handle Evade events
1061 for (std::list<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
1063 if ((*i).Event.event_type == EVENT_T_DEATH)
1064 ProcessEvent(*i, killer);
1068 void CreatureEventAI::KilledUnit(Unit* victim)
1070 if (bEmptyList || victim->GetTypeId() != TYPEID_PLAYER)
1071 return;
1073 for (std::list<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
1075 if ((*i).Event.event_type == EVENT_T_KILL)
1076 ProcessEvent(*i, victim);
1080 void CreatureEventAI::JustSummoned(Creature* pUnit)
1082 if (bEmptyList || !pUnit)
1083 return;
1085 for (std::list<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
1087 if ((*i).Event.event_type == EVENT_T_SUMMONED_UNIT)
1088 ProcessEvent(*i, pUnit);
1092 void CreatureEventAI::EnterCombat(Unit *enemy)
1094 //Check for on combat start events
1095 if (!bEmptyList)
1097 for (std::list<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
1099 switch ((*i).Event.event_type)
1101 case EVENT_T_AGGRO:
1102 (*i).Enabled = true;
1103 ProcessEvent(*i, enemy);
1104 break;
1105 //Reset all in combat timers
1106 case EVENT_T_TIMER:
1107 if ((*i).Event.event_param2 == (*i).Event.event_param1)
1109 (*i).Time = (*i).Event.event_param1;
1110 (*i).Enabled = true;
1112 else if ((*i).Event.event_param2 > (*i).Event.event_param1)
1114 (*i).Time = urand((*i).Event.event_param1, (*i).Event.event_param2);
1115 (*i).Enabled = true;
1117 else
1118 sLog.outErrorDb("CreatureEventAI: Creature %u using Event %u (Type = %u) has InitialMax < InitialMin. Event disabled.", m_creature->GetEntry(), (*i).Event.event_id, (*i).Event.event_type);
1119 break;
1120 //All normal events need to be re-enabled and their time set to 0
1121 default:
1122 (*i).Enabled = true;
1123 (*i).Time = 0;
1124 break;
1129 EventUpdateTime = EVENT_UPDATE_TIME;
1130 EventDiff = 0;
1133 void CreatureEventAI::AttackStart(Unit *who)
1135 if (!who)
1136 return;
1138 if (m_creature->Attack(who, MeleeEnabled))
1140 m_creature->AddThreat(who, 0.0f);
1141 m_creature->SetInCombatWith(who);
1142 who->SetInCombatWith(m_creature);
1144 if (CombatMovementEnabled)
1146 m_creature->GetMotionMaster()->MoveChase(who, AttackDistance, AttackAngle);
1148 else
1150 m_creature->GetMotionMaster()->MoveIdle();
1151 m_creature->StopMoving();
1156 void CreatureEventAI::MoveInLineOfSight(Unit *who)
1158 if (!who)
1159 return;
1161 //Check for OOC LOS Event
1162 if (!bEmptyList && !m_creature->getVictim())
1164 for (std::list<CreatureEventAIHolder>::iterator itr = CreatureEventAIList.begin(); itr != CreatureEventAIList.end(); ++itr)
1166 if ((*itr).Event.event_type == EVENT_T_OOC_LOS)
1168 //can trigger if closer than fMaxAllowedRange
1169 float fMaxAllowedRange = (*itr).Event.event_param2;
1171 //if range is ok and we are actually in LOS
1172 if (m_creature->IsWithinDistInMap(who, fMaxAllowedRange) && m_creature->IsWithinLOSInMap(who))
1174 //if friendly event&&who is not hostile OR hostile event&&who is hostile
1175 if (((*itr).Event.event_param1 && !m_creature->IsHostileTo(who)) ||
1176 ((!(*itr).Event.event_param1) && m_creature->IsHostileTo(who)))
1177 ProcessEvent(*itr, who);
1183 if (m_creature->isCivilian() || m_creature->IsNeutralToAll())
1184 return;
1186 if (!m_creature->hasUnitState(UNIT_STAT_STUNNED) && who->isTargetableForAttack() &&
1187 m_creature->IsHostileTo(who) && who->isInAccessablePlaceFor(m_creature))
1189 if (!m_creature->canFly() && m_creature->GetDistanceZ(who) > CREATURE_Z_ATTACK_RANGE)
1190 return;
1192 float attackRadius = m_creature->GetAttackDistance(who);
1193 if (m_creature->IsWithinDistInMap(who, attackRadius) && m_creature->IsWithinLOSInMap(who))
1195 if (!m_creature->getVictim())
1197 AttackStart(who);
1198 who->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH);
1200 else if (m_creature->GetMap()->IsDungeon())
1202 m_creature->AddThreat(who, 0.0f);
1203 who->SetInCombatWith(m_creature);
1209 void CreatureEventAI::SpellHit(Unit* pUnit, const SpellEntry* pSpell)
1212 if (bEmptyList)
1213 return;
1215 for (std::list<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
1217 if ((*i).Event.event_type == EVENT_T_SPELLHIT)
1219 //If spell id matches (or no spell id) & if spell school matches (or no spell school)
1220 if (!(*i).Event.event_param1 || pSpell->Id == (*i).Event.event_param1)
1222 if ((*i).Event.event_param2_s == -1 || pSpell->SchoolMask == (*i).Event.event_param2)
1223 ProcessEvent(*i, pUnit);
1229 void CreatureEventAI::UpdateAI(const uint32 diff)
1231 //Check if we are in combat (also updates calls threat update code)
1232 bool Combat = m_creature->SelectHostilTarget() && m_creature->getVictim();
1234 //Must return if creature isn't alive. Normally select hostil target and get victim prevent this
1235 if (!m_creature->isAlive())
1236 return;
1238 if (!bEmptyList)
1240 //Events are only updated once every EVENT_UPDATE_TIME ms to prevent lag with large amount of events
1241 if (EventUpdateTime < diff)
1243 EventDiff += diff;
1245 //Check for time based events
1246 for (std::list<CreatureEventAIHolder>::iterator i = CreatureEventAIList.begin(); i != CreatureEventAIList.end(); ++i)
1248 //Decrement Timers
1249 if ((*i).Time)
1251 if ((*i).Time > EventDiff)
1253 //Do not decrement timers if event cannot trigger in this phase
1254 if (!((*i).Event.event_inverse_phase_mask & (1 << Phase)))
1255 (*i).Time -= EventDiff;
1257 //Skip processing of events that have time remaining
1258 continue;
1260 else (*i).Time = 0;
1263 //Events that are updated every EVENT_UPDATE_TIME
1264 switch ((*i).Event.event_type)
1266 case EVENT_T_TIMER_OOC:
1267 ProcessEvent(*i);
1268 break;
1269 case EVENT_T_TIMER:
1270 case EVENT_T_MANA:
1271 case EVENT_T_HP:
1272 case EVENT_T_TARGET_HP:
1273 case EVENT_T_TARGET_CASTING:
1274 case EVENT_T_FRIENDLY_HP:
1275 if (Combat)
1276 ProcessEvent(*i);
1277 break;
1278 case EVENT_T_RANGE:
1279 if (Combat)
1281 if (m_creature->IsWithinDistInMap(m_creature->getVictim(),(float)(*i).Event.event_param2))
1283 if (m_creature->GetDistance(m_creature->getVictim()) >= (float)(*i).Event.event_param1)
1284 ProcessEvent(*i);
1287 break;
1291 EventDiff = 0;
1292 EventUpdateTime = EVENT_UPDATE_TIME;
1294 else
1296 EventDiff += diff;
1297 EventUpdateTime -= diff;
1301 //Melee Auto-Attack
1302 if (Combat && MeleeEnabled)
1303 DoMeleeAttackIfReady();
1306 bool CreatureEventAI::IsVisible(Unit *pl) const
1308 return m_creature->GetDistance(pl) < sWorld.getConfig(CONFIG_SIGHT_MONSTER)
1309 && pl->isVisibleForOrDetect(m_creature,true);
1312 inline Unit* CreatureEventAI::SelectUnit(AttackingTarget target, uint32 position)
1314 //ThreatList m_threatlist;
1315 std::list<HostilReference*>& m_threatlist = m_creature->getThreatManager().getThreatList();
1316 std::list<HostilReference*>::iterator i = m_threatlist.begin();
1317 std::list<HostilReference*>::reverse_iterator r = m_threatlist.rbegin();
1319 if (position >= m_threatlist.size() || !m_threatlist.size())
1320 return NULL;
1322 switch (target)
1324 case ATTACKING_TARGET_RANDOM:
1326 advance ( i , position + (rand() % (m_threatlist.size() - position ) ));
1327 return Unit::GetUnit(*m_creature,(*i)->getUnitGuid());
1329 case ATTACKING_TARGET_TOPAGGRO:
1331 advance ( i , position);
1332 return Unit::GetUnit(*m_creature,(*i)->getUnitGuid());
1334 case ATTACKING_TARGET_BOTTOMAGGRO:
1336 advance ( r , position);
1337 return Unit::GetUnit(*m_creature,(*r)->getUnitGuid());
1340 return NULL;
1343 inline uint32 CreatureEventAI::GetRandActionParam(uint32 rnd, uint32 param1, uint32 param2, uint32 param3)
1345 switch (rnd % 3)
1347 case 0:
1348 return param1;
1349 break;
1350 case 1:
1351 return param2;
1352 break;
1353 case 2:
1354 return param3;
1355 break;
1357 return 0;
1360 inline Unit* CreatureEventAI::GetTargetByType(uint32 Target, Unit* pActionInvoker)
1362 switch (Target)
1364 case TARGET_T_SELF:
1365 return m_creature;
1366 break;
1367 case TARGET_T_HOSTILE:
1368 return m_creature->getVictim();
1369 break;
1370 case TARGET_T_HOSTILE_SECOND_AGGRO:
1371 return SelectUnit(ATTACKING_TARGET_TOPAGGRO,1);
1372 break;
1373 case TARGET_T_HOSTILE_LAST_AGGRO:
1374 return SelectUnit(ATTACKING_TARGET_BOTTOMAGGRO,0);
1375 break;
1376 case TARGET_T_HOSTILE_RANDOM:
1377 return SelectUnit(ATTACKING_TARGET_RANDOM,0);
1378 break;
1379 case TARGET_T_HOSTILE_RANDOM_NOT_TOP:
1380 return SelectUnit(ATTACKING_TARGET_RANDOM,1);
1381 break;
1382 case TARGET_T_ACTION_INVOKER:
1383 return pActionInvoker;
1384 break;
1385 default:
1386 return NULL;
1387 break;
1391 Unit* CreatureEventAI::DoSelectLowestHpFriendly(float range, uint32 MinHPDiff)
1393 CellPair p(MaNGOS::ComputeCellPair(m_creature->GetPositionX(), m_creature->GetPositionY()));
1394 Cell cell(p);
1395 cell.data.Part.reserved = ALL_DISTRICT;
1396 cell.SetNoCreate();
1398 Unit* pUnit = NULL;
1400 MaNGOS::MostHPMissingInRange u_check(m_creature, range, MinHPDiff);
1401 MaNGOS::UnitLastSearcher<MaNGOS::MostHPMissingInRange> searcher(m_creature, pUnit, u_check);
1404 typedef TYPELIST_4(GameObject, Creature*except pets*, DynamicObject, Corpse*Bones*) AllGridObjectTypes;
1405 This means that if we only search grid then we cannot possibly return pets or players so this is safe
1407 TypeContainerVisitor<MaNGOS::UnitLastSearcher<MaNGOS::MostHPMissingInRange>, GridTypeMapContainer > grid_unit_searcher(searcher);
1409 CellLock<GridReadGuard> cell_lock(cell, p);
1410 cell_lock->Visit(cell_lock, grid_unit_searcher, *m_creature->GetMap());
1411 return pUnit;
1414 void CreatureEventAI::DoFindFriendlyCC(std::list<Creature*>& _list, float range)
1416 CellPair p(MaNGOS::ComputeCellPair(m_creature->GetPositionX(), m_creature->GetPositionY()));
1417 Cell cell(p);
1418 cell.data.Part.reserved = ALL_DISTRICT;
1419 cell.SetNoCreate();
1421 MaNGOS::FriendlyCCedInRange u_check(m_creature, range);
1422 MaNGOS::CreatureListSearcher<MaNGOS::FriendlyCCedInRange> searcher(m_creature, _list, u_check);
1424 TypeContainerVisitor<MaNGOS::CreatureListSearcher<MaNGOS::FriendlyCCedInRange>, GridTypeMapContainer > grid_creature_searcher(searcher);
1426 CellLock<GridReadGuard> cell_lock(cell, p);
1427 cell_lock->Visit(cell_lock, grid_creature_searcher, *m_creature->GetMap());
1430 void CreatureEventAI::DoFindFriendlyMissingBuff(std::list<Creature*>& _list, float range, uint32 spellid)
1432 CellPair p(MaNGOS::ComputeCellPair(m_creature->GetPositionX(), m_creature->GetPositionY()));
1433 Cell cell(p);
1434 cell.data.Part.reserved = ALL_DISTRICT;
1435 cell.SetNoCreate();
1437 MaNGOS::FriendlyMissingBuffInRange u_check(m_creature, range, spellid);
1438 MaNGOS::CreatureListSearcher<MaNGOS::FriendlyMissingBuffInRange> searcher(m_creature, _list, u_check);
1440 TypeContainerVisitor<MaNGOS::CreatureListSearcher<MaNGOS::FriendlyMissingBuffInRange>, GridTypeMapContainer > grid_creature_searcher(searcher);
1442 CellLock<GridReadGuard> cell_lock(cell, p);
1443 cell_lock->Visit(cell_lock, grid_creature_searcher, *m_creature->GetMap());
1446 //*********************************
1447 //*** Functions used globally ***
1449 void CreatureEventAI::DoScriptText(int32 textEntry, WorldObject* pSource, Unit* target)
1451 if (!pSource)
1453 sLog.outErrorDb("CreatureEventAI: DoScriptText entry %i, invalid Source pointer.",textEntry);
1454 return;
1457 if (textEntry >= 0)
1459 sLog.outErrorDb("CreatureEventAI: DoScriptText with source entry %u (TypeId=%u, guid=%u) attempts to process text entry %i, but text entry must be negative.",pSource->GetEntry(),pSource->GetTypeId(),pSource->GetGUIDLow(),textEntry);
1460 return;
1463 CreatureEventAI_TextMap::const_iterator i = CreatureEAI_Mgr.GetCreatureEventAITextMap().find(textEntry);
1465 if (i == CreatureEAI_Mgr.GetCreatureEventAITextMap().end())
1467 sLog.outErrorDb("CreatureEventAI: DoScriptText with source entry %u (TypeId=%u, guid=%u) could not find text entry %i.",pSource->GetEntry(),pSource->GetTypeId(),pSource->GetGUIDLow(),textEntry);
1468 return;
1471 sLog.outDebug("CreatureEventAI: DoScriptText: text entry=%i, Sound=%u, Type=%u, Language=%u, Emote=%u",textEntry,(*i).second.SoundId,(*i).second.Type,(*i).second.Language,(*i).second.Emote);
1473 if((*i).second.SoundId)
1475 if (GetSoundEntriesStore()->LookupEntry((*i).second.SoundId))
1476 pSource->PlayDirectSound((*i).second.SoundId);
1477 else
1478 sLog.outErrorDb("CreatureEventAI: DoScriptText entry %i tried to process invalid sound id %u.",textEntry,(*i).second.SoundId);
1481 if((*i).second.Emote)
1483 if (pSource->GetTypeId() == TYPEID_UNIT || pSource->GetTypeId() == TYPEID_PLAYER)
1485 ((Unit*)pSource)->HandleEmoteCommand((*i).second.Emote);
1487 else
1488 sLog.outErrorDb("CreatureEventAI: DoScriptText entry %i tried to process emote for invalid TypeId (%u).",textEntry,pSource->GetTypeId());
1491 switch((*i).second.Type)
1493 case CHAT_TYPE_SAY:
1494 pSource->MonsterSay(textEntry, (*i).second.Language, target ? target->GetGUID() : 0);
1495 break;
1496 case CHAT_TYPE_YELL:
1497 pSource->MonsterYell(textEntry, (*i).second.Language, target ? target->GetGUID() : 0);
1498 break;
1499 case CHAT_TYPE_TEXT_EMOTE:
1500 pSource->MonsterTextEmote(textEntry, target ? target->GetGUID() : 0);
1501 break;
1502 case CHAT_TYPE_BOSS_EMOTE:
1503 pSource->MonsterTextEmote(textEntry, target ? target->GetGUID() : 0, true);
1504 break;
1505 case CHAT_TYPE_WHISPER:
1507 if (target && target->GetTypeId() == TYPEID_PLAYER)
1508 pSource->MonsterWhisper(textEntry, target->GetGUID());
1509 else sLog.outErrorDb("CreatureEventAI: DoScriptText entry %i cannot whisper without target unit (TYPEID_PLAYER).", textEntry);
1510 }break;
1511 case CHAT_TYPE_BOSS_WHISPER:
1513 if (target && target->GetTypeId() == TYPEID_PLAYER)
1514 pSource->MonsterWhisper(textEntry, target->GetGUID(), true);
1515 else sLog.outErrorDb("CreatureEventAI: DoScriptText entry %i cannot whisper without target unit (TYPEID_PLAYER).", textEntry);
1516 }break;
1517 case CHAT_TYPE_ZONE_YELL:
1518 pSource->MonsterYellToZone(textEntry, (*i).second.Language, target ? target->GetGUID() : 0);
1519 break;
1523 void CreatureEventAI::DoZoneInCombat(Unit* pUnit)
1525 if (!pUnit)
1526 pUnit = m_creature;
1528 Map *map = pUnit->GetMap();
1530 if (!map->IsDungeon()) //use IsDungeon instead of Instanceable, in case battlegrounds will be instantiated
1532 sLog.outErrorDb("CreatureEventAI: DoZoneInCombat call for map that isn't an instance (pUnit entry = %d)", pUnit->GetTypeId() == TYPEID_UNIT ? ((Creature*)pUnit)->GetEntry() : 0);
1533 return;
1536 if (!pUnit->CanHaveThreatList() || pUnit->getThreatManager().isThreatListEmpty())
1538 sLog.outErrorDb("CreatureEventAI: DoZoneInCombat called for creature that either cannot have threat list or has empty threat list (pUnit entry = %d)", pUnit->GetTypeId() == TYPEID_UNIT ? ((Creature*)pUnit)->GetEntry() : 0);
1540 return;
1543 Map::PlayerList const &PlayerList = map->GetPlayers();
1544 for(Map::PlayerList::const_iterator i = PlayerList.begin(); i != PlayerList.end(); ++i)
1545 if (Player* i_pl = i->getSource())
1546 if (!i_pl->isGameMaster())
1547 pUnit->AddThreat(i_pl, 0.0f);
1550 void CreatureEventAI::DoMeleeAttackIfReady()
1552 //Make sure our attack is ready before checking distance
1553 if (m_creature->isAttackReady())
1555 //If we are within range melee the target
1556 if (m_creature->IsWithinDistInMap(m_creature->getVictim(), ATTACK_DISTANCE))
1558 m_creature->AttackerStateUpdate(m_creature->getVictim());
1559 m_creature->resetAttackTimer();
1564 bool CreatureEventAI::CanCast(Unit* Target, SpellEntry const *Spell, bool Triggered)
1566 //No target so we can't cast
1567 if (!Target || !Spell)
1568 return false;
1570 //Silenced so we can't cast
1571 if (!Triggered && m_creature->HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_SILENCED))
1572 return false;
1574 //Check for power
1575 if (!Triggered && m_creature->GetPower((Powers)Spell->powerType) < Spell->manaCost)
1576 return false;
1578 SpellRangeEntry const *TempRange = NULL;
1580 TempRange = GetSpellRangeStore()->LookupEntry(Spell->rangeIndex);
1582 //Spell has invalid range store so we can't use it
1583 if (!TempRange)
1584 return false;
1586 //Unit is out of range of this spell
1587 if (m_creature->GetDistance(Target) > TempRange->maxRange || m_creature->GetDistance(Target) < TempRange->minRange)
1588 return false;
1590 return true;
1593 void CreatureEventAI::ReceiveEmote(Player* pPlayer, uint32 text_emote)
1595 if (bEmptyList)
1596 return;
1598 for (std::list<CreatureEventAIHolder>::iterator itr = CreatureEventAIList.begin(); itr != CreatureEventAIList.end(); ++itr)
1600 if ((*itr).Event.event_type == EVENT_T_RECEIVE_EMOTE)
1602 if ((*itr).Event.event_param1 != text_emote)
1603 return;
1605 bool bProcess = false;
1607 switch((*itr).Event.event_param2)
1609 //enum ConditionType
1610 case CONDITION_NONE: // 0 0
1611 bProcess = true;
1612 break;
1613 case CONDITION_AURA: // spell_id effindex
1614 if (pPlayer->HasAura((*itr).Event.event_param3,(*itr).Event.event_param4))
1615 bProcess = true;
1616 break;
1617 case CONDITION_ITEM: // item_id count
1618 if (pPlayer->HasItemCount((*itr).Event.event_param3,(*itr).Event.event_param4))
1619 bProcess = true;
1620 break;
1621 case CONDITION_ITEM_EQUIPPED: // item_id count
1622 if (pPlayer->HasItemOrGemWithIdEquipped((*itr).Event.event_param3,(*itr).Event.event_param4))
1623 bProcess = true;
1624 break;
1625 case CONDITION_ZONEID: // zone_id 0
1626 if (pPlayer->GetZoneId() == (*itr).Event.event_param3)
1627 bProcess = true;
1628 break;
1629 case CONDITION_REPUTATION_RANK: // faction_id min_rank
1630 if (pPlayer->GetReputationRank((*itr).Event.event_param3) >= (*itr).Event.event_param4)
1631 bProcess = true;
1632 break;
1633 case CONDITION_TEAM: // player_team 0, (469 - Alliance 67 - Horde)
1634 if (pPlayer->GetTeam() == (*itr).Event.event_param3)
1635 bProcess = true;
1636 break;
1637 case CONDITION_SKILL: // skill_id min skill_value
1638 if (pPlayer->HasSkill((*itr).Event.event_param3) && pPlayer->GetSkillValue((*itr).Event.event_param3) >= (*itr).Event.event_param4)
1639 bProcess = true;
1640 break;
1641 case CONDITION_QUESTREWARDED: // quest_id 0
1642 if (pPlayer->GetQuestRewardStatus((*itr).Event.event_param3))
1643 bProcess = true;
1644 break;
1645 case CONDITION_QUESTTAKEN: // quest_id 0, for condition true while quest active.
1646 if (pPlayer->GetQuestStatus((*itr).Event.event_param3) == QUEST_STATUS_INCOMPLETE)
1647 bProcess = true;
1648 break;
1649 case CONDITION_ACTIVE_EVENT: // event_id 0
1650 if (IsHolidayActive(HolidayIds((*itr).Event.event_param3)))
1651 bProcess = true;
1652 break;
1655 if (bProcess)
1657 sLog.outDebug("CreatureEventAI: ReceiveEmote CreatureEventAI: Condition ok, processing");
1658 ProcessEvent(*itr, pPlayer);