Overwrite

Complete Overwrite of the Folder with the free shard. ServUO 57.3 has been added.
This commit is contained in:
Unstable Kitsune
2023-11-28 23:20:26 -05:00
parent 3cd54811de
commit b918192e4e
11608 changed files with 2644205 additions and 47 deletions

View File

@@ -0,0 +1,310 @@
using System;
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
{
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 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;
private Point3D m_Goal;
public int Heuristic(int x, int y, int z)
{
x -= this.m_Goal.X - m_xOffset;
y -= this.m_Goal.Y - m_yOffset;
z -= this.m_Goal.Z;
x *= 11;
y *= 11;
return (x * x) + (y * y) + (z * z);
}
public override bool CheckCondition(IPoint3D p, Map map, Point3D start, Point3D goal)
{
return Utility.InRange(start, goal, AreaSize);
}
public override Direction[] Find(IPoint3D p, Map map, Point3D start, Point3D goal)
{
if (!Utility.InRange(start, goal, AreaSize))
return null;
m_Touched.SetAll(false);
this.m_Goal = goal;
m_xOffset = (start.X + goal.X - AreaSize) / 2;
m_yOffset = (start.Y + goal.Y - AreaSize) / 2;
int fromNode = this.GetIndex(start.X, start.Y, start.Z);
int destNode = this.GetIndex(goal.X, goal.Y, goal.Z);
m_OpenList = fromNode;
m_Nodes[m_OpenList].cost = 0;
m_Nodes[m_OpenList].total = this.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 = p as BaseCreature;
int pathCount, parent;
int backtrack = 0, depth = 0;
Direction[] path = m_Path;
while (m_OpenList != -1)
{
int bestNode = this.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 = this.GetSuccessors(bestNode, p, 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)
{
int newCost = m_Nodes[bestNode].cost + 1;
int newTotal = newCost + this.Heuristic(newNode % AreaSize, (newNode / AreaSize) % AreaSize, m_Nodes[newNode].z);
if (!wasTouched || m_Nodes[newNode].total > newTotal)
{
m_Nodes[newNode].parent = bestNode;
m_Nodes[newNode].cost = newCost;
m_Nodes[newNode].total = newTotal;
if (!wasTouched || !m_OnOpen[newNode])
{
this.AddToChain(newNode);
if (newNode == destNode)
{
pathCount = 0;
parent = m_Nodes[newNode].parent;
while (parent != -1)
{
path[pathCount++] = this.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;
}
public int GetSuccessors(int p, IPoint3D pnt, Map map)
{
int px = p % AreaSize;
int py = (p / AreaSize) % AreaSize;
int pz = m_Nodes[p].z;
int x, y, 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)
{
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(pnt, map, p3D, (Direction)i, out z))
{
int idx = this.GetIndex(x + m_xOffset, y + m_yOffset, z);
if (idx >= 0 && idx < NodeCount)
{
m_Nodes[idx].z = z;
vals[count++] = idx;
}
}
}
return count;
}
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;
this.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;
}
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;
}
this.RemoveFromChain(leastNode);
m_Touched[leastNode] = true;
m_OnOpen[leastNode] = false;
return leastNode;
}
}
}

View File

