From 990e86b983c3a7d2bbc4ef9c992bd0781537f940 Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Tue, 6 May 2025 23:54:13 -0700 Subject: [PATCH] fix: Fixes guard infinite loop, and streamlines call methods (#2175) --- .../UOContent/Mobiles/Guards/ArcherGuard.cs | 2 +- .../UOContent/Mobiles/Guards/BaseGuard.cs | 35 ++++++++----------- .../UOContent/Mobiles/Guards/WarriorGuard.cs | 2 +- Projects/UOContent/Regions/GuardedRegion.cs | 31 ++++------------ 4 files changed, 24 insertions(+), 46 deletions(-) diff --git a/Projects/UOContent/Mobiles/Guards/ArcherGuard.cs b/Projects/UOContent/Mobiles/Guards/ArcherGuard.cs index 6ef01d1ce..895e0724f 100644 --- a/Projects/UOContent/Mobiles/Guards/ArcherGuard.cs +++ b/Projects/UOContent/Mobiles/Guards/ArcherGuard.cs @@ -108,7 +108,7 @@ public partial class ArcherGuard : BaseGuard if (_focus != null) { AttackTimer = new GuardAttackTimer(this); - AttackTimer.DoOnTick(); + AttackTimer.Start(); } else { diff --git a/Projects/UOContent/Mobiles/Guards/BaseGuard.cs b/Projects/UOContent/Mobiles/Guards/BaseGuard.cs index 571b16321..6dd80edb1 100644 --- a/Projects/UOContent/Mobiles/Guards/BaseGuard.cs +++ b/Projects/UOContent/Mobiles/Guards/BaseGuard.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using ModernUO.Serialization; using Server.Items; @@ -13,33 +14,34 @@ public abstract partial class BaseGuard : Mobile GuardsInstantKill = ServerConfiguration.GetSetting("guards.instantKill", true); } - public static void Spawn(Mobile caller, Mobile target, int amount = 1, bool onlyAdditional = false) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Spawn(Mobile caller, Mobile target, int amount = 1, bool onlyAdditional = false) => + Spawn(caller.Region, target, amount, onlyAdditional); + + public static void Spawn(Region region, Mobile target, int amount = 1, bool onlyAdditional = false) { if (target?.Deleted != false) { return; } - foreach (var m in target.GetMobilesInRange(15)) + foreach (var g in target.GetMobilesInRange(15)) { - if (m is BaseGuard g) + if (g.Focus == null) // idling { - if (g.Focus == null) // idling - { - g.Focus = target; + g.Focus = target; - --amount; - } - else if (g.Focus == target && !onlyAdditional) - { - --amount; - } + --amount; + } + else if (g.Focus == target && !onlyAdditional) + { + --amount; } } while (amount-- > 0) { - caller.Region.MakeGuard(target); + region.MakeGuard(target); } } @@ -242,13 +244,6 @@ public class GuardAttackTimer : Timer return; } - if (_owner.Weapon is Fists) - { - _owner.Kill(); - Stop(); - return; - } - if (target != null && _owner.Combatant != target) { _owner.Combatant = target; diff --git a/Projects/UOContent/Mobiles/Guards/WarriorGuard.cs b/Projects/UOContent/Mobiles/Guards/WarriorGuard.cs index 3ee7bf47f..c402980b4 100644 --- a/Projects/UOContent/Mobiles/Guards/WarriorGuard.cs +++ b/Projects/UOContent/Mobiles/Guards/WarriorGuard.cs @@ -124,7 +124,7 @@ public partial class WarriorGuard : BaseGuard if (_focus != null) { AttackTimer = new GuardAttackTimer(this); - AttackTimer.DoOnTick(); + AttackTimer.Start(); } else { diff --git a/Projects/UOContent/Regions/GuardedRegion.cs b/Projects/UOContent/Regions/GuardedRegion.cs index 9b48caf2d..bfd54bd03 100644 --- a/Projects/UOContent/Regions/GuardedRegion.cs +++ b/Projects/UOContent/Regions/GuardedRegion.cs @@ -152,32 +152,15 @@ public class GuardedRegion : BaseRegion public override void MakeGuard(Mobile focus) { - BaseGuard useGuard = null; - foreach (var m in focus.GetMobilesInRange(8)) - { - if (m.Focus == null) - { - useGuard = m; - break; - } - } + m_GuardParams[0] = focus; - if (useGuard == null) + try { - m_GuardParams[0] = focus; - - try - { - GuardType.CreateInstance(m_GuardParams); - } - catch - { - // ignored - } + GuardType.CreateInstance(m_GuardParams); } - else + catch { - useGuard.Focus = focus; + // ignored } } @@ -296,7 +279,7 @@ public class GuardedRegion : BaseRegion { fakeCall.Say(Utility.Random(1013037, 16)); - MakeGuard(m); + BaseGuard.Spawn(fakeCall, m); timer.Stop(); m_GuardCandidates.Remove(m); m.SendLocalizedMessage(502276); // Guards can no longer be called on you. @@ -332,7 +315,7 @@ public class GuardedRegion : BaseRegion while (queue.Count > 0) { var m = queue.Dequeue(); - MakeGuard(m); + BaseGuard.Spawn(this, m); m.SendLocalizedMessage(502276); // Guards can no longer be called on you. } }