Merge pull request #8708 from reaperrr/harv-caching

Improved caching in harvester activities
This commit is contained in:
Oliver Brakmann
2015-07-13 17:43:39 +02:00
6 changed files with 156 additions and 102 deletions

View File

@@ -18,17 +18,26 @@ namespace OpenRA.Mods.Common.Activities
public class DeliverResources : Activity public class DeliverResources : Activity
{ {
const int NextChooseTime = 100; const int NextChooseTime = 100;
readonly IMove movement;
readonly Harvester harv;
readonly HarvesterInfo harvInfo;
bool isDocking; bool isDocking;
int chosenTicks; 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) public override Activity Tick(Actor self)
{ {
if (NextActivity != null) if (NextActivity != null)
return NextActivity; 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: // Find the nearest best refinery if not explicitly ordered to a specific refinery:
if (harv.OwnerLinkedProc == null || !harv.OwnerLinkedProc.IsInWorld) if (harv.OwnerLinkedProc == null || !harv.OwnerLinkedProc.IsInWorld)
{ {
@@ -46,8 +55,9 @@ namespace OpenRA.Mods.Common.Activities
if (harv.LinkedProc == null || !harv.LinkedProc.IsInWorld) if (harv.LinkedProc == null || !harv.LinkedProc.IsInWorld)
harv.ChooseNewProc(self, null); harv.ChooseNewProc(self, null);
if (harv.LinkedProc == null) // no procs exist; check again in 1s. // No refineries exist; check again after delay defined in Harvester.
return Util.SequenceActivities(new Wait(25), this); if (harv.LinkedProc == null)
return Util.SequenceActivities(new Wait(harvInfo.SearchForDeliveryBuildingDelay), this);
var proc = harv.LinkedProc; var proc = harv.LinkedProc;
var iao = proc.Trait<IAcceptResources>(); var iao = proc.Trait<IAcceptResources>();
@@ -56,7 +66,7 @@ namespace OpenRA.Mods.Common.Activities
if (self.Location != proc.Location + iao.DeliveryOffset) if (self.Location != proc.Location + iao.DeliveryOffset)
{ {
var notify = self.TraitsImplementing<INotifyHarvesterAction>(); var notify = self.TraitsImplementing<INotifyHarvesterAction>();
var next = new DeliverResources(); var next = new DeliverResources(self);
foreach (var n in notify) foreach (var n in notify)
n.MovingToRefinery(self, proc.Location + iao.DeliveryOffset, next); n.MovingToRefinery(self, proc.Location + iao.DeliveryOffset, next);

View File

@@ -21,31 +21,42 @@ 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);
if (harv.IsFull) 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>();
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;
@@ -53,7 +64,7 @@ namespace OpenRA.Mods.Common.Activities
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 =>
{ {
@@ -87,19 +98,21 @@ 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)
return new DeliverResources(); return deliver;
else else
{ {
// 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);
} }
} }
@@ -107,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:
@@ -117,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(self), next);
} }
// Diagonal distance heuristic // Diagonal distance heuristic
@@ -138,54 +150,4 @@ namespace OpenRA.Mods.Common.Activities
yield return Target.FromCell(self.World, self.Location); yield return Target.FromCell(self.World, self.Location);
} }
} }
public class HarvestResource : Activity
{
public override Activity Tick(Actor self)
{
var territory = self.World.WorldActor.TraitOrDefault<ResourceClaimLayer>();
if (IsCanceled)
{
if (territory != null)
territory.UnclaimByActor(self);
return NextActivity;
}
var harv = self.Trait<Harvester>();
var harvInfo = self.Info.Traits.Get<HarvesterInfo>();
harv.LastHarvestedCell = self.Location;
if (harv.IsFull)
{
if (territory != null)
territory.UnclaimByActor(self);
return NextActivity;
}
// Turn to one of the harvestable facings
if (harvInfo.HarvestFacings != 0)
{
var facing = self.Trait<IFacing>().Facing;
var desired = Util.QuantizeFacing(facing, harvInfo.HarvestFacings) * (256 / harvInfo.HarvestFacings);
if (desired != facing)
return Util.SequenceActivities(new Turn(self, desired), this);
}
var resLayer = self.World.WorldActor.Trait<ResourceLayer>();
var resource = resLayer.Harvest(self.Location);
if (resource == null)
{
if (territory != null)
territory.UnclaimByActor(self);
return NextActivity;
}
harv.AcceptResource(resource);
foreach (var t in self.TraitsImplementing<INotifyHarvesterAction>())
t.Harvested(self, resource);
return Util.SequenceActivities(new Wait(harvInfo.LoadTicksPerBale), this);
}
}
} }

