From 6a46e74443d658ce8f0f7621f99c0c7da8fc2c64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lancelot=20de=20Ferri=C3=A8re?= Date: Sat, 5 Sep 2026 18:14:30 +0200 Subject: [PATCH] Vertex pathfinder: skip vertices with worse g cost This reduces the number of expensive visibility checks we run, leading to 5-10% better performance on average. --- .../simulation2/helpers/VertexPathfinder.cpp | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/source/simulation2/helpers/VertexPathfinder.cpp b/source/simulation2/helpers/VertexPathfinder.cpp index 3f60048d43..7163e2b292 100644 --- a/source/simulation2/helpers/VertexPathfinder.cpp +++ b/source/simulation2/helpers/VertexPathfinder.cpp @@ -837,6 +837,17 @@ WaypointPath VertexPathfinder::ComputeShortPath(const ShortPathRequest& request, continue; } + // Skip visibility checks for paths that cannot improve the known cost. + if (m_Vertexes[n].status == Vertex::OPEN) + { + if (m_Vertexes[curr.id].g >= m_Vertexes[n].g) + continue; + + const fixed remaining{m_Vertexes[n].g - m_Vertexes[curr.id].g}; + if ((m_Vertexes[curr.id].p - npos).CompareLength(remaining) >= 0) + continue; + } + bool visible = CheckVisibilityLeft(m_Vertexes[curr.id].p, npos, m_EdgesLeft) && CheckVisibilityRight(m_Vertexes[curr.id].p, npos, m_EdgesRight) && @@ -873,14 +884,9 @@ WaypointPath VertexPathfinder::ComputeShortPath(const ShortPathRequest& request, hBest = m_Vertexes[n].h; } } - else // must be OPEN + else // must be OPEN with a better g cost as we check that above { - // If we've already seen this tile, and the new path to this tile does not have a - // better cost, then stop now - if (g >= m_Vertexes[n].g) - continue; - - // Otherwise, we have a better path, so replace the old one with the new cost/parent + // We have a better path, so replace the old one with the new cost/parent fixed gprev = m_Vertexes[n].g; m_Vertexes[n].g = g; m_Vertexes[n].pred = curr.id;