From a59f4b2c4a40e1e9c2faf75a98fedf67c95265c7 Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Sun, 14 Nov 2021 09:31:34 +0000 Subject: [PATCH] Add a helper for multiplying by sqrt(2) --- OpenRA.Game/Exts.cs | 5 +++++ OpenRA.Mods.Common/Pathfinder/BasePathSearch.cs | 5 +++-- OpenRA.Mods.Common/Pathfinder/PathGraph.cs | 9 ++++----- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/OpenRA.Game/Exts.cs b/OpenRA.Game/Exts.cs index 6d3fd1681e..576c909ac4 100644 --- a/OpenRA.Game/Exts.cs +++ b/OpenRA.Game/Exts.cs @@ -357,6 +357,11 @@ namespace OpenRA return root; } + public static int MultiplyBySqrtTwo(short number) + { + return number * 46341 / 32768; + } + public static int IntegerDivisionRoundingAwayFromZero(int dividend, int divisor) { var quotient = Math.DivRem(dividend, divisor, out var remainder); diff --git a/OpenRA.Mods.Common/Pathfinder/BasePathSearch.cs b/OpenRA.Mods.Common/Pathfinder/BasePathSearch.cs index c08e96adda..ce3426b371 100644 --- a/OpenRA.Mods.Common/Pathfinder/BasePathSearch.cs +++ b/OpenRA.Mods.Common/Pathfinder/BasePathSearch.cs @@ -72,7 +72,8 @@ namespace OpenRA.Mods.Common.Pathfinder // a deterministic set of calculations protected readonly IPriorityQueue StartPoints; - private readonly int cellCost, diagonalCellCost; + readonly short cellCost; + readonly int diagonalCellCost; protected BasePathSearch(IGraph graph) { @@ -84,7 +85,7 @@ namespace OpenRA.Mods.Common.Pathfinder // Determine the minimum possible cost for moving horizontally between cells based on terrain speeds. // The minimum possible cost diagonally is then Sqrt(2) times more costly. cellCost = ((Mobile)graph.Actor.OccupiesSpace).Info.LocomotorInfo.TerrainSpeeds.Values.Min(ti => ti.Cost); - diagonalCellCost = cellCost * 141421 / 100000; + diagonalCellCost = Exts.MultiplyBySqrtTwo(cellCost); } /// diff --git a/OpenRA.Mods.Common/Pathfinder/PathGraph.cs b/OpenRA.Mods.Common/Pathfinder/PathGraph.cs index a55f849cf9..d0ee88b82b 100644 --- a/OpenRA.Mods.Common/Pathfinder/PathGraph.cs +++ b/OpenRA.Mods.Common/Pathfinder/PathGraph.cs @@ -226,12 +226,11 @@ namespace OpenRA.Mods.Common.Pathfinder return PathCostForInvalidPath; } - int CalculateCellPathCost(CPos neighborCPos, CVec direction, int movementCost) + int CalculateCellPathCost(CPos neighborCPos, CVec direction, short movementCost) { - var cellCost = movementCost; - - if (direction.X * direction.Y != 0) - cellCost = (cellCost * 34) / 24; + var cellCost = direction.X * direction.Y != 0 + ? Exts.MultiplyBySqrtTwo(movementCost) + : movementCost; if (CustomCost != null) {