Formats UO Content (#201)

This commit is contained in:
Kamron Batman 2020-08-27 18:30:38 -07:00 • committed by GitHub
parent 86b7b3aed1
commit ad3775c4d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3249 changed files with 431083 additions and 440981 deletions

View file

@ -1,314 +1,322 @@
using System.Collections;
using Server.Mobiles;
using CalcMoves = Server.Movement.Movement;
using MoveImpl = Server.Movement.MovementImpl;
namespace Server.PathAlgorithms.FastAStar
{
public struct PathNode
{
public int cost, total;
public int parent, next, prev;
public int z;
}
public class FastAStarAlgorithm : PathAlgorithm
{
private const int MaxDepth = 300;
private const int AreaSize = 38;
private const int NodeCount = AreaSize * AreaSize * PlaneCount;
private const int PlaneOffset = 128;
private const int PlaneCount = 13;
private const int PlaneHeight = 20;
public static PathAlgorithm Instance = new FastAStarAlgorithm();
private static readonly Direction[] m_Path = new Direction[AreaSize * AreaSize];
private static readonly PathNode[] m_Nodes = new PathNode[NodeCount];
private static readonly BitArray m_Touched = new BitArray(NodeCount);
private static readonly BitArray m_OnOpen = new BitArray(NodeCount);
private static readonly int[] m_Successors = new int[8];
private static int m_xOffset, m_yOffset;
private static int m_OpenList;
private Point3D m_Goal;
public int Heuristic(int x, int y, int z)
{
x -= m_Goal.X - m_xOffset;
y -= m_Goal.Y - m_yOffset;
z -= m_Goal.Z;
x *= 11;
y *= 11;
return x * x + y * y + z * z;
}
public override bool CheckCondition(Mobile m, Map map, Point3D start, Point3D goal) => Utility.InRange(start, goal, AreaSize);
private void RemoveFromChain(int node)
{
if (node < 0 || node >= NodeCount)
return;
if (!m_Touched[node] || !m_OnOpen[node])
return;
int prev = m_Nodes[node].prev;
int next = m_Nodes[node].next;
if (m_OpenList == node)
m_OpenList = next;
if (prev != -1)
m_Nodes[prev].next = next;
if (next != -1)
m_Nodes[next].prev = prev;
m_Nodes[node].prev = -1;
m_Nodes[node].next = -1;
}
private void AddToChain(int node)
{
if (node < 0 || node >= NodeCount)
return;
RemoveFromChain(node);
if (m_OpenList != -1)
m_Nodes[m_OpenList].prev = node;
m_Nodes[node].next = m_OpenList;
m_Nodes[node].prev = -1;
m_OpenList = node;
m_Touched[node] = true;
m_OnOpen[node] = true;
}
public override Direction[] Find(Mobile m, Map map, Point3D start, Point3D goal)
{
if (!Utility.InRange(start, goal, AreaSize))
return null;
m_Touched.SetAll(false);
m_Goal = goal;
m_xOffset = (start.X + goal.X - AreaSize) / 2;
m_yOffset = (start.Y + goal.Y - AreaSize) / 2;
int fromNode = GetIndex(start.X, start.Y, start.Z);
int destNode = GetIndex(goal.X, goal.Y, goal.Z);
m_OpenList = fromNode;
m_Nodes[m_OpenList].cost = 0;
m_Nodes[m_OpenList].total = Heuristic(start.X - m_xOffset, start.Y - m_yOffset, start.Z);
m_Nodes[m_OpenList].parent = -1;
m_Nodes[m_OpenList].next = -1;
m_Nodes[m_OpenList].prev = -1;
m_Nodes[m_OpenList].z = start.Z;
m_OnOpen[m_OpenList] = true;
m_Touched[m_OpenList] = true;
BaseCreature bc = m as BaseCreature;
int backtrack = 0, depth = 0;
Direction[] path = m_Path;
while (m_OpenList != -1)
{
int bestNode = FindBest(m_OpenList);
if (++depth > MaxDepth)
break;
if (bc != null)
{
MoveImpl.AlwaysIgnoreDoors = bc.CanOpenDoors;
MoveImpl.IgnoreMovableImpassables = bc.CanMoveOverObstacles;
}
MoveImpl.Goal = goal;
int[] vals = m_Successors;
int count = GetSuccessors(bestNode, m, map);
MoveImpl.AlwaysIgnoreDoors = false;
MoveImpl.IgnoreMovableImpassables = false;
MoveImpl.Goal = Point3D.Zero;
if (count == 0)
break;
for (int i = 0; i < count; ++i)
{
int newNode = vals[i];
bool wasTouched = m_Touched[newNode];
if (wasTouched)
continue;
int newCost = m_Nodes[bestNode].cost + 1;
int newTotal = newCost + Heuristic(newNode % AreaSize, newNode / AreaSize % AreaSize,
m_Nodes[newNode].z);
if (m_Nodes[newNode].total <= newTotal)
continue;
m_Nodes[newNode].parent = bestNode;
m_Nodes[newNode].cost = newCost;
m_Nodes[newNode].total = newTotal;
if (m_OnOpen[newNode])
continue;
AddToChain(newNode);
if (newNode != destNode)
continue;
int pathCount = 0;
int parent = m_Nodes[newNode].parent;
while (parent != -1)
{
path[pathCount++] = GetDirection(parent % AreaSize, parent / AreaSize % AreaSize,
newNode % AreaSize, newNode / AreaSize % AreaSize);
newNode = parent;
parent = m_Nodes[newNode].parent;
if (newNode == fromNode)
break;
}
Direction[] dirs = new Direction[pathCount];
while (pathCount > 0)
dirs[backtrack++] = path[--pathCount];
return dirs;
}
}
return null;
}
private int GetIndex(int x, int y, int z)
{
x -= m_xOffset;
y -= m_yOffset;
z += PlaneOffset;
z /= PlaneHeight;
return x + y * AreaSize + z * AreaSize * AreaSize;
}
private int FindBest(int node)
{
int least = m_Nodes[node].total;
int leastNode = node;
while (node != -1)
{
if (m_Nodes[node].total < least)
{
least = m_Nodes[node].total;
leastNode = node;
}
node = m_Nodes[node].next;
}
RemoveFromChain(leastNode);
m_Touched[leastNode] = true;
m_OnOpen[leastNode] = false;
return leastNode;
}
public int GetSuccessors(int p, Mobile m, Map map)
{
int px = p % AreaSize;
int py = p / AreaSize % AreaSize;
int pz = m_Nodes[p].z;
Point3D p3D = new Point3D(px + m_xOffset, py + m_yOffset, pz);
int[] vals = m_Successors;
int count = 0;
for (int i = 0; i < 8; ++i)
{
int x;
int y;
switch (i)
{
default:
case 0:
x = 0;
y = -1;
break;
case 1:
x = 1;
y = -1;
break;
case 2:
x = 1;
y = 0;
break;
case 3:
x = 1;
y = 1;
break;
case 4:
x = 0;
y = 1;
break;
case 5:
x = -1;
y = 1;
break;
case 6:
x = -1;
y = 0;
break;
case 7:
x = -1;
y = -1;
break;
}
x += px;
y += py;
if (x < 0 || x >= AreaSize || y < 0 || y >= AreaSize)
continue;
if (CalcMoves.CheckMovement(m, map, p3D, (Direction)i, out int z))
{
int idx = GetIndex(x + m_xOffset, y + m_yOffset, z);
if (idx >= 0 && idx < NodeCount)
{
m_Nodes[idx].z = z;
vals[count++] = idx;
}
}
}
return count;
}
}
}
using System.Collections;
using Server.Mobiles;
using CalcMoves = Server.Movement.Movement;
using MoveImpl = Server.Movement.MovementImpl;
namespace Server.PathAlgorithms.FastAStar
{
public struct PathNode
{
public int cost, total;
public int parent, next, prev;
public int z;
}
public class FastAStarAlgorithm : PathAlgorithm
{
private const int MaxDepth = 300;
private const int AreaSize = 38;
private const int NodeCount = AreaSize * AreaSize * PlaneCount;
private const int PlaneOffset = 128;
private const int PlaneCount = 13;
private const int PlaneHeight = 20;
public static PathAlgorithm Instance = new FastAStarAlgorithm();
private static readonly Direction[] m_Path = new Direction[AreaSize * AreaSize];
private static readonly PathNode[] m_Nodes = new PathNode[NodeCount];
private static readonly BitArray m_Touched = new BitArray(NodeCount);
private static readonly BitArray m_OnOpen = new BitArray(NodeCount);
private static readonly int[] m_Successors = new int[8];
private static int m_xOffset, m_yOffset;
private static int m_OpenList;
private Point3D m_Goal;
public int Heuristic(int x, int y, int z)
{
x -= m_Goal.X - m_xOffset;
y -= m_Goal.Y - m_yOffset;
z -= m_Goal.Z;
x *= 11;
y *= 11;
return x * x + y * y + z * z;
}
public override bool CheckCondition(Mobile m, Map map, Point3D start, Point3D goal) =>
Utility.InRange(start, goal, AreaSize);
private void RemoveFromChain(int node)
{
if (node < 0 || node >= NodeCount)
return;
if (!m_Touched[node] || !m_OnOpen[node])
return;
var prev = m_Nodes[node].prev;
var next = m_Nodes[node].next;
if (m_OpenList == node)
m_OpenList = next;
if (prev != -1)
m_Nodes[prev].next = next;
if (next != -1)
m_Nodes[next].prev = prev;
m_Nodes[node].prev = -1;
m_Nodes[node].next = -1;
}
private void AddToChain(int node)
{
if (node < 0 || node >= NodeCount)
return;
RemoveFromChain(node);
if (m_OpenList != -1)
m_Nodes[m_OpenList].prev = node;
m_Nodes[node].next = m_OpenList;
m_Nodes[node].prev = -1;
m_OpenList = node;
m_Touched[node] = true;
m_OnOpen[node] = true;
}
public override Direction[] Find(Mobile m, Map map, Point3D start, Point3D goal)
{
if (!Utility.InRange(start, goal, AreaSize))
return null;
m_Touched.SetAll(false);
m_Goal = goal;
m_xOffset = (start.X + goal.X - AreaSize) / 2;
m_yOffset = (start.Y + goal.Y - AreaSize) / 2;
var fromNode = GetIndex(start.X, start.Y, start.Z);
var destNode = GetIndex(goal.X, goal.Y, goal.Z);
m_OpenList = fromNode;
m_Nodes[m_OpenList].cost = 0;
m_Nodes[m_OpenList].total = Heuristic(start.X - m_xOffset, start.Y - m_yOffset, start.Z);
m_Nodes[m_OpenList].parent = -1;
m_Nodes[m_OpenList].next = -1;
m_Nodes[m_OpenList].prev = -1;
m_Nodes[m_OpenList].z = start.Z;
m_OnOpen[m_OpenList] = true;
m_Touched[m_OpenList] = true;
var bc = m as BaseCreature;
int backtrack = 0, depth = 0;
var path = m_Path;
while (m_OpenList != -1)
{
var bestNode = FindBest(m_OpenList);
if (++depth > MaxDepth)
break;
if (bc != null)
{
MoveImpl.AlwaysIgnoreDoors = bc.CanOpenDoors;
MoveImpl.IgnoreMovableImpassables = bc.CanMoveOverObstacles;
}
MoveImpl.Goal = goal;
var vals = m_Successors;
var count = GetSuccessors(bestNode, m, map);
MoveImpl.AlwaysIgnoreDoors = false;
MoveImpl.IgnoreMovableImpassables = false;
MoveImpl.Goal = Point3D.Zero;
if (count == 0)
break;
for (var i = 0; i < count; ++i)
{
var newNode = vals[i];
var wasTouched = m_Touched[newNode];
if (wasTouched)
continue;
var newCost = m_Nodes[bestNode].cost + 1;
var newTotal = newCost + Heuristic(
newNode % AreaSize,
newNode / AreaSize % AreaSize,
m_Nodes[newNode].z
);
if (m_Nodes[newNode].total <= newTotal)
continue;
m_Nodes[newNode].parent = bestNode;
m_Nodes[newNode].cost = newCost;
m_Nodes[newNode].total = newTotal;
if (m_OnOpen[newNode])
continue;
AddToChain(newNode);
if (newNode != destNode)
continue;
var pathCount = 0;
var parent = m_Nodes[newNode].parent;
while (parent != -1)
{
path[pathCount++] = GetDirection(
parent % AreaSize,
parent / AreaSize % AreaSize,
newNode % AreaSize,
newNode / AreaSize % AreaSize
);
newNode = parent;
parent = m_Nodes[newNode].parent;
if (newNode == fromNode)
break;
}
var dirs = new Direction[pathCount];
while (pathCount > 0)
dirs[backtrack++] = path[--pathCount];
return dirs;
}
}
return null;
}
private int GetIndex(int x, int y, int z)
{
x -= m_xOffset;
y -= m_yOffset;
z += PlaneOffset;
z /= PlaneHeight;
return x + y * AreaSize + z * AreaSize * AreaSize;
}
private int FindBest(int node)
{
var least = m_Nodes[node].total;
var leastNode = node;
while (node != -1)
{
if (m_Nodes[node].total < least)
{
least = m_Nodes[node].total;
leastNode = node;
}
node = m_Nodes[node].next;
}
RemoveFromChain(leastNode);
m_Touched[leastNode] = true;
m_OnOpen[leastNode] = false;
return leastNode;
}
public int GetSuccessors(int p, Mobile m, Map map)
{
var px = p % AreaSize;
var py = p / AreaSize % AreaSize;
var pz = m_Nodes[p].z;
var p3D = new Point3D(px + m_xOffset, py + m_yOffset, pz);
var vals = m_Successors;
var count = 0;
for (var i = 0; i < 8; ++i)
{
int x;
int y;
switch (i)
{
default:
case 0:
x = 0;
y = -1;
break;
case 1:
x = 1;
y = -1;
break;
case 2:
x = 1;
y = 0;
break;
case 3:
x = 1;
y = 1;
break;
case 4:
x = 0;
y = 1;
break;
case 5:
x = -1;
y = 1;
break;
case 6:
x = -1;
y = 0;
break;
case 7:
x = -1;
y = -1;
break;
}
x += px;
y += py;
if (x < 0 || x >= AreaSize || y < 0 || y >= AreaSize)
continue;
if (CalcMoves.CheckMovement(m, map, p3D, (Direction)i, out var z))
{
var idx = GetIndex(x + m_xOffset, y + m_yOffset, z);
if (idx >= 0 && idx < NodeCount)
{
m_Nodes[idx].z = z;
vals[count++] = idx;
}
}
}
return count;
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -1,12 +1,12 @@
namespace Server
{
public delegate MoveResult MoveMethod(Direction d);
public enum MoveResult
{
BadState,
Blocked,
Success,
SuccessAutoTurn
}
}
namespace Server
{
public delegate MoveResult MoveMethod(Direction d);
public enum MoveResult
{
BadState,
Blocked,
Success,
SuccessAutoTurn
}
}

File diff suppressed because it is too large Load diff

View file

@ -1,106 +1,106 @@
using System;
using Server.Items;
using Server.PathAlgorithms;
using Server.PathAlgorithms.FastAStar;
using Server.PathAlgorithms.SlowAStar;
using Server.Spells;
using Server.Targeting;
namespace Server
{
public sealed class MovementPath
{
public MovementPath(Mobile m, Point3D goal)
{
Point3D start = m.Location;
Map map = m.Map;
Map = map;
Start = start;
Goal = goal;
if (map == null || map == Map.Internal)
return;
if (Utility.InRange(start, goal, 1))
return;
try
{
PathAlgorithm alg = OverrideAlgorithm ?? FastAStarAlgorithm.Instance;
if (alg?.CheckCondition(m, map, start, goal) == true)
Directions = alg.Find(m, map, start, goal);
}
catch (Exception e)
{
Console.WriteLine("Warning: {0}: Pathing error from {1} to {2}", e.GetType().Name, start, goal);
}
}
public Map Map { get; }
public Point3D Start { get; }
public Point3D Goal { get; }
public Direction[] Directions { get; }
public bool Success => Directions?.Length > 0;
public static PathAlgorithm OverrideAlgorithm { get; set; }
public static void Initialize()
{
CommandSystem.Register("Path", AccessLevel.GameMaster, Path_OnCommand);
}
public static void Path_OnCommand(CommandEventArgs e)
{
e.Mobile.BeginTarget(-1, true, TargetFlags.None, Path_OnTarget);
e.Mobile.SendMessage("Target a location and a path will be drawn there.");
}
private static void Path(Mobile from, IPoint3D p, PathAlgorithm alg, string name, int zOffset)
{
OverrideAlgorithm = alg;
long start = DateTime.UtcNow.Ticks;
MovementPath path = new MovementPath(from, new Point3D(p));
long end = DateTime.UtcNow.Ticks;
double len = Math.Round((end - start) / 10000.0, 2);
if (!path.Success)
{
from.SendMessage("{0} path failed: {1}ms", name, len);
}
else
{
from.SendMessage("{0} path success: {1}ms", name, len);
int x = from.X;
int y = from.Y;
int z = from.Z;
for (int i = 0; i < path.Directions.Length; ++i)
{
Movement.Movement.Offset(path.Directions[i], ref x, ref y);
new RecallRune().MoveToWorld(new Point3D(x, y, z + zOffset), from.Map);
}
}
}
public static void Path_OnTarget(Mobile from, object targeted)
{
if (!(targeted is IPoint3D p))
return;
SpellHelper.GetSurfaceTop(ref p);
Path(from, p, FastAStarAlgorithm.Instance, "Fast", 0);
Path(from, p, SlowAStarAlgorithm.Instance, "Slow", 2);
OverrideAlgorithm = null;
}
}
}
using System;
using Server.Items;
using Server.PathAlgorithms;
using Server.PathAlgorithms.FastAStar;
using Server.PathAlgorithms.SlowAStar;
using Server.Spells;
using Server.Targeting;
namespace Server
{
public sealed class MovementPath
{
public MovementPath(Mobile m, Point3D goal)
{
var start = m.Location;
var map = m.Map;
Map = map;
Start = start;
Goal = goal;
if (map == null || map == Map.Internal)
return;
if (Utility.InRange(start, goal, 1))
return;
try
{
var alg = OverrideAlgorithm ?? FastAStarAlgorithm.Instance;
if (alg?.CheckCondition(m, map, start, goal) == true)
Directions = alg.Find(m, map, start, goal);
}
catch (Exception e)
{
Console.WriteLine("Warning: {0}: Pathing error from {1} to {2}", e.GetType().Name, start, goal);
}
}
public Map Map { get; }
public Point3D Start { get; }
public Point3D Goal { get; }
public Direction[] Directions { get; }
public bool Success => Directions?.Length > 0;
public static PathAlgorithm OverrideAlgorithm { get; set; }
public static void Initialize()
{
CommandSystem.Register("Path", AccessLevel.GameMaster, Path_OnCommand);
}
public static void Path_OnCommand(CommandEventArgs e)
{
e.Mobile.BeginTarget(-1, true, TargetFlags.None, Path_OnTarget);
e.Mobile.SendMessage("Target a location and a path will be drawn there.");
}
private static void Path(Mobile from, IPoint3D p, PathAlgorithm alg, string name, int zOffset)
{
OverrideAlgorithm = alg;
var start = DateTime.UtcNow.Ticks;
var path = new MovementPath(from, new Point3D(p));
var end = DateTime.UtcNow.Ticks;
var len = Math.Round((end - start) / 10000.0, 2);
if (!path.Success)
{
from.SendMessage("{0} path failed: {1}ms", name, len);
}
else
{
from.SendMessage("{0} path success: {1}ms", name, len);
var x = from.X;
var y = from.Y;
var z = from.Z;
for (var i = 0; i < path.Directions.Length; ++i)
{
Movement.Movement.Offset(path.Directions[i], ref x, ref y);
new RecallRune().MoveToWorld(new Point3D(x, y, z + zOffset), from.Map);
}
}
}
public static void Path_OnTarget(Mobile from, object targeted)
{
if (!(targeted is IPoint3D p))
return;
SpellHelper.GetSurfaceTop(ref p);
Path(from, p, FastAStarAlgorithm.Instance, "Fast", 0);
Path(from, p, SlowAStarAlgorithm.Instance, "Slow", 2);
OverrideAlgorithm = null;
}
}
}

View file

@ -1,32 +1,33 @@
namespace Server.PathAlgorithms
{
public abstract class PathAlgorithm
{
private static readonly Direction[] m_CalcDirections = {
Direction.Up,
Direction.North,
Direction.Right,
Direction.West,
Direction.North,
Direction.East,
Direction.Left,
Direction.South,
Direction.Down
};
public abstract bool CheckCondition(Mobile m, Map map, Point3D start, Point3D goal);
public abstract Direction[] Find(Mobile m, Map map, Point3D start, Point3D goal);
public Direction GetDirection(int xSource, int ySource, int xDest, int yDest)
{
int x = xDest + 1 - xSource;
int y = yDest + 1 - ySource;
int v = y * 3 + x;
if (v < 0 || v >= 9)
return Direction.North;
return m_CalcDirections[v];
}
}
}
namespace Server.PathAlgorithms
{
public abstract class PathAlgorithm
{
private static readonly Direction[] m_CalcDirections =
{
Direction.Up,
Direction.North,
Direction.Right,
Direction.West,
Direction.North,
Direction.East,
Direction.Left,
Direction.South,
Direction.Down
};
public abstract bool CheckCondition(Mobile m, Map map, Point3D start, Point3D goal);
public abstract Direction[] Find(Mobile m, Map map, Point3D start, Point3D goal);
public Direction GetDirection(int xSource, int ySource, int xDest, int yDest)
{
var x = xDest + 1 - xSource;
var y = yDest + 1 - ySource;
var v = y * 3 + x;
if (v < 0 || v >= 9)
return Direction.North;
return m_CalcDirections[v];
}
}
}

View file

@ -1,179 +1,180 @@
using System;
using CalcMoves = Server.Movement.Movement;
namespace Server
{
public class PathFollower
{
private static bool Enabled;
private static readonly TimeSpan RepathDelay = TimeSpan.FromSeconds(2.0);
private readonly Mobile m_From;
private int m_Index;
private DateTime m_LastPathTime;
private Point3D m_Next, m_LastGoalLoc;
private MovementPath m_Path;
public static void Initialize()
{
Enabled = ServerConfiguration.GetOrUpdateSetting("pathfinding.enable", true);
}
public PathFollower(Mobile from, IPoint3D goal)
{
m_From = from;
Goal = goal;
}
public MoveMethod Mover { get; set; }
public IPoint3D Goal { get; }
public MoveResult Move(Direction d)
{
if (Mover == null)
return m_From.Move(d) ? MoveResult.Success : MoveResult.Blocked;
return Mover(d);
}
public Point3D GetGoalLocation()
{
if (Goal is Item item)
return item.GetWorldLocation();
return new Point3D(Goal);
}
public void Advance(ref Point3D p, int index)
{
if (m_Path?.Success == true)
{
Direction[] dirs = m_Path.Directions;
if (index >= 0 && index < dirs.Length)
{
int x = p.X, y = p.Y;
CalcMoves.Offset(dirs[index], ref x, ref y);
p.X = x;
p.Y = y;
}
}
}
public void ForceRepath()
{
m_Path = null;
}
public bool CheckPath()
{
if (!Enabled)
return false;
Point3D goal = GetGoalLocation();
if (m_Path != null && ((m_Path.Success && goal == m_LastGoalLoc) || m_LastPathTime + RepathDelay > DateTime.Now) &&
!(m_Path.Success && Check(m_From.Location, m_LastGoalLoc, 0)))
return false;
m_LastPathTime = DateTime.UtcNow;
m_LastGoalLoc = goal;
m_Path = new MovementPath(m_From, goal);
m_Index = 0;
m_Next = m_From.Location;
Advance(ref m_Next, m_Index);
return true;
}
public bool Check(Point3D loc, Point3D goal, int range) => Utility.InRange(loc, goal, range) && (range > 1 || Math.Abs(loc.Z - goal.Z) < 16);
public bool Follow(bool run, int range)
{
Point3D goal = GetGoalLocation();
Direction d;
if (Check(m_From.Location, goal, range))
return true;
bool repathed = CheckPath();
if (!(Enabled && m_Path.Success))
{
d = m_From.GetDirectionTo(goal);
if (run)
d |= Direction.Running;
m_From.SetDirection(d);
Move(d);
return Check(m_From.Location, goal, range);
}
d = m_From.GetDirectionTo(m_Next);
if (run)
d |= Direction.Running;
m_From.SetDirection(d);
MoveResult res = Move(d);
if (res == MoveResult.Blocked)
{
if (repathed)
return false;
m_Path = null;
CheckPath();
if (!m_Path.Success)
{
d = m_From.GetDirectionTo(goal);
if (run)
d |= Direction.Running;
m_From.SetDirection(d);
Move(d);
return Check(m_From.Location, goal, range);
}
d = m_From.GetDirectionTo(m_Next);
if (run)
d |= Direction.Running;
m_From.SetDirection(d);
res = Move(d);
if (res == MoveResult.Blocked)
return false;
}
if (m_From.X == m_Next.X && m_From.Y == m_Next.Y)
{
if (m_From.Z == m_Next.Z)
{
++m_Index;
Advance(ref m_Next, m_Index);
}
else
{
m_Path = null;
}
}
return Check(m_From.Location, goal, range);
}
}
}
using System;
using CalcMoves = Server.Movement.Movement;
namespace Server
{
public class PathFollower
{
private static bool Enabled;
private static readonly TimeSpan RepathDelay = TimeSpan.FromSeconds(2.0);
private readonly Mobile m_From;
private int m_Index;
private DateTime m_LastPathTime;
private Point3D m_Next, m_LastGoalLoc;
private MovementPath m_Path;
public PathFollower(Mobile from, IPoint3D goal)
{
m_From = from;
Goal = goal;
}
public MoveMethod Mover { get; set; }
public IPoint3D Goal { get; }
public static void Initialize()
{
Enabled = ServerConfiguration.GetOrUpdateSetting("pathfinding.enable", true);
}
public MoveResult Move(Direction d)
{
if (Mover == null)
return m_From.Move(d) ? MoveResult.Success : MoveResult.Blocked;
return Mover(d);
}
public Point3D GetGoalLocation()
{
if (Goal is Item item)
return item.GetWorldLocation();
return new Point3D(Goal);
}
public void Advance(ref Point3D p, int index)
{
if (m_Path?.Success == true)
{
var dirs = m_Path.Directions;
if (index >= 0 && index < dirs.Length)
{
int x = p.X, y = p.Y;
CalcMoves.Offset(dirs[index], ref x, ref y);
p.X = x;
p.Y = y;
}
}
}
public void ForceRepath()
{
m_Path = null;
}
public bool CheckPath()
{
if (!Enabled)
return false;
var goal = GetGoalLocation();
if (m_Path != null && (m_Path.Success && goal == m_LastGoalLoc || m_LastPathTime + RepathDelay > DateTime.Now) &&
!(m_Path.Success && Check(m_From.Location, m_LastGoalLoc, 0)))
return false;
m_LastPathTime = DateTime.UtcNow;
m_LastGoalLoc = goal;
m_Path = new MovementPath(m_From, goal);
m_Index = 0;
m_Next = m_From.Location;
Advance(ref m_Next, m_Index);
return true;
}
public bool Check(Point3D loc, Point3D goal, int range) =>
Utility.InRange(loc, goal, range) && (range > 1 || Math.Abs(loc.Z - goal.Z) < 16);
public bool Follow(bool run, int range)
{
var goal = GetGoalLocation();
Direction d;
if (Check(m_From.Location, goal, range))
return true;
var repathed = CheckPath();
if (!(Enabled && m_Path.Success))
{
d = m_From.GetDirectionTo(goal);
if (run)
d |= Direction.Running;
m_From.SetDirection(d);
Move(d);
return Check(m_From.Location, goal, range);
}
d = m_From.GetDirectionTo(m_Next);
if (run)
d |= Direction.Running;
m_From.SetDirection(d);
var res = Move(d);
if (res == MoveResult.Blocked)
{
if (repathed)
return false;
m_Path = null;
CheckPath();
if (!m_Path.Success)
{
d = m_From.GetDirectionTo(goal);
if (run)
d |= Direction.Running;
m_From.SetDirection(d);
Move(d);
return Check(m_From.Location, goal, range);
}
d = m_From.GetDirectionTo(m_Next);
if (run)
d |= Direction.Running;
m_From.SetDirection(d);
res = Move(d);
if (res == MoveResult.Blocked)
return false;
}
if (m_From.X == m_Next.X && m_From.Y == m_Next.Y)
{
if (m_From.Z == m_Next.Z)
{
++m_Index;
Advance(ref m_Next, m_Index);
}
else
{
m_Path = null;
}
}
return Check(m_From.Location, goal, range);
}
}
}

View file

@ -1,274 +1,274 @@
using System;
using Server.Mobiles;
using CalcMoves = Server.Movement.Movement;
using MoveImpl = Server.Movement.MovementImpl;
namespace Server.PathAlgorithms.SlowAStar
{
public struct PathNode
{
public int x, y, z;
public int g, h;
public int px, py, pz;
public int dir;
}
public class SlowAStarAlgorithm : PathAlgorithm
{
private const int MaxDepth = 300;
private const int MaxNodes = MaxDepth * 16;
public static PathAlgorithm Instance = new SlowAStarAlgorithm();
private static readonly PathNode[] m_Closed = new PathNode[MaxNodes];
private static readonly PathNode[] m_Open = new PathNode[MaxNodes];
private static readonly PathNode[] m_Successors = new PathNode[8];
private static readonly Direction[] m_Path = new Direction[MaxNodes];
private Point3D m_Goal;
public int Heuristic(int x, int y, int z)
{
x -= m_Goal.X;
y -= m_Goal.Y;
z -= m_Goal.Z;
x *= 11;
y *= 11;
return x * x + y * y + z * z;
}
public override bool CheckCondition(Mobile m, Map map, Point3D start, Point3D goal) => false;
public override Direction[] Find(Mobile m, Map map, Point3D start, Point3D goal)
{
m_Goal = goal;
BaseCreature bc = m as BaseCreature;
PathNode curNode;
PathNode goalNode = new PathNode();
goalNode.x = goal.X;
goalNode.y = goal.Y;
goalNode.z = goal.Z;
PathNode startNode = new PathNode();
startNode.x = start.X;
startNode.y = start.Y;
startNode.z = start.Z;
startNode.h = Heuristic(startNode.x, startNode.y, startNode.z);
PathNode[] closed = m_Closed, open = m_Open, successors = m_Successors;
Direction[] path = m_Path;
int closedCount = 0, openCount = 0;
int pathCount = 0;
int depth = 0;
int iBacktrack = 0;
open[openCount++] = startNode;
while (openCount > 0)
{
curNode = open[0];
int curF = curNode.g + curNode.h;
int popIndex = 0;
for (int i = 1; i < openCount; ++i)
if (open[i].g + open[i].h < curF)
{
curNode = open[i];
curF = curNode.g + curNode.h;
popIndex = i;
}
if (curNode.x == goalNode.x && curNode.y == goalNode.y && Math.Abs(curNode.z - goalNode.z) < 16)
{
if (closedCount == MaxNodes)
break;
closed[closedCount++] = curNode;
int xBacktrack = curNode.px;
int yBacktrack = curNode.py;
int zBacktrack = curNode.pz;
if (pathCount == MaxNodes)
break;
path[pathCount++] = (Direction)curNode.dir;
while (xBacktrack != startNode.x || yBacktrack != startNode.y || zBacktrack != startNode.z)
{
bool found = false;
for (int j = 0; !found && j < closedCount; ++j)
if (closed[j].x == xBacktrack && closed[j].y == yBacktrack && closed[j].z == zBacktrack)
{
if (pathCount == MaxNodes)
break;
curNode = closed[j];
path[pathCount++] = (Direction)curNode.dir;
xBacktrack = curNode.px;
yBacktrack = curNode.py;
zBacktrack = curNode.pz;
found = true;
}
if (!found)
{
Console.WriteLine("bugaboo..");
return null;
}
if (pathCount == MaxNodes)
break;
}
if (pathCount == MaxNodes)
break;
Direction[] dirs = new Direction[pathCount];
while (pathCount > 0)
dirs[iBacktrack++] = path[--pathCount];
return dirs;
}
--openCount;
for (int i = popIndex; i < openCount; ++i)
open[i] = open[i + 1];
int sucCount = 0;
if (bc != null)
{
MoveImpl.AlwaysIgnoreDoors = bc.CanOpenDoors;
MoveImpl.IgnoreMovableImpassables = bc.CanMoveOverObstacles;
}
MoveImpl.Goal = goal;
int x;
int y;
int z;
for (int i = 0; i < 8; ++i)
{
switch (i)
{
default:
x = 0;
y = -1;
break;
case 1:
x = 1;
y = -1;
break;
case 2:
x = 1;
y = 0;
break;
case 3:
x = 1;
y = 1;
break;
case 4:
x = 0;
y = 1;
break;
case 5:
x = -1;
y = 1;
break;
case 6:
x = -1;
y = 0;
break;
case 7:
x = -1;
y = -1;
break;
}
if (CalcMoves.CheckMovement(m, map, new Point3D(curNode.x, curNode.y, curNode.z), (Direction)i, out z))
{
successors[sucCount].x = x + curNode.x;
successors[sucCount].y = y + curNode.y;
successors[sucCount++].z = z;
}
}
MoveImpl.AlwaysIgnoreDoors = false;
MoveImpl.IgnoreMovableImpassables = false;
MoveImpl.Goal = Point3D.Zero;
if (sucCount == 0 || ++depth > MaxDepth)
break;
for (int i = 0; i < sucCount; ++i)
{
x = successors[i].x;
y = successors[i].y;
z = successors[i].z;
successors[i].g = curNode.g + 1;
int openIndex = -1, closedIndex = -1;
for (int j = 0; openIndex == -1 && j < openCount; ++j)
if (open[j].x == x && open[j].y == y && open[j].z == z)
openIndex = j;
if (openIndex >= 0 && open[openIndex].g < successors[i].g)
continue;
for (int j = 0; closedIndex == -1 && j < closedCount; ++j)
if (closed[j].x == x && closed[j].y == y && closed[j].z == z)
closedIndex = j;
if (closedIndex >= 0 && closed[closedIndex].g < successors[i].g)
continue;
if (openIndex >= 0)
{
--openCount;
for (int j = openIndex; j < openCount; ++j)
open[j] = open[j + 1];
}
if (closedIndex >= 0)
{
--closedCount;
for (int j = closedIndex; j < closedCount; ++j)
closed[j] = closed[j + 1];
}
successors[i].px = curNode.x;
successors[i].py = curNode.y;
successors[i].pz = curNode.z;
successors[i].dir = (int)GetDirection(curNode.x, curNode.y, x, y);
successors[i].h = Heuristic(x, y, z);
if (openCount == MaxNodes)
break;
open[openCount++] = successors[i];
}
if (openCount == MaxNodes || closedCount == MaxNodes)
break;
closed[closedCount++] = curNode;
}
return null;
}
}
}
using System;
using Server.Mobiles;
using CalcMoves = Server.Movement.Movement;
using MoveImpl = Server.Movement.MovementImpl;
namespace Server.PathAlgorithms.SlowAStar
{
public struct PathNode
{
public int x, y, z;
public int g, h;
public int px, py, pz;
public int dir;
}
public class SlowAStarAlgorithm : PathAlgorithm
{
private const int MaxDepth = 300;
private const int MaxNodes = MaxDepth * 16;
public static PathAlgorithm Instance = new SlowAStarAlgorithm();
private static readonly PathNode[] m_Closed = new PathNode[MaxNodes];
private static readonly PathNode[] m_Open = new PathNode[MaxNodes];
private static readonly PathNode[] m_Successors = new PathNode[8];
private static readonly Direction[] m_Path = new Direction[MaxNodes];
private Point3D m_Goal;
public int Heuristic(int x, int y, int z)
{
x -= m_Goal.X;
y -= m_Goal.Y;
z -= m_Goal.Z;
x *= 11;
y *= 11;
return x * x + y * y + z * z;
}
public override bool CheckCondition(Mobile m, Map map, Point3D start, Point3D goal) => false;
public override Direction[] Find(Mobile m, Map map, Point3D start, Point3D goal)
{
m_Goal = goal;
var bc = m as BaseCreature;
PathNode curNode;
var goalNode = new PathNode();
goalNode.x = goal.X;
goalNode.y = goal.Y;
goalNode.z = goal.Z;
var startNode = new PathNode();
startNode.x = start.X;
startNode.y = start.Y;
startNode.z = start.Z;
startNode.h = Heuristic(startNode.x, startNode.y, startNode.z);
PathNode[] closed = m_Closed, open = m_Open, successors = m_Successors;
var path = m_Path;
int closedCount = 0, openCount = 0;
var pathCount = 0;
var depth = 0;
var iBacktrack = 0;
open[openCount++] = startNode;
while (openCount > 0)
{
curNode = open[0];
var curF = curNode.g + curNode.h;
var popIndex = 0;
for (var i = 1; i < openCount; ++i)
if (open[i].g + open[i].h < curF)
{
curNode = open[i];
curF = curNode.g + curNode.h;
popIndex = i;
}
if (curNode.x == goalNode.x && curNode.y == goalNode.y && Math.Abs(curNode.z - goalNode.z) < 16)
{
if (closedCount == MaxNodes)
break;
closed[closedCount++] = curNode;
var xBacktrack = curNode.px;
var yBacktrack = curNode.py;
var zBacktrack = curNode.pz;
if (pathCount == MaxNodes)
break;
path[pathCount++] = (Direction)curNode.dir;
while (xBacktrack != startNode.x || yBacktrack != startNode.y || zBacktrack != startNode.z)
{
var found = false;
for (var j = 0; !found && j < closedCount; ++j)
if (closed[j].x == xBacktrack && closed[j].y == yBacktrack && closed[j].z == zBacktrack)
{
if (pathCount == MaxNodes)
break;
curNode = closed[j];
path[pathCount++] = (Direction)curNode.dir;
xBacktrack = curNode.px;
yBacktrack = curNode.py;
zBacktrack = curNode.pz;
found = true;
}
if (!found)
{
Console.WriteLine("bugaboo..");
return null;
}
if (pathCount == MaxNodes)
break;
}
if (pathCount == MaxNodes)
break;
var dirs = new Direction[pathCount];
while (pathCount > 0)
dirs[iBacktrack++] = path[--pathCount];
return dirs;
}
--openCount;
for (var i = popIndex; i < openCount; ++i)
open[i] = open[i + 1];
var sucCount = 0;
if (bc != null)
{
MoveImpl.AlwaysIgnoreDoors = bc.CanOpenDoors;
MoveImpl.IgnoreMovableImpassables = bc.CanMoveOverObstacles;
}
MoveImpl.Goal = goal;
int x;
int y;
int z;
for (var i = 0; i < 8; ++i)
{
switch (i)
{
default:
x = 0;
y = -1;
break;
case 1:
x = 1;
y = -1;
break;
case 2:
x = 1;
y = 0;
break;
case 3:
x = 1;
y = 1;
break;
case 4:
x = 0;
y = 1;
break;
case 5:
x = -1;
y = 1;
break;
case 6:
x = -1;
y = 0;
break;
case 7:
x = -1;
y = -1;
break;
}
if (CalcMoves.CheckMovement(m, map, new Point3D(curNode.x, curNode.y, curNode.z), (Direction)i, out z))
{
successors[sucCount].x = x + curNode.x;
successors[sucCount].y = y + curNode.y;
successors[sucCount++].z = z;
}
}
MoveImpl.AlwaysIgnoreDoors = false;
MoveImpl.IgnoreMovableImpassables = false;
MoveImpl.Goal = Point3D.Zero;
if (sucCount == 0 || ++depth > MaxDepth)
break;
for (var i = 0; i < sucCount; ++i)
{
x = successors[i].x;
y = successors[i].y;
z = successors[i].z;
successors[i].g = curNode.g + 1;
int openIndex = -1, closedIndex = -1;
for (var j = 0; openIndex == -1 && j < openCount; ++j)
if (open[j].x == x && open[j].y == y && open[j].z == z)
openIndex = j;
if (openIndex >= 0 && open[openIndex].g < successors[i].g)
continue;
for (var j = 0; closedIndex == -1 && j < closedCount; ++j)
if (closed[j].x == x && closed[j].y == y && closed[j].z == z)
closedIndex = j;
if (closedIndex >= 0 && closed[closedIndex].g < successors[i].g)
continue;
if (openIndex >= 0)
{
--openCount;
for (var j = openIndex; j < openCount; ++j)
open[j] = open[j + 1];
}
if (closedIndex >= 0)
{
--closedCount;
for (var j = closedIndex; j < closedCount; ++j)
closed[j] = closed[j + 1];
}
successors[i].px = curNode.x;
successors[i].py = curNode.y;
successors[i].pz = curNode.z;
successors[i].dir = (int)GetDirection(curNode.x, curNode.y, x, y);
successors[i].h = Heuristic(x, y, z);
if (openCount == MaxNodes)
break;
open[openCount++] = successors[i];
}
if (openCount == MaxNodes || closedCount == MaxNodes)
break;
closed[closedCount++] = curNode;
}
return null;
}
}
}