Skip to content

Commit

Permalink
Fix TakeOffOnCreation
Browse files Browse the repository at this point in the history
  • Loading branch information
PunkPun authored and Mailaender committed Sep 23, 2023
1 parent c009f58 commit 60a4461
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 12 deletions.
18 changes: 14 additions & 4 deletions OpenRA.Mods.Common/Traits/Air/Aircraft.cs
Expand Up @@ -286,6 +286,7 @@ public WAngle GetTurnSpeed(bool isIdleTurn)
public bool RequireForceMove;

readonly int creationActivityDelay;
readonly CPos[] creationRallyPoint;

bool notify = true;

Expand Down Expand Up @@ -320,6 +321,7 @@ public Aircraft(ActorInitializer init, AircraftInfo info)

Facing = init.GetValue<FacingInit, WAngle>(Info.InitialFacing);
creationActivityDelay = init.GetValue<CreationActivityDelayInit, int>(0);
creationRallyPoint = init.GetOrDefault<RallyPointInit>()?.Value;
}

public WDist LandAltitude
Expand Down Expand Up @@ -1219,19 +1221,21 @@ void IActorPreviewInitModifier.ModifyActorPreviewInit(Actor self, TypeDictionary

Activity ICreationActivity.GetCreationActivity()
{
return new AssociateWithAirfieldActivity(self, creationActivityDelay);
return new AssociateWithAirfieldActivity(self, creationActivityDelay, creationRallyPoint);
}

sealed class AssociateWithAirfieldActivity : Activity
{
readonly Aircraft aircraft;
readonly int delay;
readonly CPos[] rallyPoint;

public AssociateWithAirfieldActivity(Actor self, int delay = 0)
public AssociateWithAirfieldActivity(Actor self, int delay, CPos[] rallyPoint)
{
aircraft = self.Trait<Aircraft>();
IsInterruptible = false;
this.delay = delay;
this.rallyPoint = rallyPoint;
}

protected override void OnFirstRun(Actor self)
Expand All @@ -1253,8 +1257,14 @@ public override bool Tick(Actor self)
return true;
}

if (self.World.Map.DistanceAboveTerrain(aircraft.CenterPosition).Length <= aircraft.LandAltitude.Length)
QueueChild(new TakeOff(self));
if (rallyPoint != null && aircraft.Info.TakeOffOnCreation)
{
foreach (var cell in rallyPoint)
self.QueueActivity(new AttackMoveActivity(self, () => aircraft.MoveTo(cell, 1, evaluateNearestMovableCell: true, targetLineColor: Color.OrangeRed)));
}
else
if (self.World.Map.DistanceAboveTerrain(aircraft.CenterPosition).Length <= aircraft.LandAltitude.Length)
QueueChild(new TakeOff(self));

aircraft.UnReserve();
return true;
Expand Down
33 changes: 32 additions & 1 deletion OpenRA.Mods.Common/Traits/Mobile.cs
Expand Up @@ -177,6 +177,7 @@ public class Mobile : PausableConditionalTrait<MobileInfo>, IIssueOrder, IResolv
readonly bool returnToCellOnCreation;
readonly bool returnToCellOnCreationRecalculateSubCell = true;
readonly int creationActivityDelay;
readonly CPos[] creationRallypoint;

#region IMove CurrentMovementTypes
MovementType movementTypes;
Expand Down Expand Up @@ -298,6 +299,7 @@ public Mobile(ActorInitializer init, MobileInfo info)
}

creationActivityDelay = init.GetValue<CreationActivityDelayInit, int>(0);
creationRallypoint = init.GetOrDefault<RallyPointInit>()?.Value;
}

protected override void Created(Actor self)
Expand Down Expand Up @@ -967,9 +969,38 @@ string IOrderVoice.VoicePhraseForOrder(Actor self, Order order)
}
}

public class LeaveProductionActivity : Activity
{
readonly Mobile mobile;
readonly int delay;
readonly CPos[] rallyPoint;
readonly ReturnToCellActivity returnToCell;

public LeaveProductionActivity(Actor self, int delay, CPos[] rallyPoint, ReturnToCellActivity returnToCell)
{
mobile = self.Trait<Mobile>();
IsInterruptible = false;
this.delay = delay;
this.rallyPoint = rallyPoint;
this.returnToCell = returnToCell;
}

protected override void OnFirstRun(Actor self)
{
if (returnToCell != null)
self.QueueActivity(returnToCell);
else if (delay > 0)
self.QueueActivity(new Wait(delay));

if (rallyPoint != null)
foreach (var cell in rallyPoint)
self.QueueActivity(new AttackMoveActivity(self, () => mobile.MoveTo(cell, 1, evaluateNearestMovableCell: true, targetLineColor: Color.OrangeRed)));
}
}

Activity ICreationActivity.GetCreationActivity()
{
return returnToCellOnCreation ? new ReturnToCellActivity(self, creationActivityDelay, returnToCellOnCreationRecalculateSubCell) : null;
return new LeaveProductionActivity(self, creationActivityDelay, creationRallypoint, returnToCellOnCreation ? new ReturnToCellActivity(self, creationActivityDelay, returnToCellOnCreationRecalculateSubCell) : null);
}

sealed class MoveOrderTargeter : IOrderTargeter
Expand Down
16 changes: 9 additions & 7 deletions OpenRA.Mods.Common/Traits/Production.cs
Expand Up @@ -11,7 +11,6 @@

using System;
using System.Collections.Generic;
using OpenRA.Mods.Common.Activities;
using OpenRA.Primitives;
using OpenRA.Traits;

Expand Down Expand Up @@ -82,18 +81,15 @@ public virtual void DoProduction(Actor self, ActorInfo producee, ExitInfo exitin
td.Add(new CenterPositionInit(spawn));
td.Add(new FacingInit(initialFacing));
if (exitinfo != null)
{
td.Add(new CreationActivityDelayInit(exitinfo.ExitDelay));
td.Add(new RallyPointInit(exitLocations.ToArray()));
}
}

self.World.AddFrameEndTask(w =>
{
var newUnit = self.World.CreateActor(producee.Name, td);
var move = newUnit.TraitOrDefault<IMove>();
if (exitinfo != null && move != null)
foreach (var cell in exitLocations)
newUnit.QueueActivity(new AttackMoveActivity(newUnit, () => move.MoveTo(cell, 1, evaluateNearestMovableCell: true, targetLineColor: Color.OrangeRed)));
if (!self.IsDead)
foreach (var t in self.TraitsImplementing<INotifyProduction>())
t.UnitProduced(self, newUnit, exit);
Expand Down Expand Up @@ -143,4 +139,10 @@ static bool CanUseExit(Actor self, ActorInfo producee, ExitInfo s)
mobileInfo.CanEnterCell(self.World, self, self.Location + s.ExitCell, ignoreActor: self);
}
}

public class RallyPointInit : ValueActorInit<CPos[]>, ISingleInstanceInit
{
public RallyPointInit(CPos[] value)
: base(value) { }
}
}

0 comments on commit 60a4461

Please sign in to comment.