private List<Hex3D> Pathfinder(Hex3D s, Hex3D d)
{
Console.WriteLine(s.ToString() + ", " + d.ToString());
//Assume d is occupable.
if (s == d) {
List<Hex3D> ret = new List<Hex3D>();
ret.Add(d);
return ret;
}
List<Hex3D> path = new List<Hex3D>();
HashSet<Hex3D> HexFrontier = new HashSet<Hex3D>();
HashSet<Hex3D> ExploredHexes = new HashSet<Hex3D>();
HashSet<Hex3D> OldHexFrontier = new HashSet<Hex3D>();
ExploredHexes.Add(d);
OldHexFrontier.Add(d);
d.distance = 0;
while (true) {
foreach (Hex3D h1 in OldHexFrontier) {
//Console.WriteLine("h1 is " + h1.ToString());
//Console.WriteLine("h1 dist is " + h1.distance);
foreach(Hex3D h2 in h1.getNeighbors()) {
//Console.WriteLine("h2 is " + h2.ToString());
//Console.WriteLine("h2 dist is " + h2.distance);
if (h2 == s)
{
Console.WriteLine("Victory!\n"+h2.distance + " vs. " + h1.distance);
}
if ((h2.distance < 0 || h2.distance > h1.distance + 1) && (h2.GetGameObject() == null || s == h2)) {
h2.distance = h1.distance+1;
h2.prev = h1;
HexFrontier.Add(h2);
ExploredHexes.Add(h2);
}
}
}
if (HexFrontier.Contains(s))
break;
if (HexFrontier.Count == 0) {
foreach (Hex3D h in ExploredHexes)
{
h.distance = -1;
h.prev = null;
}
return null;
}
OldHexFrontier = HexFrontier;
HexFrontier = new HashSet<Hex3D>();
}
path.Add(s);
Hex3D cursor = s;
while (cursor != d) {
cursor = cursor.prev;
path.Add(cursor);
}
if (d.GetGameObject() != null) {
path.Remove(d);
}
foreach (Hex3D h in ExploredHexes) {
h.distance = -1;
h.prev = null;
}
return path;
}