Clean up PathSearch
- Remove functionality for tracking cells considered during the search, as nothing relies on this. - Rename various parameters in the expand function to closer match naming of fields used in CellInfo, intended to improve clarity.
This commit is contained in:
committed by
Matthias Mailänder
parent
e1ade59a32
commit
8c627aa185
@@ -10,7 +10,6 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Mods.Common.Traits;
|
||||
using OpenRA.Primitives;
|
||||
@@ -24,15 +23,8 @@ namespace OpenRA.Mods.Common.Pathfinder
|
||||
/// </summary>
|
||||
IGraph<CellInfo> Graph { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Stores the analyzed nodes by the expand function
|
||||
/// </summary>
|
||||
IEnumerable<(CPos Cell, int Cost)> Considered { get; }
|
||||
|
||||
Player Owner { get; }
|
||||
|
||||
int MaxCost { get; }
|
||||
|
||||
IPathSearch Reverse();
|
||||
|
||||
IPathSearch WithCustomBlocker(Func<CPos, bool> customBlock);
|
||||
@@ -68,11 +60,7 @@ namespace OpenRA.Mods.Common.Pathfinder
|
||||
|
||||
protected IPriorityQueue<GraphConnection> OpenQueue { get; private set; }
|
||||
|
||||
public abstract IEnumerable<(CPos Cell, int Cost)> Considered { get; }
|
||||
|
||||
public Player Owner => Graph.Actor.Owner;
|
||||
public int MaxCost { get; protected set; }
|
||||
public bool Debug { get; set; }
|
||||
protected Func<CPos, int> heuristic;
|
||||
protected Func<CPos, bool> isGoal;
|
||||
protected int heuristicWeightPercentage;
|
||||
@@ -91,7 +79,6 @@ namespace OpenRA.Mods.Common.Pathfinder
|
||||
Graph = graph;
|
||||
OpenQueue = new PriorityQueue<GraphConnection>(GraphConnection.ConnectionCostComparer);
|
||||
StartPoints = new PriorityQueue<GraphConnection>(GraphConnection.ConnectionCostComparer);
|
||||
MaxCost = 0;
|
||||
heuristicWeightPercentage = 100;
|
||||
|
||||
// Determine the minimum possible cost for moving horizontally between cells based on terrain speeds.
|
||||
|
||||
@@ -29,16 +29,9 @@ namespace OpenRA.Mods.Common.Pathfinder
|
||||
return LayerPoolTable.GetValue(world, CreateLayerPool);
|
||||
}
|
||||
|
||||
public override IEnumerable<(CPos, int)> Considered => considered;
|
||||
|
||||
LinkedList<(CPos, int)> considered;
|
||||
|
||||
#region Constructors
|
||||
|
||||
private PathSearch(IGraph<CellInfo> graph)
|
||||
: base(graph)
|
||||
{
|
||||
considered = new LinkedList<(CPos, int)>();
|
||||
}
|
||||
|
||||
public static IPathSearch Search(World world, Locomotor locomotor, Actor self, BlockedByActor check, Func<CPos, bool> goalCondition)
|
||||
@@ -90,11 +83,8 @@ namespace OpenRA.Mods.Common.Pathfinder
|
||||
var connection = new GraphConnection(location, cost);
|
||||
OpenQueue.Add(connection);
|
||||
StartPoints.Add(connection);
|
||||
considered.AddLast((location, 0));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// This function analyzes the neighbors of the most promising node in the Pathfinding graph
|
||||
/// using the A* algorithm (A-star) and returns that node
|
||||
@@ -104,8 +94,8 @@ namespace OpenRA.Mods.Common.Pathfinder
|
||||
{
|
||||
var currentMinNode = OpenQueue.Pop().Destination;
|
||||
|
||||
var currentCell = Graph[currentMinNode];
|
||||
Graph[currentMinNode] = new CellInfo(CellStatus.Closed, currentCell.CostSoFar, currentCell.EstimatedTotalCost, currentCell.PreviousNode);
|
||||
var currentInfo = Graph[currentMinNode];
|
||||
Graph[currentMinNode] = new CellInfo(CellStatus.Closed, currentInfo.CostSoFar, currentInfo.EstimatedTotalCost, currentInfo.PreviousNode);
|
||||
|
||||
if (Graph.CustomCost != null && Graph.CustomCost(currentMinNode) == PathGraph.PathCostForInvalidPath)
|
||||
return currentMinNode;
|
||||
@@ -113,38 +103,30 @@ namespace OpenRA.Mods.Common.Pathfinder
|
||||
foreach (var connection in Graph.GetConnections(currentMinNode))
|
||||
{
|
||||
// Calculate the cost up to that point
|
||||
var gCost = currentCell.CostSoFar + connection.Cost;
|
||||
var costSoFarToNeighbor = currentInfo.CostSoFar + connection.Cost;
|
||||
|
||||
var neighborCPos = connection.Destination;
|
||||
var neighborCell = Graph[neighborCPos];
|
||||
var neighbor = connection.Destination;
|
||||
var neighborInfo = Graph[neighbor];
|
||||
|
||||
// Cost is even higher; next direction:
|
||||
if (neighborCell.Status == CellStatus.Closed ||
|
||||
(neighborCell.Status == CellStatus.Open && gCost >= neighborCell.CostSoFar))
|
||||
if (neighborInfo.Status == CellStatus.Closed ||
|
||||
(neighborInfo.Status == CellStatus.Open && costSoFarToNeighbor >= neighborInfo.CostSoFar))
|
||||
continue;
|
||||
|
||||
// Now we may seriously consider this direction using heuristics. If the cell has
|
||||
// already been processed, we can reuse the result (just the difference between the
|
||||
// estimated total and the cost so far
|
||||
int hCost;
|
||||
if (neighborCell.Status == CellStatus.Open)
|
||||
hCost = neighborCell.EstimatedTotalCost - neighborCell.CostSoFar;
|
||||
// estimated total and the cost so far)
|
||||
int estimatedRemainingCostToTarget;
|
||||
if (neighborInfo.Status == CellStatus.Open)
|
||||
estimatedRemainingCostToTarget = neighborInfo.EstimatedTotalCost - neighborInfo.CostSoFar;
|
||||
else
|
||||
hCost = heuristic(neighborCPos);
|
||||
estimatedRemainingCostToTarget = heuristic(neighbor);
|
||||
|
||||
var estimatedCost = gCost + hCost;
|
||||
Graph[neighborCPos] = new CellInfo(CellStatus.Open, gCost, estimatedCost, currentMinNode);
|
||||
var estimatedTotalCostToTarget = costSoFarToNeighbor + estimatedRemainingCostToTarget;
|
||||
Graph[neighbor] = new CellInfo(CellStatus.Open, costSoFarToNeighbor, estimatedTotalCostToTarget, currentMinNode);
|
||||
|
||||
if (neighborCell.Status != CellStatus.Open)
|
||||
OpenQueue.Add(new GraphConnection(neighborCPos, estimatedCost));
|
||||
|
||||
if (Debug)
|
||||
{
|
||||
if (gCost > MaxCost)
|
||||
MaxCost = gCost;
|
||||
|
||||
considered.AddLast((neighborCPos, gCost));
|
||||
}
|
||||
if (neighborInfo.Status != CellStatus.Open)
|
||||
OpenQueue.Add(new GraphConnection(neighbor, estimatedTotalCostToTarget));
|
||||
}
|
||||
|
||||
return currentMinNode;
|
||||
|
||||
Reference in New Issue
Block a user