From a5cfd149b72746c7eeb8f06599c94d63a3d379dc Mon Sep 17 00:00:00 2001 From: hondacrx Date: Wed, 7 Sep 2022 13:23:53 -0400 Subject: [PATCH] Core/Combat: Allow PvE combat references to become suppressed, just like PvP ones Port From (https://github.com/TrinityCore/TrinityCore/commit/9bfc29f4aff01cd7f366faffcda0a84071970d6a) --- Source/Game/Combat/CombatManager.cs | 97 ++++++++++++++---------- Source/Game/Entities/Unit/Unit.Combat.cs | 4 +- 2 files changed, 58 insertions(+), 43 deletions(-) diff --git a/Source/Game/Combat/CombatManager.cs b/Source/Game/Combat/CombatManager.cs index 6c1184d47..da85aef6a 100644 --- a/Source/Game/Combat/CombatManager.cs +++ b/Source/Game/Combat/CombatManager.cs @@ -83,10 +83,18 @@ namespace Game.Combat } } + public bool HasPvECombat() + { + foreach (var (_, refe) in _pveRefs) + if (!refe.IsSuppressedFor(_owner)) + return true; + return false; + } + public bool HasPvECombatWithPlayers() { foreach (var reference in _pveRefs) - if (reference.Value.GetOther(_owner).IsPlayer()) + if (!reference.Value.IsSuppressedFor(_owner) && reference.Value.GetOther(_owner).IsPlayer()) return true; return false; @@ -103,8 +111,9 @@ namespace Game.Combat public Unit GetAnyTarget() { - if (!_pveRefs.Empty()) - return _pveRefs.First().Value.GetOther(_owner); + foreach (var pair in _pveRefs) + if (!pair.Value.IsSuppressedFor(_owner)) + return pair.Value.GetOther(_owner); foreach (var pair in _pvpRefs) if (!pair.Value.IsSuppressedFor(_owner)) @@ -113,17 +122,23 @@ namespace Game.Combat return null; } - public bool SetInCombatWith(Unit who, bool suppressPvpSecond = false) + public bool SetInCombatWith(Unit who, bool addSecondUnitSuppressed = false) { // Are we already in combat? If yes, refresh pvp combat - var pvpRefe = _pvpRefs.LookupByKey(who.GetGUID()); - if (pvpRefe != null) + var existingPvpRef = _pvpRefs.LookupByKey(who.GetGUID()); + if (existingPvpRef != null) { - pvpRefe.Refresh(); + existingPvpRef.RefreshTimer(); + existingPvpRef.Refresh(); return true; } - else if (_pveRefs.ContainsKey(who.GetGUID())) + + var existingPveRef = _pveRefs.LookupByKey(who.GetGUID()); + if (existingPveRef != null) + { + existingPveRef.Refresh(); return true; + } // Otherwise, check validity... if (!CanBeginCombat(_owner, who)) @@ -132,15 +147,13 @@ namespace Game.Combat // ...then create new reference CombatReference refe; if (_owner.IsControlledByPlayer() && who.IsControlledByPlayer()) - { - PvPCombatReference refPvp = new PvPCombatReference(_owner, who); - if (suppressPvpSecond) - refPvp.SuppressFor(who); - refe = refPvp; - } + refe = new PvPCombatReference(_owner, who); else refe = new CombatReference(_owner, who); + if (addSecondUnitSuppressed) + refe.Suppress(who); + // ...and insert it into both managers PutReference(who.GetGUID(), refe); who.GetCombatManager().PutReference(_owner.GetGUID(), refe); @@ -324,13 +337,10 @@ namespace Game.Combat return true; } - - Unit GetOwner() { return _owner; } + public Unit GetOwner() { return _owner; } public bool HasCombat() { return HasPvECombat() || HasPvPCombat(); } - public bool HasPvECombat() { return !_pveRefs.Empty(); } - public Dictionary GetPvECombatRefs() { return _pveRefs; } public Dictionary GetPvPCombatRefs() { return _pvpRefs; } @@ -347,6 +357,9 @@ namespace Game.Combat public Unit first; public Unit second; public bool _isPvP; + + bool _suppressFirst; + bool _suppressSecond; public CombatReference(Unit a, Unit b, bool pvp = false) { @@ -386,31 +399,8 @@ namespace Game.Combat } } - public Unit GetOther(Unit me) { return (first == me) ? second : first; } - } - - public class PvPCombatReference : CombatReference - { - public static uint PVP_COMBAT_TIMEOUT = 5 * Time.InMilliseconds; - - uint _combatTimer = PVP_COMBAT_TIMEOUT; - bool _suppressFirst = false; - bool _suppressSecond = false; - - public PvPCombatReference(Unit first, Unit second) : base(first, second, true) { } - - public bool Update(uint tdiff) - { - if (_combatTimer <= tdiff) - return false; - _combatTimer -= tdiff; - return true; - } - public void Refresh() { - _combatTimer = PVP_COMBAT_TIMEOUT; - bool needFirstAI = false, needSecondAI = false; if (_suppressFirst) { @@ -451,5 +441,30 @@ namespace Game.Combat else _suppressSecond = true; } + + public Unit GetOther(Unit me) { return (first == me) ? second : first; } + } + + public class PvPCombatReference : CombatReference + { + public static uint PVP_COMBAT_TIMEOUT = 5 * Time.InMilliseconds; + + uint _combatTimer = PVP_COMBAT_TIMEOUT; + + + public PvPCombatReference(Unit first, Unit second) : base(first, second, true) { } + + public bool Update(uint tdiff) + { + if (_combatTimer <= tdiff) + return false; + _combatTimer -= tdiff; + return true; + } + + public void RefreshTimer() + { + _combatTimer = PVP_COMBAT_TIMEOUT; + } } } diff --git a/Source/Game/Entities/Unit/Unit.Combat.cs b/Source/Game/Entities/Unit/Unit.Combat.cs index 804cabea1..4a81040e2 100644 --- a/Source/Game/Entities/Unit/Unit.Combat.cs +++ b/Source/Game/Entities/Unit/Unit.Combat.cs @@ -95,10 +95,10 @@ namespace Game.Entities public bool IsPetInCombat() { return HasUnitFlag(UnitFlags.PetInCombat); } - public void SetInCombatWith(Unit enemy, bool suppressPvpTargetCombat = false) + public void SetInCombatWith(Unit enemy, bool addSecondUnitSuppressed = false) { if (enemy != null) - m_combatManager.SetInCombatWith(enemy, suppressPvpTargetCombat); + m_combatManager.SetInCombatWith(enemy, addSecondUnitSuppressed); } public void EngageWithTarget(Unit enemy)