Fix pathfinding using PriorityQueue incorrectly.

By providing a comparer that could change over time (as estimated costs on the graph were updated), this meant the priority queue could have its heap property invalidated and thus not maintain a correct ordering. Instead we store elements into the queue with their estimations at the time. This preserves the heap property and thus ensures the queue returns properly ordered results, although it may contain out of date estimations.

This also improves performance. The fixed comparer need not perform expensive lookups into the graph, but can instead use the readily available value. This speeds up adds and removes on the queue significantly.
This commit is contained in:
RoosterDragon
2015-08-20 23:36:44 +01:00
parent 77923a27c1
commit ac55c5bf09
4 changed files with 25 additions and 28 deletions

View File

@@ -47,6 +47,18 @@ namespace OpenRA.Mods.Common.Pathfinder
public struct GraphConnection
{
public static readonly CostComparer ConnectionCostComparer = CostComparer.Instance;
public sealed class CostComparer : IComparer<GraphConnection>
{
public static readonly CostComparer Instance = new CostComparer();
CostComparer() { }
public int Compare(GraphConnection x, GraphConnection y)
{
return x.Cost.CompareTo(y.Cost);
}
}
public readonly CPos Destination;
public readonly int Cost;