Drop targets when switching to a more restrictive stance.

This commit is contained in:
Paul Chote
2019-05-19 16:29:59 +01:00
committed by abcdefg30
parent 62b5d22e53
commit 3ca9d4b773
10 changed files with 233 additions and 18 deletions

View File

@@ -27,21 +27,23 @@ namespace OpenRA.Mods.Common.Traits
public override Activity GetAttackActivity(Actor self, Target newTarget, bool allowMove, bool forceAttack)
{
return new SetTarget(this, newTarget, allowMove);
return new SetTarget(this, newTarget, allowMove, forceAttack);
}
// Some 3rd-party mods rely on this being public
public class SetTarget : Activity
public class SetTarget : Activity, IActivityNotifyStanceChanged
{
readonly AttackOmni attack;
readonly bool allowMove;
readonly bool forceAttack;
Target target;
public SetTarget(AttackOmni attack, Target target, bool allowMove)
public SetTarget(AttackOmni attack, Target target, bool allowMove, bool forceAttack)
{
this.target = target;
this.attack = attack;
this.allowMove = allowMove;
this.forceAttack = forceAttack;
}
public override Activity Tick(Actor self)
@@ -55,6 +57,26 @@ namespace OpenRA.Mods.Common.Traits
attack.DoAttack(self, target);
return this;
}
void IActivityNotifyStanceChanged.StanceChanged(Actor self, AutoTarget autoTarget, UnitStance oldStance, UnitStance newStance)
{
// Cancel non-forced targets when switching to a more restrictive stance if they are no longer valid for auto-targeting
if (newStance > oldStance || forceAttack)
return;
if (target.Type == TargetType.Actor)
{
var a = target.Actor;
if (!autoTarget.HasValidTargetPriority(self, a.Owner, a.GetEnabledTargetTypes()))
target = Target.Invalid;
}
else if (target.Type == TargetType.FrozenActor)
{
var fa = target.FrozenActor;
if (!autoTarget.HasValidTargetPriority(self, fa.Owner, fa.TargetTypes))
target = Target.Invalid;
}
}
}
}
}