diff --git a/OpenRA.Mods.Common/Activities/Air/FlyCircle.cs b/OpenRA.Mods.Common/Activities/Air/FlyCircle.cs index 1176449a87..f55324d614 100644 --- a/OpenRA.Mods.Common/Activities/Air/FlyCircle.cs +++ b/OpenRA.Mods.Common/Activities/Air/FlyCircle.cs @@ -29,7 +29,7 @@ namespace OpenRA.Mods.Common.Activities public override Activity Tick(Actor self) { - if (NextActivity != null && remainingTicks <= 0) + if (remainingTicks == 0 || (NextActivity != null && remainingTicks < 0)) return NextActivity; // Refuse to take off if it would land immediately again. diff --git a/OpenRA.Mods.Common/Activities/Air/ReturnToBase.cs b/OpenRA.Mods.Common/Activities/Air/ReturnToBase.cs index 6924ceb671..9ad1e2de27 100644 --- a/OpenRA.Mods.Common/Activities/Air/ReturnToBase.cs +++ b/OpenRA.Mods.Common/Activities/Air/ReturnToBase.cs @@ -27,6 +27,7 @@ namespace OpenRA.Mods.Common.Activities readonly bool alwaysLand; readonly bool abortOnResupply; bool isCalculated; + bool resupplied; Actor dest; WPos w1, w2, w3; @@ -121,12 +122,29 @@ namespace OpenRA.Mods.Common.Activities 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. // Special case: Don't kill other deploy hotkey activities. if (aircraft.ForceLanding) return NextActivity; - if (IsCanceling || self.IsDead) + // If a Cancel was triggered at this point, it's unlikely that previously queued child activities finished, + // so 'resupplied' needs to be set to false, else it + abortOnResupply might cause another Cancel + // that would cancel any other activities that were queued after the first Cancel was triggered. + // TODO: This is a mess, we need to somehow make the activity cancelling a bit less tricky. + if (resupplied && IsCanceling) + resupplied = false; + + if (resupplied && abortOnResupply) + Cancel(self); + + if (resupplied || IsCanceling || self.IsDead) return NextActivity; if (dest == null || dest.IsDead || !Reservable.IsAvailableFor(dest, self)) @@ -135,13 +153,13 @@ namespace OpenRA.Mods.Common.Activities if (!isCalculated) Calculate(self); - if (dest == null || dest.IsDead) + if (dest == null) { var nearestResupplier = ChooseResupplier(self, false); if (nearestResupplier != null) { - if (aircraft.Info.VTOL) + if (aircraft.Info.CanHover) { var distanceFromResupplier = (nearestResupplier.CenterPosition - self.CenterPosition).HorizontalLength; var distanceLength = aircraft.Info.WaitDistanceFromResupplyBase.Length; @@ -150,28 +168,32 @@ namespace OpenRA.Mods.Common.Activities if (distanceFromResupplier > distanceLength) { var randomPosition = WVec.FromPDF(self.World.SharedRandom, 2) * distanceLength / 1024; - var target = Target.FromPos(nearestResupplier.CenterPosition + randomPosition); - return ActivityUtils.SequenceActivities(self, - new HeliFly(self, target, WDist.Zero, aircraft.Info.WaitDistanceFromResupplyBase, targetLineColor: Color.Green), - this); + QueueChild(self, new HeliFly(self, target, WDist.Zero, aircraft.Info.WaitDistanceFromResupplyBase, targetLineColor: Color.Green), true); } return this; } else - return ActivityUtils.SequenceActivities(self, + { + QueueChild(self, new Fly(self, Target.FromActor(nearestResupplier), WDist.Zero, aircraft.Info.WaitDistanceFromResupplyBase, targetLineColor: Color.Green), - new FlyCircle(self, aircraft.Info.NumberOfTicksToVerifyAvailableAirport), - this); + true); + + QueueChild(self, new FlyCircle(self, aircraft.Info.NumberOfTicksToVerifyAvailableAirport), true); + return this; + } } else if (nearestResupplier == null && aircraft.Info.VTOL && aircraft.Info.LandWhenIdle) { + // Using Queue instead of QueueChild here is intentional, as we want VTOLs with LandWhenIdle to land and stay there in this situation + Cancel(self); if (aircraft.Info.TurnToLand) - return ActivityUtils.SequenceActivities(self, new Turn(self, aircraft.Info.InitialFacing), new HeliLand(self, true)); + Queue(self, new Turn(self, aircraft.Info.InitialFacing)); - return new HeliLand(self, true); + Queue(self, new HeliLand(self, true)); + return NextActivity; } else { @@ -184,19 +206,19 @@ namespace OpenRA.Mods.Common.Activities var exit = dest.FirstExitOrDefault(null); var offset = exit != null ? exit.Info.SpawnOffset : WVec.Zero; - List landingProcedures = new List(); - if (aircraft.Info.CanHover) - landingProcedures.Add(new HeliFly(self, Target.FromPos(dest.CenterPosition + offset))); + QueueChild(self, new HeliFly(self, Target.FromPos(dest.CenterPosition + offset)), true); + else if (aircraft.Info.VTOL) + QueueChild(self, new Fly(self, Target.FromPos(dest.CenterPosition + offset)), true); else { var turnRadius = Fly.CalculateTurnRadius(aircraft.Info.Speed, aircraft.Info.TurnSpeed); - landingProcedures.Add(new Fly(self, Target.FromPos(w1), WDist.Zero, new WDist(turnRadius * 3))); - landingProcedures.Add(new Fly(self, Target.FromPos(w2))); + QueueChild(self, new Fly(self, Target.FromPos(w1), WDist.Zero, new WDist(turnRadius * 3)), true); + QueueChild(self, new Fly(self, Target.FromPos(w2)), true); // Fix a problem when the airplane is sent to resupply near the airport - landingProcedures.Add(new Fly(self, Target.FromPos(w3), WDist.Zero, new WDist(turnRadius / 2))); + QueueChild(self, new Fly(self, Target.FromPos(w3), WDist.Zero, new WDist(turnRadius / 2)), true); } if (ShouldLandAtBuilding(self, dest)) @@ -206,20 +228,18 @@ namespace OpenRA.Mods.Common.Activities if (aircraft.Info.VTOL) { if (aircraft.Info.TurnToDock) - landingProcedures.Add(new Turn(self, aircraft.Info.InitialFacing)); + QueueChild(self, new Turn(self, aircraft.Info.InitialFacing), true); - landingProcedures.Add(new HeliLand(self, false)); + QueueChild(self, new HeliLand(self, false), true); } else - landingProcedures.Add(new Land(self, Target.FromPos(dest.CenterPosition + offset))); + QueueChild(self, new Land(self, Target.FromPos(dest.CenterPosition + offset)), true); - landingProcedures.Add(new ResupplyAircraft(self)); + QueueChild(self, new ResupplyAircraft(self), true); + resupplied = true; } - if (!abortOnResupply) - landingProcedures.Add(NextActivity); - - return ActivityUtils.SequenceActivities(self, landingProcedures.ToArray()); + return this; } } }