aboutsummaryrefslogtreecommitdiff
path: root/src/game/UnitAI.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/game/UnitAI.cpp')
-rw-r--r--src/game/UnitAI.cpp40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/game/UnitAI.cpp b/src/game/UnitAI.cpp
index a295d2d29b7..728e6f36916 100644
--- a/src/game/UnitAI.cpp
+++ b/src/game/UnitAI.cpp
@@ -17,26 +17,31 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
+
#include "UnitAI.h"
#include "Player.h"
#include "Creature.h"
#include "SpellAuras.h"
#include "SpellMgr.h"
#include "CreatureAIImpl.h"
+
void UnitAI::AttackStart(Unit *victim)
{
if(victim && me->Attack(victim, true))
me->GetMotionMaster()->MoveChase(victim);
}
+
void UnitAI::AttackStartCaster(Unit *victim, float dist)
{
if(victim && me->Attack(victim, false))
me->GetMotionMaster()->MoveChase(victim, dist);
}
+
void UnitAI::DoMeleeAttackIfReady()
{
if(me->hasUnitState(UNIT_STAT_CASTING))
return;
+
//Make sure our attack is ready and we aren't currently casting before checking distance
if (me->isAttackReady())
{
@@ -57,10 +62,12 @@ void UnitAI::DoMeleeAttackIfReady()
}
}
}
+
bool UnitAI::DoSpellAttackIfReady(uint32 spell)
{
if(me->hasUnitState(UNIT_STAT_CASTING))
return true;
+
if(me->isAttackReady())
{
if(me->IsWithinCombatRange(me->getVictim(), GetSpellMaxRange(spell, false)))
@@ -73,12 +80,15 @@ bool UnitAI::DoSpellAttackIfReady(uint32 spell)
}
return true;
}
+
inline bool SelectTargetHelper(const Unit * me, const Unit * target, const bool &playerOnly, const float &dist, const int32 &aura)
{
if(playerOnly && (!target || target->GetTypeId() != TYPEID_PLAYER))
return false;
+
if(dist && (!me || !target || !me->IsWithinCombatRange(target, dist)))
return false;
+
if(aura)
{
if(aura > 0)
@@ -92,8 +102,10 @@ inline bool SelectTargetHelper(const Unit * me, const Unit * target, const bool
return false;
}
}
+
return true;
}
+
struct TargetDistanceOrder : public std::binary_function<const Unit *, const Unit *, bool>
{
const Unit * me;
@@ -104,6 +116,7 @@ struct TargetDistanceOrder : public std::binary_function<const Unit *, const Uni
return (me->GetExactDistSq(_Left) < me->GetExactDistSq(_Right));
}
};
+
Unit* UnitAI::SelectTarget(SelectAggroTarget targetType, uint32 position, float dist, bool playerOnly, int32 aura)
{
if(targetType == SELECT_TARGET_NEAREST || targetType == SELECT_TARGET_FARTHEST)
@@ -111,13 +124,17 @@ Unit* UnitAI::SelectTarget(SelectAggroTarget targetType, uint32 position, float
std::list<HostilReference*> &m_threatlist = me->getThreatManager().getThreatList();
if(position >= m_threatlist.size())
return NULL;
+
std::list<Unit*> targetList;
for(std::list<HostilReference*>::iterator itr = m_threatlist.begin(); itr!= m_threatlist.end(); ++itr)
if(SelectTargetHelper(me, (*itr)->getTarget(), playerOnly, dist, aura))
targetList.push_back((*itr)->getTarget());
+
if(position >= targetList.size())
return NULL;
+
targetList.sort(TargetDistanceOrder(me));
+
if(targetType == SELECT_TARGET_NEAREST)
{
std::list<Unit*>::iterator i = targetList.begin();
@@ -159,14 +176,17 @@ Unit* UnitAI::SelectTarget(SelectAggroTarget targetType, uint32 position, float
}
}
}
+
if(SelectTargetHelper(me, (*i)->getTarget(), playerOnly, dist, aura))
return (*i)->getTarget();
else
m_threatlist.erase(i);
}
}
+
return NULL;
}
+
void UnitAI::SelectTargetList(std::list<Unit*> &targetList, uint32 num, SelectAggroTarget targetType, float dist, bool playerOnly, int32 aura)
{
if(targetType == SELECT_TARGET_NEAREST || targetType == SELECT_TARGET_FARTHEST)
@@ -174,9 +194,11 @@ void UnitAI::SelectTargetList(std::list<Unit*> &targetList, uint32 num, SelectAg
std::list<HostilReference*> &m_threatlist = me->getThreatManager().getThreatList();
if(m_threatlist.empty())
return;
+
for(std::list<HostilReference*>::iterator itr = m_threatlist.begin(); itr!= m_threatlist.end(); ++itr)
if(SelectTargetHelper(me, (*itr)->getTarget(), playerOnly, dist, aura))
targetList.push_back((*itr)->getTarget());
+
targetList.sort(TargetDistanceOrder(me));
targetList.resize(num);
if(targetType == SELECT_TARGET_FARTHEST)
@@ -199,6 +221,7 @@ void UnitAI::SelectTargetList(std::list<Unit*> &targetList, uint32 num, SelectAg
if(targetType == SELECT_TARGET_RANDOM)
advance(i, rand()%m_threatlist.size());
}
+
if(SelectTargetHelper(me, (*i)->getTarget(), playerOnly, dist, aura))
{
targetList.push_back((*i)->getTarget());
@@ -208,10 +231,12 @@ void UnitAI::SelectTargetList(std::list<Unit*> &targetList, uint32 num, SelectAg
}
}
}
+
float UnitAI::DoGetSpellMaxRange(uint32 spellId, bool positive)
{
return GetSpellMaxRange(spellId, positive);
}
+
void UnitAI::DoCast(uint32 spellId)
{
Unit *target = NULL;
@@ -245,28 +270,36 @@ void UnitAI::DoCast(uint32 spellId)
break;
}
}
+
if(target)
me->CastSpell(target, spellId, false);
}
+
#define UPDATE_TARGET(a) {if(AIInfo->target<a) AIInfo->target=a;}
+
void UnitAI::FillAISpellInfo()
{
AISpellInfo = new AISpellInfoType[GetSpellStore()->GetNumRows()];
+
AISpellInfoType *AIInfo = AISpellInfo;
const SpellEntry * spellInfo;
+
for(uint32 i = 0; i < GetSpellStore()->GetNumRows(); ++i, ++AIInfo)
{
spellInfo = GetSpellStore()->LookupEntry(i);
if(!spellInfo)
continue;
+
if(spellInfo->Attributes & SPELL_ATTR_CASTABLE_WHILE_DEAD)
AIInfo->condition = AICOND_DIE;
else if(IsPassiveSpell(i) || GetSpellDuration(spellInfo) == -1)
AIInfo->condition = AICOND_AGGRO;
else
AIInfo->condition = AICOND_COMBAT;
+
if(AIInfo->cooldown < spellInfo->RecoveryTime)
AIInfo->cooldown = spellInfo->RecoveryTime;
+
if(!GetSpellMaxRange(spellInfo, false))
UPDATE_TARGET(AITARGET_SELF)
else
@@ -274,11 +307,13 @@ void UnitAI::FillAISpellInfo()
for(uint32 j = 0; j < 3; ++j)
{
uint32 targetType = spellInfo->EffectImplicitTargetA[j];
+
if(targetType == TARGET_UNIT_TARGET_ENEMY
|| targetType == TARGET_DST_TARGET_ENEMY)
UPDATE_TARGET(AITARGET_VICTIM)
else if(targetType == TARGET_UNIT_AREA_ENEMY_DST)
UPDATE_TARGET(AITARGET_ENEMY)
+
if(spellInfo->Effect[j] == SPELL_EFFECT_APPLY_AURA)
{
if(targetType == TARGET_UNIT_TARGET_ENEMY)
@@ -294,11 +329,14 @@ void UnitAI::FillAISpellInfo()
AIInfo->maxRange = srange->maxRangeHostile * 3 / 4;
}
}
+
//Enable PlayerAI when charmed
void PlayerAI::OnCharmed(bool apply) { me->IsAIEnabled = apply; }
+
void SimpleCharmedAI::UpdateAI(const uint32 /*diff*/)
{
Creature *charmer = (Creature*)me->GetCharmer();
+
//kill self if charm aura has infinite duration
if(charmer->IsInEvadeMode())
{
@@ -310,8 +348,10 @@ void SimpleCharmedAI::UpdateAI(const uint32 /*diff*/)
return;
}
}
+
if(!charmer->isInCombat())
me->GetMotionMaster()->MoveFollow(charmer, PET_FOLLOW_DIST, me->GetFollowAngle());
+
Unit *target = me->getVictim();
if(!target || !charmer->canAttack(target))
AttackStart(charmer->SelectNearestTarget());