OregonCore  revision be9e804-git
Your Favourite TBC server
UnitAI Class Referenceabstract

#include <UnitAI.h>

+ Inheritance diagram for UnitAI:

Public Member Functions

 UnitAI (Unit *u)
 
virtual ~UnitAI ()
 
virtual bool CanAIAttack (const Unit *) const
 
virtual void AttackStart (Unit *)
 
virtual void UpdateAI (const uint32)=0
 
virtual void InitializeAI ()
 
virtual void Reset ()
 
virtual void EventHappens (uint32)
 
virtual void ScheduleEvent (uint32 data, uint32 delay)
 
virtual void OnCharmed (bool)=0
 
virtual void DoAction (const int32 param=0)
 
virtual uint32 GetData (uint32 id=0)
 
virtual void SetData (uint32, uint32)
 
virtual void SetGUID (const uint64 &, int32 id=0)
 
virtual uint64 GetGUID (int32 id=0)
 
virtual void DamageDealt (Unit *, uint32 &, DamageEffectType)
 
UnitSelectTarget (SelectAggroTarget target, uint32 position=0, float dist=0, bool playerOnly=false, int32 aura=0)
 
void SelectTargetList (std::list< Unit * > &targetList, uint32 num, SelectAggroTarget target, float dist=0, bool playerOnly=false, int32 aura=0)
 
void AttackStartCaster (Unit *victim, float dist)
 
virtual void HealReceived (Unit *, uint32 &)
 
void DoCast (uint32 spellId)
 
void DoCast (Unit *victim, uint32 spellId, bool triggered=false)
 
void DoCastSelf (uint32 spellId, bool triggered=false)
 
void DoCastVictim (uint32 spellId, bool triggered=false)
 
void DoCastAOE (uint32 spellId, bool triggered=false)
 
float DoGetSpellMaxRange (uint32 spellId, bool positive=false)
 
void DoMeleeAttackIfReady ()
 
bool DoSpellAttackIfReady (uint32 spell)
 
virtual void sGossipHello (Player *)
 
virtual void sGossipSelect (Player *, uint32, uint32)
 
virtual void sGossipSelectCode (Player *, uint32, uint32, char const *)
 
virtual void QuestAccept (Player *, Quest const *)
 
virtual void sQuestSelect (Player *, Quest const *)
 
virtual void sQuestReward (Player *, Quest const *, uint32)
 
virtual bool sOnDummyEffect (Unit *, uint32, SpellEffIndex)
 
virtual void sOnGameEvent (bool, uint16)
 

Static Public Member Functions

static void FillAISpellInfo ()
 
static void ClearAISpellInfo ()
 

Static Public Attributes

static AISpellInfoTypeAISpellInfo
 

Protected Attributes

Unit *const me
 

Detailed Description

Definition at line 41 of file UnitAI.h.

Constructor & Destructor Documentation

UnitAI::UnitAI ( Unit u)
inlineexplicit

Definition at line 46 of file UnitAI.h.

46 : me(u) {}
Unit *const me
Definition: UnitAI.h:44
virtual UnitAI::~UnitAI ( )
inlinevirtual

Definition at line 47 of file UnitAI.h.

47 {}

Member Function Documentation

void UnitAI::AttackStart ( Unit victim)
virtual

Reimplemented in CreatureEventAI, Scripted_NoMovementAI, SmartAI, NullCreatureAI, CasterAI, npc_escortAI, PossessedAI, FollowerAI, PetAI, TotemAI, and PassiveAI.

Definition at line 25 of file UnitAI.cpp.

References Unit::Attack(), MotionMaster::Clear(), Unit::ClearUnitState(), Unit::GetMotionMaster(), Unit::HasUnitState(), me, MotionMaster::MoveChase(), and UNIT_STATE_DISTRACTED.

