From 8253573cedda397dc0e9154381ddc9cdc8bd6ac2 Mon Sep 17 00:00:00 2001 From: reaperrr Date: Thu, 2 Jul 2015 21:08:38 +0200 Subject: [PATCH] Cache lookups in DeliverResources constructor Only re-check for refinery every 5 seconds instead of every second. --- .../Activities/DeliverResources.cs | 20 ++++++++++++++----- .../Activities/FindResources.cs | 6 ++++-- OpenRA.Mods.Common/Traits/Harvester.cs | 7 ++++--- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/OpenRA.Mods.Common/Activities/DeliverResources.cs b/OpenRA.Mods.Common/Activities/DeliverResources.cs index 1bfe5637fb..a6d49cec96 100644 --- a/OpenRA.Mods.Common/Activities/DeliverResources.cs +++ b/OpenRA.Mods.Common/Activities/DeliverResources.cs @@ -18,17 +18,26 @@ namespace OpenRA.Mods.Common.Activities public class DeliverResources : Activity { const int NextChooseTime = 100; + + readonly IMove movement; + readonly Harvester harv; + readonly HarvesterInfo harvInfo; + bool isDocking; int chosenTicks; + public DeliverResources(Actor self) + { + movement = self.Trait(); + harv = self.Trait(); + harvInfo = self.Info.Traits.Get(); + } + public override Activity Tick(Actor self) { if (NextActivity != null) return NextActivity; - var movement = self.Trait(); - var harv = self.Trait(); - // Find the nearest best refinery if not explicitly ordered to a specific refinery: if (harv.OwnerLinkedProc == null || !harv.OwnerLinkedProc.IsInWorld) { @@ -46,7 +55,8 @@ namespace OpenRA.Mods.Common.Activities if (harv.LinkedProc == null || !harv.LinkedProc.IsInWorld) harv.ChooseNewProc(self, null); - if (harv.LinkedProc == null) // no procs exist; check again in 1s. + // No refineries exist; check again after delay of 1s. + if (harv.LinkedProc == null) return Util.SequenceActivities(new Wait(25), this); var proc = harv.LinkedProc; @@ -56,7 +66,7 @@ namespace OpenRA.Mods.Common.Activities if (self.Location != proc.Location + iao.DeliveryOffset) { var notify = self.TraitsImplementing(); - var next = new DeliverResources(); + var next = new DeliverResources(self); foreach (var n in notify) n.MovingToRefinery(self, proc.Location + iao.DeliveryOffset, next); diff --git a/OpenRA.Mods.Common/Activities/FindResources.cs b/OpenRA.Mods.Common/Activities/FindResources.cs index d2f3444f1a..c6095e752a 100644 --- a/OpenRA.Mods.Common/Activities/FindResources.cs +++ b/OpenRA.Mods.Common/Activities/FindResources.cs @@ -38,8 +38,10 @@ namespace OpenRA.Mods.Common.Activities var harv = self.Trait(); + var deliver = new DeliverResources(self); + if (harv.IsFull) - return Util.SequenceActivities(new DeliverResources(), NextActivity); + return Util.SequenceActivities(deliver, NextActivity); var harvInfo = self.Info.Traits.Get(); var mobile = self.Trait(); @@ -90,7 +92,7 @@ namespace OpenRA.Mods.Common.Activities if (path.Count == 0) { if (!harv.IsEmpty) - return new DeliverResources(); + return deliver; else { // Get out of the way if we are: diff --git a/OpenRA.Mods.Common/Traits/Harvester.cs b/OpenRA.Mods.Common/Traits/Harvester.cs index 479dfc7fb7..590a112bc5 100644 --- a/OpenRA.Mods.Common/Traits/Harvester.cs +++ b/OpenRA.Mods.Common/Traits/Harvester.cs @@ -237,7 +237,7 @@ namespace OpenRA.Mods.Common.Traits // Are we not empty? Deliver resources: if (!IsEmpty) { - self.QueueActivity(new DeliverResources()); + self.QueueActivity(new DeliverResources(self)); return; } @@ -378,10 +378,11 @@ namespace OpenRA.Mods.Common.Traits self.SetTargetLine(Target.FromOrder(self.World, order), Color.Green); self.CancelActivity(); - self.QueueActivity(new DeliverResources()); + + var next = new DeliverResources(self); + self.QueueActivity(next); var notify = self.TraitsImplementing(); - var next = new DeliverResources(); foreach (var n in notify) n.MovingToRefinery(self, order.TargetLocation, next); }