Refactor Creature::SelectNearestTarget(float dist)
This function is used in many scripts to find the nearest enemy within the given distance. However, it had an implicit limit to the ATTACK_DISTANCE. so in many case the "dist" was in fact just ignored. In other case, the ATTACK_DISTANCE is required. So 2 functions are necessary to avoid ambiguities. The refactoring is the split of the function in 2: SelectNearestTarget and SelectNearestTargetInAttackDistance. --HG-- branch : trunk
This commit is contained in:
@@ -940,6 +940,31 @@ namespace Trinity
|
||||
|
||||
// Creature checks
|
||||
|
||||
class NearestHostileUnitCheck
|
||||
{
|
||||
public:
|
||||
explicit NearestHostileUnitCheck(Creature const* creature, float dist = 0) : me(creature)
|
||||
{
|
||||
m_range = (dist == 0 ? 9999 : dist);
|
||||
}
|
||||
bool operator()(Unit* u)
|
||||
{
|
||||
if (!me->IsWithinDistInMap(u, m_range))
|
||||
return false;
|
||||
|
||||
if (!me->canAttack(u))
|
||||
return false;
|
||||
|
||||
m_range = me->GetDistance(u); // use found unit range as new range limit for next check
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
Creature const *me;
|
||||
float m_range;
|
||||
NearestHostileUnitCheck(NearestHostileUnitCheck const&);
|
||||
};
|
||||
|
||||
class NearestHostileUnitInAttackDistanceCheck
|
||||
{
|
||||
public:
|
||||
@@ -950,7 +975,6 @@ namespace Trinity
|
||||
}
|
||||
bool operator()(Unit* u)
|
||||
{
|
||||
// TODO: addthreat for every enemy in range?
|
||||
if (!me->IsWithinDistInMap(u, m_range))
|
||||
return false;
|
||||
|
||||
@@ -965,7 +989,7 @@ namespace Trinity
|
||||
return false;
|
||||
}
|
||||
|
||||
m_range = me->GetDistance(u);
|
||||
m_range = me->GetDistance(u); // use found unit range as new range limit for next check
|
||||
return true;
|
||||
}
|
||||
float GetLastRange() const { return m_range; }
|
||||
|
||||
Reference in New Issue
Block a user