Referenced by CanAIAttack(), Unit::CombatStart(), Spell::DoAllEffectOnTarget(), CreatureAI::DoZoneInCombat(), Spell::EffectDummy(), Spell::EffectSummonType(), Spell::EffectTaunt(), AssistDelayEvent::Execute(), TimedFleeingMovementGenerator::Finalize(), WorldSession::HandlePetActionHelper(), CreatureGroup::MemberAttackStart(), CreatureAI::MoveInLineOfSight(), Oregon::CallOfHelpCreatureInRangeDo::operator()(), CreatureEventAI::ProcessAction(), CreatureAI::SetGazeOn(), Unit::TauntApply(), Unit::TauntFadeOut(), Creature::Update(), SimpleCharmedAI::UpdateAI(), CreatureAI::UpdateVictim(), CreatureAI::UpdateVictimByReact(), and CreatureAI::UpdateVictimWithGaze().

26 {
27  if (victim && me->Attack(victim, true))
28  {
29  // Clear distracted state on attacking
31  {
33  me->GetMotionMaster()->Clear();
34  }
35  me->GetMotionMaster()->MoveChase(victim);
36  }
37 }
bool Attack(Unit *victim, bool meleeAttack)
Definition: Unit.cpp:7425
void Clear(bool reset=true)
Definition: MotionMaster.h:145
void MoveChase(Unit *target, float dist=0.0f, float angle=0.0f)
MotionMaster * GetMotionMaster()
Definition: Unit.h:1893
Unit *const me
Definition: UnitAI.h:44
void ClearUnitState(uint32 f)
Definition: Unit.h:1034
bool HasUnitState(const uint32 f) const
Definition: Unit.h:1030
void UnitAI::AttackStartCaster ( Unit victim,
float  dist 
)

Definition at line 39 of file UnitAI.cpp.

References Unit::Attack(), Unit::GetMotionMaster(), me, and MotionMaster::MoveChase().

Referenced by CasterAI::AttackStart(), and DamageDealt().

40 {
41  if (victim && me->Attack(victim, false))
42  me->GetMotionMaster()->MoveChase(victim, dist);
43 }
bool Attack(Unit *victim, bool meleeAttack)
Definition: Unit.cpp:7425
void MoveChase(Unit *target, float dist=0.0f, float angle=0.0f)
MotionMaster * GetMotionMaster()
Definition: Unit.h:1893
Unit *const me
Definition: UnitAI.h:44
virtual bool UnitAI::CanAIAttack ( const Unit ) const
inlinevirtual

Definition at line 49 of file UnitAI.h.

References AttackStart(), and UpdateAI().

Referenced by Creature::CanCreatureAttack().

50  {
51  return true;
52  }
void UnitAI::ClearAISpellInfo ( )
static

Definition at line 325 of file UnitAI.cpp.

References AISpellInfo.

Referenced by SpellMgr::~SpellMgr().

326 {
327  delete[] AISpellInfo;
328  AISpellInfo = NULL;
329 }
static AISpellInfoType * AISpellInfo
Definition: UnitAI.h:121
virtual void UnitAI::DamageDealt ( Unit ,
uint32 ,
DamageEffectType   
)
inlinevirtual

Reimplemented in SmartAI, and PetAI.

Definition at line 91 of file UnitAI.h.

References AttackStartCaster(), SelectTarget(), and SelectTargetList().

Referenced by Unit::DealDamage().

91 { }
virtual void UnitAI::DoAction ( const int32  param = 0)
inlinevirtual

Reimplemented in ScriptedAI, and SmartAI.

Definition at line 70 of file UnitAI.h.

References UNUSED.

Referenced by SummonList::DoAction().

71  {
72  UNUSED(param);
73  }
#define UNUSED(x)
Definition: Common.h:175
void UnitAI::DoCast ( uint32  spellId)

Definition at line 203 of file UnitAI.cpp.

References AISpellInfo, AITARGET_ALLY, AITARGET_BUFF, AITARGET_DEBUFF, AITARGET_ENEMY, AITARGET_SELF, AITARGET_VICTIM, SpellEntry::Attributes, SpellEntry::AttributesEx3, AURA_INTERRUPT_FLAG_NOT_VICTIM, SpellEntry::AuraInterruptFlags, Unit::CastSpell(), GetSpellMaxRange(), GetSpellStore(), Unit::GetVictim(), me, SELECT_TARGET_RANDOM, SelectTarget(), SelectTargetHelper(), SPELL_ATTR0_HEARTBEAT_RESIST_CHECK, and SPELL_ATTR3_PLAYERS_ONLY.

