From c26e77552eadd86aa5fdb50d4ffaf057a1f309ef Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Fri, 25 Jan 2019 23:49:03 +0000 Subject: [PATCH] Allow turreted actors to acquire targets while doing other activities. --- .../Traits/Attack/AttackFollow.cs | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/OpenRA.Mods.Common/Traits/Attack/AttackFollow.cs b/OpenRA.Mods.Common/Traits/Attack/AttackFollow.cs index 49f0280a53..771908f541 100644 --- a/OpenRA.Mods.Common/Traits/Attack/AttackFollow.cs +++ b/OpenRA.Mods.Common/Traits/Attack/AttackFollow.cs @@ -20,6 +20,9 @@ namespace OpenRA.Mods.Common.Traits [Desc("Actor will follow units until in range to attack them.")] public class AttackFollowInfo : AttackBaseInfo { + [Desc("Automatically acquire and fire on targets of opportunity when not actively attacking.")] + public readonly bool OpportunityFire = true; + public override object Create(ActorInitializer init) { return new AttackFollow(init.Self, this); } } @@ -31,6 +34,7 @@ namespace OpenRA.Mods.Common.Traits protected Target opportunityTarget; protected bool opportunityForceAttack; Mobile mobile; + AutoTarget autoTarget; public AttackFollow(Actor self, AttackFollowInfo info) : base(self, info) { } @@ -38,6 +42,7 @@ namespace OpenRA.Mods.Common.Traits protected override void Created(Actor self) { mobile = self.TraitOrDefault(); + autoTarget = self.TraitOrDefault(); base.Created(self); } @@ -81,14 +86,26 @@ namespace OpenRA.Mods.Common.Traits if (IsAiming) DoAttack(self, requestedTarget); } - else if (opportunityTarget.Type != TargetType.Invalid) + else { - IsAiming = CanAimAtTarget(self, opportunityTarget, opportunityForceAttack); + IsAiming = false; + + if (opportunityTarget.Type != TargetType.Invalid) + IsAiming = CanAimAtTarget(self, opportunityTarget, opportunityForceAttack); + + if (!IsAiming && ((AttackFollowInfo)Info).OpportunityFire && autoTarget != null && + !autoTarget.IsTraitDisabled && autoTarget.Stance >= UnitStance.Defend) + { + opportunityTarget = autoTarget.ScanForTarget(self, false); + opportunityForceAttack = false; + + if (opportunityTarget.Type != TargetType.Invalid) + IsAiming = CanAimAtTarget(self, opportunityTarget, opportunityForceAttack); + } + if (IsAiming) DoAttack(self, opportunityTarget); } - else - IsAiming = false; base.Tick(self); }