@@ -0,0 +1,731 @@
#region References
using System;
using System.Collections.Generic;
using System.Linq;
using Server.Items;
using Server.Mobiles;
#endregion
namespace Server.Movement
{
public class FastMovementImpl : IMovementImpl
{
public static bool Enabled = true;
private const int PersonHeight = 16;
private const int StepHeight = 2;
private const TileFlag ImpassableSurface = TileFlag.Impassable | TileFlag.Surface;
private static IMovementImpl _Successor;
public static void Initialize()
{
_Successor = Movement.Impl;
Movement.Impl = new FastMovementImpl();
}
private FastMovementImpl()
{ }
private static bool IsOk(StaticTile tile, int ourZ, int ourTop)
{
var itemData = TileData.ItemTable[tile.ID & TileData.MaxItemValue];
return tile.Z + itemData.CalcHeight <= ourZ || ourTop <= tile.Z || (itemData.Flags & ImpassableSurface) == 0;
}
private static bool IsOk(Mobile m, Item item, int ourZ, int ourTop, bool ignoreDoors, bool ignoreSpellFields)
{
var itemID = item.ItemID & TileData.MaxItemValue;
var itemData = TileData.ItemTable[itemID];
if ((itemData.Flags & ImpassableSurface) == 0)
{
return true;
}
if (((itemData.Flags & TileFlag.Door) != 0 || itemID == 0x692 || itemID == 0x846 || itemID == 0x873 ||
(itemID >= 0x6F5 && itemID <= 0x6F6)) && ignoreDoors)
{
return !(item is BaseHouseDoor) || m == null || ((BaseHouseDoor)item).CheckAccess(m);
}
if ((itemID == 0x82 || itemID == 0x3946 || itemID == 0x3956) && ignoreSpellFields)
{
return true;
}
// hidden containers, per EA
if ((itemData.Flags & TileFlag.Container) != 0 && !item.Visible)
{
return true;
}
return item.Z + itemData.CalcHeight <= ourZ || ourTop <= item.Z;
}
private static bool IsOk(
Mobile m,
bool ignoreDoors,
bool ignoreSpellFields,
int ourZ,
int ourTop,
IEnumerable<StaticTile> tiles,
IEnumerable<Item> items)
{
return tiles.All(t => IsOk(t, ourZ, ourTop)) && items.All(i => IsOk(m, i, ourZ, ourTop, ignoreDoors, ignoreSpellFields));
}
private static bool Check(
Map map,
IPoint3D p,
List<Item> items,
int x,
int y,
int startTop,
int startZ,
bool canSwim,
bool cantWalk,
out int newZ)
{
newZ = 0;
var tiles = map.Tiles.GetStaticTiles(x, y, true);
var landTile = map.Tiles.GetLandTile(x, y);
var landData = TileData.LandTable[landTile.ID & TileData.MaxLandValue];
var landBlocks = (landData.Flags & TileFlag.Impassable) != 0;
var considerLand = !landTile.Ignored;
if (landBlocks && canSwim && (landData.Flags & TileFlag.Wet) != 0)
{
//Impassable, Can Swim, and Is water. Don't block it.
landBlocks = false;
}
else if (cantWalk && (landData.Flags & TileFlag.Wet) == 0)
{
//Can't walk and it's not water
landBlocks = true;
}
int landZ = 0, landCenter = 0, landTop = 0;
map.GetAverageZ(x, y, ref landZ, ref landCenter, ref landTop);
var moveIsOk = false;
var stepTop = startTop + StepHeight;
var checkTop = startZ + PersonHeight;
Mobile m = p as Mobile;
var ignoreDoors = MovementImpl.AlwaysIgnoreDoors || m == null || !m.Alive || m.IsDeadBondedPet || m.Body.IsGhost ||
m.Body.BodyID == 987;
var ignoreSpellFields = m is PlayerMobile && map.MapID != 0;
int itemZ, itemTop, ourZ, ourTop, testTop;
ItemData itemData;
TileFlag flags;
#region Tiles
foreach (var tile in tiles)
{
itemData = TileData.ItemTable[tile.ID & TileData.MaxItemValue];
flags = itemData.Flags;
#region SA
if (m != null && m.Flying && (Insensitive.Equals(itemData.Name, "hover over") || (flags & TileFlag.HoverOver) != 0))
{
newZ = tile.Z;
return true;
}
// Stygian Dragon
if (m != null && m.Body == 826 && map != null && map.MapID == 5)
{
if (x >= 307 && x <= 354 && y >= 126 && y <= 192)
{
if (tile.Z > newZ)
{
newZ = tile.Z;
}
moveIsOk = true;
}
else if (x >= 42 && x <= 89)
{
if ((y >= 333 && y <= 399) || (y >= 531 && y <= 597) || (y >= 739 && y <= 805))
{
if (tile.Z > newZ)
{
newZ = tile.Z;
}
moveIsOk = true;
}
}
}
#endregion
if ((flags & ImpassableSurface) != TileFlag.Surface && (!canSwim || (flags & TileFlag.Wet) == 0))
{
continue;
}
if (cantWalk && (flags & TileFlag.Wet) == 0)
{
continue;
}
itemZ = tile.Z;
itemTop = itemZ;
ourZ = itemZ + itemData.CalcHeight;
ourTop = ourZ + PersonHeight;
testTop = checkTop;
if (moveIsOk)
{
var cmp = Math.Abs(ourZ - p.Z) - Math.Abs(newZ - p.Z);
if (cmp > 0 || (cmp == 0 && ourZ > newZ))
{
continue;
}
}
if (ourTop > testTop)
{
testTop = ourTop;
}
if (!itemData.Bridge)
{
itemTop += itemData.Height;
}
if (stepTop < itemTop)
{
continue;
}
var landCheck = itemZ;
if (itemData.Height >= StepHeight)
{
landCheck += StepHeight;
}
else
{
landCheck += itemData.Height;
}
if (considerLand && landCheck < landCenter && landCenter > ourZ && testTop > landZ)
{
continue;
}
if (!IsOk(m, ignoreDoors, ignoreSpellFields, ourZ, testTop, tiles, items))
{
continue;
}
newZ = ourZ;
moveIsOk = true;
}
#endregion
#region Items
foreach (var item in items)
{
itemData = item.ItemData;
flags = itemData.Flags;
#region SA
if (m != null && m.Flying && (Insensitive.Equals(itemData.Name, "hover over") || (flags & TileFlag.HoverOver) != 0))
{
newZ = item.Z;
return true;
}
#endregion
if (item.Movable)
{
continue;
}
if ((flags & ImpassableSurface) != TileFlag.Surface && ((m != null && !m.CanSwim) || (flags & TileFlag.Wet) == 0))
{
continue;
}
if (cantWalk && (flags & TileFlag.Wet) == 0)
{
continue;
}
itemZ = item.Z;
itemTop = itemZ;
ourZ = itemZ + itemData.CalcHeight;
ourTop = ourZ + PersonHeight;
testTop = checkTop;
if (moveIsOk)
{
var cmp = Math.Abs(ourZ - p.Z) - Math.Abs(newZ - p.Z);
if (cmp > 0 || (cmp == 0 && ourZ > newZ))
{
continue;
}
}
if (ourTop > testTop)
{
testTop = ourTop;
}
if (!itemData.Bridge)
{
itemTop += itemData.Height;
}
if (stepTop < itemTop)
{
continue;
}
var landCheck = itemZ;
if (itemData.Height >= StepHeight)
{
landCheck += StepHeight;
}
else
{
landCheck += itemData.Height;
}
if (considerLand && landCheck < landCenter && landCenter > ourZ && testTop > landZ)
{
continue;
}
if (!IsOk(m, ignoreDoors, ignoreSpellFields, ourZ, testTop, tiles, items))
{
continue;
}
newZ = ourZ;
moveIsOk = true;
}
#endregion
if (!considerLand || landBlocks || stepTop < landZ)
{
return moveIsOk;
}
ourZ = landCenter;
ourTop = ourZ + PersonHeight;
testTop = checkTop;
if (ourTop > testTop)
{
testTop = ourTop;
}
var shouldCheck = true;
if (moveIsOk)
{
var cmp = Math.Abs(ourZ - p.Z) - Math.Abs(newZ - p.Z);
if (cmp > 0 || (cmp == 0 && ourZ > newZ))
{
shouldCheck = false;
}
}
if (!shouldCheck || !IsOk(m, ignoreDoors, ignoreSpellFields, ourZ, testTop, tiles, items))
{
return moveIsOk;
}
newZ = ourZ;
moveIsOk = true;
return moveIsOk;
}
public bool CheckMovement(IPoint3D p, Map map, Point3D loc, Direction d, out int newZ)
{
if (!Enabled && _Successor != null)
{
return _Successor.CheckMovement(p, map, loc, d, out newZ);
}
if (map == null || map == Map.Internal)
{
newZ = 0;
return false;
}
var xStart = loc.X;
var yStart = loc.Y;
int xForward = xStart, yForward = yStart;
int xRight = xStart, yRight = yStart;
int xLeft = xStart, yLeft = yStart;
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);
if (xForward < 0 || yForward < 0 || xForward >= map.Width || yForward >= map.Height)
{
newZ = 0;
return false;
}
int startZ, startTop;
IEnumerable<Item> itemsStart, itemsForward, itemsLeft, itemsRight;
var ignoreMovableImpassables = MovementImpl.IgnoreMovableImpassables;
var reqFlags = ImpassableSurface;
if (p is Mobile && ((Mobile)p).CanSwim)
{
reqFlags |= TileFlag.Wet;
}
if (checkDiagonals)
{
var sStart = map.GetSector(xStart, yStart);
var sForward = map.GetSector(xForward, yForward);
var sLeft = map.GetSector(xLeft, yLeft);
var sRight = map.GetSector(xRight, yRight);
itemsStart = sStart.Items.Where(i => Verify(i, reqFlags, ignoreMovableImpassables, xStart, yStart));
itemsForward = sForward.Items.Where(i => Verify(i, reqFlags, ignoreMovableImpassables, xForward, yForward));
itemsLeft = sLeft.Items.Where(i => Verify(i, reqFlags, ignoreMovableImpassables, xLeft, yLeft));
itemsRight = sRight.Items.Where(i => Verify(i, reqFlags, ignoreMovableImpassables, xRight, yRight));
}
else
{
var sStart = map.GetSector(xStart, yStart);
var sForward = map.GetSector(xForward, yForward);
itemsStart = sStart.Items.Where(i => Verify(i, reqFlags, ignoreMovableImpassables, xStart, yStart));
itemsForward = sForward.Items.Where(i => Verify(i, reqFlags, ignoreMovableImpassables, xForward, yForward));
itemsLeft = Enumerable.Empty<Item>();
itemsRight = Enumerable.Empty<Item>();
}
GetStartZ(p, map, loc, itemsStart, out startZ, out startTop);
List<Item> list = null;
MovementPool.AcquireMoveCache(ref list, itemsForward);
Mobile m = p as Mobile;
var moveIsOk = Check(map, p, list, xForward, yForward, startTop, startZ, m != null && m.CanSwim, m != null && m.CantWalk, out newZ);
if (m != null && moveIsOk && checkDiagonals)
{
int hold;
if (m.Player && m.AccessLevel < AccessLevel.GameMaster)
{
MovementPool.AcquireMoveCache(ref list, itemsLeft);
if (!Check(map, m, list, xLeft, yLeft, startTop, startZ, m.CanSwim, m.CantWalk, out hold))
{
moveIsOk = false;
}
else
{
MovementPool.AcquireMoveCache(ref list, itemsRight);
if (!Check(map, m, list, xRight, yRight, startTop, startZ, m.CanSwim, m.CantWalk, out hold))
{
moveIsOk = false;
}
}
}
else
{
MovementPool.AcquireMoveCache(ref list, itemsLeft);
if (!Check(map, m, list, xLeft, yLeft, startTop, startZ, m.CanSwim, m.CantWalk, out hold))
{
MovementPool.AcquireMoveCache(ref list, itemsRight);
if (!Check(map, m, list, xRight, yRight, startTop, startZ, m.CanSwim, m.CantWalk, out hold))
{
moveIsOk = false;
}
}
}
}
MovementPool.ClearMoveCache(ref list, true);
if (!moveIsOk)
{
newZ = startZ;
}
return moveIsOk;
}
private static bool Verify(Item item, int x, int y)
{
return item != null && item.AtWorldPoint(x, y);
}
private static bool Verify(Item item, TileFlag reqFlags, bool ignoreMovableImpassables)
{
if (item == null)
{
return false;
}
if (ignoreMovableImpassables && item.Movable && item.ItemData.Impassable)
{
return false;
}
if ((item.ItemData.Flags & reqFlags) == 0)
{
return false;
}
if (item is BaseMulti || item.ItemID > TileData.MaxItemValue)
{
return false;
}
return true;
}
private static bool Verify(Item item, TileFlag reqFlags, bool ignoreMovableImpassables, int x, int y)
{
return Verify(item, reqFlags, ignoreMovableImpassables) && Verify(item, x, y);
}
private static void GetStartZ(IPoint3D p, Map map, Point3D loc, IEnumerable<Item> itemList, out int zLow, out int zTop)
{
int xCheck = loc.X, yCheck = loc.Y;
var landTile = map.Tiles.GetLandTile(xCheck, yCheck);
var landData = TileData.LandTable[landTile.ID & TileData.MaxLandValue];
var landBlocks = (landData.Flags & TileFlag.Impassable) != 0;
Mobile m = p as Mobile;
if (m != null)
{
if (landBlocks && m.CanSwim && (landData.Flags & TileFlag.Wet) != 0)
{
landBlocks = false;
}
else if (m.CantWalk && (landData.Flags & TileFlag.Wet) == 0)
{
landBlocks = true;
}
}
int landZ = 0, landCenter = 0, landTop = 0;
map.GetAverageZ(xCheck, yCheck, ref landZ, ref landCenter, ref landTop);
var considerLand = !landTile.Ignored;
var zCenter = zLow = zTop = 0;
var isSet = false;
if (considerLand && !landBlocks && loc.Z >= landCenter)
{
zLow = landZ;
zCenter = landCenter;
zTop = landTop;
isSet = true;
}
var staticTiles = map.Tiles.GetStaticTiles(xCheck, yCheck, true);
foreach (var tile in staticTiles)
{
var tileData = TileData.ItemTable[tile.ID & TileData.MaxItemValue];
var calcTop = (tile.Z + tileData.CalcHeight);
if (isSet && calcTop < zCenter)
{
continue;
}
if ((tileData.Flags & TileFlag.Surface) == 0 && ((m != null && !m.CanSwim) || (tileData.Flags & TileFlag.Wet) == 0))
{
continue;
}
if (loc.Z < calcTop)
{
continue;
}
if (m != null && m.CantWalk && (tileData.Flags & TileFlag.Wet) == 0)
{
continue;
}
zLow = tile.Z;
zCenter = calcTop;
var top = tile.Z + tileData.Height;
if (!isSet || top > zTop)
{
zTop = top;
}
isSet = true;
}
ItemData itemData;
foreach (var item in itemList)
{
itemData = item.ItemData;
var calcTop = item.Z + itemData.CalcHeight;
if (isSet && calcTop < zCenter)
{
continue;
}
if ((itemData.Flags & TileFlag.Surface) == 0 && ((m != null && !m.CanSwim) || (itemData.Flags & TileFlag.Wet) == 0))
{
continue;
}
if (loc.Z < calcTop)
{
continue;
}
if (m != null && m.CantWalk && (itemData.Flags & TileFlag.Wet) == 0)
{
continue;
}
zLow = item.Z;
zCenter = calcTop;
var top = item.Z + itemData.Height;
if (!isSet || top > zTop)
{
zTop = top;
}
isSet = true;
}
if (!isSet)
{
zLow = zTop = loc.Z;
}
else if (loc.Z > zTop)
{
zTop = loc.Z;
}
}
public 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;
}
}
private static class MovementPool
{
private static readonly object _MovePoolLock = new object();
private static readonly Queue<List<Item>> _MoveCachePool = new Queue<List<Item>>(0x400);
public static void AcquireMoveCache(ref List<Item> cache, IEnumerable<Item> items)
{
if (cache == null)
{
lock (_MovePoolLock)
{
cache = _MoveCachePool.Count > 0 ? _MoveCachePool.Dequeue() : new List<Item>(0x10);
}
}
else
{
cache.Clear();
}
cache.AddRange(items);
}
public static void ClearMoveCache(ref List<Item> cache, bool free)
{
if (cache != null)
{
cache.Clear();
}
if (!free)
{
return;
}
lock (_MovePoolLock)
{
if (_MoveCachePool.Count < 0x400)
{
_MoveCachePool.Enqueue(cache);
}
}
cache = null;
}
}
}
}

