Allow Attack activities to target FrozenActors directly.

Removing the legacy FrozenActor to Actor workaround
fixes a number of long-standing bugs.

This also prevents units from losing their target when
it transforms into a different actor type.
This commit is contained in:
Paul Chote
2018-12-16 13:01:01 +13:00
committed by Oliver Brakmann
parent 0406b89a96
commit c34dd4b824
12 changed files with 133 additions and 47 deletions

View File

@@ -18,10 +18,10 @@ namespace OpenRA.Mods.Common.Activities
{ {
public class FlyAttack : Activity public class FlyAttack : Activity
{ {
readonly Target target;
readonly Aircraft aircraft; readonly Aircraft aircraft;
readonly AttackAircraft attackAircraft; readonly AttackAircraft attackAircraft;
readonly Rearmable rearmable; readonly Rearmable rearmable;
Target target;
int ticksUntilTurn; int ticksUntilTurn;
@@ -43,6 +43,8 @@ namespace OpenRA.Mods.Common.Activities
return NextActivity; return NextActivity;
} }
target = target.Recalculate(self.Owner);
if (!target.IsValidFor(self)) if (!target.IsValidFor(self))
return NextActivity; return NextActivity;

View File

@@ -23,13 +23,13 @@ namespace OpenRA.Mods.Common.Activities
[Flags] [Flags]
protected enum AttackStatus { UnableToAttack, NeedsToTurn, NeedsToMove, Attacking } protected enum AttackStatus { UnableToAttack, NeedsToTurn, NeedsToMove, Attacking }
protected readonly Target Target; readonly AttackFrontal[] attackTraits;
readonly AttackBase[] attackTraits; readonly RevealsShroud[] revealsShroud;
readonly IMove move; readonly IMove move;
readonly IFacing facing; readonly IFacing facing;
readonly IPositionable positionable; readonly IPositionable positionable;
readonly bool forceAttack; readonly bool forceAttack;
readonly int facingTolerance; protected Target target;
WDist minRange; WDist minRange;
WDist maxRange; WDist maxRange;
@@ -37,22 +37,27 @@ namespace OpenRA.Mods.Common.Activities
Activity moveActivity; Activity moveActivity;
AttackStatus attackStatus = AttackStatus.UnableToAttack; AttackStatus attackStatus = AttackStatus.UnableToAttack;
public Attack(Actor self, Target target, bool allowMovement, bool forceAttack, int facingTolerance) public Attack(Actor self, Target target, bool allowMovement, bool forceAttack)
{ {
Target = target; this.target = target;
this.forceAttack = forceAttack; this.forceAttack = forceAttack;
this.facingTolerance = facingTolerance;
attackTraits = self.TraitsImplementing<AttackBase>().ToArray(); attackTraits = self.TraitsImplementing<AttackFrontal>().ToArray();
revealsShroud = self.TraitsImplementing<RevealsShroud>().ToArray();
facing = self.Trait<IFacing>(); facing = self.Trait<IFacing>();
positionable = self.Trait<IPositionable>(); positionable = self.Trait<IPositionable>();
move = allowMovement ? self.TraitOrDefault<IMove>() : null; move = allowMovement ? self.TraitOrDefault<IMove>() : null;
} }
protected virtual Target RecalculateTarget(Actor self)
{
return target.Recalculate(self.Owner);
}
public override Activity Tick(Actor self) public override Activity Tick(Actor self)
{ {
target = RecalculateTarget(self);
turnActivity = moveActivity = null; turnActivity = moveActivity = null;
attackStatus = AttackStatus.UnableToAttack; attackStatus = AttackStatus.UnableToAttack;
@@ -74,31 +79,37 @@ namespace OpenRA.Mods.Common.Activities
return NextActivity; return NextActivity;
} }
protected virtual bool IgnoresVisibility { get { return false; } } protected virtual AttackStatus TickAttack(Actor self, AttackFrontal attack)
protected virtual AttackStatus TickAttack(Actor self, AttackBase attack)
{ {
if (IsCanceled) if (IsCanceled)
return AttackStatus.UnableToAttack; return AttackStatus.UnableToAttack;
var type = Target.Type; if (!target.IsValidFor(self))
if (!Target.IsValidFor(self) || type == TargetType.FrozenActor)
return AttackStatus.UnableToAttack; return AttackStatus.UnableToAttack;
if (attack.Info.AttackRequiresEnteringCell && !positionable.CanEnterCell(Target.Actor.Location, null, false)) if (attack.Info.AttackRequiresEnteringCell && !positionable.CanEnterCell(target.Actor.Location, null, false))
return AttackStatus.UnableToAttack; return AttackStatus.UnableToAttack;
// Drop the target if it moves under the shroud / fog. if (!attack.Info.TargetFrozenActors && !forceAttack && target.Type == TargetType.FrozenActor)
// HACK: This would otherwise break targeting frozen actors {
// The problem is that Shroud.IsTargetable returns false (as it should) for // Try to move within range, drop the target otherwise
// frozen actors, but we do want to explicitly target the underlying actor here. if (move == null)
if (!IgnoresVisibility && type == TargetType.Actor return AttackStatus.UnableToAttack;
&& !Target.Actor.Info.HasTraitInfo<FrozenUnderFogInfo>()
&& !Target.Actor.CanBeViewedByPlayer(self.Owner)) var rs = revealsShroud
return AttackStatus.UnableToAttack; .Where(Exts.IsTraitEnabled)
.MaxByOrDefault(s => s.Range);
// Default to 2 cells if there are no active traits
var sightRange = rs != null ? rs.Range : WDist.FromCells(2);
attackStatus |= AttackStatus.NeedsToMove;
moveActivity = ActivityUtils.SequenceActivities(move.MoveWithinRange(target, sightRange), this);
return AttackStatus.NeedsToMove;
}
// Drop the target once none of the weapons are effective against it // Drop the target once none of the weapons are effective against it
var armaments = attack.ChooseArmamentsForTarget(Target, forceAttack).ToList(); var armaments = attack.ChooseArmamentsForTarget(target, forceAttack).ToList();
if (armaments.Count == 0) if (armaments.Count == 0)
return AttackStatus.UnableToAttack; return AttackStatus.UnableToAttack;
@@ -108,8 +119,8 @@ namespace OpenRA.Mods.Common.Activities
var pos = self.CenterPosition; var pos = self.CenterPosition;
var mobile = move as Mobile; var mobile = move as Mobile;
if (!Target.IsInRange(pos, maxRange) if (!target.IsInRange(pos, maxRange)
|| (minRange.Length != 0 && Target.IsInRange(pos, minRange)) || (minRange.Length != 0 && target.IsInRange(pos, minRange))
|| (mobile != null && !mobile.CanInteractWithGroundLayer(self))) || (mobile != null && !mobile.CanInteractWithGroundLayer(self)))
{ {
// Try to move within range, drop the target otherwise // Try to move within range, drop the target otherwise
@@ -117,13 +128,13 @@ namespace OpenRA.Mods.Common.Activities
return AttackStatus.UnableToAttack; return AttackStatus.UnableToAttack;
attackStatus |= AttackStatus.NeedsToMove; attackStatus |= AttackStatus.NeedsToMove;
moveActivity = ActivityUtils.SequenceActivities(move.MoveWithinRange(Target, minRange, maxRange), this); moveActivity = ActivityUtils.SequenceActivities(move.MoveWithinRange(target, minRange, maxRange), this);
return AttackStatus.NeedsToMove; return AttackStatus.NeedsToMove;
} }
var targetedPosition = attack.GetTargetPosition(pos, Target); var targetedPosition = attack.GetTargetPosition(pos, target);
var desiredFacing = (targetedPosition - pos).Yaw.Facing; var desiredFacing = (targetedPosition - pos).Yaw.Facing;
if (!Util.FacingWithinTolerance(facing.Facing, desiredFacing, facingTolerance)) if (!Util.FacingWithinTolerance(facing.Facing, desiredFacing, ((AttackFrontalInfo)attack.Info).FacingTolerance))
{ {
attackStatus |= AttackStatus.NeedsToTurn; attackStatus |= AttackStatus.NeedsToTurn;
turnActivity = ActivityUtils.SequenceActivities(new Turn(self, desiredFacing), this); turnActivity = ActivityUtils.SequenceActivities(new Turn(self, desiredFacing), this);
@@ -131,9 +142,48 @@ namespace OpenRA.Mods.Common.Activities
} }
attackStatus |= AttackStatus.Attacking; attackStatus |= AttackStatus.Attacking;
attack.DoAttack(self, Target, armaments); attack.DoAttack(self, target, armaments);
return AttackStatus.Attacking; return AttackStatus.Attacking;
} }
} }
public static class TargetExts
{
/// <summary>Update (Frozen)Actor targets to account for visibility changes or actor replacement</summary>
public static Target Recalculate(this Target t, Player viewer)
{
// Check whether the target has transformed into something else
// HACK: This relies on knowing the internal implementation details of Target
if (t.Type == TargetType.Invalid && t.Actor != null && t.Actor.ReplacedByActor != null)
t = Target.FromActor(t.Actor.ReplacedByActor);
if (t.Type == TargetType.Actor)
{
// Actor has been hidden under the fog
if (!t.Actor.CanBeViewedByPlayer(viewer))
{
// Replace with FrozenActor if applicable, otherwise drop the target
var frozen = viewer.FrozenActorLayer.FromID(t.Actor.ActorID);
return frozen != null ? Target.FromFrozenActor(frozen) : Target.Invalid;
}
}
else if (t.Type == TargetType.FrozenActor)
{
// Frozen actor has been revealed
if (!t.FrozenActor.Visible || !t.FrozenActor.IsValid)
{
// Original actor is still alive
if (t.FrozenActor.Actor != null && t.FrozenActor.Actor.CanBeViewedByPlayer(viewer))
return Target.FromActor(t.FrozenActor.Actor);
// Original actor was killed while hidden
if (t.Actor == null)
return Target.Invalid;
}
}
return t;
}
}
} }

