Improved the performance and intelligence of resource harvesting by

refactoring the Harvesters' pathfinding. Now they in first place assess
which is the closest resource inside their search area and then a path is
calculated

Changed the way harvesters find resources by always trying to find the
closest resource to their refinery.

Changed the strategy of finding to find resources in Annulus.
This commit is contained in:
David Jiménez
2015-03-13 14:58:22 +01:00
committed by reaperrr
parent a0dc38c252
commit 787609d51e
6 changed files with 154 additions and 132 deletions

View File

@@ -110,6 +110,7 @@ namespace OpenRA.Mods.Common.Pathfinder
public bool Debug { get; set; }
string id;
protected Func<CPos, int> heuristic;
protected Func<CPos, bool> isGoal;
// This member is used to compute the ID of PathSearch.
// Essentially, it represents a collection of the initial
@@ -194,8 +195,7 @@ namespace OpenRA.Mods.Common.Pathfinder
public bool IsTarget(CPos location)
{
var locInfo = Graph[location];
return locInfo.EstimatedTotal - locInfo.CostSoFar == 0;
return isGoal(location);
}
public abstract CPos Expand();

View File

@@ -25,5 +25,7 @@ namespace OpenRA.Mods.Common.Pathfinder
/// a unit took to move one cell diagonally)
/// </summary>
public const int DiagonalCellCost = 177;
public const int InvalidNode = int.MaxValue;
}
}

View File

@@ -23,7 +23,7 @@ namespace OpenRA.Mods.Common.Pathfinder
/// <summary>
/// Gets all the Connections for a given node in the graph
/// </summary>
ICollection<GraphConnection> GetConnections(CPos position);
IEnumerable<GraphConnection> GetConnections(CPos position);
/// <summary>
/// Retrieves an object given a node in the graph
@@ -102,7 +102,7 @@ namespace OpenRA.Mods.Common.Pathfinder
new[] { new CVec(1, -1), new CVec(1, 0), new CVec(-1, 1), new CVec(0, 1), new CVec(1, 1) },
};
public ICollection<GraphConnection> GetConnections(CPos position)
public IEnumerable<GraphConnection> GetConnections(CPos position)
{
var previousPos = cellInfo[position].PreviousPos;
@@ -116,7 +116,7 @@ namespace OpenRA.Mods.Common.Pathfinder
{
var neighbor = position + directions[i];
var movementCost = GetCostToNode(neighbor, directions[i]);
if (movementCost != InvalidNode)
if (movementCost != Constants.InvalidNode)
validNeighbors.AddLast(new GraphConnection(position, neighbor, movementCost));
}
@@ -137,7 +137,7 @@ namespace OpenRA.Mods.Common.Pathfinder
return CalculateCellCost(destNode, direction, movementCost);
}
return InvalidNode;
return Constants.InvalidNode;
}
int CalculateCellCost(CPos neighborCPos, CVec direction, int movementCost)
@@ -148,7 +148,13 @@ namespace OpenRA.Mods.Common.Pathfinder
cellCost = (cellCost * 34) / 24;
if (CustomCost != null)
cellCost += CustomCost(neighborCPos);
{
var customCost = CustomCost(neighborCPos);
if (customCost == Constants.InvalidNode)
return Constants.InvalidNode;
cellCost += customCost;
}
// directional bonuses for smoother flow!
if (LaneBias != 0)
@@ -184,15 +190,8 @@ namespace OpenRA.Mods.Common.Pathfinder
public CellInfo this[CPos pos]
{
get
{
return cellInfo[pos];
}
set
{
cellInfo[pos] = value;
}
get { return cellInfo[pos]; }
set { cellInfo[pos] = value; }
}
}
}

View File

@@ -8,6 +8,7 @@
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Traits;
@@ -32,10 +33,13 @@ namespace OpenRA.Mods.Common.Pathfinder
considered = new LinkedList<Pair<CPos, int>>();
}
public static IPathSearch Search(World world, MobileInfo mi, Actor self, bool checkForBlocked)
public static IPathSearch Search(World world, MobileInfo mi, Actor self, bool checkForBlocked, Func<CPos, bool> goalCondition)
{
var graph = new PathGraph(CellInfoLayerManager.Instance.NewLayer(world.Map), mi, self, world, checkForBlocked);
return new PathSearch(graph);
var search = new PathSearch(graph);
search.isGoal = goalCondition;
search.heuristic = loc => 0;
return search;
}
public static IPathSearch FromPoint(World world, MobileInfo mi, Actor self, CPos from, CPos target, bool checkForBlocked)
@@ -46,6 +50,12 @@ namespace OpenRA.Mods.Common.Pathfinder
heuristic = DefaultEstimator(target)
};
search.isGoal = loc =>
{
var locInfo = search.Graph[loc];
return locInfo.EstimatedTotal - locInfo.CostSoFar == 0;
};
if (world.Map.Contains(from))
search.AddInitialCell(from);
@@ -60,6 +70,12 @@ namespace OpenRA.Mods.Common.Pathfinder
heuristic = DefaultEstimator(target)
};
search.isGoal = loc =>
{
var locInfo = search.Graph[loc];
return locInfo.EstimatedTotal - locInfo.CostSoFar == 0;
};
foreach (var sl in froms.Where(sl => world.Map.Contains(sl)))
search.AddInitialCell(sl);
@@ -88,12 +104,8 @@ namespace OpenRA.Mods.Common.Pathfinder
var currentCell = Graph[currentMinNode];
Graph[currentMinNode] = new CellInfo(currentCell.CostSoFar, currentCell.EstimatedTotal, currentCell.PreviousPos, CellStatus.Closed);
if (Graph.CustomCost != null)
{
var c = Graph.CustomCost(currentMinNode);
if (c == int.MaxValue)
return currentMinNode;
}
if (Graph.CustomCost != null && Graph.CustomCost(currentMinNode) == Constants.InvalidNode)
return currentMinNode;
foreach (var connection in Graph.GetConnections(currentMinNode))
{