NearestHostileUnitCheck - before I break something...

I used SelectNearestTarget in the PetAI because it indicated in the comments that it returns the nearest hostile target by using NearestHostileUnitCheck. Problem is, that piece of code doesn’t only return hostile targets but neutral as well.

If I add a IsHostileTo() check (as noted below), will it break anything? I’m not sure which other parts of the core might use this function but it seems logical that based on its name it shouldn’t be returning neutral mobs like Boars and such.

[CODE] 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->IsValidAttackTarget(u))

                return false;                  

            // Without this it also returns neutral mobs. This is supposed to only return hostile mobs.
            if (!u->IsHostileTo(me))
                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&);

};[/CODE]

Add a boolean defaulting to false. If true, check if the mob is really hostile, not simply neutral. Sounds like the less intrusive method.

Well what I meant by break things was is there any reason not to expect this function to only return hostile mobs? Right now I just run the check outside of the function in the PetAI but its name implies I really shouldn’t have to.

So after further testing, this function doesn’t work as its name implies. When setting pets to aggressive and calling this function, it returns rabbits, cows, boars and other neutral mobs.

Instead of rewriting the function, I renamed it something more appropriate and wrote a new function. I will present this in my next PetAI update. Here are some details:

NearestAttackableUnitCheck

This is a copy of the original NearestHostileUnitCheck which was globally replaced throughout the solution because it returns both hostile and neutral (only attackable neutral) results.

It is called by existing functions: SelectNearestTarget() and SelectNearestTargetInAttackDistance()

NearestHostileUnitCheck

This has been rewritten to exclude neutral targets. It also has an optional useLOS parameter which will exclude all units that are not within LOS even if they are attackable.

It is called by a new function: SelectNearestHostileTarget()