Fix target invalidation and reacquisition in AttackFollow.

This commit is contained in:
Paul Chote
2019-01-25 22:14:02 +00:00
parent 5ef7809002
commit 0bfc487999
4 changed files with 149 additions and 68 deletions

View File

@@ -59,10 +59,10 @@ namespace OpenRA.Mods.Cnc.Traits
{ {
// Check that AttackTDGunboatTurreted hasn't cancelled the target by modifying attack.Target // Check that AttackTDGunboatTurreted hasn't cancelled the target by modifying attack.Target
// Having both this and AttackTDGunboatTurreted modify that field is a horrible hack. // Having both this and AttackTDGunboatTurreted modify that field is a horrible hack.
if (hasTicked && attack.Target.Type == TargetType.Invalid) if (hasTicked && attack.requestedTarget.Type == TargetType.Invalid)
return NextActivity; return NextActivity;
attack.Target = target; attack.requestedTarget = target;
hasTicked = true; hasTicked = true;
} }

View File

@@ -13,7 +13,6 @@ using System;
using System.Drawing; using System.Drawing;
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
@@ -26,24 +25,53 @@ namespace OpenRA.Mods.Common.Traits
public class AttackFollow : AttackBase, INotifyOwnerChanged public class AttackFollow : AttackBase, INotifyOwnerChanged
{ {
public Target Target { get; protected set; } protected Target requestedTarget;
protected bool requestedForceAttack;
Mobile mobile;
public AttackFollow(Actor self, AttackFollowInfo info) public AttackFollow(Actor self, AttackFollowInfo info)
: base(self, info) { } : base(self, info) { }
protected override void Tick(Actor self) protected override void Created(Actor self)
{ {
// We can safely ignore target visibility here - the armament will handle this for us. mobile = self.TraitOrDefault<Mobile>();
bool targetIsHiddenActor; base.Created(self);
Target = Target.Recalculate(self.Owner, out targetIsHiddenActor);
if (IsTraitDisabled)
{
Target = Target.Invalid;
return;
} }
DoAttack(self, Target); protected bool CanAimAtTarget(Actor self, Target target, bool forceAttack)
IsAiming = Target.IsValidFor(self); {
if (target.Type == TargetType.Actor && !target.Actor.CanBeViewedByPlayer(self.Owner))
return false;
if (target.Type == TargetType.FrozenActor && !target.FrozenActor.IsValid)
return false;
var pos = self.CenterPosition;
var armaments = ChooseArmamentsForTarget(target, forceAttack);
foreach (var a in armaments)
if (target.IsInRange(pos, a.MaxRange()) && (a.Weapon.MinRange == WDist.Zero || !target.IsInRange(pos, a.Weapon.MinRange)))
return true;
return false;
}
protected override void Tick(Actor self)
{
if (IsTraitDisabled)
requestedTarget = Target.Invalid;
// Can't fire on anything
if (mobile != null && !mobile.CanInteractWithGroundLayer(self))
return;
if (requestedTarget.Type != TargetType.Invalid)
{
IsAiming = CanAimAtTarget(self, requestedTarget, requestedForceAttack);
if (IsAiming)
DoAttack(self, requestedTarget);
}
else
IsAiming = false;
base.Tick(self); base.Tick(self);
} }
@@ -55,13 +83,18 @@ namespace OpenRA.Mods.Common.Traits
public override void OnStopOrder(Actor self) public override void OnStopOrder(Actor self)
{ {
Target = Target.Invalid; requestedTarget = Target.Invalid;
base.OnStopOrder(self); base.OnStopOrder(self);
} }
public void OnOwnerChanged(Actor self, Player oldOwner, Player newOwner) public bool HasReachableTarget(bool allowMove)
{ {
Target = Target.Invalid; return IsReachableTarget(requestedTarget, allowMove);
}
void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)
{
requestedTarget = Target.Invalid;
} }
class AttackActivity : Activity class AttackActivity : Activity
@@ -70,7 +103,13 @@ namespace OpenRA.Mods.Common.Traits
readonly RevealsShroud[] revealsShroud; readonly RevealsShroud[] revealsShroud;
readonly IMove move; readonly IMove move;
readonly bool forceAttack; readonly bool forceAttack;
Target target; Target target;
Target lastVisibleTarget;
bool useLastVisibleTarget;
WDist lastVisibleMaximumRange;
WDist lastVisibleMinimumRange;
bool wasMovingWithinRange;
bool hasTicked; bool hasTicked;
public AttackActivity(Actor self, Target target, bool allowMove, bool forceAttack) public AttackActivity(Actor self, Target target, bool allowMove, bool forceAttack)
@@ -81,40 +120,58 @@ namespace OpenRA.Mods.Common.Traits
this.target = target; this.target = target;
this.forceAttack = forceAttack; this.forceAttack = forceAttack;
// The target may become hidden between the initial order request and the first tick (e.g. if queued)
// Moving to any position (even if quite stale) is still better than immediately giving up
if ((target.Type == TargetType.Actor && target.Actor.CanBeViewedByPlayer(self.Owner))
|| target.Type == TargetType.FrozenActor || target.Type == TargetType.Terrain)
{
lastVisibleTarget = Target.FromPos(target.CenterPosition);
lastVisibleMaximumRange = attack.GetMaximumRangeVersusTarget(target);
lastVisibleMinimumRange = attack.GetMinimumRangeVersusTarget(target);
}
} }
public override Activity Tick(Actor self) public override Activity Tick(Actor self)
{ {
// All of the interesting behaviour to move to the last known target position if it becomes hidden if (IsCanceled)
// and to reacquire the target if it is revealed enroute is handled inside MoveWithinRange. return NextActivity;
// At this point in the activity chain we are either ticking against the target for the first time
// (and so don't know where it is), or after MoveWithinRange has lost the target and given up. // Check that AttackFollow hasn't cancelled the target by modifying attack.Target
// We can therefore treat a hidden targets as invalid and give up if we can't currently see it. // Having both this and AttackFollow modify that field is a horrible hack.
target = target.RecalculateInvalidatingHiddenTargets(self.Owner); if (hasTicked && attack.requestedTarget.Type == TargetType.Invalid)
if (IsCanceled || !target.IsValidFor(self))
return NextActivity; return NextActivity;
if (attack.IsTraitPaused) if (attack.IsTraitPaused)
return this; return this;
var weapon = attack.ChooseArmamentsForTarget(target, forceAttack).FirstOrDefault(); bool targetIsHiddenActor;
if (weapon != null) attack.requestedForceAttack = forceAttack;
attack.requestedTarget = target = target.Recalculate(self.Owner, out targetIsHiddenActor);
hasTicked = true;
if (!targetIsHiddenActor && target.Type == TargetType.Actor)
{ {
// Check that AttackFollow hasn't cancelled the target by modifying attack.Target lastVisibleTarget = Target.FromTargetPositions(target);
// Having both this and AttackFollow modify that field is a horrible hack. lastVisibleMaximumRange = attack.GetMaximumRangeVersusTarget(target);
if (hasTicked && attack.Target.Type == TargetType.Invalid) lastVisibleMinimumRange = attack.GetMinimumRange();
return NextActivity;
var targetIsMobile = (target.Type == TargetType.Actor && target.Actor.Info.HasTraitInfo<IMoveInfo>()) // Try and sit at least one cell away from the min or max ranges to give some leeway if the target starts moving.
|| (target.Type == TargetType.FrozenActor && target.FrozenActor.Info.HasTraitInfo<IMoveInfo>()); if (target.Actor.Info.HasTraitInfo<IMoveInfo>())
{
var preferMinRange = Math.Min(lastVisibleMinimumRange.Length + 1024, lastVisibleMaximumRange.Length);
var preferMaxRange = Math.Max(lastVisibleMaximumRange.Length - 1024, lastVisibleMinimumRange.Length);
lastVisibleMaximumRange = new WDist((lastVisibleMaximumRange.Length - 1024).Clamp(preferMinRange, preferMaxRange));
}
}
// Try and sit at least one cell closer than the max range to give some leeway if the target starts moving. var oldUseLastVisibleTarget = useLastVisibleTarget;
var modifiedRange = weapon.MaxRange(); var maxRange = lastVisibleMaximumRange;
var maxRange = targetIsMobile ? new WDist(Math.Max(weapon.Weapon.MinRange.Length, modifiedRange.Length - 1024)) var minRange = lastVisibleMinimumRange;
: modifiedRange; useLastVisibleTarget = targetIsHiddenActor || !target.IsValidFor(self);
// Most actors want to be able to see their target before shooting // Most actors want to be able to see their target before shooting
if (!attack.Info.TargetFrozenActors && !forceAttack && target.Type == TargetType.FrozenActor) if (target.Type == TargetType.FrozenActor && !attack.Info.TargetFrozenActors && !forceAttack)
{ {
var rs = revealsShroud var rs = revealsShroud
.Where(Exts.IsTraitEnabled) .Where(Exts.IsTraitEnabled)
@@ -126,23 +183,53 @@ namespace OpenRA.Mods.Common.Traits
maxRange = sightRange; maxRange = sightRange;
} }
attack.Target = target; // If we are ticking again after previously sequencing a MoveWithRange then that move must have completed
hasTicked = true; // Either we are in range and can see the target, or we've lost track of it and should give up
if (wasMovingWithinRange && targetIsHiddenActor)
{
attack.requestedTarget = Target.Invalid;
return NextActivity;
}
if (move != null) // Update target lines if required
return ActivityUtils.SequenceActivities( if (useLastVisibleTarget != oldUseLastVisibleTarget)
move.MoveFollow(self, target, weapon.Weapon.MinRange, maxRange, targetLineColor: Color.Red), self.SetTargetLine(useLastVisibleTarget ? lastVisibleTarget : target, Color.Red, false);
this);
// Target is hidden or dead, and we don't have a fallback position to move towards
if (useLastVisibleTarget && !lastVisibleTarget.IsValidFor(self))
{
attack.requestedTarget = Target.Invalid;
return NextActivity;
}
var pos = self.CenterPosition;
var checkTarget = useLastVisibleTarget ? lastVisibleTarget : target;
// We've reached the required range - if the target is visible and valid then we wait
// otherwise if it is hidden or dead we give up
if (checkTarget.IsInRange(pos, maxRange) && !checkTarget.IsInRange(pos, minRange))
{
if (useLastVisibleTarget)
{
attack.requestedTarget = Target.Invalid;
return NextActivity;
}
if (target.IsInRange(self.CenterPosition, weapon.MaxRange()) &&
!target.IsInRange(self.CenterPosition, weapon.Weapon.MinRange))
return this; return this;
} }
attack.Target = Target.Invalid; // We can't move into range, so give up
if (move == null)
{
attack.requestedTarget = Target.Invalid;
return NextActivity; return NextActivity;
} }
wasMovingWithinRange = true;
return ActivityUtils.SequenceActivities(
move.MoveWithinRange(target, minRange, maxRange, checkTarget.CenterPosition, Color.Red),
this);
}
} }
} }
} }

View File

@@ -236,7 +236,7 @@ namespace OpenRA.Mods.Common.Traits
// PERF: Avoid LINQ. // PERF: Avoid LINQ.
foreach (var attackFollow in attackFollows) foreach (var attackFollow in attackFollows)
if (!attackFollow.IsTraitDisabled && attackFollow.IsReachableTarget(attackFollow.Target, allowMove)) if (!attackFollow.IsTraitDisabled && attackFollow.HasReachableTarget(allowMove))
return false; return false;
return true; return true;

View File

@@ -255,12 +255,6 @@ namespace OpenRA.Mods.Common.Traits
if (attack != null && attack.IsAiming) if (attack != null && attack.IsAiming)
attack.OnStopOrder(self); attack.OnStopOrder(self);
} }
protected override void TraitResumed(Actor self)
{
if (attack != null)
FaceTarget(self, attack.Target);
}
} }
public class TurretFacingInit : IActorInit<int> public class TurretFacingInit : IActorInit<int>