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

@@ -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)