From 4261bc2f4840cd98ea4e50cb46ebeb01abcb9e71 Mon Sep 17 00:00:00 2001 From: glax Date: Tue, 23 Jul 2024 02:34:40 +0200 Subject: [PATCH] Fix neighbor-nodes creating endless loop --- astar/Astar.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/astar/Astar.cs b/astar/Astar.cs index d611e07..ceb65a6 100644 --- a/astar/Astar.cs +++ b/astar/Astar.cs @@ -47,8 +47,8 @@ namespace astar if (neighborNode.PreviousIsFromStart is false)//Check if we found the opposite End return PathFound(graph, currentNodeStart, neighborNode, logger); - float distance = (float)currentNodeStart.Distance! + (float)currentNodeStart.DistanceTo(neighborNode); - if (neighborNode.PreviousNodeId is null || neighborNode.Distance > distance) + float distance = (currentNodeStart.Distance??float.MaxValue) + (float)currentNodeStart.DistanceTo(neighborNode); + if (neighborNode.PreviousNodeId is null || neighborNode.Distance > distance && currentNodeStart.PreviousNodeId != neighborId) { neighborNode.PreviousNodeId = currentNodeStartId; neighborNode.Distance = distance; @@ -69,8 +69,8 @@ namespace astar if (neighborNode.PreviousIsFromStart is true)//Check if we found the opposite End return PathFound(graph, neighborNode, currentNodeEnd, logger); - float distance = (float)currentNodeStart.Distance! + (float)currentNodeStart.DistanceTo(neighborNode); - if (neighborNode.PreviousNodeId is null || neighborNode.Distance > distance) + float distance = (currentNodeStart.Distance??float.MaxValue) + (float)currentNodeStart.DistanceTo(neighborNode); + if (neighborNode.PreviousIsFromStart is null || neighborNode.Distance > distance && currentNodeEnd.PreviousNodeId != neighborId) { neighborNode.PreviousNodeId = currentNodeEndId; neighborNode.Distance = distance;