Referenced by SimpleAI::DamageTaken(), DoCastSelf(), DoCastVictim(), CasterAI::EnterCombat(), HealReceived(), SimpleAI::KilledUnit(), CombatAI::UpdateAI(), SimpleAI::UpdateAI(), and CasterAI::UpdateAI().

204 {
205  Unit* target = NULL;
206  //sLog.outError("aggre %u %u", spellId, (uint32)AISpellInfo[spellId].target);
207  switch (AISpellInfo[spellId].target)
208  {
209  default:
210  case AITARGET_SELF:
211  target = me;
212  break;
213  case AITARGET_VICTIM:
214  target = me->GetVictim();
215  break;
216  case AITARGET_ENEMY:
217  {
218  const SpellEntry* spellInfo = GetSpellStore()->LookupEntry(spellId);
219  bool playerOnly = spellInfo->AttributesEx3 & SPELL_ATTR3_PLAYERS_ONLY;
220  target = SelectTarget(SELECT_TARGET_RANDOM, 0, GetSpellMaxRange(spellInfo), playerOnly);
221  break;
222  }
223  case AITARGET_ALLY:
224  target = me;
225  break;
226  case AITARGET_BUFF:
227  target = me;
228  break;
229  case AITARGET_DEBUFF:
230  {
231  const SpellEntry* spellInfo = GetSpellStore()->LookupEntry(spellId);
232  bool playerOnly = spellInfo->AttributesEx3 & SPELL_ATTR3_PLAYERS_ONLY;
233  float range = GetSpellMaxRange(spellInfo);
236  && SelectTargetHelper(me, me->GetVictim(), playerOnly, range, -(int32)spellId))
237  target = me->GetVictim();
238  else
239  target = SelectTarget(SELECT_TARGET_RANDOM, 0, range, playerOnly, -(int32)spellId);
240  break;
241  }
242  }
243 
244  if (target)
245  me->CastSpell(target, spellId, false);
246 }
uint32 AuraInterruptFlags
Definition: DBCStructure.h:700
static AISpellInfoType * AISpellInfo
Definition: UnitAI.h:121
DBCStorage< SpellEntry > const * GetSpellStore()
Definition: DBCStores.cpp:776
ACE_INT32 int32
Definition: Define.h:67
Unit *const me
Definition: UnitAI.h:44
uint32 Attributes
Definition: DBCStructure.h:679
uint32 AttributesEx3
Definition: DBCStructure.h:682
Unit * GetVictim() const
Definition: Unit.h:1013
void CastSpell(Unit *Victim, uint32 spellId, bool triggered, Item *castItem=NULL, Aura *triggeredByAura=NULL, uint64 originalCaster=0)
Definition: Unit.cpp:1260
bool SelectTargetHelper(const Unit *me, const Unit *target, const bool &playerOnly, const float &dist, const int32 &aura)
Definition: UnitAI.cpp:90
Definition: Unit.h:908
Unit * SelectTarget(SelectAggroTarget target, uint32 position=0, float dist=0, bool playerOnly=false, int32 aura=0)
Definition: UnitAI.cpp:126
float GetSpellMaxRange(SpellRangeEntry const *range)
Definition: SpellMgr.h:143
void UnitAI::DoCast ( Unit victim,
uint32  spellId,
bool  triggered = false 
)

Definition at line 248 of file UnitAI.cpp.

References Unit::CastSpell(), Unit::HasUnitState(), me, and UNIT_STATE_CASTING.

249 {
250  if (!victim || (me->HasUnitState(UNIT_STATE_CASTING) && !triggered))
251  return;
252 
253  me->CastSpell(victim, spellId, triggered);
254 }
Unit *const me
Definition: UnitAI.h:44
void CastSpell(Unit *Victim, uint32 spellId, bool triggered, Item *castItem=NULL, Aura *triggeredByAura=NULL, uint64 originalCaster=0)
Definition: Unit.cpp:1260
bool HasUnitState(const uint32 f) const
Definition: Unit.h:1030
void UnitAI::DoCastAOE ( uint32  spellId,
bool  triggered = false 
)

