Cache FindResources trait lookups in constructor

This commit is contained in:
reaperrr
2015-07-09 19:27:06 +02:00
parent 8253573ced
commit c8661ca2f9
2 changed files with 37 additions and 27 deletions

View File

@@ -21,41 +21,50 @@ namespace OpenRA.Mods.Common.Activities
{
public class FindResources : Activity
{
readonly Harvester harv;
readonly HarvesterInfo harvInfo;
readonly Mobile mobile;
readonly MobileInfo mobileInfo;
readonly ResourceLayer resLayer;
readonly ResourceClaimLayer territory;
readonly IPathFinder pathFinder;
CPos? avoidCell;
public FindResources()
public FindResources(Actor self)
{
harv = self.Trait<Harvester>();
harvInfo = self.Info.Traits.Get<HarvesterInfo>();
mobile = self.Trait<Mobile>();
mobileInfo = self.Info.Traits.Get<MobileInfo>();
resLayer = self.World.WorldActor.Trait<ResourceLayer>();
territory = self.World.WorldActor.TraitOrDefault<ResourceClaimLayer>();
pathFinder = self.World.WorldActor.Trait<IPathFinder>();
}
public FindResources(CPos avoidCell)
public FindResources(Actor self, CPos avoidCell)
: this(self)
{
this.avoidCell = avoidCell;
}
public override Activity Tick(Actor self)
{
if (IsCanceled || NextActivity != null) return NextActivity;
var harv = self.Trait<Harvester>();
if (IsCanceled || NextActivity != null)
return NextActivity;
var deliver = new DeliverResources(self);
if (harv.IsFull)
return Util.SequenceActivities(deliver, NextActivity);
var harvInfo = self.Info.Traits.Get<HarvesterInfo>();
var mobile = self.Trait<Mobile>();
var mobileInfo = self.Info.Traits.Get<MobileInfo>();
var resLayer = self.World.WorldActor.Trait<ResourceLayer>();
var territory = self.World.WorldActor.TraitOrDefault<ResourceClaimLayer>();
// Determine where to search from and how far to search:
var searchFromLoc = harv.LastOrderLocation ?? (harv.LastLinkedProc ?? harv.LinkedProc ?? self).Location;
var searchRadius = harv.LastOrderLocation.HasValue ? harvInfo.SearchFromOrderRadius : harvInfo.SearchFromProcRadius;
var searchRadiusSquared = searchRadius * searchRadius;
// Find harvestable resources nearby:
var path = self.World.WorldActor.Trait<IPathFinder>().FindPath(
var path = pathFinder.FindPath(
PathSearch.Search(self.World, mobileInfo, self, true)
.WithHeuristic(loc =>
{
@@ -89,6 +98,8 @@ namespace OpenRA.Mods.Common.Activities
})
.FromPoint(self.Location));
var next = this;
if (path.Count == 0)
{
if (!harv.IsEmpty)
@@ -97,11 +108,11 @@ namespace OpenRA.Mods.Common.Activities
{
// Get out of the way if we are:
harv.UnblockRefinery(self);
var randFrames = 125 + self.World.SharedRandom.Next(-35, 35);
var randFrames = self.World.SharedRandom.Next(90, 160);
if (NextActivity != null)
return Util.SequenceActivities(NextActivity, new Wait(randFrames), new FindResources());
return Util.SequenceActivities(NextActivity, new Wait(randFrames), next);
else
return Util.SequenceActivities(new Wait(randFrames), new FindResources());
return Util.SequenceActivities(new Wait(randFrames), next);
}
}
@@ -109,7 +120,7 @@ namespace OpenRA.Mods.Common.Activities
if (territory != null)
{
if (!territory.ClaimResource(self, path[0]))
return Util.SequenceActivities(new Wait(25), new FindResources());
return Util.SequenceActivities(new Wait(25), next);
}
// If not given a direct order, assume ordered to the first resource location we find:
@@ -119,11 +130,10 @@ namespace OpenRA.Mods.Common.Activities
self.SetTargetLine(Target.FromCell(self.World, path[0]), Color.Red, false);
var notify = self.TraitsImplementing<INotifyHarvesterAction>();
var next = new FindResources();
foreach (var n in notify)
n.MovingToResources(self, path[0], next);
return Util.SequenceActivities(mobile.MoveTo(path[0], 1), new HarvestResource(), new FindResources());
return Util.SequenceActivities(mobile.MoveTo(path[0], 1), new HarvestResource(), next);
}
// Diagonal distance heuristic