Skip to content

Commit

Permalink
fix: Consolidates and fixes movement/direction checks
Browse files Browse the repository at this point in the history
  • Loading branch information
kamronbatman committed Jul 5, 2024
1 parent c914d3d commit 4ca72f3
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 291 deletions.
27 changes: 15 additions & 12 deletions Projects/Server/Items/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "")
{
Expand Down
75 changes: 17 additions & 58 deletions Projects/Server/Mobiles/Mobile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using Server.Logging;
using Server.Menus;
using Server.Mobiles;
using Server.Movement;

Check warning on line 29 in Projects/Server/Mobiles/Mobile.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Redundant using directive

Using directive is not required by the code and can be safely removed
using Server.Network;
using Server.Prompts;
using Server.Targeting;
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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)
{
Expand Down
30 changes: 17 additions & 13 deletions Projects/Server/Mobiles/Movement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/

using System.Runtime.CompilerServices;

namespace Server.Movement;

public static class Movement
Expand Down Expand Up @@ -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;
}
}
Expand Down
58 changes: 3 additions & 55 deletions Projects/UOContent/Engines/Pathing/Movement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -578,57 +578,5 @@ private static void GetStartZ(Mobile m, Map map, Point3D loc, List<Item> 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;
}
}
}
}
}
49 changes: 2 additions & 47 deletions Projects/UOContent/Items/Construction/Doors/BaseDoor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Runtime.CompilerServices;
using ModernUO.Serialization;
using Server.Targeting;
using CalcMoves = Server.Movement.Movement;

namespace Server.Items;

Expand Down Expand Up @@ -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))
{
Expand Down
Loading

0 comments on commit 4ca72f3

Please sign in to comment.