Reduce duplication around AttackFollow's targets.

This commit is contained in:
Paul Chote
2019-06-20 20:32:31 +00:00
committed by reaperrr
parent caa440ce9a
commit 8f7426f579
3 changed files with 53 additions and 49 deletions

View File

@@ -62,7 +62,7 @@ namespace OpenRA.Mods.Cnc.Traits
if (hasTicked && attack.RequestedTarget.Type == TargetType.Invalid) if (hasTicked && attack.RequestedTarget.Type == TargetType.Invalid)
return NextActivity; return NextActivity;
attack.RequestedTarget = target; attack.SetRequestedTarget(self, target);
hasTicked = true; hasTicked = true;
} }

View File

@@ -79,14 +79,7 @@ namespace OpenRA.Mods.Common.Activities
if (IsCanceling) if (IsCanceling)
{ {
// Cancel the requested target, but keep firing on it while in range // Cancel the requested target, but keep firing on it while in range
if (attackAircraft.Info.PersistentTargeting) attackAircraft.ClearRequestedTarget();
{
attackAircraft.OpportunityTarget = attackAircraft.RequestedTarget;
attackAircraft.OpportunityForceAttack = attackAircraft.RequestedForceAttack;
attackAircraft.OpportunityTargetIsPersistentTarget = true;
}
attackAircraft.RequestedTarget = Target.Invalid;
return NextActivity; return NextActivity;
} }
@@ -99,8 +92,8 @@ namespace OpenRA.Mods.Common.Activities
return this; return this;
bool targetIsHiddenActor; bool targetIsHiddenActor;
attackAircraft.RequestedTarget = target = target.Recalculate(self.Owner, out targetIsHiddenActor); target = target.Recalculate(self.Owner, out targetIsHiddenActor);
attackAircraft.RequestedTargetLastTick = self.World.WorldTick; attackAircraft.SetRequestedTarget(self, target, forceAttack);
hasTicked = true; hasTicked = true;
if (!targetIsHiddenActor && target.Type == TargetType.Actor) if (!targetIsHiddenActor && target.Type == TargetType.Actor)
@@ -121,7 +114,7 @@ namespace OpenRA.Mods.Common.Activities
// Target is hidden or dead, and we don't have a fallback position to move towards // Target is hidden or dead, and we don't have a fallback position to move towards
if (useLastVisibleTarget && !lastVisibleTarget.IsValidFor(self)) if (useLastVisibleTarget && !lastVisibleTarget.IsValidFor(self))
{ {
attackAircraft.RequestedTarget = Target.Invalid; attackAircraft.ClearRequestedTarget();
return NextActivity; return NextActivity;
} }
@@ -141,7 +134,7 @@ namespace OpenRA.Mods.Common.Activities
// We've reached the assumed position but it is not there - give up // We've reached the assumed position but it is not there - give up
if (checkTarget.IsInRange(pos, lastVisibleMaximumRange)) if (checkTarget.IsInRange(pos, lastVisibleMaximumRange))
{ {
attackAircraft.RequestedTarget = Target.Invalid; attackAircraft.ClearRequestedTarget();
return NextActivity; return NextActivity;
} }
@@ -184,7 +177,7 @@ namespace OpenRA.Mods.Common.Activities
return; return;
if (!autoTarget.HasValidTargetPriority(self, lastVisibleOwner, lastVisibleTargetTypes)) if (!autoTarget.HasValidTargetPriority(self, lastVisibleOwner, lastVisibleTargetTypes))
attackAircraft.RequestedTarget = Target.Invalid; attackAircraft.ClearRequestedTarget();
} }
} }
} }

View File

