-
Notifications
You must be signed in to change notification settings - Fork 37
/
PathFinder.cs
78 lines (71 loc) · 2.57 KB
/
PathFinder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using StardewValley;
using xTile;
using xTile.Layers;
namespace Entoarox.DynamicDungeons
{
internal static class PathFinder
{
/*********
** Public methods
*********/
public static List<Point> FindPath(Map location, Point start, Point end)
{
Layer backLayer = location.GetLayer("Back");
Layer buildingsLayer = location.GetLayer("Buildings");
List<PathNode> nodes = new List<PathNode>();
sbyte[,] array =
{
{ -1, 0 },
{ 1, 0 },
{ 0, 1 },
{ 0, -1 }
};
bool IsOccupied(int x, int y)
{
return x < 0 || x >= buildingsLayer.LayerWidth || y < 0 || y > buildingsLayer.DisplayHeight || buildingsLayer.Tiles[x, y] != null || backLayer.Tiles[x, y]?.TileIndex != 138;
}
List<Point> ReconstructPath(PathNode node)
{
List<Point> myp = new List<Point>
{
new Point(node.x, node.y)
};
while (node.parent != null)
{
node = node.parent;
myp.Add(new Point(node.x, node.y));
}
return myp;
}
PriorityQueue queue = new PriorityQueue();
int limit = buildingsLayer.LayerWidth * buildingsLayer.LayerHeight / 2;
queue.Enqueue(new PathNode(start.X, start.Y, null), Math.Abs(end.X - start.X) + Math.Abs(end.Y - start.Y));
while (!queue.IsEmpty())
{
PathNode node = queue.Dequeue();
if (node.x == end.X && node.y == end.Y)
return ReconstructPath(node);
if (nodes.Contains(node))
continue;
nodes.Add(node);
for (int i = 0; i < 4; i++)
{
PathNode node2 = new PathNode(node.x + array[i, 0], node.y + array[i, 1], node)
{
g = (byte)(node.g + 1)
};
int priority = node2.g + Math.Abs(end.X - node2.x) + Math.Abs(end.Y - node.y);
if (!IsOccupied(node2.x, node2.y) && !queue.Contains(node2, priority))
queue.Enqueue(node2, priority);
}
limit--;
if (limit < 1)
return null;
}
return null;
}
}
}