Allow opportunity fire for aircraft.

This commit is contained in:
tovl
2019-03-31 22:09:43 +02:00
committed by reaperrr
parent f16ff9eaa0
commit 9abf715fd7
19 changed files with 114 additions and 92 deletions

View File

@@ -39,6 +39,17 @@ namespace OpenRA.Mods.Common.Traits
[VoiceReference] public readonly string Voice = "Action";
[Desc("Tolerance for attack angle. Range [0, 128], 128 covers 360 degrees.")]
public readonly int FacingTolerance = 128;
public override void RulesetLoaded(Ruleset rules, ActorInfo ai)
{
base.RulesetLoaded(rules, ai);
if (FacingTolerance < 0 || FacingTolerance > 128)
throw new YamlException("Facing tolerance must be in range of [0, 128], 128 covers 360 degrees.");
}
public override abstract object Create(ActorInitializer init);
}
@@ -101,6 +112,21 @@ namespace OpenRA.Mods.Common.Traits
return () => armaments;
}
public bool TargetInFiringArc(Actor self, Target target, int facingTolerance)
{
if (facing == null)
return true;
var pos = self.CenterPosition;
var targetedPosition = GetTargetPosition(pos, target);
var delta = targetedPosition - pos;
if (delta.HorizontalLengthSquared == 0)
return true;
return Util.FacingWithinTolerance(facing.Facing, delta.Yaw.Facing, facingTolerance);
}
protected virtual bool CanAttack(Actor self, Target target)
{
if (!self.IsInWorld || IsTraitDisabled || IsTraitPaused)

View File

@@ -23,21 +23,28 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Automatically acquire and fire on targets of opportunity when not actively attacking.")]
public readonly bool OpportunityFire = true;
[Desc("Keep firing on targets even after attack order is cancelled")]
public readonly bool PersistentTargeting = true;
public override object Create(ActorInitializer init) { return new AttackFollow(init.Self, this); }
}
public class AttackFollow : AttackBase, INotifyOwnerChanged
{
public new readonly AttackFollowInfo Info;
public Target RequestedTarget;
protected bool requestedForceAttack;
public bool RequestedForceAttack;
public int RequestedTargetLastTick;
public Target OpportunityTarget;
protected bool opportunityForceAttack;
public bool OpportunityForceAttack;
Mobile mobile;
AutoTarget autoTarget;
public AttackFollow(Actor self, AttackFollowInfo info)
: base(self, info) { }
: base(self, info)
{
Info = info;
}
protected override void Created(Actor self)
{
@@ -58,7 +65,8 @@ namespace OpenRA.Mods.Common.Traits
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;
if (TargetInFiringArc(self, target, Info.FacingTolerance))
return true;
return false;
}
@@ -82,7 +90,7 @@ namespace OpenRA.Mods.Common.Traits
if (RequestedTarget.Type != TargetType.Invalid)
{
IsAiming = CanAimAtTarget(self, RequestedTarget, requestedForceAttack);
IsAiming = CanAimAtTarget(self, RequestedTarget, RequestedForceAttack);
if (IsAiming)
DoAttack(self, RequestedTarget);
}
@@ -91,16 +99,16 @@ namespace OpenRA.Mods.Common.Traits
IsAiming = false;
if (OpportunityTarget.Type != TargetType.Invalid)
IsAiming = CanAimAtTarget(self, OpportunityTarget, opportunityForceAttack);
IsAiming = CanAimAtTarget(self, OpportunityTarget, OpportunityForceAttack);
if (!IsAiming && ((AttackFollowInfo)Info).OpportunityFire && autoTarget != null &&
if (!IsAiming && Info.OpportunityFire && autoTarget != null &&
!autoTarget.IsTraitDisabled && autoTarget.Stance >= UnitStance.Defend)
{
OpportunityTarget = autoTarget.ScanForTarget(self, false);
opportunityForceAttack = false;
OpportunityTarget = autoTarget.ScanForTarget(self, false, false);
OpportunityForceAttack = false;
if (OpportunityTarget.Type != TargetType.Invalid)
IsAiming = CanAimAtTarget(self, OpportunityTarget, opportunityForceAttack);
IsAiming = CanAimAtTarget(self, OpportunityTarget, OpportunityForceAttack);
}
if (IsAiming)
@@ -123,7 +131,7 @@ namespace OpenRA.Mods.Common.Traits
if (!queued)
{
RequestedTarget = target;
requestedForceAttack = forceAttack;
RequestedForceAttack = forceAttack;
RequestedTargetLastTick = self.World.WorldTick;
}
}
@@ -186,8 +194,12 @@ namespace OpenRA.Mods.Common.Traits
if (IsCanceling)
{
// Cancel the requested target, but keep firing on it while in range
attack.OpportunityTarget = attack.RequestedTarget;
attack.opportunityForceAttack = attack.requestedForceAttack;
if (attack.Info.PersistentTargeting)
{
attack.OpportunityTarget = attack.RequestedTarget;
attack.OpportunityForceAttack = attack.RequestedForceAttack;
}
attack.RequestedTarget = Target.Invalid;
return NextActivity;
}
@@ -201,7 +213,7 @@ namespace OpenRA.Mods.Common.Traits
return this;
bool targetIsHiddenActor;
attack.requestedForceAttack = forceAttack;
attack.RequestedForceAttack = forceAttack;
attack.RequestedTarget = target = target.Recalculate(self.Owner, out targetIsHiddenActor);
attack.RequestedTargetLastTick = self.World.WorldTick;
hasTicked = true;

View File

@@ -17,27 +17,20 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Unit got to face the target")]
public class AttackFrontalInfo : AttackBaseInfo, Requires<IFacingInfo>
{
public readonly int FacingTolerance = 0;
public override void RulesetLoaded(Ruleset rules, ActorInfo ai)
{
base.RulesetLoaded(rules, ai);
if (FacingTolerance < 0 || FacingTolerance > 128)
throw new YamlException("Facing tolerance must be in range of [0, 128], 128 covers 360 degrees.");
}
[Desc("Tolerance for attack angle. Range [0, 128], 128 covers 360 degrees.")]
public readonly new int FacingTolerance = 0;
public override object Create(ActorInitializer init) { return new AttackFrontal(init.Self, this); }
}
public class AttackFrontal : AttackBase
{
readonly AttackFrontalInfo info;
public new readonly AttackFrontalInfo Info;
public AttackFrontal(Actor self, AttackFrontalInfo info)
: base(self, info)
{
this.info = info;
this.Info = info;
}
protected override bool CanAttack(Actor self, Target target)
@@ -45,14 +38,7 @@ namespace OpenRA.Mods.Common.Traits
if (!base.CanAttack(self, target))
return false;
var pos = self.CenterPosition;
var targetedPosition = GetTargetPosition(pos, target);
var delta = targetedPosition - pos;
if (delta.HorizontalLengthSquared == 0)
return true;
return Util.FacingWithinTolerance(facing.Facing, delta.Yaw.Facing, info.FacingTolerance);
return TargetInFiringArc(self, target, Info.FacingTolerance);
}
public override Activity GetAttackActivity(Actor self, Target newTarget, bool allowMove, bool forceAttack)