diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/game/Unit.cpp | 69 | ||||
-rw-r--r-- | src/game/Unit.h | 1 |
2 files changed, 56 insertions, 14 deletions
diff --git a/src/game/Unit.cpp b/src/game/Unit.cpp index cee275d0e4f..5214aa81d40 100644 --- a/src/game/Unit.cpp +++ b/src/game/Unit.cpp @@ -7646,6 +7646,59 @@ void Unit::SetCharm(Unit* pet) SetUInt64Value(UNIT_FIELD_CHARM, pet ? pet->GetGUID() : 0); } +Unit* Unit::GetNextRandomRaidMemberOrPet(float radius) +{ + Player* player = NULL; + if(GetTypeId() == TYPEID_PLAYER) + player = (Player*)this; + // Should we enable this also for charmed units? + else if (GetTypeId() == TYPEID_UNIT && ((Creature*)this)->isPet()) + player=(Player*)GetOwner(); + + if (!player) + return NULL; + Group *pGroup = player->GetGroup(); + //When there is no group check pet presence + if (!pGroup) + { + // We are pet now, return owner + if(player!=this) + return IsWithinDistInMap(player, radius) ? player : NULL; + Unit * pet = GetPet(); + //No pet, no group, nothing to return + if (!pet) + return NULL; + // We are owner now, return pet + return IsWithinDistInMap(pet, radius) ? pet : NULL; + } + + std::vector<Unit*> nearMembers; + //reserve place for players and pets because resizing vector every unit push is unefficient (vector is reallocated then) + nearMembers.reserve(pGroup->GetMembersCount()*2); + + for(GroupReference *itr = pGroup->GetFirstMember(); itr != NULL; itr = itr->next()) + { + Player* Target = itr->getSource(); + + // IsHostileTo check duel and controlled by enemy + if( Target && Target !=this && IsWithinDistInMap(Target, radius) && + !IsHostileTo(Target) ) + nearMembers.push_back(Target); + + // Push player's pet to vector + Unit * pet = Target->GetPet(); + if (pet && pet !=this && IsWithinDistInMap(pet, radius) && + !IsHostileTo(pet) ) + nearMembers.push_back(pet); + } + + if (nearMembers.empty()) + return NULL; + + uint32 randTarget = urand(0,nearMembers.size()-1); + return nearMembers[randTarget]; +} + void Unit::AddPlayerToVision(Player* plr) { if (m_sharedVision.empty() && GetTypeId() == TYPEID_UNIT @@ -11996,13 +12049,7 @@ bool Unit::HandleAuraRaidProcFromChargeWithValue( Aura* triggeredByAura ) caster->ApplySpellMod(spellProto->Id, SPELLMOD_CHARGES, maxJumps, NULL); - Player* player = NULL; - if(GetTypeId() == TYPEID_PLAYER) - player = (Player*)this; - else if (GetTypeId() == TYPEID_UNIT && ((Creature*)this)->isPet()) - player=(Player*)GetOwner(); - Player* target= player ? player->GetNextRandomRaidMember(radius): NULL; - if (target) + if (Unit* target= GetNextRandomRaidMemberOrPet(radius)) { // aura will applied from caster, but spell casted from current aura holder SpellModifier *mod = new SpellModifier; @@ -12076,13 +12123,7 @@ bool Unit::HandleAuraRaidProcFromCharge( Aura* triggeredByAura ) caster->ApplySpellMod(spellProto->Id, SPELLMOD_CHARGES, maxJumps, NULL); - Player* player = NULL; - if(GetTypeId() == TYPEID_PLAYER) - player = (Player*)this; - else if (GetTypeId() == TYPEID_UNIT && ((Creature*)this)->isPet()) - player=(Player*)GetOwner(); - Player* target= player ? player->GetNextRandomRaidMember(radius): NULL; - if (target) + if (Unit* target= GetNextRandomRaidMemberOrPet(radius)) { // aura will applied from caster, but spell casted from current aura holder SpellModifier *mod = new SpellModifier; diff --git a/src/game/Unit.h b/src/game/Unit.h index d57440c4cd4..f199e19070b 100644 --- a/src/game/Unit.h +++ b/src/game/Unit.h @@ -1172,6 +1172,7 @@ class TRINITY_DLL_SPEC Unit : public WorldObject void SetPet(Pet* pet); void SetCharm(Unit* pet); + Unit* GetNextRandomRaidMemberOrPet(float radius); void SetCharmedOrPossessedBy(Unit* charmer, bool possess); void RemoveCharmedOrPossessedBy(Unit* charmer); void RestoreFaction(); |