Definition at line 262 of file UnitAI.cpp.

References Unit::CastSpell(), Unit::HasUnitState(), me, and UNIT_STATE_CASTING.

Referenced by DoCastSelf().

263 {
264  if (!triggered && me->HasUnitState(UNIT_STATE_CASTING))
265  return;
266 
267  me->CastSpell((Unit*)NULL, spellId, triggered);
268 }
Unit *const me
Definition: UnitAI.h:44
void CastSpell(Unit *Victim, uint32 spellId, bool triggered, Item *castItem=NULL, Aura *triggeredByAura=NULL, uint64 originalCaster=0)
Definition: Unit.cpp:1260
bool HasUnitState(const uint32 f) const
Definition: Unit.h:1030
Definition: Unit.h:908
void UnitAI::DoCastSelf ( uint32  spellId,
bool  triggered = false 
)
inline

Definition at line 103 of file UnitAI.h.

References DoCast(), DoCastAOE(), DoCastVictim(), DoGetSpellMaxRange(), DoMeleeAttackIfReady(), and DoSpellAttackIfReady().

103 { DoCast(me, spellId, triggered); }
Unit *const me
Definition: UnitAI.h:44
void DoCast(uint32 spellId)
Definition: UnitAI.cpp:203
void UnitAI::DoCastVictim ( uint32  spellId,
bool  triggered = false 
)

Definition at line 256 of file UnitAI.cpp.

References DoCast(), Unit::GetVictim(), and me.

Referenced by DoCastSelf().

257 {
258  // Why don't we check for casting unit_state and existing target as we do in DoCast(.. ?
259  DoCast(me->GetVictim(), spellId, triggered);
260 }
Unit *const me
Definition: UnitAI.h:44
Unit * GetVictim() const
Definition: Unit.h:1013
void DoCast(uint32 spellId)
Definition: UnitAI.cpp:203
float UnitAI::DoGetSpellMaxRange ( uint32  spellId,
bool  positive = false 
)

Definition at line 198 of file UnitAI.cpp.

References GetSpellMaxRange().

Referenced by DoCastSelf().

199 {
200  return GetSpellMaxRange(spellId);
201 }
float GetSpellMaxRange(SpellRangeEntry const *range)
Definition: SpellMgr.h:143
void UnitAI::DoMeleeAttackIfReady ( )

Definition at line 45 of file UnitAI.cpp.

References Unit::AttackerStateUpdate(), Unit::GetVictim(), Unit::HasUnitState(), Unit::haveOffhandWeapon(), Unit::isAttackReady(), Unit::IsWithinMeleeRange(), me, OFF_ATTACK, Unit::resetAttackTimer(), and UNIT_STATE_CASTING.

Referenced by DoCastSelf(), AggressorAI::UpdateAI(), PetAI::UpdateAI(), PossessedAI::UpdateAI(), CombatAI::UpdateAI(), SimpleAI::UpdateAI(), ScriptedAI::UpdateAI(), SmartAI::UpdateAI(), CreatureEventAI::UpdateAI(), npc_escortAI::UpdateEscortAI(), and FollowerAI::UpdateFollowerAI().

46 {
48  return;
49 
50  //Make sure our attack is ready and we aren't currently casting before checking distance
51  if (me->isAttackReady())
52  {
53  //If we are within range melee the target
55  {
58  }
59  }
61  {
62  //If we are within range melee the target
64  {
67  }
68  }
69 }
void AttackerStateUpdate(Unit *victim, WeaponAttackType attType=BASE_ATTACK, bool extra=false)
Definition: Unit.cpp:2367
Unit *const me
Definition: UnitAI.h:44
Unit * GetVictim() const
Definition: Unit.h:1013
bool haveOffhandWeapon() const
Definition: Unit.cpp:628
bool isAttackReady(WeaponAttackType type=BASE_ATTACK) const
Definition: Unit.h:952
void resetAttackTimer(WeaponAttackType type=BASE_ATTACK)
Definition: Unit.cpp:644
bool HasUnitState(const uint32 f) const
Definition: Unit.h:1030
bool IsWithinMeleeRange(Unit *obj, float dist=MELEE_RANGE) const
Definition: Unit.cpp:688
bool UnitAI::DoSpellAttackIfReady ( uint32  spell)

