Abstract docking logic from Harvester and Refinery
This commit is contained in:
committed by
Matthias Mailänder
parent
da16e4ed99
commit
d0974cfdd2
@@ -12,32 +12,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Activities;
|
||||
using OpenRA.Mods.Common.Activities;
|
||||
using OpenRA.Mods.Common.Effects;
|
||||
using OpenRA.Mods.Common.Traits.Render;
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
public class RefineryInfo : TraitInfo, Requires<WithSpriteBodyInfo>, IAcceptResourcesInfo
|
||||
public class RefineryInfo : TraitInfo, Requires<WithSpriteBodyInfo>, Requires<IDockHostInfo>
|
||||
{
|
||||
[Desc("Actual harvester facing when docking.")]
|
||||
public readonly WAngle DockAngle = WAngle.Zero;
|
||||
|
||||
[Desc("Docking cell relative to top-left cell.")]
|
||||
public readonly CVec DockOffset = CVec.Zero;
|
||||
|
||||
[Desc("Does the refinery require the harvester to be dragged in?")]
|
||||
public readonly bool IsDragRequired = false;
|
||||
|
||||
[Desc("Vector by which the harvester will be dragged when docking.")]
|
||||
public readonly WVec DragOffset = WVec.Zero;
|
||||
|
||||
[Desc("In how many steps to perform the dragging?")]
|
||||
public readonly int DragLength = 0;
|
||||
|
||||
[Desc("Store resources in silos. Adds cash directly without storing if set to false.")]
|
||||
public readonly bool UseStorage = true;
|
||||
|
||||
@@ -50,33 +32,16 @@ namespace OpenRA.Mods.Common.Traits
|
||||
public override object Create(ActorInitializer init) { return new Refinery(init.Self, this); }
|
||||
}
|
||||
|
||||
public class Refinery : INotifyCreated, ITick, IAcceptResources, INotifySold, INotifyCapture,
|
||||
INotifyOwnerChanged, ISync, INotifyActorDisposing
|
||||
public class Refinery : IAcceptResources, INotifyCreated, ITick, INotifyOwnerChanged
|
||||
{
|
||||
readonly Actor self;
|
||||
readonly RefineryInfo info;
|
||||
PlayerResources playerResources;
|
||||
IEnumerable<int> resourceValueModifiers;
|
||||
|
||||
int currentDisplayTick = 0;
|
||||
int currentDisplayValue = 0;
|
||||
|
||||
[Sync]
|
||||
Actor dockedHarv = null;
|
||||
|
||||
[Sync]
|
||||
bool preventDock = false;
|
||||
|
||||
public bool AllowDocking => !preventDock;
|
||||
public WPos DeliveryPosition => self.World.Map.CenterOfCell(self.Location + info.DockOffset);
|
||||
public WAngle DeliveryAngle => info.DockAngle;
|
||||
public bool IsDragRequired => info.IsDragRequired;
|
||||
public WVec DragOffset => info.DragOffset;
|
||||
public int DragLength => info.DragLength;
|
||||
|
||||
public Refinery(Actor self, RefineryInfo info)
|
||||
{
|
||||
this.self = self;
|
||||
this.info = info;
|
||||
playerResources = self.Owner.PlayerActor.Trait<PlayerResources>();
|
||||
currentDisplayTick = info.TickRate;
|
||||
@@ -87,13 +52,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
resourceValueModifiers = self.TraitsImplementing<IResourceValueModifier>().ToArray().Select(m => m.GetResourceValueModifier());
|
||||
}
|
||||
|
||||
public IEnumerable<TraitPair<Harvester>> GetLinkedHarvesters()
|
||||
{
|
||||
return self.World.ActorsWithTrait<Harvester>()
|
||||
.Where(a => a.Trait.LinkedProc == self);
|
||||
}
|
||||
|
||||
int IAcceptResources.AcceptResources(string resourceType, int count)
|
||||
int IAcceptResources.AcceptResources(Actor self, string resourceType, int count)
|
||||
{
|
||||
if (!playerResources.Info.ResourceValues.TryGetValue(resourceType, out var resourceValue))
|
||||
return 0;
|
||||
@@ -131,17 +90,8 @@ namespace OpenRA.Mods.Common.Traits
|
||||
return count;
|
||||
}
|
||||
|
||||
void CancelDock()
|
||||
{
|
||||
preventDock = true;
|
||||
}
|
||||
|
||||
void ITick.Tick(Actor self)
|
||||
{
|
||||
// Harvester was killed while unloading
|
||||
if (dockedHarv != null && dockedHarv.IsDead)
|
||||
dockedHarv = null;
|
||||
|
||||
if (info.ShowTicks && currentDisplayValue > 0 && --currentDisplayTick <= 0)
|
||||
{
|
||||
var temp = currentDisplayValue;
|
||||
@@ -152,49 +102,9 @@ namespace OpenRA.Mods.Common.Traits
|
||||
}
|
||||
}
|
||||
|
||||
void INotifyActorDisposing.Disposing(Actor self)
|
||||
{
|
||||
CancelDock();
|
||||
foreach (var harv in GetLinkedHarvesters())
|
||||
harv.Trait.UnlinkProc(harv.Actor, self);
|
||||
}
|
||||
|
||||
public void OnDock(Actor harv, MoveToDock dockOrder)
|
||||
{
|
||||
if (!preventDock)
|
||||
{
|
||||
dockOrder.QueueChild(new CallFunc(() => dockedHarv = harv, false));
|
||||
dockOrder.QueueChild(new HarvesterDockSequence(harv, self, this));
|
||||
dockOrder.QueueChild(new CallFunc(() => dockedHarv = null, false));
|
||||
}
|
||||
}
|
||||
|
||||
void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)
|
||||
{
|
||||
// Unlink any harvesters
|
||||
foreach (var harv in GetLinkedHarvesters())
|
||||
harv.Trait.UnlinkProc(harv.Actor, self);
|
||||
|
||||
playerResources = newOwner.PlayerActor.Trait<PlayerResources>();
|
||||
}
|
||||
|
||||
void INotifyCapture.OnCapture(Actor self, Actor captor, Player oldOwner, Player newOwner, BitSet<CaptureType> captureTypes)
|
||||
{
|
||||
// Steal any docked harv too
|
||||
if (dockedHarv != null)
|
||||
{
|
||||
dockedHarv.ChangeOwner(newOwner);
|
||||
|
||||
// Relink to this refinery
|
||||
dockedHarv.Trait<Harvester>().LinkProc(self);
|
||||
}
|
||||
}
|
||||
|
||||
void INotifySold.Selling(Actor self) { CancelDock(); }
|
||||
void INotifySold.Sold(Actor self)
|
||||
{
|
||||
foreach (var harv in GetLinkedHarvesters())
|
||||
harv.Trait.UnlinkProc(harv.Actor, self);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,10 +36,9 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
void INotifyHarvesterAction.MovingToRefinery(Actor self, Actor refineryActor)
|
||||
{
|
||||
var iao = refineryActor.Trait<IAcceptResources>();
|
||||
var location = self.World.Map.CellContaining(iao.DeliveryPosition);
|
||||
var dock = refineryActor.Trait<IDockHost>();
|
||||
foreach (var t in transports)
|
||||
t.RequestTransport(self, location);
|
||||
t.RequestTransport(self, self.World.Map.CellContaining(dock.DockPosition));
|
||||
}
|
||||
|
||||
void INotifyHarvesterAction.MovementCancelled(Actor self)
|
||||
|
||||
54
OpenRA.Mods.Common/Traits/DockClientBase.cs
Normal file
54
OpenRA.Mods.Common/Traits/DockClientBase.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright (c) The OpenRA Developers and Contributors
|
||||
* 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, either version 3 of
|
||||
* the License, or (at your option) any later version. For more
|
||||
* information, see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
public abstract class DockClientBaseInfo : ConditionalTraitInfo, IDockClientInfo, Requires<DockClientManagerInfo> { }
|
||||
|
||||
public abstract class DockClientBase<InfoType> : ConditionalTrait<InfoType>, IDockClient, INotifyCreated where InfoType : DockClientBaseInfo
|
||||
{
|
||||
readonly Actor self;
|
||||
|
||||
public abstract BitSet<DockType> GetDockType { get; }
|
||||
public DockClientManager DockClientManager { get; }
|
||||
|
||||
protected DockClientBase(Actor self, InfoType info)
|
||||
: base(info)
|
||||
{
|
||||
this.self = self;
|
||||
DockClientManager = self.Trait<DockClientManager>();
|
||||
}
|
||||
|
||||
protected virtual bool CanDock()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual bool IsDockingPossible(BitSet<DockType> type, bool forceEnter = false)
|
||||
{
|
||||
return !IsTraitDisabled && GetDockType.Overlaps(type) && (forceEnter || CanDock());
|
||||
}
|
||||
|
||||
public virtual bool CanDockAt(Actor hostActor, IDockHost host, bool forceEnter = false, bool ignoreOccupancy = false)
|
||||
{
|
||||
return (forceEnter || self.Owner.IsAlliedWith(hostActor.Owner)) && IsDockingPossible(host.GetDockType, forceEnter) && host.IsDockingPossible(self, this, ignoreOccupancy);
|
||||
}
|
||||
|
||||
public virtual void OnDockStarted(Actor self, Actor hostActor, IDockHost host) { }
|
||||
|
||||
public virtual bool OnDockTick(Actor self, Actor hostActor, IDockHost host) { return false; }
|
||||
|
||||
public virtual void OnDockCompleted(Actor self, Actor hostActor, IDockHost host) { }
|
||||
}
|
||||
}
|
||||
334
OpenRA.Mods.Common/Traits/DockClientManager.cs
Normal file
334
OpenRA.Mods.Common/Traits/DockClientManager.cs
Normal file
@@ -0,0 +1,334 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright (c) The OpenRA Developers and Contributors
|
||||
* 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, either version 3 of
|
||||
* the License, or (at your option) any later version. For more
|
||||
* information, see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Mods.Common.Activities;
|
||||
using OpenRA.Mods.Common.Orders;
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
[Desc("Manages DockClients on the actor.")]
|
||||
public class DockClientManagerInfo : ConditionalTraitInfo
|
||||
{
|
||||
[Desc("How long (in ticks) to wait until (re-)checking for a nearby available DockHost.")]
|
||||
public readonly int SearchForDockDelay = 125;
|
||||
|
||||
[Desc("The pathfinding cost penalty applied for each dock client waiting to unload at a DockHost.")]
|
||||
public readonly int OccupancyCostModifier = 12;
|
||||
|
||||
[CursorReference]
|
||||
[Desc("Cursor to display when able to dock at target actor.")]
|
||||
public readonly string EnterCursor = "enter";
|
||||
|
||||
[CursorReference]
|
||||
[Desc("Cursor to display when unable to dock at target actor.")]
|
||||
public readonly string EnterBlockedCursor = "enter-blocked";
|
||||
|
||||
[VoiceReference]
|
||||
[Desc("Voice to be played when ordered to dock.")]
|
||||
public readonly string Voice = "Action";
|
||||
|
||||
[Desc("Color to use for the target line of docking orders.")]
|
||||
public readonly Color DockLineColor = Color.Green;
|
||||
|
||||
public override object Create(ActorInitializer init) { return new DockClientManager(init.Self, this); }
|
||||
}
|
||||
|
||||
public class DockClientManager : ConditionalTrait<DockClientManagerInfo>, IResolveOrder, IOrderVoice, IIssueOrder, INotifyKilled, INotifyActorDisposing
|
||||
{
|
||||
readonly Actor self;
|
||||
protected IDockClient[] dockClients;
|
||||
public Color DockLineColor => Info.DockLineColor;
|
||||
public int OccupancyCostModifier => Info.OccupancyCostModifier;
|
||||
|
||||
public DockClientManager(Actor self, DockClientManagerInfo info)
|
||||
: base(info)
|
||||
{
|
||||
this.self = self;
|
||||
}
|
||||
|
||||
protected override void Created(Actor self)
|
||||
{
|
||||
base.Created(self);
|
||||
dockClients = self.TraitsImplementing<IDockClient>().ToArray();
|
||||
}
|
||||
|
||||
public Actor ReservedHostActor { get; protected set; }
|
||||
public IDockHost ReservedHost { get; protected set; }
|
||||
|
||||
IDockHost lastReservedDockHost = null;
|
||||
public IDockHost LastReservedHost
|
||||
{
|
||||
get
|
||||
{
|
||||
if (lastReservedDockHost != null)
|
||||
{
|
||||
if (!lastReservedDockHost.IsEnabledAndInWorld)
|
||||
lastReservedDockHost = null;
|
||||
else
|
||||
return lastReservedDockHost;
|
||||
}
|
||||
|
||||
return ReservedHost;
|
||||
}
|
||||
}
|
||||
|
||||
public void UnreserveHost()
|
||||
{
|
||||
if (ReservedHost != null)
|
||||
{
|
||||
lastReservedDockHost = ReservedHost;
|
||||
ReservedHost = null;
|
||||
ReservedHostActor = null;
|
||||
lastReservedDockHost.Unreserve(this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>In addition returns true if reservation was succesful or we have already been reserved at <paramref name="host"/>.</summary>
|
||||
public bool ReserveHost(Actor hostActor, IDockHost host)
|
||||
{
|
||||
if (host == null)
|
||||
return false;
|
||||
|
||||
if (ReservedHost == host)
|
||||
return true;
|
||||
|
||||
UnreserveHost();
|
||||
if (host.Reserve(hostActor, this))
|
||||
{
|
||||
ReservedHost = host;
|
||||
ReservedHostActor = hostActor;
|
||||
|
||||
// After we have reserved a new Host we want to forget our old host.
|
||||
lastReservedDockHost = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void OnDockStarted(Actor self, Actor hostActor, IDockHost host)
|
||||
{
|
||||
foreach (var client in dockClients)
|
||||
client.OnDockStarted(self, hostActor, host);
|
||||
}
|
||||
|
||||
public bool OnDockTick(Actor self, Actor hostActor, IDockHost host)
|
||||
{
|
||||
if (IsTraitDisabled)
|
||||
return true;
|
||||
|
||||
var cancel = true;
|
||||
foreach (var client in dockClients)
|
||||
if (!client.OnDockTick(self, hostActor, host))
|
||||
cancel = false;
|
||||
|
||||
return cancel;
|
||||
}
|
||||
|
||||
public void OnDockCompleted(Actor self, Actor hostActor, IDockHost host)
|
||||
{
|
||||
foreach (var client in dockClients)
|
||||
client.OnDockCompleted(self, hostActor, host);
|
||||
|
||||
UnreserveHost();
|
||||
}
|
||||
|
||||
IEnumerable<IOrderTargeter> IIssueOrder.Orders
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return new EnterAlliedActorTargeter<IDockHostInfo>(
|
||||
"ForceDock",
|
||||
6,
|
||||
Info.EnterCursor,
|
||||
Info.EnterBlockedCursor,
|
||||
DockingPossible,
|
||||
target => CanDockAt(target, true, true));
|
||||
yield return new EnterAlliedActorTargeter<IDockHostInfo>(
|
||||
"Dock",
|
||||
5,
|
||||
Info.EnterCursor,
|
||||
Info.EnterBlockedCursor,
|
||||
(actor, modifiers) => DockingPossible(actor),
|
||||
target => CanDockAt(target, false, true));
|
||||
}
|
||||
}
|
||||
|
||||
void IResolveOrder.ResolveOrder(Actor self, Order order)
|
||||
{
|
||||
if (order.OrderString == "Dock")
|
||||
{
|
||||
var target = order.Target;
|
||||
|
||||
// Deliver orders are only valid for own/allied actors,
|
||||
// which are guaranteed to never be frozen.
|
||||
// TODO: support frozen actors
|
||||
if (target.Type != TargetType.Actor)
|
||||
return;
|
||||
|
||||
if (IsTraitDisabled)
|
||||
return;
|
||||
|
||||
var dock = AvailableDockHosts(target.Actor, false, true).ClosestDock(self, this);
|
||||
if (!dock.HasValue)
|
||||
return;
|
||||
|
||||
self.QueueActivity(order.Queued, new MoveToDock(self, dock.Value.Actor, dock.Value.Trait));
|
||||
self.ShowTargetLines();
|
||||
}
|
||||
else if (order.OrderString == "ForceDock")
|
||||
{
|
||||
var target = order.Target;
|
||||
|
||||
// Deliver orders are only valid for own/allied actors,
|
||||
// which are guaranteed to never be frozen.
|
||||
// TODO: support frozen actors
|
||||
if (target.Type != TargetType.Actor)
|
||||
return;
|
||||
|
||||
if (IsTraitDisabled)
|
||||
return;
|
||||
|
||||
var dock = AvailableDockHosts(target.Actor, true, true).ClosestDock(self, this);
|
||||
if (!dock.HasValue)
|
||||
return;
|
||||
|
||||
self.QueueActivity(order.Queued, new MoveToDock(self, dock.Value.Actor, dock.Value.Trait));
|
||||
self.ShowTargetLines();
|
||||
}
|
||||
}
|
||||
|
||||
string IOrderVoice.VoicePhraseForOrder(Actor self, Order order)
|
||||
{
|
||||
if (order.Target.Type != TargetType.Actor || IsTraitDisabled)
|
||||
return null;
|
||||
|
||||
if (order.OrderString == "Dock" && CanDockAt(order.Target.Actor, false, true))
|
||||
return Info.Voice;
|
||||
else if (order.OrderString == "ForceDock" && CanDockAt(order.Target.Actor, true, true))
|
||||
return Info.Voice;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Order IIssueOrder.IssueOrder(Actor self, IOrderTargeter order, in Target target, bool queued)
|
||||
{
|
||||
if (order.OrderID == "Dock" || order.OrderID == "ForceDock")
|
||||
return new Order(order.OrderID, self, target, queued);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>Do we have an enabled client with matching <paramref name="type"/>.</summary>
|
||||
public bool DockingPossible(BitSet<DockType> type, bool forceEnter = false)
|
||||
{
|
||||
return !IsTraitDisabled && dockClients.Any(client => client.IsDockingPossible(type, forceEnter));
|
||||
}
|
||||
|
||||
/// <summary>Does this <paramref name="target"/> contain at least one enabled <see cref="IDockHost"/> with maching <see cref="DockType"/>.</summary>
|
||||
public bool DockingPossible(Actor target)
|
||||
{
|
||||
return !IsTraitDisabled && target.TraitsImplementing<IDockHost>().Any(host => dockClients.Any(client => client.IsDockingPossible(host.GetDockType)));
|
||||
}
|
||||
|
||||
/// <summary>Does this <paramref name="target"/> contain at least one enabled <see cref="IDockHost"/> with maching <see cref="DockType"/>.</summary>
|
||||
public bool DockingPossible(Actor target, TargetModifiers modifiers)
|
||||
{
|
||||
var forceEnter = modifiers.HasModifier(TargetModifiers.ForceMove);
|
||||
return !IsTraitDisabled && target.TraitsImplementing<IDockHost>().Any(host => dockClients.Any(client => client.IsDockingPossible(host.GetDockType, forceEnter)));
|
||||
}
|
||||
|
||||
/// <summary>Can we dock to this <paramref name="host"/>.</summary>
|
||||
public bool CanDockAt(Actor hostActor, IDockHost host, bool forceEnter = false, bool ignoreOccupancy = false)
|
||||
{
|
||||
return !IsTraitDisabled && dockClients.Any(client => client.CanDockAt(hostActor, host, forceEnter, ignoreOccupancy));
|
||||
}
|
||||
|
||||
/// <summary>Can we dock to this <paramref name="target"/>.</summary>
|
||||
public bool CanDockAt(Actor target, bool forceEnter = false, bool ignoreOccupancy = false)
|
||||
{
|
||||
return !IsTraitDisabled && target.TraitsImplementing<IDockHost>().Any(host => dockClients.Any(client => client.CanDockAt(target, host, forceEnter, ignoreOccupancy)));
|
||||
}
|
||||
|
||||
/// <summary>Find the closest viable <see cref="IDockHost"/>.</summary>
|
||||
/// <remarks>If <paramref name="type"/> is not set, scans all clients. Does not check if <see cref="DockClientManager"/> is enabled.</remarks>
|
||||
public TraitPair<IDockHost>? ClosestDock(IDockHost ignore, BitSet<DockType> type = default, bool forceEnter = false, bool ignoreOccupancy = false)
|
||||
{
|
||||
var clients = type.IsEmpty ? dockClients : AvailableDockClients(type);
|
||||
return self.World.ActorsWithTrait<IDockHost>()
|
||||
.Where(host => host.Trait != ignore && clients.Any(client => client.CanDockAt(host.Actor, host.Trait, forceEnter, ignoreOccupancy)))
|
||||
.ClosestDock(self, this);
|
||||
}
|
||||
|
||||
/// <summary>Get viable <see cref="IDockHost"/>'s on the <paramref name="target"/>.</summary>
|
||||
/// <remarks>Does not check if <see cref="DockClientManager"/> is enabled.</remarks>
|
||||
public IEnumerable<TraitPair<IDockHost>> AvailableDockHosts(Actor target, bool forceEnter = false, bool ignoreOccupancy = false)
|
||||
{
|
||||
return target.TraitsImplementing<IDockHost>()
|
||||
.Where(host => dockClients.Any(client => client.CanDockAt(target, host, forceEnter, ignoreOccupancy)))
|
||||
.Select(host => new TraitPair<IDockHost>(target, host));
|
||||
}
|
||||
|
||||
/// <summary>Get clients of matching <paramref name="type"/>.</summary>
|
||||
/// <remarks>Does not check if <see cref="DockClientManager"/> is enabled.</remarks>
|
||||
public IEnumerable<IDockClient> AvailableDockClients(BitSet<DockType> type)
|
||||
{
|
||||
return dockClients.Where(client => client.IsDockingPossible(type));
|
||||
}
|
||||
|
||||
void INotifyKilled.Killed(Actor self, AttackInfo e) { UnreserveHost(); }
|
||||
|
||||
void INotifyActorDisposing.Disposing(Actor self) { UnreserveHost(); }
|
||||
}
|
||||
|
||||
public static class DockExts
|
||||
{
|
||||
public static TraitPair<IDockHost>? ClosestDock(this IEnumerable<TraitPair<IDockHost>> docks, Actor clientActor, DockClientManager client)
|
||||
{
|
||||
var mobile = clientActor.TraitOrDefault<Mobile>();
|
||||
if (mobile != null)
|
||||
{
|
||||
// Overlapping docks can become hidden.
|
||||
var lookup = docks.ToDictionary(dock => clientActor.World.Map.CellContaining(dock.Trait.DockPosition));
|
||||
|
||||
// Start a search from each docks position:
|
||||
var path = mobile.PathFinder.FindPathToTargetCell(
|
||||
clientActor, lookup.Keys, clientActor.Location, BlockedByActor.None,
|
||||
location =>
|
||||
{
|
||||
if (!lookup.ContainsKey(location))
|
||||
return 0;
|
||||
|
||||
var dock = lookup[location];
|
||||
|
||||
// Prefer docks with less occupancy (multiplier is to offset distance cost):
|
||||
// TODO: add custom wieghts. E.g. owner vs allied.
|
||||
return dock.Trait.ReservationCount * client.OccupancyCostModifier;
|
||||
});
|
||||
|
||||
if (path.Count > 0)
|
||||
return lookup[path.Last()];
|
||||
}
|
||||
else
|
||||
{
|
||||
return docks
|
||||
.OrderBy(dock => (clientActor.Location - clientActor.World.Map.CellContaining(dock.Trait.DockPosition)).LengthSquared + dock.Trait.ReservationCount * client.OccupancyCostModifier)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
183
OpenRA.Mods.Common/Traits/DockHost.cs
Normal file
183
OpenRA.Mods.Common/Traits/DockHost.cs
Normal file
@@ -0,0 +1,183 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright (c) The OpenRA Developers and Contributors
|
||||
* 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, either version 3 of
|
||||
* the License, or (at your option) any later version. For more
|
||||
* information, see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
using System.Collections.Generic;
|
||||
using OpenRA.Activities;
|
||||
using OpenRA.Mods.Common.Activities;
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
public sealed class DockType { DockType() { } }
|
||||
|
||||
[Desc("A generic dock that services DockClients.")]
|
||||
public class DockHostInfo : ConditionalTraitInfo, IDockHostInfo
|
||||
{
|
||||
[Desc("Docking type.")]
|
||||
public readonly BitSet<DockType> Type;
|
||||
|
||||
[Desc("How many clients can this dock be reserved for?")]
|
||||
public readonly int MaxQueueLength = 3;
|
||||
|
||||
[Desc("How long should the client wait before starting the docking sequence.")]
|
||||
public readonly int DockWait = 10;
|
||||
|
||||
[Desc("Actual client facing when docking.")]
|
||||
public readonly WAngle DockAngle = WAngle.Zero;
|
||||
|
||||
[Desc("Docking cell relative to the centre of the actor.")]
|
||||
public readonly WVec DockOffset = WVec.Zero;
|
||||
|
||||
[Desc("Does client need to be dragged in?")]
|
||||
public readonly bool IsDragRequired = false;
|
||||
|
||||
[Desc("Vector by which the client will be dragged when docking.")]
|
||||
public readonly WVec DragOffset = WVec.Zero;
|
||||
|
||||
[Desc("In how many steps to perform the dragging?")]
|
||||
public readonly int DragLength = 0;
|
||||
|
||||
public override object Create(ActorInitializer init) { return new DockHost(init.Self, this); }
|
||||
}
|
||||
|
||||
public class DockHost : ConditionalTrait<DockHostInfo>, IDockHost, IDockHostDrag, ITick, INotifySold, INotifyCapture, INotifyOwnerChanged, ISync, INotifyKilled, INotifyActorDisposing
|
||||
{
|
||||
readonly Actor self;
|
||||
|
||||
public BitSet<DockType> GetDockType => Info.Type;
|
||||
public bool IsEnabledAndInWorld => !preventDock && !IsTraitDisabled && !self.IsDead && self.IsInWorld;
|
||||
public int ReservationCount => ReservedDockClients.Count;
|
||||
public bool CanBeReserved => ReservationCount < Info.MaxQueueLength;
|
||||
protected readonly List<DockClientManager> ReservedDockClients = new();
|
||||
|
||||
public WPos DockPosition => self.CenterPosition + Info.DockOffset;
|
||||
public int DockWait => Info.DockWait;
|
||||
public WAngle DockAngle => Info.DockAngle;
|
||||
|
||||
bool IDockHostDrag.IsDragRequired => Info.IsDragRequired;
|
||||
WVec IDockHostDrag.DragOffset => Info.DragOffset;
|
||||
int IDockHostDrag.DragLength => Info.DragLength;
|
||||
|
||||
[Sync]
|
||||
bool preventDock = false;
|
||||
|
||||
[Sync]
|
||||
protected Actor dockedClientActor = null;
|
||||
protected DockClientManager dockedClient = null;
|
||||
|
||||
public DockHost(Actor self, DockHostInfo info)
|
||||
: base(info)
|
||||
{
|
||||
this.self = self;
|
||||
}
|
||||
|
||||
public virtual bool IsDockingPossible(Actor clientActor, IDockClient client, bool ignoreReservations = false)
|
||||
{
|
||||
return !IsTraitDisabled && (ignoreReservations || CanBeReserved || ReservedDockClients.Contains(client.DockClientManager));
|
||||
}
|
||||
|
||||
public virtual bool Reserve(Actor self, DockClientManager client)
|
||||
{
|
||||
if (CanBeReserved && !ReservedDockClients.Contains(client))
|
||||
{
|
||||
ReservedDockClients.Add(client);
|
||||
client.ReserveHost(self, this);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public virtual void UnreserveAll()
|
||||
{
|
||||
while (ReservedDockClients.Count > 0)
|
||||
Unreserve(ReservedDockClients[0]);
|
||||
}
|
||||
|
||||
public virtual void Unreserve(DockClientManager client)
|
||||
{
|
||||
if (ReservedDockClients.Contains(client))
|
||||
{
|
||||
ReservedDockClients.Remove(client);
|
||||
client.UnreserveHost();
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void OnDockStarted(Actor self, Actor clientActor, DockClientManager client)
|
||||
{
|
||||
dockedClientActor = clientActor;
|
||||
dockedClient = client;
|
||||
}
|
||||
|
||||
public virtual void OnDockCompleted(Actor self, Actor clientActor, DockClientManager client)
|
||||
{
|
||||
dockedClientActor = null;
|
||||
dockedClient = null;
|
||||
}
|
||||
|
||||
void ITick.Tick(Actor self)
|
||||
{
|
||||
Tick(self);
|
||||
}
|
||||
|
||||
protected virtual void Tick(Actor self)
|
||||
{
|
||||
// Client was killed during docking.
|
||||
if (dockedClientActor != null && (dockedClientActor.IsDead || !dockedClientActor.IsInWorld))
|
||||
OnDockCompleted(self, dockedClientActor, dockedClient);
|
||||
}
|
||||
|
||||
public virtual bool QueueMoveActivity(Activity moveToDockActivity, Actor self, Actor clientActor, DockClientManager client)
|
||||
{
|
||||
var move = clientActor.Trait<IMove>();
|
||||
|
||||
// Make sure the actor is at dock, at correct facing, and aircraft are landed.
|
||||
// Mobile cannot freely move in WPos, so when we calculate close enough we convert to CPos.
|
||||
if ((move is Mobile ? clientActor.Location != clientActor.World.Map.CellContaining(DockPosition) : clientActor.CenterPosition != DockPosition)
|
||||
|| move is not IFacing facing || facing.Facing != DockAngle)
|
||||
{
|
||||
moveToDockActivity.QueueChild(move.MoveOntoTarget(clientActor, Target.FromActor(self), DockPosition - self.CenterPosition, DockAngle));
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public virtual void QueueDockActivity(Activity moveToDockActivity, Actor self, Actor clientActor, DockClientManager client)
|
||||
{
|
||||
moveToDockActivity.QueueChild(new GenericDockSequence(clientActor, client, self, this));
|
||||
}
|
||||
|
||||
protected override void TraitDisabled(Actor self) { UnreserveAll(); }
|
||||
|
||||
void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner) { UnreserveAll(); }
|
||||
|
||||
void INotifyCapture.OnCapture(Actor self, Actor captor, Player oldOwner, Player newOwner, BitSet<CaptureType> captureTypes)
|
||||
{
|
||||
// Steal any docked unit too.
|
||||
if (dockedClientActor != null && !dockedClientActor.IsDead && dockedClientActor.IsInWorld)
|
||||
{
|
||||
dockedClientActor.ChangeOwner(newOwner);
|
||||
|
||||
// On capture OnOwnerChanged event is called first, so we need to re-reserve.
|
||||
dockedClient.ReserveHost(self, this);
|
||||
}
|
||||
}
|
||||
|
||||
void INotifySold.Selling(Actor self) { preventDock = true; }
|
||||
|
||||
void INotifySold.Sold(Actor self) { UnreserveAll(); }
|
||||
|
||||
void INotifyKilled.Killed(Actor self, AttackInfo e) { UnreserveAll(); }
|
||||
|
||||
void INotifyActorDisposing.Disposing(Actor self) { preventDock = true; UnreserveAll(); }
|
||||
}
|
||||
}
|
||||
@@ -14,18 +14,15 @@ using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using OpenRA.Mods.Common.Activities;
|
||||
using OpenRA.Mods.Common.Orders;
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
public class HarvesterInfo : ConditionalTraitInfo, Requires<MobileInfo>
|
||||
public class HarvesterInfo : DockClientBaseInfo, Requires<MobileInfo>
|
||||
{
|
||||
public readonly HashSet<string> DeliveryBuildings = new();
|
||||
|
||||
[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("Docking type")]
|
||||
public readonly BitSet<DockType> Type = new("Unload");
|
||||
|
||||
[Desc("Cell to move to when automatically unblocking DeliveryBuilding.")]
|
||||
public readonly CVec UnblockCell = new(0, 4);
|
||||
@@ -61,12 +58,6 @@ namespace OpenRA.Mods.Common.Traits
|
||||
[Desc("Interval to wait between searches when there are no resources nearby.")]
|
||||
public readonly int WaitDuration = 25;
|
||||
|
||||
[Desc("Find a new refinery to unload at if more than this many harvesters are already waiting.")]
|
||||
public readonly int MaxUnloadQueue = 3;
|
||||
|
||||
[Desc("The pathfinding cost penalty applied for each harvester waiting to unload at a refinery.")]
|
||||
public readonly int UnloadQueueCostModifier = 12;
|
||||
|
||||
[Desc("The pathfinding cost penalty applied for cells directly away from the refinery.")]
|
||||
public readonly int ResourceRefineryDirectionPenalty = 200;
|
||||
|
||||
@@ -80,23 +71,9 @@ namespace OpenRA.Mods.Common.Traits
|
||||
[VoiceReference]
|
||||
public readonly string HarvestVoice = "Action";
|
||||
|
||||
[VoiceReference]
|
||||
public readonly string DeliverVoice = "Action";
|
||||
|
||||
[Desc("Color to use for the target line of harvest orders.")]
|
||||
public readonly Color HarvestLineColor = Color.Crimson;
|
||||
|
||||
[Desc("Color to use for the target line of harvest orders.")]
|
||||
public readonly Color DeliverLineColor = Color.Green;
|
||||
|
||||
[CursorReference]
|
||||
[Desc("Cursor to display when able to unload at target actor.")]
|
||||
public readonly string EnterCursor = "enter";
|
||||
|
||||
[CursorReference]
|
||||
[Desc("Cursor to display when unable to unload at target actor.")]
|
||||
public readonly string EnterBlockedCursor = "enter-blocked";
|
||||
|
||||
[CursorReference]
|
||||
[Desc("Cursor to display when ordering to harvest resources.")]
|
||||
public readonly string HarvestCursor = "harvest";
|
||||
@@ -104,7 +81,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
public override object Create(ActorInitializer init) { return new Harvester(init.Self, this); }
|
||||
}
|
||||
|
||||
public class Harvester : ConditionalTrait<HarvesterInfo>, IIssueOrder, IResolveOrder, IOrderVoice,
|
||||
public class Harvester : DockClientBase<HarvesterInfo>, IIssueOrder, IResolveOrder, IOrderVoice,
|
||||
ISpeedModifier, ISync, INotifyCreated
|
||||
{
|
||||
public readonly IReadOnlyDictionary<string, int> Contents;
|
||||
@@ -115,11 +92,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
readonly Dictionary<string, int> contents = new();
|
||||
int conditionToken = Actor.InvalidConditionToken;
|
||||
|
||||
[Sync]
|
||||
public Actor LastLinkedProc = null;
|
||||
|
||||
[Sync]
|
||||
public Actor LinkedProc = null;
|
||||
public override BitSet<DockType> GetDockType => Info.Type;
|
||||
|
||||
[Sync]
|
||||
int currentUnloadTicks;
|
||||
@@ -137,7 +110,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
}
|
||||
|
||||
public Harvester(Actor self, HarvesterInfo info)
|
||||
: base(info)
|
||||
: base(self, info)
|
||||
{
|
||||
Contents = new ReadOnlyDictionary<string, int>(contents);
|
||||
mobile = self.Trait<Mobile>();
|
||||
@@ -156,70 +129,15 @@ namespace OpenRA.Mods.Common.Traits
|
||||
base.Created(self);
|
||||
}
|
||||
|
||||
public void LinkProc(Actor proc)
|
||||
{
|
||||
LinkedProc = proc;
|
||||
}
|
||||
|
||||
public void UnlinkProc(Actor self, Actor proc)
|
||||
{
|
||||
if (LinkedProc == proc)
|
||||
ChooseNewProc(self, proc);
|
||||
}
|
||||
|
||||
public void ChooseNewProc(Actor self, Actor ignore)
|
||||
{
|
||||
LastLinkedProc = null;
|
||||
LinkProc(ClosestProc(self, ignore));
|
||||
}
|
||||
|
||||
bool IsAcceptableProcType(Actor proc)
|
||||
{
|
||||
return Info.DeliveryBuildings.Count == 0 ||
|
||||
Info.DeliveryBuildings.Contains(proc.Info.Name);
|
||||
}
|
||||
|
||||
public Actor ClosestProc(Actor self, Actor ignore)
|
||||
{
|
||||
// Find all refineries and their occupancy count:
|
||||
// Exclude refineries with too many harvesters clogging the delivery location.
|
||||
var refineries = self.World.ActorsWithTrait<IAcceptResources>()
|
||||
.Where(r => r.Actor != ignore && r.Actor.Owner == self.Owner && IsAcceptableProcType(r.Actor))
|
||||
.Select(r => new
|
||||
{
|
||||
Location = r.Actor.World.Map.CellContaining(r.Trait.DeliveryPosition),
|
||||
Actor = r.Actor,
|
||||
Occupancy = self.World.ActorsHavingTrait<Harvester>(h => h.LinkedProc == r.Actor).Count()
|
||||
})
|
||||
.Where(r => r.Occupancy < Info.MaxUnloadQueue)
|
||||
.ToDictionary(r => r.Location);
|
||||
|
||||
if (refineries.Count == 0)
|
||||
return null;
|
||||
|
||||
// Start a search from each refinery's delivery location:
|
||||
var path = mobile.PathFinder.FindPathToTargetCells(
|
||||
self, self.Location, refineries.Select(r => r.Key), BlockedByActor.None,
|
||||
location =>
|
||||
{
|
||||
if (!refineries.ContainsKey(location))
|
||||
return 0;
|
||||
|
||||
// Prefer refineries with less occupancy (multiplier is to offset distance cost):
|
||||
var occupancy = refineries[location].Occupancy;
|
||||
return occupancy * Info.UnloadQueueCostModifier;
|
||||
});
|
||||
|
||||
if (path.Count > 0)
|
||||
return refineries[path[0]].Actor;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool IsFull => contents.Values.Sum() == Info.Capacity;
|
||||
public bool IsEmpty => contents.Values.Sum() == 0;
|
||||
public int Fullness => contents.Values.Sum() * 100 / Info.Capacity;
|
||||
|
||||
protected override bool CanDock()
|
||||
{
|
||||
return !IsEmpty;
|
||||
}
|
||||
|
||||
void UpdateCondition(Actor self)
|
||||
{
|
||||
if (string.IsNullOrEmpty(Info.EmptyCondition))
|
||||
@@ -243,21 +161,29 @@ namespace OpenRA.Mods.Common.Traits
|
||||
UpdateCondition(self);
|
||||
}
|
||||
|
||||
// Returns true when unloading is complete
|
||||
public virtual bool TickUnload(Actor self, Actor proc)
|
||||
IAcceptResources acceptResources;
|
||||
public override void OnDockStarted(Actor self, Actor hostActor, IDockHost host)
|
||||
{
|
||||
if (IsDockingPossible(host.GetDockType))
|
||||
acceptResources = hostActor.TraitOrDefault<IAcceptResources>();
|
||||
}
|
||||
|
||||
public override bool OnDockTick(Actor self, Actor hostActor, IDockHost host)
|
||||
{
|
||||
if (acceptResources == null || IsTraitDisabled)
|
||||
return true;
|
||||
|
||||
// Wait until the next bale is ready
|
||||
if (--currentUnloadTicks > 0)
|
||||
return false;
|
||||
|
||||
if (contents.Keys.Count > 0)
|
||||
{
|
||||
var acceptResources = proc.Trait<IAcceptResources>();
|
||||
foreach (var c in contents)
|
||||
{
|
||||
var resourceType = c.Key;
|
||||
var count = Math.Min(c.Value, Info.BaleUnloadAmount);
|
||||
var accepted = acceptResources.AcceptResources(resourceType, count);
|
||||
var accepted = acceptResources.AcceptResources(hostActor, resourceType, count);
|
||||
if (accepted == 0)
|
||||
continue;
|
||||
|
||||
@@ -274,6 +200,19 @@ namespace OpenRA.Mods.Common.Traits
|
||||
return contents.Count == 0;
|
||||
}
|
||||
|
||||
public override void OnDockCompleted(Actor self, Actor hostActor, IDockHost dock)
|
||||
{
|
||||
acceptResources = null;
|
||||
|
||||
// After having docked at a refinery make sure we are running FindAndDeliverResources activity.
|
||||
if (GetDockType.Overlaps(dock.GetDockType))
|
||||
{
|
||||
var currentActivity = self.CurrentActivity;
|
||||
if (currentActivity == null || (currentActivity is not FindAndDeliverResources && currentActivity.NextActivity == null))
|
||||
self.QueueActivity(true, new FindAndDeliverResources(self));
|
||||
}
|
||||
}
|
||||
|
||||
public bool CanHarvestCell(CPos cell)
|
||||
{
|
||||
// Resources only exist in the ground layer
|
||||
@@ -295,20 +234,13 @@ namespace OpenRA.Mods.Common.Traits
|
||||
if (IsTraitDisabled)
|
||||
yield break;
|
||||
|
||||
yield return new EnterAlliedActorTargeter<IAcceptResourcesInfo>(
|
||||
"Deliver",
|
||||
5,
|
||||
Info.EnterCursor,
|
||||
Info.EnterBlockedCursor,
|
||||
(proc, _) => IsAcceptableProcType(proc),
|
||||
proc => proc.Trait<IAcceptResources>().AllowDocking);
|
||||
yield return new HarvestOrderTargeter();
|
||||
}
|
||||
}
|
||||
|
||||
Order IIssueOrder.IssueOrder(Actor self, IOrderTargeter order, in Target target, bool queued)
|
||||
{
|
||||
if (order.OrderID == "Deliver" || order.OrderID == "Harvest")
|
||||
if (order.OrderID == "Harvest")
|
||||
return new Order(order.OrderID, self, target, queued);
|
||||
|
||||
return null;
|
||||
@@ -319,9 +251,6 @@ namespace OpenRA.Mods.Common.Traits
|
||||
if (order.OrderString == "Harvest")
|
||||
return Info.HarvestVoice;
|
||||
|
||||
if (order.OrderString == "Deliver" && !IsEmpty)
|
||||
return Info.DeliverVoice;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -329,9 +258,6 @@ namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
if (order.OrderString == "Harvest")
|
||||
{
|
||||
// NOTE: An explicit harvest order allows the harvester to decide which refinery to deliver to.
|
||||
LinkProc(null);
|
||||
|
||||
CPos loc;
|
||||
if (order.Target.Type != TargetType.Invalid)
|
||||
{
|
||||
@@ -349,21 +275,6 @@ namespace OpenRA.Mods.Common.Traits
|
||||
self.QueueActivity(order.Queued, new FindAndDeliverResources(self, loc));
|
||||
self.ShowTargetLines();
|
||||
}
|
||||
else if (order.OrderString == "Deliver")
|
||||
{
|
||||
// Deliver orders are only valid for own/allied actors,
|
||||
// which are guaranteed to never be frozen.
|
||||
if (order.Target.Type != TargetType.Actor)
|
||||
return;
|
||||
|
||||
var targetActor = order.Target.Actor;
|
||||
var iao = targetActor.TraitOrDefault<IAcceptResources>();
|
||||
if (iao == null || !iao.AllowDocking || !IsAcceptableProcType(targetActor))
|
||||
return;
|
||||
|
||||
self.QueueActivity(order.Queued, new FindAndDeliverResources(self, targetActor));
|
||||
self.ShowTargetLines();
|
||||
}
|
||||
}
|
||||
|
||||
int ISpeedModifier.GetSpeedModifier()
|
||||
@@ -373,8 +284,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
protected override void TraitDisabled(Actor self)
|
||||
{
|
||||
LastLinkedProc = null;
|
||||
LinkedProc = null;
|
||||
base.TraitDisabled(self);
|
||||
contents.Clear();
|
||||
|
||||
if (conditionToken != Actor.InvalidConditionToken)
|
||||
|
||||
Reference in New Issue
Block a user