From 4ca72f3030d81ec7072f5fe18e7a0037d17cd57d Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Fri, 5 Jul 2024 16:20:03 -0700 Subject: [PATCH] fix: Consolidates and fixes movement/direction checks --- Projects/Server/Items/Item.cs | 27 +-- Projects/Server/Mobiles/Mobile.cs | 75 ++------ Projects/Server/Mobiles/Movement.cs | 30 ++-- .../UOContent/Engines/Pathing/Movement.cs | 58 +----- .../Items/Construction/Doors/BaseDoor.cs | 49 +----- .../Items/Weapons/Knives/ThrowingDagger.cs | 166 +++++++----------- 6 files changed, 114 insertions(+), 291 deletions(-) diff --git a/Projects/Server/Items/Item.cs b/Projects/Server/Items/Item.cs index 5c73f91a3b..1c6ab296e2 100644 --- a/Projects/Server/Items/Item.cs +++ b/Projects/Server/Items/Item.cs @@ -3840,20 +3840,23 @@ public Point3D GetSurfaceTop() { var root = RootParent; - if (root == null) - { - return new Point3D( - m_Location.m_X, - m_Location.m_Y, - m_Location.m_Z + (ItemData.Surface ? ItemData.CalcHeight : 0) - ); - } - - return root.Location; + return (root as Item)?.GetSurfaceTop() ?? root?.Location ?? new Point3D( + m_Location.m_X, + m_Location.m_Y, + m_Location.m_Z + (ItemData.Surface ? ItemData.CalcHeight : 0) + ); } - public Point3D GetWorldTop() => RootParent?.Location ?? - new Point3D(m_Location.m_X, m_Location.m_Y, m_Location.m_Z + ItemData.CalcHeight); + public Point3D GetWorldTop() + { + var root = RootParent; + + return (root as Item)?.GetWorldTop() ?? root?.Location ?? new Point3D( + m_Location.m_X, + m_Location.m_Y, + m_Location.m_Z + ItemData.CalcHeight + ); + } public void SendLocalizedMessageTo(Mobile to, int number, string args = "") { diff --git a/Projects/Server/Mobiles/Mobile.cs b/Projects/Server/Mobiles/Mobile.cs index a454136557..a40d7753f0 100644 --- a/Projects/Server/Mobiles/Mobile.cs +++ b/Projects/Server/Mobiles/Mobile.cs @@ -26,6 +26,7 @@ using Server.Logging; using Server.Menus; using Server.Mobiles; +using Server.Movement; using Server.Network; using Server.Prompts; using Server.Targeting; @@ -4089,10 +4090,12 @@ protected virtual bool OnMove(Direction d) return true; } - public virtual bool CheckMovement(Direction d, out int newZ) => Movement.Movement.CheckMovement(this, d, out newZ); + public virtual bool CheckMovement(Direction d, out int newZ) => CalcMoves.CheckMovement(this, d, out newZ); - private bool CanMove(Direction d, Point3D oldLocation, ref Point3D newLocation) + private bool CanMove(Direction d, Point3D oldLocation, out Point3D newLocation) { + newLocation = oldLocation; + if (m_Spell?.OnCasterMoving(d) == false) { return false; @@ -4110,61 +4113,13 @@ private bool CanMove(Direction d, Point3D oldLocation, ref Point3D newLocation) return false; } - int x = oldLocation.m_X, y = oldLocation.m_Y; - int oldX = x, oldY = y; + var oldX = oldLocation.m_X; + var oldY = oldLocation.m_Y; var oldZ = oldLocation.m_Z; - - switch (d & Direction.Mask) - { - case Direction.North: - { - --y; - break; - } - case Direction.Right: - { - ++x; - --y; - break; - } - case Direction.East: - { - ++x; - break; - } - case Direction.Down: - { - ++x; - ++y; - break; - } - case Direction.South: - { - ++y; - break; - } - case Direction.Left: - { - --x; - ++y; - break; - } - case Direction.West: - { - --x; - break; - } - case Direction.Up: - { - --x; - --y; - break; - } - } - - newLocation.m_X = x; - newLocation.m_Y = y; - newLocation.m_Z = newZ; + int x = 0; + int y = 0; + CalcMoves.Offset(d, ref x, ref y); + newLocation = new Point3D(x, y, newZ); Pushing = false; @@ -4344,18 +4299,22 @@ public virtual bool Move(Direction d) } var oldLocation = m_Location; - Point3D newLocation = oldLocation; + Point3D newLocation; if ((m_Direction & Direction.Mask) == (d & Direction.Mask)) { // We are actually moving (not just a direction change) - if (!CanMove(d, oldLocation, ref newLocation)) + if (!CanMove(d, oldLocation, out newLocation)) { return false; } DisruptiveAction(); } + else + { + newLocation = oldLocation; + } if (m_NetState != null) { diff --git a/Projects/Server/Mobiles/Movement.cs b/Projects/Server/Mobiles/Movement.cs index def0c50202..7da0813cab 100644 --- a/Projects/Server/Mobiles/Movement.cs +++ b/Projects/Server/Mobiles/Movement.cs @@ -13,6 +13,8 @@ * along with this program. If not, see . * *************************************************************************/ +using System.Runtime.CompilerServices; + namespace Server.Movement; public static class Movement @@ -56,52 +58,54 @@ public static bool CheckMovement(Mobile m, Map map, Point3D loc, Direction d, ou return false; } - public static void Offset(Direction d, ref int x, ref int y) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Offset(Direction d, ref int x, ref int y, int count = 1) { switch (d & Direction.Mask) { case Direction.North: { - --y; + y -= count; break; } case Direction.South: { - ++y; + y += count; break; } case Direction.West: { - --x; + x -= count; break; } case Direction.East: { - ++x; + x += count; break; } case Direction.Right: { - ++x; - --y; + x += count; + y += count; break; } case Direction.Left: { - --x; - ++y; + x -= count; + y -= count; break; } case Direction.Down: { - ++x; - ++y; + x += count; + y += count; + break; } case Direction.Up: { - --x; - --y; + x -= count; + y -= count; break; } } diff --git a/Projects/UOContent/Engines/Pathing/Movement.cs b/Projects/UOContent/Engines/Pathing/Movement.cs index b1919369e2..3f1b263f28 100644 --- a/Projects/UOContent/Engines/Pathing/Movement.cs +++ b/Projects/UOContent/Engines/Pathing/Movement.cs @@ -54,9 +54,9 @@ public bool CheckMovement(Mobile m, Map map, Point3D loc, Direction d, out int n var checkDiagonals = ((int)d & 0x1) == 0x1; - Offset(d, ref xForward, ref yForward); - Offset((Direction)(((int)d - 1) & 0x7), ref xLeft, ref yLeft); - Offset((Direction)(((int)d + 1) & 0x7), ref xRight, ref yRight); + Movement.Offset(d, ref xForward, ref yForward); + Movement.Offset((Direction)(((int)d - 1) & 0x7), ref xLeft, ref yLeft); + Movement.Offset((Direction)(((int)d + 1) & 0x7), ref xRight, ref yRight); if (xForward < 0 || yForward < 0 || xForward >= map.Width || yForward >= map.Height) { @@ -578,57 +578,5 @@ private static void GetStartZ(Mobile m, Map map, Point3D loc, List itemLis zTop = loc.Z; } } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Offset(Direction d, ref int x, ref int y) - { - switch (d & Direction.Mask) - { - case Direction.North: - { - --y; - break; - } - case Direction.South: - { - ++y; - break; - } - case Direction.West: - { - --x; - break; - } - case Direction.East: - { - ++x; - break; - } - case Direction.Right: - { - ++x; - --y; - break; - } - case Direction.Left: - { - --x; - ++y; - break; - } - case Direction.Down: - { - ++x; - ++y; - break; - } - case Direction.Up: - { - --x; - --y; - break; - } - } - } } } diff --git a/Projects/UOContent/Items/Construction/Doors/BaseDoor.cs b/Projects/UOContent/Items/Construction/Doors/BaseDoor.cs index b5608bf406..5997ab735c 100644 --- a/Projects/UOContent/Items/Construction/Doors/BaseDoor.cs +++ b/Projects/UOContent/Items/Construction/Doors/BaseDoor.cs @@ -3,6 +3,7 @@ using System.Runtime.CompilerServices; using ModernUO.Serialization; using Server.Targeting; +using CalcMoves = Server.Movement.Movement; namespace Server.Items; @@ -233,53 +234,7 @@ public static void OpenDoorMacroUsed(Mobile m) int x = m.X; int y = m.Y; - switch (m.Direction & Direction.Mask) - { - case Direction.North: - { - --y; - break; - } - case Direction.Right: - { - ++x; - --y; - break; - } - case Direction.East: - { - ++x; - break; - } - case Direction.Down: - { - ++x; - ++y; - break; - } - case Direction.South: - { - ++y; - break; - } - case Direction.Left: - { - --x; - ++y; - break; - } - case Direction.West: - { - --x; - break; - } - case Direction.Up: - { - --x; - --y; - break; - } - } + CalcMoves.Offset(m.Direction, ref x, ref y); foreach (var item in m.Map.GetItemsAt(x, y)) { diff --git a/Projects/UOContent/Items/Weapons/Knives/ThrowingDagger.cs b/Projects/UOContent/Items/Weapons/Knives/ThrowingDagger.cs index c4ccc45f08..eaac95e8c1 100644 --- a/Projects/UOContent/Items/Weapons/Knives/ThrowingDagger.cs +++ b/Projects/UOContent/Items/Weapons/Knives/ThrowingDagger.cs @@ -1,132 +1,86 @@ using System; using ModernUO.Serialization; using Server.Targeting; +using CalcMoves = Server.Movement.Movement; -namespace Server.Items +namespace Server.Items; + +[Flippable(0xF52, 0xF51)] +[SerializationGenerator(0, false)] +public partial class ThrowingDagger : Item { - [Flippable(0xF52, 0xF51)] - [SerializationGenerator(0, false)] - public partial class ThrowingDagger : Item + [Constructible] + public ThrowingDagger() : base(0xF52) + { + Weight = 1.0; + Layer = Layer.OneHanded; + } + + public override string DefaultName => "a throwing dagger"; + + public override void OnDoubleClick(Mobile from) { - [Constructible] - public ThrowingDagger() : base(0xF52) + if (from.Items.Contains(this)) + { + var t = new InternalTarget(this); + from.Target = t; + } + else { - Weight = 1.0; - Layer = Layer.OneHanded; + from.SendMessage("You must be holding that weapon to use it."); } + } + + private class InternalTarget : Target + { + private readonly ThrowingDagger m_Dagger; - public override string DefaultName => "a throwing dagger"; + public InternalTarget(ThrowingDagger dagger) : base(10, false, TargetFlags.Harmful) => m_Dagger = dagger; - public override void OnDoubleClick(Mobile from) + protected override void OnTarget(Mobile from, object targeted) { - if (from.Items.Contains(this)) + if (m_Dagger.Deleted) { - var t = new InternalTarget(this); - from.Target = t; + return; } - else + + if (!from.Items.Contains(m_Dagger)) { from.SendMessage("You must be holding that weapon to use it."); } - } + else if (targeted is Mobile m && m != from && from.HarmfulCheck(m)) + { + var to = from.GetDirectionTo(m); - private class InternalTarget : Target - { - private readonly ThrowingDagger m_Dagger; + from.Direction = to; - public InternalTarget(ThrowingDagger dagger) : base(10, false, TargetFlags.Harmful) => m_Dagger = dagger; + from.Animate(from.Mounted ? 26 : 9, 7, 1, true, false, 0); - protected override void OnTarget(Mobile from, object targeted) - { - if (m_Dagger.Deleted) + if (Utility.RandomDouble() >= Math.Sqrt(m.Dex / 100.0) * 0.8) { - return; - } + from.MovingEffect(m, 0x1BFE, 7, 1, false, false, 0x481, 0); - if (!from.Items.Contains(m_Dagger)) - { - from.SendMessage("You must be holding that weapon to use it."); + AOS.Damage(m, from, Utility.Random(5, from.Str / 10), 100, 0, 0, 0, 0); + + m_Dagger.MoveToWorld(m.Location, m.Map); } - else if (targeted is Mobile m && m != from && from.HarmfulCheck(m)) + else { - var to = from.GetDirectionTo(m); - - from.Direction = to; - - from.Animate(from.Mounted ? 26 : 9, 7, 1, true, false, 0); - - if (Utility.RandomDouble() >= Math.Sqrt(m.Dex / 100.0) * 0.8) - { - from.MovingEffect(m, 0x1BFE, 7, 1, false, false, 0x481, 0); - - AOS.Damage(m, from, Utility.Random(5, from.Str / 10), 100, 0, 0, 0, 0); - - m_Dagger.MoveToWorld(m.Location, m.Map); - } - else - { - int x = 0, y = 0; - - switch (to & Direction.Mask) - { - case Direction.North: - { - --y; - break; - } - case Direction.South: - { - ++y; - break; - } - case Direction.West: - { - --x; - break; - } - case Direction.East: - { - ++x; - break; - } - case Direction.Up: - { - --x; - --y; - break; - } - case Direction.Down: - { - ++x; - ++y; - break; - } - case Direction.Left: - { - --x; - ++y; - break; - } - case Direction.Right: - { - ++x; - --y; - break; - } - } - - x += Utility.Random(-1, 3); - y += Utility.Random(-1, 3); - - x += m.X; - y += m.Y; - - m_Dagger.MoveToWorld(new Point3D(x, y, m.Z), m.Map); - - from.MovingEffect(m_Dagger, 0x1BFE, 7, 1, false, false, 0x481, 0); - - from.SendMessage("You miss."); - } + int x = 0; + int y = 0; + CalcMoves.Offset(to, ref x, ref y); + + x += Utility.Random(-1, 3); + y += Utility.Random(-1, 3); + + x += m.X; + y += m.Y; + + m_Dagger.MoveToWorld(new Point3D(x, y, m.Z), m.Map); + + from.MovingEffect(m_Dagger, 0x1BFE, 7, 1, false, false, 0x481, 0); + + from.SendMessage("You miss."); } } }