View File

@@ -31,6 +31,9 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Does the attack type require the attacker to enter the target's cell?")] [Desc("Does the attack type require the attacker to enter the target's cell?")]
public readonly bool AttackRequiresEnteringCell = false; public readonly bool AttackRequiresEnteringCell = false;
[Desc("Allow firing into the fog to target frozen actors without requiring force-fire.")]
public readonly bool TargetFrozenActors = false;
[VoiceReference] public readonly string Voice = "Action"; [VoiceReference] public readonly string Voice = "Action";
public override abstract object Create(ActorInitializer init); public override abstract object Create(ActorInitializer init);
@@ -154,12 +157,11 @@ namespace OpenRA.Mods.Common.Traits
var forceAttack = order.OrderString == forceAttackOrderName; var forceAttack = order.OrderString == forceAttackOrderName;
if (forceAttack || order.OrderString == attackOrderName) if (forceAttack || order.OrderString == attackOrderName)
{ {
var target = self.ResolveFrozenActorOrder(order, Color.Red); if (!order.Target.IsValidFor(self))
if (!target.IsValidFor(self))
return; return;
self.SetTargetLine(target, Color.Red); self.SetTargetLine(order.Target, Color.Red);
AttackTarget(target, order.Queued, true, forceAttack); AttackTarget(order.Target, order.Queued, true, forceAttack);
} }
if (order.OrderString == "Stop") if (order.OrderString == "Stop")
@@ -417,7 +419,8 @@ namespace OpenRA.Mods.Common.Traits
if (a == null) if (a == null)
a = armaments.First(); a = armaments.First();
cursor = !target.IsInRange(self.CenterPosition, a.MaxRange()) cursor = !target.IsInRange(self.CenterPosition, a.MaxRange()) ||
(!forceAttack && target.Type == TargetType.FrozenActor && !ab.Info.TargetFrozenActors)
? ab.Info.OutsideRangeCursor ?? a.Info.OutsideRangeCursor ? ab.Info.OutsideRangeCursor ?? a.Info.OutsideRangeCursor
: ab.Info.Cursor ?? a.Info.Cursor; : ab.Info.Cursor ?? a.Info.Cursor;

