From ff9db0bf7a067166edbf7039b05105d7649b9653 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Thu, 20 Jun 2019 20:42:57 +0000 Subject: [PATCH] Reset RequestedTargets that are cancelled before the first attack tick. --- .../Activities/Move/AttackMoveActivity.cs | 5 +-- .../Traits/Attack/AttackBase.cs | 7 +++-- .../Traits/Attack/AttackFollow.cs | 31 ++++++++++++------- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/OpenRA.Mods.Common/Activities/Move/AttackMoveActivity.cs b/OpenRA.Mods.Common/Activities/Move/AttackMoveActivity.cs index 6c873dd121..7ad572ae3e 100644 --- a/OpenRA.Mods.Common/Activities/Move/AttackMoveActivity.cs +++ b/OpenRA.Mods.Common/Activities/Move/AttackMoveActivity.cs @@ -63,8 +63,9 @@ namespace OpenRA.Mods.Common.Activities var attackBases = autoTarget.ActiveAttackBases; foreach (var ab in attackBases) { - QueueChild(self, ab.GetAttackActivity(self, target, true, false)); - ab.OnQueueAttackActivity(self, target, false, true, false); + var activity = ab.GetAttackActivity(self, target, true, false); + QueueChild(self, activity); + ab.OnQueueAttackActivity(self, activity, target, true, false); } // Make sure to continue moving when the attack activities have finished. diff --git a/OpenRA.Mods.Common/Traits/Attack/AttackBase.cs b/OpenRA.Mods.Common/Traits/Attack/AttackBase.cs index f1182afad6..524e4d0364 100644 --- a/OpenRA.Mods.Common/Traits/Attack/AttackBase.cs +++ b/OpenRA.Mods.Common/Traits/Attack/AttackBase.cs @@ -392,11 +392,12 @@ namespace OpenRA.Mods.Common.Traits if (!queued) self.CancelActivity(); - self.QueueActivity(GetAttackActivity(self, target, allowMove, forceAttack)); - OnQueueAttackActivity(self, target, queued, allowMove, forceAttack); + var activity = GetAttackActivity(self, target, allowMove, forceAttack); + self.QueueActivity(activity); + OnQueueAttackActivity(self, activity, target, allowMove, forceAttack); } - public virtual void OnQueueAttackActivity(Actor self, Target target, bool queued, bool allowMove, bool forceAttack) { } + public virtual void OnQueueAttackActivity(Actor self, Activity activity, Target target, bool allowMove, bool forceAttack) { } public bool IsReachableTarget(Target target, bool allowMove) { diff --git a/OpenRA.Mods.Common/Traits/Attack/AttackFollow.cs b/OpenRA.Mods.Common/Traits/Attack/AttackFollow.cs index 38d80c8a69..4a7b9c039d 100644 --- a/OpenRA.Mods.Common/Traits/Attack/AttackFollow.cs +++ b/OpenRA.Mods.Common/Traits/Attack/AttackFollow.cs @@ -37,8 +37,8 @@ namespace OpenRA.Mods.Common.Traits Mobile mobile; AutoTarget autoTarget; - int requestedTargetLastTick; bool requestedForceAttack; + Activity requestedTargetPresetForActivity; bool opportunityForceAttack; bool opportunityTargetIsPersistentTarget; @@ -46,7 +46,7 @@ namespace OpenRA.Mods.Common.Traits { RequestedTarget = target; requestedForceAttack = isForceAttack; - requestedTargetLastTick = self.World.WorldTick; + requestedTargetPresetForActivity = null; } public void ClearRequestedTarget() @@ -59,6 +59,7 @@ namespace OpenRA.Mods.Common.Traits } RequestedTarget = Target.Invalid; + requestedTargetPresetForActivity = null; } public AttackFollow(Actor self, AttackFollowInfo info) @@ -100,12 +101,19 @@ namespace OpenRA.Mods.Common.Traits opportunityTargetIsPersistentTarget = false; } - if (requestedTargetLastTick != self.World.WorldTick) + if (requestedTargetPresetForActivity != null) { - // Activities tick before traits, so if we are here it means the activity didn't run - // (either queued next or already cancelled) and we need to recalculate the target ourself - bool targetIsHiddenActor; - RequestedTarget = RequestedTarget.Recalculate(self.Owner, out targetIsHiddenActor); + // RequestedTarget was set by OnQueueAttackActivity in preparation for a queued activity + // requestedTargetPresetForActivity will be cleared once the activity starts running and calls UpdateRequestedTarget + if (self.CurrentActivity != null && self.CurrentActivity.NextActivity == requestedTargetPresetForActivity) + { + bool targetIsHiddenActor; + RequestedTarget = RequestedTarget.Recalculate(self.Owner, out targetIsHiddenActor); + } + + // Requested activity has been canceled + else + ClearRequestedTarget(); } // Can't fire on anything @@ -148,16 +156,15 @@ namespace OpenRA.Mods.Common.Traits return new AttackActivity(self, newTarget, allowMove, forceAttack); } - public override void OnQueueAttackActivity(Actor self, Target target, bool queued, bool allowMove, bool forceAttack) + public override void OnQueueAttackActivity(Actor self, Activity activity, Target target, bool allowMove, bool forceAttack) { - // If not queued we know that the attack activity will run next // We can improve responsiveness for turreted actors by preempting - // the last order (usually a move) and set the target immediately - if (!queued) + // the last order (usually a move) and setting the target immediately + if (self.CurrentActivity != null && self.CurrentActivity.NextActivity == activity) { RequestedTarget = target; requestedForceAttack = forceAttack; - requestedTargetLastTick = self.World.WorldTick; + requestedTargetPresetForActivity = activity; } }