aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/game/Creature.cpp37
-rw-r--r--src/game/Creature.h1
-rw-r--r--src/game/Unit.cpp5
3 files changed, 30 insertions, 13 deletions
diff --git a/src/game/Creature.cpp b/src/game/Creature.cpp
index 1dc436b104f..fa710a41be3 100644
--- a/src/game/Creature.cpp
+++ b/src/game/Creature.cpp
@@ -1689,8 +1689,7 @@ bool Creature::canStartAttack(Unit const* who, bool force) const
if(!force)
{
- // if victim(1) has a victim(2), and victim(2) is a non-friendly player, don't attack victim(1) unless forced
- if(who->getVictim() && who->getVictim()->GetCharmerOrOwnerPlayerOrPlayerItself() && !IsFriendlyTo(who->getVictim()))
+ if(!_IsTargetAcceptable(who))
return false;
if(who->isInCombat())
@@ -2149,14 +2148,6 @@ bool Creature::CanAssistTo(const Unit* u, const Unit* enemy, bool checkfaction /
if (GetCharmerOrOwnerGUID())
return false;
- // don't help players or units controlled or owned by players
- if (u->GetCharmerOrOwnerPlayerOrPlayerItself())
- return false;
-
- // don't help if the unit is fleeing from the enemy player
- if (!u->isAttackingPlayer() && enemy->GetTypeId() == TYPEID_PLAYER)
- return false;
-
// only from same creature faction
if (checkfaction)
{
@@ -2176,6 +2167,32 @@ bool Creature::CanAssistTo(const Unit* u, const Unit* enemy, bool checkfaction /
return true;
}
+// use this function to avoid having hostile creatures attack
+// friendlies and other mobs they shouldn't attack
+bool Creature::_IsTargetAcceptable(const Unit* target) const
+{
+ const Unit* targetVictim = target->getAttackerForHelper();
+
+ if(!targetVictim) // if target does not have a victim, the target is acceptable
+ return true;
+
+ const Player* targetVictimPlayer = targetVictim->GetCharmerOrOwnerPlayerOrPlayerItself();
+
+ if(!targetVictimPlayer) // if the victim is not a player, the target is acceptable
+ return true;
+
+ // if the victim is a non-friendly player, the target is not acceptable
+ if(!IsFriendlyTo(targetVictim))
+ return false;
+
+ // if the target is passive, the target is not acceptable
+ if(((Creature*)target)->GetReactState() == REACT_PASSIVE)
+ return false;
+
+ // otherwise, the target is acceptable
+ return true;
+}
+
void Creature::SaveRespawnTime()
{
if(isSummon() || !m_DBTableGuid || m_creatureData && !m_creatureData->dbData)
diff --git a/src/game/Creature.h b/src/game/Creature.h
index bfe9709e081..5fbe4a07a28 100644
--- a/src/game/Creature.h
+++ b/src/game/Creature.h
@@ -641,6 +641,7 @@ class TRINITY_DLL_SPEC Creature : public Unit
void SetNoSearchAssistance(bool val) { m_AlreadySearchedAssistance = val; }
bool HasSearchedAssistance() { return m_AlreadySearchedAssistance; }
bool CanAssistTo(const Unit* u, const Unit* enemy, bool checkfaction = true) const;
+ bool _IsTargetAcceptable(const Unit* target) const;
MovementGeneratorType GetDefaultMovementType() const { return m_defaultMovementType; }
void SetDefaultMovementType(MovementGeneratorType mgt) { m_defaultMovementType = mgt; }
diff --git a/src/game/Unit.cpp b/src/game/Unit.cpp
index da42631e135..2db1ad96a8a 100644
--- a/src/game/Unit.cpp
+++ b/src/game/Unit.cpp
@@ -11344,7 +11344,7 @@ Unit* Creature::SelectVictim()
else
return NULL;
- if(target && (!target->getVictim() || IsFriendlyTo(target->getVictim()))) // if victim(1) has a victim(2), only attack victim(1) if we are friendly with victim(2)
+ if(target && _IsTargetAcceptable(target))
{
SetInFront(target);
return target;
@@ -11358,7 +11358,6 @@ Unit* Creature::SelectVictim()
{
if((*itr) && canCreatureAttack(*itr) && (*itr)->GetTypeId() != TYPEID_PLAYER
&& !((Creature*)(*itr))->HasUnitTypeMask(UNIT_MASK_CONTROLABLE_GUARDIAN))
- // && (!(*itr)->getVictim() || IsFriendlyTo((*itr)->getVictim()))) // if victim(1) has a victim(2), only attack victim(1) if we are friendly with victim(2)
return NULL;
}
@@ -11369,7 +11368,7 @@ Unit* Creature::SelectVictim()
// search nearby enemy before enter evade mode
if(HasReactState(REACT_AGGRESSIVE))
if(target = SelectNearestTarget())
- if(!target->getVictim() || IsFriendlyTo(target->getVictim())) // if victim(1) has a victim(2), only attack victim(1) if we are friendly with victim(2)
+ if(_IsTargetAcceptable(target))
return target;
if(m_invisibilityMask)