Add a helper for multiplying by sqrt(2)
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -72,7 +72,8 @@ namespace OpenRA.Mods.Common.Pathfinder
|
||||
// a deterministic set of calculations
|
||||
protected readonly IPriorityQueue<GraphConnection> StartPoints;
|
||||
|
||||
private readonly int cellCost, diagonalCellCost;
|
||||
readonly short cellCost;
|
||||
readonly int diagonalCellCost;
|
||||
|
||||
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.
|
||||
// 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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user