Definition at line 71 of file UnitAI.cpp.

References Unit::CastSpell(), GetSpellMaxRange(), GetSpellStore(), Unit::GetVictim(), Unit::HasUnitState(), Unit::isAttackReady(), Unit::IsWithinCombatRange(), me, Unit::resetAttackTimer(), and UNIT_STATE_CASTING.

Referenced by DoCastSelf().

72 {
74  return true;
75 
76  if (me->isAttackReady())
77  {
78  const SpellEntry* spellInfo = GetSpellStore()->LookupEntry(spell);
79  if (me->IsWithinCombatRange(me->GetVictim(), GetSpellMaxRange(spellInfo)))
80  {
81  me->CastSpell(me->GetVictim(), spell, false);
83  }
84  else
85  return false;
86  }
87  return true;
88 }
bool IsWithinCombatRange(const Unit *obj, float dist2compare) const
Definition: Unit.cpp:672
DBCStorage< SpellEntry > const * GetSpellStore()
Definition: DBCStores.cpp:776
Unit *const me
Definition: UnitAI.h:44
Unit * GetVictim() const
Definition: Unit.h:1013
void CastSpell(Unit *Victim, uint32 spellId, bool triggered, Item *castItem=NULL, Aura *triggeredByAura=NULL, uint64 originalCaster=0)
Definition: Unit.cpp:1260
bool isAttackReady(WeaponAttackType type=BASE_ATTACK) const
Definition: Unit.h:952
void resetAttackTimer(WeaponAttackType type=BASE_ATTACK)
Definition: Unit.cpp:644
bool HasUnitState(const uint32 f) const
Definition: Unit.h:1030
float GetSpellMaxRange(SpellRangeEntry const *range)
Definition: SpellMgr.h:143
virtual void UnitAI::EventHappens ( uint32  )
inlinevirtual

Definition at line 63 of file UnitAI.h.

References OnCharmed(), and ScheduleEvent().

63 {}
void UnitAI::FillAISpellInfo ( )
static

Definition at line 272 of file UnitAI.cpp.

References AICOND_AGGRO, AICOND_COMBAT, AICOND_DIE, AISpellInfo, AITARGET_BUFF, AITARGET_DEBUFF, AITARGET_ENEMY, AITARGET_SELF, AITARGET_VICTIM, SpellEntry::Attributes, AISpellInfoType::condition, AISpellInfoType::cooldown, SpellEntry::Effect, SpellEntry::EffectImplicitTargetA, GetSpellDuration(), GetSpellMaxRange(), GetSpellStore(), IsPassiveSpell(), IsPositiveSpell(), AISpellInfoType::maxRange, SpellRangeEntry::maxRange, SpellEntry::rangeIndex, AISpellInfoType::realCooldown, SpellEntry::RecoveryTime, SPELL_ATTR0_CASTABLE_WHILE_DEAD, SPELL_EFFECT_APPLY_AURA, sSpellRangeStore, SpellEntry::StartRecoveryTime, TARGET_DST_TARGET_ENEMY, TARGET_UNIT_AREA_ENEMY_DST, TARGET_UNIT_TARGET_ENEMY, and UPDATE_TARGET.

Referenced by SpellMgr::LoadSpellCustomAttr().

