Skip to content

Commit

Permalink
Reduce duplication around AttackFollow's targets.
Browse files Browse the repository at this point in the history
  • Loading branch information
pchote authored and reaperrr committed Jun 29, 2019
1 parent caa440c commit 8f7426f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 49 deletions.
2 changes: 1 addition & 1 deletion OpenRA.Mods.Cnc/Traits/Attack/AttackTDGunboatTurreted.cs
Expand Up @@ -62,7 +62,7 @@ public override Activity Tick(Actor self)
if (hasTicked && attack.RequestedTarget.Type == TargetType.Invalid)
return NextActivity;

attack.RequestedTarget = target;
attack.SetRequestedTarget(self, target);
hasTicked = true;
}

Expand Down
19 changes: 6 additions & 13 deletions OpenRA.Mods.Common/Activities/Air/FlyAttack.cs
Expand Up @@ -79,14 +79,7 @@ public override Activity Tick(Actor self)
if (IsCanceling)
{
// Cancel the requested target, but keep firing on it while in range
if (attackAircraft.Info.PersistentTargeting)
{
attackAircraft.OpportunityTarget = attackAircraft.RequestedTarget;
attackAircraft.OpportunityForceAttack = attackAircraft.RequestedForceAttack;
attackAircraft.OpportunityTargetIsPersistentTarget = true;
}

attackAircraft.RequestedTarget = Target.Invalid;
attackAircraft.ClearRequestedTarget();
return NextActivity;
}

Expand All @@ -99,8 +92,8 @@ public override Activity Tick(Actor self)
return this;

bool targetIsHiddenActor;
attackAircraft.RequestedTarget = target = target.Recalculate(self.Owner, out targetIsHiddenActor);
attackAircraft.RequestedTargetLastTick = self.World.WorldTick;
target = target.Recalculate(self.Owner, out targetIsHiddenActor);
attackAircraft.SetRequestedTarget(self, target, forceAttack);
hasTicked = true;

if (!targetIsHiddenActor && target.Type == TargetType.Actor)
Expand All @@ -121,7 +114,7 @@ public override Activity Tick(Actor self)
// Target is hidden or dead, and we don't have a fallback position to move towards
if (useLastVisibleTarget && !lastVisibleTarget.IsValidFor(self))
{
attackAircraft.RequestedTarget = Target.Invalid;
attackAircraft.ClearRequestedTarget();
return NextActivity;
}

Expand All @@ -141,7 +134,7 @@ public override Activity Tick(Actor self)
// We've reached the assumed position but it is not there - give up
if (checkTarget.IsInRange(pos, lastVisibleMaximumRange))
{
attackAircraft.RequestedTarget = Target.Invalid;
attackAircraft.ClearRequestedTarget();
return NextActivity;
}

