FindAndDeliverResources, trivial optimizations.

This commit is contained in:
Vapre
2021-07-16 02:02:57 +02:00
committed by Pavel Penev
parent eff7e803bf
commit 573a6cf645
2 changed files with 19 additions and 12 deletions

View File

@@ -180,7 +180,8 @@ namespace OpenRA.Mods.Common.Activities
var searchRadiusSquared = searchRadius * searchRadius;
var procPos = procLoc.HasValue ? (WPos?)self.World.Map.CenterOfCell(procLoc.Value) : null;
var map = self.World.Map;
var procPos = procLoc.HasValue ? (WPos?)map.CenterOfCell(procLoc.Value) : null;
var harvPos = self.CenterPosition;
// Find any harvestable resources:
@@ -190,25 +191,28 @@ namespace OpenRA.Mods.Common.Activities
.WithCustomCost(loc =>
{
if ((loc - searchFromLoc).LengthSquared > searchRadiusSquared)
return int.MaxValue;
return PathGraph.CostForInvalidCell;
// Add a cost modifier to harvestable cells to prefer resources that are closer to the refinery.
// This reduces the tendancy for harvesters to move in straight lines
if (procPos.HasValue && harvInfo.ResourceRefineryDirectionPenalty > 0 && harv.CanHarvestCell(self, loc))
{
var pos = self.World.Map.CenterOfCell(loc);
var pos = map.CenterOfCell(loc);
// Calculate harv-cell-refinery angle (cosine rule)
var a = harvPos - procPos.Value;
var b = pos - procPos.Value;
var c = pos - harvPos;
if (b != WVec.Zero && c != WVec.Zero)
if (b != WVec.Zero)
{
var cosA = (int)(512 * (b.LengthSquared + c.LengthSquared - a.LengthSquared) / b.Length / c.Length);
var c = pos - harvPos;
if (c != WVec.Zero)
{
var a = harvPos - procPos.Value;
var cosA = (int)(512 * (b.LengthSquared + c.LengthSquared - a.LengthSquared) / b.Length / c.Length);
// Cost modifier varies between 0 and ResourceRefineryDirectionPenalty
return Math.Abs(harvInfo.ResourceRefineryDirectionPenalty / 2) + harvInfo.ResourceRefineryDirectionPenalty * cosA / 2048;
// Cost modifier varies between 0 and ResourceRefineryDirectionPenalty
return Math.Abs(harvInfo.ResourceRefineryDirectionPenalty / 2) + harvInfo.ResourceRefineryDirectionPenalty * cosA / 2048;
}
}
}