From 0dfa919f09e21309f47a1bad23f3e5a6f4789215 Mon Sep 17 00:00:00 2001 From: Ovahlord Date: Mon, 26 Jul 2021 15:48:16 +0200 Subject: [PATCH] Core/Creatures: optimized sparring checks by handling them via variable instead of map lookups and expose its value to helpers to allow manipulation during runtime --- .../game/Entities/Creature/Creature.cpp | 9 ++---- src/server/game/Entities/Creature/Creature.h | 9 ++++-- src/server/game/Entities/Unit/Unit.cpp | 32 ++++++------------- src/server/game/Spells/Spell.cpp | 12 ++----- 4 files changed, 22 insertions(+), 40 deletions(-) diff --git a/src/server/game/Entities/Creature/Creature.cpp b/src/server/game/Entities/Creature/Creature.cpp index 21ed88edf1..912a4fd68c 100644 --- a/src/server/game/Entities/Creature/Creature.cpp +++ b/src/server/game/Entities/Creature/Creature.cpp @@ -272,7 +272,7 @@ m_respawnDelay(300), m_corpseDelay(60), m_respawnradius(0.0f), m_boundaryCheckTi m_defaultMovementType(IDLE_MOTION_TYPE), m_spawnId(0), m_equipmentId(0), m_originalEquipmentId(0), m_AlreadyCallAssistance(false), m_AlreadySearchedAssistance(false), m_regenHealth(true), m_cannotReachTarget(false), m_cannotReachTimer(0), m_AI_locked(false), m_meleeDamageSchoolMask(SPELL_SCHOOL_MASK_NORMAL), m_originalEntry(0), m_homePosition(), m_transportHomePosition(), m_creatureInfo(nullptr), m_creatureData(nullptr), _waypointPathId(0), _currentWaypointNodeInfo(0, 0), _cyclicSplinePathId(0), -m_formation(nullptr), m_triggerJustAppeared(true), m_respawnCompatibilityMode(false), _isMissingSwimmingFlagOutOfCombat(false) +m_formation(nullptr), m_triggerJustAppeared(true), m_respawnCompatibilityMode(false), _isMissingSwimmingFlagOutOfCombat(false), _noNpcDamageBelowPctHealth(0.f) { m_valuesCount = UNIT_END; @@ -566,6 +566,8 @@ bool Creature::InitEntry(uint32 entry, CreatureData const* data /*= nullptr*/) for (uint8 i = 0; i < MAX_CREATURE_SPELLS; ++i) m_spells[i] = GetCreatureTemplate()->spells[i]; + SetNoNpcDamageBelowPctHealthValue(sObjectMgr->GetSparringHealthLimitFor(GetEntry())); + return true; } @@ -3362,8 +3364,3 @@ void Creature::MakeInterruptable(bool apply) ApplySpellImmune(0, IMMUNITY_MECHANIC, MECHANIC_INTERRUPT, !apply); ApplySpellImmune(0, IMMUNITY_EFFECT, SPELL_EFFECT_INTERRUPT_CAST, !apply); } - -float Creature::GetSparringHealthLimit() const -{ - return sObjectMgr->GetSparringHealthLimitFor(GetEntry()); -} diff --git a/src/server/game/Entities/Creature/Creature.h b/src/server/game/Entities/Creature/Creature.h index 874ecf3dbe..c4becbd66e 100644 --- a/src/server/game/Entities/Creature/Creature.h +++ b/src/server/game/Entities/Creature/Creature.h @@ -181,8 +181,6 @@ class TC_GAME_API Creature : public Unit, public GridObject, public Ma CreatureData const* GetCreatureData() const { return m_creatureData; } CreatureAddon const* GetCreatureAddon() const; - float GetSparringHealthLimit() const; - std::string const& GetAIName() const; std::string GetScriptName() const; uint32 GetScriptId() const; @@ -374,6 +372,11 @@ class TC_GAME_API Creature : public Unit, public GridObject, public Ma CreatureMovementInfo const& GetCreatureMovementInfo() const { return _creatureMovementInfo; } + // Sets the the max health percentage threshold at which uncontrolled/unowned creatures can no longer deal damage to the creature + void SetNoNpcDamageBelowPctHealthValue(float value) { _noNpcDamageBelowPctHealth = std::min(value, 100.f); } + void ResetNoNpcDamageBelowPctHealthValue() { _noNpcDamageBelowPctHealth = 0.f; } + float const GetNoNpcDamageBelowPctHealthValue() const { return _noNpcDamageBelowPctHealth; } + protected: bool CreateFromProto(ObjectGuid::LowType guidlow, uint32 entry, CreatureData const* data = nullptr, uint32 vehId = 0); bool InitEntry(uint32 entry, CreatureData const* data = nullptr); @@ -464,6 +467,8 @@ class TC_GAME_API Creature : public Unit, public GridObject, public Ma bool _isMissingSwimmingFlagOutOfCombat; CreatureMovementInfo _creatureMovementInfo; + + float _noNpcDamageBelowPctHealth; }; class TC_GAME_API AssistDelayEvent : public BasicEvent diff --git a/src/server/game/Entities/Unit/Unit.cpp b/src/server/game/Entities/Unit/Unit.cpp index 1e408afd7b..5a245dc3a1 100644 --- a/src/server/game/Entities/Unit/Unit.cpp +++ b/src/server/game/Entities/Unit/Unit.cpp @@ -763,15 +763,13 @@ uint32 Unit::DealDamage(Unit* victim, uint32 damage, CleanDamage const* cleanDam // Sparring Checks if (Creature* target = victim->ToCreature()) { - if (GetTypeId() == TYPEID_UNIT && !IsCharmedOwnedByPlayerOrPlayer()) + if (IsCreature() && !IsCharmedOwnedByPlayerOrPlayer()) { - float sparringLimitPct = target->GetSparringHealthLimit(); - - if (sparringLimitPct != 0.0f) + if (target->GetNoNpcDamageBelowPctHealthValue() != 0.0f) { if (damage >= target->GetHealth()) // First check: if we have a sparring limit we will never allow creatures to kill the sparring victim damage = target->GetHealth() - 1; - else if (target->GetHealthPct() <= sparringLimitPct) // Second check: stop incomming damage when we have surpassed the health limit + else if (target->GetHealthPct() <= target->GetNoNpcDamageBelowPctHealthValue()) // Second check: stop incomming damage when we have surpassed the health limit damage = 0; } } @@ -1949,16 +1947,10 @@ void Unit::CalcAbsorbResist(DamageInfo& damageInfo) // Sparring Checks if (Creature* target = damageInfo.GetVictim()->ToCreature()) - { - if (caster->GetTypeId() == TYPEID_UNIT && !caster->IsCharmedOwnedByPlayerOrPlayer()) - { - float sparringLimitPct = target->GetSparringHealthLimit(); - - if (sparringLimitPct != 0.0f) - if (target->GetHealthPct() <= sparringLimitPct) + if (IsCreature() && !IsCharmedOwnedByPlayerOrPlayer()) + if (target->GetNoNpcDamageBelowPctHealthValue() != 0.0f) + if (target->GetHealthPct() <= target->GetNoNpcDamageBelowPctHealthValue()) damageInfo.ModifyDamage(damageInfo.GetDamage() * -1); - } - } SendSpellNonMeleeDamageLog(caster, (*itr)->GetSpellInfo()->Id, splitDamage, damageInfo.GetSchoolMask(), split_absorb, 0, false, 0, false); @@ -2096,16 +2088,10 @@ void Unit::AttackerStateUpdate(Unit* victim, WeaponAttackType attType, bool extr // Sparring Checks if (Creature* target = victim->ToCreature()) - { - if (GetTypeId() == TYPEID_UNIT && !IsCharmedOwnedByPlayerOrPlayer()) - { - float sparringLimitPct = target->GetSparringHealthLimit(); - - if (sparringLimitPct != 0.0f) - if (target->GetHealthPct() <= sparringLimitPct) + if (IsCreature() && !IsCharmedOwnedByPlayerOrPlayer()) + if (target->GetNoNpcDamageBelowPctHealthValue() != 0.0f) + if (target->GetHealthPct() <= target->GetNoNpcDamageBelowPctHealthValue()) damageInfo.HitInfo |= HITINFO_FAKE_DAMAGE; - } - } SendAttackStateUpdate(&damageInfo); diff --git a/src/server/game/Spells/Spell.cpp b/src/server/game/Spells/Spell.cpp index d9b5529338..f1712b7574 100644 --- a/src/server/game/Spells/Spell.cpp +++ b/src/server/game/Spells/Spell.cpp @@ -2737,16 +2737,10 @@ void Spell::DoAllEffectOnTarget(TargetInfo* target) caster->DealDamageMods(damageInfo.target, damageInfo.damage, &damageInfo.absorb); if (Creature* target = damageInfo.target->ToCreature()) - { - if (caster->GetTypeId() == TYPEID_UNIT && !caster->IsCharmedOwnedByPlayerOrPlayer()) - { - float sparringLimitPct = target->GetSparringHealthLimit(); - - if (sparringLimitPct != 0.0f) - if (target->GetHealthPct() <= sparringLimitPct) + if (caster->IsCreature() && !caster->IsCharmedOwnedByPlayerOrPlayer()) + if (target->GetNoNpcDamageBelowPctHealthValue() != 0.0f) + if (target->GetHealthPct() <= target->GetNoNpcDamageBelowPctHealthValue()) damageInfo.damage = 0; - } - } // Send log damage message to client caster->SendSpellNonMeleeDamageLog(&damageInfo);