273 {
274  AISpellInfo = new AISpellInfoType[GetSpellStore()->GetNumRows()];
275 
276  AISpellInfoType* AIInfo = AISpellInfo;
277  const SpellEntry* spellInfo;
278 
279  for (uint32 i = 0; i < GetSpellStore()->GetNumRows(); ++i, ++AIInfo)
280  {
281  spellInfo = GetSpellStore()->LookupEntry(i);
282  if (!spellInfo)
283  continue;
284 
286  AIInfo->condition = AICOND_DIE;
287  else if (IsPassiveSpell(i) || GetSpellDuration(spellInfo) == -1)
288  AIInfo->condition = AICOND_AGGRO;
289  else
290  AIInfo->condition = AICOND_COMBAT;
291 
292  if (AIInfo->cooldown < spellInfo->RecoveryTime)
293  AIInfo->cooldown = spellInfo->RecoveryTime;
294 
295  if (!GetSpellMaxRange(spellInfo))
297  else
298  {
299  for (uint32 j = 0; j < 3; ++j)
300  {
301  uint32 targetType = spellInfo->EffectImplicitTargetA[j];
302 
303  if (targetType == TARGET_UNIT_TARGET_ENEMY
304  || targetType == TARGET_DST_TARGET_ENEMY)
306  else if (targetType == TARGET_UNIT_AREA_ENEMY_DST)
308 
309  if (spellInfo->Effect[j] == SPELL_EFFECT_APPLY_AURA)
310  {
311  if (targetType == TARGET_UNIT_TARGET_ENEMY)
313  else if (IsPositiveSpell(i))
315  }
316  }
317  }
318  AIInfo->realCooldown = spellInfo->RecoveryTime + spellInfo->StartRecoveryTime;
319  SpellRangeEntry const* srange = sSpellRangeStore.LookupEntry(spellInfo->rangeIndex);
320  if (srange)
321  AIInfo->maxRange = srange->maxRange * 3 / 4;
322  }
323 }
static AISpellInfoType * AISpellInfo
Definition: UnitAI.h:121
DBCStorage< SpellEntry > const * GetSpellStore()
Definition: DBCStores.cpp:776
bool IsPassiveSpell(uint32 spellId)
Definition: SpellMgr.cpp:278
#define UPDATE_TARGET(a)
Definition: UnitAI.cpp:270
AICondition condition
uint32 Attributes
Definition: DBCStructure.h:679
uint32 EffectImplicitTargetA[MAX_SPELL_EFFECTS]
Definition: DBCStructure.h:731
bool IsPositiveSpell(uint32 spellId)
Definition: SpellMgr.cpp:773
uint32 RecoveryTime
Definition: DBCStructure.h:697
ACE_UINT32 uint32
Definition: Define.h:71
DBCStorage< SpellRangeEntry > sSpellRangeStore(SpellRangefmt)
int32 GetSpellDuration(SpellEntry const *spellInfo)
Definition: SpellMgr.cpp:228
float GetSpellMaxRange(SpellRangeEntry const *range)
Definition: SpellMgr.h:143
virtual uint32 UnitAI::GetData ( uint32  id = 0)
inlinevirtual

Reimplemented in SmartAI.

Definition at line 74 of file UnitAI.h.

References UNUSED.

75  {
76  UNUSED(id);
77  return 0;
78  }
#define UNUSED(x)
Definition: Common.h:175
virtual uint64 UnitAI::GetGUID ( int32  id = 0)
inlinevirtual

Reimplemented in SmartAI.

Definition at line 84 of file UnitAI.h.

References UNUSED.

85  {
86  UNUSED(id);
87  return 0;
88  }
#define UNUSED(x)
Definition: Common.h:175
virtual void UnitAI::HealReceived ( Unit ,
uint32  
)
inlinevirtual

Reimplemented in SmartAI.

Definition at line 99 of file UnitAI.h.

References DoCast().

Referenced by Aura::PeriodicTick().

99 { }
virtual void UnitAI::InitializeAI ( )
inlinevirtual

Reimplemented in SmartAI, CasterAI, and CombatAI.

Definition at line 56 of file UnitAI.h.

References Unit::isDead(), and Reset().

Referenced by Creature::AIM_Initialize(), CasterAI::CasterAI(), CombatAI::CombatAI(), and CombatAI::InitializeAI().

57  {
58  if (!me->isDead()) Reset();
59  }
Unit *const me
Definition: UnitAI.h:44
bool isDead() const
Definition: Unit.h:1338
virtual void Reset()
Definition: UnitAI.h:61
virtual void UnitAI::OnCharmed ( bool  )
pure virtual

