Skip to content

Commit

Permalink
Implement logic for ATTACKING_TARGET_FARTHEST_AWAY
Browse files Browse the repository at this point in the history
Signed-off-by: Xfurry <xfurry@cmangos.net>
  • Loading branch information
kartoffelsup authored and xfurry committed Sep 3, 2016
1 parent 9b5f383 commit e9f9b43
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/game/Creature.cpp
Expand Up @@ -2274,6 +2274,25 @@ Unit* Creature::SelectAttackingTarget(AttackingTarget target, uint32 position, S

break;
}
case ATTACKING_TARGET_FARTHEST_AWAY:
{
float distance = -1;
Unit* suitableTarget = nullptr;

for (; itr != threatlist.end(); ++itr)
if (Unit* pTarget = GetMap()->GetUnit((*itr)->getUnitGuid()))
{
float combatDistance = Creature::GetCombatDistance(pTarget, false);

if (combatDistance > distance && MeetsSelectAttackingRequirement(pTarget, pSpellInfo, selectFlags))
{
distance = combatDistance;
suitableTarget = pTarget;
}
}

return suitableTarget;
}
}

This comment has been minimized.

Copy link
@boxa

boxa Sep 3, 2016

Contributor

my old variant, more universally:

    case ATTACKING_TARGET_NEAREST:
    case ATTACKING_TARGET_FARTHEST:
    {
        std::list<Unit*> suitableUnits;

        for (; itr != threatlist.end(); ++itr)
        {
            if (Unit* pTarget = GetMap()->GetUnit((*itr)->getUnitGuid()))
            {
                if (!selectFlags || MeetsSelectAttackingRequirement(pTarget, pSpellInfo, selectFlags))
                    suitableUnits.push_back(pTarget);
            }
        }

        if (!suitableUnits.empty())
        {
            if (position >= suitableUnits.size())
                break;

            if (suitableUnits.size() > 1)
            {
                if (target == ATTACKING_TARGET_NEAREST)
                    suitableUnits.sort(TargetDistanceOrderNear(this));
                else
                    suitableUnits.sort(TargetDistanceOrderFarAway(this));
            }

            std::list<Unit*>::iterator itr2 = suitableUnits.begin();
            if (position)
                std::advance(itr2, position);
            return *itr2;
        }

        break;
    }

moved from Spell.cpp to Unit.h:

struct TargetDistanceOrderNear : public std::binary_function<Unit const, Unit const, bool>
{
...
}

struct TargetDistanceOrderFarAway : public std::binary_function<Unit const, Unit const, bool>
{
...
}

This comment has been minimized.

Copy link
@Phatcat

Phatcat Sep 3, 2016

Contributor

You should consider possibly watching the other cores (TBC and classic) as this has been PR'd for the TBC branch since february.. it's no issue, but I think you are right that this is a bit more flexible which is why it's a shame we didn't get to push your version instead of kartoffels right away.


return nullptr;
Expand Down
1 change: 1 addition & 0 deletions src/game/Creature.h
Expand Up @@ -319,6 +319,7 @@ enum AttackingTarget
ATTACKING_TARGET_RANDOM = 0, // Just selects a random target
ATTACKING_TARGET_TOPAGGRO, // Selects targes from top aggro to bottom
ATTACKING_TARGET_BOTTOMAGGRO, // Selects targets from bottom aggro to top
ATTACKING_TARGET_FARTHEST_AWAY, // Selects the farthest away target
};

enum SelectFlags
Expand Down

0 comments on commit e9f9b43

Please sign in to comment.