Allow the default value of a CellInfo to be an Unvisited location.
In CellInfoLayerPool, instead of having to store a layer with the default values, we know we can just clear the pooled layer in order to reset it. This saves on memory, and also makes resetting marginally faster. In PathSearch, we need to amend a check to ensure a cell info is not Unvisited before we check on its cost.
This commit is contained in:
@@ -9,13 +9,15 @@
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
|
||||
namespace OpenRA.Mods.Common.Pathfinder
|
||||
{
|
||||
/// <summary>
|
||||
/// Describes the three states that a node in the graph can have.
|
||||
/// Based on A* algorithm specification
|
||||
/// </summary>
|
||||
public enum CellStatus
|
||||
public enum CellStatus : byte
|
||||
{
|
||||
Unvisited,
|
||||
Open,
|
||||
@@ -23,36 +25,52 @@ namespace OpenRA.Mods.Common.Pathfinder
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stores information about nodes in the pathfinding graph
|
||||
/// Stores information about nodes in the pathfinding graph.
|
||||
/// The default value of this struct represents an <see cref="CellStatus.Unvisited"/> location.
|
||||
/// </summary>
|
||||
public readonly struct CellInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// The cost to move from the start up to this node
|
||||
/// The cost to move from the start up to this node.
|
||||
/// </summary>
|
||||
public readonly int CostSoFar;
|
||||
|
||||
/// <summary>
|
||||
/// The estimation of how far is the node from our goal
|
||||
/// The estimation of how far this node is from our target.
|
||||
/// </summary>
|
||||
public readonly int EstimatedTotal;
|
||||
|
||||
/// <summary>
|
||||
/// The previous node of this one that follows the shortest path
|
||||
/// The previous node of this one that follows the shortest path.
|
||||
/// </summary>
|
||||
public readonly CPos PreviousPos;
|
||||
|
||||
/// <summary>
|
||||
/// The status of this node
|
||||
/// The status of this node. Accessing other fields is only valid when the status is not <see cref="CellStatus.Unvisited"/>.
|
||||
/// </summary>
|
||||
public readonly CellStatus Status;
|
||||
|
||||
public CellInfo(int costSoFar, int estimatedTotal, CPos previousPos, CellStatus status)
|
||||
{
|
||||
CostSoFar = costSoFar;
|
||||
PreviousPos = previousPos;
|
||||
if (status == CellStatus.Unvisited)
|
||||
throw new ArgumentException(
|
||||
$"The default {nameof(CellInfo)} is the only such {nameof(CellInfo)} allowed for representing an {nameof(CellStatus.Unvisited)} location.",
|
||||
nameof(status));
|
||||
|
||||
Status = status;
|
||||
CostSoFar = costSoFar;
|
||||
EstimatedTotal = estimatedTotal;
|
||||
PreviousPos = previousPos;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
if (Status == CellStatus.Unvisited)
|
||||
return Status.ToString();
|
||||
|
||||
return
|
||||
$"{Status} {nameof(CostSoFar)}={CostSoFar} " +
|
||||
$"{nameof(EstimatedTotal)}={EstimatedTotal} {nameof(PreviousPos)}={PreviousPos}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using OpenRA.Primitives;
|
||||
|
||||
namespace OpenRA.Mods.Common.Pathfinder
|
||||
{
|
||||
@@ -19,15 +18,11 @@ namespace OpenRA.Mods.Common.Pathfinder
|
||||
{
|
||||
const int MaxPoolSize = 4;
|
||||
readonly Stack<CellLayer<CellInfo>> pool = new Stack<CellLayer<CellInfo>>(MaxPoolSize);
|
||||
readonly CellLayer<CellInfo> defaultLayer;
|
||||
readonly Map map;
|
||||
|
||||
public CellInfoLayerPool(Map map)
|
||||
{
|
||||
defaultLayer =
|
||||
CellLayer<CellInfo>.CreateInstance(
|
||||
mpos => new CellInfo(PathGraph.PathCostForInvalidPath, PathGraph.PathCostForInvalidPath, mpos.ToCPos(map), CellStatus.Unvisited),
|
||||
new Size(map.MapSize.X, map.MapSize.Y),
|
||||
map.Grid.Type);
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
public PooledCellInfoLayer Get()
|
||||
@@ -42,9 +37,14 @@ namespace OpenRA.Mods.Common.Pathfinder
|
||||
if (pool.Count > 0)
|
||||
layer = pool.Pop();
|
||||
|
||||
// As the default value of CellInfo represents an Unvisited location,
|
||||
// we don't need to initialize the values in the layer,
|
||||
// we can just clear them to the defaults.
|
||||
if (layer == null)
|
||||
layer = new CellLayer<CellInfo>(defaultLayer.GridType, defaultLayer.Size);
|
||||
layer.CopyValuesFrom(defaultLayer);
|
||||
layer = new CellLayer<CellInfo>(map);
|
||||
else
|
||||
layer.Clear();
|
||||
|
||||
return layer;
|
||||
}
|
||||
|
||||
|
||||
@@ -119,7 +119,8 @@ namespace OpenRA.Mods.Common.Pathfinder
|
||||
var neighborCell = Graph[neighborCPos];
|
||||
|
||||
// Cost is even higher; next direction:
|
||||
if (neighborCell.Status == CellStatus.Closed || gCost >= neighborCell.CostSoFar)
|
||||
if (neighborCell.Status == CellStatus.Closed ||
|
||||
(neighborCell.Status == CellStatus.Open && gCost >= neighborCell.CostSoFar))
|
||||
continue;
|
||||
|
||||
// Now we may seriously consider this direction using heuristics. If the cell has
|
||||
|
||||
Reference in New Issue
Block a user