Add RequireForceMoveCondition to DockClientManager

This commit is contained in:
Gustas
2024-01-10 17:25:16 +02:00
committed by Paul Chote
parent 9c6cb501a4
commit 1334575ba9
2 changed files with 95 additions and 81 deletions

View File

@@ -12,7 +12,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using OpenRA.Mods.Common.Activities; using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Orders;
using OpenRA.Primitives; using OpenRA.Primitives;
using OpenRA.Traits; using OpenRA.Traits;
@@ -66,20 +65,13 @@ namespace OpenRA.Mods.Common.Traits
{ {
get get
{ {
yield return new EnterAlliedActorTargeter<DockHostInfo>( yield return new DockActorTargeter(
"ForceDock",
6, 6,
Info.EnterCursor, Info.EnterCursor,
Info.EnterBlockedCursor, Info.EnterBlockedCursor,
ForceDockingPossible, () => Info.RequiresForceMove,
target => CanDockAt(target, true));
yield return new EnterAlliedActorTargeter<DockHostInfo>(
"Dock",
5,
Info.EnterCursor,
Info.EnterBlockedCursor,
DockingPossible, DockingPossible,
target => CanDockAt(target, false)); CanDockAt);
} }
} }
@@ -133,30 +125,14 @@ namespace OpenRA.Mods.Common.Traits
return null; return null;
} }
/// <summary>Clone of <see cref="DockClientManager.DockingPossible(Actor)"/>.</summary> /// <summary>Clone of <see cref="DockClientManager.DockingPossible(Actor, bool)"/>.</summary>
public bool DockingPossible(Actor target, TargetModifiers modifiers) public bool DockingPossible(Actor target, bool forceEnter)
{ {
var forceEnter = modifiers.HasModifier(TargetModifiers.ForceMove); return !IsTraitDisabled && target.TraitsImplementing<DockHost>().Any(host => dockClients.Any(client => client.IsDockingPossible(host.GetDockType)));
if (Info.RequiresForceMove && !forceEnter)
return false;
return !IsTraitDisabled && target.TraitsImplementing<DockHost>().Any(
host => dockClients.Any(client => client.IsDockingPossible(host.GetDockType)));
}
/// <summary>Clone of <see cref="DockClientManager.DockingPossible(Actor, TargetModifiers)"/>.</summary>
public bool ForceDockingPossible(Actor target, TargetModifiers modifiers)
{
var forceEnter = modifiers.HasModifier(TargetModifiers.ForceMove);
if (Info.RequiresForceMove && !forceEnter)
return false;
return !IsTraitDisabled && target.TraitsImplementing<DockHost>().Any(
host => dockClients.Any(client => client.IsDockingPossible(host.GetDockType, forceEnter)));
} }
/// <summary>Clone of <see cref="DockClientManager.CanDockAt(Actor, bool, bool)"/>.</summary> /// <summary>Clone of <see cref="DockClientManager.CanDockAt(Actor, bool, bool)"/>.</summary>
public bool CanDockAt(Actor target, bool forceEnter = false) public bool CanDockAt(Actor target, bool forceEnter)
{ {
if (!(self.CurrentActivity is Transform || transforms.Any(t => !t.IsTraitDisabled && !t.IsTraitPaused))) if (!(self.CurrentActivity is Transform || transforms.Any(t => !t.IsTraitDisabled && !t.IsTraitPaused)))
return false; return false;

View File

@@ -9,11 +9,12 @@
*/ */
#endregion #endregion
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using OpenRA.Mods.Common.Activities; using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Orders;
using OpenRA.Primitives; using OpenRA.Primitives;
using OpenRA.Support;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
@@ -27,6 +28,10 @@ namespace OpenRA.Mods.Common.Traits
[Desc("The pathfinding cost penalty applied for each dock client waiting to unload at a DockHost.")] [Desc("The pathfinding cost penalty applied for each dock client waiting to unload at a DockHost.")]
public readonly int OccupancyCostModifier = 12; public readonly int OccupancyCostModifier = 12;
[ConsumedConditionReference]
[Desc("Boolean expression defining the condition under which the regular (non-force) enter cursor is disabled.")]
public readonly BooleanExpression RequireForceMoveCondition = null;
[CursorReference] [CursorReference]
[Desc("Cursor to display when able to dock at target actor.")] [Desc("Cursor to display when able to dock at target actor.")]
public readonly string EnterCursor = "enter"; public readonly string EnterCursor = "enter";
@@ -51,6 +56,7 @@ namespace OpenRA.Mods.Common.Traits
protected IDockClient[] dockClients; protected IDockClient[] dockClients;
public Color DockLineColor => Info.DockLineColor; public Color DockLineColor => Info.DockLineColor;
public int OccupancyCostModifier => Info.OccupancyCostModifier; public int OccupancyCostModifier => Info.OccupancyCostModifier;
bool requireForceMove;
public DockClientManager(Actor self, DockClientManagerInfo info) public DockClientManager(Actor self, DockClientManagerInfo info)
: base(info) : base(info)
@@ -149,26 +155,19 @@ namespace OpenRA.Mods.Common.Traits
{ {
get get
{ {
yield return new EnterAlliedActorTargeter<IDockHostInfo>( yield return new DockActorTargeter(
"ForceDock",
6, 6,
Info.EnterCursor, Info.EnterCursor,
Info.EnterBlockedCursor, Info.EnterBlockedCursor,
() => requireForceMove,
DockingPossible, DockingPossible,
target => CanDockAt(target, true, true)); (target, forceEnter) => CanDockAt(target, forceEnter, 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) void IResolveOrder.ResolveOrder(Actor self, Order order)
{ {
if (order.OrderString == "Dock") if (order.OrderString == "Dock" || order.OrderString == "ForceDock")
{ {
var target = order.Target; var target = order.Target;
@@ -181,27 +180,9 @@ namespace OpenRA.Mods.Common.Traits
if (IsTraitDisabled) if (IsTraitDisabled)
return; return;
var dock = AvailableDockHosts(target.Actor, false, true).ClosestDock(self, this); var dock = AvailableDockHosts(target.Actor, default, order.OrderString == "ForceDock", true)
if (!dock.HasValue) .ClosestDock(self, this);
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) if (!dock.HasValue)
return; return;
@@ -231,6 +212,20 @@ namespace OpenRA.Mods.Common.Traits
return null; return null;
} }
public override IEnumerable<VariableObserver> GetVariableObservers()
{
foreach (var observer in base.GetVariableObservers())
yield return observer;
if (Info.RequireForceMoveCondition != null)
yield return new VariableObserver(RequireForceMoveConditionChanged, Info.RequireForceMoveCondition.Variables);
}
void RequireForceMoveConditionChanged(Actor self, IReadOnlyDictionary<string, int> conditions)
{
requireForceMove = Info.RequireForceMoveCondition.Evaluate(conditions);
}
/// <summary>Do we have an enabled client with matching <paramref name="type"/>.</summary> /// <summary>Do we have an enabled client with matching <paramref name="type"/>.</summary>
public bool DockingPossible(BitSet<DockType> type, bool forceEnter = false) public bool DockingPossible(BitSet<DockType> type, bool forceEnter = false)
{ {
@@ -238,18 +233,11 @@ namespace OpenRA.Mods.Common.Traits
} }
/// <summary>Does this <paramref name="target"/> contain at least one enabled <see cref="IDockHost"/> with maching <see cref="DockType"/>.</summary> /// <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) public bool DockingPossible(Actor target, bool forceEnter = false)
{ {
return !IsTraitDisabled && target.TraitsImplementing<IDockHost>().Any( return !IsTraitDisabled &&
host => dockClients.Any(client => client.IsDockingPossible(host.GetDockType))); target.TraitsImplementing<IDockHost>()
} .Any(host => dockClients.Any(client => client.IsDockingPossible(host.GetDockType, 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, 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> /// <summary>Can we dock to this <paramref name="host"/>.</summary>
@@ -270,7 +258,7 @@ namespace OpenRA.Mods.Common.Traits
/// <remarks>If <paramref name="type"/> is not set, scans all clients. Does not check if <see cref="DockClientManager"/> is enabled.</remarks> /// <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) public TraitPair<IDockHost>? ClosestDock(IDockHost ignore, BitSet<DockType> type = default, bool forceEnter = false, bool ignoreOccupancy = false)
{ {
var clients = type.IsEmpty ? dockClients : AvailableDockClients(type); var clients = type.IsEmpty ? dockClients : AvailableDockClients(type, forceEnter);
return self.World.ActorsWithTrait<IDockHost>() return self.World.ActorsWithTrait<IDockHost>()
.Where(host => .Where(host =>
host.Trait != ignore && host.Trait != ignore &&
@@ -279,19 +267,21 @@ namespace OpenRA.Mods.Common.Traits
} }
/// <summary>Get viable <see cref="IDockHost"/>'s on the <paramref name="target"/>.</summary> /// <summary>Get viable <see cref="IDockHost"/>'s on the <paramref name="target"/>.</summary>
/// <remarks>Does not check if <see cref="DockClientManager"/> is enabled.</remarks> /// <remarks>If <paramref name="type"/> is not set, checks all clients. Does not check if <see cref="DockClientManager"/> is enabled.</remarks>
public IEnumerable<TraitPair<IDockHost>> AvailableDockHosts(Actor target, bool forceEnter = false, bool ignoreOccupancy = false) public IEnumerable<TraitPair<IDockHost>> AvailableDockHosts(Actor target, BitSet<DockType> type = default,
bool forceEnter = false, bool ignoreOccupancy = false)
{ {
var clients = type.IsEmpty ? dockClients : AvailableDockClients(type, forceEnter);
return target.TraitsImplementing<IDockHost>() return target.TraitsImplementing<IDockHost>()
.Where(host => dockClients.Any(client => client.CanDockAt(target, host, forceEnter, ignoreOccupancy))) .Where(host => clients.Any(client => client.CanDockAt(target, host, forceEnter, ignoreOccupancy)))
.Select(host => new TraitPair<IDockHost>(target, host)); .Select(host => new TraitPair<IDockHost>(target, host));
} }
/// <summary>Get clients of matching <paramref name="type"/>.</summary> /// <summary>Get clients of matching <paramref name="type"/>.</summary>
/// <remarks>Does not check if <see cref="DockClientManager"/> is enabled.</remarks> /// <remarks>Does not check if <see cref="DockClientManager"/> is enabled.</remarks>
public IEnumerable<IDockClient> AvailableDockClients(BitSet<DockType> type) public IEnumerable<IDockClient> AvailableDockClients(BitSet<DockType> type, bool forceEnter = false)
{ {
return dockClients.Where(client => client.IsDockingPossible(type)); return dockClients.Where(client => client.IsDockingPossible(type, forceEnter));
} }
void INotifyKilled.Killed(Actor self, AttackInfo e) { UnreserveHost(); } void INotifyKilled.Killed(Actor self, AttackInfo e) { UnreserveHost(); }
@@ -299,6 +289,54 @@ namespace OpenRA.Mods.Common.Traits
void INotifyActorDisposing.Disposing(Actor self) { UnreserveHost(); } void INotifyActorDisposing.Disposing(Actor self) { UnreserveHost(); }
} }
public class DockActorTargeter : IOrderTargeter
{
readonly string enterCursor;
readonly string enterBlockedCursor;
readonly Func<bool> requireForceMove;
readonly Func<Actor, bool, bool> canTarget;
readonly Func<Actor, bool, bool> useEnterCursor;
public DockActorTargeter(int priority, string enterCursor, string enterBlockedCursor,
Func<bool> requireForceMove, Func<Actor, bool, bool> canTarget, Func<Actor, bool, bool> useEnterCursor)
{
OrderID = "Dock";
OrderPriority = priority;
this.enterCursor = enterCursor;
this.enterBlockedCursor = enterBlockedCursor;
this.requireForceMove = requireForceMove;
this.canTarget = canTarget;
this.useEnterCursor = useEnterCursor;
}
public string OrderID { get; private set; }
public int OrderPriority { get; }
public bool TargetOverridesSelection(Actor self, in Target target, List<Actor> actorsAt, CPos xy, TargetModifiers modifiers) { return true; }
public bool CanTarget(Actor self, in Target target, ref TargetModifiers modifiers, ref string cursor)
{
// TODO: support frozen actors
if (target.Type != TargetType.Actor)
return false;
cursor = enterCursor;
IsQueued = modifiers.HasModifier(TargetModifiers.ForceQueue);
var forceEnter = modifiers.HasModifier(TargetModifiers.ForceMove);
OrderID = forceEnter ? "ForceDock" : "Dock";
if (requireForceMove() && !forceEnter)
return false;
if (!self.Owner.IsAlliedWith(target.Actor.Owner) || !canTarget(target.Actor, forceEnter))
return false;
cursor = useEnterCursor(target.Actor, forceEnter) ? enterCursor : enterBlockedCursor;
return true;
}
public virtual bool IsQueued { get; protected set; }
}
public static class DockExts public static class DockExts
{ {
public static TraitPair<IDockHost>? ClosestDock(this IEnumerable<TraitPair<IDockHost>> docks, Actor clientActor, DockClientManager client) public static TraitPair<IDockHost>? ClosestDock(this IEnumerable<TraitPair<IDockHost>> docks, Actor clientActor, DockClientManager client)
@@ -318,7 +356,7 @@ namespace OpenRA.Mods.Common.Traits
return 0; return 0;
// Prefer docks with less occupancy (multiplier is to offset distance cost): // Prefer docks with less occupancy (multiplier is to offset distance cost):
// TODO: add custom wieghts. E.g. owner vs allied. // TODO: add custom weights. E.g. owner vs allied.
return dock.Trait.ReservationCount * client.OccupancyCostModifier; return dock.Trait.ReservationCount * client.OccupancyCostModifier;
}); });