Cache lookups in DeliverResources constructor

Only re-check for refinery every 5 seconds instead of every second.
This commit is contained in:
reaperrr
2015-07-02 21:08:38 +02:00
parent 27c6f5d6d6
commit 8253573ced
3 changed files with 23 additions and 10 deletions

View File

@@ -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<IMove>();
harv = self.Trait<Harvester>();
harvInfo = self.Info.Traits.Get<HarvesterInfo>();
}
public override Activity Tick(Actor self)
{
if (NextActivity != null)
return NextActivity;
var movement = self.Trait<IMove>();
var harv = self.Trait<Harvester>();
// 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<INotifyHarvesterAction>();
var next = new DeliverResources();
var next = new DeliverResources(self);
foreach (var n in notify)
n.MovingToRefinery(self, proc.Location + iao.DeliveryOffset, next);

View File

@@ -38,8 +38,10 @@ namespace OpenRA.Mods.Common.Activities
var harv = self.Trait<Harvester>();
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<HarvesterInfo>();
var mobile = self.Trait<Mobile>();
@@ -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:

View File

@@ -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<INotifyHarvesterAction>();
var next = new DeliverResources();
foreach (var n in notify)
n.MovingToRefinery(self, order.TargetLocation, next);
}