View File

@@ -12,6 +12,7 @@
using System; using System;
using System.Linq; using System.Linq;
using OpenRA.Activities; using OpenRA.Activities;
using OpenRA.Mods.Common.Activities;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
@@ -31,6 +32,7 @@ namespace OpenRA.Mods.Common.Traits
protected override void Tick(Actor self) protected override void Tick(Actor self)
{ {
Target = Target.Recalculate(self.Owner);
if (IsTraitDisabled) if (IsTraitDisabled)
{ {
Target = Target.Invalid; Target = Target.Invalid;
@@ -62,15 +64,17 @@ namespace OpenRA.Mods.Common.Traits
class AttackActivity : Activity class AttackActivity : Activity
{ {
readonly AttackFollow attack; readonly AttackFollow attack;
readonly RevealsShroud[] revealsShroud;
readonly IMove move; readonly IMove move;
readonly Target target;
readonly bool forceAttack; readonly bool forceAttack;
Target target;
bool hasTicked; bool hasTicked;
public AttackActivity(Actor self, Target target, bool allowMove, bool forceAttack) public AttackActivity(Actor self, Target target, bool allowMove, bool forceAttack)
{ {
attack = self.Trait<AttackFollow>(); attack = self.Trait<AttackFollow>();
move = allowMove ? self.TraitOrDefault<IMove>() : null; move = allowMove ? self.TraitOrDefault<IMove>() : null;
revealsShroud = self.TraitsImplementing<RevealsShroud>().ToArray();
this.target = target; this.target = target;
this.forceAttack = forceAttack; this.forceAttack = forceAttack;
@@ -78,6 +82,7 @@ namespace OpenRA.Mods.Common.Traits
public override Activity Tick(Actor self) public override Activity Tick(Actor self)
{ {
target = target.Recalculate(self.Owner);
if (IsCanceled || !target.IsValidFor(self)) if (IsCanceled || !target.IsValidFor(self))
return NextActivity; return NextActivity;
@@ -87,6 +92,11 @@ namespace OpenRA.Mods.Common.Traits
var weapon = attack.ChooseArmamentsForTarget(target, forceAttack).FirstOrDefault(); var weapon = attack.ChooseArmamentsForTarget(target, forceAttack).FirstOrDefault();
if (weapon != null) if (weapon != null)
{ {
// Check that AttackFollow hasn't cancelled the target by modifying attack.Target
// Having both this and AttackFollow modify that field is a horrible hack.
if (hasTicked && attack.Target.Type == TargetType.Invalid)
return NextActivity;
var targetIsMobile = (target.Type == TargetType.Actor && target.Actor.Info.HasTraitInfo<IMoveInfo>()) var targetIsMobile = (target.Type == TargetType.Actor && target.Actor.Info.HasTraitInfo<IMoveInfo>())
|| (target.Type == TargetType.FrozenActor && target.FrozenActor.Info.HasTraitInfo<IMoveInfo>()); || (target.Type == TargetType.FrozenActor && target.FrozenActor.Info.HasTraitInfo<IMoveInfo>());
@@ -95,10 +105,18 @@ namespace OpenRA.Mods.Common.Traits
var maxRange = targetIsMobile ? new WDist(Math.Max(weapon.Weapon.MinRange.Length, modifiedRange.Length - 1024)) var maxRange = targetIsMobile ? new WDist(Math.Max(weapon.Weapon.MinRange.Length, modifiedRange.Length - 1024))
: modifiedRange; : modifiedRange;
// Check that AttackFollow hasn't cancelled the target by modifying attack.Target // Most actors want to be able to see their target before shooting
// Having both this and AttackFollow modify that field is a horrible hack. if (!attack.Info.TargetFrozenActors && !forceAttack && target.Type == TargetType.FrozenActor)
if (hasTicked && attack.Target.Type == TargetType.Invalid) {
return NextActivity; var rs = revealsShroud
.Where(Exts.IsTraitEnabled)
.MaxByOrDefault(s => s.Range);
// Default to 2 cells if there are no active traits
var sightRange = rs != null ? rs.Range : WDist.FromCells(2);
if (sightRange < maxRange)
maxRange = sightRange;
}
attack.Target = target; attack.Target = target;
hasTicked = true; hasTicked = true;

View File

@@ -57,7 +57,7 @@ namespace OpenRA.Mods.Common.Traits
public override Activity GetAttackActivity(Actor self, Target newTarget, bool allowMove, bool forceAttack) public override Activity GetAttackActivity(Actor self, Target newTarget, bool allowMove, bool forceAttack)
{ {
return new Activities.Attack(self, newTarget, allowMove, forceAttack, info.FacingTolerance); return new Activities.Attack(self, newTarget, allowMove, forceAttack);
} }
} }
} }

View File

@@ -10,6 +10,7 @@
#endregion #endregion
using OpenRA.Activities; using OpenRA.Activities;
using OpenRA.Mods.Common.Activities;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
@@ -32,9 +33,9 @@ namespace OpenRA.Mods.Common.Traits
// Some 3rd-party mods rely on this being public // Some 3rd-party mods rely on this being public
public class SetTarget : Activity public class SetTarget : Activity
{ {
readonly Target target;
readonly AttackOmni attack; readonly AttackOmni attack;
readonly bool allowMove; readonly bool allowMove;
Target target;
public SetTarget(AttackOmni attack, Target target, bool allowMove) public SetTarget(AttackOmni attack, Target target, bool allowMove)
{ {
@@ -45,6 +46,7 @@ namespace OpenRA.Mods.Common.Traits
public override Activity Tick(Actor self) public override Activity Tick(Actor self)
{ {
target = target.Recalculate(self.Owner);
if (IsCanceled || !target.IsValidFor(self) || !attack.IsReachableTarget(target, allowMove)) if (IsCanceled || !target.IsValidFor(self) || !attack.IsReachableTarget(target, allowMove))
return NextActivity; return NextActivity;

View File

@@ -72,16 +72,19 @@ namespace OpenRA.Mods.D2k.Traits
public override Activity GetAttackActivity(Actor self, Target newTarget, bool allowMove, bool forceAttack) public override Activity GetAttackActivity(Actor self, Target newTarget, bool allowMove, bool forceAttack)
{ {
return new SwallowTarget(self, newTarget, allowMove, forceAttack, Info.FacingTolerance); return new SwallowTarget(self, newTarget, allowMove, forceAttack);
} }
public class SwallowTarget : Attack public class SwallowTarget : Attack
{ {
public SwallowTarget(Actor self, Target target, bool allowMovement, bool forceAttack, int facingTolerance) public SwallowTarget(Actor self, Target target, bool allowMovement, bool forceAttack)
: base(self, target, allowMovement, forceAttack, facingTolerance) { } : base(self, target, allowMovement, forceAttack) { }
// Worms ignore visibility, so don't need to recalculate targets protected override Target RecalculateTarget(Actor self)
protected override bool IgnoresVisibility { get { return true; } } {
// Worms ignore visibility, so don't need to recalculate targets
return target;
}
} }
} }
} }

View File

@@ -174,6 +174,7 @@ ARTY:
LocalOffset: 624,0,208 LocalOffset: 624,0,208
MuzzleSequence: muzzle MuzzleSequence: muzzle
AttackFrontal: AttackFrontal:
TargetFrozenActors: True
WithMuzzleOverlay: WithMuzzleOverlay:
AutoTarget: AutoTarget:
InitialStanceAI: Defend InitialStanceAI: Defend
@@ -520,6 +521,7 @@ MSAM:
Weapon: 227mm Weapon: 227mm
LocalOffset: 213,-128,0, 213,128,0 LocalOffset: 213,-128,0, 213,128,0
AttackFrontal: AttackFrontal:
TargetFrozenActors: True
WithSpriteTurret: WithSpriteTurret:
SpawnActorOnDeath: SpawnActorOnDeath:
Actor: MSAM.Husk Actor: MSAM.Husk

View File

@@ -118,6 +118,7 @@ MSUB:
LocalOffset: 0,-171,0, 0,171,0 LocalOffset: 0,-171,0, 0,171,0
FireDelay: 2 FireDelay: 2
AttackFrontal: AttackFrontal:
TargetFrozenActors: True
SelectionDecorations: SelectionDecorations:
AutoTarget: AutoTarget:
InitialStance: HoldFire InitialStance: HoldFire
@@ -234,6 +235,7 @@ CA:
MuzzleSequence: muzzle MuzzleSequence: muzzle
AttackTurreted: AttackTurreted:
Turrets: primary, secondary Turrets: primary, secondary
TargetFrozenActors: True
WithMuzzleOverlay: WithMuzzleOverlay:
SelectionDecorations: SelectionDecorations:
WithSpriteTurret@PRIMARY: WithSpriteTurret@PRIMARY:

View File

@@ -28,6 +28,7 @@ V2RL:
AutoTarget: AutoTarget:
ScanRadius: 10 ScanRadius: 10
AttackFrontal: AttackFrontal:
TargetFrozenActors: True
WithFacingSpriteBody: WithFacingSpriteBody:
RequiresCondition: !reloading RequiresCondition: !reloading
Name: loaded Name: loaded
@@ -263,6 +264,7 @@ ARTY:
LocalOffset: 624,0,208 LocalOffset: 624,0,208
MuzzleSequence: muzzle MuzzleSequence: muzzle
AttackFrontal: AttackFrontal:
TargetFrozenActors: True
WithMuzzleOverlay: WithMuzzleOverlay:
Explodes: Explodes:
Weapon: ArtilleryExplode Weapon: ArtilleryExplode

View File

@@ -363,6 +363,7 @@ JUGG:
Turrets: deployed Turrets: deployed
RequiresCondition: deployed RequiresCondition: deployed
PauseOnCondition: empdisable PauseOnCondition: empdisable
TargetFrozenActors: True
Armament@deployed: Armament@deployed:
Name: deployed Name: deployed
Turret: deployed Turret: deployed

View File

@@ -258,6 +258,7 @@ ART2:
Turrets: deployed Turrets: deployed
RequiresCondition: deployed RequiresCondition: deployed
PauseOnCondition: empdisable PauseOnCondition: empdisable
TargetFrozenActors: True
Armament@deployed: Armament@deployed:
Name: deployed Name: deployed
Turret: deployed Turret: deployed