Abstract docking logic from Harvester and Refinery

This commit is contained in:
Gustas
2023-01-24 19:33:42 +02:00
committed by Matthias Mailänder
parent da16e4ed99
commit d0974cfdd2
22 changed files with 1016 additions and 358 deletions

View File

@@ -24,8 +24,6 @@ namespace OpenRA.Mods.Common.Activities
readonly HarvesterInfo harvInfo;
readonly Mobile mobile;
readonly ResourceClaimLayer claimLayer;
Actor deliverActor;
CPos? orderLocation;
CPos? lastHarvestedCell;
bool hasDeliveredLoad;
@@ -34,19 +32,14 @@ namespace OpenRA.Mods.Common.Activities
public bool LastSearchFailed { get; private set; }
public FindAndDeliverResources(Actor self, Actor deliverActor = null)
public FindAndDeliverResources(Actor self, CPos? orderLocation = null)
{
harv = self.Trait<Harvester>();
harvInfo = self.Info.TraitInfo<HarvesterInfo>();
mobile = self.Trait<Mobile>();
claimLayer = self.World.WorldActor.Trait<ResourceClaimLayer>();
this.deliverActor = deliverActor;
}
public FindAndDeliverResources(Actor self, CPos orderLocation)
: this(self, null)
{
this.orderLocation = orderLocation;
if (orderLocation.HasValue)
this.orderLocation = orderLocation.Value;
}
protected override void OnFirstRun(Actor self)
@@ -63,14 +56,6 @@ namespace OpenRA.Mods.Common.Activities
if (harv.IsFull)
QueueChild(new MoveToDock(self));
}
// If an explicit "deliver" order is given, the harvester goes immediately to the refinery.
if (deliverActor != null)
{
QueueChild(new MoveToDock(self, deliverActor));
hasDeliveredLoad = true;
deliverActor = null;
}
}
public override bool Tick(Actor self)
@@ -92,9 +77,12 @@ namespace OpenRA.Mods.Common.Activities
// Are we full or have nothing more to gather? Deliver resources.
if (harv.IsFull || (!harv.IsEmpty && LastSearchFailed))
{
// If we are reserved it means docking was already initiated and we should wait.
if (harv.DockClientManager.ReservedHost != null)
return false;
QueueChild(new MoveToDock(self));
hasDeliveredLoad = true;
return false;
}
// After a failed search, wait and sit still for a bit before searching again.
@@ -128,13 +116,13 @@ namespace OpenRA.Mods.Common.Activities
// of the refinery entrance.
if (LastSearchFailed)
{
var lastproc = harv.LastLinkedProc ?? harv.LinkedProc;
if (lastproc != null && !lastproc.Disposed)
var lastproc = harv.DockClientManager.LastReservedHost;
if (lastproc != null)
{
var deliveryLoc = lastproc.Trait<IAcceptResources>().DeliveryPosition;
if (self.CenterPosition == deliveryLoc && harv.IsEmpty)
var deliveryLoc = self.World.Map.CellContaining(lastproc.DockPosition);
if (self.Location == deliveryLoc && harv.IsEmpty)
{
var unblockCell = self.World.Map.CellContaining(deliveryLoc) + harv.Info.UnblockCell;
var unblockCell = deliveryLoc + harv.Info.UnblockCell;
var moveTo = mobile.NearestMoveableCell(unblockCell, 1, 5);
QueueChild(mobile.MoveTo(moveTo, 1));
}
@@ -171,14 +159,31 @@ namespace OpenRA.Mods.Common.Activities
}
// Determine where to search from and how far to search:
var procLoc = GetSearchFromProcLocation();
var searchFromLoc = lastHarvestedCell ?? procLoc ?? self.Location;
var searchRadius = lastHarvestedCell.HasValue ? harvInfo.SearchFromHarvesterRadius : harvInfo.SearchFromProcRadius;
// Prioritise search by these locations in this order: lastHarvestedCell -> lastLinkedDock -> self.
CPos searchFromLoc;
int searchRadius;
WPos? dockPos = null;
if (lastHarvestedCell.HasValue)
{
searchRadius = harvInfo.SearchFromHarvesterRadius;
searchFromLoc = lastHarvestedCell.Value;
}
else
{
searchRadius = harvInfo.SearchFromProcRadius;
var dock = harv.DockClientManager.LastReservedHost;
if (dock != null)
{
dockPos = dock.DockPosition;
searchFromLoc = self.World.Map.CellContaining(dockPos.Value);
}
else
searchFromLoc = self.Location;
}
var searchRadiusSquared = searchRadius * searchRadius;
var map = self.World.Map;
var procPos = procLoc.HasValue ? (WPos?)map.CenterOfCell(procLoc.Value) : null;
var harvPos = self.CenterPosition;
// Find any harvestable resources:
@@ -196,19 +201,19 @@ namespace OpenRA.Mods.Common.Activities
// Add a cost modifier to harvestable cells to prefer resources that are closer to the refinery.
// This reduces the tendency for harvesters to move in straight lines
if (procPos.HasValue && harvInfo.ResourceRefineryDirectionPenalty > 0 && harv.CanHarvestCell(loc))
if (dockPos.HasValue && harvInfo.ResourceRefineryDirectionPenalty > 0 && harv.CanHarvestCell(loc))
{
var pos = map.CenterOfCell(loc);
// Calculate harv-cell-refinery angle (cosine rule)
var b = pos - procPos.Value;
var b = pos - dockPos.Value;
if (b != WVec.Zero)
{
var c = pos - harvPos;
if (c != WVec.Zero)
{
var a = harvPos - procPos.Value;
var a = harvPos - dockPos.Value;
var cosA = (int)(512 * (b.LengthSquared + c.LengthSquared - a.LengthSquared) / b.Length / c.Length);
// Cost modifier varies between 0 and ResourceRefineryDirectionPenalty
@@ -239,19 +244,12 @@ namespace OpenRA.Mods.Common.Activities
if (orderLocation != null)
yield return new TargetLineNode(Target.FromCell(self.World, orderLocation.Value), harvInfo.HarvestLineColor);
else if (deliverActor != null)
yield return new TargetLineNode(Target.FromActor(deliverActor), harvInfo.DeliverLineColor);
}
CPos? GetSearchFromProcLocation()
{
if (harv.LastLinkedProc != null && !harv.LastLinkedProc.IsDead && harv.LastLinkedProc.IsInWorld)
return harv.LastLinkedProc.World.Map.CellContaining(harv.LastLinkedProc.Trait<IAcceptResources>().DeliveryPosition);
if (harv.LinkedProc != null && !harv.LinkedProc.IsDead && harv.LinkedProc.IsInWorld)
return harv.LinkedProc.World.Map.CellContaining(harv.LinkedProc.Trait<IAcceptResources>().DeliveryPosition);
return null;
else
{
var manager = harv.DockClientManager;
if (manager.ReservedHostActor != null)
yield return new TargetLineNode(Target.FromActor(manager.ReservedHostActor), manager.DockLineColor);
}
}
}
}