Fixed instant depletion of toVisit()

This commit is contained in:
C9Glax 2023-02-08 18:09:06 +01:00
parent 9444e2ac8e
commit d7b084659a

View File

@ -10,7 +10,6 @@ public class Pathfinder
{ {
RegionManager regionManager = new RegionManager(workingDir); RegionManager regionManager = new RegionManager(workingDir);
Region startRegion, goalRegion; Region startRegion, goalRegion;
OsmNode? startNode, goalNode;
try try
{ {
startRegion = regionManager.GetRegion(start); startRegion = regionManager.GetRegion(start);
@ -21,18 +20,18 @@ public class Pathfinder
throw new Exception(string.Format("No region at coordinates {0}", e.FileName), e); throw new Exception(string.Format("No region at coordinates {0}", e.FileName), e);
} }
startNode = ClosestNodeToCoordinates(start, startRegion); OsmNode? startNode = ClosestNodeToCoordinates(start, startRegion);
goalNode = ClosestNodeToCoordinates(goal, goalRegion); OsmNode? goalNode = ClosestNodeToCoordinates(goal, goalRegion);
if (startNode == null || goalNode == null) if (startNode == null || goalNode == null)
return new List<OsmNode>(); return new List<OsmNode>();
List<OsmNode> toVisit = new() { startNode }; List<OsmNode> toVisit = new() { startNode };
OsmNode closestNodeToGoal; OsmNode closestNodeToGoal = toVisit.First();
closestNodeToGoal.currentPathWeight = 0;
bool stop = false; bool stop = false;
while (toVisit.Count > 0 && !stop) while (toVisit.Count > 0 && !stop)
{ {
Console.WriteLine("toVisit length: {0}", toVisit.Count.ToString());
closestNodeToGoal = toVisit.First(); closestNodeToGoal = toVisit.First();
foreach (OsmNode node in toVisit) foreach (OsmNode node in toVisit)
{ {
@ -46,11 +45,10 @@ public class Pathfinder
} }
} }
foreach (OsmEdge connection in closestNodeToGoal.edges) foreach (OsmEdge edge in closestNodeToGoal.edges)
{ {
OsmNode? neighbor = regionManager.GetNode(connection.neighborCoordinates); OsmNode? neighbor = regionManager.GetNode(edge.neighborCoordinates);
Console.WriteLine(neighbor); if (neighbor != null && neighbor.currentPathWeight > closestNodeToGoal.currentPathWeight + Utils.DistanceBetween(closestNodeToGoal, neighbor))
if (neighbor != null && neighbor.currentPathWeight < closestNodeToGoal.currentPathWeight + Utils.DistanceBetween(closestNodeToGoal, neighbor))
{ {
neighbor.previousPathNode = closestNodeToGoal; neighbor.previousPathNode = closestNodeToGoal;
neighbor.currentPathWeight = closestNodeToGoal.currentPathWeight + neighbor.currentPathWeight = closestNodeToGoal.currentPathWeight +