From c8661ca2f9f786542a39dc6ea773d633604fd024 Mon Sep 17 00:00:00 2001 From: reaperrr Date: Thu, 9 Jul 2015 19:27:06 +0200 Subject: [PATCH] Cache FindResources trait lookups in constructor --- .../Activities/FindResources.cs | 46 +++++++++++-------- OpenRA.Mods.Common/Traits/Harvester.cs | 18 ++++---- 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/OpenRA.Mods.Common/Activities/FindResources.cs b/OpenRA.Mods.Common/Activities/FindResources.cs index c6095e752a..30ca37a8b1 100644 --- a/OpenRA.Mods.Common/Activities/FindResources.cs +++ b/OpenRA.Mods.Common/Activities/FindResources.cs @@ -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(); + harvInfo = self.Info.Traits.Get(); + mobile = self.Trait(); + mobileInfo = self.Info.Traits.Get(); + resLayer = self.World.WorldActor.Trait(); + territory = self.World.WorldActor.TraitOrDefault(); + pathFinder = self.World.WorldActor.Trait(); } - 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(); + 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(); - var mobile = self.Trait(); - var mobileInfo = self.Info.Traits.Get(); - var resLayer = self.World.WorldActor.Trait(); - var territory = self.World.WorldActor.TraitOrDefault(); - // 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().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(); - 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 diff --git a/OpenRA.Mods.Common/Traits/Harvester.cs b/OpenRA.Mods.Common/Traits/Harvester.cs index 590a112bc5..555291a25c 100644 --- a/OpenRA.Mods.Common/Traits/Harvester.cs +++ b/OpenRA.Mods.Common/Traits/Harvester.cs @@ -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(); - 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(); - 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)