Implement a secondary target-of-opportunity for AttackFollow.

This commit is contained in:
Paul Chote
2019-02-02 11:18:50 +00:00
committed by Oliver Brakmann
parent 1830b3ee80
commit 012b17b974
2 changed files with 21 additions and 28 deletions

View File

@@ -28,6 +28,8 @@ namespace OpenRA.Mods.Common.Traits
protected Target requestedTarget;
protected bool requestedForceAttack;
protected int requestedTargetLastTick;
protected Target opportunityTarget;
protected bool opportunityForceAttack;
Mobile mobile;
public AttackFollow(Actor self, AttackFollowInfo info)
@@ -59,7 +61,7 @@ namespace OpenRA.Mods.Common.Traits
protected override void Tick(Actor self)
{
if (IsTraitDisabled)
requestedTarget = Target.Invalid;
requestedTarget = opportunityTarget = Target.Invalid;
if (requestedTargetLastTick != self.World.WorldTick)
{
@@ -79,6 +81,12 @@ namespace OpenRA.Mods.Common.Traits
if (IsAiming)
DoAttack(self, requestedTarget);
}
else if (opportunityTarget.Type != TargetType.Invalid)
{
IsAiming = CanAimAtTarget(self, opportunityTarget, opportunityForceAttack);
if (IsAiming)
DoAttack(self, opportunityTarget);
}
else
IsAiming = false;
@@ -105,18 +113,13 @@ namespace OpenRA.Mods.Common.Traits
public override void OnStopOrder(Actor self)
{
requestedTarget = Target.Invalid;
requestedTarget = opportunityTarget = Target.Invalid;
base.OnStopOrder(self);
}
public bool HasReachableTarget(bool allowMove)
{
return IsReachableTarget(requestedTarget, allowMove);
}
void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)
{
requestedTarget = Target.Invalid;
requestedTarget = opportunityTarget = Target.Invalid;
}
class AttackActivity : Activity
@@ -157,7 +160,13 @@ namespace OpenRA.Mods.Common.Traits
public override Activity Tick(Actor self)
{
if (IsCanceled)
{
// Cancel the requested target, but keep firing on it while in range
attack.opportunityTarget = attack.requestedTarget;
attack.opportunityForceAttack = attack.requestedForceAttack;
attack.requestedTarget = Target.Invalid;
return NextActivity;
}
// Check that AttackFollow hasn't cancelled the target by modifying attack.Target
// Having both this and AttackFollow modify that field is a horrible hack.

View File

@@ -112,7 +112,6 @@ namespace OpenRA.Mods.Common.Traits
public class AutoTarget : ConditionalTrait<AutoTargetInfo>, INotifyIdle, INotifyDamage, ITick, IResolveOrder, ISync, INotifyCreated, INotifyOwnerChanged
{
readonly IEnumerable<AttackBase> activeAttackBases;
readonly AttackFollow[] attackFollows;
[Sync] int nextScanTime = 0;
public UnitStance Stance { get { return stance; } }
@@ -161,7 +160,6 @@ namespace OpenRA.Mods.Common.Traits
stance = self.Owner.IsBot || !self.Owner.Playable ? info.InitialStanceAI : info.InitialStance;
PredictedStance = stance;
attackFollows = self.TraitsImplementing<AttackFollow>().ToArray();
}
void INotifyCreated.Created(Actor self)
@@ -221,9 +219,8 @@ namespace OpenRA.Mods.Common.Traits
Aggressor = attacker;
bool allowMove;
if (ShouldAttack(out allowMove))
Attack(self, Target.FromActor(Aggressor), allowMove);
var allowMove = Info.AllowMovement && Stance > UnitStance.Defend;
Attack(self, Target.FromActor(Aggressor), allowMove);
}
void INotifyIdle.TickIdle(Actor self)
@@ -231,21 +228,8 @@ namespace OpenRA.Mods.Common.Traits
if (IsTraitDisabled || Stance < UnitStance.Defend)
return;
bool allowMove;
if (ShouldAttack(out allowMove))
ScanAndAttack(self, allowMove);
}
bool ShouldAttack(out bool allowMove)
{
allowMove = Info.AllowMovement && Stance > UnitStance.Defend;
// PERF: Avoid LINQ.
foreach (var attackFollow in attackFollows)
if (!attackFollow.IsTraitDisabled && attackFollow.HasReachableTarget(allowMove))
return false;
return true;
var allowMove = Info.AllowMovement && Stance > UnitStance.Defend;
ScanAndAttack(self, allowMove);
}
void ITick.Tick(Actor self)