Implemented in CreatureAI, SmartAI, PlayerAI, and NullCreatureAI.

Referenced by EventHappens(), and PlayerAI::PlayerAI().

virtual void UnitAI::QuestAccept ( Player ,
Quest const *   
)
inlinevirtual

Reimplemented in SmartAI.

Definition at line 115 of file UnitAI.h.

Referenced by Player::AddQuestAndCheckCompletion().

115 { }
virtual void UnitAI::Reset ( )
inlinevirtual
void UnitAI::ScheduleEvent ( uint32  data,
uint32  delay 
)
inlinevirtual

Definition at line 158 of file UnitAI.h.

References EventProcessor::AddEvent(), EventProcessor::CalculateTime(), Unit::m_Events, and me.

Referenced by EventHappens().

159 {
160  me->m_Events.AddEvent(new ScriptEvent(me, data), me->m_Events.CalculateTime(delay), false);
161 }
EventProcessor m_Events
Definition: Unit.h:1636
void AddEvent(BasicEvent *Event, uint64 e_time, bool set_addtime=true)
Unit *const me
Definition: UnitAI.h:44
uint64 CalculateTime(uint64 t_offset)
Unit * UnitAI::SelectTarget ( SelectAggroTarget  target,
uint32  position = 0,
float  dist = 0,
bool  playerOnly = false,
int32  aura = 0 
)

Definition at line 126 of file UnitAI.cpp.

References ThreatManager::getThreatList(), Unit::getThreatManager(), me, SELECT_TARGET_BOTTOMAGGRO, SELECT_TARGET_FARTHEST, SELECT_TARGET_NEAREST, SELECT_TARGET_RANDOM, SELECT_TARGET_TOPAGGRO, SelectRandomContainerElement(), and SelectTargetHelper().

Referenced by DamageDealt(), DoCast(), CreatureEventAI::GetTargetByType(), and ScriptedAI::SelectUnit().

127 {
128  const std::list<HostileReference*>& threatlist = me->getThreatManager().getThreatList();
129  std::list<Unit*> targetList;
130 
131  for (std::list<HostileReference*>::const_iterator itr = threatlist.begin(); itr != threatlist.end(); ++itr)
132  if (SelectTargetHelper(me, (*itr)->getTarget(), playerOnly, dist, aura))
133  targetList.push_back((*itr)->getTarget());
134 
135  if (position >= targetList.size())
136  return NULL;
137 
138  if (targetType == SELECT_TARGET_NEAREST || targetType == SELECT_TARGET_FARTHEST)
139  targetList.sort(TargetDistanceOrder(me));
140 
141  switch (targetType)
142  {
145  {
146  std::list<Unit*>::iterator itr = targetList.begin();
147  advance(itr, position);
148  return *itr;
149  }
150  break;
151 
154  {
155  std::list<Unit*>::reverse_iterator ritr = targetList.rbegin();
156  advance(ritr, position);
157  return *ritr;
158  }
159  break;
160 
162  return SelectRandomContainerElement(targetList);
163  break;
164  }
165 
166  return NULL;
167 }
ThreatContainer::StorageType const & getThreatList() const
Unit *const me
Definition: UnitAI.h:44
bool SelectTargetHelper(const Unit *me, const Unit *target, const bool &playerOnly, const float &dist, const int32 &aura)
Definition: UnitAI.cpp:90
ThreatManager & getThreatManager()
Definition: Unit.h:1721
C::value_type const & SelectRandomContainerElement(C const &container)
Definition: Util.h:352
void UnitAI::SelectTargetList ( std::list< Unit * > &  targetList,
uint32  num,
SelectAggroTarget  target,
float  dist = 0,
bool  playerOnly = false,
int32  aura = 0 
)

Definition at line 169 of file UnitAI.cpp.

References ThreatManager::getThreatList(), Unit::getThreatManager(), me, SELECT_TARGET_BOTTOMAGGRO, SELECT_TARGET_FARTHEST, SELECT_TARGET_NEAREST, SELECT_TARGET_RANDOM, SelectTargetHelper(), and urand().

