Skip to content

Commit

Permalink
Add LandOnCondition to the trait Aircraft which triggers a landing an…
Browse files Browse the repository at this point in the history
…d prevents takeoffs while the condition is met (OpenRA#13434)
  • Loading branch information
jrb0001 committed Jul 15, 2017
1 parent 8c35231 commit 1ef098d
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 2 deletions.
7 changes: 7 additions & 0 deletions OpenRA.Mods.Common/Activities/Air/Fly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public static void FlyToward(Actor self, Aircraft plane, int desiredFacing, WDis

public override Activity Tick(Actor self)
{
// Refuse to take off if it would land immediately again.
if (plane.ForceLanding)
{
Cancel(self);
return NextActivity;
}

if (IsCanceled || !target.IsValidFor(self))
return NextActivity;

Expand Down
7 changes: 7 additions & 0 deletions OpenRA.Mods.Common/Activities/Air/FlyAttack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ public FlyAttack(Actor self, Target target)

public override Activity Tick(Actor self)
{
// Refuse to take off if it would land immediately again.
if (aircraft.ForceLanding)
{
Cancel(self);
return NextActivity;
}

if (!target.IsValidFor(self))
return NextActivity;

Expand Down
7 changes: 7 additions & 0 deletions OpenRA.Mods.Common/Activities/Air/FlyCircle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ public FlyCircle(Actor self)

public override Activity Tick(Actor self)
{
// Refuse to take off if it would land immediately again.
if (plane.ForceLanding)
{
Cancel(self);
return NextActivity;
}

if (IsCanceled)
return NextActivity;

Expand Down
7 changes: 7 additions & 0 deletions OpenRA.Mods.Common/Activities/Air/FlyFollow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ public FlyFollow(Actor self, Target target, WDist minRange, WDist maxRange)

public override Activity Tick(Actor self)
{
// Refuse to take off if it would land immediately again.
if (plane.ForceLanding)
{
Cancel(self);
return NextActivity;
}

if (IsCanceled || !target.IsValidFor(self))
return NextActivity;

Expand Down
7 changes: 7 additions & 0 deletions OpenRA.Mods.Common/Activities/Air/FlyOffMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public FlyOffMap(Actor self)

public override Activity Tick(Actor self)
{
// Refuse to take off if it would land immediately again.
if (plane.ForceLanding)
{
Cancel(self);
return NextActivity;
}

if (IsCanceled || !self.World.Map.Contains(self.Location))
return NextActivity;

Expand Down
7 changes: 7 additions & 0 deletions OpenRA.Mods.Common/Activities/Air/FlyTimed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public FlyTimed(int ticks, Actor self)

public override Activity Tick(Actor self)
{
// Refuse to take off if it would land immediately again.
if (plane.ForceLanding)
{
Cancel(self);
return NextActivity;
}

if (IsCanceled || remainingTicks-- == 0)
return NextActivity;

Expand Down
7 changes: 7 additions & 0 deletions OpenRA.Mods.Common/Activities/Air/HeliAttack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ public HeliAttack(Actor self, Target target, bool attackOnlyVisibleTargets = tru

public override Activity Tick(Actor self)
{
// Refuse to take off if it would land immediately again.
if (helicopter.ForceLanding)
{
Cancel(self);
return NextActivity;
}

if (IsCanceled || !target.IsValidFor(self))
return NextActivity;

Expand Down
7 changes: 7 additions & 0 deletions OpenRA.Mods.Common/Activities/Air/HeliFly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ public static bool AdjustAltitude(Actor self, Aircraft helicopter, WDist targetA

public override Activity Tick(Actor self)
{
// Refuse to take off if it would land immediately again.
if (helicopter.ForceLanding)
{
Cancel(self);
return NextActivity;
}

if (IsCanceled || !target.IsValidFor(self))
return NextActivity;

Expand Down
7 changes: 7 additions & 0 deletions OpenRA.Mods.Common/Activities/Air/HeliFlyCircle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ public HeliFlyCircle(Actor self)

public override Activity Tick(Actor self)
{
// Refuse to take off if it would land immediately again.
if (helicopter.ForceLanding)
{
Cancel(self);
return NextActivity;
}

if (IsCanceled)
return NextActivity;

Expand Down
4 changes: 4 additions & 0 deletions OpenRA.Mods.Common/Activities/Air/HeliReturnToBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public Actor ChooseHelipad(Actor self, bool unreservedOnly)

public override Activity Tick(Actor self)
{
// Refuse to take off if it would land immediately again.
// Special case: Don't kill other deploy hotkey activities.
if (heli.ForceLanding) return NextActivity;

if (IsCanceled)
return NextActivity;

Expand Down
4 changes: 4 additions & 0 deletions OpenRA.Mods.Common/Activities/Air/ReturnToBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ bool ShouldLandAtBuilding(Actor self, Actor dest)

public override Activity Tick(Actor self)
{
// Refuse to take off if it would land immediately again.
// Special case: Don't kill other deploy hotkey activities.
if (plane.ForceLanding) return NextActivity;

if (IsCanceled || self.IsDead)
return NextActivity;

Expand Down
7 changes: 7 additions & 0 deletions OpenRA.Mods.Common/Activities/Air/TakeOff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ public TakeOff(Actor self)

public override Activity Tick(Actor self)
{
// Refuse to take off if it would land immediately again.
if (aircraft.ForceLanding)
{
Cancel(self);
return NextActivity;
}

aircraft.UnReserve();

var host = aircraft.GetActorBelow();
Expand Down
46 changes: 44 additions & 2 deletions OpenRA.Mods.Common/Traits/Air/Aircraft.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Orders;
using OpenRA.Primitives;
using OpenRA.Support;
using OpenRA.Traits;

namespace OpenRA.Mods.Common.Traits
Expand All @@ -39,7 +40,7 @@ public class AircraftInfo : ITraitInfo, IPositionableInfo, IFacingInfo, IMoveInf
public readonly int TurnSpeed = 255;
public readonly int Speed = 1;

[Desc("Minimum altitude where this aircraft is considered airborne")]
[Desc("Minimum altitude where this aircraft is considered airborne.")]
public readonly int MinAirborneAltitude = 1;
public readonly HashSet<string> LandableTerrainTypes = new HashSet<string>();

Expand Down Expand Up @@ -100,6 +101,9 @@ IEnumerable<object> IActorPreviewInitInfo.ActorPreviewInits(ActorInfo ai, ActorP
yield return new FacingInit(PreviewFacing);
}

[Desc("Condition when this aircraft should land as soon as possible and refuse to take off.")]
public readonly BooleanExpression LandOnCondition;

public IReadOnlyDictionary<CPos, SubCell> OccupiedCells(ActorInfo info, CPos location, SubCell subCell = SubCell.Any) { return new ReadOnlyDictionary<CPos, SubCell>(); }
bool IOccupySpaceInfo.SharesCell { get { return false; } }

Expand All @@ -124,7 +128,7 @@ public bool CanEnterCell(World world, Actor self, CPos cell, Actor ignoreActor =
}

public class Aircraft : ITick, ISync, IFacing, IPositionable, IMove, IIssueOrder, IResolveOrder, IOrderVoice, IDeathActorInitModifier,
INotifyCreated, INotifyAddedToWorld, INotifyRemovedFromWorld, INotifyActorDisposing, IActorPreviewInitModifier, IIssueDeployOrder
INotifyCreated, INotifyAddedToWorld, INotifyRemovedFromWorld, INotifyActorDisposing, IActorPreviewInitModifier, IIssueDeployOrder, IObservesVariables
{
static readonly Pair<CPos, SubCell>[] NoCells = { };

Expand All @@ -142,6 +146,7 @@ public class Aircraft : ITick, ISync, IFacing, IPositionable, IMove, IIssueOrder
public int TurnSpeed { get { return Info.TurnSpeed; } }
public Actor ReservedActor { get; private set; }
public bool MayYieldReservation { get; private set; }
public bool ForceLanding { get; private set; }

bool airborne;
bool cruising;
Expand All @@ -152,6 +157,7 @@ public class Aircraft : ITick, ISync, IFacing, IPositionable, IMove, IIssueOrder
bool isMoving;
bool isMovingVertically;
WPos cachedPosition;
bool? landNow;

public Aircraft(ActorInitializer init, AircraftInfo info)
{
Expand All @@ -171,6 +177,17 @@ public Aircraft(ActorInitializer init, AircraftInfo info)
IsPlane = !info.CanHover;
}

public virtual IEnumerable<VariableObserver> GetVariableObservers()
{
if (Info.LandOnCondition != null)
yield return new VariableObserver(ForceLandConditionChanged, Info.LandOnCondition.Variables);
}

void ForceLandConditionChanged(Actor self, IReadOnlyDictionary<string, int> variables)
{
landNow = Info.LandOnCondition.Evaluate(variables);
}

public void Created(Actor self)
{
conditionManager = self.TraitOrDefault<ConditionManager>();
Expand Down Expand Up @@ -208,6 +225,31 @@ public virtual void Tick(Actor self)
self.QueueActivity(new TakeOff(self));
}

// Add land activity if LandOnCondidion resolves to true and the actor can land at the current location.
if (landNow.HasValue && landNow.Value && airborne && CanLand(self.Location)
&& (self.CurrentActivity == null || self.CurrentActivity is HeliLand || self.CurrentActivity is Turn))
{
self.CancelActivity();

if (Info.TurnToLand)
self.QueueActivity(new Turn(self, Info.InitialFacing));

self.QueueActivity(new HeliLand(self, true));

ForceLanding = true;
}

// Add takeoff activity if LandOnCondidion resolves to false and the actor should not land when idle.
if (landNow.HasValue && !landNow.Value && !cruising && !Info.LandWhenIdle
&& (self.CurrentActivity == null || self.CurrentActivity is TakeOff))
{
self.CancelActivity();

self.QueueActivity(new TakeOff(self));

ForceLanding = false;
}

var oldCachedPosition = cachedPosition;
cachedPosition = self.CenterPosition;
isMoving = (oldCachedPosition - cachedPosition).HorizontalLengthSquared != 0;
Expand Down

0 comments on commit 1ef098d

Please sign in to comment.