Fix neighbor-nodes creating endless loop

This commit is contained in:
glax 2024-07-23 02:34:40 +02:00
parent f21845ced4
commit 4261bc2f48

View File

@ -47,8 +47,8 @@ namespace astar
if (neighborNode.PreviousIsFromStart is false)//Check if we found the opposite End if (neighborNode.PreviousIsFromStart is false)//Check if we found the opposite End
return PathFound(graph, currentNodeStart, neighborNode, logger); return PathFound(graph, currentNodeStart, neighborNode, logger);
float distance = (float)currentNodeStart.Distance! + (float)currentNodeStart.DistanceTo(neighborNode); float distance = (currentNodeStart.Distance??float.MaxValue) + (float)currentNodeStart.DistanceTo(neighborNode);
if (neighborNode.PreviousNodeId is null || neighborNode.Distance > distance) if (neighborNode.PreviousNodeId is null || neighborNode.Distance > distance && currentNodeStart.PreviousNodeId != neighborId)
{ {
neighborNode.PreviousNodeId = currentNodeStartId; neighborNode.PreviousNodeId = currentNodeStartId;
neighborNode.Distance = distance; neighborNode.Distance = distance;
@ -69,8 +69,8 @@ namespace astar
if (neighborNode.PreviousIsFromStart is true)//Check if we found the opposite End if (neighborNode.PreviousIsFromStart is true)//Check if we found the opposite End
return PathFound(graph, neighborNode, currentNodeEnd, logger); return PathFound(graph, neighborNode, currentNodeEnd, logger);
float distance = (float)currentNodeStart.Distance! + (float)currentNodeStart.DistanceTo(neighborNode); float distance = (currentNodeStart.Distance??float.MaxValue) + (float)currentNodeStart.DistanceTo(neighborNode);
if (neighborNode.PreviousNodeId is null || neighborNode.Distance > distance) if (neighborNode.PreviousIsFromStart is null || neighborNode.Distance > distance && currentNodeEnd.PreviousNodeId != neighborId)
{ {
neighborNode.PreviousNodeId = currentNodeEndId; neighborNode.PreviousNodeId = currentNodeEndId;
neighborNode.Distance = distance; neighborNode.Distance = distance;