@@ -32,15 +32,34 @@ namespace OpenRA.Mods.Common.Traits
public class AttackFollow : AttackBase, INotifyOwnerChanged, IDisableAutoTarget, INotifyStanceChanged public class AttackFollow : AttackBase, INotifyOwnerChanged, IDisableAutoTarget, INotifyStanceChanged
{ {
public new readonly AttackFollowInfo Info; public new readonly AttackFollowInfo Info;
public Target RequestedTarget; public Target RequestedTarget { get; private set; }
public bool RequestedForceAttack; public Target OpportunityTarget { get; private set; }
public int RequestedTargetLastTick;
public Target OpportunityTarget;
public bool OpportunityForceAttack;
public bool OpportunityTargetIsPersistentTarget;
Mobile mobile; Mobile mobile;
AutoTarget autoTarget; 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) public AttackFollow(Actor self, AttackFollowInfo info)
: base(self, info) : base(self, info)
@@ -78,10 +97,10 @@ namespace OpenRA.Mods.Common.Traits
if (IsTraitDisabled) if (IsTraitDisabled)
{ {
RequestedTarget = OpportunityTarget = Target.Invalid; 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 // 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 // (either queued next or already cancelled) and we need to recalculate the target ourself
@@ -95,7 +114,7 @@ namespace OpenRA.Mods.Common.Traits
if (RequestedTarget.Type != TargetType.Invalid) if (RequestedTarget.Type != TargetType.Invalid)
{ {
IsAiming = CanAimAtTarget(self, RequestedTarget, RequestedForceAttack); IsAiming = CanAimAtTarget(self, RequestedTarget, requestedForceAttack);
if (IsAiming) if (IsAiming)
DoAttack(self, RequestedTarget); DoAttack(self, RequestedTarget);
} }
@@ -104,17 +123,17 @@ namespace OpenRA.Mods.Common.Traits
IsAiming = false; IsAiming = false;
if (OpportunityTarget.Type != TargetType.Invalid) if (OpportunityTarget.Type != TargetType.Invalid)
IsAiming = CanAimAtTarget(self, OpportunityTarget, OpportunityForceAttack); IsAiming = CanAimAtTarget(self, OpportunityTarget, opportunityForceAttack);
if (!IsAiming && Info.OpportunityFire && autoTarget != null && if (!IsAiming && Info.OpportunityFire && autoTarget != null &&
!autoTarget.IsTraitDisabled && autoTarget.Stance >= UnitStance.Defend) !autoTarget.IsTraitDisabled && autoTarget.Stance >= UnitStance.Defend)
{ {
OpportunityTarget = autoTarget.ScanForTarget(self, false, false); OpportunityTarget = autoTarget.ScanForTarget(self, false, false);
OpportunityForceAttack = false; opportunityForceAttack = false;
OpportunityTargetIsPersistentTarget = false; opportunityTargetIsPersistentTarget = false;
if (OpportunityTarget.Type != TargetType.Invalid) if (OpportunityTarget.Type != TargetType.Invalid)
IsAiming = CanAimAtTarget(self, OpportunityTarget, OpportunityForceAttack); IsAiming = CanAimAtTarget(self, OpportunityTarget, opportunityForceAttack);
} }
if (IsAiming) if (IsAiming)
@@ -137,34 +156,34 @@ namespace OpenRA.Mods.Common.Traits
if (!queued) if (!queued)
{ {
RequestedTarget = target; RequestedTarget = target;
RequestedForceAttack = forceAttack; requestedForceAttack = forceAttack;
RequestedTargetLastTick = self.World.WorldTick; requestedTargetLastTick = self.World.WorldTick;
} }
} }
public override void OnStopOrder(Actor self) public override void OnStopOrder(Actor self)
{ {
RequestedTarget = OpportunityTarget = Target.Invalid; RequestedTarget = OpportunityTarget = Target.Invalid;
OpportunityTargetIsPersistentTarget = false; opportunityTargetIsPersistentTarget = false;
base.OnStopOrder(self); base.OnStopOrder(self);
} }
void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner) void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)
{ {
RequestedTarget = OpportunityTarget = Target.Invalid; RequestedTarget = OpportunityTarget = Target.Invalid;
OpportunityTargetIsPersistentTarget = false; opportunityTargetIsPersistentTarget = false;
} }
bool IDisableAutoTarget.DisableAutoTarget(Actor self) bool IDisableAutoTarget.DisableAutoTarget(Actor self)
{ {
return RequestedTarget.Type != TargetType.Invalid || 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) 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 // 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; return;
if (OpportunityTarget.Type == TargetType.Actor) if (OpportunityTarget.Type == TargetType.Actor)
@@ -241,14 +260,7 @@ namespace OpenRA.Mods.Common.Traits
if (IsCanceling) if (IsCanceling)
{ {
// Cancel the requested target, but keep firing on it while in range // Cancel the requested target, but keep firing on it while in range
if (attack.Info.PersistentTargeting) attack.ClearRequestedTarget();
{
attack.OpportunityTarget = attack.RequestedTarget;
attack.OpportunityForceAttack = attack.RequestedForceAttack;
attack.OpportunityTargetIsPersistentTarget = true;
}
attack.RequestedTarget = Target.Invalid;
return NextActivity; return NextActivity;
} }
@@ -261,9 +273,8 @@ namespace OpenRA.Mods.Common.Traits
return this; return this;
bool targetIsHiddenActor; bool targetIsHiddenActor;
attack.RequestedForceAttack = forceAttack; target = target.Recalculate(self.Owner, out targetIsHiddenActor);
attack.RequestedTarget = target = target.Recalculate(self.Owner, out targetIsHiddenActor); attack.SetRequestedTarget(self, target, forceAttack);
attack.RequestedTargetLastTick = self.World.WorldTick;
hasTicked = true; hasTicked = true;
if (!targetIsHiddenActor && target.Type == TargetType.Actor) if (!targetIsHiddenActor && target.Type == TargetType.Actor)
@@ -305,7 +316,7 @@ namespace OpenRA.Mods.Common.Traits
// Either we are in range and can see the target, or we've lost track of it and should give up // Either we are in range and can see the target, or we've lost track of it and should give up
if (wasMovingWithinRange && targetIsHiddenActor) if (wasMovingWithinRange && targetIsHiddenActor)
{ {
attack.RequestedTarget = Target.Invalid; attack.ClearRequestedTarget();
return NextActivity; return NextActivity;
} }
@@ -316,7 +327,7 @@ namespace OpenRA.Mods.Common.Traits
// Target is hidden or dead, and we don't have a fallback position to move towards // Target is hidden or dead, and we don't have a fallback position to move towards
if (useLastVisibleTarget && !lastVisibleTarget.IsValidFor(self)) if (useLastVisibleTarget && !lastVisibleTarget.IsValidFor(self))
{ {
attack.RequestedTarget = Target.Invalid; attack.ClearRequestedTarget();
return NextActivity; return NextActivity;
} }
@@ -329,7 +340,7 @@ namespace OpenRA.Mods.Common.Traits
{ {
if (useLastVisibleTarget) if (useLastVisibleTarget)
{ {
attack.RequestedTarget = Target.Invalid; attack.ClearRequestedTarget();
return NextActivity; return NextActivity;
} }
@@ -339,7 +350,7 @@ namespace OpenRA.Mods.Common.Traits
// We can't move into range, so give up // We can't move into range, so give up
if (move == null || maxRange == WDist.Zero || maxRange < minRange) if (move == null || maxRange == WDist.Zero || maxRange < minRange)
{ {
attack.RequestedTarget = Target.Invalid; attack.ClearRequestedTarget();
return NextActivity; return NextActivity;
} }
@@ -355,7 +366,7 @@ namespace OpenRA.Mods.Common.Traits
return; return;
if (!autoTarget.HasValidTargetPriority(self, lastVisibleOwner, lastVisibleTargetTypes)) if (!autoTarget.HasValidTargetPriority(self, lastVisibleOwner, lastVisibleTargetTypes))
attack.RequestedTarget = Target.Invalid; attack.ClearRequestedTarget();
} }
} }
} }