View File

@@ -0,0 +1,77 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using OpenRA.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Activities
{
public class HarvestResource : Activity
{
readonly Harvester harv;
readonly HarvesterInfo harvInfo;
readonly IFacing facing;
readonly ResourceClaimLayer territory;
readonly ResourceLayer resLayer;
public HarvestResource(Actor self)
{
harv = self.Trait<Harvester>();
harvInfo = self.Info.Traits.Get<HarvesterInfo>();
facing = self.Trait<IFacing>();
territory = self.World.WorldActor.TraitOrDefault<ResourceClaimLayer>();
resLayer = self.World.WorldActor.Trait<ResourceLayer>();
}
public override Activity Tick(Actor self)
{
if (IsCanceled)
{
if (territory != null)
territory.UnclaimByActor(self);
return NextActivity;
}
harv.LastHarvestedCell = self.Location;
if (harv.IsFull)
{
if (territory != null)
territory.UnclaimByActor(self);
return NextActivity;
}
// Turn to one of the harvestable facings
if (harvInfo.HarvestFacings != 0)
{
var current = facing.Facing;
var desired = Util.QuantizeFacing(current, harvInfo.HarvestFacings) * (256 / harvInfo.HarvestFacings);
if (desired != current)
return Util.SequenceActivities(new Turn(self, desired), this);
}
var resource = resLayer.Harvest(self.Location);
if (resource == null)
{
if (territory != null)
territory.UnclaimByActor(self);
return NextActivity;
}
harv.AcceptResource(resource);
foreach (var t in self.TraitsImplementing<INotifyHarvesterAction>())
t.Harvested(self, resource);
return Util.SequenceActivities(new Wait(harvInfo.LoadTicksPerBale), this);
}
}
}

View File

@@ -112,6 +112,7 @@
<Compile Include="Activities\EnterTransport.cs" /> <Compile Include="Activities\EnterTransport.cs" />
<Compile Include="Activities\ExternalCaptureActor.cs" /> <Compile Include="Activities\ExternalCaptureActor.cs" />
<Compile Include="Activities\FindResources.cs" /> <Compile Include="Activities\FindResources.cs" />
<Compile Include="Activities\HarvestResource.cs" />
<Compile Include="Activities\HarvesterDockSequence.cs" /> <Compile Include="Activities\HarvesterDockSequence.cs" />
<Compile Include="Activities\Heal.cs" /> <Compile Include="Activities\Heal.cs" />
<Compile Include="Activities\Hunt.cs" /> <Compile Include="Activities\Hunt.cs" />

View File

