ModernUO/Projects/Scripts/Engines/Pathing/FastMovement.cs
Kamron Batman 179cb50557 Updates Packets & Randomizer (#43)
* Fixes a bug with the main loop. Removes unnecessary optimizations for RDRand.

* WIP - Rewriting packets.

* Cleanup and updates README

* Converts more packets

* Fixes header

* Converts Effects packets.

* Forgot the send command

* Adds the start of containers packets.

* Adds message packets with caching

* Extends the send methods

* Starts to add Acquire methods

* Converted the packets

* Cleanup

* Cleanup

* Adds more packets

* Adds more packets

* Fixes clearing arrays from pool. Adds Mobile Incoming

* Converts more packets.

* Moves more packets

* Moves more packets

* Test compression

* Merges

* Migrating to an idiomatic syntax that also supports compression, and proxying.

* Optimize the stack alloc. Changes attributes

* Converted container packets

* Visual Studio doesn't auto save files. I am still getting used to subpar IDEs.

* Adds static packet caching. Will profile later. Converts more packets

* Fixes packets and changes how compression is configured

* WIP - Adds basics for Gumps. Not finished though.

* Fix file

* Fixes formatting

* Changed SpanWriter to be more idiomatic.

* Fixes Span vs RawSpan and missing stackallocs

* Converts over some more gumps

* WIP - Deletes 32bit support.

* Drops RDRand32 support.

* Fixes gump compilation

* Converting gump components

* Removes old huffman compression function

* Revert signature for backwards compatibility

* WIP - Converting more gump components

* Creates ArraySet for the strings. Updates AppendTo to reference that.

* Converts the maining gump components

* Cleans up gump components

* Cleans up directives

* Cleans up ArraySet and moves it. Adds null-coalescing-assignment

* Cleanup, Target Packets, and C# 8 changes.

* Removed OPL Packet

* World packets

* Fixing packet uses

* Cleans up code. Fixes packet uses in various places.

* More cleanup for packets

* Code cleanup

* Converts more packet uses and cleans up more code

* More code cleanup

* Finishes fixing the packets in Item

* Updates secure trade packets

* Updates core and gets it to compile.

* Moved packets to scripts. Fixed account handler use of packets

* Code cleanup

* Updates chat packets

* Code cleanuo

* Adds party packets, but need to implement them.

* Party packets WIP

* Finishes party packets

* Rearrange movement namespaces and classes

* Finishes plant packets

* Code Formatting

* Fixes moving effects

* Code cleanup, eliminates equipinfo

* Adds more packets. Fixes bugs with various packets.

* Finishes mahjon packets

* Fixes mahjong packet

* Code cleanup

* Finishes mahjong packets

* Adds Map packets

* Add multifacet maps and charts

* Cleans up some packets with UTF8

* Optimizes packets

* Cleans up more packets. Moves the MessageHelper

* Removes assistant support. Removes extended protocol. Incorporates MapUO packets as normal packets.

* Updates protocol extensions packet receiver

* Fixes a few bugs. Fixes a few more packets.

* Code cleanup and fixing more packets

* Fixes packet effects

* Cleaned up more code

* Buff Icon cleanup

* Removed unused constructors

* Code Cleanup. Adds BoatHS Packets

* Moves house files. Updates house foundation packets.

* Deployment cleanup

* Fixes:

* Code cleanup

* More code cleanup

* More code cleanup

* Cleaned up BaseHouse

* Enforces styling

* Converts foreach to linq where possible.

* Dont need that

* Goals/Readme updates

* Removes 32bit support at the highest level. Turns on HRT by default.

* Code cleanup. Fixes extended features packet.

* Fixes various bugs

* Code cleanup

* Code cleanup

* Code cleanup. Fixes gump X/Y assignment.

* Code cleanup using |= operator

* More code cleanup

* Cleanup

* Fixes spacing issues. Thanks Visual Studio. You suck.

* Compiler error

* Fixes NPE from RunUO 2.7

* Renames ScriptCompiler to AssemblyHandler. Fixes packets. Updates README

* Fixes more packets. Stupid trailing nulls.

* Fixes various bugs.

* Fixes for gumps

* Fixes more gump stuff. Going to split it out later since it is getting insane

* Recoded the gump writing

* WIP

* *Added output path of scripts project to dev branch
*Activated debugging in code
2019-11-11 12:40:16 +01:00

544 lines
16 KiB
C#

#region References
using System;
using System.Collections.Generic;
using System.Linq;
using Server.Items;
using Server.Mobiles;
#endregion
namespace Server
{
public class FastMovementImpl : IMovementImpl
{
private const int PersonHeight = 16;
private const int StepHeight = 2;
private const TileFlag ImpassableSurface = TileFlag.Impassable | TileFlag.Surface;
public static bool Enabled = false;
private static IMovementImpl _Successor;
private FastMovementImpl()
{
}
public bool CheckMovement(Mobile m, Map map, Point3D loc, Direction d, out int newZ)
{
if (!Enabled && _Successor != null) return _Successor.CheckMovement(m, map, loc, d, out 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;
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;
}
IEnumerable<Item> itemsStart, itemsForward, itemsLeft, itemsRight;
bool ignoreMovableImpassables = MovementImpl.IgnoreMovableImpassables;
TileFlag reqFlags = ImpassableSurface;
if (m.CanSwim) reqFlags |= TileFlag.Wet;
if (checkDiagonals)
{
Sector sStart = map.GetSector(xStart, yStart);
Sector sForward = map.GetSector(xForward, yForward);
Sector sLeft = map.GetSector(xLeft, yLeft);
Sector 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
{
Sector sStart = map.GetSector(xStart, yStart);
Sector 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(m, map, loc, itemsStart, out int startZ, out int startTop);
List<Item> list = null;
MovementPool.AcquireMoveCache(ref list, itemsForward);
bool moveIsOk = Check(map, m, list, xForward, yForward, startTop, startZ, m.CanSwim, m.CantWalk, out newZ);
if (moveIsOk && checkDiagonals)
{
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 _))
{
moveIsOk = false;
}
else
{
MovementPool.AcquireMoveCache(ref list, itemsRight);
if (!Check(map, m, list, xRight, yRight, startTop, startZ, m.CanSwim, m.CantWalk, out _))
moveIsOk = false;
}
}
else
{
MovementPool.AcquireMoveCache(ref list, itemsLeft);
if (!Check(map, m, list, xLeft, yLeft, startTop, startZ, m.CanSwim, m.CantWalk, out _))
{
MovementPool.AcquireMoveCache(ref list, itemsRight);
if (!Check(map, m, list, xRight, yRight, startTop, startZ, m.CanSwim, m.CantWalk, out _))
moveIsOk = false;
}
}
}
MovementPool.ClearMoveCache(ref list, true);
if (!moveIsOk) newZ = startZ;
return moveIsOk;
}
public bool CheckMovement(Mobile m, Direction d, out int newZ) =>
!Enabled && _Successor != null
? _Successor.CheckMovement(m, d, out newZ)
: CheckMovement(m, m.Map, m.Location, d, out newZ);
public static void Initialize()
{
_Successor = Movement.Impl;
Movement.Impl = new FastMovementImpl();
}
private static bool IsOk(StaticTile tile, int ourZ, int ourTop)
{
ItemData itemData = TileData.ItemTable[tile.ID & TileData.MaxItemValue];
return tile.Z + itemData.CalcHeight <= ourZ || ourTop <= tile.Z || (itemData.Flags & ImpassableSurface) == 0;
}
private static bool IsOk(Item item, int ourZ, int ourTop, bool ignoreDoors, bool ignoreSpellFields)
{
int itemID = item.ItemID & TileData.MaxItemValue;
ItemData 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 true;
if ((itemID == 0x82 || itemID == 0x3946 || itemID == 0x3956) && ignoreSpellFields) return true;
return item.Z + itemData.CalcHeight <= ourZ || ourTop <= item.Z;
}
private static bool IsOk(
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(i, ourZ, ourTop, ignoreDoors, ignoreSpellFields));
}
private static bool Check(
Map map,
Mobile m,
IReadOnlyCollection<Item> items,
int x,
int y,
int startTop,
int startZ,
bool canSwim,
bool cantWalk,
out int newZ)
{
newZ = 0;
StaticTile[] tiles = map.Tiles.GetStaticTiles(x, y, true);
LandTile landTile = map.Tiles.GetLandTile(x, y);
LandData landData = TileData.LandTable[landTile.ID & TileData.MaxLandValue];
bool landBlocks = (landData.Flags & TileFlag.Impassable) != 0;
bool considerLand = !landTile.Ignored;
if (landBlocks && canSwim && (landData.Flags & TileFlag.Wet) != 0)
landBlocks = false;
else if (cantWalk && (landData.Flags & TileFlag.Wet) == 0) 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 = MovementImpl.AlwaysIgnoreDoors || !m.Alive || m.IsDeadBondedPet || m.Body.IsGhost ||
m.Body.BodyID == 987;
bool ignoreSpellFields = m is PlayerMobile && map.MapID != 0;
int itemZ, itemTop, ourZ, ourTop, testTop;
ItemData itemData;
TileFlag flags;
#region Tiles
foreach (StaticTile tile in tiles)
{
itemData = TileData.ItemTable[tile.ID & TileData.MaxItemValue];
#region SA
if (m.Flying && Insensitive.Equals(itemData.Name, "hover over"))
{
newZ = tile.Z;
return true;
}
// Stygian Dragon
if (m.Body == 826 && 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
flags = itemData.Flags;
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)
{
int cmp = Math.Abs(ourZ - m.Z) - Math.Abs(newZ - m.Z);
if (cmp > 0 || cmp == 0 && ourZ > newZ) continue;
}
if (ourTop > testTop) testTop = ourTop;
if (!itemData.Bridge) itemTop += itemData.Height;
if (stepTop < itemTop) continue;
int landCheck = itemZ;
if (itemData.Height >= StepHeight)
landCheck += StepHeight;
else
landCheck += itemData.Height;
if (considerLand && landCheck < landCenter && landCenter > ourZ && testTop > landZ) continue;
if (!IsOk(ignoreDoors, ignoreSpellFields, ourZ, testTop, tiles, items)) continue;
newZ = ourZ;
moveIsOk = true;
}
#endregion
#region Items
foreach (Item item in items)
{
itemData = item.ItemData;
flags = itemData.Flags;
#region SA
if (m.Flying && Insensitive.Equals(itemData.Name, "hover over"))
{
newZ = item.Z;
return true;
}
#endregion
if (item.Movable) continue;
if ((flags & ImpassableSurface) != TileFlag.Surface && (!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)
{
int cmp = Math.Abs(ourZ - m.Z) - Math.Abs(newZ - m.Z);
if (cmp > 0 || cmp == 0 && ourZ > newZ) continue;
}
if (ourTop > testTop) testTop = ourTop;
if (!itemData.Bridge) itemTop += itemData.Height;
if (stepTop < itemTop) continue;
int landCheck = itemZ;
if (itemData.Height >= StepHeight)
landCheck += StepHeight;
else
landCheck += itemData.Height;
if (considerLand && landCheck < landCenter && landCenter > ourZ && testTop > landZ) continue;
if (!IsOk(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;
bool shouldCheck = true;
if (moveIsOk)
{
int cmp = Math.Abs(ourZ - m.Z) - Math.Abs(newZ - m.Z);
if (cmp > 0 || cmp == 0 && ourZ > newZ) shouldCheck = false;
}
if (!shouldCheck || !IsOk(ignoreDoors, ignoreSpellFields, ourZ, testTop, tiles, items)) return moveIsOk;
newZ = ourZ;
moveIsOk = true;
return moveIsOk;
}
private static bool Verify(Item item, int x, int y) => item.AtWorldPoint(x, y);
private static bool Verify(Item item, TileFlag reqFlags, bool ignoreMovableImpassables) =>
item != null && (!ignoreMovableImpassables || !item.Movable || !item.ItemData.Impassable) &&
(item.ItemData.Flags & reqFlags) != 0 && !(item is BaseMulti) && item.ItemID <= TileData.MaxItemValue;
private static bool Verify(Item item, TileFlag reqFlags, bool ignoreMovableImpassables, int x, int y) => Verify(item, reqFlags, ignoreMovableImpassables) && Verify(item, x, y);
private static void GetStartZ(Mobile m, Map map, Point3D loc, IEnumerable<Item> itemList, out int zLow, out int zTop)
{
int xCheck = loc.X, yCheck = loc.Y;
LandTile landTile = map.Tiles.GetLandTile(xCheck, yCheck);
LandData landData = TileData.LandTable[landTile.ID & TileData.MaxLandValue];
bool landBlocks = (landData.Flags & TileFlag.Impassable) != 0;
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);
bool considerLand = !landTile.Ignored;
int zCenter = zLow = zTop = 0;
bool isSet = false;
if (considerLand && !landBlocks && loc.Z >= landCenter)
{
zLow = landZ;
zCenter = landCenter;
zTop = landTop;
isSet = true;
}
StaticTile[] staticTiles = map.Tiles.GetStaticTiles(xCheck, yCheck, true);
foreach (StaticTile tile in staticTiles)
{
ItemData tileData = TileData.ItemTable[tile.ID & TileData.MaxItemValue];
int calcTop = tile.Z + tileData.CalcHeight;
if (isSet && calcTop < zCenter) continue;
if ((tileData.Flags & TileFlag.Surface) == 0 &&
(!m.CanSwim || (tileData.Flags & TileFlag.Wet) == 0)) continue;
if (loc.Z < calcTop) continue;
if (m.CantWalk && (tileData.Flags & TileFlag.Wet) == 0) continue;
zLow = tile.Z;
zCenter = calcTop;
int top = tile.Z + tileData.Height;
if (!isSet || top > zTop) zTop = top;
isSet = true;
}
foreach (Item item in itemList)
{
ItemData itemData = item.ItemData;
int calcTop = item.Z + itemData.CalcHeight;
if (isSet && calcTop < zCenter) continue;
if ((itemData.Flags & TileFlag.Surface) == 0 &&
(!m.CanSwim || (itemData.Flags & TileFlag.Wet) == 0)) continue;
if (loc.Z < calcTop) continue;
if (m.CantWalk && (itemData.Flags & TileFlag.Wet) == 0) continue;
zLow = item.Z;
zCenter = calcTop;
int 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)
{
cache?.Clear();
if (!free) return;
lock (_MovePoolLock)
{
if (_MoveCachePool.Count < 0x400) _MoveCachePool.Enqueue(cache);
}
cache = null;
}
}
}
}