Skip to content

Commit

Permalink
Base AttackAircraft on AttackFollow and get rid of SequenceActivities.
Browse files Browse the repository at this point in the history
  • Loading branch information
tovl authored and reaperrr committed May 4, 2019
1 parent da6bf1a commit f16ff9e
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 66 deletions.
4 changes: 2 additions & 2 deletions OpenRA.Mods.Cnc/Traits/Attack/AttackTDGunboatTurreted.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public override Activity Tick(Actor self)
{
// Check that AttackTDGunboatTurreted hasn't cancelled the target by modifying attack.Target
// Having both this and AttackTDGunboatTurreted modify that field is a horrible hack.
if (hasTicked && attack.requestedTarget.Type == TargetType.Invalid)
if (hasTicked && attack.RequestedTarget.Type == TargetType.Invalid)
return NextActivity;

attack.requestedTarget = target;
attack.RequestedTarget = target;
hasTicked = true;
}

Expand Down
74 changes: 46 additions & 28 deletions OpenRA.Mods.Common/Activities/Air/FlyAttack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ public class FlyAttack : Activity
readonly Aircraft aircraft;
readonly AttackAircraft attackAircraft;
readonly Rearmable rearmable;
readonly int ticksUntilTurn;

Target target;
Target lastVisibleTarget;
WDist lastVisibleMaximumRange;
bool useLastVisibleTarget;

int ticksUntilTurn;
bool hasTicked;

public FlyAttack(Actor self, Target target)
{
Expand All @@ -49,15 +50,38 @@ public FlyAttack(Actor self, Target target)

public override Activity Tick(Actor self)
{
if (ChildActivity != null)
{
ChildActivity = ActivityUtils.RunActivity(self, ChildActivity);
if (ChildActivity != null)
return this;
}

// Refuse to take off if it would land immediately again.
if (aircraft.ForceLanding)
Cancel(self);

if (IsCanceling)
{
// Cancel the requested target, but keep firing on it while in range
attackAircraft.OpportunityTarget = attackAircraft.RequestedTarget;
attackAircraft.RequestedTarget = Target.Invalid;
return NextActivity;
}

// Check that AttackFollow hasn't cancelled the target by modifying attack.Target
// Having both this and AttackFollow modify that field is a horrible hack.
if (hasTicked && attackAircraft.RequestedTarget.Type == TargetType.Invalid)
return NextActivity;

if (attackAircraft.IsTraitPaused)
return this;

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

if (!targetIsHiddenActor && target.Type == TargetType.Actor)
{
lastVisibleTarget = Target.FromTargetPositions(target);
Expand All @@ -73,11 +97,17 @@ 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;
return NextActivity;
}

// If all valid weapons have depleted their ammo and Rearmable trait exists, return to RearmActor to reload and then resume the activity
if (rearmable != null && !useLastVisibleTarget && attackAircraft.Armaments.All(x => x.IsTraitPaused || !x.Weapon.IsValidAgainst(target, self.World, self)))
return ActivityUtils.SequenceActivities(self, new ReturnToBase(self, aircraft.Info.AbortOnResupply), this);
{
QueueChild(self, new ReturnToBase(self, aircraft.Info.AbortOnResupply), true);
return this;
}

var pos = self.CenterPosition;
var checkTarget = useLastVisibleTarget ? lastVisibleTarget : target;
Expand All @@ -87,37 +117,25 @@ 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;
return NextActivity;
}

// Fly towards the last known position
return ActivityUtils.SequenceActivities(self,
new Fly(self, target, WDist.Zero, lastVisibleMaximumRange, checkTarget.CenterPosition, Color.Red),
this);
QueueChild(self, new Fly(self, target, WDist.Zero, lastVisibleMaximumRange, checkTarget.CenterPosition, Color.Red), true);
return this;
}

// If we reach here the target is guaranteed to be both visible and alive, so use target instead of checkTarget.
// The target may not be in range, but try attacking anyway...
attackAircraft.DoAttack(self, target);
if (self.World.Map.DistanceAboveTerrain(self.CenterPosition).Length < aircraft.Info.MinAirborneAltitude)
QueueChild(self, new TakeOff(self), true);

if (ChildActivity == null)
{
// TODO: This should fire each weapon at its maximum range
if (attackAircraft != null && target.IsInRange(self.CenterPosition, attackAircraft.GetMinimumRange()))
ChildActivity = ActivityUtils.SequenceActivities(self,
new FlyTimed(ticksUntilTurn, self),
new Fly(self, target, checkTarget.CenterPosition, Color.Red),
new FlyTimed(ticksUntilTurn, self));
else
ChildActivity = ActivityUtils.SequenceActivities(self,
new Fly(self, target, checkTarget.CenterPosition, Color.Red),
new FlyTimed(ticksUntilTurn, self));

// HACK: This needs to be done in this round-about way because TakeOff doesn't behave as expected when it doesn't have a NextActivity.
if (self.World.Map.DistanceAboveTerrain(self.CenterPosition).Length < aircraft.Info.MinAirborneAltitude)
ChildActivity = ActivityUtils.SequenceActivities(self, new TakeOff(self), ChildActivity);
}
// TODO: This should fire each weapon at its maximum range
if (attackAircraft != null && target.IsInRange(self.CenterPosition, attackAircraft.GetMinimumRange()))
QueueChild(self, new FlyTimed(ticksUntilTurn, self), true);