@@ -105,7 +105,7 @@ namespace OpenRA.Mods.Common.Pathfinder
public abstract IEnumerable<Pair<CPos, int>> Considered { get; } public abstract IEnumerable<Pair<CPos, int>> Considered { get; }
public Player Owner { get { return this.Graph.Actor.Owner; } } public Player Owner { get { return Graph.Actor.Owner; } }
public int MaxCost { get; protected set; } public int MaxCost { get; protected set; }
public bool Debug { get; set; } public bool Debug { get; set; }
string id; string id;
@@ -184,7 +184,7 @@ namespace OpenRA.Mods.Common.Pathfinder
public IPathSearch FromPoint(CPos from) public IPathSearch FromPoint(CPos from)
{ {
if (this.Graph.World.Map.Contains(from)) if (Graph.World.Map.Contains(from))
AddInitialCell(from); AddInitialCell(from);
return this; return this;

View File

@@ -19,10 +19,13 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
{ {
public class HarvesterInfo : ITraitInfo public class HarvesterInfo : ITraitInfo, Requires<MobileInfo>
{ {
public readonly string[] DeliveryBuildings = { }; public readonly string[] DeliveryBuildings = { };
[Desc("How long (in ticks) to wait until (re-)checking for a nearby available DeliveryBuilding if not yet linked to one.")]
public readonly int SearchForDeliveryBuildingDelay = 125;
[Desc("How much resources it can carry.")] [Desc("How much resources it can carry.")]
public readonly int Capacity = 28; public readonly int Capacity = 28;
@@ -62,6 +65,7 @@ namespace OpenRA.Mods.Common.Traits
INotifyResourceClaimLost, INotifyIdle, INotifyBlockingMove, INotifyBuildComplete INotifyResourceClaimLost, INotifyIdle, INotifyBlockingMove, INotifyBuildComplete
{ {
readonly HarvesterInfo info; readonly HarvesterInfo info;
readonly Mobile mobile;
Dictionary<ResourceTypeInfo, int> contents = new Dictionary<ResourceTypeInfo, int>(); Dictionary<ResourceTypeInfo, int> contents = new Dictionary<ResourceTypeInfo, int>();
[Sync] public Actor OwnerLinkedProc = null; [Sync] public Actor OwnerLinkedProc = null;
@@ -76,19 +80,20 @@ namespace OpenRA.Mods.Common.Traits
public Harvester(Actor self, HarvesterInfo info) public Harvester(Actor self, HarvesterInfo info)
{ {
this.info = info; this.info = info;
mobile = self.Trait<Mobile>();
self.QueueActivity(new CallFunc(() => ChooseNewProc(self, null))); self.QueueActivity(new CallFunc(() => ChooseNewProc(self, null)));
} }
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)
@@ -128,7 +133,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)
@@ -137,7 +142,7 @@ namespace OpenRA.Mods.Common.Traits
info.DeliveryBuildings.Contains(proc.Info.Name); info.DeliveryBuildings.Contains(proc.Info.Name);
} }
Actor ClosestProc(Actor self, Actor ignore) public Actor ClosestProc(Actor self, Actor ignore)
{ {
// Find all refineries and their occupancy count: // Find all refineries and their occupancy count:
var refs = ( var refs = (
@@ -150,14 +155,16 @@ namespace OpenRA.Mods.Common.Traits
var mi = self.Info.Traits.Get<MobileInfo>(); var mi = self.Info.Traits.Get<MobileInfo>();
var path = self.World.WorldActor.Trait<IPathFinder>().FindPath( var path = self.World.WorldActor.Trait<IPathFinder>().FindPath(
PathSearch.FromPoints(self.World, mi, self, refs.Values.Select(r => r.Location), self.Location, false) PathSearch.FromPoints(self.World, mi, self, refs.Values.Select(r => r.Location), self.Location, false)
.WithCustomCost((loc) => .WithCustomCost(loc =>
{ {
if (!refs.ContainsKey(loc)) return 0; if (!refs.ContainsKey(loc))
return 0;
var occupancy = refs[loc].Occupancy; var occupancy = refs[loc].Occupancy;
// 4 harvesters clogs up the refinery's delivery location: // 4 harvesters clogs up the refinery's delivery location:
if (occupancy >= 3) return int.MaxValue; if (occupancy >= 3)
return int.MaxValue;
// Prefer refineries with less occupancy (multiplier is to offset distance cost): // Prefer refineries with less occupancy (multiplier is to offset distance cost):
return occupancy * 12; return occupancy * 12;
@@ -189,22 +196,20 @@ namespace OpenRA.Mods.Common.Traits
if (self.Location == deliveryLoc) if (self.Location == deliveryLoc)
{ {
// Get out of the way: // Get out of the way:
var mobile = self.Trait<Mobile>(); var moveTo = LastHarvestedCell ?? (deliveryLoc + new CVec(0, 4));
var harv = self.Trait<Harvester>();
var moveTo = harv.LastHarvestedCell ?? (deliveryLoc + new CVec(0, 4));
self.QueueActivity(mobile.MoveTo(moveTo, 1)); self.QueueActivity(mobile.MoveTo(moveTo, 1));
self.SetTargetLine(Target.FromCell(self.World, moveTo), Color.Gray, false); self.SetTargetLine(Target.FromCell(self.World, moveTo), Color.Gray, false);
var territory = self.World.WorldActor.TraitOrDefault<ResourceClaimLayer>(); var territory = self.World.WorldActor.TraitOrDefault<ResourceClaimLayer>();
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;
} }
} }
@@ -219,7 +224,6 @@ namespace OpenRA.Mods.Common.Traits
if (act is Wait) if (act is Wait)
{ {
self.CancelActivity(); self.CancelActivity();
var mobile = self.Trait<Mobile>();
var cell = self.Location; var cell = self.Location;
var moveTo = mobile.NearestMoveableCell(cell, 2, 5); var moveTo = mobile.NearestMoveableCell(cell, 2, 5);
@@ -227,7 +231,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));
} }
} }
@@ -239,7 +243,7 @@ namespace OpenRA.Mods.Common.Traits
// Are we not empty? Deliver resources: // Are we not empty? Deliver resources:
if (!IsEmpty) if (!IsEmpty)
{ {
self.QueueActivity(new DeliverResources()); self.QueueActivity(new DeliverResources(self));
return; return;
} }
@@ -316,7 +320,6 @@ namespace OpenRA.Mods.Common.Traits
self.CancelActivity(); self.CancelActivity();
var mobile = self.Trait<Mobile>();
if (order.TargetLocation != CPos.Zero) if (order.TargetLocation != CPos.Zero)
{ {
var loc = order.TargetLocation; var loc = order.TargetLocation;
@@ -338,7 +341,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);
@@ -361,7 +364,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")
{ {
@@ -381,10 +384,11 @@ namespace OpenRA.Mods.Common.Traits
self.SetTargetLine(Target.FromOrder(self.World, order), Color.Green); self.SetTargetLine(Target.FromOrder(self.World, order), Color.Green);
self.CancelActivity(); self.CancelActivity();
self.QueueActivity(new DeliverResources());
var next = new DeliverResources(self);
self.QueueActivity(next);
var notify = self.TraitsImplementing<INotifyHarvesterAction>(); var notify = self.TraitsImplementing<INotifyHarvesterAction>();
var next = new DeliverResources();
foreach (var n in notify) foreach (var n in notify)
n.MovingToRefinery(self, order.TargetLocation, next); n.MovingToRefinery(self, order.TargetLocation, next);
} }
@@ -421,9 +425,9 @@ namespace OpenRA.Mods.Common.Traits
if (!harvInfo.Resources.Contains(resType.Info.Name)) if (!harvInfo.Resources.Contains(resType.Info.Name))
return Constants.CellCost; return Constants.CellCost;
// Another harvester has claimed this resource:
if (territory != null) if (territory != null)
{ {
// Another harvester has claimed this resource:
ResourceClaim claim; ResourceClaim claim;
if (territory.IsClaimedByAnyoneElse(self, loc, out claim)) if (territory.IsClaimedByAnyoneElse(self, loc, out claim))
return Constants.CellCost; return Constants.CellCost;
@@ -445,7 +449,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)