Merge pull request #9144 from Mailaender/opt-find-resource
Optimized FindResources worse case scenario path finding
This commit is contained in:
@@ -27,6 +27,7 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
readonly ResourceLayer resLayer;
|
readonly ResourceLayer resLayer;
|
||||||
readonly ResourceClaimLayer territory;
|
readonly ResourceClaimLayer territory;
|
||||||
readonly IPathFinder pathFinder;
|
readonly IPathFinder pathFinder;
|
||||||
|
readonly DomainIndex domainIndex;
|
||||||
|
|
||||||
CPos? avoidCell;
|
CPos? avoidCell;
|
||||||
|
|
||||||
@@ -39,6 +40,7 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
resLayer = self.World.WorldActor.Trait<ResourceLayer>();
|
resLayer = self.World.WorldActor.Trait<ResourceLayer>();
|
||||||
territory = self.World.WorldActor.TraitOrDefault<ResourceClaimLayer>();
|
territory = self.World.WorldActor.TraitOrDefault<ResourceClaimLayer>();
|
||||||
pathFinder = self.World.WorldActor.Trait<IPathFinder>();
|
pathFinder = self.World.WorldActor.Trait<IPathFinder>();
|
||||||
|
domainIndex = self.World.WorldActor.Trait<DomainIndex>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public FindResources(Actor self, CPos avoidCell)
|
public FindResources(Actor self, CPos avoidCell)
|
||||||
@@ -142,8 +144,9 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
var searchRadius = harv.LastOrderLocation.HasValue ? harvInfo.SearchFromOrderRadius : harvInfo.SearchFromProcRadius;
|
var searchRadius = harv.LastOrderLocation.HasValue ? harvInfo.SearchFromOrderRadius : harvInfo.SearchFromProcRadius;
|
||||||
var searchRadiusSquared = searchRadius * searchRadius;
|
var searchRadiusSquared = searchRadius * searchRadius;
|
||||||
|
|
||||||
|
var passable = (uint)mobileInfo.GetMovementClass(self.World.TileSet);
|
||||||
var search = PathSearch.Search(self.World, mobileInfo, self, true,
|
var search = PathSearch.Search(self.World, mobileInfo, self, true,
|
||||||
loc => IsHarvestable(self, loc))
|
loc => domainIndex.IsPassable(self.Location, loc, passable) && IsHarvestable(self, loc))
|
||||||
.WithCustomCost(loc =>
|
.WithCustomCost(loc =>
|
||||||
{
|
{
|
||||||
if ((avoidCell.HasValue && loc == avoidCell.Value) ||
|
if ((avoidCell.HasValue && loc == avoidCell.Value) ||
|
||||||
|
|||||||
@@ -353,12 +353,8 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// A bot order gives us a CPos.Zero TargetLocation, so find some good resources for him:
|
// A bot order gives us a CPos.Zero TargetLocation.
|
||||||
loc = FindNextResourceForBot(self);
|
loc = self.Location;
|
||||||
|
|
||||||
// No more resources? Oh well.
|
|
||||||
if (!loc.HasValue)
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var next = new FindResources(self);
|
var next = new FindResources(self);
|
||||||
@@ -411,45 +407,6 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static CPos? FindNextResourceForBot(Actor self)
|
|
||||||
{
|
|
||||||
// NOTE: This is only used for the AI to find the next available resource to harvest.
|
|
||||||
var harvInfo = self.Info.Traits.Get<HarvesterInfo>();
|
|
||||||
var mobileInfo = self.Info.Traits.Get<MobileInfo>();
|
|
||||||
var resLayer = self.World.WorldActor.Trait<ResourceLayer>();
|
|
||||||
var territory = self.World.WorldActor.TraitOrDefault<ResourceClaimLayer>();
|
|
||||||
|
|
||||||
// Find any harvestable resources:
|
|
||||||
var path = self.World.WorldActor.Trait<IPathFinder>().FindPath(
|
|
||||||
PathSearch.Search(self.World, mobileInfo, self, true,
|
|
||||||
loc =>
|
|
||||||
{
|
|
||||||
var resType = resLayer.GetResource(loc);
|
|
||||||
if (resType == null)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// Can the harvester collect this kind of resource?
|
|
||||||
if (!harvInfo.Resources.Contains(resType.Info.Name))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (territory != null)
|
|
||||||
{
|
|
||||||
// Another harvester has claimed this resource:
|
|
||||||
ResourceClaim claim;
|
|
||||||
if (territory.IsClaimedByAnyoneElse(self, loc, out claim))
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
})
|
|
||||||
.FromPoint(self.Location));
|
|
||||||
|
|
||||||
if (path.Count == 0)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
return path[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
public void OnNotifyResourceClaimLost(Actor self, ResourceClaim claim, Actor claimer)
|
public void OnNotifyResourceClaimLost(Actor self, ResourceClaim claim, Actor claimer)
|
||||||
{
|
{
|
||||||
if (self == claimer) return;
|
if (self == claimer) return;
|
||||||
|
|||||||
Reference in New Issue
Block a user