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

@@ -20,25 +20,29 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Cnc.Activities
{
public class LeapAttack : Activity
public class LeapAttack : Activity, IActivityNotifyStanceChanged
{
readonly AttackLeapInfo info;
readonly AttackLeap attack;
readonly Mobile mobile;
readonly bool allowMovement;
readonly bool forceAttack;
Target target;
Target lastVisibleTarget;
bool useLastVisibleTarget;
WDist lastVisibleMinRange;
WDist lastVisibleMaxRange;
BitSet<TargetableType> lastVisibleTargetTypes;
Player lastVisibleOwner;
public LeapAttack(Actor self, Target target, bool allowMovement, AttackLeap attack, AttackLeapInfo info)
public LeapAttack(Actor self, Target target, bool allowMovement, bool forceAttack, AttackLeap attack, AttackLeapInfo info)
{
this.target = target;
this.info = info;
this.attack = attack;
this.allowMovement = allowMovement;
this.forceAttack = forceAttack;
mobile = self.Trait<Mobile>();
// The target may become hidden between the initial order request and the first tick (e.g. if queued)
@@ -49,6 +53,17 @@ namespace OpenRA.Mods.Cnc.Activities
lastVisibleTarget = Target.FromPos(target.CenterPosition);
lastVisibleMinRange = attack.GetMinimumRangeVersusTarget(target);
lastVisibleMaxRange = attack.GetMaximumRangeVersusTarget(target);
if (target.Type == TargetType.Actor)
{
lastVisibleOwner = target.Actor.Owner;
lastVisibleTargetTypes = target.Actor.GetEnabledTargetTypes();
}
else if (target.Type == TargetType.FrozenActor)
{
lastVisibleOwner = target.FrozenActor.Owner;
lastVisibleTargetTypes = target.FrozenActor.TargetTypes;
}
}
}
@@ -76,6 +91,8 @@ namespace OpenRA.Mods.Cnc.Activities
lastVisibleTarget = Target.FromTargetPositions(target);
lastVisibleMinRange = attack.GetMinimumRangeVersusTarget(target);
lastVisibleMaxRange = attack.GetMaximumRangeVersusTarget(target);
lastVisibleOwner = target.Actor.Owner;
lastVisibleTargetTypes = target.Actor.GetEnabledTargetTypes();
}
var oldUseLastVisibleTarget = useLastVisibleTarget;
@@ -141,5 +158,15 @@ namespace OpenRA.Mods.Cnc.Activities
{
attack.IsAiming = false;
}
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 (!autoTarget.HasValidTargetPriority(self, lastVisibleOwner, lastVisibleTargetTypes))
target = Target.Invalid;
}
}
}

View File

@@ -73,7 +73,7 @@ namespace OpenRA.Mods.Cnc.Traits
public override Activity GetAttackActivity(Actor self, Target newTarget, bool allowMove, bool forceAttack)
{
return new LeapAttack(self, newTarget, allowMove, this, info);
return new LeapAttack(self, newTarget, allowMove, forceAttack, this, info);
}
}
}

View File

@@ -75,18 +75,20 @@ namespace OpenRA.Mods.Cnc.Traits
public override Activity GetAttackActivity(Actor self, Target newTarget, bool allowMove, bool forceAttack)
{
return new ChargeAttack(this, newTarget);
return new ChargeAttack(this, newTarget, forceAttack);
}
class ChargeAttack : Activity
class ChargeAttack : Activity, IActivityNotifyStanceChanged
{
readonly AttackTesla attack;
readonly Target target;
readonly bool forceAttack;
public ChargeAttack(AttackTesla attack, Target target)
public ChargeAttack(AttackTesla attack, Target target, bool forceAttack)
{
this.attack = attack;
this.target = target;
this.forceAttack = forceAttack;
}
public override Activity Tick(Actor self)
@@ -114,6 +116,26 @@ namespace OpenRA.Mods.Cnc.Traits
QueueChild(self, new ChargeFire(attack, 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()))
Cancel(self, true);
}
else if (target.Type == TargetType.FrozenActor)
{
var fa = target.FrozenActor;
if (!autoTarget.HasValidTargetPriority(self, fa.Owner, fa.TargetTypes))
Cancel(self, true);
}
}
}
class ChargeFire : Activity