From 8c627aa1854a9bedeb00e9c63693ee0847ff5a22 Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Thu, 18 Nov 2021 15:42:48 +0000 Subject: [PATCH] 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. --- .../Pathfinder/BasePathSearch.cs | 13 ----- OpenRA.Mods.Common/Pathfinder/PathSearch.cs | 50 ++++++------------- 2 files changed, 16 insertions(+), 47 deletions(-) diff --git a/OpenRA.Mods.Common/Pathfinder/BasePathSearch.cs b/OpenRA.Mods.Common/Pathfinder/BasePathSearch.cs index 4c91af4b3b..c08e96adda 100644 --- a/OpenRA.Mods.Common/Pathfinder/BasePathSearch.cs +++ b/OpenRA.Mods.Common/Pathfinder/BasePathSearch.cs @@ -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 /// IGraph Graph { get; } - /// - /// Stores the analyzed nodes by the expand function - /// - IEnumerable<(CPos Cell, int Cost)> Considered { get; } - Player Owner { get; } - int MaxCost { get; } - IPathSearch Reverse(); IPathSearch WithCustomBlocker(Func customBlock); @@ -68,11 +60,7 @@ namespace OpenRA.Mods.Common.Pathfinder protected IPriorityQueue 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 heuristic; protected Func isGoal; protected int heuristicWeightPercentage; @@ -91,7 +79,6 @@ namespace OpenRA.Mods.Common.Pathfinder Graph = graph; OpenQueue = new PriorityQueue(GraphConnection.ConnectionCostComparer); StartPoints = new PriorityQueue(GraphConnection.ConnectionCostComparer); - MaxCost = 0; heuristicWeightPercentage = 100; // Determine the minimum possible cost for moving horizontally between cells based on terrain speeds. diff --git a/OpenRA.Mods.Common/Pathfinder/PathSearch.cs b/OpenRA.Mods.Common/Pathfinder/PathSearch.cs index 15c6c9f557..70f314894b 100644 --- a/OpenRA.Mods.Common/Pathfinder/PathSearch.cs +++ b/OpenRA.Mods.Common/Pathfinder/PathSearch.cs @@ -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 graph) : base(graph) { - considered = new LinkedList<(CPos, int)>(); } public static IPathSearch Search(World world, Locomotor locomotor, Actor self, BlockedByActor check, Func 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 - /// /// 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;