Support multiple capture traits in order targeter and script plumbing.
This commit is contained in:
@@ -20,25 +20,24 @@ using OpenRA.Traits;
|
|||||||
namespace OpenRA.Mods.Common.Scripting
|
namespace OpenRA.Mods.Common.Scripting
|
||||||
{
|
{
|
||||||
[ScriptPropertyGroup("Ability")]
|
[ScriptPropertyGroup("Ability")]
|
||||||
public class CaptureProperties : ScriptActorProperties
|
public class CaptureProperties : ScriptActorProperties, Requires<CaptureManagerInfo>
|
||||||
{
|
{
|
||||||
readonly Captures[] captures;
|
readonly CaptureManager captureManager;
|
||||||
|
|
||||||
public CaptureProperties(ScriptContext context, Actor self)
|
public CaptureProperties(ScriptContext context, Actor self)
|
||||||
: base(context, self)
|
: base(context, self)
|
||||||
{
|
{
|
||||||
captures = Self.TraitsImplementing<Captures>().ToArray();
|
captureManager = Self.Trait<CaptureManager>();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Desc("Captures the target actor.")]
|
[Desc("Captures the target actor.")]
|
||||||
public void Capture(Actor target)
|
public void Capture(Actor target)
|
||||||
{
|
{
|
||||||
var capturable = target.Info.TraitInfoOrDefault<CapturableInfo>();
|
var targetManager = target.TraitOrDefault<CaptureManager>();
|
||||||
if (capturable == null)
|
if (targetManager == null || !targetManager.CanBeTargetedBy(target, Self, captureManager))
|
||||||
throw new LuaException("Actor '{0}' cannot capture actor '{1}'!".F(Self, target));
|
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));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,8 @@ using OpenRA.Traits;
|
|||||||
|
|
||||||
namespace OpenRA.Mods.Common.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<CaptureManagerInfo>
|
public class CapturableInfo : ConditionalTraitInfo, Requires<CaptureManagerInfo>
|
||||||
{
|
{
|
||||||
[FieldLoader.Require]
|
[FieldLoader.Require]
|
||||||
@@ -27,22 +28,6 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
public readonly bool CancelActivity = false;
|
public readonly bool CancelActivity = false;
|
||||||
|
|
||||||
public override object Create(ActorInitializer init) { return new Capturable(init.Self, this); }
|
public override object Create(ActorInitializer init) { return new Capturable(init.Self, this); }
|
||||||
|
|
||||||
public bool CanBeTargetedBy(Actor captor, Player owner)
|
|
||||||
{
|
|
||||||
var c = captor.Info.TraitInfoOrDefault<CapturesInfo>();
|
|
||||||
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<CapturableInfo>, INotifyCapture
|
public class Capturable : ConditionalTrait<CapturableInfo>, 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 TraitEnabled(Actor self) { captureManager.RefreshCapturable(self); }
|
||||||
protected override void TraitDisabled(Actor self) { captureManager.RefreshCapturable(self); }
|
protected override void TraitDisabled(Actor self) { captureManager.RefreshCapturable(self); }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,6 +40,20 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
public readonly bool PreventsAutoTarget = true;
|
public readonly bool PreventsAutoTarget = true;
|
||||||
|
|
||||||
public virtual object Create(ActorInitializer init) { return new CaptureManager(this); }
|
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<CapturableInfo>()
|
||||||
|
.Any(c => c.ValidStances.HasStance(stance) &&
|
||||||
|
captures.Info.CaptureTypes.Overlaps(c.Types));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class CaptureManager : INotifyCreated, INotifyCapture, ITick, IPreventsAutoTarget
|
public class CaptureManager : INotifyCreated, INotifyCapture, ITick, IPreventsAutoTarget
|
||||||
|
|||||||
@@ -127,7 +127,6 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
}
|
}
|
||||||
|
|
||||||
cursor = captures.Info.EnterCursor;
|
cursor = captures.Info.EnterCursor;
|
||||||
|
|
||||||
if (captures.Info.SabotageThreshold > 0 && !target.Owner.NonCombatant)
|
if (captures.Info.SabotageThreshold > 0 && !target.Owner.NonCombatant)
|
||||||
{
|
{
|
||||||
var health = target.Trait<IHealth>();
|
var health = target.Trait<IHealth>();
|
||||||
@@ -142,11 +141,10 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
|
|
||||||
public override bool CanTargetFrozenActor(Actor self, FrozenActor target, TargetModifiers modifiers, ref string cursor)
|
public override bool CanTargetFrozenActor(Actor self, FrozenActor target, TargetModifiers modifiers, ref string cursor)
|
||||||
{
|
{
|
||||||
// Actors with FrozenUnderFog should not disable the Capturable trait.
|
var captureManagerInfo = target.Info.TraitInfoOrDefault<CaptureManagerInfo>();
|
||||||
var c = target.Info.TraitInfoOrDefault<CapturableInfo>();
|
if (captureManagerInfo == null || !captureManagerInfo.CanBeTargetedBy(target, self, captures))
|
||||||
if (c == null || !c.CanBeTargetedBy(self, target.Owner))
|
|
||||||
{
|
{
|
||||||
cursor = captures.Info.EnterCursor;
|
cursor = captures.Info.EnterBlockedCursor;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user