Use named pathfinding constants.

- Rename CostForInvalidCell to PathCostForInvalidPath
- Add MovementCostForUnreachableCell
- Update usages of int.MaxValue and short.Maxvalue to use named constants where relevant.
- Update costs on ICustomMovementLayer to return short, for consistency with costs from Locomotor.
- Rename some methods to distinguish between path/movement cost.
This commit is contained in:
RoosterDragon
2021-10-08 20:35:33 +01:00
committed by abcdefg30
parent 884e6cdb51
commit df9398a871
16 changed files with 57 additions and 54 deletions

View File

@@ -75,7 +75,8 @@ namespace OpenRA.Mods.Common.Pathfinder
sealed class PathGraph : IGraph<CellInfo>
{
public const int CostForInvalidCell = int.MaxValue;
public const int PathCostForInvalidPath = int.MaxValue;
public const short MovementCostForUnreachableCell = short.MaxValue;
public Actor Actor { get; private set; }
public World World { get; private set; }
@@ -147,11 +148,11 @@ namespace OpenRA.Mods.Common.Pathfinder
{
var dir = directions[i];
var neighbor = position + dir;
var movementCost = GetCostToNode(neighbor, dir);
var pathCost = GetPathCostToNode(neighbor, dir);
// PERF: Skip closed cells already, 15% of all cells
if (movementCost != CostForInvalidCell && info[neighbor].Status != CellStatus.Closed)
validNeighbors.Add(new GraphConnection(neighbor, movementCost));
if (pathCost != PathCostForInvalidPath && info[neighbor].Status != CellStatus.Closed)
validNeighbors.Add(new GraphConnection(neighbor, pathCost));
}
if (posLayer == 0)
@@ -160,7 +161,7 @@ namespace OpenRA.Mods.Common.Pathfinder
{
var layerPosition = new CPos(position.X, position.Y, cli.Layer.Index);
var entryCost = cli.Layer.EntryMovementCost(locomotor.Info, layerPosition);
if (entryCost != CostForInvalidCell)
if (entryCost != MovementCostForUnreachableCell)
validNeighbors.Add(new GraphConnection(layerPosition, entryCost));
}
}
@@ -168,23 +169,23 @@ namespace OpenRA.Mods.Common.Pathfinder
{
var layerPosition = new CPos(position.X, position.Y, 0);
var exitCost = customLayerInfo[posLayer].Layer.ExitMovementCost(locomotor.Info, layerPosition);
if (exitCost != CostForInvalidCell)
if (exitCost != MovementCostForUnreachableCell)
validNeighbors.Add(new GraphConnection(layerPosition, exitCost));
}
return validNeighbors;
}
int GetCostToNode(CPos destNode, CVec direction)
int GetPathCostToNode(CPos destNode, CVec direction)
{
var movementCost = locomotor.MovementCostToEnterCell(Actor, destNode, checkConditions, IgnoreActor);
if (movementCost != short.MaxValue && !(CustomBlock != null && CustomBlock(destNode)))
return CalculateCellCost(destNode, direction, movementCost);
if (movementCost != MovementCostForUnreachableCell && !(CustomBlock != null && CustomBlock(destNode)))
return CalculateCellPathCost(destNode, direction, movementCost);
return CostForInvalidCell;
return PathCostForInvalidPath;
}
int CalculateCellCost(CPos neighborCPos, CVec direction, int movementCost)
int CalculateCellPathCost(CPos neighborCPos, CVec direction, int movementCost)
{
var cellCost = movementCost;
@@ -194,8 +195,8 @@ namespace OpenRA.Mods.Common.Pathfinder
if (CustomCost != null)
{
var customCost = CustomCost(neighborCPos);
if (customCost == CostForInvalidCell)
return CostForInvalidCell;
if (customCost == PathCostForInvalidPath)
return PathCostForInvalidPath;
cellCost += customCost;
}
@@ -206,7 +207,7 @@ namespace OpenRA.Mods.Common.Pathfinder
var heightLayer = World.Map.Height;
var from = neighborCPos - direction;
if (Math.Abs(heightLayer[neighborCPos] - heightLayer[from]) > 1)
return CostForInvalidCell;
return PathCostForInvalidPath;
}
// Directional bonuses for smoother flow!