From 70392e3621e7f9929a77dcd8ccea2b457287da42 Mon Sep 17 00:00:00 2001 From: Schmoozerd Date: Fri, 31 May 2013 17:20:04 +0200 Subject: [PATCH] [12516] EventAI: Improve code * Drop rather pointless bool to check if the number of assigned events is empty * Before the phase was resetted on death if and only if the npc has Events defined * DoMeleeAttackIfReady could have been called even though combat state could have changed while processing events --- src/game/CreatureEventAI.cpp | 189 ++++++++++++++--------------------- src/game/CreatureEventAI.h | 1 - src/shared/revision_nr.h | 2 +- 3 files changed, 76 insertions(+), 116 deletions(-) diff --git a/src/game/CreatureEventAI.cpp b/src/game/CreatureEventAI.cpp index 87574edcf71..2ce8b8942c8 100644 --- a/src/game/CreatureEventAI.cpp +++ b/src/game/CreatureEventAI.cpp @@ -126,8 +126,6 @@ CreatureEventAI::CreatureEventAI(Creature* c) : CreatureAI(c), else sLog.outErrorEventAI("EventMap for Creature %u is empty but creature is using CreatureEventAI.", m_creature->GetEntry()); - m_bEmptyList = m_CreatureEventAIList.empty(); - // Handle Spawned Events, also calls Reset() JustRespawned(); } @@ -954,9 +952,6 @@ void CreatureEventAI::JustRespawned() // NOTE that this is { Reset(); - if (m_bEmptyList) - return; - for (CreatureEventAIList::iterator i = m_CreatureEventAIList.begin(); i != m_CreatureEventAIList.end(); ++i) { // Reset generic timer @@ -977,9 +972,6 @@ void CreatureEventAI::Reset() m_EventDiff = 0; m_throwAIEventStep = 0; - if (m_bEmptyList) - return; - // Reset all events to enabled for (CreatureEventAIList::iterator i = m_CreatureEventAIList.begin(); i != m_CreatureEventAIList.end(); ++i) { @@ -1004,13 +996,10 @@ void CreatureEventAI::Reset() void CreatureEventAI::JustReachedHome() { - if (!m_bEmptyList) + for (CreatureEventAIList::iterator i = m_CreatureEventAIList.begin(); i != m_CreatureEventAIList.end(); ++i) { - for (CreatureEventAIList::iterator i = m_CreatureEventAIList.begin(); i != m_CreatureEventAIList.end(); ++i) - { - if ((*i).Event.event_type == EVENT_T_REACHED_HOME) - ProcessEvent(*i); - } + if ((*i).Event.event_type == EVENT_T_REACHED_HOME) + ProcessEvent(*i); } Reset(); @@ -1027,9 +1016,6 @@ void CreatureEventAI::EnterEvadeMode() m_creature->SetLootRecipient(NULL); - if (m_bEmptyList) - return; - // Handle Evade events for (CreatureEventAIList::iterator i = m_CreatureEventAIList.begin(); i != m_CreatureEventAIList.end(); ++i) { @@ -1052,9 +1038,6 @@ void CreatureEventAI::JustDied(Unit* killer) if (m_throwAIEventMask & (1 << AI_EVENT_JUST_DIED)) SendAIEvent(AI_EVENT_JUST_DIED, killer, 0, AIEVENT_DEFAULT_THROW_RADIUS); - if (m_bEmptyList) - return; - // Handle On Death events for (CreatureEventAIList::iterator i = m_CreatureEventAIList.begin(); i != m_CreatureEventAIList.end(); ++i) { @@ -1068,7 +1051,7 @@ void CreatureEventAI::JustDied(Unit* killer) void CreatureEventAI::KilledUnit(Unit* victim) { - if (m_bEmptyList || victim->GetTypeId() != TYPEID_PLAYER) + if (victim->GetTypeId() != TYPEID_PLAYER) return; for (CreatureEventAIList::iterator i = m_CreatureEventAIList.begin(); i != m_CreatureEventAIList.end(); ++i) @@ -1080,9 +1063,6 @@ void CreatureEventAI::KilledUnit(Unit* victim) void CreatureEventAI::JustSummoned(Creature* pUnit) { - if (m_bEmptyList || !pUnit) - return; - for (CreatureEventAIList::iterator i = m_CreatureEventAIList.begin(); i != m_CreatureEventAIList.end(); ++i) { if ((*i).Event.event_type == EVENT_T_SUMMONED_UNIT) @@ -1092,9 +1072,6 @@ void CreatureEventAI::JustSummoned(Creature* pUnit) void CreatureEventAI::SummonedCreatureJustDied(Creature* pUnit) { - if (m_bEmptyList || !pUnit) - return; - for (CreatureEventAIList::iterator i = m_CreatureEventAIList.begin(); i != m_CreatureEventAIList.end(); ++i) { if ((*i).Event.event_type == EVENT_T_SUMMONED_JUST_DIED) @@ -1104,9 +1081,6 @@ void CreatureEventAI::SummonedCreatureJustDied(Creature* pUnit) void CreatureEventAI::SummonedCreatureDespawn(Creature* pUnit) { - if (m_bEmptyList || !pUnit) - return; - for (CreatureEventAIList::iterator i = m_CreatureEventAIList.begin(); i != m_CreatureEventAIList.end(); ++i) { if ((*i).Event.event_type == EVENT_T_SUMMONED_JUST_DESPAWN) @@ -1116,8 +1090,7 @@ void CreatureEventAI::SummonedCreatureDespawn(Creature* pUnit) void CreatureEventAI::ReceiveAIEvent(AIEventType eventType, Creature* pSender, Unit* pInvoker, uint32 /*miscValue*/) { - if (m_bEmptyList || !pSender) - return; + MANGOS_ASSERT(pSender); for (CreatureEventAIList::iterator itr = m_CreatureEventAIList.begin(); itr != m_CreatureEventAIList.end(); ++itr) { @@ -1130,28 +1103,25 @@ void CreatureEventAI::ReceiveAIEvent(AIEventType eventType, Creature* pSender, U void CreatureEventAI::EnterCombat(Unit* enemy) { // Check for on combat start events - if (!m_bEmptyList) + for (CreatureEventAIList::iterator i = m_CreatureEventAIList.begin(); i != m_CreatureEventAIList.end(); ++i) { - for (CreatureEventAIList::iterator i = m_CreatureEventAIList.begin(); i != m_CreatureEventAIList.end(); ++i) + CreatureEventAI_Event const& event = (*i).Event; + switch (event.event_type) { - CreatureEventAI_Event const& event = (*i).Event; - switch (event.event_type) - { - case EVENT_T_AGGRO: - (*i).Enabled = true; - ProcessEvent(*i, enemy); - break; - // Reset all in combat timers - case EVENT_T_TIMER_IN_COMBAT: - if ((*i).UpdateRepeatTimer(m_creature, event.timer.initialMin, event.timer.initialMax)) - (*i).Enabled = true; - break; - // All normal events need to be re-enabled and their time set to 0 - default: + case EVENT_T_AGGRO: + (*i).Enabled = true; + ProcessEvent(*i, enemy); + break; + // Reset all in combat timers + case EVENT_T_TIMER_IN_COMBAT: + if ((*i).UpdateRepeatTimer(m_creature, event.timer.initialMin, event.timer.initialMax)) (*i).Enabled = true; - (*i).Time = 0; - break; - } + break; + // All normal events need to be re-enabled and their time set to 0 + default: + (*i).Enabled = true; + (*i).Time = 0; + break; } } @@ -1180,7 +1150,7 @@ void CreatureEventAI::MoveInLineOfSight(Unit* who) return; // Check for OOC LOS Event - if (!m_bEmptyList && !m_creature->getVictim()) + if (!m_creature->getVictim()) { for (CreatureEventAIList::iterator itr = m_CreatureEventAIList.begin(); itr != m_CreatureEventAIList.end(); ++itr) { @@ -1229,9 +1199,6 @@ void CreatureEventAI::MoveInLineOfSight(Unit* who) void CreatureEventAI::SpellHit(Unit* pUnit, const SpellEntry* pSpell) { - if (m_bEmptyList) - return; - for (CreatureEventAIList::iterator i = m_CreatureEventAIList.begin(); i != m_CreatureEventAIList.end(); ++i) if ((*i).Event.event_type == EVENT_T_SPELLHIT) // If spell id matches (or no spell id) & if spell school matches (or no spell school) @@ -1245,74 +1212,71 @@ void CreatureEventAI::UpdateAI(const uint32 diff) // Check if we are in combat (also updates calls threat update code) bool Combat = m_creature->SelectHostileTarget() && m_creature->getVictim(); - if (!m_bEmptyList) + // Events are only updated once every EVENT_UPDATE_TIME ms to prevent lag with large amount of events + if (m_EventUpdateTime < diff) { - // Events are only updated once every EVENT_UPDATE_TIME ms to prevent lag with large amount of events - if (m_EventUpdateTime < diff) - { - m_EventDiff += diff; + m_EventDiff += diff; - // Check for time based events - for (CreatureEventAIList::iterator i = m_CreatureEventAIList.begin(); i != m_CreatureEventAIList.end(); ++i) + // Check for time based events + for (CreatureEventAIList::iterator i = m_CreatureEventAIList.begin(); i != m_CreatureEventAIList.end(); ++i) + { + // Decrement Timers + if ((*i).Time) { - // Decrement Timers - if ((*i).Time) + if ((*i).Time > m_EventDiff) { - if ((*i).Time > m_EventDiff) - { - // Do not decrement timers if event cannot trigger in this phase - if (!((*i).Event.event_inverse_phase_mask & (1 << m_Phase))) - (*i).Time -= m_EventDiff; + // Do not decrement timers if event cannot trigger in this phase + if (!((*i).Event.event_inverse_phase_mask & (1 << m_Phase))) + (*i).Time -= m_EventDiff; - // Skip processing of events that have time remaining - continue; - } - else (*i).Time = 0; + // Skip processing of events that have time remaining + continue; } + else (*i).Time = 0; + } - // Events that are updated every EVENT_UPDATE_TIME - switch ((*i).Event.event_type) - { - case EVENT_T_TIMER_OOC: - case EVENT_T_TIMER_GENERIC: + // Events that are updated every EVENT_UPDATE_TIME + switch ((*i).Event.event_type) + { + case EVENT_T_TIMER_OOC: + case EVENT_T_TIMER_GENERIC: + ProcessEvent(*i); + break; + case EVENT_T_TIMER_IN_COMBAT: + case EVENT_T_MANA: + case EVENT_T_HP: + case EVENT_T_TARGET_HP: + case EVENT_T_TARGET_CASTING: + case EVENT_T_FRIENDLY_HP: + case EVENT_T_AURA: + case EVENT_T_TARGET_AURA: + case EVENT_T_MISSING_AURA: + case EVENT_T_TARGET_MISSING_AURA: + if (Combat) ProcessEvent(*i); - break; - case EVENT_T_TIMER_IN_COMBAT: - case EVENT_T_MANA: - case EVENT_T_HP: - case EVENT_T_TARGET_HP: - case EVENT_T_TARGET_CASTING: - case EVENT_T_FRIENDLY_HP: - case EVENT_T_AURA: - case EVENT_T_TARGET_AURA: - case EVENT_T_MISSING_AURA: - case EVENT_T_TARGET_MISSING_AURA: - if (Combat) - ProcessEvent(*i); - break; - case EVENT_T_RANGE: - if (Combat) - { - if (m_creature->getVictim() && m_creature->IsInMap(m_creature->getVictim())) - if (m_creature->IsInRange(m_creature->getVictim(), (float)(*i).Event.range.minDist, (float)(*i).Event.range.maxDist)) - ProcessEvent(*i); - } - break; - } + break; + case EVENT_T_RANGE: + if (Combat) + { + if (m_creature->getVictim() && m_creature->IsInMap(m_creature->getVictim())) + if (m_creature->IsInRange(m_creature->getVictim(), (float)(*i).Event.range.minDist, (float)(*i).Event.range.maxDist)) + ProcessEvent(*i); + } + break; } - - m_EventDiff = 0; - m_EventUpdateTime = EVENT_UPDATE_TIME; - } - else - { - m_EventDiff += diff; - m_EventUpdateTime -= diff; } + + m_EventDiff = 0; + m_EventUpdateTime = EVENT_UPDATE_TIME; + } + else + { + m_EventDiff += diff; + m_EventUpdateTime -= diff; } - // Melee Auto-Attack - if (Combat && m_MeleeEnabled) + // Melee Auto-Attack (recheck m_creature->getVictim in case of combat state was changed while processing events) + if (Combat && m_MeleeEnabled && m_creature->getVictim()) DoMeleeAttackIfReady(); } @@ -1513,9 +1477,6 @@ void CreatureEventAI::DoScriptText(int32 textEntry, WorldObject* pSource, Unit* void CreatureEventAI::ReceiveEmote(Player* pPlayer, uint32 text_emote) { - if (m_bEmptyList) - return; - for (CreatureEventAIList::iterator itr = m_CreatureEventAIList.begin(); itr != m_CreatureEventAIList.end(); ++itr) { if ((*itr).Event.event_type == EVENT_T_RECEIVE_EMOTE) diff --git a/src/game/CreatureEventAI.h b/src/game/CreatureEventAI.h index ca4e1d17581..4d5ee38db27 100644 --- a/src/game/CreatureEventAI.h +++ b/src/game/CreatureEventAI.h @@ -659,7 +659,6 @@ class MANGOS_DLL_SPEC CreatureEventAI : public CreatureAI protected: uint32 m_EventUpdateTime; // Time between event updates uint32 m_EventDiff; // Time between the last event call - bool m_bEmptyList; // Variables used by Events themselves typedef std::vector CreatureEventAIList; diff --git a/src/shared/revision_nr.h b/src/shared/revision_nr.h index ca501e0def8..7cf783fc8f0 100644 --- a/src/shared/revision_nr.h +++ b/src/shared/revision_nr.h @@ -1,4 +1,4 @@ #ifndef __REVISION_NR_H__ #define __REVISION_NR_H__ - #define REVISION_NR "12515" + #define REVISION_NR "12516" #endif // __REVISION_NR_H__