Referenced by DamageDealt().

170 {
171  const std::list<HostileReference*>& threatlist = me->getThreatManager().getThreatList();
172  if (threatlist.empty())
173  return;
174 
175  for (std::list<HostileReference*>::const_iterator itr = threatlist.begin(); itr != threatlist.end(); ++itr)
176  if (SelectTargetHelper(me, (*itr)->getTarget(), playerOnly, dist, aura))
177  targetList.push_back((*itr)->getTarget());
178 
179  if (targetType == SELECT_TARGET_NEAREST || targetType == SELECT_TARGET_FARTHEST)
180  targetList.sort(TargetDistanceOrder(me));
181 
182  if (targetType == SELECT_TARGET_FARTHEST || targetType == SELECT_TARGET_BOTTOMAGGRO)
183  targetList.reverse();
184 
185  if (targetType == SELECT_TARGET_RANDOM)
186  {
187  while (num < targetList.size())
188  {
189  std::list<Unit*>::iterator itr = targetList.begin();
190  advance(itr, urand(0, targetList.size() - 1));
191  targetList.erase(itr);
192  }
193  }
194  else
195  targetList.resize(num);
196 }
ThreatContainer::StorageType const & getThreatList() const
Unit *const me
Definition: UnitAI.h:44
bool SelectTargetHelper(const Unit *me, const Unit *target, const bool &playerOnly, const float &dist, const int32 &aura)
Definition: UnitAI.cpp:90
ThreatManager & getThreatManager()
Definition: Unit.h:1721
uint32 urand(uint32 min, uint32 max)
Definition: Util.cpp:33
virtual void UnitAI::SetData ( uint32  ,
uint32   
)
inlinevirtual

Reimplemented in SmartAI.

Definition at line 79 of file UnitAI.h.

79 {}
virtual void UnitAI::SetGUID ( const uint64 ,
int32  id = 0 
)
inlinevirtual

Reimplemented in SmartAI.

Definition at line 80 of file UnitAI.h.

References UNUSED.

81  {
82  UNUSED(id);
83  }
#define UNUSED(x)
Definition: Common.h:175
virtual void UnitAI::sGossipHello ( Player )
inlinevirtual

Reimplemented in SmartAI.

Definition at line 112 of file UnitAI.h.

Referenced by WorldSession::HandleGossipHelloOpcode(), and WorldSession::HandleQuestgiverHelloOpcode().

112 { }
virtual void UnitAI::sGossipSelect ( Player ,
uint32  ,
uint32   
)
inlinevirtual

Reimplemented in SmartAI.

Definition at line 113 of file UnitAI.h.

Referenced by WorldSession::HandleGossipSelectOptionOpcode().

113 { }
virtual void UnitAI::sGossipSelectCode ( Player ,
uint32  ,
uint32  ,
char const *   
)
inlinevirtual

Definition at line 114 of file UnitAI.h.

Referenced by WorldSession::HandleGossipSelectOptionOpcode().

114 { }
virtual bool UnitAI::sOnDummyEffect ( Unit ,
uint32  ,
SpellEffIndex   
)
inlinevirtual

Reimplemented in SmartAI.

Definition at line 118 of file UnitAI.h.

Referenced by Aura::TriggerSpell().

118 { return false; }
virtual void UnitAI::sOnGameEvent ( bool  ,
uint16   
)
inlinevirtual

Reimplemented in SmartAI.

Definition at line 119 of file UnitAI.h.

119 { }
virtual void UnitAI::sQuestReward ( Player ,
Quest const *  ,
uint32   
)
inlinevirtual

Reimplemented in SmartAI.

Definition at line 117 of file UnitAI.h.

Referenced by WorldSession::HandleQuestgiverChooseRewardOpcode().

117 { }
virtual void UnitAI::sQuestSelect ( Player ,
Quest const *   
)
inlinevirtual

Definition at line 116 of file UnitAI.h.

116 { }

Member Data Documentation


The documentation for this class was generated from the following files: