Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor PathGraph. #19532

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions OpenRA.Mods.Common/Activities/FindAndDeliverResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,22 +186,24 @@ public override bool Tick(Actor self)

// Find any harvestable resources:
List<CPos> path;
using (var search = PathSearch.Search(self.World, mobile.Locomotor, self, BlockedByActor.Stationary, loc =>
domainIndex.IsPassable(self.Location, loc, mobile.Locomotor) && harv.CanHarvestCell(self, loc) && claimLayer.CanClaimCell(self, loc))
.WithCustomCost(loc =>
Func<CPos, int> customCost;

if (procPos.HasValue && harvInfo.ResourceRefineryDirectionPenalty > 0)
{
customCost = loc =>
{
if ((loc - searchFromLoc).LengthSquared > searchRadiusSquared)
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
if (procPos.HasValue && harvInfo.ResourceRefineryDirectionPenalty > 0 && harv.CanHarvestCell(self, loc))
if (harv.CanHarvestCell(self, loc))
{
var pos = map.CenterOfCell(loc);

// Calculate harv-cell-refinery angle (cosine rule)
var b = pos - procPos.Value;

if (b != WVec.Zero)
{
var c = pos - harvPos;
Expand All @@ -217,9 +219,32 @@ public override bool Tick(Actor self)
}

return 0;
})
.FromPoint(searchFromLoc)
.FromPoint(self.Location))
};
}
else
{
customCost = loc =>
{
if ((loc - searchFromLoc).LengthSquared > searchRadiusSquared)
return PathGraph.CostForInvalidCell;
return 0;
};
}

var query = new PathQuery(
queryType: PathQueryType.ConditionUnidirectional,
world: self.World,
locomotor: mobile.Locomotor,
actor: self,
check: BlockedByActor.Stationary,
isGoal: loc => domainIndex.IsPassable(self.Location, loc, mobile.Locomotor)
&& harv.CanHarvestCell(self, loc)
&& claimLayer.CanClaimCell(self, loc),
customCost: customCost,
fromPositions: self.Location.Equals(searchFromLoc) ?
new CPos[] { self.Location } : new CPos[] { searchFromLoc, self.Location });

using (var search = new PathSearch(query))
path = mobile.Pathfinder.FindPath(search);

if (path.Count > 0)
Expand Down
15 changes: 12 additions & 3 deletions OpenRA.Mods.Common/Activities/Move/Move.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,19 @@ public Move(Actor self, CPos destination, Color? targetLineColor = null)
getPath = check =>
{
List<CPos> path;
using (var search =
PathSearch.FromPoint(self.World, mobile.Locomotor, self, mobile.ToCell, destination, check)
.WithoutLaneBias())
var query = new PathQuery(
queryType: PathQueryType.PositionUnidirectional,
world: self.World,
locomotor: mobile.Locomotor,
actor: self,
laneBiasDisabled: true,
fromPosition: mobile.ToCell,
toPosition: destination,
check: check);

using (var search = new PathSearch(query))
path = mobile.Pathfinder.FindPath(search);

return path;
};

Expand Down
23 changes: 21 additions & 2 deletions OpenRA.Mods.Common/Activities/Move/MoveAdjacentTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,27 @@ List<CPos> CalculatePathToTarget(Actor self, BlockedByActor check)
if (!searchCells.Any())
return NoPath;

using (var fromSrc = PathSearch.FromPoints(self.World, Mobile.Locomotor, self, searchCells, loc, check))
using (var fromDest = PathSearch.FromPoint(self.World, Mobile.Locomotor, self, loc, lastVisibleTargetLocation, check).Reverse())
var fromSrcQuery = new PathQuery(
queryType: PathQueryType.PositionBidirectional,
world: self.World,
locomotor: Mobile.Locomotor,
actor: self,
fromPositions: searchCells,
toPosition: loc,
check: check);

var fromDestQuery = new PathQuery(
queryType: PathQueryType.PositionBidirectional,
world: self.World,
locomotor: Mobile.Locomotor,
actor: self,
fromPosition: loc,
toPosition: lastVisibleTargetLocation,
check: check,
reverse: true);

using (var fromSrc = new PathSearch(fromSrcQuery))
using (var fromDest = new PathSearch(fromDestQuery))
return Mobile.Pathfinder.FindBidiPath(fromSrc, fromDest);
}

Expand Down
Loading