From d7b084659ad71a4820e5c8d80377e2873e6a3c34 Mon Sep 17 00:00:00 2001 From: C9Glax <13404778+C9Glax@users.noreply.github.com> Date: Wed, 8 Feb 2023 18:09:06 +0100 Subject: [PATCH] Fixed instant depletion of toVisit() --- Pathfinding/Pathfinder.cs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/Pathfinding/Pathfinder.cs b/Pathfinding/Pathfinder.cs index 2458e52..8703bfb 100644 --- a/Pathfinding/Pathfinder.cs +++ b/Pathfinding/Pathfinder.cs @@ -10,7 +10,6 @@ public class Pathfinder { RegionManager regionManager = new RegionManager(workingDir); Region startRegion, goalRegion; - OsmNode? startNode, goalNode; try { startRegion = regionManager.GetRegion(start); @@ -21,18 +20,18 @@ public class Pathfinder throw new Exception(string.Format("No region at coordinates {0}", e.FileName), e); } - startNode = ClosestNodeToCoordinates(start, startRegion); - goalNode = ClosestNodeToCoordinates(goal, goalRegion); + OsmNode? startNode = ClosestNodeToCoordinates(start, startRegion); + OsmNode? goalNode = ClosestNodeToCoordinates(goal, goalRegion); if (startNode == null || goalNode == null) return new List(); List toVisit = new() { startNode }; - OsmNode closestNodeToGoal; + OsmNode closestNodeToGoal = toVisit.First(); + closestNodeToGoal.currentPathWeight = 0; bool stop = false; while (toVisit.Count > 0 && !stop) { - Console.WriteLine("toVisit length: {0}", toVisit.Count.ToString()); closestNodeToGoal = toVisit.First(); 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); - Console.WriteLine(neighbor); - if (neighbor != null && neighbor.currentPathWeight < closestNodeToGoal.currentPathWeight + Utils.DistanceBetween(closestNodeToGoal, neighbor)) + OsmNode? neighbor = regionManager.GetNode(edge.neighborCoordinates); + if (neighbor != null && neighbor.currentPathWeight > closestNodeToGoal.currentPathWeight + Utils.DistanceBetween(closestNodeToGoal, neighbor)) { neighbor.previousPathNode = closestNodeToGoal; neighbor.currentPathWeight = closestNodeToGoal.currentPathWeight +