From f45d063f1e9385aafd05979766e06e78df691efa Mon Sep 17 00:00:00 2001 From: Pavlos Touboulidis Date: Thu, 10 Jul 2014 11:35:02 +0300 Subject: [PATCH] Make tiles sorting deterministic in all cases This may fix issue #5916. In any case, it's wanted because this kind of sort is "unstable". According to the docs: "This implementation performs an unstable sort; that is, if two elements are equal, their order might not be preserved." --- OpenRA.Game/Map/Map.cs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/OpenRA.Game/Map/Map.cs b/OpenRA.Game/Map/Map.cs index 67f38c06d9..9ad6248792 100644 --- a/OpenRA.Game/Map/Map.cs +++ b/OpenRA.Game/Map/Map.cs @@ -716,7 +716,28 @@ namespace OpenRA // Sort each integer-distance group by the actual distance foreach (var list in ts) - list.Sort((a, b) => a.LengthSquared.CompareTo(b.LengthSquared)); + { + list.Sort((a, b) => + { + var result = a.LengthSquared.CompareTo(b.LengthSquared); + if (result != 0) + return result; + + // If the lengths are equal, use other means to sort them. + // Try the hashcode first because it gives more + // random-appearing results than X or Y that would always + // prefer the leftmost/topmost position. + result = a.GetHashCode().CompareTo(b.GetHashCode()); + if (result != 0) + return result; + + result = a.X.CompareTo(b.X); + if (result != 0) + return result; + + return a.Y.CompareTo(b.Y); + }); + } return ts.Select(list => list.ToArray()).ToArray(); }