diff --git a/Source/Plugins/BuilderModes/ClassicModes/LinedefsMode.cs b/Source/Plugins/BuilderModes/ClassicModes/LinedefsMode.cs index e7f1bb54..7520e7c3 100755 --- a/Source/Plugins/BuilderModes/ClassicModes/LinedefsMode.cs +++ b/Source/Plugins/BuilderModes/ClassicModes/LinedefsMode.cs @@ -362,7 +362,11 @@ namespace CodeImp.DoomBuilder.BuilderModes if(curdistance < closest2) closest2 = curdistance; // Return closer one - return (int)(closest1 - closest2); + // biwa: the difference between closest1 and closest2 can exceed the capacity of int, and that + // sometimes seem to cause problems, resulting in the sorting to throw an ArgumentException + // because of inconsistent results. Making sure to only return -1, 0, or 1 seems to fix the issue + // See https://github.com/UltimateDoomBuilder/UltimateDoomBuilder/issues/1053 + return (closest1 - closest2) < 0 ? -1 : ((closest1 - closest2) > 0 ? 1 : 0); }); return result; diff --git a/Source/Plugins/BuilderModes/ClassicModes/SectorsMode.cs b/Source/Plugins/BuilderModes/ClassicModes/SectorsMode.cs index e4c78d58..e936f517 100755 --- a/Source/Plugins/BuilderModes/ClassicModes/SectorsMode.cs +++ b/Source/Plugins/BuilderModes/ClassicModes/SectorsMode.cs @@ -722,7 +722,11 @@ namespace CodeImp.DoomBuilder.BuilderModes } // Return closer one - return (int)(closest1 - closest2); + // biwa: the difference between closest1 and closest2 can exceed the capacity of int, and that + // sometimes seem to cause problems, resulting in the sorting to throw an ArgumentException + // because of inconsistent results. Making sure to only return -1, 0, or 1 seems to fix the issue + // See https://github.com/UltimateDoomBuilder/UltimateDoomBuilder/issues/1053 + return (closest1 - closest2) < 0 ? -1 : ((closest1 - closest2) > 0 ? 1 : 0); }); return result; diff --git a/Source/Plugins/BuilderModes/ClassicModes/ThingsMode.cs b/Source/Plugins/BuilderModes/ClassicModes/ThingsMode.cs index 247ec261..c6d39c6d 100755 --- a/Source/Plugins/BuilderModes/ClassicModes/ThingsMode.cs +++ b/Source/Plugins/BuilderModes/ClassicModes/ThingsMode.cs @@ -1011,7 +1011,11 @@ namespace CodeImp.DoomBuilder.BuilderModes double closest2 = Vector2D.DistanceSq(t2.Position, targetpoint); // Return closer one - return (int)(closest1 - closest2); + // biwa: the difference between closest1 and closest2 can exceed the capacity of int, and that + // sometimes seem to cause problems, resulting in the sorting to throw an ArgumentException + // because of inconsistent results. Making sure to only return -1, 0, or 1 seems to fix the issue + // See https://github.com/UltimateDoomBuilder/UltimateDoomBuilder/issues/1053 + return (closest1 - closest2) < 0 ? -1 : ((closest1 - closest2) > 0 ? 1 : 0); }); return result;