From dc3de04d62b04c4bea1acda8139dfadde16062df Mon Sep 17 00:00:00 2001 From: Schmoozerd Date: Fri, 31 May 2013 12:11:23 +0100 Subject: [PATCH] [c12616] Simplify use of CombatReach --- src/game/Creature.cpp | 6 +++--- src/game/CreatureAI.cpp | 2 +- src/game/Spell.cpp | 2 +- src/game/Unit.cpp | 37 +++++++++++++++++++++++-------------- src/game/Unit.h | 9 +++++++-- src/shared/revision_nr.h | 2 +- 6 files changed, 36 insertions(+), 22 deletions(-) diff --git a/src/game/Creature.cpp b/src/game/Creature.cpp index 946204fa4..ba71b6db9 100644 --- a/src/game/Creature.cpp +++ b/src/game/Creature.cpp @@ -1683,7 +1683,7 @@ SpellEntry const* Creature::ReachWithSpellAttack(Unit* pVictim) float range = GetSpellMaxRange(srange); float minrange = GetSpellMinRange(srange); - float dist = GetCombatDistance(pVictim); + float dist = GetCombatDistance(pVictim, spellInfo->rangeIndex == SPELL_RANGE_IDX_COMBAT); // if(!isInFront( pVictim, range ) && spellInfo->AttributesEx ) // continue; @@ -1739,7 +1739,7 @@ SpellEntry const* Creature::ReachWithSpellCure(Unit* pVictim) float range = GetSpellMaxRange(srange); float minrange = GetSpellMinRange(srange); - float dist = GetCombatDistance(pVictim); + float dist = GetCombatDistance(pVictim, spellInfo->rangeIndex == SPELL_RANGE_IDX_COMBAT); // if(!isInFront( pVictim, range ) && spellInfo->AttributesEx ) // continue; @@ -2096,7 +2096,7 @@ bool Creature::MeetsSelectAttackingRequirement(Unit* pTarget, SpellEntry const* SpellRangeEntry const* srange = sSpellRangeStore.LookupEntry(pSpellInfo->rangeIndex); float max_range = GetSpellMaxRange(srange); float min_range = GetSpellMinRange(srange); - float dist = GetCombatDistance(pTarget); + float dist = GetCombatDistance(pTarget, false); return dist < max_range && dist >= min_range; } diff --git a/src/game/CreatureAI.cpp b/src/game/CreatureAI.cpp index 97e3600f4..2cc22c78b 100644 --- a/src/game/CreatureAI.cpp +++ b/src/game/CreatureAI.cpp @@ -56,7 +56,7 @@ CanCastResult CreatureAI::CanCastSpell(Unit* pTarget, const SpellEntry* pSpell, if (pTarget != m_creature) { // pTarget is out of range of this spell (also done by Spell::CheckCast()) - float fDistance = m_creature->GetCombatDistance(pTarget); + float fDistance = m_creature->GetCombatDistance(pTarget, pSpell->rangeIndex == SPELL_RANGE_IDX_COMBAT); if (fDistance > (m_creature->IsHostileTo(pTarget) ? pSpellRange->maxRange : pSpellRange->maxRangeFriendly)) return CAST_FAIL_TOO_FAR; diff --git a/src/game/Spell.cpp b/src/game/Spell.cpp index 00fda0fc4..7bd1398bb 100644 --- a/src/game/Spell.cpp +++ b/src/game/Spell.cpp @@ -6554,7 +6554,7 @@ SpellCastResult Spell::CheckRange(bool strict) if (target && target != m_caster) { // distance from target in checks - float dist = m_caster->GetCombatDistance(target); + float dist = m_caster->GetCombatDistance(target, m_spellInfo->rangeIndex == SPELL_RANGE_IDX_COMBAT); if (dist > max_range) return SPELL_FAILED_OUT_OF_RANGE; diff --git a/src/game/Unit.cpp b/src/game/Unit.cpp index 18d263c15..560c433e9 100644 --- a/src/game/Unit.cpp +++ b/src/game/Unit.cpp @@ -738,17 +738,36 @@ void Unit::resetAttackTimer(WeaponAttackType type) m_attackTimer[type] = uint32(GetAttackTime(type) * m_modAttackSpeedPct[type]); } -bool Unit::CanReachWithMeleeAttack(Unit* pVictim, float flat_mod /*= 0.0f*/) const +float Unit::GetCombatReach(Unit const* pVictim, bool forMeleeRange /*=true*/, float flat_mod /*=0.0f*/) const { - MANGOS_ASSERT(pVictim); - // The measured values show BASE_MELEE_OFFSET in (1.3224, 1.342) float reach = GetFloatValue(UNIT_FIELD_COMBATREACH) + pVictim->GetFloatValue(UNIT_FIELD_COMBATREACH) + BASE_MELEERANGE_OFFSET + flat_mod; - if (reach < ATTACK_DISTANCE) + if (forMeleeRange && reach < ATTACK_DISTANCE) reach = ATTACK_DISTANCE; + return reach; +} + +float Unit::GetCombatDistance(Unit const* target, bool forMeleeRange) const +{ + float radius = GetCombatReach(target, forMeleeRange); + + float dx = GetPositionX() - target->GetPositionX(); + float dy = GetPositionY() - target->GetPositionY(); + float dz = GetPositionZ() - target->GetPositionZ(); + float dist = sqrt((dx * dx) + (dy * dy) + (dz * dz)) - radius; + + return (dist > 0.0f ? dist : 0.0f); +} + +bool Unit::CanReachWithMeleeAttack(Unit const* pVictim, float flat_mod /*= 0.0f*/) const +{ + MANGOS_ASSERT(pVictim); + + float reach = GetCombatReach(pVictim, true, flat_mod); + // This check is not related to bounding radius float dx = GetPositionX() - pVictim->GetPositionX(); float dy = GetPositionY() - pVictim->GetPositionY(); @@ -6330,16 +6349,6 @@ void Unit::Uncharm() } } -float Unit::GetCombatDistance(const Unit* target) const -{ - float radius = target->GetFloatValue(UNIT_FIELD_COMBATREACH) + GetFloatValue(UNIT_FIELD_COMBATREACH); - float dx = GetPositionX() - target->GetPositionX(); - float dy = GetPositionY() - target->GetPositionY(); - float dz = GetPositionZ() - target->GetPositionZ(); - float dist = sqrt((dx * dx) + (dy * dy) + (dz * dz)) - radius; - return (dist > 0 ? dist : 0); -} - void Unit::SetPet(Pet* pet) { SetPetGuid(pet ? pet->GetObjectGuid() : ObjectGuid()); diff --git a/src/game/Unit.h b/src/game/Unit.h index 44b686b7a..f84900daa 100644 --- a/src/game/Unit.h +++ b/src/game/Unit.h @@ -1187,7 +1187,13 @@ class MANGOS_DLL_SPEC Unit : public WorldObject return !HasFlag(UNIT_FIELD_FLAGS_2, UNIT_FLAG2_DISARM_RANGED); } } - bool CanReachWithMeleeAttack(Unit* pVictim, float flat_mod = 0.0f) const; + + /// Returns the combined combat reach of two mobs + float GetCombatReach(Unit const* pVictim, bool forMeleeRange = true, float flat_mod = 0.0f) const; + /// Returns the remaining combat distance between two mobs (CombatReach substracted) + float GetCombatDistance(Unit const* target, bool forMeleeRange) const; + /// Returns if the Unit can reach a victim with Melee Attack + bool CanReachWithMeleeAttack(Unit const* pVictim, float flat_mod = 0.0f) const; uint32 m_extraAttacks; void _addAttacker(Unit* pAttacker) // must be called only from Unit::Attack(Unit*) @@ -1540,7 +1546,6 @@ class MANGOS_DLL_SPEC Unit : public WorldObject bool IsCharmerOrOwnerPlayerOrPlayerItself() const; Player* GetCharmerOrOwnerPlayerOrPlayerItself(); Player const* GetCharmerOrOwnerPlayerOrPlayerItself() const; - float GetCombatDistance(const Unit* target) const; void SetPet(Pet* pet); void SetCharm(Unit* pet); diff --git a/src/shared/revision_nr.h b/src/shared/revision_nr.h index 4362e4af0..bfec4fe40 100644 --- a/src/shared/revision_nr.h +++ b/src/shared/revision_nr.h @@ -1,4 +1,4 @@ #ifndef __REVISION_NR_H__ #define __REVISION_NR_H__ - #define REVISION_NR "12615" + #define REVISION_NR "12616" #endif // __REVISION_NR_H__