Refactor resource harvesting logic.
This commit is contained in:
@@ -30,6 +30,7 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
readonly Actor deliverActor;
|
readonly Actor deliverActor;
|
||||||
|
|
||||||
CPos? orderLocation;
|
CPos? orderLocation;
|
||||||
|
CPos? lastHarvestedCell;
|
||||||
bool hasDeliveredLoad;
|
bool hasDeliveredLoad;
|
||||||
bool hasHarvestedCell;
|
bool hasHarvestedCell;
|
||||||
bool hasWaited;
|
bool hasWaited;
|
||||||
@@ -58,7 +59,7 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
// the previous harvested cell for the initial search.
|
// the previous harvested cell for the initial search.
|
||||||
if (orderLocation != null)
|
if (orderLocation != null)
|
||||||
{
|
{
|
||||||
harv.LastHarvestedCell = orderLocation;
|
lastHarvestedCell = orderLocation;
|
||||||
|
|
||||||
// If two "harvest" orders are issued consecutively, we deliver the load first if needed.
|
// If two "harvest" orders are issued consecutively, we deliver the load first if needed.
|
||||||
// We have to make sure the actual "harvest" order is not skipped if a third order is queued,
|
// We have to make sure the actual "harvest" order is not skipped if a third order is queued,
|
||||||
@@ -114,33 +115,35 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
var closestHarvestableCell = ClosestHarvestablePos(self);
|
hasWaited = false;
|
||||||
|
|
||||||
// If no resources are found near the current field, search near the refinery instead.
|
// Scan for resources. If no resources are found near the current field, search near the refinery
|
||||||
// If that doesn't help, give up for now.
|
// instead. If that doesn't help, give up for now.
|
||||||
|
var closestHarvestableCell = ClosestHarvestablePos(self);
|
||||||
if (!closestHarvestableCell.HasValue)
|
if (!closestHarvestableCell.HasValue)
|
||||||
{
|
{
|
||||||
if (harv.LastHarvestedCell != null)
|
if (lastHarvestedCell != null)
|
||||||
{
|
{
|
||||||
harv.LastHarvestedCell = null; // Forces search from backup position.
|
lastHarvestedCell = null; // Forces search from backup position.
|
||||||
closestHarvestableCell = ClosestHarvestablePos(self);
|
closestHarvestableCell = ClosestHarvestablePos(self);
|
||||||
harv.LastSearchFailed = !closestHarvestableCell.HasValue;
|
harv.LastSearchFailed = !closestHarvestableCell.HasValue;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
harv.LastSearchFailed = true;
|
harv.LastSearchFailed = true;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
harv.LastSearchFailed = false;
|
||||||
|
|
||||||
if (harv.LastSearchFailed)
|
|
||||||
{
|
|
||||||
// If no harvestable position could be found and we are at the refinery, get out of the way
|
// If no harvestable position could be found and we are at the refinery, get out of the way
|
||||||
// of the refinery entrance.
|
// of the refinery entrance.
|
||||||
|
if (harv.LastSearchFailed)
|
||||||
|
{
|
||||||
var lastproc = harv.LastLinkedProc ?? harv.LinkedProc;
|
var lastproc = harv.LastLinkedProc ?? harv.LinkedProc;
|
||||||
if (lastproc != null && !lastproc.Disposed)
|
if (lastproc != null && !lastproc.Disposed)
|
||||||
{
|
{
|
||||||
var deliveryLoc = lastproc.Location + lastproc.Trait<IAcceptResources>().DeliveryOffset;
|
var deliveryLoc = lastproc.Location + lastproc.Trait<IAcceptResources>().DeliveryOffset;
|
||||||
if (self.Location == deliveryLoc && harv.IsEmpty)
|
if (self.Location == deliveryLoc && harv.IsEmpty)
|
||||||
{
|
{
|
||||||
// Get out of the way:
|
|
||||||
var unblockCell = deliveryLoc + harv.Info.UnblockCell;
|
var unblockCell = deliveryLoc + harv.Info.UnblockCell;
|
||||||
var moveTo = mobile.NearestMoveableCell(unblockCell, 1, 5);
|
var moveTo = mobile.NearestMoveableCell(unblockCell, 1, 5);
|
||||||
self.SetTargetLine(Target.FromCell(self.World, moveTo), Color.Green, false);
|
self.SetTargetLine(Target.FromCell(self.World, moveTo), Color.Green, false);
|
||||||
@@ -151,21 +154,9 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attempt to claim the target cell
|
// If we get here, our search for resources was successful. Commence harvesting.
|
||||||
if (!claimLayer.TryClaimCell(self, closestHarvestableCell.Value))
|
QueueChild(self, new HarvestResource(self, closestHarvestableCell.Value), true);
|
||||||
{
|
lastHarvestedCell = closestHarvestableCell.Value;
|
||||||
QueueChild(self, new Wait(25), true);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
harv.LastSearchFailed = false;
|
|
||||||
|
|
||||||
foreach (var n in self.TraitsImplementing<INotifyHarvesterAction>())
|
|
||||||
n.MovingToResources(self, closestHarvestableCell.Value, new FindAndDeliverResources(self));
|
|
||||||
|
|
||||||
self.SetTargetLine(Target.FromCell(self.World, closestHarvestableCell.Value), Color.Red, false);
|
|
||||||
QueueChild(self, mobile.MoveTo(closestHarvestableCell.Value, 1), true);
|
|
||||||
QueueChild(self, new HarvestResource(self));
|
|
||||||
hasHarvestedCell = true;
|
hasHarvestedCell = true;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -191,8 +182,8 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Determine where to search from and how far to search:
|
// Determine where to search from and how far to search:
|
||||||
var searchFromLoc = harv.LastHarvestedCell ?? GetSearchFromLocation(self);
|
var searchFromLoc = lastHarvestedCell ?? GetSearchFromLocation(self);
|
||||||
var searchRadius = harv.LastHarvestedCell.HasValue ? harvInfo.SearchFromOrderRadius : harvInfo.SearchFromProcRadius;
|
var searchRadius = lastHarvestedCell.HasValue ? harvInfo.SearchFromOrderRadius : harvInfo.SearchFromProcRadius;
|
||||||
var searchRadiusSquared = searchRadius * searchRadius;
|
var searchRadiusSquared = searchRadius * searchRadius;
|
||||||
|
|
||||||
// Find any harvestable resources:
|
// Find any harvestable resources:
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
using OpenRA.Activities;
|
using OpenRA.Activities;
|
||||||
using OpenRA.Mods.Common.Traits;
|
using OpenRA.Mods.Common.Traits;
|
||||||
|
using OpenRA.Primitives;
|
||||||
using OpenRA.Traits;
|
using OpenRA.Traits;
|
||||||
|
|
||||||
namespace OpenRA.Mods.Common.Activities
|
namespace OpenRA.Mods.Common.Activities
|
||||||
@@ -23,15 +24,27 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
readonly ResourceClaimLayer claimLayer;
|
readonly ResourceClaimLayer claimLayer;
|
||||||
readonly ResourceLayer resLayer;
|
readonly ResourceLayer resLayer;
|
||||||
readonly BodyOrientation body;
|
readonly BodyOrientation body;
|
||||||
|
readonly IMove move;
|
||||||
|
readonly CPos targetCell;
|
||||||
|
|
||||||
public HarvestResource(Actor self)
|
public HarvestResource(Actor self, CPos targetcell)
|
||||||
{
|
{
|
||||||
harv = self.Trait<Harvester>();
|
harv = self.Trait<Harvester>();
|
||||||
harvInfo = self.Info.TraitInfo<HarvesterInfo>();
|
harvInfo = self.Info.TraitInfo<HarvesterInfo>();
|
||||||
facing = self.Trait<IFacing>();
|
facing = self.Trait<IFacing>();
|
||||||
body = self.Trait<BodyOrientation>();
|
body = self.Trait<BodyOrientation>();
|
||||||
|
move = self.Trait<IMove>();
|
||||||
claimLayer = self.World.WorldActor.Trait<ResourceClaimLayer>();
|
claimLayer = self.World.WorldActor.Trait<ResourceClaimLayer>();
|
||||||
resLayer = self.World.WorldActor.Trait<ResourceLayer>();
|
resLayer = self.World.WorldActor.Trait<ResourceLayer>();
|
||||||
|
this.targetCell = targetcell;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnFirstRun(Actor self)
|
||||||
|
{
|
||||||
|
// We can safely assume the claim is successful, since this is only called in the
|
||||||
|
// same actor-tick as the targetCell is selected. Therefore no other harvester
|
||||||
|
// would have been able to claim.
|
||||||
|
claimLayer.TryClaimCell(self, targetCell);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Activity Tick(Actor self)
|
public override Activity Tick(Actor self)
|
||||||
@@ -43,19 +56,22 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IsCanceling)
|
if (IsCanceling || harv.IsFull)
|
||||||
{
|
|
||||||
claimLayer.RemoveClaim(self);
|
|
||||||
return NextActivity;
|
return NextActivity;
|
||||||
|
|
||||||
|
// Move towards the target cell
|
||||||
|
if (self.Location != targetCell)
|
||||||
|
{
|
||||||
|
foreach (var n in self.TraitsImplementing<INotifyHarvesterAction>())
|
||||||
|
n.MovingToResources(self, targetCell, new FindAndDeliverResources(self));
|
||||||
|
|
||||||
|
self.SetTargetLine(Target.FromCell(self.World, targetCell), Color.Red, false);
|
||||||
|
QueueChild(self, move.MoveTo(targetCell, 0), true);
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
harv.LastHarvestedCell = self.Location;
|
if (!harv.CanHarvestCell(self, self.Location))
|
||||||
|
|
||||||
if (harv.IsFull)
|
|
||||||
{
|
|
||||||
claimLayer.RemoveClaim(self);
|
|
||||||
return NextActivity;
|
return NextActivity;
|
||||||
}
|
|
||||||
|
|
||||||
// Turn to one of the harvestable facings
|
// Turn to one of the harvestable facings
|
||||||
if (harvInfo.HarvestFacings != 0)
|
if (harvInfo.HarvestFacings != 0)
|
||||||
@@ -71,10 +87,7 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
|
|
||||||
var resource = resLayer.Harvest(self.Location);
|
var resource = resLayer.Harvest(self.Location);
|
||||||
if (resource == null)
|
if (resource == null)
|
||||||
{
|
|
||||||
claimLayer.RemoveClaim(self);
|
|
||||||
return NextActivity;
|
return NextActivity;
|
||||||
}
|
|
||||||
|
|
||||||
harv.AcceptResource(self, resource);
|
harv.AcceptResource(self, resource);
|
||||||
|
|
||||||
@@ -84,5 +97,10 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
QueueChild(self, new Wait(harvInfo.BaleLoadDelay), true);
|
QueueChild(self, new Wait(harvInfo.BaleLoadDelay), true);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void OnLastRun(Actor self)
|
||||||
|
{
|
||||||
|
claimLayer.RemoveClaim(self);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -101,7 +101,6 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
[Sync] public Actor LastLinkedProc = null;
|
[Sync] public Actor LastLinkedProc = null;
|
||||||
[Sync] public Actor LinkedProc = null;
|
[Sync] public Actor LinkedProc = null;
|
||||||
[Sync] int currentUnloadTicks;
|
[Sync] int currentUnloadTicks;
|
||||||
public CPos? LastHarvestedCell = null;
|
|
||||||
|
|
||||||
[Sync]
|
[Sync]
|
||||||
public int ContentValue
|
public int ContentValue
|
||||||
|
|||||||
Reference in New Issue
Block a user