Jump to content

[one]SelectAttackingTarget


Auntie Mangos
 Share

Recommended Posts

  • 41 years later...

a few more features for this method

diff --git a/src/game/Creature.h b/src/game/Creature.h
index b4421cb..0534698 100644
--- a/src/game/Creature.h
+++ b/src/game/Creature.h
@@ -284,11 +284,28 @@ 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
-    /* not implemented
    ATTACKING_TARGET_RANDOM_PLAYER,                         //Just selects a random target (player only)
    ATTACKING_TARGET_TOPAGGRO_PLAYER,                       //Selects targes from top aggro to bottom (player only)
    ATTACKING_TARGET_BOTTOMAGGRO_PLAYER,                    //Selects targets from bottom aggro to top (player only)
-    */
+      ATTACKING_TARGET_NEAREST_PLAYER,
+	ATTACKING_TARGET_FARTHEST_PLAYER,
+	ATTACKING_TARGET_LOWEST_HP,
+	ATTACKING_TARGET_HIGHEST_HP,
+};
+//
+enum AttackingTargetRangeFlag
+{
+	AT_RANGE_FLAG_NO_FLAG			= 0,
+	AT_RANGE_FLAG_IN_MELEE_RANGE	= 1,
+	AT_RANGE_FLAG_OUT_OF_MELEE_RANGE= 2,
+
+};
+//add rage & energy?
+enum AttackingTargetExtraFlag
+{
+	AT_EXTRA_FLAG_NO_FLAG			= 0,
+	AT_EXTRA_FLAG_NO_MANA_CLASS		= 1,
+	AT_EXTRA_FLAG_MANA_CLASS		= 2,
};

// Vendors
@@ -674,7 +691,7 @@ class MANGOS_DLL_SPEC Creature : public Unit

        void SetInCombatWithZone();

-        Unit* SelectAttackingTarget(AttackingTarget target, uint32 position) const;
+       Unit* SelectAttackingTarget(AttackingTarget target, uint32 position, AttackingTargetRangeFlag  RangeFlag = AT_RANGE_FLAG_NO_FLAG, AttackingTargetExtraFlag ExtraFlag = AT_EXTRA_FLAG_NO_FLAG) const;

        bool HasQuest(uint32 quest_id) const;
        bool HasInvolvedQuest(uint32 quest_id)  const;

diff --git a/src/game/Creature.cpp b/src/game/Creature.cpp
index 82bbb67..ffeaf33 100644
--- a/src/game/Creature.cpp
+++ b/src/game/Creature.cpp
@@ -2019,7 +2019,8 @@ void Creature::SetInCombatWithZone()
    }
}

