Merge pull request #7618 from Rydra/upstream/hv-heuristic

Changed the harvester heuristic, solved several regressions in Pathfinder and harvesters
This commit is contained in:
Pavel Penev
2015-03-13 01:55:50 +02:00
4 changed files with 23 additions and 6 deletions

View File

@@ -38,6 +38,7 @@ Also thanks to:
* D2k Sardaukar * D2k Sardaukar
* Daniel Derejvanik (Harisson) * Daniel Derejvanik (Harisson)
* Danny Keary (Dan9550) * Danny Keary (Dan9550)
* David Jiménez (Rydra)
* David Russell (DavidARussell) * David Russell (DavidARussell)
* DeadlySurprise * DeadlySurprise
* Erasmus Schroder (rasco) * Erasmus Schroder (rasco)

View File

@@ -8,6 +8,7 @@
*/ */
#endregion #endregion
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
@@ -58,28 +59,28 @@ namespace OpenRA.Mods.Common.Activities
{ {
// Avoid this cell: // Avoid this cell:
if (avoidCell.HasValue && loc == avoidCell.Value) if (avoidCell.HasValue && loc == avoidCell.Value)
return Constants.CellCost; return EstimateDistance(loc, searchFromLoc) + Constants.CellCost;
// Don't harvest out of range: // Don't harvest out of range:
var distSquared = (loc - searchFromLoc).LengthSquared; var distSquared = (loc - searchFromLoc).LengthSquared;
if (distSquared > searchRadiusSquared) if (distSquared > searchRadiusSquared)
return Constants.CellCost * 2; return EstimateDistance(loc, searchFromLoc) + Constants.CellCost * 2;
// Get the resource at this location: // Get the resource at this location:
var resType = resLayer.GetResource(loc); var resType = resLayer.GetResource(loc);
if (resType == null) if (resType == null)
return Constants.CellCost; return EstimateDistance(loc, searchFromLoc) + Constants.CellCost;
// Can the harvester collect this kind of resource? // Can the harvester collect this kind of resource?
if (!harvInfo.Resources.Contains(resType.Info.Name)) if (!harvInfo.Resources.Contains(resType.Info.Name))
return Constants.CellCost; return EstimateDistance(loc, searchFromLoc) + Constants.CellCost;
if (territory != null) if (territory != null)
{ {
// Another harvester has claimed this resource: // Another harvester has claimed this resource:
ResourceClaim claim; ResourceClaim claim;
if (territory.IsClaimedByAnyoneElse(self, loc, out claim)) if (territory.IsClaimedByAnyoneElse(self, loc, out claim))
return Constants.CellCost; return EstimateDistance(loc, searchFromLoc) + Constants.CellCost;
} }
return 0; return 0;
@@ -123,6 +124,15 @@ namespace OpenRA.Mods.Common.Activities
return Util.SequenceActivities(mobile.MoveTo(path[0], 1), new HarvestResource(), new FindResources()); return Util.SequenceActivities(mobile.MoveTo(path[0], 1), new HarvestResource(), new FindResources());
} }
// Diagonal distance heuristic
static int EstimateDistance(CPos here, CPos destination)
{
var diag = Math.Min(Math.Abs(here.X - destination.X), Math.Abs(here.Y - destination.Y));
var straight = Math.Abs(here.X - destination.X) + Math.Abs(here.Y - destination.Y);
return Constants.CellCost * straight + (Constants.DiagonalCellCost - 2 * Constants.CellCost) * diag;
}
public override IEnumerable<Target> GetTargets(Actor self) public override IEnumerable<Target> GetTargets(Actor self)
{ {
yield return Target.FromCell(self.World, self.Location); yield return Target.FromCell(self.World, self.Location);

View File

@@ -60,7 +60,6 @@ namespace OpenRA.Mods.Common.Pathfinder
return null; return null;
} }
cached.Tick = world.WorldTick;
return cached.Result; return cached.Result;
} }

View File

@@ -88,6 +88,13 @@ namespace OpenRA.Mods.Common.Pathfinder
var currentCell = Graph[currentMinNode]; var currentCell = Graph[currentMinNode];
Graph[currentMinNode] = new CellInfo(currentCell.CostSoFar, currentCell.EstimatedTotal, currentCell.PreviousPos, CellStatus.Closed); 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;
}
foreach (var connection in Graph.GetConnections(currentMinNode)) foreach (var connection in Graph.GetConnections(currentMinNode))
{ {
// Calculate the cost up to that point // Calculate the cost up to that point