Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Consolidates and fixes movement/direction checks #1858

Merged
merged 4 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions Projects/Server/Items/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3836,24 +3836,27 @@ public Point3D GetWorldLocation()
// return root == null ? m_Location : new Point3D( (IPoint3D) root );
}

public Point3D GetSurfaceTop()
public IPoint3D 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() ?? (ItemData.Surface ? new Point3D(
m_Location.m_X,
m_Location.m_Y,
m_Location.m_Z + ItemData.CalcHeight
) : this);
}

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() ?? 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
74 changes: 16 additions & 58 deletions Projects/Server/Mobiles/Mobile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4089,10 +4089,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 +4112,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 +4298,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
Loading