View File

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

View File

@@ -0,0 +1,709 @@
using System;
using System.Collections.Generic;
using Server.Items;
using Server.Mobiles;
namespace Server.Movement
{
public class MovementImpl : IMovementImpl
{
private const int PersonHeight = 16;
private const int StepHeight = 2;
private const TileFlag ImpassableSurface = TileFlag.Impassable | TileFlag.Surface;
private static bool m_AlwaysIgnoreDoors;
private static bool m_IgnoreMovableImpassables;
private static bool m_IgnoreSpellFields;
private static Point3D m_Goal;
public static bool AlwaysIgnoreDoors
{
get
{
return m_AlwaysIgnoreDoors;
}
set
{
m_AlwaysIgnoreDoors = value;
}
}
public static bool IgnoreMovableImpassables
{
get
{
return m_IgnoreMovableImpassables;
}
set
{
m_IgnoreMovableImpassables = value;
}
}
public static bool IgnoreSpellFields
{
get
{
return m_IgnoreSpellFields;
}
set
{
m_IgnoreSpellFields = value;
}
}
public static Point3D Goal
{
get
{
return m_Goal;
}
set
{
m_Goal = value;
}
}
public static void Configure()
{
Movement.Impl = new MovementImpl();
}
private MovementImpl()
{
}
private bool IsOk(Mobile m, bool ignoreDoors, bool ignoreSpellFields, int ourZ, int ourTop, StaticTile[] tiles, List<Item> items)
{
for (int i = 0; i < tiles.Length; ++i)
{
StaticTile check = tiles[i];
ItemData itemData = TileData.ItemTable[check.ID & TileData.MaxItemValue];
if ((itemData.Flags & ImpassableSurface) != 0) // Impassable || Surface
{
int checkZ = check.Z;
int checkTop = checkZ + itemData.CalcHeight;
if (checkTop > ourZ && ourTop > checkZ)
return false;
}
}
for (int i = 0; i < items.Count; ++i)
{
Item item = items[i];
int itemID = item.ItemID & TileData.MaxItemValue;
ItemData itemData = TileData.ItemTable[itemID];
TileFlag flags = itemData.Flags;
if ((flags & ImpassableSurface) != 0) // Impassable || Surface
{
if (ignoreDoors && ((flags & TileFlag.Door) != 0 || itemID == 0x692 || itemID == 0x846 || itemID == 0x873 || (itemID >= 0x6F5 && itemID <= 0x6F6)))
{
if (item is BaseHouseDoor && m != null && !((BaseHouseDoor)item).CheckAccess(m))
{
return false;
}
else
{
continue;
}
}
if (ignoreSpellFields && (itemID == 0x82 || itemID == 0x3946 || itemID == 0x3956))
{
continue;
}
// hidden containers, per EA
if ((flags & TileFlag.Container) != 0 && !item.Visible)
{
continue;
}
int checkZ = item.Z;
int checkTop = checkZ + itemData.CalcHeight;
if (checkTop > ourZ && ourTop > checkZ)
return false;
}
}
return true;
}
private readonly List<Item>[] m_Pools = new List<Item>[4]
{
new List<Item>(), new List<Item>(),
new List<Item>(), new List<Item>(),
};
private readonly List<Mobile>[] m_MobPools = new List<Mobile>[3]
{
new List<Mobile>(), new List<Mobile>(),
new List<Mobile>(),
};
private readonly List<Sector> m_Sectors = new List<Sector>();
private bool Check(Map map, IPoint3D p, List<Item> items, List<Mobile> mobiles, int x, int y, int startTop, int startZ, bool canSwim, bool cantWalk, out int newZ)
{
newZ = 0;
Mobile m = p as Mobile;
StaticTile[] tiles = map.Tiles.GetStaticTiles(x, y, true);
LandTile landTile = map.Tiles.GetLandTile(x, y);
bool landBlocks = (TileData.LandTable[landTile.ID & TileData.MaxLandValue].Flags & TileFlag.Impassable) != 0;
bool considerLand = !landTile.Ignored;
if (landBlocks && canSwim && (TileData.LandTable[landTile.ID & TileData.MaxLandValue].Flags & TileFlag.Wet) != 0) //Impassable, Can Swim, and Is water. Don't block it.
landBlocks = false;
else if (cantWalk && (TileData.LandTable[landTile.ID & TileData.MaxLandValue].Flags & TileFlag.Wet) == 0) //Can't walk and it's not water
landBlocks = true;
int landZ = 0, landCenter = 0, landTop = 0;
map.GetAverageZ(x, y, ref landZ, ref landCenter, ref landTop);
bool moveIsOk = false;
int stepTop = startTop + StepHeight;
int checkTop = startZ + PersonHeight;
bool ignoreDoors = (m_AlwaysIgnoreDoors || m == null || !m.Alive || m.Body.BodyID == 0x3DB || m.IsDeadBondedPet);
bool ignoreSpellFields = m is PlayerMobile && map != Map.Felucca;
#region Tiles
for (int i = 0; i < tiles.Length; ++i)
{
StaticTile tile = tiles[i];
ItemData itemData = TileData.ItemTable[tile.ID & TileData.MaxItemValue];
TileFlag flags = itemData.Flags;
#region SA
if (m != null && m.Flying && (itemData.Name == "hover over" || (flags & TileFlag.HoverOver) != 0))
{
newZ = tile.Z;
return true;
}
else if (m is StygianDragon && map == Map.TerMur)
{
if (x >= 307 && x <= 354 && y >= 126 && y <= 192)
{
if (tile.Z > newZ)
newZ = tile.Z;
moveIsOk = true;
}
else if (x >= 42 && x <= 89)
{
if ((y >= 333 && y <= 399) || (y >= 531 && y <= 597) || (y >= 739 && y <= 805))
{
if (tile.Z > newZ)
newZ = tile.Z;
moveIsOk = true;
}
}
}
#endregion
if ((flags & ImpassableSurface) == TileFlag.Surface || (canSwim && (flags & TileFlag.Wet) != 0)) // Surface && !Impassable
{
if (cantWalk && (flags & TileFlag.Wet) == 0)
continue;
int itemZ = tile.Z;
int itemTop = itemZ;
int ourZ = itemZ + itemData.CalcHeight;
int ourTop = ourZ + PersonHeight;
int testTop = checkTop;
if (moveIsOk)
{
int cmp = Math.Abs(ourZ - p.Z) - Math.Abs(newZ - p.Z);
if (cmp > 0 || (cmp == 0 && ourZ > newZ))
continue;
}
if (ourZ + PersonHeight > testTop)
testTop = ourZ + PersonHeight;
if (!itemData.Bridge)
itemTop += itemData.Height;
if (stepTop >= itemTop)
{
int landCheck = itemZ;
if (itemData.Height >= StepHeight)
landCheck += StepHeight;
else
landCheck += itemData.Height;
if (considerLand && landCheck < landCenter && landCenter > ourZ && testTop > landZ)
continue;
if (this.IsOk(m, ignoreDoors, ignoreSpellFields, ourZ, testTop, tiles, items))
{
newZ = ourZ;
moveIsOk = true;
}
}
}
}
#endregion
#region Items
for (int i = 0; i < items.Count; ++i)
{
Item item = items[i];
ItemData itemData = item.ItemData;
TileFlag flags = itemData.Flags;
#region SA
if (m != null && m.Flying && (itemData.Name == "hover over" || (flags & TileFlag.HoverOver) != 0))
{
newZ = item.Z;
return true;
}
#endregion
if (!item.Movable && ((flags & ImpassableSurface) == TileFlag.Surface || (m != null && m.CanSwim && (flags & TileFlag.Wet) != 0))) // Surface && !Impassable && !Movable
{
if (cantWalk && (flags & TileFlag.Wet) == 0)
continue;
int itemZ = item.Z;
int itemTop = itemZ;
int ourZ = itemZ + itemData.CalcHeight;
int ourTop = ourZ + PersonHeight;
int testTop = checkTop;
if (moveIsOk)
{
int cmp = Math.Abs(ourZ - p.Z) - Math.Abs(newZ - p.Z);
if (cmp > 0 || (cmp == 0 && ourZ > newZ))
continue;
}
if (ourZ + PersonHeight > testTop)
testTop = ourZ + PersonHeight;
if (!itemData.Bridge)
itemTop += itemData.Height;
if (stepTop >= itemTop)
{
int landCheck = itemZ;
if (itemData.Height >= StepHeight)
landCheck += StepHeight;
else
landCheck += itemData.Height;
if (considerLand && landCheck < landCenter && landCenter > ourZ && testTop > landZ)
continue;
if (this.IsOk(m, ignoreDoors, ignoreSpellFields, ourZ, testTop, tiles, items))
{
newZ = ourZ;
moveIsOk = true;
}
}
}
}
#endregion
if (considerLand && !landBlocks && stepTop >= landZ)
{
int ourZ = landCenter;
int ourTop = ourZ + PersonHeight;
int testTop = checkTop;
if (ourZ + PersonHeight > testTop)
testTop = ourZ + PersonHeight;
bool shouldCheck = true;
if (moveIsOk)
{
int cmp = Math.Abs(ourZ - p.Z) - Math.Abs(newZ - p.Z);
if (cmp > 0 || (cmp == 0 && ourZ > newZ))
shouldCheck = false;
}
if (shouldCheck && this.IsOk(m, ignoreDoors, ignoreSpellFields, ourZ, testTop, tiles, items))
{
newZ = ourZ;
moveIsOk = true;
}
}
#region Mobiles
if (moveIsOk)
{
for (int i = 0; moveIsOk && i < mobiles.Count; ++i)
{
Mobile mob = mobiles[i];
if (mob != m && (mob.Z + 15) > newZ && (newZ + 15) > mob.Z && !this.CanMoveOver(m, mob))
moveIsOk = false;
}
}
#endregion
return moveIsOk;
}
private bool CanMoveOver(Mobile m, Mobile t)
{
return (!t.Alive || m == null || !m.Alive || t.IsDeadBondedPet || m.IsDeadBondedPet) || (t.Hidden && t.IsStaff());
}
public bool CheckMovement(IPoint3D p, Map map, Point3D loc, Direction d, out int newZ)
{
if (map == null || map == Map.Internal)
{
newZ = 0;
return false;
}
int xStart = loc.X;
int yStart = loc.Y;
int xForward = xStart, yForward = yStart;
int xRight = xStart, yRight = yStart;
int xLeft = xStart, yLeft = yStart;
bool checkDiagonals = ((int)d & 0x1) == 0x1;
this.Offset(d, ref xForward, ref yForward);
this.Offset((Direction)(((int)d - 1) & 0x7), ref xLeft, ref yLeft);
this.Offset((Direction)(((int)d + 1) & 0x7), ref xRight, ref yRight);
if (xForward < 0 || yForward < 0 || xForward >= map.Width || yForward >= map.Height)
{
newZ = 0;
return false;
}
int startZ, startTop;
List<Item> itemsStart = this.m_Pools[0];
List<Item> itemsForward = this.m_Pools[1];
List<Item> itemsLeft = this.m_Pools[2];
List<Item> itemsRight = this.m_Pools[3];
bool ignoreMovableImpassables = m_IgnoreMovableImpassables;
TileFlag reqFlags = ImpassableSurface;
Mobile m = p as Mobile;
if (m != null && m.CanSwim)
reqFlags |= TileFlag.Wet;
List<Mobile> mobsForward = this.m_MobPools[0];
List<Mobile> mobsLeft = this.m_MobPools[1];
List<Mobile> mobsRight = this.m_MobPools[2];
bool checkMobs = (p is BaseCreature && !((BaseCreature)p).Controlled && (xForward != m_Goal.X || yForward != m_Goal.Y));
if (checkDiagonals)
{
Sector sectorStart = map.GetSector(xStart, yStart);
Sector sectorForward = map.GetSector(xForward, yForward);
Sector sectorLeft = map.GetSector(xLeft, yLeft);
Sector sectorRight = map.GetSector(xRight, yRight);
List<Sector> sectors = this.m_Sectors;
sectors.Add(sectorStart);
if (!sectors.Contains(sectorForward))
sectors.Add(sectorForward);
if (!sectors.Contains(sectorLeft))
sectors.Add(sectorLeft);
if (!sectors.Contains(sectorRight))
sectors.Add(sectorRight);
for (int i = 0; i < sectors.Count; ++i)
{
Sector sector = sectors[i];
for (int j = 0; j < sector.Items.Count; ++j)
{
Item item = sector.Items[j];
if (ignoreMovableImpassables && item.Movable && (item.ItemData.Flags & ImpassableSurface) != 0)
continue;
if ((item.ItemData.Flags & reqFlags) == 0)
continue;
if (sector == sectorStart && item.AtWorldPoint(xStart, yStart) && !(item is BaseMulti) && item.ItemID <= TileData.MaxItemValue)
itemsStart.Add(item);
else if (sector == sectorForward && item.AtWorldPoint(xForward, yForward) && !(item is BaseMulti) && item.ItemID <= TileData.MaxItemValue)
itemsForward.Add(item);
else if (sector == sectorLeft && item.AtWorldPoint(xLeft, yLeft) && !(item is BaseMulti) && item.ItemID <= TileData.MaxItemValue)
itemsLeft.Add(item);
else if (sector == sectorRight && item.AtWorldPoint(xRight, yRight) && !(item is BaseMulti) && item.ItemID <= TileData.MaxItemValue)
itemsRight.Add(item);
}
if (checkMobs)
{
for (int j = 0; j < sector.Mobiles.Count; ++j)
{
Mobile mob = sector.Mobiles[j];
if (sector == sectorForward && mob.X == xForward && mob.Y == yForward)
mobsForward.Add(mob);
else if (sector == sectorLeft && mob.X == xLeft && mob.Y == yLeft)
mobsLeft.Add(mob);
else if (sector == sectorRight && mob.X == xRight && mob.Y == yRight)
mobsRight.Add(mob);
}
}
}
if (this.m_Sectors.Count > 0)
this.m_Sectors.Clear();
}
else
{
Sector sectorStart = map.GetSector(xStart, yStart);
Sector sectorForward = map.GetSector(xForward, yForward);
if (sectorStart == sectorForward)
{
for (int i = 0; i < sectorStart.Items.Count; ++i)
{
Item item = sectorStart.Items[i];
if (ignoreMovableImpassables && item.Movable && (item.ItemData.Flags & ImpassableSurface) != 0)
continue;
if ((item.ItemData.Flags & reqFlags) == 0)
continue;
if (item.AtWorldPoint(xStart, yStart) && !(item is BaseMulti) && item.ItemID <= TileData.MaxItemValue)
itemsStart.Add(item);
else if (item.AtWorldPoint(xForward, yForward) && !(item is BaseMulti) && item.ItemID <= TileData.MaxItemValue)
itemsForward.Add(item);
}
}
else
{
for (int i = 0; i < sectorForward.Items.Count; ++i)
{
Item item = sectorForward.Items[i];
if (ignoreMovableImpassables && item.Movable && (item.ItemData.Flags & ImpassableSurface) != 0)
continue;
if ((item.ItemData.Flags & reqFlags) == 0)
continue;
if (item.AtWorldPoint(xForward, yForward) && !(item is BaseMulti) && item.ItemID <= TileData.MaxItemValue)
itemsForward.Add(item);
}
for (int i = 0; i < sectorStart.Items.Count; ++i)
{
Item item = sectorStart.Items[i];
if (ignoreMovableImpassables && item.Movable && (item.ItemData.Flags & ImpassableSurface) != 0)
continue;
if ((item.ItemData.Flags & reqFlags) == 0)
continue;
if (item.AtWorldPoint(xStart, yStart) && !(item is BaseMulti) && item.ItemID <= TileData.MaxItemValue)
itemsStart.Add(item);
}
}
if (checkMobs)
{
for (int i = 0; i < sectorForward.Mobiles.Count; ++i)
{
Mobile mob = sectorForward.Mobiles[i];
if (mob.X == xForward && mob.Y == yForward)
mobsForward.Add(mob);
}
}
}
this.GetStartZ(p, map, loc, itemsStart, out startZ, out startTop);
bool moveIsOk = this.Check(map, p, itemsForward, mobsForward, xForward, yForward, startTop, startZ, m != null && m.CanSwim, m != null && m.CantWalk, out newZ);
if (moveIsOk && checkDiagonals)
{
int hold;
if (m != null && m.Player && m.AccessLevel < AccessLevel.GameMaster)
{
if (!this.Check(map, p, itemsLeft, mobsLeft, xLeft, yLeft, startTop, startZ, m != null && m.CanSwim, m != null && m.CantWalk, out hold) || !this.Check(map, m, itemsRight, mobsRight, xRight, yRight, startTop, startZ, m != null && m.CanSwim, m != null && m.CantWalk, out hold))
moveIsOk = false;
}
else
{
if (!this.Check(map, p, itemsLeft, mobsLeft, xLeft, yLeft, startTop, startZ, m != null && m.CanSwim, m != null && m.CantWalk, out hold) && !this.Check(map, p, itemsRight, mobsRight, xRight, yRight, startTop, startZ, m != null && m.CanSwim, m != null && m.CantWalk, out hold))
moveIsOk = false;
}
}
for (int i = 0; i < (checkDiagonals ? 4 : 2); ++i)
{
if (this.m_Pools[i].Count != 0)
this.m_Pools[i].Clear();
}
for (int i = 0; i < (checkDiagonals ? 3 : 1); ++i)
{
if (this.m_MobPools[i].Count != 0)
this.m_MobPools[i].Clear();
}
if (!moveIsOk)
newZ = startZ;
return moveIsOk;
}
/*public bool CheckMovement(IPoint3D p, Direction d, out int newZ)
{
return this.CheckMovement(p, m.Map, m.Location, d, out newZ);
}*/
private void GetStartZ(IPoint3D p, Map map, Point3D loc, List<Item> itemList, out int zLow, out int zTop)
{
Mobile m = p as Mobile;
int xCheck = loc.X, yCheck = loc.Y;
LandTile landTile = map.Tiles.GetLandTile(xCheck, yCheck);
int landZ = 0, landCenter = 0, landTop = 0;
bool landBlocks = (TileData.LandTable[landTile.ID & TileData.MaxLandValue].Flags & TileFlag.Impassable) != 0;
if (landBlocks && m != null && m.CanSwim && (TileData.LandTable[landTile.ID & TileData.MaxLandValue].Flags & TileFlag.Wet) != 0)
landBlocks = false;
else if (m != null && m.CantWalk && (TileData.LandTable[landTile.ID & TileData.MaxLandValue].Flags & TileFlag.Wet) == 0)
landBlocks = true;
map.GetAverageZ(xCheck, yCheck, ref landZ, ref landCenter, ref landTop);
bool considerLand = !landTile.Ignored;
int zCenter = zLow = zTop = 0;
bool isSet = false;
if (considerLand && !landBlocks && loc.Z >= landCenter)
{
zLow = landZ;
zCenter = landCenter;
if (!isSet || landTop > zTop)
zTop = landTop;
isSet = true;
}
StaticTile[] staticTiles = map.Tiles.GetStaticTiles(xCheck, yCheck, true);
for (int i = 0; i < staticTiles.Length; ++i)
{
StaticTile tile = staticTiles[i];
ItemData id = TileData.ItemTable[tile.ID & TileData.MaxItemValue];
int calcTop = (tile.Z + id.CalcHeight);
if ((!isSet || calcTop >= zCenter) && ((id.Flags & TileFlag.Surface) != 0 || (m != null && m.CanSwim && (id.Flags & TileFlag.Wet) != 0)) && loc.Z >= calcTop)
{
if (m != null && m.CantWalk && (id.Flags & TileFlag.Wet) == 0)
continue;
zLow = tile.Z;
zCenter = calcTop;
int top = tile.Z + id.Height;
if (!isSet || top > zTop)
zTop = top;
isSet = true;
}
}
for (int i = 0; i < itemList.Count; ++i)
{
Item item = itemList[i];
ItemData id = item.ItemData;
int calcTop = item.Z + id.CalcHeight;
if ((!isSet || calcTop >= zCenter) && ((id.Flags & TileFlag.Surface) != 0 || (m != null && m.CanSwim && (id.Flags & TileFlag.Wet) != 0)) && loc.Z >= calcTop)
{
if (m != null && m.CantWalk && (id.Flags & TileFlag.Wet) == 0)
continue;
zLow = item.Z;
zCenter = calcTop;
int top = item.Z + id.Height;
if (!isSet || top > zTop)
zTop = top;
isSet = true;
}
}
if (!isSet)
zLow = zTop = loc.Z;
else if (loc.Z > zTop)
zTop = loc.Z;
}
public 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;
}
}
}
}

