Abstract docking logic from Harvester and Refinery
This commit is contained in:
committed by
Matthias Mailänder
parent
da16e4ed99
commit
d0974cfdd2
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,12 +24,12 @@ namespace OpenRA.Mods.Common.Activities
|
||||
{
|
||||
protected enum DockingState { Wait, Drag, Dock, Loop, Undock, Complete }
|
||||
|
||||
protected readonly Actor RefineryActor;
|
||||
protected readonly Actor DockHostActor;
|
||||
protected readonly IDockHost DockHost;
|
||||
protected readonly WithDockingOverlay DockHostSpriteOverlay;
|
||||
protected readonly Harvester Harv;
|
||||
protected readonly DockClientManager DockClient;
|
||||
protected readonly IDockClientBody DockClientBody;
|
||||
protected readonly bool IsDragRequired;
|
||||
protected readonly WVec DragOffset;
|
||||
protected readonly int DragLength;
|
||||
protected readonly WPos StartDrag;
|
||||
protected readonly WPos EndDrag;
|
||||
@@ -41,20 +41,30 @@ namespace OpenRA.Mods.Common.Activities
|
||||
|
||||
bool dockInitiated = false;
|
||||
|
||||
public GenericDockSequence(Actor self, Actor refineryActor, Refinery refinery)
|
||||
public GenericDockSequence(Actor self, DockClientManager client, Actor hostActor, IDockHost host)
|
||||
{
|
||||
dockingState = DockingState.Drag;
|
||||
RefineryActor = refineryActor;
|
||||
DockHostSpriteOverlay = refineryActor.TraitOrDefault<WithDockingOverlay>();
|
||||
IsDragRequired = refinery.IsDragRequired;
|
||||
DragOffset = refinery.DragOffset;
|
||||
DragLength = refinery.DragLength;
|
||||
Harv = self.Trait<Harvester>();
|
||||
|
||||
DockClient = client;
|
||||
DockClientBody = self.TraitOrDefault<IDockClientBody>();
|
||||
StartDrag = self.CenterPosition;
|
||||
EndDrag = refineryActor.CenterPosition + DragOffset;
|
||||
notifyDockClients = self.TraitsImplementing<INotifyDockClient>().ToArray();
|
||||
notifyDockHosts = refineryActor.TraitsImplementing<INotifyDockHost>().ToArray();
|
||||
|
||||
DockHost = host;
|
||||
DockHostActor = hostActor;
|
||||
DockHostSpriteOverlay = hostActor.TraitOrDefault<WithDockingOverlay>();
|
||||
notifyDockHosts = hostActor.TraitsImplementing<INotifyDockHost>().ToArray();
|
||||
|
||||
if (host is IDockHostDrag sequence)
|
||||
{
|
||||
IsDragRequired = sequence.IsDragRequired;
|
||||
DragLength = sequence.DragLength;
|
||||
StartDrag = self.CenterPosition;
|
||||
EndDrag = hostActor.CenterPosition + sequence.DragOffset;
|
||||
}
|
||||
else
|
||||
IsDragRequired = false;
|
||||
|
||||
QueueChild(new Wait(host.DockWait));
|
||||
}
|
||||
|
||||
public override bool Tick(Actor self)
|
||||
@@ -65,8 +75,11 @@ namespace OpenRA.Mods.Common.Activities
|
||||
return false;
|
||||
|
||||
case DockingState.Drag:
|
||||
if (IsCanceling || !RefineryActor.IsInWorld || RefineryActor.IsDead || Harv.IsTraitDisabled)
|
||||
if (IsCanceling || DockHostActor.IsDead || !DockHostActor.IsInWorld || !DockClient.CanDockAt(DockHostActor, DockHost, false, true))
|
||||
{
|
||||
DockClient.UnreserveHost();
|
||||
return true;
|
||||
}
|
||||
|
||||
dockingState = DockingState.Dock;
|
||||
if (IsDragRequired)
|
||||
@@ -75,10 +88,12 @@ namespace OpenRA.Mods.Common.Activities
|
||||
return false;
|
||||
|
||||
case DockingState.Dock:
|
||||
if (!IsCanceling && RefineryActor.IsInWorld && !RefineryActor.IsDead && !Harv.IsTraitDisabled)
|
||||
if (!IsCanceling && !DockHostActor.IsDead && DockHostActor.IsInWorld && DockClient.CanDockAt(DockHostActor, DockHost, false, true))
|
||||
{
|
||||
dockInitiated = true;
|
||||
PlayDockAnimations(self);
|
||||
DockHost.OnDockStarted(DockHostActor, self, DockClient);
|
||||
DockClient.OnDockStarted(self, DockHostActor, DockHost);
|
||||
NotifyDocked(self);
|
||||
}
|
||||
else
|
||||
@@ -87,7 +102,7 @@ namespace OpenRA.Mods.Common.Activities
|
||||
return false;
|
||||
|
||||
case DockingState.Loop:
|
||||
if (IsCanceling || !RefineryActor.IsInWorld || RefineryActor.IsDead || Harv.IsTraitDisabled || Harv.TickUnload(self, RefineryActor))
|
||||
if (IsCanceling || DockHostActor.IsDead || !DockHostActor.IsInWorld || DockClient.OnDockTick(self, DockHostActor, DockHost))
|
||||
dockingState = DockingState.Undock;
|
||||
|
||||
return false;
|
||||
@@ -101,8 +116,8 @@ namespace OpenRA.Mods.Common.Activities
|
||||
return false;
|
||||
|
||||
case DockingState.Complete:
|
||||
Harv.LastLinkedProc = Harv.LinkedProc;
|
||||
Harv.LinkProc(null);
|
||||
DockHost.OnDockCompleted(DockHostActor, self, DockClient);
|
||||
DockClient.OnDockCompleted(self, DockHostActor, DockHost);
|
||||
NotifyUndocked(self);
|
||||
if (IsDragRequired)
|
||||
QueueChild(new Drag(self, EndDrag, StartDrag, DragLength));
|
||||
@@ -145,7 +160,7 @@ namespace OpenRA.Mods.Common.Activities
|
||||
|
||||
public virtual void PlayUndockAnimations(Actor self)
|
||||
{
|
||||
if (RefineryActor.IsInWorld && !RefineryActor.IsDead && DockHostSpriteOverlay != null && !DockHostSpriteOverlay.Visible)
|
||||
if (DockHostActor.IsInWorld && !DockHostActor.IsDead && DockHostSpriteOverlay != null && !DockHostSpriteOverlay.Visible)
|
||||
{
|
||||
dockingState = DockingState.Wait;
|
||||
DockHostSpriteOverlay.Visible = true;
|
||||
@@ -176,30 +191,30 @@ namespace OpenRA.Mods.Common.Activities
|
||||
void NotifyDocked(Actor self)
|
||||
{
|
||||
foreach (var nd in notifyDockClients)
|
||||
nd.Docked(self, RefineryActor);
|
||||
nd.Docked(self, DockHostActor);
|
||||
|
||||
foreach (var nd in notifyDockHosts)
|
||||
nd.Docked(RefineryActor, self);
|
||||
nd.Docked(DockHostActor, self);
|
||||
}
|
||||
|
||||
void NotifyUndocked(Actor self)
|
||||
{
|
||||
foreach (var nd in notifyDockClients)
|
||||
nd.Undocked(self, RefineryActor);
|
||||
nd.Undocked(self, DockHostActor);
|
||||
|
||||
if (RefineryActor.IsInWorld && !RefineryActor.IsDead)
|
||||
if (DockHostActor.IsInWorld && !DockHostActor.IsDead)
|
||||
foreach (var nd in notifyDockHosts)
|
||||
nd.Undocked(RefineryActor, self);
|
||||
nd.Undocked(DockHostActor, self);
|
||||
}
|
||||
|
||||
public override IEnumerable<Target> GetTargets(Actor self)
|
||||
{
|
||||
yield return Target.FromActor(RefineryActor);
|
||||
yield return Target.FromActor(DockHostActor);
|
||||
}
|
||||
|
||||
public override IEnumerable<TargetLineNode> TargetLineNodes(Actor self)
|
||||
{
|
||||
yield return new TargetLineNode(Target.FromActor(RefineryActor), Color.Green);
|
||||
yield return new TargetLineNode(Target.FromActor(DockHostActor), Color.Green);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,66 +19,71 @@ namespace OpenRA.Mods.Common.Activities
|
||||
{
|
||||
public class MoveToDock : Activity
|
||||
{
|
||||
readonly IMove movement;
|
||||
readonly Harvester harv;
|
||||
readonly Actor targetActor;
|
||||
readonly DockClientManager dockClient;
|
||||
Actor dockHostActor;
|
||||
IDockHost dockHost;
|
||||
readonly INotifyHarvesterAction[] notifyHarvesterActions;
|
||||
|
||||
Actor proc;
|
||||
|
||||
public MoveToDock(Actor self, Actor targetActor = null)
|
||||
public MoveToDock(Actor self, Actor dockHostActor = null, IDockHost dockHost = null)
|
||||
{
|
||||
movement = self.Trait<IMove>();
|
||||
harv = self.Trait<Harvester>();
|
||||
this.targetActor = targetActor;
|
||||
dockClient = self.Trait<DockClientManager>();
|
||||
this.dockHostActor = dockHostActor;
|
||||
this.dockHost = dockHost;
|
||||
notifyHarvesterActions = self.TraitsImplementing<INotifyHarvesterAction>().ToArray();
|
||||
}
|
||||
|
||||
protected override void OnFirstRun(Actor self)
|
||||
{
|
||||
if (targetActor != null && targetActor.IsInWorld)
|
||||
harv.LinkProc(targetActor);
|
||||
}
|
||||
|
||||
public override bool Tick(Actor self)
|
||||
{
|
||||
if (harv.IsTraitDisabled)
|
||||
Cancel(self, true);
|
||||
|
||||
if (IsCanceling)
|
||||
return true;
|
||||
|
||||
// Find the nearest best refinery if not explicitly ordered to a specific refinery:
|
||||
if (harv.LinkedProc == null || !harv.LinkedProc.IsInWorld)
|
||||
harv.ChooseNewProc(self, null);
|
||||
|
||||
// No refineries exist; check again after delay defined in Harvester.
|
||||
if (harv.LinkedProc == null)
|
||||
if (dockClient.IsTraitDisabled)
|
||||
{
|
||||
QueueChild(new Wait(harv.Info.SearchForDeliveryBuildingDelay));
|
||||
return false;
|
||||
Cancel(self, true);
|
||||
return true;
|
||||
}
|
||||
|
||||
proc = harv.LinkedProc;
|
||||
var iao = proc.Trait<IAcceptResources>();
|
||||
|
||||
if (self.CenterPosition != iao.DeliveryPosition)
|
||||
// Find the nearest DockHost if not explicitly ordered to a specific dock.
|
||||
if (dockHost == null || !dockHost.IsEnabledAndInWorld)
|
||||
{
|
||||
foreach (var n in notifyHarvesterActions)
|
||||
n.MovingToRefinery(self, proc);
|
||||
|
||||
var target = Target.FromActor(proc);
|
||||
QueueChild(movement.MoveOntoTarget(self, target, iao.DeliveryPosition - proc.CenterPosition, iao.DeliveryAngle));
|
||||
return false;
|
||||
var host = dockClient.ClosestDock(null);
|
||||
if (host.HasValue)
|
||||
{
|
||||
dockHost = host.Value.Trait;
|
||||
dockHostActor = host.Value.Actor;
|
||||
}
|
||||
else
|
||||
{
|
||||
// No docks exist; check again after delay defined in dockClient.
|
||||
QueueChild(new Wait(dockClient.Info.SearchForDockDelay));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
QueueChild(new Wait(10));
|
||||
iao.OnDock(self, this);
|
||||
return true;
|
||||
if (dockClient.ReserveHost(dockHostActor, dockHost))
|
||||
{
|
||||
if (dockHost.QueueMoveActivity(this, dockHostActor, self, dockClient))
|
||||
{
|
||||
foreach (var n in notifyHarvesterActions)
|
||||
n.MovingToRefinery(self, dockHostActor);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
dockHost.QueueDockActivity(this, dockHostActor, self, dockClient);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// The dock explicitely chosen by the user is currently occupied. Wait and check again.
|
||||
QueueChild(new Wait(dockClient.Info.SearchForDockDelay));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Cancel(Actor self, bool keepQueue = false)
|
||||
{
|
||||
dockClient.UnreserveHost();
|
||||
foreach (var n in notifyHarvesterActions)
|
||||
n.MovementCancelled(self);
|
||||
|
||||
@@ -87,10 +92,13 @@ namespace OpenRA.Mods.Common.Activities
|
||||
|
||||
public override IEnumerable<TargetLineNode> TargetLineNodes(Actor self)
|
||||
{
|
||||
if (proc != null)
|
||||
yield return new TargetLineNode(Target.FromActor(proc), harv.Info.DeliverLineColor);
|
||||
if (dockHostActor != null)
|
||||
yield return new TargetLineNode(Target.FromActor(dockHostActor), dockClient.DockLineColor);
|
||||
else
|
||||
yield return new TargetLineNode(Target.FromActor(harv.LinkedProc), harv.Info.DeliverLineColor);
|
||||
{
|
||||
if (dockClient.ReservedHostActor != null)
|
||||
yield return new TargetLineNode(Target.FromActor(dockClient.ReservedHostActor), dockClient.DockLineColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user