Expand Down Expand Up @@ -184,7 +177,7 @@ void IActivityNotifyStanceChanged.StanceChanged(Actor self, AutoTarget autoTarge
return;

if (!autoTarget.HasValidTargetPriority(self, lastVisibleOwner, lastVisibleTargetTypes))
attackAircraft.RequestedTarget = Target.Invalid;
attackAircraft.ClearRequestedTarget();
}
}
}
81 changes: 46 additions & 35 deletions OpenRA.Mods.Common/Traits/Attack/AttackFollow.cs
Expand Up @@ -32,15 +32,34 @@ public class AttackFollowInfo : AttackBaseInfo
public class AttackFollow : AttackBase, INotifyOwnerChanged, IDisableAutoTarget, INotifyStanceChanged
{
public new readonly AttackFollowInfo Info;
public Target RequestedTarget;
public bool RequestedForceAttack;
public int RequestedTargetLastTick;
public Target OpportunityTarget;
public bool OpportunityForceAttack;
public bool OpportunityTargetIsPersistentTarget;
public Target RequestedTarget { get; private set; }
public Target OpportunityTarget { get; private set; }

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

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

public void ClearRequestedTarget()
{
if (Info.PersistentTargeting)
{
OpportunityTarget = RequestedTarget;
opportunityForceAttack = requestedForceAttack;
opportunityTargetIsPersistentTarget = true;
}

RequestedTarget = Target.Invalid;
}

public AttackFollow(Actor self, AttackFollowInfo info)
: base(self, info)
Expand Down Expand Up @@ -78,10 +97,10 @@ protected override void Tick(Actor self)
if (IsTraitDisabled)
{
RequestedTarget = OpportunityTarget = Target.Invalid;
OpportunityTargetIsPersistentTarget = false;
opportunityTargetIsPersistentTarget = false;
}

if (RequestedTargetLastTick != self.World.WorldTick)
if (requestedTargetLastTick != self.World.WorldTick)
{
// 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
Expand All @@ -95,7 +114,7 @@ protected override void Tick(Actor self)

if (RequestedTarget.Type != TargetType.Invalid)
{
IsAiming = CanAimAtTarget(self, RequestedTarget, RequestedForceAttack);
IsAiming = CanAimAtTarget(self, RequestedTarget, requestedForceAttack);
if (IsAiming)
DoAttack(self, RequestedTarget);
}
Expand All @@ -104,17 +123,17 @@ protected override void Tick(Actor self)
IsAiming = false;

if (OpportunityTarget.Type != TargetType.Invalid)
IsAiming = CanAimAtTarget(self, OpportunityTarget, OpportunityForceAttack);
IsAiming = CanAimAtTarget(self, OpportunityTarget, opportunityForceAttack);

if (!IsAiming && Info.OpportunityFire && autoTarget != null &&
!autoTarget.IsTraitDisabled && autoTarget.Stance >= UnitStance.Defend)
{
OpportunityTarget = autoTarget.ScanForTarget(self, false, false);
OpportunityForceAttack = false;
OpportunityTargetIsPersistentTarget = false;
opportunityForceAttack = false;
opportunityTargetIsPersistentTarget = false;

if (OpportunityTarget.Type != TargetType.Invalid)
IsAiming = CanAimAtTarget(self, OpportunityTarget, OpportunityForceAttack);
IsAiming = CanAimAtTarget(self, OpportunityTarget, opportunityForceAttack);
}

if (IsAiming)
Expand All @@ -137,34 +156,34 @@ public override void OnQueueAttackActivity(Actor self, Target target, bool queue
if (!queued)
{
RequestedTarget = target;
RequestedForceAttack = forceAttack;
RequestedTargetLastTick = self.World.WorldTick;
requestedForceAttack = forceAttack;
requestedTargetLastTick = self.World.WorldTick;
}
}

public override void OnStopOrder(Actor self)
{
RequestedTarget = OpportunityTarget = Target.Invalid;
OpportunityTargetIsPersistentTarget = false;
opportunityTargetIsPersistentTarget = false;
base.OnStopOrder(self);
}

void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)
{
RequestedTarget = OpportunityTarget = Target.Invalid;
OpportunityTargetIsPersistentTarget = false;
opportunityTargetIsPersistentTarget = false;
}

bool IDisableAutoTarget.DisableAutoTarget(Actor self)
{
return RequestedTarget.Type != TargetType.Invalid ||
(OpportunityTargetIsPersistentTarget && OpportunityTarget.Type != TargetType.Invalid);
(opportunityTargetIsPersistentTarget && OpportunityTarget.Type != TargetType.Invalid);
}

void INotifyStanceChanged.StanceChanged(Actor self, AutoTarget autoTarget, UnitStance oldStance, UnitStance newStance)
{
// Cancel opportunity targets when switching to a more restrictive stance if they are no longer valid for auto-targeting
if (newStance > oldStance || OpportunityForceAttack)
if (newStance > oldStance || opportunityForceAttack)
return;

if (OpportunityTarget.Type == TargetType.Actor)
Expand Down Expand Up @@ -241,14 +260,7 @@ public override Activity Tick(Actor self)
if (IsCanceling)
{
// Cancel the requested target, but keep firing on it while in range
if (attack.Info.PersistentTargeting)
{
attack.OpportunityTarget = attack.RequestedTarget;
attack.OpportunityForceAttack = attack.RequestedForceAttack;
attack.OpportunityTargetIsPersistentTarget = true;
}

attack.RequestedTarget = Target.Invalid;
attack.ClearRequestedTarget();
return NextActivity;
}

Expand All @@ -261,9 +273,8 @@ public override Activity Tick(Actor self)
return this;

bool targetIsHiddenActor;
attack.RequestedForceAttack = forceAttack;
attack.RequestedTarget = target = target.Recalculate(self.Owner, out targetIsHiddenActor);
attack.RequestedTargetLastTick = self.World.WorldTick;
target = target.Recalculate(self.Owner, out targetIsHiddenActor);
attack.SetRequestedTarget(self, target, forceAttack);
hasTicked = true;

if (!targetIsHiddenActor && target.Type == TargetType.Actor)
Expand Down Expand Up @@ -305,7 +316,7 @@ public override Activity Tick(Actor self)
// Either we are in range and can see the target, or we've lost track of it and should give up
if (wasMovingWithinRange && targetIsHiddenActor)
{
attack.RequestedTarget = Target.Invalid;
attack.ClearRequestedTarget();
return NextActivity;
}

Expand All @@ -316,7 +327,7 @@ public override Activity Tick(Actor self)
// Target is hidden or dead, and we don't have a fallback position to move towards
if (useLastVisibleTarget && !lastVisibleTarget.IsValidFor(self))
{
attack.RequestedTarget = Target.Invalid;
attack.ClearRequestedTarget();
return NextActivity;
}

Expand All @@ -329,7 +340,7 @@ public override Activity Tick(Actor self)
{
if (useLastVisibleTarget)
{
attack.RequestedTarget = Target.Invalid;
attack.ClearRequestedTarget();
return NextActivity;
}

Expand All @@ -339,7 +350,7 @@ public override Activity Tick(Actor self)
// We can't move into range, so give up
if (move == null || maxRange == WDist.Zero || maxRange < minRange)
{
attack.RequestedTarget = Target.Invalid;
attack.ClearRequestedTarget();
return NextActivity;
}

Expand All @@ -355,7 +366,7 @@ void IActivityNotifyStanceChanged.StanceChanged(Actor self, AutoTarget autoTarge
return;

if (!autoTarget.HasValidTargetPriority(self, lastVisibleOwner, lastVisibleTargetTypes))
attack.RequestedTarget = Target.Invalid;
attack.ClearRequestedTarget();
}
}
}
Expand Down

0 comments on commit 8f7426f

Please sign in to comment.