View File

@@ -0,0 +1,196 @@
using System;
using Server.Commands;
using Server.PathAlgorithms;
using Server.PathAlgorithms.FastAStar;
using Server.PathAlgorithms.SlowAStar;
using Server.Targeting;
namespace Server
{
public sealed class MovementPath
{
private static PathAlgorithm m_OverrideAlgorithm;
private readonly Map m_Map;
private readonly Point3D m_Start;
private readonly Point3D m_Goal;
private readonly Direction[] m_Directions;
public MovementPath(Mobile m, Point3D goal)
: this(m, goal, m.Map)
{
}
public MovementPath(IPoint3D p, Point3D goal, Map map)
{
Point3D start = new Point3D(p);
this.m_Map = map;
this.m_Start = start;
this.m_Goal = goal;
if (map == null || map == Map.Internal)
return;
if (Utility.InRange(start, goal, 1))
return;
try
{
PathAlgorithm alg = m_OverrideAlgorithm;
if (alg == null)
{
alg = FastAStarAlgorithm.Instance;
//if ( !alg.CheckCondition( m, map, start, goal ) ) // SlowAstar is still broken
// alg = SlowAStarAlgorithm.Instance; // TODO: Fix SlowAstar
}
if (alg != null && alg.CheckCondition(p, map, start, goal))
this.m_Directions = alg.Find(p, map, start, goal);
}
catch (Exception e)
{
Console.WriteLine("Warning: {0}: Pathing error from {1} to {2}", e.GetType().Name, start, goal);
Console.WriteLine(e.StackTrace);
}
}
public static PathAlgorithm OverrideAlgorithm
{
get
{
return m_OverrideAlgorithm;
}
set
{
m_OverrideAlgorithm = value;
}
}
public Map Map
{
get
{
return this.m_Map;
}
}
public Point3D Start
{
get
{
return this.m_Start;
}
}
public Point3D Goal
{
get
{
return this.m_Goal;
}
}
public Direction[] Directions
{
get
{
return this.m_Directions;
}
}
public bool Success
{
get
{
return (this.m_Directions != null && this.m_Directions.Length > 0);
}
}
public static void Initialize()
{
CommandSystem.Register("Path", AccessLevel.GameMaster, new CommandEventHandler(Path_OnCommand));
}
public static void Path_OnCommand(CommandEventArgs e)
{
e.Mobile.BeginTarget(-1, true, TargetFlags.None, new TargetCallback(Path_OnTarget));
e.Mobile.SendMessage("Target a location and a path will be drawn there.");
}
public static void Path_OnTarget(Mobile from, object obj)
{
IPoint3D p = obj as IPoint3D;
if (p == null)
return;
Spells.SpellHelper.GetSurfaceTop(ref p);
Path(from, p, FastAStarAlgorithm.Instance, "Fast", 0);
Path(from, p, SlowAStarAlgorithm.Instance, "Slow", 2);
m_OverrideAlgorithm = null;
/*MovementPath path = new MovementPath( from, new Point3D( p ) );
if ( !path.Success )
{
from.SendMessage( "No path to there could be found." );
}
else
{
//for ( int i = 0; i < path.Directions.Length; ++i )
// Timer.DelayCall( TimeSpan.FromSeconds( 0.1 + (i * 0.3) ), new TimerStateCallback( Pathfind ), new object[]{ from, path.Directions[i] } );
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 Items.RecallRune().MoveToWorld( new Point3D( x, y, z ), from.Map );
}
}*/
}
public static void Pathfind(object state)
{
object[] states = (object[])state;
Mobile from = (Mobile)states[0];
Direction d = (Direction)states[1];
try
{
from.Direction = d;
from.NetState.BlockAllPackets = true;
from.Move(d);
from.NetState.BlockAllPackets = false;
from.ProcessDelta();
}
catch
{
}
}
private static void Path(Mobile from, IPoint3D p, PathAlgorithm alg, string name, int zOffset)
{
m_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 Items.RecallRune().MoveToWorld(new Point3D(x, y, z + zOffset), from.Map);
}
}
}
}
}

