Skip to content

Commit

Permalink
Reset RequestedTargets that are cancelled before the first attack tick.
Browse files Browse the repository at this point in the history
  • Loading branch information
pchote authored and reaperrr committed Jun 29, 2019
1 parent 8f7426f commit ff9db0b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
5 changes: 3 additions & 2 deletions OpenRA.Mods.Common/Activities/Move/AttackMoveActivity.cs
Expand Up @@ -63,8 +63,9 @@ public override Activity Tick(Actor self)
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.
Expand Down
7 changes: 4 additions & 3 deletions OpenRA.Mods.Common/Traits/Attack/AttackBase.cs
Expand Up @@ -392,11 +392,12 @@ public void AttackTarget(Target target, bool queued, bool allowMove, bool forceA
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)
{
Expand Down
31 changes: 19 additions & 12 deletions OpenRA.Mods.Common/Traits/Attack/AttackFollow.cs
Expand Up @@ -37,16 +37,16 @@ public class AttackFollow : AttackBase, INotifyOwnerChanged, IDisableAutoTarget,

Mobile mobile;
AutoTarget autoTarget;
int requestedTargetLastTick;
bool requestedForceAttack;
Activity requestedTargetPresetForActivity;
bool opportunityForceAttack;
bool opportunityTargetIsPersistentTarget;

public void SetRequestedTarget(Actor self, Target target, bool isForceAttack = false)
{
RequestedTarget = target;
requestedForceAttack = isForceAttack;
requestedTargetLastTick = self.World.WorldTick;
requestedTargetPresetForActivity = null;
}

public void ClearRequestedTarget()
Expand All @@ -59,6 +59,7 @@ public void ClearRequestedTarget()
}

RequestedTarget = Target.Invalid;
requestedTargetPresetForActivity = null;
}

public AttackFollow(Actor self, AttackFollowInfo info)
Expand Down Expand Up @@ -100,12 +101,19 @@ protected override void Tick(Actor self)
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
Expand Down Expand Up @@ -148,16 +156,15 @@ public override Activity GetAttackActivity(Actor self, Target newTarget, bool al
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;
}
}

Expand Down

0 comments on commit ff9db0b

Please sign in to comment.