Cache FindResources trait lookups in constructor
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -84,13 +84,13 @@ namespace OpenRA.Mods.Common.Traits
|
||||
public void Created(Actor self)
|
||||
{
|
||||
if (info.SearchOnCreation)
|
||||
self.QueueActivity(new FindResources());
|
||||
self.QueueActivity(new FindResources(self));
|
||||
}
|
||||
|
||||
public void BuildingComplete(Actor self)
|
||||
{
|
||||
if (info.SearchOnCreation)
|
||||
self.QueueActivity(new FindResources());
|
||||
self.QueueActivity(new FindResources(self));
|
||||
}
|
||||
|
||||
public void SetProcLines(Actor proc)
|
||||
@@ -130,7 +130,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
// Move out of the refinery dock and continue harvesting:
|
||||
UnblockRefinery(self);
|
||||
self.QueueActivity(new FindResources());
|
||||
self.QueueActivity(new FindResources(self));
|
||||
}
|
||||
|
||||
bool IsAcceptableProcType(Actor proc)
|
||||
@@ -199,11 +199,11 @@ namespace OpenRA.Mods.Common.Traits
|
||||
if (territory != null) territory.ClaimResource(self, moveTo);
|
||||
|
||||
var notify = self.TraitsImplementing<INotifyHarvesterAction>();
|
||||
var next = new FindResources();
|
||||
var next = new FindResources(self);
|
||||
foreach (var n in notify)
|
||||
n.MovingToResources(self, moveTo, next);
|
||||
|
||||
self.QueueActivity(new FindResources());
|
||||
self.QueueActivity(new FindResources(self));
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -225,7 +225,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
self.SetTargetLine(Target.FromCell(self.World, moveTo), Color.Gray, false);
|
||||
|
||||
// Find more resources but not at this location:
|
||||
self.QueueActivity(new FindResources(cell));
|
||||
self.QueueActivity(new FindResources(self, cell));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -335,7 +335,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
self.SetTargetLine(Target.FromCell(self.World, loc), Color.Red);
|
||||
|
||||
var notify = self.TraitsImplementing<INotifyHarvesterAction>();
|
||||
var next = new FindResources();
|
||||
var next = new FindResources(self);
|
||||
foreach (var n in notify)
|
||||
n.MovingToResources(self, loc, next);
|
||||
|
||||
@@ -358,7 +358,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
// This prevents harvesters returning to an empty patch when the player orders them to a new patch:
|
||||
LastHarvestedCell = LastOrderLocation;
|
||||
self.QueueActivity(new FindResources());
|
||||
self.QueueActivity(new FindResources(self));
|
||||
}
|
||||
else if (order.OrderString == "Deliver")
|
||||
{
|
||||
@@ -443,7 +443,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
// Our claim on a resource was stolen, find more unclaimed resources:
|
||||
self.CancelActivity();
|
||||
self.QueueActivity(new FindResources());
|
||||
self.QueueActivity(new FindResources(self));
|
||||
}
|
||||
|
||||
PipType GetPipAt(int i)
|
||||
|
||||
Reference in New Issue
Block a user