diff --git a/OpenRA.Mods.Common/Scripting/Properties/CaptureProperties.cs b/OpenRA.Mods.Common/Scripting/Properties/CaptureProperties.cs index 9913458f99..bfcf9105e8 100644 --- a/OpenRA.Mods.Common/Scripting/Properties/CaptureProperties.cs +++ b/OpenRA.Mods.Common/Scripting/Properties/CaptureProperties.cs @@ -20,25 +20,24 @@ using OpenRA.Traits; namespace OpenRA.Mods.Common.Scripting { [ScriptPropertyGroup("Ability")] - public class CaptureProperties : ScriptActorProperties + public class CaptureProperties : ScriptActorProperties, Requires { - readonly Captures[] captures; + readonly CaptureManager captureManager; public CaptureProperties(ScriptContext context, Actor self) : base(context, self) { - captures = Self.TraitsImplementing().ToArray(); + captureManager = Self.Trait(); } [Desc("Captures the target actor.")] public void Capture(Actor target) { - var capturable = target.Info.TraitInfoOrDefault(); - if (capturable == null) + var targetManager = target.TraitOrDefault(); + if (targetManager == null || !targetManager.CanBeTargetedBy(target, Self, captureManager)) throw new LuaException("Actor '{0}' cannot capture actor '{1}'!".F(Self, target)); - if (captures.Any(x => !x.IsTraitDisabled && x.Info.CaptureTypes.Overlaps(capturable.Types))) - Self.QueueActivity(new CaptureActor(Self, target)); + Self.QueueActivity(new CaptureActor(Self, target)); } } } diff --git a/OpenRA.Mods.Common/Traits/Capturable.cs b/OpenRA.Mods.Common/Traits/Capturable.cs index 15bf165859..7005a7afca 100644 --- a/OpenRA.Mods.Common/Traits/Capturable.cs +++ b/OpenRA.Mods.Common/Traits/Capturable.cs @@ -14,7 +14,8 @@ using OpenRA.Traits; namespace OpenRA.Mods.Common.Traits { - [Desc("This actor can be captured by a unit with Captures: trait.")] + [Desc("This actor can be captured by a unit with Captures: trait.", + "This trait should not be disabled if the actor also uses FrozenUnderFog.")] public class CapturableInfo : ConditionalTraitInfo, Requires { [FieldLoader.Require] @@ -27,22 +28,6 @@ namespace OpenRA.Mods.Common.Traits public readonly bool CancelActivity = false; public override object Create(ActorInitializer init) { return new Capturable(init.Self, this); } - - public bool CanBeTargetedBy(Actor captor, Player owner) - { - var c = captor.Info.TraitInfoOrDefault(); - if (c == null) - return false; - - var stance = owner.Stances[captor.Owner]; - if (!ValidStances.HasStance(stance)) - return false; - - if (!c.CaptureTypes.Overlaps(Types)) - return false; - - return true; - } } public class Capturable : ConditionalTrait, INotifyCapture @@ -65,14 +50,6 @@ namespace OpenRA.Mods.Common.Traits } } - public bool CanBeTargetedBy(Actor captor, Player owner) - { - if (IsTraitDisabled) - return false; - - return Info.CanBeTargetedBy(captor, owner); - } - protected override void TraitEnabled(Actor self) { captureManager.RefreshCapturable(self); } protected override void TraitDisabled(Actor self) { captureManager.RefreshCapturable(self); } } diff --git a/OpenRA.Mods.Common/Traits/CaptureManager.cs b/OpenRA.Mods.Common/Traits/CaptureManager.cs index 4a90c2f98e..ae738a0ca6 100644 --- a/OpenRA.Mods.Common/Traits/CaptureManager.cs +++ b/OpenRA.Mods.Common/Traits/CaptureManager.cs @@ -40,6 +40,20 @@ namespace OpenRA.Mods.Common.Traits public readonly bool PreventsAutoTarget = true; public virtual object Create(ActorInitializer init) { return new CaptureManager(this); } + + public bool CanBeTargetedBy(FrozenActor frozenActor, Actor captor, Captures captures) + { + if (captures.IsTraitDisabled) + return false; + + // TODO: FrozenActors don't yet have a way of caching conditions, so we can't filter disabled traits + // This therefore assumes that all Capturable traits are enabled, which is probably wrong. + // Actors with FrozenUnderFog should therefore not disable the Capturable trait. + var stance = frozenActor.Owner.Stances[captor.Owner]; + return frozenActor.Info.TraitInfos() + .Any(c => c.ValidStances.HasStance(stance) && + captures.Info.CaptureTypes.Overlaps(c.Types)); + } } public class CaptureManager : INotifyCreated, INotifyCapture, ITick, IPreventsAutoTarget diff --git a/OpenRA.Mods.Common/Traits/Captures.cs b/OpenRA.Mods.Common/Traits/Captures.cs index 8950eddc63..f719e2efbb 100644 --- a/OpenRA.Mods.Common/Traits/Captures.cs +++ b/OpenRA.Mods.Common/Traits/Captures.cs @@ -127,7 +127,6 @@ namespace OpenRA.Mods.Common.Traits } cursor = captures.Info.EnterCursor; - if (captures.Info.SabotageThreshold > 0 && !target.Owner.NonCombatant) { var health = target.Trait(); @@ -142,11 +141,10 @@ namespace OpenRA.Mods.Common.Traits public override bool CanTargetFrozenActor(Actor self, FrozenActor target, TargetModifiers modifiers, ref string cursor) { - // Actors with FrozenUnderFog should not disable the Capturable trait. - var c = target.Info.TraitInfoOrDefault(); - if (c == null || !c.CanBeTargetedBy(self, target.Owner)) + var captureManagerInfo = target.Info.TraitInfoOrDefault(); + if (captureManagerInfo == null || !captureManagerInfo.CanBeTargetedBy(target, self, captures)) { - cursor = captures.Info.EnterCursor; + cursor = captures.Info.EnterBlockedCursor; return false; }