ModernUO/Projects/Scripts/Engines/Pathing/Movement.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

625 lines
19 KiB
C#

using System;
using System.Collections.Generic;
using Server.Items;
using Server.Mobiles;
namespace Server
{
public class MovementImpl : IMovementImpl
{
private const int PersonHeight = 16;
private const int StepHeight = 2;
private const TileFlag ImpassableSurface = TileFlag.Impassable | TileFlag.Surface;
private List<Mobile>[] m_MobPools = {
new List<Mobile>(), new List<Mobile>(),
new List<Mobile>()
};
private List<Item>[] m_Pools = {
new List<Item>(), new List<Item>(),
new List<Item>(), new List<Item>()
};
private List<Sector> m_Sectors = new List<Sector>();
private MovementImpl()
{
}
public static bool AlwaysIgnoreDoors{ get; set; }
public static bool IgnoreMovableImpassables{ get; set; }
public static bool IgnoreSpellFields{ get; set; }
public static Point3D Goal{ get; set; }
public bool CheckMovement(Mobile m, 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;
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;
}
List<Item> itemsStart = m_Pools[0];
List<Item> itemsForward = m_Pools[1];
List<Item> itemsLeft = m_Pools[2];
List<Item> itemsRight = m_Pools[3];
bool ignoreMovableImpassables = IgnoreMovableImpassables;
TileFlag reqFlags = ImpassableSurface;
if (m.CanSwim)
reqFlags |= TileFlag.Wet;
List<Mobile> mobsForward = m_MobPools[0];
List<Mobile> mobsLeft = m_MobPools[1];
List<Mobile> mobsRight = m_MobPools[2];
BaseCreature bc = m as BaseCreature;
bool checkMobs = bc?.Controlled == false &&
(xForward != Goal.X || yForward != 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 = 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 (!(item is BaseMulti))
{
if (sector == sectorStart && item.AtWorldPoint(xStart, yStart) &&
item.ItemID <= TileData.MaxItemValue)
itemsStart.Add(item);
else if (sector == sectorForward && item.AtWorldPoint(xForward, yForward) &&
item.ItemID <= TileData.MaxItemValue)
itemsForward.Add(item);
else if (sector == sectorLeft && item.AtWorldPoint(xLeft, yLeft) &&
item.ItemID <= TileData.MaxItemValue)
itemsLeft.Add(item);
else if (sector == sectorRight && item.AtWorldPoint(xRight, yRight) &&
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 (m_Sectors.Count > 0)
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 is BaseMulti))
{
if (item.AtWorldPoint(xStart, yStart) &&
item.ItemID <= TileData.MaxItemValue)
itemsStart.Add(item);
else if (item.AtWorldPoint(xForward, yForward) &&
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);
}
}
GetStartZ(m, map, loc, itemsStart, out int startZ, out int startTop);
bc?.DebugSay("My Z/Top is {0} {1}", startZ, startTop);
bool moveIsOk = Check(map, m, itemsForward, mobsForward, xForward, yForward, startTop, startZ, m.CanSwim,
m.CantWalk, out newZ);
bc?.DebugSay("Can I move? {0} {1}", moveIsOk, newZ);
if (moveIsOk && checkDiagonals)
{
if (m.Player && m.AccessLevel < AccessLevel.GameMaster)
{
if (!(Check(map, m, itemsLeft, mobsLeft, xLeft, yLeft, startTop, startZ, m.CanSwim, m.CantWalk,
out _) && Check(map, m, itemsRight, mobsRight, xRight, yRight, startTop, startZ, m.CanSwim,
m.CantWalk, out _)))
moveIsOk = false;
}
else
{
if (!(Check(map, m, itemsLeft, mobsLeft, xLeft, yLeft, startTop, startZ, m.CanSwim, m.CantWalk,
out _) || Check(map, m, itemsRight, mobsRight, xRight, yRight, startTop, startZ, m.CanSwim,
m.CantWalk, out _)))
moveIsOk = false;
}
bc?.DebugSay("Can I still move? {0} {1}", moveIsOk, newZ);
}
for (int i = 0; i < (checkDiagonals ? 4 : 2); ++i)
if (m_Pools[i].Count != 0)
m_Pools[i].Clear();
for (int i = 0; i < (checkDiagonals ? 3 : 1); ++i)
if (m_MobPools[i].Count != 0)
m_MobPools[i].Clear();
if (!moveIsOk)
newZ = startZ;
return moveIsOk;
}
public bool CheckMovement(Mobile m, Direction d, out int newZ) => CheckMovement(m, m.Map, m.Location, d, out newZ);
public static void Configure()
{
Movement.Impl = new MovementImpl();
}
private bool IsOk(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))
continue;
if (ignoreSpellFields && (itemID == 0x82 || itemID == 0x3946 || itemID == 0x3956))
continue;
int checkZ = item.Z;
int checkTop = checkZ + itemData.CalcHeight;
if (checkTop > ourZ && ourTop > checkZ)
return false;
}
}
return true;
}
private bool Check(Map map, Mobile m, List<Item> items, IReadOnlyList<Mobile> mobiles, 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);
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 = AlwaysIgnoreDoors || !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;
// Surface && !Impassable
if ((flags & ImpassableSurface) != TileFlag.Surface && (!canSwim || (flags & TileFlag.Wet) == 0))
continue;
if (cantWalk && (flags & TileFlag.Wet) == 0)
continue;
int itemZ = tile.Z;
int itemTop = itemZ;
int ourZ = itemZ + itemData.CalcHeight;
int 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 (ourZ + PersonHeight > testTop)
testTop = ourZ + PersonHeight;
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))
{
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;
if (!item.Movable && ((flags & ImpassableSurface) == TileFlag.Surface ||
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 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 (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 (IsOk(ignoreDoors, ignoreSpellFields, ourZ, testTop, tiles, items))
{
newZ = ourZ;
moveIsOk = true;
}
}
}
}
#endregion
if (considerLand && !landBlocks && stepTop >= landZ)
{
int ourZ = landCenter;
int testTop = checkTop;
if (ourZ + PersonHeight > testTop)
testTop = ourZ + PersonHeight;
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))
{
newZ = ourZ;
moveIsOk = true;
}
}
#region Mobiles
for (int i = 0; moveIsOk && i < mobiles.Count; ++i)
{
Mobile mob = mobiles[i];
if (mob != m && mob.Z + 15 > newZ && newZ + 15 > mob.Z && !CanMoveOver(m, mob))
moveIsOk = false;
}
#endregion
return moveIsOk;
}
private bool CanMoveOver(Mobile m, Mobile t) =>
!t.Alive || !m.Alive || t.IsDeadBondedPet || m.IsDeadBondedPet ||
t.Hidden && t.AccessLevel > AccessLevel.Player;
private void GetStartZ(Mobile m, Map map, Point3D loc, IReadOnlyList<Item> itemList, out int zLow, out int zTop)
{
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.CanSwim &&
(TileData.LandTable[landTile.ID & TileData.MaxLandValue].Flags & TileFlag.Wet) != 0)
landBlocks = false;
else if (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 (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.CanSwim && (id.Flags & TileFlag.Wet) != 0) && loc.Z >= calcTop)
{
if (m.CantWalk && (id.Flags & TileFlag.Wet) == 0)
continue;
zLow = tile.Z;
zCenter = calcTop;
zTop = tile.Z + id.Height;
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.CanSwim && (id.Flags & TileFlag.Wet) != 0) && loc.Z >= calcTop)
{
if (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;
}
}
}
}