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 // Need make copy for filter unneeded steps and safe in case table reload
43 CreatureEventAI_Event_Map::const_iterator CreatureEvents
= CreatureEAI_Mgr
.GetCreatureEventAIMap().find(m_creature
->GetEntry());
44 if (CreatureEvents
!= CreatureEAI_Mgr
.GetCreatureEventAIMap().end())
46 std::vector
<CreatureEventAI_Event
>::const_iterator i
;
47 for (i
= (*CreatureEvents
).second
.begin(); i
!= (*CreatureEvents
).second
.end(); ++i
)
52 if ((*i
).event_flags
& EFLAG_DEBUG_ONLY
)
55 if (m_creature
->GetMap()->IsDungeon())
57 if( (m_creature
->GetMap()->IsHeroic() && (*i
).event_flags
& EFLAG_HEROIC
) ||
58 (!m_creature
->GetMap()->IsHeroic() && (*i
).event_flags
& EFLAG_NORMAL
))
60 //event flagged for instance mode
61 CreatureEventAIList
.push_back(CreatureEventAIHolder(*i
));
65 CreatureEventAIList
.push_back(CreatureEventAIHolder(*i
));
67 //EventMap had events but they were not added because they must be for instance
68 if (CreatureEventAIList
.empty())
69 sLog
.outError("CreatureEventAI: Creature %u has events but no events added to list because of instance flags.", m_creature
->GetEntry());
72 sLog
.outError("CreatureEventAI: EventMap for Creature %u is empty but creature is using CreatureEventAI.", m_creature
->GetEntry());
74 bEmptyList
= CreatureEventAIList
.empty();
76 CombatMovementEnabled
= true;
81 //Handle Spawned Events
84 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
86 if ((*i
).Event
.event_type
== EVENT_T_SPAWNED
)
93 bool CreatureEventAI::ProcessEvent(CreatureEventAIHolder
& pHolder
, Unit
* pActionInvoker
)
95 if (!pHolder
.Enabled
|| pHolder
.Time
)
98 //Check the inverse phase mask (event doesn't trigger if current phase bit is set in mask)
99 if (pHolder
.Event
.event_inverse_phase_mask
& (1 << Phase
))
102 //Store random here so that all random actions match up
105 //Return if chance for event is not met
106 if (pHolder
.Event
.event_chance
<= rnd
% 100)
109 uint32 param1
= pHolder
.Event
.event_param1
;
110 uint32 param2
= pHolder
.Event
.event_param2
;
111 uint32 param3
= pHolder
.Event
.event_param3
;
112 uint32 param4
= pHolder
.Event
.event_param4
;
114 //Check event conditions based on the event type, also reset events
115 switch (pHolder
.Event
.event_type
)
119 if (!m_creature
->isInCombat())
123 if (param3
== param4
)
125 pHolder
.Time
= param3
;
127 }else if (param4
> param3
)
128 pHolder
.Time
= urand(param3
, param4
);
131 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
);
132 pHolder
.Enabled
= false;
136 case EVENT_T_TIMER_OOC
:
138 if (m_creature
->isInCombat())
142 if (param3
== param4
)
144 pHolder
.Time
= param3
;
146 }else if (param4
> param3
)
147 pHolder
.Time
= urand(param3
, param4
);
151 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
);
152 pHolder
.Enabled
= false;
158 if (!m_creature
->isInCombat() || !m_creature
->GetMaxHealth())
161 uint32 perc
= (m_creature
->GetHealth()*100) / m_creature
->GetMaxHealth();
163 if (perc
> param1
|| perc
< param2
)
167 if (param3
== param4
)
169 pHolder
.Time
= param3
;
171 }else if (param4
> param3
)
172 pHolder
.Time
= urand(param3
, param4
);
176 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
);
177 pHolder
.Enabled
= false;
183 if (!m_creature
->isInCombat() || !m_creature
->GetMaxPower(POWER_MANA
))
186 uint32 perc
= (m_creature
->GetPower(POWER_MANA
)*100) / m_creature
->GetMaxPower(POWER_MANA
);
188 if (perc
> param1
|| perc
< param2
)
192 if (param3
== param4
)
194 pHolder
.Time
= param3
;
196 }else if (param4
> param3
)
197 pHolder
.Time
= urand(param3
, param4
);
201 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
);
202 pHolder
.Enabled
= false;
213 if (param1
== param2
)
215 pHolder
.Time
= param1
;
217 }else if (param2
> param1
)
218 pHolder
.Time
= urand(param1
, param2
);
222 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
);
223 pHolder
.Enabled
= false;
234 case EVENT_T_SPELLHIT
:
236 //Spell hit is special case, param1 and param2 handled within CreatureEventAI::SpellHit
239 if (param3
== param4
)
241 pHolder
.Time
= param3
;
243 }else if (param4
> param3
)
244 pHolder
.Time
= urand(param3
, param4
);
248 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
);
249 pHolder
.Enabled
= false;
256 if (param3
== param4
)
258 pHolder
.Time
= param3
;
260 }else if (param4
> param3
)
261 pHolder
.Time
= urand(param3
, param4
);
265 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
);
266 pHolder
.Enabled
= false;
270 case EVENT_T_OOC_LOS
:
273 if (param3
== param4
)
275 pHolder
.Time
= param3
;
277 }else if (param4
> param3
)
278 pHolder
.Time
= urand(param3
, param4
);
282 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
);
283 pHolder
.Enabled
= false;
287 case EVENT_T_SPAWNED
:
291 case EVENT_T_TARGET_HP
:
293 if (!m_creature
->isInCombat() || !m_creature
->getVictim() || !m_creature
->getVictim()->GetMaxHealth())
296 uint32 perc
= (m_creature
->getVictim()->GetHealth()*100) / m_creature
->getVictim()->GetMaxHealth();
298 if (perc
> param1
|| perc
< param2
)
302 if (param3
== param4
)
304 pHolder
.Time
= param3
;
306 }else if (param4
> param3
)
307 pHolder
.Time
= urand(param3
, param4
);
311 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
);
312 pHolder
.Enabled
= false;
316 case EVENT_T_TARGET_CASTING
:
318 if (!m_creature
->isInCombat() || !m_creature
->getVictim() || !m_creature
->getVictim()->IsNonMeleeSpellCasted(false, false, true))
322 if (param1
== param2
)
324 pHolder
.Time
= param1
;
326 }else if (param2
> param1
)
327 pHolder
.Time
= urand(param1
, param2
);
331 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
);
332 pHolder
.Enabled
= false;
336 case EVENT_T_FRIENDLY_HP
:
338 if (!m_creature
->isInCombat())
341 Unit
* pUnit
= DoSelectLowestHpFriendly(param2
, param1
);
346 pActionInvoker
= pUnit
;
349 if (param3
== param4
)
351 pHolder
.Time
= param3
;
353 }else if (param4
> param3
)
354 pHolder
.Time
= urand(param3
, param4
);
358 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
);
359 pHolder
.Enabled
= false;
363 case EVENT_T_FRIENDLY_IS_CC
:
365 if (!m_creature
->isInCombat())
368 std::list
<Creature
*> pList
;
369 DoFindFriendlyCC(pList
, param2
);
375 //We don't really care about the whole list, just return first available
376 pActionInvoker
= *(pList
.begin());
379 if (param3
== param4
)
381 pHolder
.Time
= param3
;
383 }else if (param4
> param3
)
384 pHolder
.Time
= urand(param3
, param4
);
387 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
);
388 pHolder
.Enabled
= false;
392 case EVENT_T_FRIENDLY_MISSING_BUFF
:
394 std::list
<Creature
*> pList
;
395 DoFindFriendlyMissingBuff(pList
, param2
, param1
);
401 //We don't really care about the whole list, just return first available
402 pActionInvoker
= *(pList
.begin());
405 if (param3
== param4
)
407 pHolder
.Time
= param3
;
409 }else if (param4
> param3
)
410 pHolder
.Time
= urand(param3
, param4
);
414 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
);
415 pHolder
.Enabled
= false;
419 case EVENT_T_SUMMONED_UNIT
:
421 //Prevent event from occuring on no unit or non creatures
422 if (!pActionInvoker
|| pActionInvoker
->GetTypeId()!=TYPEID_UNIT
)
425 //Creature id doesn't match up
426 if (param1
&& ((Creature
*)pActionInvoker
)->GetEntry() != param1
)
430 if (param2
== param3
)
432 pHolder
.Time
= param2
;
434 }else if (param3
> param2
)
435 pHolder
.Time
= urand(param2
, param3
);
439 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
);
440 pHolder
.Enabled
= false;
444 case EVENT_T_REACHED_HOME
:
448 case EVENT_T_RECEIVE_EMOTE
:
454 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
);
458 //Disable non-repeatable events
459 if (!(pHolder
.Event
.event_flags
& EFLAG_REPEATABLE
))
460 pHolder
.Enabled
= false;
463 for (uint32 j
= 0; j
< MAX_ACTIONS
; j
++)
464 ProcessAction(pHolder
.Event
.action
[j
], rnd
, pHolder
.Event
.event_id
, pActionInvoker
);
469 void CreatureEventAI::ProcessAction(CreatureEventAI_Action
const& action
, uint32 rnd
, uint32 EventId
, Unit
* pActionInvoker
)
475 if (!action
.text
.TextId1
)
480 if (action
.text
.TextId2
&& action
.text
.TextId3
)
484 case 0: temp
= action
.text
.TextId1
; break;
485 case 2: temp
= action
.text
.TextId2
; break;
486 case 3: temp
= action
.text
.TextId3
; break;
489 else if (action
.text
.TextId2
&& urand(0,1))
490 temp
= action
.text
.TextId2
;
492 temp
= action
.text
.TextId1
;
500 if (pActionInvoker
->GetTypeId() == TYPEID_PLAYER
)
501 target
= pActionInvoker
;
502 else if (Unit
* owner
= pActionInvoker
->GetOwner())
504 if (owner
->GetTypeId() == TYPEID_PLAYER
)
508 else if (target
= m_creature
->getVictim())
510 if (target
->GetTypeId() != TYPEID_PLAYER
)
511 if (Unit
* owner
= target
->GetOwner())
512 if (owner
->GetTypeId() == TYPEID_PLAYER
)
516 DoScriptText(temp
, m_creature
, target
);
520 case ACTION_T_SET_FACTION
:
522 if (action
.set_faction
.factionId
)
523 m_creature
->setFaction(action
.set_faction
.factionId
);
526 if (CreatureInfo
const* ci
= GetCreatureTemplateStore(m_creature
->GetEntry()))
528 //if no id provided, assume reset and then use default
529 if (m_creature
->getFaction() != ci
->faction_A
)
530 m_creature
->setFaction(ci
->faction_A
);
535 case ACTION_T_MORPH_TO_ENTRY_OR_MODEL
:
537 if (action
.morph
.creatireId
|| action
.morph
.modelId
)
539 //set model based on entry from creature_template
540 if (action
.morph
.creatireId
)
542 if (CreatureInfo
const* ci
= GetCreatureTemplateStore(action
.morph
.creatireId
))
544 //use default display
546 m_creature
->SetDisplayId(ci
->DisplayID_A
);
549 //if no param1, then use value from param2 (modelId)
551 m_creature
->SetDisplayId(action
.morph
.modelId
);
554 m_creature
->DeMorph();
558 m_creature
->PlayDirectSound(action
.sound
.soundId
);
561 m_creature
->HandleEmoteCommand(action
.emote
.emoteId
);
563 case ACTION_T_RANDOM_SOUND
:
565 int32 temp
= GetRandActionParam(rnd
, action
.random_sound
.soundId1
, action
.random_sound
.soundId2
, action
.random_sound
.soundId3
);
567 m_creature
->PlayDirectSound(temp
);
570 case ACTION_T_RANDOM_EMOTE
:
572 int32 temp
= GetRandActionParam(rnd
, action
.random_emote
.emoteId1
, action
.random_emote
.emoteId2
, action
.random_emote
.emoteId3
);
574 m_creature
->HandleEmoteCommand(temp
);
579 Unit
* target
= GetTargetByType(action
.cast
.target
, pActionInvoker
);
580 Unit
* caster
= m_creature
;
585 if (action
.cast
.castFlags
& CAST_FORCE_TARGET_SELF
)
588 //Allowed to cast only if not casting (unless we interrupt ourself) or if spell is triggered
589 bool canCast
= !caster
->IsNonMeleeSpellCasted(false) || (action
.cast
.castFlags
& (CAST_TRIGGERED
| CAST_INTURRUPT_PREVIOUS
));
591 // If cast flag CAST_AURA_NOT_PRESENT is active, check if target already has aura on them
592 if(action
.cast
.castFlags
& CAST_AURA_NOT_PRESENT
)
594 for(uint8 i
= 0; i
< 3; ++i
)
595 if(target
->HasAura(action
.cast
.spellId
, i
))
601 const SpellEntry
* tSpell
= GetSpellStore()->LookupEntry(action
.cast
.spellId
);
603 //Verify that spell exists
606 //Check if cannot cast spell
607 if (!(action
.cast
.castFlags
& (CAST_FORCE_TARGET_SELF
| CAST_FORCE_CAST
)) &&
608 !CanCast(target
, tSpell
, (action
.cast
.castFlags
& CAST_TRIGGERED
)))
610 //Melee current victim if flag not set
611 if (!(action
.cast
.castFlags
& CAST_NO_MELEE_IF_OOM
))
613 if (m_creature
->GetMotionMaster()->GetCurrentMovementGeneratorType() == TARGETED_MOTION_TYPE
)
618 m_creature
->GetMotionMaster()->Clear(false);
619 m_creature
->GetMotionMaster()->MoveChase(m_creature
->getVictim(), AttackDistance
, AttackAngle
);
626 //Interrupt any previous spell
627 if (caster
->IsNonMeleeSpellCasted(false) && action
.cast
.castFlags
& CAST_INTURRUPT_PREVIOUS
)
628 caster
->InterruptNonMeleeSpells(false);
630 caster
->CastSpell(target
, action
.cast
.spellId
, (action
.cast
.castFlags
& CAST_TRIGGERED
));
635 sLog
.outErrorDb("CreatureEventAI: event %d creature %d attempt to cast spell that doesn't exist %d", EventId
, m_creature
->GetEntry(), action
.cast
.spellId
);
639 case ACTION_T_SUMMON
:
641 Unit
* target
= GetTargetByType(action
.summon
.target
, pActionInvoker
);
643 Creature
* pCreature
= NULL
;
645 if (action
.summon
.duration
)
646 pCreature
= m_creature
->SummonCreature(action
.summon
.creatured
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, TEMPSUMMON_TIMED_OR_DEAD_DESPAWN
, action
.summon
.duration
);
648 pCreature
= m_creature
->SummonCreature(action
.summon
.creatured
, 0.0f
, 0.0f
, 0.0f
, 0.0f
, TEMPSUMMON_TIMED_DESPAWN_OUT_OF_COMBAT
, 0);
651 sLog
.outErrorDb( "CreatureEventAI: failed to spawn creature %u. Spawn event %d is on creature %d", action
.summon
.creatured
, EventId
, m_creature
->GetEntry());
652 else if (action
.summon
.target
!= TARGET_T_SELF
&& target
)
653 pCreature
->AI()->AttackStart(target
);
656 case ACTION_T_THREAT_SINGLE_PCT
:
657 if (Unit
* target
= GetTargetByType(action
.threat_single_pct
.target
, pActionInvoker
))
658 m_creature
->getThreatManager().modifyThreatPercent(target
, action
.threat_single_pct
.percent
);
660 case ACTION_T_THREAT_ALL_PCT
:
662 std::list
<HostilReference
*>& threatList
= m_creature
->getThreatManager().getThreatList();
663 for (std::list
<HostilReference
*>::iterator i
= threatList
.begin(); i
!= threatList
.end(); ++i
)
664 if(Unit
* Temp
= Unit::GetUnit(*m_creature
,(*i
)->getUnitGuid()))
665 m_creature
->getThreatManager().modifyThreatPercent(Temp
, action
.threat_all_pct
.percent
);
668 case ACTION_T_QUEST_EVENT
:
669 if (Unit
* target
= GetTargetByType(action
.quest_event
.target
, pActionInvoker
))
670 if (target
->GetTypeId() == TYPEID_PLAYER
)
671 ((Player
*)target
)->AreaExploredOrEventHappens(action
.quest_event
.questId
);
673 case ACTION_T_CAST_EVENT
:
674 if (Unit
* target
= GetTargetByType(action
.cast_event
.target
, pActionInvoker
))
675 if (target
->GetTypeId() == TYPEID_PLAYER
)
676 ((Player
*)target
)->CastedCreatureOrGO(action
.cast_event
.creatureId
, m_creature
->GetGUID(), action
.cast_event
.spellId
);
678 case ACTION_T_SET_UNIT_FIELD
:
680 Unit
* target
= GetTargetByType(action
.set_unit_field
.target
, pActionInvoker
);
682 // not allow modify important for integrity object fields
683 if (action
.set_unit_field
.field
< OBJECT_END
|| action
.set_unit_field
.field
>= UNIT_END
)
687 target
->SetUInt32Value(action
.set_unit_field
.field
, action
.set_unit_field
.value
);
691 case ACTION_T_SET_UNIT_FLAG
:
692 if (Unit
* target
= GetTargetByType(action
.unit_flag
.target
, pActionInvoker
))
693 target
->SetFlag(UNIT_FIELD_FLAGS
, action
.unit_flag
.value
);
695 case ACTION_T_REMOVE_UNIT_FLAG
:
696 if (Unit
* target
= GetTargetByType(action
.unit_flag
.target
, pActionInvoker
))
697 target
->RemoveFlag(UNIT_FIELD_FLAGS
, action
.unit_flag
.value
);
699 case ACTION_T_AUTO_ATTACK
:
700 MeleeEnabled
= action
.auto_attack
.state
!= 0;
702 case ACTION_T_COMBAT_MOVEMENT
:
703 CombatMovementEnabled
= action
.combat_movement
.state
!= 0;
705 //Allow movement (create new targeted movement gen only if idle)
706 if (CombatMovementEnabled
)
708 if (m_creature
->GetMotionMaster()->GetCurrentMovementGeneratorType() == IDLE_MOTION_TYPE
)
710 m_creature
->GetMotionMaster()->Clear(false);
711 m_creature
->GetMotionMaster()->MoveChase(m_creature
->getVictim(), AttackDistance
, AttackAngle
);
715 if (m_creature
->GetMotionMaster()->GetCurrentMovementGeneratorType() == TARGETED_MOTION_TYPE
)
717 m_creature
->GetMotionMaster()->Clear(false);
718 m_creature
->GetMotionMaster()->MoveIdle();
719 m_creature
->StopMoving();
722 case ACTION_T_SET_PHASE
:
723 Phase
= action
.set_phase
.phase
;
725 case ACTION_T_INC_PHASE
:
727 int32 new_phase
= int32(Phase
)+action
.set_inc_phase
.step
;
730 sLog
.outErrorDb( "CreatureEventAI: Event %d decrease Phase under 0. CreatureEntry = %d", EventId
, m_creature
->GetEntry());
733 else if (new_phase
>= MAX_PHASE
)
735 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());
747 //TODO: Replace with Flee movement generator
748 m_creature
->CastSpell(m_creature
, SPELL_RUN_AWAY
, true);
750 case ACTION_T_QUEST_EVENT_ALL
:
751 if (pActionInvoker
&& pActionInvoker
->GetTypeId() == TYPEID_PLAYER
)
753 if (Unit
* Temp
= Unit::GetUnit(*m_creature
,pActionInvoker
->GetGUID()))
754 if (Temp
->GetTypeId() == TYPEID_PLAYER
)
755 ((Player
*)Temp
)->GroupEventHappens(action
.quest_event_all
.questId
,m_creature
);
758 case ACTION_T_CAST_EVENT_ALL
:
760 std::list
<HostilReference
*>& threatList
= m_creature
->getThreatManager().getThreatList();
761 for (std::list
<HostilReference
*>::iterator i
= threatList
.begin(); i
!= threatList
.end(); ++i
)
762 if (Unit
* Temp
= Unit::GetUnit(*m_creature
,(*i
)->getUnitGuid()))
763 if (Temp
->GetTypeId() == TYPEID_PLAYER
)
764 ((Player
*)Temp
)->CastedCreatureOrGO(action
.cast_event_all
.creatureId
, m_creature
->GetGUID(), action
.cast_event_all
.spellId
);
767 case ACTION_T_REMOVEAURASFROMSPELL
:
768 if (Unit
* target
= GetTargetByType(action
.remove_aura
.target
, pActionInvoker
))
769 target
->RemoveAurasDueToSpell(action
.remove_aura
.spellId
);
771 case ACTION_T_RANGED_MOVEMENT
:
772 AttackDistance
= action
.ranged_movement
.distance
;
773 AttackAngle
= ((float)action
.ranged_movement
.angle
/180)*M_PI
;
775 if (CombatMovementEnabled
)
777 if (m_creature
->GetMotionMaster()->GetCurrentMovementGeneratorType() == TARGETED_MOTION_TYPE
)
779 //Drop current movement gen
780 m_creature
->GetMotionMaster()->Clear(false);
781 m_creature
->GetMotionMaster()->MoveChase(m_creature
->getVictim(), AttackDistance
, AttackAngle
);
785 case ACTION_T_RANDOM_PHASE
:
786 Phase
= GetRandActionParam(rnd
, action
.random_phase
.phase1
, action
.random_phase
.phase2
, action
.random_phase
.phase3
);
788 case ACTION_T_RANDOM_PHASE_RANGE
:
789 if (action
.random_phase_range
.phaseMax
> action
.random_phase_range
.phaseMin
)
790 Phase
= action
.random_phase_range
.phaseMin
+ (rnd
% (action
.random_phase_range
.phaseMax
- action
.random_phase_range
.phaseMin
));
792 sLog
.outErrorDb( "CreatureEventAI: ACTION_T_RANDOM_PHASE_RANGE cannot have Param2 <= Param1. Divide by Zero. Event = %d. CreatureEntry = %d", EventId
, m_creature
->GetEntry());
794 case ACTION_T_SUMMON_ID
:
796 Unit
* target
= GetTargetByType(action
.summon_id
.target
, pActionInvoker
);
798 CreatureEventAI_Summon_Map::const_iterator i
= CreatureEAI_Mgr
.GetCreatureEventAISummonMap().find(action
.summon_id
.spawnId
);
799 if (i
== CreatureEAI_Mgr
.GetCreatureEventAISummonMap().end())
801 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());
805 Creature
* pCreature
= NULL
;
806 if ((*i
).second
.SpawnTimeSecs
)
807 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
);
809 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);
812 sLog
.outErrorDb( "CreatureEventAI: failed to spawn creature %u. EventId %d.Creature %d", action
.summon_id
.creatureId
, EventId
, m_creature
->GetEntry());
813 else if (action
.summon_id
.target
!= TARGET_T_SELF
&& target
)
814 pCreature
->AI()->AttackStart(target
);
818 case ACTION_T_KILLED_MONSTER
:
819 //first attempt player who tapped creature
820 if (Player
* pPlayer
= m_creature
->GetLootRecipient())
821 pPlayer
->RewardPlayerAndGroupAtEvent(action
.killed_monster
.creatureId
, m_creature
);
824 //if not available, use pActionInvoker
825 if (Unit
* pTarget
= GetTargetByType(action
.killed_monster
.target
, pActionInvoker
))
826 if (Player
* pPlayer2
= pTarget
->GetCharmerOrOwnerPlayerOrPlayerItself())
827 pPlayer2
->RewardPlayerAndGroupAtEvent(action
.killed_monster
.creatureId
, m_creature
);
830 case ACTION_T_SET_INST_DATA
:
832 InstanceData
* pInst
= (InstanceData
*)m_creature
->GetInstanceData();
835 sLog
.outErrorDb("CreatureEventAI: Event %d attempt to set instance data without instance script. Creature %d", EventId
, m_creature
->GetEntry());
839 pInst
->SetData(action
.set_inst_data
.field
, action
.set_inst_data
.value
);
842 case ACTION_T_SET_INST_DATA64
:
844 Unit
* target
= GetTargetByType(action
.set_inst_data64
.target
, pActionInvoker
);
847 sLog
.outErrorDb("CreatureEventAI: Event %d attempt to set instance data64 but Target == NULL. Creature %d", EventId
, m_creature
->GetEntry());
851 InstanceData
* pInst
= (InstanceData
*)m_creature
->GetInstanceData();
854 sLog
.outErrorDb("CreatureEventAI: Event %d attempt to set instance data64 without instance script. Creature %d", EventId
, m_creature
->GetEntry());
858 pInst
->SetData64(action
.set_inst_data64
.field
, target
->GetGUID());
861 case ACTION_T_UPDATE_TEMPLATE
:
863 if (m_creature
->GetEntry() == action
.update_template
.creatureId
)
866 sLog
.outErrorDb("CreatureEventAI: Event %d ACTION_T_UPDATE_TEMPLATE call with param1 == current entry. Creature %d", EventId
, m_creature
->GetEntry());
870 m_creature
->UpdateEntry(action
.update_template
.creatureId
, action
.update_template
.team
? HORDE
: ALLIANCE
);
875 if (m_creature
->isDead())
878 sLog
.outErrorDb("CreatureEventAI: Event %d ACTION_T_DIE on dead creature. Creature %d", EventId
, m_creature
->GetEntry());
881 m_creature
->DealDamage(m_creature
, m_creature
->GetMaxHealth(),NULL
, DIRECT_DAMAGE
, SPELL_SCHOOL_MASK_NORMAL
, NULL
, false);
884 case ACTION_T_ZONE_COMBAT_PULSE
:
886 if (!m_creature
->isInCombat() || !m_creature
->GetMap()->IsDungeon())
889 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());
893 DoZoneInCombat(m_creature
);
899 void CreatureEventAI::JustRespawned()
906 //Handle Spawned Events
907 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
909 if ((*i
).Event
.event_type
== EVENT_T_SPAWNED
)
914 void CreatureEventAI::Reset()
916 EventUpdateTime
= EVENT_UPDATE_TIME
;
922 //Reset all events to enabled
923 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
925 switch ((*i
).Event
.event_type
)
927 //Reset all out of combat timers
928 case EVENT_T_TIMER_OOC
:
930 if ((*i
).Event
.event_param2
== (*i
).Event
.event_param1
)
932 (*i
).Time
= (*i
).Event
.event_param1
;
935 else if ((*i
).Event
.event_param2
> (*i
).Event
.event_param1
)
937 (*i
).Time
= urand((*i
).Event
.event_param1
, (*i
).Event
.event_param2
);
941 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
);
945 //TODO: enable below code line / verify this is correct to enable events previously disabled (ex. aggro yell), instead of enable this in void Aggro()
946 //(*i).Enabled = true;
953 void CreatureEventAI::JustReachedHome()
955 m_creature
->LoadCreaturesAddon();
959 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
961 if ((*i
).Event
.event_type
== EVENT_T_REACHED_HOME
)
969 void CreatureEventAI::EnterEvadeMode()
971 m_creature
->RemoveAllAuras();
972 m_creature
->DeleteThreatList();
973 m_creature
->CombatStop(true);
975 if (m_creature
->isAlive())
976 m_creature
->GetMotionMaster()->MoveTargetedHome();
978 m_creature
->SetLootRecipient(NULL
);
983 //Handle Evade events
984 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
986 if ((*i
).Event
.event_type
== EVENT_T_EVADE
)
991 void CreatureEventAI::JustDied(Unit
* killer
)
998 //Handle Evade events
999 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
1001 if ((*i
).Event
.event_type
== EVENT_T_DEATH
)
1002 ProcessEvent(*i
, killer
);
1006 void CreatureEventAI::KilledUnit(Unit
* victim
)
1008 if (bEmptyList
|| victim
->GetTypeId() != TYPEID_PLAYER
)
1011 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
1013 if ((*i
).Event
.event_type
== EVENT_T_KILL
)
1014 ProcessEvent(*i
, victim
);
1018 void CreatureEventAI::JustSummoned(Creature
* pUnit
)
1020 if (bEmptyList
|| !pUnit
)
1023 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
1025 if ((*i
).Event
.event_type
== EVENT_T_SUMMONED_UNIT
)
1026 ProcessEvent(*i
, pUnit
);
1030 void CreatureEventAI::EnterCombat(Unit
*enemy
)
1032 //Check for on combat start events
1035 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
1037 switch ((*i
).Event
.event_type
)
1040 (*i
).Enabled
= true;
1041 ProcessEvent(*i
, enemy
);
1043 //Reset all in combat timers
1045 if ((*i
).Event
.event_param2
== (*i
).Event
.event_param1
)
1047 (*i
).Time
= (*i
).Event
.event_param1
;
1048 (*i
).Enabled
= true;
1050 else if ((*i
).Event
.event_param2
> (*i
).Event
.event_param1
)
1052 (*i
).Time
= urand((*i
).Event
.event_param1
, (*i
).Event
.event_param2
);
1053 (*i
).Enabled
= true;
1056 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
);
1058 //All normal events need to be re-enabled and their time set to 0
1060 (*i
).Enabled
= true;
1067 EventUpdateTime
= EVENT_UPDATE_TIME
;
1071 void CreatureEventAI::AttackStart(Unit
*who
)
1076 if (m_creature
->Attack(who
, MeleeEnabled
))
1078 m_creature
->AddThreat(who
, 0.0f
);
1079 m_creature
->SetInCombatWith(who
);
1080 who
->SetInCombatWith(m_creature
);
1082 if (CombatMovementEnabled
)
1084 m_creature
->GetMotionMaster()->MoveChase(who
, AttackDistance
, AttackAngle
);
1088 m_creature
->GetMotionMaster()->MoveIdle();
1089 m_creature
->StopMoving();
1094 void CreatureEventAI::MoveInLineOfSight(Unit
*who
)
1099 //Check for OOC LOS Event
1100 if (!bEmptyList
&& !m_creature
->getVictim())
1102 for (std::list
<CreatureEventAIHolder
>::iterator itr
= CreatureEventAIList
.begin(); itr
!= CreatureEventAIList
.end(); ++itr
)
1104 if ((*itr
).Event
.event_type
== EVENT_T_OOC_LOS
)
1106 //can trigger if closer than fMaxAllowedRange
1107 float fMaxAllowedRange
= (*itr
).Event
.event_param2
;
1109 //if range is ok and we are actually in LOS
1110 if (m_creature
->IsWithinDistInMap(who
, fMaxAllowedRange
) && m_creature
->IsWithinLOSInMap(who
))
1112 //if friendly event&&who is not hostile OR hostile event&&who is hostile
1113 if (((*itr
).Event
.event_param1
&& !m_creature
->IsHostileTo(who
)) ||
1114 ((!(*itr
).Event
.event_param1
) && m_creature
->IsHostileTo(who
)))
1115 ProcessEvent(*itr
, who
);
1121 if (m_creature
->isCivilian() || m_creature
->IsNeutralToAll())
1124 if (!m_creature
->hasUnitState(UNIT_STAT_STUNNED
) && who
->isTargetableForAttack() &&
1125 m_creature
->IsHostileTo(who
) && who
->isInAccessablePlaceFor(m_creature
))
1127 if (!m_creature
->canFly() && m_creature
->GetDistanceZ(who
) > CREATURE_Z_ATTACK_RANGE
)
1130 float attackRadius
= m_creature
->GetAttackDistance(who
);
1131 if (m_creature
->IsWithinDistInMap(who
, attackRadius
) && m_creature
->IsWithinLOSInMap(who
))
1133 if (!m_creature
->getVictim())
1136 who
->RemoveSpellsCausingAura(SPELL_AURA_MOD_STEALTH
);
1138 else if (m_creature
->GetMap()->IsDungeon())
1140 m_creature
->AddThreat(who
, 0.0f
);
1141 who
->SetInCombatWith(m_creature
);
1147 void CreatureEventAI::SpellHit(Unit
* pUnit
, const SpellEntry
* pSpell
)
1153 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
1155 if ((*i
).Event
.event_type
== EVENT_T_SPELLHIT
)
1157 //If spell id matches (or no spell id) & if spell school matches (or no spell school)
1158 if (!(*i
).Event
.event_param1
|| pSpell
->Id
== (*i
).Event
.event_param1
)
1160 if ((*i
).Event
.event_param2_s
== -1 || pSpell
->SchoolMask
== (*i
).Event
.event_param2
)
1161 ProcessEvent(*i
, pUnit
);
1167 void CreatureEventAI::UpdateAI(const uint32 diff
)
1169 //Check if we are in combat (also updates calls threat update code)
1170 bool Combat
= m_creature
->SelectHostilTarget() && m_creature
->getVictim();
1172 //Must return if creature isn't alive. Normally select hostil target and get victim prevent this
1173 if (!m_creature
->isAlive())
1178 //Events are only updated once every EVENT_UPDATE_TIME ms to prevent lag with large amount of events
1179 if (EventUpdateTime
< diff
)
1183 //Check for time based events
1184 for (std::list
<CreatureEventAIHolder
>::iterator i
= CreatureEventAIList
.begin(); i
!= CreatureEventAIList
.end(); ++i
)
1189 if ((*i
).Time
> EventDiff
)
1191 //Do not decrement timers if event cannot trigger in this phase
1192 if (!((*i
).Event
.event_inverse_phase_mask
& (1 << Phase
)))
1193 (*i
).Time
-= EventDiff
;
1195 //Skip processing of events that have time remaining
1201 //Events that are updated every EVENT_UPDATE_TIME
1202 switch ((*i
).Event
.event_type
)
1204 case EVENT_T_TIMER_OOC
:
1210 case EVENT_T_TARGET_HP
:
1211 case EVENT_T_TARGET_CASTING
:
1212 case EVENT_T_FRIENDLY_HP
:
1218 if (m_creature
->IsInMap(m_creature
->getVictim()))
1219 if (m_creature
->IsInRange(m_creature
->getVictim(),
1220 (float)(*i
).Event
.event_param1
,(float)(*i
).Event
.event_param2
))
1227 EventUpdateTime
= EVENT_UPDATE_TIME
;
1232 EventUpdateTime
-= diff
;
1237 if (Combat
&& MeleeEnabled
)
1238 DoMeleeAttackIfReady();
1241 bool CreatureEventAI::IsVisible(Unit
*pl
) const
1243 return m_creature
->IsWithinDist(pl
,sWorld
.getConfig(CONFIG_SIGHT_MONSTER
))
1244 && pl
->isVisibleForOrDetect(m_creature
,true);
1247 inline Unit
* CreatureEventAI::SelectUnit(AttackingTarget target
, uint32 position
)
1249 //ThreatList m_threatlist;
1250 std::list
<HostilReference
*>& m_threatlist
= m_creature
->getThreatManager().getThreatList();
1251 std::list
<HostilReference
*>::iterator i
= m_threatlist
.begin();
1252 std::list
<HostilReference
*>::reverse_iterator r
= m_threatlist
.rbegin();
1254 if (position
>= m_threatlist
.size() || !m_threatlist
.size())
1259 case ATTACKING_TARGET_RANDOM
:
1261 advance ( i
, position
+ (rand() % (m_threatlist
.size() - position
) ));
1262 return Unit::GetUnit(*m_creature
,(*i
)->getUnitGuid());
1264 case ATTACKING_TARGET_TOPAGGRO
:
1266 advance ( i
, position
);
1267 return Unit::GetUnit(*m_creature
,(*i
)->getUnitGuid());
1269 case ATTACKING_TARGET_BOTTOMAGGRO
:
1271 advance ( r
, position
);
1272 return Unit::GetUnit(*m_creature
,(*r
)->getUnitGuid());
1278 inline uint32
CreatureEventAI::GetRandActionParam(uint32 rnd
, uint32 param1
, uint32 param2
, uint32 param3
)
1282 case 0: return param1
;
1283 case 1: return param2
;
1284 case 2: return param3
;
1289 inline int32
CreatureEventAI::GetRandActionParam(uint32 rnd
, int32 param1
, int32 param2
, int32 param3
)
1293 case 0: return param1
;
1294 case 1: return param2
;
1295 case 2: return param3
;
1300 inline Unit
* CreatureEventAI::GetTargetByType(uint32 Target
, Unit
* pActionInvoker
)
1306 case TARGET_T_HOSTILE
:
1307 return m_creature
->getVictim();
1308 case TARGET_T_HOSTILE_SECOND_AGGRO
:
1309 return SelectUnit(ATTACKING_TARGET_TOPAGGRO
,1);
1310 case TARGET_T_HOSTILE_LAST_AGGRO
:
1311 return SelectUnit(ATTACKING_TARGET_BOTTOMAGGRO
,0);
1312 case TARGET_T_HOSTILE_RANDOM
:
1313 return SelectUnit(ATTACKING_TARGET_RANDOM
,0);
1314 case TARGET_T_HOSTILE_RANDOM_NOT_TOP
:
1315 return SelectUnit(ATTACKING_TARGET_RANDOM
,1);
1316 case TARGET_T_ACTION_INVOKER
:
1317 return pActionInvoker
;
1323 Unit
* CreatureEventAI::DoSelectLowestHpFriendly(float range
, uint32 MinHPDiff
)
1325 CellPair
p(MaNGOS::ComputeCellPair(m_creature
->GetPositionX(), m_creature
->GetPositionY()));
1327 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1332 MaNGOS::MostHPMissingInRange
u_check(m_creature
, range
, MinHPDiff
);
1333 MaNGOS::UnitLastSearcher
<MaNGOS::MostHPMissingInRange
> searcher(m_creature
, pUnit
, u_check
);
1336 typedef TYPELIST_4(GameObject, Creature*except pets*, DynamicObject, Corpse*Bones*) AllGridObjectTypes;
1337 This means that if we only search grid then we cannot possibly return pets or players so this is safe
1339 TypeContainerVisitor
<MaNGOS::UnitLastSearcher
<MaNGOS::MostHPMissingInRange
>, GridTypeMapContainer
> grid_unit_searcher(searcher
);
1341 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1342 cell_lock
->Visit(cell_lock
, grid_unit_searcher
, *m_creature
->GetMap());
1346 void CreatureEventAI::DoFindFriendlyCC(std::list
<Creature
*>& _list
, float range
)
1348 CellPair
p(MaNGOS::ComputeCellPair(m_creature
->GetPositionX(), m_creature
->GetPositionY()));
1350 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1353 MaNGOS::FriendlyCCedInRange
u_check(m_creature
, range
);
1354 MaNGOS::CreatureListSearcher
<MaNGOS::FriendlyCCedInRange
> searcher(m_creature
, _list
, u_check
);
1356 TypeContainerVisitor
<MaNGOS::CreatureListSearcher
<MaNGOS::FriendlyCCedInRange
>, GridTypeMapContainer
> grid_creature_searcher(searcher
);
1358 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1359 cell_lock
->Visit(cell_lock
, grid_creature_searcher
, *m_creature
->GetMap());
1362 void CreatureEventAI::DoFindFriendlyMissingBuff(std::list
<Creature
*>& _list
, float range
, uint32 spellid
)
1364 CellPair
p(MaNGOS::ComputeCellPair(m_creature
->GetPositionX(), m_creature
->GetPositionY()));
1366 cell
.data
.Part
.reserved
= ALL_DISTRICT
;
1369 MaNGOS::FriendlyMissingBuffInRange
u_check(m_creature
, range
, spellid
);
1370 MaNGOS::CreatureListSearcher
<MaNGOS::FriendlyMissingBuffInRange
> searcher(m_creature
, _list
, u_check
);
1372 TypeContainerVisitor
<MaNGOS::CreatureListSearcher
<MaNGOS::FriendlyMissingBuffInRange
>, GridTypeMapContainer
> grid_creature_searcher(searcher
);
1374 CellLock
<GridReadGuard
> cell_lock(cell
, p
);
1375 cell_lock
->Visit(cell_lock
, grid_creature_searcher
, *m_creature
->GetMap());
1378 //*********************************
1379 //*** Functions used globally ***
1381 void CreatureEventAI::DoScriptText(int32 textEntry
, WorldObject
* pSource
, Unit
* target
)
1385 sLog
.outErrorDb("CreatureEventAI: DoScriptText entry %i, invalid Source pointer.",textEntry
);
1391 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
);
1395 CreatureEventAI_TextMap::const_iterator i
= CreatureEAI_Mgr
.GetCreatureEventAITextMap().find(textEntry
);
1397 if (i
== CreatureEAI_Mgr
.GetCreatureEventAITextMap().end())
1399 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
);
1403 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
);
1405 if((*i
).second
.SoundId
)
1407 if (GetSoundEntriesStore()->LookupEntry((*i
).second
.SoundId
))
1408 pSource
->PlayDirectSound((*i
).second
.SoundId
);
1410 sLog
.outErrorDb("CreatureEventAI: DoScriptText entry %i tried to process invalid sound id %u.",textEntry
,(*i
).second
.SoundId
);
1413 if((*i
).second
.Emote
)
1415 if (pSource
->GetTypeId() == TYPEID_UNIT
|| pSource
->GetTypeId() == TYPEID_PLAYER
)
1417 ((Unit
*)pSource
)->HandleEmoteCommand((*i
).second
.Emote
);
1420 sLog
.outErrorDb("CreatureEventAI: DoScriptText entry %i tried to process emote for invalid TypeId (%u).",textEntry
,pSource
->GetTypeId());
1423 switch((*i
).second
.Type
)
1426 pSource
->MonsterSay(textEntry
, (*i
).second
.Language
, target
? target
->GetGUID() : 0);
1428 case CHAT_TYPE_YELL
:
1429 pSource
->MonsterYell(textEntry
, (*i
).second
.Language
, target
? target
->GetGUID() : 0);
1431 case CHAT_TYPE_TEXT_EMOTE
:
1432 pSource
->MonsterTextEmote(textEntry
, target
? target
->GetGUID() : 0);
1434 case CHAT_TYPE_BOSS_EMOTE
:
1435 pSource
->MonsterTextEmote(textEntry
, target
? target
->GetGUID() : 0, true);
1437 case CHAT_TYPE_WHISPER
:
1439 if (target
&& target
->GetTypeId() == TYPEID_PLAYER
)
1440 pSource
->MonsterWhisper(textEntry
, target
->GetGUID());
1441 else sLog
.outErrorDb("CreatureEventAI: DoScriptText entry %i cannot whisper without target unit (TYPEID_PLAYER).", textEntry
);
1443 case CHAT_TYPE_BOSS_WHISPER
:
1445 if (target
&& target
->GetTypeId() == TYPEID_PLAYER
)
1446 pSource
->MonsterWhisper(textEntry
, target
->GetGUID(), true);
1447 else sLog
.outErrorDb("CreatureEventAI: DoScriptText entry %i cannot whisper without target unit (TYPEID_PLAYER).", textEntry
);
1449 case CHAT_TYPE_ZONE_YELL
:
1450 pSource
->MonsterYellToZone(textEntry
, (*i
).second
.Language
, target
? target
->GetGUID() : 0);
1455 void CreatureEventAI::DoZoneInCombat(Unit
* pUnit
)
1460 Map
*map
= pUnit
->GetMap();
1462 if (!map
->IsDungeon()) //use IsDungeon instead of Instanceable, in case battlegrounds will be instantiated
1464 sLog
.outErrorDb("CreatureEventAI: DoZoneInCombat call for map that isn't an instance (pUnit entry = %d)", pUnit
->GetTypeId() == TYPEID_UNIT
? ((Creature
*)pUnit
)->GetEntry() : 0);
1468 if (!pUnit
->CanHaveThreatList() || pUnit
->getThreatManager().isThreatListEmpty())
1470 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);
1475 Map::PlayerList
const &PlayerList
= map
->GetPlayers();
1476 for(Map::PlayerList::const_iterator i
= PlayerList
.begin(); i
!= PlayerList
.end(); ++i
)
1477 if (Player
* i_pl
= i
->getSource())
1478 if (!i_pl
->isGameMaster())
1479 pUnit
->AddThreat(i_pl
, 0.0f
);
1482 void CreatureEventAI::DoMeleeAttackIfReady()
1484 //Make sure our attack is ready before checking distance
1485 if (m_creature
->isAttackReady())
1487 //If we are within range melee the target
1488 if (m_creature
->IsWithinDistInMap(m_creature
->getVictim(), ATTACK_DISTANCE
))
1490 m_creature
->AttackerStateUpdate(m_creature
->getVictim());
1491 m_creature
->resetAttackTimer();
1496 bool CreatureEventAI::CanCast(Unit
* Target
, SpellEntry
const *Spell
, bool Triggered
)
1498 //No target so we can't cast
1499 if (!Target
|| !Spell
)
1502 //Silenced so we can't cast
1503 if (!Triggered
&& m_creature
->HasFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_SILENCED
))
1507 if (!Triggered
&& m_creature
->GetPower((Powers
)Spell
->powerType
) < Spell
->manaCost
)
1510 SpellRangeEntry
const *TempRange
= NULL
;
1512 TempRange
= GetSpellRangeStore()->LookupEntry(Spell
->rangeIndex
);
1514 //Spell has invalid range store so we can't use it
1518 //Unit is out of range of this spell
1519 if (!m_creature
->IsInRange(Target
,TempRange
->minRange
,TempRange
->maxRange
))
1525 void CreatureEventAI::ReceiveEmote(Player
* pPlayer
, uint32 text_emote
)
1530 for (std::list
<CreatureEventAIHolder
>::iterator itr
= CreatureEventAIList
.begin(); itr
!= CreatureEventAIList
.end(); ++itr
)
1532 if ((*itr
).Event
.event_type
== EVENT_T_RECEIVE_EMOTE
)
1534 if ((*itr
).Event
.event_param1
!= text_emote
)
1537 bool bProcess
= false;
1539 switch((*itr
).Event
.event_param2
)
1541 //enum ConditionType
1542 case CONDITION_NONE
: // 0 0
1545 case CONDITION_AURA
: // spell_id effindex
1546 if (pPlayer
->HasAura((*itr
).Event
.event_param3
,(*itr
).Event
.event_param4
))
1549 case CONDITION_ITEM
: // item_id count
1550 if (pPlayer
->HasItemCount((*itr
).Event
.event_param3
,(*itr
).Event
.event_param4
))
1553 case CONDITION_ITEM_EQUIPPED
: // item_id count
1554 if (pPlayer
->HasItemOrGemWithIdEquipped((*itr
).Event
.event_param3
,(*itr
).Event
.event_param4
))
1557 case CONDITION_ZONEID
: // zone_id 0
1558 if (pPlayer
->GetZoneId() == (*itr
).Event
.event_param3
)
1561 case CONDITION_REPUTATION_RANK
: // faction_id min_rank
1562 if (pPlayer
->GetReputationRank((*itr
).Event
.event_param3
) >= (*itr
).Event
.event_param4
)
1565 case CONDITION_TEAM
: // player_team 0, (469 - Alliance 67 - Horde)
1566 if (pPlayer
->GetTeam() == (*itr
).Event
.event_param3
)
1569 case CONDITION_SKILL
: // skill_id min skill_value
1570 if (pPlayer
->HasSkill((*itr
).Event
.event_param3
) && pPlayer
->GetSkillValue((*itr
).Event
.event_param3
) >= (*itr
).Event
.event_param4
)
1573 case CONDITION_QUESTREWARDED
: // quest_id 0
1574 if (pPlayer
->GetQuestRewardStatus((*itr
).Event
.event_param3
))
1577 case CONDITION_QUESTTAKEN
: // quest_id 0, for condition true while quest active.
1578 if (pPlayer
->GetQuestStatus((*itr
).Event
.event_param3
) == QUEST_STATUS_INCOMPLETE
)
1581 case CONDITION_ACTIVE_EVENT
: // event_id 0
1582 if (IsHolidayActive(HolidayIds((*itr
).Event
.event_param3
)))
1589 sLog
.outDebug("CreatureEventAI: ReceiveEmote CreatureEventAI: Condition ok, processing");
1590 ProcessEvent(*itr
, pPlayer
);