Skip to content

Commit

Permalink
[12516] EventAI: Improve code
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Schmoozerd committed May 31, 2013
1 parent e6930bd commit 70392e3
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 116 deletions.
189 changes: 75 additions & 114 deletions src/game/CreatureEventAI.cpp
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
{
Expand All @@ -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();
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
{
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand All @@ -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();
}

Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/game/CreatureEventAI.h
Expand Up @@ -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<CreatureEventAIHolder> CreatureEventAIList;
Expand Down
2 changes: 1 addition & 1 deletion 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__

0 comments on commit 70392e3

Please sign in to comment.