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 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; 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; this.avoidCell = avoidCell;
} }
public override Activity Tick(Actor self) public override Activity Tick(Actor self)
{ {
if (IsCanceled || NextActivity != null) return NextActivity; if (IsCanceled || NextActivity != null)
return NextActivity;
var harv = self.Trait<Harvester>();
var deliver = new DeliverResources(self); var deliver = new DeliverResources(self);
if (harv.IsFull) if (harv.IsFull)
return Util.SequenceActivities(deliver, NextActivity); 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: // Determine where to search from and how far to search:
var searchFromLoc = harv.LastOrderLocation ?? (harv.LastLinkedProc ?? harv.LinkedProc ?? self).Location; var searchFromLoc = harv.LastOrderLocation ?? (harv.LastLinkedProc ?? harv.LinkedProc ?? self).Location;
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;
// Find harvestable resources nearby: // Find harvestable resources nearby:
var path = self.World.WorldActor.Trait<IPathFinder>().FindPath( var path = pathFinder.FindPath(
PathSearch.Search(self.World, mobileInfo, self, true) PathSearch.Search(self.World, mobileInfo, self, true)
.WithHeuristic(loc => .WithHeuristic(loc =>
{ {
@@ -89,6 +98,8 @@ namespace OpenRA.Mods.Common.Activities
}) })
.FromPoint(self.Location)); .FromPoint(self.Location));
var next = this;
if (path.Count == 0) if (path.Count == 0)
{ {
if (!harv.IsEmpty) if (!harv.IsEmpty)
@@ -97,11 +108,11 @@ namespace OpenRA.Mods.Common.Activities
{ {
// Get out of the way if we are: // Get out of the way if we are:
harv.UnblockRefinery(self); harv.UnblockRefinery(self);
var randFrames = 125 + self.World.SharedRandom.Next(-35, 35); var randFrames = self.World.SharedRandom.Next(90, 160);
if (NextActivity != null) if (NextActivity != null)
return Util.SequenceActivities(NextActivity, new Wait(randFrames), new FindResources()); return Util.SequenceActivities(NextActivity, new Wait(randFrames), next);
else 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 != null)
{ {
if (!territory.ClaimResource(self, path[0])) 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: // 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); self.SetTargetLine(Target.FromCell(self.World, path[0]), Color.Red, false);
var notify = self.TraitsImplementing<INotifyHarvesterAction>(); var notify = self.TraitsImplementing<INotifyHarvesterAction>();
var next = new FindResources();
foreach (var n in notify) foreach (var n in notify)
n.MovingToResources(self, path[0], next); 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 // Diagonal distance heuristic

View File

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