View File

@@ -0,0 +1,35 @@
using System;
namespace Server.PathAlgorithms
{
public abstract class PathAlgorithm
{
private static readonly Direction[] m_CalcDirections = new Direction[9]
{
Direction.Up,
Direction.North,
Direction.Right,
Direction.West,
Direction.North,
Direction.East,
Direction.Left,
Direction.South,
Direction.Down
};
public abstract bool CheckCondition(IPoint3D p, Map map, Point3D start, Point3D goal);
public abstract Direction[] Find(IPoint3D p, 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];
}
}
}

View File

@@ -0,0 +1,206 @@
using System;
using CalcMoves = Server.Movement.Movement;
namespace Server
{
public class PathFollower
{
// Should we use pathfinding? 'false' for not
private static readonly bool Enabled = true;
private static readonly TimeSpan RepathDelay = TimeSpan.FromSeconds(2.0);
private readonly Mobile m_From;
private readonly IPoint3D m_Goal;
private MovementPath m_Path;
private int m_Index;
private Point3D m_Next, m_LastGoalLoc;
private DateTime m_LastPathTime;
private MoveMethod m_Mover;
public PathFollower(Mobile from, IPoint3D goal)
{
this.m_From = from;
this.m_Goal = goal;
}
public MoveMethod Mover
{
get
{
return this.m_Mover;
}
set
{
this.m_Mover = value;
}
}
public IPoint3D Goal
{
get
{
return this.m_Goal;
}
}
public MoveResult Move(Direction d)
{
if (this.m_Mover == null)
return (this.m_From.Move(d) ? MoveResult.Success : MoveResult.Blocked);
return this.m_Mover(d);
}
public Point3D GetGoalLocation()
{
if (this.m_Goal is Item)
return ((Item)this.m_Goal).GetWorldLocation();
return new Point3D(this.m_Goal);
}
public void Advance(ref Point3D p, int index)
{
if (this.m_Path != null && this.m_Path.Success)
{
Direction[] dirs = this.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()
{
this.m_Path = null;
}
public bool CheckPath()
{
if (!Enabled)
return false;
bool repath = false;
Point3D goal = this.GetGoalLocation();
if (this.m_Path == null)
repath = true;
else if ((!this.m_Path.Success || goal != this.m_LastGoalLoc) && (this.m_LastPathTime + RepathDelay) <= DateTime.UtcNow)
repath = true;
else if (this.m_Path.Success && this.Check(this.m_From.Location, this.m_LastGoalLoc, 0))
repath = true;
if (!repath)
return false;
this.m_LastPathTime = DateTime.UtcNow;
this.m_LastGoalLoc = goal;
this.m_Path = new MovementPath(this.m_From, goal);
this.m_Index = 0;
this.m_Next = this.m_From.Location;
this.Advance(ref this.m_Next, this.m_Index);
return true;
}
public bool Check(Point3D loc, Point3D goal, int range)
{
if (!Utility.InRange(loc, goal, range))
return false;
if (range <= 1 && Math.Abs(loc.Z - goal.Z) >= 16)
return false;
return true;
}
public bool Follow(bool run, int range)
{
Point3D goal = this.GetGoalLocation();
Direction d;
if (this.Check(this.m_From.Location, goal, range))
return true;
bool repathed = this.CheckPath();
if (!Enabled || !this.m_Path.Success)
{
d = this.m_From.GetDirectionTo(goal);
if (run)
d |= Direction.Running;
this.m_From.SetDirection(d);
this.Move(d);
return this.Check(this.m_From.Location, goal, range);
}
d = this.m_From.GetDirectionTo(this.m_Next);
if (run)
d |= Direction.Running;
this.m_From.SetDirection(d);
MoveResult res = this.Move(d);
if (res == MoveResult.Blocked)
{
if (repathed)
return false;
this.m_Path = null;
this.CheckPath();
if (!this.m_Path.Success)
{
d = this.m_From.GetDirectionTo(goal);
if (run)
d |= Direction.Running;
this.m_From.SetDirection(d);
this.Move(d);
return this.Check(this.m_From.Location, goal, range);
}
d = this.m_From.GetDirectionTo(this.m_Next);
if (run)
d |= Direction.Running;
this.m_From.SetDirection(d);
res = this.Move(d);
if (res == MoveResult.Blocked)
return false;
}
if (this.m_From.X == this.m_Next.X && this.m_From.Y == this.m_Next.Y)
{
if (this.m_From.Z == this.m_Next.Z)
{
++this.m_Index;
this.Advance(ref this.m_Next, this.m_Index);
}
else
{
this.m_Path = null;
}
}
return this.Check(this.m_From.Location, goal, range);
}
}
}

View File

@@ -0,0 +1,281 @@
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
{
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 const int MaxDepth = 300;
private const int MaxNodes = MaxDepth * 16;
private Point3D m_Goal;
public int Heuristic(int x, int y, int z)
{
x -= this.m_Goal.X;
y -= this.m_Goal.Y;
z -= this.m_Goal.Z;
x *= 11;
y *= 11;
return (x * x) + (y * y) + (z * z);
}
public override bool CheckCondition(IPoint3D p, Map map, Point3D start, Point3D goal)
{
return false;
}
public override Direction[] Find(IPoint3D p, Map map, Point3D start, Point3D goal)
{
this.m_Goal = goal;
BaseCreature bc = p 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 = this.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, sucCount = 0, pathCount = 0;
int popIndex, curF;
int x, y, z;
int depth = 0;
int xBacktrack, yBacktrack, zBacktrack, iBacktrack = 0;
open[openCount++] = startNode;
while (openCount > 0)
{
curNode = open[0];
curF = curNode.g + curNode.h;
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;
xBacktrack = curNode.px;
yBacktrack = curNode.py;
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];
sucCount = 0;
if (bc != null)
{
MoveImpl.AlwaysIgnoreDoors = bc.CanOpenDoors;
MoveImpl.IgnoreMovableImpassables = bc.CanMoveOverObstacles;
}
MoveImpl.Goal = goal;
for (int i = 0; i < 8; ++i)
{
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;
}
if (CalcMoves.CheckMovement(p, 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)this.GetDirection(curNode.x, curNode.y, x, y);
successors[i].h = this.Heuristic(x, y, z);
if (openCount == MaxNodes)
break;
open[openCount++] = successors[i];
}
if (openCount == MaxNodes || closedCount == MaxNodes)
break;
closed[closedCount++] = curNode;
}
return null;
}
}
}