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
20 #include "CreatureEventAI.h"
21 #include "CreatureEventAIMgr.h"
22 #include "ObjectMgr.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
)
51 if ((*i
).event_flags
& EFLAG_DEBUG_ONLY
)
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
));
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());
71 sLog
.outError("CreatureEventAI: EventMap for Creature %u is empty but creature is using CreatureEventAI.", m_creature
->GetEntry());
73 bEmptyList
= CreatureEventAIList
.empty();
75 CombatMovementEnabled
= true;
80 //Handle Spawned Events
83 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
85 if ((*i
).Event
.event_type
== EVENT_T_SPAWNED
)
92 bool CreatureEventAI::ProcessEvent(CreatureEventAIHolder
& pHolder
, Unit
* pActionInvoker
)
94 if (!pHolder
.Enabled
|| pHolder
.Time
)
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
))
101 //Store random here so that all random actions match up
104 //Return if chance for event is not met
105 if (pHolder
.Event
.event_chance
<= rnd
% 100)
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
)
118 if (!m_creature
->isInCombat())
122 if (param3
== param4
)
124 pHolder
.Time
= param3
;
126 }else if (param4
> param3
)
127 pHolder
.Time
= urand(param3
, param4
);
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;
135 case EVENT_T_TIMER_OOC
:
137 if (m_creature
->isInCombat())
141 if (param3
== param4
)
143 pHolder
.Time
= param3
;
145 }else if (param4
> param3
)
146 pHolder
.Time
= urand(param3
, param4
);
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;
157 if (!m_creature
->isInCombat() || !m_creature
->GetMaxHealth())
160 uint32 perc
= (m_creature
->GetHealth()*100) / m_creature
->GetMaxHealth();
162 if (perc
> param1
|| perc
< param2
)
166 if (param3
== param4
)
168 pHolder
.Time
= param3
;
170 }else if (param4
> param3
)
171 pHolder
.Time
= urand(param3
, param4
);
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;
182 if (!m_creature
->isInCombat() || !m_creature
->GetMaxPower(POWER_MANA
))
185 uint32 perc
= (m_creature
->GetPower(POWER_MANA
)*100) / m_creature
->GetMaxPower(POWER_MANA
);
187 if (perc
> param1
|| perc
< param2
)
191 if (param3
== param4
)
193 pHolder
.Time
= param3
;
195 }else if (param4
> param3
)
196 pHolder
.Time
= urand(param3
, param4
);
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;
212 if (param1
== param2
)
214 pHolder
.Time
= param1
;
216 }else if (param2
> param1
)
217 pHolder
.Time
= urand(param1
, param2
);
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;
233 case EVENT_T_SPELLHIT
:
235 //Spell hit is special case, param1 and param2 handled within CreatureEventAI::SpellHit
238 if (param3
== param4
)
240 pHolder
.Time
= param3
;
242 }else if (param4
> param3
)
243 pHolder
.Time
= urand(param3
, param4
);
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;
255 if (param3
== param4
)
257 pHolder
.Time
= param3
;
259 }else if (param4
> param3
)
260 pHolder
.Time
= urand(param3
, param4
);
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;
269 case EVENT_T_OOC_LOS
:
272 if (param3
== param4
)
274 pHolder
.Time
= param3
;
276 }else if (param4
> param3
)
277 pHolder
.Time
= urand(param3
, param4
);
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;
286 case EVENT_T_SPAWNED
:
290 case EVENT_T_TARGET_HP
:
292 if (!m_creature
->isInCombat() || !m_creature
->getVictim() || !m_creature
->getVictim()->GetMaxHealth())
295 uint32 perc
= (m_creature
->getVictim()->GetHealth()*100) / m_creature
->getVictim()->GetMaxHealth();
297 if (perc
> param1
|| perc
< param2
)
301 if (param3
== param4
)
303 pHolder
.Time
= param3
;
305 }else if (param4
> param3
)
306 pHolder
.Time
= urand(param3
, param4
);
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;
315 case EVENT_T_TARGET_CASTING
:
317 if (!m_creature
->isInCombat() || !m_creature
->getVictim() || !m_creature
->getVictim()->IsNonMeleeSpellCasted(false, false, true))
321 if (param1
== param2
)
323 pHolder
.Time
= param1
;
325 }else if (param2
> param1
)
326 pHolder
.Time
= urand(param1
, param2
);
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;
335 case EVENT_T_FRIENDLY_HP
:
337 if (!m_creature
->isInCombat())
340 Unit
* pUnit
= DoSelectLowestHpFriendly(param2
, param1
);
345 pActionInvoker
= pUnit
;
348 if (param3
== param4
)
350 pHolder
.Time
= param3
;
352 }else if (param4
> param3
)
353 pHolder
.Time
= urand(param3
, param4
);
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;
362 case EVENT_T_FRIENDLY_IS_CC
:
364 if (!m_creature
->isInCombat())
367 std::list
<Creature
*> pList
;
368 DoFindFriendlyCC(pList
, param2
);
374 //We don't really care about the whole list, just return first available
375 pActionInvoker
= *(pList
.begin());
378 if (param3
== param4
)
380 pHolder
.Time
= param3
;
382 }else if (param4
> param3
)
383 pHolder
.Time
= urand(param3
, param4
);
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;
391 case EVENT_T_FRIENDLY_MISSING_BUFF
:
393 std::list
<Creature
*> pList
;
394 DoFindFriendlyMissingBuff(pList
, param2
, param1
);
400 //We don't really care about the whole list, just return first available
401 pActionInvoker
= *(pList
.begin());
404 if (param3
== param4
)
406 pHolder
.Time
= param3
;
408 }else if (param4
> param3
)
409 pHolder
.Time
= urand(param3
, param4
);
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;
418 case EVENT_T_SUMMONED_UNIT
:
420 //Prevent event from occuring on no unit or non creatures
421 if (!pActionInvoker
|| pActionInvoker
->GetTypeId()!=TYPEID_UNIT
)
424 //Creature id doesn't match up
425 if (param1
&& ((Creature
*)pActionInvoker
)->GetEntry() != param1
)
429 if (param2
== param3
)
431 pHolder
.Time
= param2
;
433 }else if (param3
> param2
)
434 pHolder
.Time
= urand(param2
, param3
);
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;
443 case EVENT_T_REACHED_HOME
:
447 case EVENT_T_RECEIVE_EMOTE
:
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
);
457 //Disable non-repeatable events
458 if (!(pHolder
.Event
.event_flags
& EFLAG_REPEATABLE
))
459 pHolder
.Enabled
= false;
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
);
468 void CreatureEventAI::ProcessAction(uint16 type
, uint32 param1
, uint32 param2
, uint32 param3
, uint32 rnd
, uint32 EventId
, Unit
* pActionInvoker
)
479 if (param2
&& param3
)
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) )
501 if (pActionInvoker
->GetTypeId() == TYPEID_PLAYER
)
502 target
= pActionInvoker
;
503 else if (Unit
* owner
= pActionInvoker
->GetOwner())
505 if (owner
->GetTypeId() == TYPEID_PLAYER
)
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
)
517 DoScriptText(temp
, m_creature
, target
);
521 case ACTION_T_SET_FACTION
:
524 m_creature
->setFaction(param1
);
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
);
536 case ACTION_T_MORPH_TO_ENTRY_OR_MODEL
:
538 if (param1
|| param2
)
540 //set model based on entry from creature_template
543 if (CreatureInfo
const* ci
= GetCreatureTemplateStore(param1
))
545 //use default display
547 m_creature
->SetDisplayId(ci
->DisplayID_A
);
550 //if no param1, then use value from param2 (modelId)
552 m_creature
->SetDisplayId(param2
);
555 m_creature
->DeMorph();
559 m_creature
->PlayDirectSound(param1
);
562 m_creature
->HandleEmoteCommand(param1
);
564 case ACTION_T_RANDOM_SOUND
:
566 uint32 temp
= GetRandActionParam(rnd
, param1
, param2
, param3
);
568 if (temp
!= uint32(0xffffffff))
569 m_creature
->PlayDirectSound( temp
);
572 case ACTION_T_RANDOM_EMOTE
:
574 uint32 temp
= GetRandActionParam(rnd
, param1
, param2
, param3
);
576 if (temp
!= uint32(0xffffffff))
577 m_creature
->HandleEmoteCommand(temp
);
582 Unit
* target
= GetTargetByType(param2
, pActionInvoker
);
583 Unit
* caster
= m_creature
;
588 //Cast is always triggered if target is forced to cast on self
589 if (param3
& CAST_FORCE_TARGET_SELF
)
591 param3
|= CAST_TRIGGERED
;
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
))
608 const SpellEntry
* tSpell
= GetSpellStore()->LookupEntry(param1
);
610 //Verify that spell exists
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
)
625 m_creature
->GetMotionMaster()->Clear(false);
626 m_creature
->GetMotionMaster()->MoveChase(m_creature
->getVictim(), AttackDistance
, AttackAngle
);
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
));
641 sLog
.outErrorDb("CreatureEventAI: event %d creature %d attempt to cast spell that doesn't exist %d", EventId
, m_creature
->GetEntry(), param1
);
645 case ACTION_T_SUMMON
:
647 Unit
* target
= GetTargetByType(param2
, pActionInvoker
);
649 Creature
* pCreature
= NULL
;
652 pCreature
= m_creature
->SummonCreature(param1
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, TEMPSUMMON_TIMED_OR_DEAD_DESPAWN
, param3
);
654 pCreature
= m_creature
->SummonCreature(param1
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, TEMPSUMMON_TIMED_DESPAWN_OUT_OF_COMBAT
, 0);
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
);
665 case ACTION_T_THREAT_SINGLE_PCT
:
667 Unit
* target
= GetTargetByType(param2
, pActionInvoker
);
670 m_creature
->getThreatManager().modifyThreatPercent(target
, param1
);
673 case ACTION_T_THREAT_ALL_PCT
:
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());
682 m_creature
->getThreatManager().modifyThreatPercent(Temp
, param1
);
686 case ACTION_T_QUEST_EVENT
:
688 Unit
* target
= GetTargetByType(param2
, pActionInvoker
);
690 if (target
&& target
->GetTypeId() == TYPEID_PLAYER
)
691 ((Player
*)target
)->AreaExploredOrEventHappens(param1
);
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
);
702 case ACTION_T_SET_UNIT_FIELD
:
704 Unit
* target
= GetTargetByType(param3
, pActionInvoker
);
706 if (param1
< OBJECT_END
|| param1
>= UNIT_END
)
710 target
->SetUInt32Value(param1
, param2
);
713 case ACTION_T_SET_UNIT_FLAG
:
715 Unit
* target
= GetTargetByType(param2
, pActionInvoker
);
718 target
->SetFlag(UNIT_FIELD_FLAGS
, param1
);
721 case ACTION_T_REMOVE_UNIT_FLAG
:
723 Unit
* target
= GetTargetByType(param2
, pActionInvoker
);
726 target
->RemoveFlag(UNIT_FIELD_FLAGS
, param1
);
729 case ACTION_T_AUTO_ATTACK
:
733 else MeleeEnabled
= false;
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
);
750 if (m_creature
->GetMotionMaster()->GetCurrentMovementGeneratorType() == TARGETED_MOTION_TYPE
)
752 m_creature
->GetMotionMaster()->Clear(false);
753 m_creature
->GetMotionMaster()->MoveIdle();
754 m_creature
->StopMoving();
758 case ACTION_T_SET_PHASE
:
763 case ACTION_T_INC_PHASE
:
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());
779 //TODO: Replace with Flee movement generator
780 m_creature
->CastSpell(m_creature
, SPELL_RUN_AWAY
, true);
783 case ACTION_T_QUEST_EVENT_ALL
:
786 if( pActionInvoker
&& pActionInvoker
->GetTypeId() == TYPEID_PLAYER
)
788 Temp
= Unit::GetUnit(*m_creature
,pActionInvoker
->GetGUID());
790 ((Player
*)Temp
)->GroupEventHappens(param1
,m_creature
);
794 case ACTION_T_CASTCREATUREGO_ALL
:
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
);
807 case ACTION_T_REMOVEAURASFROMSPELL
:
809 Unit
* target
= GetTargetByType(param1
, pActionInvoker
);
812 target
->RemoveAurasDueToSpell(param2
);
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
);
831 case ACTION_T_RANDOM_PHASE
:
833 uint32 temp
= GetRandActionParam(rnd
, param1
, param2
, param3
);
838 case ACTION_T_RANDOM_PHASE_RANGE
:
842 Phase
= param1
+ (rnd
% (param2
- param1
));
845 sLog
.outErrorDb( "CreatureEventAI: ACTION_T_RANDOM_PHASE_RANGE cannot have Param2 <= Param1. Divide by Zero. Event = %d. CreatureEntry = %d", EventId
, m_creature
->GetEntry());
848 case ACTION_T_SUMMON_ID
:
850 Unit
* target
= GetTargetByType(param2
, pActionInvoker
);
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());
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);
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
);
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
);
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
);
892 case ACTION_T_SET_INST_DATA
:
894 InstanceData
* pInst
= (InstanceData
*)m_creature
->GetInstanceData();
897 sLog
.outErrorDb("CreatureEventAI: Event %d attempt to set instance data without instance script. Creature %d", EventId
, m_creature
->GetEntry());
901 pInst
->SetData(param1
, param2
);
904 case ACTION_T_SET_INST_DATA64
:
906 Unit
* target
= GetTargetByType(param2
, pActionInvoker
);
909 sLog
.outErrorDb("CreatureEventAI: Event %d attempt to set instance data64 but Target == NULL. Creature %d", EventId
, m_creature
->GetEntry());
913 InstanceData
* pInst
= (InstanceData
*)m_creature
->GetInstanceData();
916 sLog
.outErrorDb("CreatureEventAI: Event %d attempt to set instance data64 without instance script. Creature %d", EventId
, m_creature
->GetEntry());
920 pInst
->SetData64(param1
, target
->GetGUID());
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());
932 m_creature
->UpdateEntry(param1
, param2
? HORDE
: ALLIANCE
);
937 if (m_creature
->isDead())
940 sLog
.outErrorDb("CreatureEventAI: Event %d ACTION_T_DIE on dead creature. Creature %d", EventId
, m_creature
->GetEntry());
943 m_creature
->DealDamage(m_creature
, m_creature
->GetMaxHealth(),NULL
, DIRECT_DAMAGE
, SPELL_SCHOOL_MASK_NORMAL
, NULL
, false);
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());
955 DoZoneInCombat(m_creature
);
961 void CreatureEventAI::JustRespawned()
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
)
976 void CreatureEventAI::Reset()
978 EventUpdateTime
= EVENT_UPDATE_TIME
;
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
;
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;
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
);
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;
1015 void CreatureEventAI::JustReachedHome()
1017 m_creature
->LoadCreaturesAddon();
1021 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
1023 if ((*i
).Event
.event_type
== EVENT_T_REACHED_HOME
)
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
);
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
)
1053 void CreatureEventAI::JustDied(Unit
* killer
)
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
)
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
)
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
1097 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
1099 switch ((*i
).Event
.event_type
)
1102 (*i
).Enabled
= true;
1103 ProcessEvent(*i
, enemy
);
1105 //Reset all in combat timers
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;
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
);
1120 //All normal events need to be re-enabled and their time set to 0
1122 (*i
).Enabled
= true;
1129 EventUpdateTime
= EVENT_UPDATE_TIME
;
1133 void CreatureEventAI::AttackStart(Unit
*who
)
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
);
1150 m_creature
->GetMotionMaster()->MoveIdle();
1151 m_creature
->StopMoving();
1156 void CreatureEventAI::MoveInLineOfSight(Unit
*who
)
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())
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
)
1192 float attackRadius
= m_creature
->GetAttackDistance(who
);
1193 if (m_creature
->IsWithinDistInMap(who
, attackRadius
) && m_creature
->IsWithinLOSInMap(who
))
1195 if (!m_creature
->getVictim())
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
)
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())
1240 //Events are only updated once every EVENT_UPDATE_TIME ms to prevent lag with large amount of events
1241 if (EventUpdateTime
< diff
)
1245 //Check for time based events
1246 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
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
1263 //Events that are updated every EVENT_UPDATE_TIME
1264 switch ((*i
).Event
.event_type
)
1266 case EVENT_T_TIMER_OOC
:
1272 case EVENT_T_TARGET_HP
:
1273 case EVENT_T_TARGET_CASTING
:
1274 case EVENT_T_FRIENDLY_HP
:
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
)
1292 EventUpdateTime
= EVENT_UPDATE_TIME
;
1297 EventUpdateTime
-= diff
;
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())
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());
1343 inline uint32
CreatureEventAI::GetRandActionParam(uint32 rnd
, uint32 param1
, uint32 param2
, uint32 param3
)
1360 inline Unit
* CreatureEventAI::GetTargetByType(uint32 Target
, Unit
* pActionInvoker
)
1367 case TARGET_T_HOSTILE
:
1368 return m_creature
->getVictim();
1370 case TARGET_T_HOSTILE_SECOND_AGGRO
:
1371 return SelectUnit(ATTACKING_TARGET_TOPAGGRO
,1);
1373 case TARGET_T_HOSTILE_LAST_AGGRO
:
1374 return SelectUnit(ATTACKING_TARGET_BOTTOMAGGRO
,0);
1376 case TARGET_T_HOSTILE_RANDOM
:
1377 return SelectUnit(ATTACKING_TARGET_RANDOM
,0);
1379 case TARGET_T_HOSTILE_RANDOM_NOT_TOP
:
1380 return SelectUnit(ATTACKING_TARGET_RANDOM
,1);
1382 case TARGET_T_ACTION_INVOKER
:
1383 return pActionInvoker
;
1391 Unit
* CreatureEventAI::DoSelectLowestHpFriendly(float range
, uint32 MinHPDiff
)
1393 CellPair
p(MaNGOS::ComputeCellPair(m_creature
->GetPositionX(), m_creature
->GetPositionY()));
1395 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
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());
1414 void CreatureEventAI::DoFindFriendlyCC(std::list
<Creature
*>& _list
, float range
)
1416 CellPair
p(MaNGOS::ComputeCellPair(m_creature
->GetPositionX(), m_creature
->GetPositionY()));
1418 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
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()));
1434 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
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
)
1453 sLog
.outErrorDb("CreatureEventAI: DoScriptText entry %i, invalid Source pointer.",textEntry
);
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
);
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
);
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
);
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
);
1488 sLog
.outErrorDb("CreatureEventAI: DoScriptText entry %i tried to process emote for invalid TypeId (%u).",textEntry
,pSource
->GetTypeId());
1491 switch((*i
).second
.Type
)
1494 pSource
->MonsterSay(textEntry
, (*i
).second
.Language
, target
? target
->GetGUID() : 0);
1496 case CHAT_TYPE_YELL
:
1497 pSource
->MonsterYell(textEntry
, (*i
).second
.Language
, target
? target
->GetGUID() : 0);
1499 case CHAT_TYPE_TEXT_EMOTE
:
1500 pSource
->MonsterTextEmote(textEntry
, target
? target
->GetGUID() : 0);
1502 case CHAT_TYPE_BOSS_EMOTE
:
1503 pSource
->MonsterTextEmote(textEntry
, target
? target
->GetGUID() : 0, true);
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
);
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
);
1517 case CHAT_TYPE_ZONE_YELL
:
1518 pSource
->MonsterYellToZone(textEntry
, (*i
).second
.Language
, target
? target
->GetGUID() : 0);
1523 void CreatureEventAI::DoZoneInCombat(Unit
* pUnit
)
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);
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);
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
)
1570 //Silenced so we can't cast
1571 if (!Triggered
&& m_creature
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_SILENCED
))
1575 if (!Triggered
&& m_creature
->GetPower((Powers
)Spell
->powerType
) < Spell
->manaCost
)
1578 SpellRangeEntry
const *TempRange
= NULL
;
1580 TempRange
= GetSpellRangeStore()->LookupEntry(Spell
->rangeIndex
);
1582 //Spell has invalid range store so we can't use it
1586 //Unit is out of range of this spell
1587 if (m_creature
->GetDistance(Target
) > TempRange
->maxRange
|| m_creature
->GetDistance(Target
) < TempRange
->minRange
)
1593 void CreatureEventAI::ReceiveEmote(Player
* pPlayer
, uint32 text_emote
)
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
)
1605 bool bProcess
= false;
1607 switch((*itr
).Event
.event_param2
)
1609 //enum ConditionType
1610 case CONDITION_NONE
: // 0 0
1613 case CONDITION_AURA
: // spell_id effindex
1614 if (pPlayer
->HasAura((*itr
).Event
.event_param3
,(*itr
).Event
.event_param4
))
1617 case CONDITION_ITEM
: // item_id count
1618 if (pPlayer
->HasItemCount((*itr
).Event
.event_param3
,(*itr
).Event
.event_param4
))
1621 case CONDITION_ITEM_EQUIPPED
: // item_id count
1622 if (pPlayer
->HasItemOrGemWithIdEquipped((*itr
).Event
.event_param3
,(*itr
).Event
.event_param4
))
1625 case CONDITION_ZONEID
: // zone_id 0
1626 if (pPlayer
->GetZoneId() == (*itr
).Event
.event_param3
)
1629 case CONDITION_REPUTATION_RANK
: // faction_id min_rank
1630 if (pPlayer
->GetReputationRank((*itr
).Event
.event_param3
) >= (*itr
).Event
.event_param4
)
1633 case CONDITION_TEAM
: // player_team 0, (469 - Alliance 67 - Horde)
1634 if (pPlayer
->GetTeam() == (*itr
).Event
.event_param3
)
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
)
1641 case CONDITION_QUESTREWARDED
: // quest_id 0
1642 if (pPlayer
->GetQuestRewardStatus((*itr
).Event
.event_param3
))
1645 case CONDITION_QUESTTAKEN
: // quest_id 0, for condition true while quest active.
1646 if (pPlayer
->GetQuestStatus((*itr
).Event
.event_param3
) == QUEST_STATUS_INCOMPLETE
)
1649 case CONDITION_ACTIVE_EVENT
: // event_id 0
1650 if (IsHolidayActive(HolidayIds((*itr
).Event
.event_param3
)))
1657 sLog
.outDebug("CreatureEventAI: ReceiveEmote CreatureEventAI: Condition ok, processing");
1658 ProcessEvent(*itr
, pPlayer
);