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 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; var harvPos = self.CenterPosition;
// Find any harvestable resources: // Find any harvestable resources:
@@ -190,25 +191,28 @@ namespace OpenRA.Mods.Common.Activities
.WithCustomCost(loc => .WithCustomCost(loc =>
{ {
if ((loc - searchFromLoc).LengthSquared > searchRadiusSquared) 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. // 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 // This reduces the tendancy for harvesters to move in straight lines
if (procPos.HasValue && harvInfo.ResourceRefineryDirectionPenalty > 0 && harv.CanHarvestCell(self, loc)) 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) // Calculate harv-cell-refinery angle (cosine rule)
var a = harvPos - procPos.Value;
var b = pos - 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 // Cost modifier varies between 0 and ResourceRefineryDirectionPenalty
return Math.Abs(harvInfo.ResourceRefineryDirectionPenalty / 2) + harvInfo.ResourceRefineryDirectionPenalty * cosA / 2048; return Math.Abs(harvInfo.ResourceRefineryDirectionPenalty / 2) + harvInfo.ResourceRefineryDirectionPenalty * cosA / 2048;
}
} }
} }

View File

@@ -110,11 +110,14 @@ namespace OpenRA.Mods.Common.Traits
// Correct for SubCell offset // Correct for SubCell offset
target -= world.Map.Grid.OffsetOfSubCell(srcSub); target -= world.Map.Grid.OffsetOfSubCell(srcSub);
var rangeLengthSquared = range.LengthSquared;
var map = world.Map;
// Select only the tiles that are within range from the requested SubCell // Select only the tiles that are within range from the requested SubCell
// This assumes that the SubCell does not change during the path traversal // This assumes that the SubCell does not change during the path traversal
var tilesInRange = world.Map.FindTilesInCircle(targetCell, range.Length / 1024 + 1) var tilesInRange = map.FindTilesInCircle(targetCell, range.Length / 1024 + 1)
.Where(t => (world.Map.CenterOfCell(t) - target).LengthSquared <= range.LengthSquared .Where(t => (map.CenterOfCell(t) - target).LengthSquared <= rangeLengthSquared
&& mobile.Info.CanEnterCell(self.World, self, t)); && mobile.Info.CanEnterCell(world, self, t));
// See if there is any cell within range that does not involve a cross-domain request // See if there is any cell within range that does not involve a cross-domain request
// Really, we only need to check the circle perimeter, but it's not clear that would be a performance win // Really, we only need to check the circle perimeter, but it's not clear that would be a performance win