Add a helper for multiplying by sqrt(2)

This commit is contained in:
RoosterDragon
2021-11-14 09:31:34 +00:00
committed by abcdefg30
parent f0e24f6d21
commit a59f4b2c4a
3 changed files with 12 additions and 7 deletions

View File

@@ -357,6 +357,11 @@ namespace OpenRA
return root; return root;
} }
public static int MultiplyBySqrtTwo(short number)
{
return number * 46341 / 32768;
}
public static int IntegerDivisionRoundingAwayFromZero(int dividend, int divisor) public static int IntegerDivisionRoundingAwayFromZero(int dividend, int divisor)
{ {
var quotient = Math.DivRem(dividend, divisor, out var remainder); var quotient = Math.DivRem(dividend, divisor, out var remainder);

View File

@@ -72,7 +72,8 @@ namespace OpenRA.Mods.Common.Pathfinder
// a deterministic set of calculations // a deterministic set of calculations
protected readonly IPriorityQueue<GraphConnection> StartPoints; protected readonly IPriorityQueue<GraphConnection> StartPoints;
private readonly int cellCost, diagonalCellCost; readonly short cellCost;
readonly int diagonalCellCost;
protected BasePathSearch(IGraph<CellInfo> graph) protected BasePathSearch(IGraph<CellInfo> graph)
{ {
@@ -84,7 +85,7 @@ namespace OpenRA.Mods.Common.Pathfinder
// Determine the minimum possible cost for moving horizontally between cells based on terrain speeds. // 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. // 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); cellCost = ((Mobile)graph.Actor.OccupiesSpace).Info.LocomotorInfo.TerrainSpeeds.Values.Min(ti => ti.Cost);
diagonalCellCost = cellCost * 141421 / 100000; diagonalCellCost = Exts.MultiplyBySqrtTwo(cellCost);
} }
/// <summary> /// <summary>

View File

@@ -226,12 +226,11 @@ namespace OpenRA.Mods.Common.Pathfinder
return PathCostForInvalidPath; return PathCostForInvalidPath;
} }
int CalculateCellPathCost(CPos neighborCPos, CVec direction, int movementCost) int CalculateCellPathCost(CPos neighborCPos, CVec direction, short movementCost)
{ {
var cellCost = movementCost; var cellCost = direction.X * direction.Y != 0
? Exts.MultiplyBySqrtTwo(movementCost)
if (direction.X * direction.Y != 0) : movementCost;
cellCost = (cellCost * 34) / 24;
if (CustomCost != null) if (CustomCost != null)
{ {