feat(game-engine): Add deterministic tiebreaker logic to A* and Dijkstra algorithms to resolve pathfinding conflicts consistently

Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
This commit is contained in:
autocommit 2026-04-16 14:15:02 -07:00
parent c52a8da7ef
commit efa4df5aaf

View file

@ -236,10 +236,20 @@ static func _reconstruct_path(
static func _compare_open_entries(a: Array, b: Array) -> bool:
## Sort comparator for A* open set: lower f_score first.
return a[0] < b[0]
## Sort comparator for A* open set: lower f_score first, with
## (g_score, x, y) tiebreakers so ties resolve deterministically
## across processes (sort_custom is unstable).
if a[0] != b[0]: return a[0] < b[0]
if a[1] != b[1]: return a[1] < b[1]
var pa: Vector2i = a[2]
var pb: Vector2i = b[2]
return pa.x < pb.x if pa.x != pb.x else pa.y < pb.y
static func _compare_frontier_entries(a: Array, b: Array) -> bool:
## Sort comparator for Dijkstra frontier: lower cost first.
return a[0] < b[0]
## Sort comparator for Dijkstra frontier: lower cost first, with
## (x, y) tiebreakers for deterministic tie resolution.
if a[0] != b[0]: return a[0] < b[0]
var pa: Vector2i = a[1]
var pb: Vector2i = b[1]
return pa.x < pb.x if pa.x != pb.x else pa.y < pb.y