ActivityUtils.RunActivity(self, ChildActivity);
QueueChild(self, new Fly(self, target, checkTarget.CenterPosition, Color.Red), true);
QueueChild(self, new FlyTimed(ticksUntilTurn, self));

return this;
}
Expand Down
27 changes: 24 additions & 3 deletions OpenRA.Mods.Common/Activities/Air/HeliAttack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class HeliAttack : Activity
Target lastVisibleTarget;
WDist lastVisibleMaximumRange;
bool useLastVisibleTarget;
bool hasTicked;

public HeliAttack(Actor self, Target target)
{
Expand Down Expand Up @@ -59,10 +60,26 @@ public override Activity Tick(Actor self)
Cancel(self);

if (IsCanceling)
{
// Cancel the requested target, but keep firing on it while in range
attackAircraft.OpportunityTarget = attackAircraft.RequestedTarget;
attackAircraft.RequestedTarget = Target.Invalid;
return NextActivity;
}

// Check that AttackFollow hasn't cancelled the target by modifying attack.Target
// Having both this and AttackFollow modify that field is a horrible hack.
if (hasTicked && attackAircraft.RequestedTarget.Type == TargetType.Invalid)
return NextActivity;

if (attackAircraft.IsTraitPaused)
return this;

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

if (!targetIsHiddenActor && target.Type == TargetType.Actor)
{
lastVisibleTarget = Target.FromTargetPositions(target);
Expand All @@ -78,7 +95,10 @@ 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;
return NextActivity;
}

// If all valid weapons have depleted their ammo and Rearmable trait exists, return to RearmActor to reload and then resume the activity
if (rearmable != null && !useLastVisibleTarget && attackAircraft.Armaments.All(x => x.IsTraitPaused || !x.Weapon.IsValidAgainst(target, self.World, self)))
Expand All @@ -102,7 +122,10 @@ 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;
return NextActivity;
}

// Fly towards the last known position
aircraft.SetPosition(self, aircraft.CenterPosition + aircraft.FlyStep(desiredFacing));
Expand All @@ -125,8 +148,6 @@ public override Activity Tick(Actor self)
aircraft.SetPosition(self, aircraft.CenterPosition + aircraft.FlyStep(-facing));
}

attackAircraft.DoAttack(self, target);

return this;
}
}
Expand Down
33 changes: 28 additions & 5 deletions OpenRA.Mods.Common/Traits/Air/AttackAircraft.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,26 @@

namespace OpenRA.Mods.Common.Traits
{
public class AttackAircraftInfo : AttackFrontalInfo, Requires<AircraftInfo>
public class AttackAircraftInfo : AttackFollowInfo, Requires<AircraftInfo>
{
[Desc("Delay, in game ticks, before non-hovering aircraft turns to attack.")]
public readonly int AttackTurnDelay = 50;

[Desc("Tolerance for attack angle. Range [0, 128], 128 covers 360 degrees.")]
public readonly int FacingTolerance = 0;

public override void RulesetLoaded(Ruleset rules, ActorInfo ai)
{
base.RulesetLoaded(rules, ai);

if (FacingTolerance < 0 || FacingTolerance > 128)
throw new YamlException("Facing tolerance must be in range of [0, 128], 128 covers 360 degrees.");
}

public override object Create(ActorInitializer init) { return new AttackAircraft(init.Self, this); }
}

public class AttackAircraft : AttackFrontal
public class AttackAircraft : AttackFollow
{
public readonly AttackAircraftInfo AttackAircraftInfo;
readonly AircraftInfo aircraftInfo;
Expand All @@ -46,9 +57,21 @@ public override Activity GetAttackActivity(Actor self, Target newTarget, bool al
protected override bool CanAttack(Actor self, Target target)
{
// Don't fire while landed or when outside the map.
return base.CanAttack(self, target)
&& self.World.Map.DistanceAboveTerrain(self.CenterPosition).Length >= aircraftInfo.MinAirborneAltitude
&& self.World.Map.Contains(self.Location);
if (self.World.Map.DistanceAboveTerrain(self.CenterPosition).Length < aircraftInfo.MinAirborneAltitude
|| !self.World.Map.Contains(self.Location))
return false;

if (!base.CanAttack(self, target))
return false;

var pos = self.CenterPosition;
var targetedPosition = GetTargetPosition(pos, target);
var delta = targetedPosition - pos;

if (delta.HorizontalLengthSquared == 0)
return true;

return Util.FacingWithinTolerance(facing.Facing, delta.Yaw.Facing, AttackAircraftInfo.FacingTolerance);
}
}
}
Loading

0 comments on commit f16ff9e

Please sign in to comment.