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 "InstanceData.h"
32 bool CreatureEventAIHolder::UpdateRepeatTimer( Creature
* creature
, uint32 repeatMin
, uint32 repeatMax
)
34 if (repeatMin
== repeatMax
)
36 else if (repeatMax
> repeatMin
)
37 Time
= urand(repeatMin
, repeatMax
);
40 sLog
.outErrorDb("CreatureEventAI: Creature %u using Event %u (Type = %u) has RandomMax < RandomMin. Event repeating disabled.", creature
->GetEntry(), Event
.event_id
, Event
.event_type
);
48 int CreatureEventAI::Permissible(const Creature
*creature
)
50 if( creature
->GetAIName() == "EventAI" )
51 return PERMIT_BASE_SPECIAL
;
52 return PERMIT_BASE_NO
;
55 CreatureEventAI::CreatureEventAI(Creature
*c
) : CreatureAI(c
)
57 // Need make copy for filter unneeded steps and safe in case table reload
58 CreatureEventAI_Event_Map::const_iterator CreatureEvents
= CreatureEAI_Mgr
.GetCreatureEventAIMap().find(m_creature
->GetEntry());
59 if (CreatureEvents
!= CreatureEAI_Mgr
.GetCreatureEventAIMap().end())
61 std::vector
<CreatureEventAI_Event
>::const_iterator i
;
62 for (i
= (*CreatureEvents
).second
.begin(); i
!= (*CreatureEvents
).second
.end(); ++i
)
67 if ((*i
).event_flags
& EFLAG_DEBUG_ONLY
)
70 if (m_creature
->GetMap()->IsDungeon())
72 if( (m_creature
->GetMap()->IsHeroic() && (*i
).event_flags
& EFLAG_HEROIC
) ||
73 (!m_creature
->GetMap()->IsHeroic() && (*i
).event_flags
& EFLAG_NORMAL
))
75 //event flagged for instance mode
76 CreatureEventAIList
.push_back(CreatureEventAIHolder(*i
));
80 CreatureEventAIList
.push_back(CreatureEventAIHolder(*i
));
82 //EventMap had events but they were not added because they must be for instance
83 if (CreatureEventAIList
.empty())
84 sLog
.outError("CreatureEventAI: Creature %u has events but no events added to list because of instance flags.", m_creature
->GetEntry());
87 sLog
.outError("CreatureEventAI: EventMap for Creature %u is empty but creature is using CreatureEventAI.", m_creature
->GetEntry());
89 bEmptyList
= CreatureEventAIList
.empty();
91 CombatMovementEnabled
= true;
93 AttackDistance
= 0.0f
;
96 InvinceabilityHpLevel
= 0;
98 //Handle Spawned Events
101 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
102 if (SpawnedEventConditionsCheck((*i
).Event
))
108 bool CreatureEventAI::ProcessEvent(CreatureEventAIHolder
& pHolder
, Unit
* pActionInvoker
)
110 if (!pHolder
.Enabled
|| pHolder
.Time
)
113 //Check the inverse phase mask (event doesn't trigger if current phase bit is set in mask)
114 if (pHolder
.Event
.event_inverse_phase_mask
& (1 << Phase
))
117 CreatureEventAI_Event
const& event
= pHolder
.Event
;
119 //Check event conditions based on the event type, also reset events
120 switch (event
.event_type
)
123 if (!m_creature
->isInCombat())
127 pHolder
.UpdateRepeatTimer(m_creature
,event
.timer
.repeatMin
,event
.timer
.repeatMax
);
129 case EVENT_T_TIMER_OOC
:
130 if (m_creature
->isInCombat())
134 pHolder
.UpdateRepeatTimer(m_creature
,event
.timer
.repeatMin
,event
.timer
.repeatMax
);
138 if (!m_creature
->isInCombat() || !m_creature
->GetMaxHealth())
141 uint32 perc
= (m_creature
->GetHealth()*100) / m_creature
->GetMaxHealth();
143 if (perc
> event
.percent_range
.percentMax
|| perc
< event
.percent_range
.percentMin
)
147 pHolder
.UpdateRepeatTimer(m_creature
,event
.percent_range
.repeatMin
,event
.percent_range
.repeatMax
);
152 if (!m_creature
->isInCombat() || !m_creature
->GetMaxPower(POWER_MANA
))
155 uint32 perc
= (m_creature
->GetPower(POWER_MANA
)*100) / m_creature
->GetMaxPower(POWER_MANA
);
157 if (perc
> event
.percent_range
.percentMax
|| perc
< event
.percent_range
.percentMin
)
161 pHolder
.UpdateRepeatTimer(m_creature
,event
.percent_range
.repeatMin
,event
.percent_range
.repeatMax
);
168 pHolder
.UpdateRepeatTimer(m_creature
,event
.kill
.repeatMin
,event
.kill
.repeatMax
);
173 case EVENT_T_SPELLHIT
:
174 //Spell hit is special case, param1 and param2 handled within CreatureEventAI::SpellHit
177 pHolder
.UpdateRepeatTimer(m_creature
,event
.spell_hit
.repeatMin
,event
.spell_hit
.repeatMax
);
181 pHolder
.UpdateRepeatTimer(m_creature
,event
.range
.repeatMin
,event
.range
.repeatMax
);
183 case EVENT_T_OOC_LOS
:
185 pHolder
.UpdateRepeatTimer(m_creature
,event
.ooc_los
.repeatMin
,event
.ooc_los
.repeatMax
);
187 case EVENT_T_SPAWNED
:
189 case EVENT_T_TARGET_HP
:
191 if (!m_creature
->isInCombat() || !m_creature
->getVictim() || !m_creature
->getVictim()->GetMaxHealth())
194 uint32 perc
= (m_creature
->getVictim()->GetHealth()*100) / m_creature
->getVictim()->GetMaxHealth();
196 if (perc
> event
.percent_range
.percentMax
|| perc
< event
.percent_range
.percentMin
)
200 pHolder
.UpdateRepeatTimer(m_creature
,event
.percent_range
.repeatMin
,event
.percent_range
.repeatMax
);
203 case EVENT_T_TARGET_CASTING
:
204 if (!m_creature
->isInCombat() || !m_creature
->getVictim() || !m_creature
->getVictim()->IsNonMeleeSpellCasted(false, false, true))
208 pHolder
.UpdateRepeatTimer(m_creature
,event
.target_casting
.repeatMin
,event
.target_casting
.repeatMax
);
210 case EVENT_T_FRIENDLY_HP
:
212 if (!m_creature
->isInCombat())
215 Unit
* pUnit
= DoSelectLowestHpFriendly(event
.friendly_hp
.radius
, event
.friendly_hp
.hpDeficit
);
219 pActionInvoker
= pUnit
;
222 pHolder
.UpdateRepeatTimer(m_creature
,event
.friendly_hp
.repeatMin
,event
.friendly_hp
.repeatMax
);
225 case EVENT_T_FRIENDLY_IS_CC
:
227 if (!m_creature
->isInCombat())
230 std::list
<Creature
*> pList
;
231 DoFindFriendlyCC(pList
, event
.friendly_is_cc
.radius
);
237 //We don't really care about the whole list, just return first available
238 pActionInvoker
= *(pList
.begin());
241 pHolder
.UpdateRepeatTimer(m_creature
,event
.friendly_is_cc
.repeatMin
,event
.friendly_is_cc
.repeatMax
);
244 case EVENT_T_FRIENDLY_MISSING_BUFF
:
246 std::list
<Creature
*> pList
;
247 DoFindFriendlyMissingBuff(pList
, event
.friendly_buff
.radius
, event
.friendly_buff
.spellId
);
253 //We don't really care about the whole list, just return first available
254 pActionInvoker
= *(pList
.begin());
257 pHolder
.UpdateRepeatTimer(m_creature
,event
.friendly_buff
.repeatMin
,event
.friendly_buff
.repeatMax
);
260 case EVENT_T_SUMMONED_UNIT
:
262 //Prevent event from occuring on no unit or non creatures
263 if (!pActionInvoker
|| pActionInvoker
->GetTypeId()!=TYPEID_UNIT
)
266 //Creature id doesn't match up
267 if (((Creature
*)pActionInvoker
)->GetEntry() != event
.summon_unit
.creatureId
)
271 pHolder
.UpdateRepeatTimer(m_creature
,event
.summon_unit
.repeatMin
,event
.summon_unit
.repeatMax
);
274 case EVENT_T_TARGET_MANA
:
276 if (!m_creature
->isInCombat() || !m_creature
->getVictim() || !m_creature
->getVictim()->GetMaxPower(POWER_MANA
))
279 uint32 perc
= (m_creature
->getVictim()->GetPower(POWER_MANA
)*100) / m_creature
->getVictim()->GetMaxPower(POWER_MANA
);
281 if (perc
> event
.percent_range
.percentMax
|| perc
< event
.percent_range
.percentMin
)
285 pHolder
.UpdateRepeatTimer(m_creature
,event
.percent_range
.repeatMin
,event
.percent_range
.repeatMax
);
288 case EVENT_T_REACHED_HOME
:
289 case EVENT_T_RECEIVE_EMOTE
:
293 //Note: checked only aura for effect 0, if need check aura for effect 1/2 then
294 // possible way: pack in event.buffed.amount 2 uint16 (ammount+effectIdx)
295 Aura
* aura
= m_creature
->GetAura(event
.buffed
.spellId
,0);
296 if(!aura
|| aura
->GetStackAmount() < event
.buffed
.amount
)
300 pHolder
.UpdateRepeatTimer(m_creature
,event
.buffed
.repeatMin
,event
.buffed
.repeatMax
);
303 case EVENT_T_TARGET_BUFFED
:
305 //Prevent event from occuring on no unit
309 //Note: checked only aura for effect 0, if need check aura for effect 1/2 then
310 // possible way: pack in event.buffed.amount 2 uint16 (ammount+effectIdx)
311 Aura
* aura
= pActionInvoker
->GetAura(event
.buffed
.spellId
,0);
312 if(!aura
|| aura
->GetStackAmount() < event
.buffed
.amount
)
316 pHolder
.UpdateRepeatTimer(m_creature
,event
.buffed
.repeatMin
,event
.buffed
.repeatMax
);
320 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
);
324 //Disable non-repeatable events
325 if (!(pHolder
.Event
.event_flags
& EFLAG_REPEATABLE
))
326 pHolder
.Enabled
= false;
328 //Store random here so that all random actions match up
331 //Return if chance for event is not met
332 if (pHolder
.Event
.event_chance
<= rnd
% 100)
336 for (uint32 j
= 0; j
< MAX_ACTIONS
; j
++)
337 ProcessAction(pHolder
.Event
.action
[j
], rnd
, pHolder
.Event
.event_id
, pActionInvoker
);
342 void CreatureEventAI::ProcessAction(CreatureEventAI_Action
const& action
, uint32 rnd
, uint32 EventId
, Unit
* pActionInvoker
)
348 if (!action
.text
.TextId1
)
353 if (action
.text
.TextId2
&& action
.text
.TextId3
)
357 case 0: temp
= action
.text
.TextId1
; break;
358 case 1: temp
= action
.text
.TextId2
; break;
359 case 2: temp
= action
.text
.TextId3
; break;
362 else if (action
.text
.TextId2
&& urand(0,1))
363 temp
= action
.text
.TextId2
;
365 temp
= action
.text
.TextId1
;
373 if (pActionInvoker
->GetTypeId() == TYPEID_PLAYER
)
374 target
= pActionInvoker
;
375 else if (Unit
* owner
= pActionInvoker
->GetOwner())
377 if (owner
->GetTypeId() == TYPEID_PLAYER
)
381 else if ((target
= m_creature
->getVictim()))
383 if (target
->GetTypeId() != TYPEID_PLAYER
)
384 if (Unit
* owner
= target
->GetOwner())
385 if (owner
->GetTypeId() == TYPEID_PLAYER
)
389 DoScriptText(temp
, m_creature
, target
);
393 case ACTION_T_SET_FACTION
:
395 if (action
.set_faction
.factionId
)
396 m_creature
->setFaction(action
.set_faction
.factionId
);
399 if (CreatureInfo
const* ci
= GetCreatureTemplateStore(m_creature
->GetEntry()))
401 //if no id provided, assume reset and then use default
402 if (m_creature
->getFaction() != ci
->faction_A
)
403 m_creature
->setFaction(ci
->faction_A
);
408 case ACTION_T_MORPH_TO_ENTRY_OR_MODEL
:
410 if (action
.morph
.creatureId
|| action
.morph
.modelId
)
412 //set model based on entry from creature_template
413 if (action
.morph
.creatureId
)
415 if (CreatureInfo
const* ci
= GetCreatureTemplateStore(action
.morph
.creatureId
))
417 uint32 display_id
= objmgr
.ChooseDisplayId(0,ci
);
418 m_creature
->SetDisplayId(display_id
);
421 //if no param1, then use value from param2 (modelId)
423 m_creature
->SetDisplayId(action
.morph
.modelId
);
426 m_creature
->DeMorph();
430 m_creature
->PlayDirectSound(action
.sound
.soundId
);
433 m_creature
->HandleEmoteCommand(action
.emote
.emoteId
);
435 case ACTION_T_RANDOM_SOUND
:
437 int32 temp
= GetRandActionParam(rnd
, action
.random_sound
.soundId1
, action
.random_sound
.soundId2
, action
.random_sound
.soundId3
);
439 m_creature
->PlayDirectSound(temp
);
442 case ACTION_T_RANDOM_EMOTE
:
444 int32 temp
= GetRandActionParam(rnd
, action
.random_emote
.emoteId1
, action
.random_emote
.emoteId2
, action
.random_emote
.emoteId3
);
446 m_creature
->HandleEmoteCommand(temp
);
451 Unit
* target
= GetTargetByType(action
.cast
.target
, pActionInvoker
);
452 Unit
* caster
= m_creature
;
457 if (action
.cast
.castFlags
& CAST_FORCE_TARGET_SELF
)
460 //Allowed to cast only if not casting (unless we interrupt ourself) or if spell is triggered
461 bool canCast
= !caster
->IsNonMeleeSpellCasted(false) || (action
.cast
.castFlags
& (CAST_TRIGGERED
| CAST_INTURRUPT_PREVIOUS
));
463 // If cast flag CAST_AURA_NOT_PRESENT is active, check if target already has aura on them
464 if(action
.cast
.castFlags
& CAST_AURA_NOT_PRESENT
)
466 for(uint8 i
= 0; i
< 3; ++i
)
467 if(target
->HasAura(action
.cast
.spellId
, i
))
473 const SpellEntry
* tSpell
= GetSpellStore()->LookupEntry(action
.cast
.spellId
);
475 //Verify that spell exists
478 //Check if cannot cast spell
479 if (!(action
.cast
.castFlags
& (CAST_FORCE_TARGET_SELF
| CAST_FORCE_CAST
)) &&
480 !CanCast(target
, tSpell
, (action
.cast
.castFlags
& CAST_TRIGGERED
)))
482 //Melee current victim if flag not set
483 if (!(action
.cast
.castFlags
& CAST_NO_MELEE_IF_OOM
))
485 if (m_creature
->GetMotionMaster()->GetCurrentMovementGeneratorType() == TARGETED_MOTION_TYPE
)
487 AttackDistance
= 0.0f
;
490 m_creature
->GetMotionMaster()->Clear(false);
491 m_creature
->GetMotionMaster()->MoveChase(m_creature
->getVictim(), AttackDistance
, AttackAngle
);
498 //Interrupt any previous spell
499 if (caster
->IsNonMeleeSpellCasted(false) && action
.cast
.castFlags
& CAST_INTURRUPT_PREVIOUS
)
500 caster
->InterruptNonMeleeSpells(false);
502 caster
->CastSpell(target
, action
.cast
.spellId
, (action
.cast
.castFlags
& CAST_TRIGGERED
));
507 sLog
.outErrorDb("CreatureEventAI: event %d creature %d attempt to cast spell that doesn't exist %d", EventId
, m_creature
->GetEntry(), action
.cast
.spellId
);
511 case ACTION_T_SUMMON
:
513 Unit
* target
= GetTargetByType(action
.summon
.target
, pActionInvoker
);
515 Creature
* pCreature
= NULL
;
517 if (action
.summon
.duration
)
518 pCreature
= m_creature
->SummonCreature(action
.summon
.creatureId
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, TEMPSUMMON_TIMED_OR_DEAD_DESPAWN
, action
.summon
.duration
);
520 pCreature
= m_creature
->SummonCreature(action
.summon
.creatureId
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, TEMPSUMMON_TIMED_DESPAWN_OUT_OF_COMBAT
, 0);
523 sLog
.outErrorDb( "CreatureEventAI: failed to spawn creature %u. Spawn event %d is on creature %d", action
.summon
.creatureId
, EventId
, m_creature
->GetEntry());
524 else if (action
.summon
.target
!= TARGET_T_SELF
&& target
)
525 pCreature
->AI()->AttackStart(target
);
528 case ACTION_T_THREAT_SINGLE_PCT
:
529 if (Unit
* target
= GetTargetByType(action
.threat_single_pct
.target
, pActionInvoker
))
530 m_creature
->getThreatManager().modifyThreatPercent(target
, action
.threat_single_pct
.percent
);
532 case ACTION_T_THREAT_ALL_PCT
:
534 std::list
<HostilReference
*>& threatList
= m_creature
->getThreatManager().getThreatList();
535 for (std::list
<HostilReference
*>::iterator i
= threatList
.begin(); i
!= threatList
.end(); ++i
)
536 if(Unit
* Temp
= Unit::GetUnit(*m_creature
,(*i
)->getUnitGuid()))
537 m_creature
->getThreatManager().modifyThreatPercent(Temp
, action
.threat_all_pct
.percent
);
540 case ACTION_T_QUEST_EVENT
:
541 if (Unit
* target
= GetTargetByType(action
.quest_event
.target
, pActionInvoker
))
542 if (target
->GetTypeId() == TYPEID_PLAYER
)
543 ((Player
*)target
)->AreaExploredOrEventHappens(action
.quest_event
.questId
);
545 case ACTION_T_CAST_EVENT
:
546 if (Unit
* target
= GetTargetByType(action
.cast_event
.target
, pActionInvoker
))
547 if (target
->GetTypeId() == TYPEID_PLAYER
)
548 ((Player
*)target
)->CastedCreatureOrGO(action
.cast_event
.creatureId
, m_creature
->GetGUID(), action
.cast_event
.spellId
);
550 case ACTION_T_SET_UNIT_FIELD
:
552 Unit
* target
= GetTargetByType(action
.set_unit_field
.target
, pActionInvoker
);
554 // not allow modify important for integrity object fields
555 if (action
.set_unit_field
.field
< OBJECT_END
|| action
.set_unit_field
.field
>= UNIT_END
)
559 target
->SetUInt32Value(action
.set_unit_field
.field
, action
.set_unit_field
.value
);
563 case ACTION_T_SET_UNIT_FLAG
:
564 if (Unit
* target
= GetTargetByType(action
.unit_flag
.target
, pActionInvoker
))
565 target
->SetFlag(UNIT_FIELD_FLAGS
, action
.unit_flag
.value
);
567 case ACTION_T_REMOVE_UNIT_FLAG
:
568 if (Unit
* target
= GetTargetByType(action
.unit_flag
.target
, pActionInvoker
))
569 target
->RemoveFlag(UNIT_FIELD_FLAGS
, action
.unit_flag
.value
);
571 case ACTION_T_AUTO_ATTACK
:
572 MeleeEnabled
= action
.auto_attack
.state
!= 0;
574 case ACTION_T_COMBAT_MOVEMENT
:
575 // ignore no affect case
576 if(CombatMovementEnabled
==(action
.combat_movement
.state
!=0))
579 CombatMovementEnabled
= action
.combat_movement
.state
!= 0;
581 //Allow movement (create new targeted movement gen only if idle)
582 if (CombatMovementEnabled
)
584 if(action
.combat_movement
.melee
&& m_creature
->isInCombat())
585 if(Unit
* victim
= m_creature
->getVictim())
586 m_creature
->SendMeleeAttackStart(victim
);
588 if (m_creature
->GetMotionMaster()->GetCurrentMovementGeneratorType() == IDLE_MOTION_TYPE
)
590 m_creature
->GetMotionMaster()->Clear(false);
591 m_creature
->GetMotionMaster()->MoveChase(m_creature
->getVictim(), AttackDistance
, AttackAngle
);
596 if(action
.combat_movement
.melee
&& m_creature
->isInCombat())
597 if(Unit
* victim
= m_creature
->getVictim())
598 m_creature
->SendMeleeAttackStop(victim
);
600 if (m_creature
->GetMotionMaster()->GetCurrentMovementGeneratorType() == TARGETED_MOTION_TYPE
)
602 m_creature
->GetMotionMaster()->Clear(false);
603 m_creature
->GetMotionMaster()->MoveIdle();
604 m_creature
->StopMoving();
608 case ACTION_T_SET_PHASE
:
609 Phase
= action
.set_phase
.phase
;
611 case ACTION_T_INC_PHASE
:
613 int32 new_phase
= int32(Phase
)+action
.set_inc_phase
.step
;
616 sLog
.outErrorDb( "CreatureEventAI: Event %d decrease Phase under 0. CreatureEntry = %d", EventId
, m_creature
->GetEntry());
619 else if (new_phase
>= MAX_PHASE
)
621 sLog
.outErrorDb( "CreatureEventAI: Event %d incremented Phase above %u. Phase mask cannot be used with phases past %u. CreatureEntry = %d", EventId
, MAX_PHASE
-1, MAX_PHASE
-1, m_creature
->GetEntry());
632 case ACTION_T_FLEE_FOR_ASSIST
:
633 m_creature
->DoFleeToGetAssistance();
635 case ACTION_T_QUEST_EVENT_ALL
:
636 if (pActionInvoker
&& pActionInvoker
->GetTypeId() == TYPEID_PLAYER
)
638 if (Unit
* Temp
= Unit::GetUnit(*m_creature
,pActionInvoker
->GetGUID()))
639 if (Temp
->GetTypeId() == TYPEID_PLAYER
)
640 ((Player
*)Temp
)->GroupEventHappens(action
.quest_event_all
.questId
,m_creature
);
643 case ACTION_T_CAST_EVENT_ALL
:
645 std::list
<HostilReference
*>& threatList
= m_creature
->getThreatManager().getThreatList();
646 for (std::list
<HostilReference
*>::iterator i
= threatList
.begin(); i
!= threatList
.end(); ++i
)
647 if (Unit
* Temp
= Unit::GetUnit(*m_creature
,(*i
)->getUnitGuid()))
648 if (Temp
->GetTypeId() == TYPEID_PLAYER
)
649 ((Player
*)Temp
)->CastedCreatureOrGO(action
.cast_event_all
.creatureId
, m_creature
->GetGUID(), action
.cast_event_all
.spellId
);
652 case ACTION_T_REMOVEAURASFROMSPELL
:
653 if (Unit
* target
= GetTargetByType(action
.remove_aura
.target
, pActionInvoker
))
654 target
->RemoveAurasDueToSpell(action
.remove_aura
.spellId
);
656 case ACTION_T_RANGED_MOVEMENT
:
657 AttackDistance
= (float)action
.ranged_movement
.distance
;
658 AttackAngle
= action
.ranged_movement
.angle
/180.0f
*M_PI
;
660 if (CombatMovementEnabled
)
662 if (m_creature
->GetMotionMaster()->GetCurrentMovementGeneratorType() == TARGETED_MOTION_TYPE
)
664 //Drop current movement gen
665 m_creature
->GetMotionMaster()->Clear(false);
666 m_creature
->GetMotionMaster()->MoveChase(m_creature
->getVictim(), AttackDistance
, AttackAngle
);
670 case ACTION_T_RANDOM_PHASE
:
671 Phase
= GetRandActionParam(rnd
, action
.random_phase
.phase1
, action
.random_phase
.phase2
, action
.random_phase
.phase3
);
673 case ACTION_T_RANDOM_PHASE_RANGE
:
674 if (action
.random_phase_range
.phaseMax
> action
.random_phase_range
.phaseMin
)
675 Phase
= action
.random_phase_range
.phaseMin
+ (rnd
% (action
.random_phase_range
.phaseMax
- action
.random_phase_range
.phaseMin
));
677 sLog
.outErrorDb( "CreatureEventAI: ACTION_T_RANDOM_PHASE_RANGE cannot have Param2 <= Param1. Divide by Zero. Event = %d. CreatureEntry = %d", EventId
, m_creature
->GetEntry());
679 case ACTION_T_SUMMON_ID
:
681 Unit
* target
= GetTargetByType(action
.summon_id
.target
, pActionInvoker
);
683 CreatureEventAI_Summon_Map::const_iterator i
= CreatureEAI_Mgr
.GetCreatureEventAISummonMap().find(action
.summon_id
.spawnId
);
684 if (i
== CreatureEAI_Mgr
.GetCreatureEventAISummonMap().end())
686 sLog
.outErrorDb( "CreatureEventAI: failed to spawn creature %u. Summon map index %u does not exist. EventID %d. CreatureID %d", action
.summon_id
.creatureId
, action
.summon_id
.spawnId
, EventId
, m_creature
->GetEntry());
690 Creature
* pCreature
= NULL
;
691 if ((*i
).second
.SpawnTimeSecs
)
692 pCreature
= m_creature
->SummonCreature(action
.summon_id
.creatureId
, (*i
).second
.position_x
, (*i
).second
.position_y
, (*i
).second
.position_z
, (*i
).second
.orientation
, TEMPSUMMON_TIMED_OR_DEAD_DESPAWN
, (*i
).second
.SpawnTimeSecs
);
694 pCreature
= m_creature
->SummonCreature(action
.summon_id
.creatureId
, (*i
).second
.position_x
, (*i
).second
.position_y
, (*i
).second
.position_z
, (*i
).second
.orientation
, TEMPSUMMON_TIMED_DESPAWN_OUT_OF_COMBAT
, 0);
697 sLog
.outErrorDb( "CreatureEventAI: failed to spawn creature %u. EventId %d.Creature %d", action
.summon_id
.creatureId
, EventId
, m_creature
->GetEntry());
698 else if (action
.summon_id
.target
!= TARGET_T_SELF
&& target
)
699 pCreature
->AI()->AttackStart(target
);
703 case ACTION_T_KILLED_MONSTER
:
704 //first attempt player who tapped creature
705 if (Player
* pPlayer
= m_creature
->GetLootRecipient())
706 pPlayer
->RewardPlayerAndGroupAtEvent(action
.killed_monster
.creatureId
, m_creature
);
709 //if not available, use pActionInvoker
710 if (Unit
* pTarget
= GetTargetByType(action
.killed_monster
.target
, pActionInvoker
))
711 if (Player
* pPlayer2
= pTarget
->GetCharmerOrOwnerPlayerOrPlayerItself())
712 pPlayer2
->RewardPlayerAndGroupAtEvent(action
.killed_monster
.creatureId
, m_creature
);
715 case ACTION_T_SET_INST_DATA
:
717 InstanceData
* pInst
= (InstanceData
*)m_creature
->GetInstanceData();
720 sLog
.outErrorDb("CreatureEventAI: Event %d attempt to set instance data without instance script. Creature %d", EventId
, m_creature
->GetEntry());
724 pInst
->SetData(action
.set_inst_data
.field
, action
.set_inst_data
.value
);
727 case ACTION_T_SET_INST_DATA64
:
729 Unit
* target
= GetTargetByType(action
.set_inst_data64
.target
, pActionInvoker
);
732 sLog
.outErrorDb("CreatureEventAI: Event %d attempt to set instance data64 but Target == NULL. Creature %d", EventId
, m_creature
->GetEntry());
736 InstanceData
* pInst
= (InstanceData
*)m_creature
->GetInstanceData();
739 sLog
.outErrorDb("CreatureEventAI: Event %d attempt to set instance data64 without instance script. Creature %d", EventId
, m_creature
->GetEntry());
743 pInst
->SetData64(action
.set_inst_data64
.field
, target
->GetGUID());
746 case ACTION_T_UPDATE_TEMPLATE
:
747 if (m_creature
->GetEntry() == action
.update_template
.creatureId
)
750 sLog
.outErrorDb("CreatureEventAI: Event %d ACTION_T_UPDATE_TEMPLATE call with param1 == current entry. Creature %d", EventId
, m_creature
->GetEntry());
754 m_creature
->UpdateEntry(action
.update_template
.creatureId
, action
.update_template
.team
? HORDE
: ALLIANCE
);
757 if (m_creature
->isDead())
760 sLog
.outErrorDb("CreatureEventAI: Event %d ACTION_T_DIE on dead creature. Creature %d", EventId
, m_creature
->GetEntry());
763 m_creature
->DealDamage(m_creature
, m_creature
->GetMaxHealth(),NULL
, DIRECT_DAMAGE
, SPELL_SCHOOL_MASK_NORMAL
, NULL
, false);
765 case ACTION_T_ZONE_COMBAT_PULSE
:
767 m_creature
->SetInCombatWithZone();
770 case ACTION_T_CALL_FOR_HELP
:
772 m_creature
->CallForHelp(action
.call_for_help
.radius
);
775 case ACTION_T_SET_SHEATH
:
777 m_creature
->SetSheath(SheathState(action
.set_sheath
.sheath
));
780 case ACTION_T_FORCE_DESPAWN
:
782 m_creature
->ForcedDespawn();
785 case ACTION_T_SET_INVINCIBILITY_HP_LEVEL
:
787 if(action
.invincibility_hp_level
.is_percent
)
788 InvinceabilityHpLevel
= m_creature
->GetMaxHealth()*action
.invincibility_hp_level
.hp_level
/100;
790 InvinceabilityHpLevel
= action
.invincibility_hp_level
.hp_level
;
796 void CreatureEventAI::JustRespawned()
803 //Handle Spawned Events
804 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
805 if (SpawnedEventConditionsCheck((*i
).Event
))
809 void CreatureEventAI::Reset()
811 EventUpdateTime
= EVENT_UPDATE_TIME
;
817 //Reset all events to enabled
818 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
820 CreatureEventAI_Event
const& event
= (*i
).Event
;
821 switch (event
.event_type
)
823 //Reset all out of combat timers
824 case EVENT_T_TIMER_OOC
:
826 if ((*i
).UpdateRepeatTimer(m_creature
,event
.timer
.initialMin
,event
.timer
.initialMax
))
831 //TODO: enable below code line / verify this is correct to enable events previously disabled (ex. aggro yell), instead of enable this in void Aggro()
832 //(*i).Enabled = true;
839 void CreatureEventAI::JustReachedHome()
841 m_creature
->LoadCreaturesAddon();
845 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
847 if ((*i
).Event
.event_type
== EVENT_T_REACHED_HOME
)
855 void CreatureEventAI::EnterEvadeMode()
857 m_creature
->RemoveAllAuras();
858 m_creature
->DeleteThreatList();
859 m_creature
->CombatStop(true);
861 if (m_creature
->isAlive())
862 m_creature
->GetMotionMaster()->MoveTargetedHome();
864 m_creature
->SetLootRecipient(NULL
);
869 //Handle Evade events
870 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
872 if ((*i
).Event
.event_type
== EVENT_T_EVADE
)
877 void CreatureEventAI::JustDied(Unit
* killer
)
884 //Handle Evade events
885 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
887 if ((*i
).Event
.event_type
== EVENT_T_DEATH
)
888 ProcessEvent(*i
, killer
);
891 // reset phase after any death state events
895 void CreatureEventAI::KilledUnit(Unit
* victim
)
897 if (bEmptyList
|| victim
->GetTypeId() != TYPEID_PLAYER
)
900 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
902 if ((*i
).Event
.event_type
== EVENT_T_KILL
)
903 ProcessEvent(*i
, victim
);
907 void CreatureEventAI::JustSummoned(Creature
* pUnit
)
909 if (bEmptyList
|| !pUnit
)
912 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
914 if ((*i
).Event
.event_type
== EVENT_T_SUMMONED_UNIT
)
915 ProcessEvent(*i
, pUnit
);
919 void CreatureEventAI::EnterCombat(Unit
*enemy
)
921 //Check for on combat start events
924 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
926 CreatureEventAI_Event
const& event
= (*i
).Event
;
927 switch (event
.event_type
)
931 ProcessEvent(*i
, enemy
);
933 //Reset all in combat timers
935 if ((*i
).UpdateRepeatTimer(m_creature
,event
.timer
.initialMin
,event
.timer
.initialMax
))
938 //All normal events need to be re-enabled and their time set to 0
947 EventUpdateTime
= EVENT_UPDATE_TIME
;
951 void CreatureEventAI::AttackStart(Unit
*who
)
956 if (m_creature
->Attack(who
, MeleeEnabled
))
958 m_creature
->AddThreat(who
, 0.0f
);
959 m_creature
->SetInCombatWith(who
);
960 who
->SetInCombatWith(m_creature
);
962 if (CombatMovementEnabled
)
964 m_creature
->GetMotionMaster()->MoveChase(who
, AttackDistance
, AttackAngle
);
968 m_creature
->GetMotionMaster()->MoveIdle();
969 m_creature
->StopMoving();
974 void CreatureEventAI::MoveInLineOfSight(Unit
*who
)
979 //Check for OOC LOS Event
980 if (!bEmptyList
&& !m_creature
->getVictim())
982 for (std::list
<CreatureEventAIHolder
>::iterator itr
= CreatureEventAIList
.begin(); itr
!= CreatureEventAIList
.end(); ++itr
)
984 if ((*itr
).Event
.event_type
== EVENT_T_OOC_LOS
)
986 //can trigger if closer than fMaxAllowedRange
987 float fMaxAllowedRange
= (*itr
).Event
.ooc_los
.maxRange
;
989 //if range is ok and we are actually in LOS
990 if (m_creature
->IsWithinDistInMap(who
, fMaxAllowedRange
) && m_creature
->IsWithinLOSInMap(who
))
992 //if friendly event&&who is not hostile OR hostile event&&who is hostile
993 if (((*itr
).Event
.ooc_los
.noHostile
&& !m_creature
->IsHostileTo(who
)) ||
994 ((!(*itr
).Event
.ooc_los
.noHostile
) && m_creature
->IsHostileTo(who
)))
995 ProcessEvent(*itr
, who
);
1001 if (m_creature
->isCivilian() || m_creature
->IsNeutralToAll())
1004 if (!m_creature
->hasUnitState(UNIT_STAT_STUNNED
) && who
->isTargetableForAttack() &&
1005 m_creature
->IsHostileTo(who
) && who
->isInAccessablePlaceFor(m_creature
))
1007 if (!m_creature
->canFly() && m_creature
->GetDistanceZ(who
) > CREATURE_Z_ATTACK_RANGE
)
1010 float attackRadius
= m_creature
->GetAttackDistance(who
);
1011 if (m_creature
->IsWithinDistInMap(who
, attackRadius
) && m_creature
->IsWithinLOSInMap(who
))
1013 if (!m_creature
->getVictim())
1016 who
->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH
);
1018 else if (m_creature
->GetMap()->IsDungeon())
1020 m_creature
->AddThreat(who
, 0.0f
);
1021 who
->SetInCombatWith(m_creature
);
1027 void CreatureEventAI::SpellHit(Unit
* pUnit
, const SpellEntry
* pSpell
)
1033 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
1034 if ((*i
).Event
.event_type
== EVENT_T_SPELLHIT
)
1035 //If spell id matches (or no spell id) & if spell school matches (or no spell school)
1036 if (!(*i
).Event
.spell_hit
.spellId
|| pSpell
->Id
== (*i
).Event
.spell_hit
.spellId
)
1037 if (pSpell
->SchoolMask
& (*i
).Event
.spell_hit
.schoolMask
)
1038 ProcessEvent(*i
, pUnit
);
1041 void CreatureEventAI::UpdateAI(const uint32 diff
)
1043 //Check if we are in combat (also updates calls threat update code)
1044 bool Combat
= m_creature
->SelectHostilTarget() && m_creature
->getVictim();
1046 //Must return if creature isn't alive. Normally select hostil target and get victim prevent this
1047 if (!m_creature
->isAlive())
1052 //Events are only updated once every EVENT_UPDATE_TIME ms to prevent lag with large amount of events
1053 if (EventUpdateTime
< diff
)
1057 //Check for time based events
1058 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
1063 if ((*i
).Time
> EventDiff
)
1065 //Do not decrement timers if event cannot trigger in this phase
1066 if (!((*i
).Event
.event_inverse_phase_mask
& (1 << Phase
)))
1067 (*i
).Time
-= EventDiff
;
1069 //Skip processing of events that have time remaining
1075 //Events that are updated every EVENT_UPDATE_TIME
1076 switch ((*i
).Event
.event_type
)
1078 case EVENT_T_TIMER_OOC
:
1084 case EVENT_T_TARGET_HP
:
1085 case EVENT_T_TARGET_CASTING
:
1086 case EVENT_T_FRIENDLY_HP
:
1093 if (m_creature
->getVictim() && m_creature
->IsInMap(m_creature
->getVictim()))
1094 if (m_creature
->IsInRange(m_creature
->getVictim(), (float)(*i
).Event
.range
.minDist
, (float)(*i
).Event
.range
.maxDist
))
1102 EventUpdateTime
= EVENT_UPDATE_TIME
;
1107 EventUpdateTime
-= diff
;
1112 if (Combat
&& MeleeEnabled
)
1113 DoMeleeAttackIfReady();
1116 bool CreatureEventAI::IsVisible(Unit
*pl
) const
1118 return m_creature
->IsWithinDist(pl
,sWorld
.getConfig(CONFIG_SIGHT_MONSTER
))
1119 && pl
->isVisibleForOrDetect(m_creature
,m_creature
,true);
1122 inline Unit
* CreatureEventAI::SelectUnit(AttackingTarget target
, uint32 position
)
1124 //ThreatList m_threatlist;
1125 std::list
<HostilReference
*>& m_threatlist
= m_creature
->getThreatManager().getThreatList();
1126 std::list
<HostilReference
*>::iterator i
= m_threatlist
.begin();
1127 std::list
<HostilReference
*>::reverse_iterator r
= m_threatlist
.rbegin();
1129 if (position
>= m_threatlist
.size() || !m_threatlist
.size())
1134 case ATTACKING_TARGET_RANDOM
:
1136 advance ( i
, position
+ (rand() % (m_threatlist
.size() - position
) ));
1137 return Unit::GetUnit(*m_creature
,(*i
)->getUnitGuid());
1139 case ATTACKING_TARGET_TOPAGGRO
:
1141 advance ( i
, position
);
1142 return Unit::GetUnit(*m_creature
,(*i
)->getUnitGuid());
1144 case ATTACKING_TARGET_BOTTOMAGGRO
:
1146 advance ( r
, position
);
1147 return Unit::GetUnit(*m_creature
,(*r
)->getUnitGuid());
1153 inline uint32
CreatureEventAI::GetRandActionParam(uint32 rnd
, uint32 param1
, uint32 param2
, uint32 param3
)
1157 case 0: return param1
;
1158 case 1: return param2
;
1159 case 2: return param3
;
1164 inline int32
CreatureEventAI::GetRandActionParam(uint32 rnd
, int32 param1
, int32 param2
, int32 param3
)
1168 case 0: return param1
;
1169 case 1: return param2
;
1170 case 2: return param3
;
1175 inline Unit
* CreatureEventAI::GetTargetByType(uint32 Target
, Unit
* pActionInvoker
)
1181 case TARGET_T_HOSTILE
:
1182 return m_creature
->getVictim();
1183 case TARGET_T_HOSTILE_SECOND_AGGRO
:
1184 return SelectUnit(ATTACKING_TARGET_TOPAGGRO
,1);
1185 case TARGET_T_HOSTILE_LAST_AGGRO
:
1186 return SelectUnit(ATTACKING_TARGET_BOTTOMAGGRO
,0);
1187 case TARGET_T_HOSTILE_RANDOM
:
1188 return SelectUnit(ATTACKING_TARGET_RANDOM
,0);
1189 case TARGET_T_HOSTILE_RANDOM_NOT_TOP
:
1190 return SelectUnit(ATTACKING_TARGET_RANDOM
,1);
1191 case TARGET_T_ACTION_INVOKER
:
1192 return pActionInvoker
;
1198 Unit
* CreatureEventAI::DoSelectLowestHpFriendly(float range
, uint32 MinHPDiff
)
1200 CellPair
p(MaNGOS::ComputeCellPair(m_creature
->GetPositionX(), m_creature
->GetPositionY()));
1202 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1207 MaNGOS::MostHPMissingInRange
u_check(m_creature
, range
, MinHPDiff
);
1208 MaNGOS::UnitLastSearcher
<MaNGOS::MostHPMissingInRange
> searcher(m_creature
, pUnit
, u_check
);
1211 typedef TYPELIST_4(GameObject, Creature*except pets*, DynamicObject, Corpse*Bones*) AllGridObjectTypes;
1212 This means that if we only search grid then we cannot possibly return pets or players so this is safe
1214 TypeContainerVisitor
<MaNGOS::UnitLastSearcher
<MaNGOS::MostHPMissingInRange
>, GridTypeMapContainer
> grid_unit_searcher(searcher
);
1216 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1217 cell_lock
->Visit(cell_lock
, grid_unit_searcher
, *m_creature
->GetMap());
1221 void CreatureEventAI::DoFindFriendlyCC(std::list
<Creature
*>& _list
, float range
)
1223 CellPair
p(MaNGOS::ComputeCellPair(m_creature
->GetPositionX(), m_creature
->GetPositionY()));
1225 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1228 MaNGOS::FriendlyCCedInRange
u_check(m_creature
, range
);
1229 MaNGOS::CreatureListSearcher
<MaNGOS::FriendlyCCedInRange
> searcher(m_creature
, _list
, u_check
);
1231 TypeContainerVisitor
<MaNGOS::CreatureListSearcher
<MaNGOS::FriendlyCCedInRange
>, GridTypeMapContainer
> grid_creature_searcher(searcher
);
1233 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1234 cell_lock
->Visit(cell_lock
, grid_creature_searcher
, *m_creature
->GetMap());
1237 void CreatureEventAI::DoFindFriendlyMissingBuff(std::list
<Creature
*>& _list
, float range
, uint32 spellid
)
1239 CellPair
p(MaNGOS::ComputeCellPair(m_creature
->GetPositionX(), m_creature
->GetPositionY()));
1241 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1244 MaNGOS::FriendlyMissingBuffInRange
u_check(m_creature
, range
, spellid
);
1245 MaNGOS::CreatureListSearcher
<MaNGOS::FriendlyMissingBuffInRange
> searcher(m_creature
, _list
, u_check
);
1247 TypeContainerVisitor
<MaNGOS::CreatureListSearcher
<MaNGOS::FriendlyMissingBuffInRange
>, GridTypeMapContainer
> grid_creature_searcher(searcher
);
1249 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1250 cell_lock
->Visit(cell_lock
, grid_creature_searcher
, *m_creature
->GetMap());
1253 //*********************************
1254 //*** Functions used globally ***
1256 void CreatureEventAI::DoScriptText(int32 textEntry
, WorldObject
* pSource
, Unit
* target
)
1260 sLog
.outErrorDb("CreatureEventAI: DoScriptText entry %i, invalid Source pointer.",textEntry
);
1266 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
);
1270 CreatureEventAI_TextMap::const_iterator i
= CreatureEAI_Mgr
.GetCreatureEventAITextMap().find(textEntry
);
1272 if (i
== CreatureEAI_Mgr
.GetCreatureEventAITextMap().end())
1274 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
);
1278 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
);
1280 if((*i
).second
.SoundId
)
1282 if (GetSoundEntriesStore()->LookupEntry((*i
).second
.SoundId
))
1283 pSource
->PlayDirectSound((*i
).second
.SoundId
);
1285 sLog
.outErrorDb("CreatureEventAI: DoScriptText entry %i tried to process invalid sound id %u.",textEntry
,(*i
).second
.SoundId
);
1288 if((*i
).second
.Emote
)
1290 if (pSource
->GetTypeId() == TYPEID_UNIT
|| pSource
->GetTypeId() == TYPEID_PLAYER
)
1292 ((Unit
*)pSource
)->HandleEmoteCommand((*i
).second
.Emote
);
1295 sLog
.outErrorDb("CreatureEventAI: DoScriptText entry %i tried to process emote for invalid TypeId (%u).",textEntry
,pSource
->GetTypeId());
1298 switch((*i
).second
.Type
)
1301 pSource
->MonsterSay(textEntry
, (*i
).second
.Language
, target
? target
->GetGUID() : 0);
1303 case CHAT_TYPE_YELL
:
1304 pSource
->MonsterYell(textEntry
, (*i
).second
.Language
, target
? target
->GetGUID() : 0);
1306 case CHAT_TYPE_TEXT_EMOTE
:
1307 pSource
->MonsterTextEmote(textEntry
, target
? target
->GetGUID() : 0);
1309 case CHAT_TYPE_BOSS_EMOTE
:
1310 pSource
->MonsterTextEmote(textEntry
, target
? target
->GetGUID() : 0, true);
1312 case CHAT_TYPE_WHISPER
:
1314 if (target
&& target
->GetTypeId() == TYPEID_PLAYER
)
1315 pSource
->MonsterWhisper(textEntry
, target
->GetGUID());
1316 else sLog
.outErrorDb("CreatureEventAI: DoScriptText entry %i cannot whisper without target unit (TYPEID_PLAYER).", textEntry
);
1318 case CHAT_TYPE_BOSS_WHISPER
:
1320 if (target
&& target
->GetTypeId() == TYPEID_PLAYER
)
1321 pSource
->MonsterWhisper(textEntry
, target
->GetGUID(), true);
1322 else sLog
.outErrorDb("CreatureEventAI: DoScriptText entry %i cannot whisper without target unit (TYPEID_PLAYER).", textEntry
);
1324 case CHAT_TYPE_ZONE_YELL
:
1325 pSource
->MonsterYellToZone(textEntry
, (*i
).second
.Language
, target
? target
->GetGUID() : 0);
1330 void CreatureEventAI::DoMeleeAttackIfReady()
1332 //Make sure our attack is ready before checking distance
1333 if (m_creature
->isAttackReady())
1335 //If we are within range melee the target
1336 if (m_creature
->IsWithinDistInMap(m_creature
->getVictim(), ATTACK_DISTANCE
))
1338 m_creature
->AttackerStateUpdate(m_creature
->getVictim());
1339 m_creature
->resetAttackTimer();
1344 bool CreatureEventAI::CanCast(Unit
* Target
, SpellEntry
const *Spell
, bool Triggered
)
1346 //No target so we can't cast
1347 if (!Target
|| !Spell
)
1350 //Silenced so we can't cast
1351 if (!Triggered
&& m_creature
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_SILENCED
))
1355 if (!Triggered
&& m_creature
->GetPower((Powers
)Spell
->powerType
) < Spell
->manaCost
)
1358 SpellRangeEntry
const *TempRange
= NULL
;
1360 TempRange
= GetSpellRangeStore()->LookupEntry(Spell
->rangeIndex
);
1362 //Spell has invalid range store so we can't use it
1366 //Unit is out of range of this spell
1367 if (!m_creature
->IsInRange(Target
,TempRange
->minRange
,TempRange
->maxRange
))
1373 void CreatureEventAI::ReceiveEmote(Player
* pPlayer
, uint32 text_emote
)
1378 for (std::list
<CreatureEventAIHolder
>::iterator itr
= CreatureEventAIList
.begin(); itr
!= CreatureEventAIList
.end(); ++itr
)
1380 if ((*itr
).Event
.event_type
== EVENT_T_RECEIVE_EMOTE
)
1382 if ((*itr
).Event
.receive_emote
.emoteId
!= text_emote
)
1385 PlayerCondition
pcon((*itr
).Event
.receive_emote
.condition
,(*itr
).Event
.receive_emote
.conditionValue1
,(*itr
).Event
.receive_emote
.conditionValue2
);
1386 if (pcon
.Meets(pPlayer
))
1388 sLog
.outDebug("CreatureEventAI: ReceiveEmote CreatureEventAI: Condition ok, processing");
1389 ProcessEvent(*itr
, pPlayer
);
1395 void CreatureEventAI::DamageTaken( Unit
* done_by
, uint32
& damage
)
1397 if(InvinceabilityHpLevel
> 0 && m_creature
->GetHealth() < InvinceabilityHpLevel
+damage
)
1399 if(m_creature
->GetHealth() <= InvinceabilityHpLevel
)
1402 damage
= m_creature
->GetHealth() - InvinceabilityHpLevel
;
1406 bool CreatureEventAI::SpawnedEventConditionsCheck(CreatureEventAI_Event
const& event
)
1408 if(event
.event_type
!= EVENT_T_SPAWNED
)
1411 switch (event
.spawned
.condition
)
1413 case SPAWNED_EVENT_ALWAY
:
1416 case SPAWNED_EVENT_MAP
:
1418 return m_creature
->GetMapId() == event
.spawned
.conditionValue1
;
1419 case SPAWNED_EVENT_ZONE
:
1423 m_creature
->GetZoneAndAreaId(zone
,area
);
1424 return zone
== event
.spawned
.conditionValue1
|| area
== event
.spawned
.conditionValue1
;