-Unit* Creature::SelectAttackingTarget(AttackingTarget target, uint32 position) const
+
+Unit* Creature::SelectAttackingTarget(AttackingTarget target, uint32 position, AttackingTargetRangeFlag  RangeFlag, AttackingTargetExtraFlag ExtraFlag) const
{
    if (!CanHaveThreatList())
        return NULL;
@@ -2027,33 +2028,101 @@ Unit* Creature::SelectAttackingTarget(AttackingTarget target, uint32 position) c
    //ThreatList m_threatlist;
    ThreatList const& threatlist = getThreatManager().getThreatList();
    ThreatList::const_iterator i = threatlist.begin();
-    ThreatList::const_reverse_iterator r = threatlist.rbegin();

    if (position >= threatlist.size() || !threatlist.size())
        return NULL;

+
+	std::vector<Unit *> target_list;
+
+	//prepare target list
+	for (;i != threatlist.end(); ++i)
+	{
+		Unit* pTarget = GetMap()->GetUnit((*i)->getUnitGuid());
+		
+		// only player cases:
+		if  (target == ATTACKING_TARGET_RANDOM_PLAYER ||  target == ATTACKING_TARGET_TOPAGGRO_PLAYER ||  target == ATTACKING_TARGET_BOTTOMAGGRO_PLAYER
+			|| target ==  ATTACKING_TARGET_NEAREST_PLAYER || target == ATTACKING_TARGET_FARTHEST_PLAYER )
+		{
+			if (pTarget && (pTarget->GetTypeId() != TYPEID_PLAYER || !pTarget->isAlive()))
+			{
+				pTarget = NULL;
+				continue;
+			}
+		}
+
+		//check range flags
+		if ((RangeFlag==AT_RANGE_FLAG_OUT_OF_MELEE_RANGE && this->CanReachWithMeleeAttack(pTarget))
+			|| (RangeFlag==AT_RANGE_FLAG_IN_MELEE_RANGE  && !this->CanReachWithMeleeAttack(pTarget)))
+		{
+			pTarget = NULL;
+			continue;
+		}
+		
+		//check extra flags
+		if 	((ExtraFlag == AT_EXTRA_FLAG_NO_MANA_CLASS && pTarget->getPowerType() == POWER_MANA)
+			|| (ExtraFlag == AT_EXTRA_FLAG_MANA_CLASS && pTarget->getPowerType() != POWER_MANA))
+		{
+			pTarget = NULL;
+			continue;
+		}
+
+		if (pTarget)
+			target_list.push_back(pTarget);
+	}
+	
+	//Flags excluded all existing targets:
+	if (!target_list.size())
+		return NULL;
+
+	//select target:
+
    switch(target)
    {
        case ATTACKING_TARGET_RANDOM:
+		case ATTACKING_TARGET_RANDOM_PLAYER:
        {
-            advance(i, position + (rand() % (threatlist.size() - position)));
-            return GetMap()->GetUnit((*i)->getUnitGuid());
+			return *(target_list.begin()+position+rand()%target_list.size());
        }
        case ATTACKING_TARGET_TOPAGGRO:
+		case ATTACKING_TARGET_TOPAGGRO_PLAYER: 
        {
-            advance(i, position);
-            return GetMap()->GetUnit((*i)->getUnitGuid());
+            return *(target_list.begin()+position);
        }
        case ATTACKING_TARGET_BOTTOMAGGRO:
+		case ATTACKING_TARGET_BOTTOMAGGRO_PLAYER:
        {
-            advance(r, position);
-            return GetMap()->GetUnit((*r)->getUnitGuid());
+            return *(target_list.end()-position);
        }
-        // TODO: implement these
-        //case ATTACKING_TARGET_RANDOM_PLAYER:
-        //case ATTACKING_TARGET_TOPAGGRO_PLAYER:
-        //case ATTACKING_TARGET_BOTTOMAGGRO_PLAYER:
-    }
+		case ATTACKING_TARGET_NEAREST_PLAYER:
+		case ATTACKING_TARGET_FARTHEST_PLAYER:
+		{
+			Unit* currentTarget = NULL;
+			for (size_t s_i = 0 ; s_i < target_list.size() ; ++s_i)
+			{
+				if (!currentTarget || 
+					(target == ATTACKING_TARGET_NEAREST_PLAYER ? this->GetDistance(currentTarget) > this->GetDistance(target_list[s_i]) :
+																 this->GetDistance(currentTarget) < this->GetDistance(target_list[s_i])))
+
+					currentTarget = target_list[s_i];
+			}
+			return currentTarget;
+		}
+		case ATTACKING_TARGET_LOWEST_HP:
+		case ATTACKING_TARGET_HIGHEST_HP:
+		{
+			Unit* currentTarget = NULL;
+			for (size_t s_i = 0 ; s_i < target_list.size() ; ++s_i)
+			{
+				if (!currentTarget || 
+					(target == ATTACKING_TARGET_LOWEST_HP ? currentTarget->GetHealth() > target_list[s_i]->GetHealth() :
+															currentTarget->GetHealth() < target_list[s_i]->GetHealth()))
+
+					currentTarget = target_list[s_i];
+			}
+			return currentTarget;
+		}
+	}

    return NULL;
}

Link to comment
Share on other sites

  • 4 weeks later...
  • 1 month later...
Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy Terms of Use