diff --git a/inf3.csproj b/inf3.csproj index 534cde3..1bd44a0 100644 --- a/inf3.csproj +++ b/inf3.csproj @@ -80,6 +80,7 @@ + diff --git a/src/Pathfinder.cs b/src/Pathfinder.cs new file mode 100644 index 0000000..d193d38 --- /dev/null +++ b/src/Pathfinder.cs @@ -0,0 +1,46 @@ +using System.Collections.Generic; +using System.Runtime.InteropServices; + +namespace WorldOfPeacecraft +{ + class Pathfinder + { + private struct PathNode + { + [MarshalAs(UnmanagedType.U4)] + public uint X; + [MarshalAs(UnmanagedType.U4)] + public uint Y; + } + + public static LinkedList FindPath (uint posFrom, uint posTo, Map map) + { + Tile[,] tiles = map.GetTiles (); + uint mapWidth = tiles.GetLength (0); + uint mapHeight = tiles.GetLength (1); + bool[] boolMap = new bool[mapHeight * mapWidth]; + for (int y = 0; y < mapHeight; y++) { + for (int x = 0; x < mapWidth; y++) { + boolMap [x + y * mapWidth] = tiles [x, y].IsWalkable (); + } + } + PathNode[] result = new PathNode[mapHeight * mapWidth]; + int noSteps = findPath (posFrom, posTo, mapWidth, mapHeight, boolMap, result); + LinkedList mappedResult = new LinkedList(); + for (int i = 0; i < noSteps; i++) { + mappedResult.AddLast(new Coordinate(result[i].X, result[i].Y)); + } + return mappedResult; + } + + [DllImport("pathfinding.dll")] + [return: MarshalAs(UnmanagedType.U4)] + private static extern uint findPath( + [MarshalAs(UnmanagedType.U4)] uint posFrom, + [MarshalAs(UnmanagedType.U4)] uint posTo, + [MarshalAs(UnmanagedType.U4)] uint mapWidth, + [MarshalAs(UnmanagedType.U4)] uint mapHeight, + [MarshalAs(UnmanagedType.LPArray)] bool[] map, + [MarshalAs(UnmanagedType.LPArray)] PathNode[] result); + } +} \ No newline at end of file