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

272 lines
7.1 KiB
C#

using System;
using Server.Mobiles;
namespace Server.PathAlgorithms
{
public class SlowAStarAlgorithm : PathAlgorithm
{
public struct PathNode
{
public int x, y, z;
public int g, h;
public int px, py, pz;
public int dir;
}
private const int MaxDepth = 300;
private const int MaxNodes = MaxDepth * 16;
public static PathAlgorithm Instance = new SlowAStarAlgorithm();
private static PathNode[] m_Closed = new PathNode[MaxNodes];
private static PathNode[] m_Open = new PathNode[MaxNodes];
private static PathNode[] m_Successors = new PathNode[8];
private static 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)
{
MovementImpl.AlwaysIgnoreDoors = bc.CanOpenDoors;
MovementImpl.IgnoreMovableImpassables = bc.CanMoveOverObstacles;
}
MovementImpl.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 (Movement.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;
}
}
MovementImpl.AlwaysIgnoreDoors = false;
MovementImpl.IgnoreMovableImpassables = false;
MovementImpl.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;
}
}
}