diff --git a/OpenRA.Mods.Common/Activities/Air/HeliAttack.cs b/OpenRA.Mods.Common/Activities/Air/HeliAttack.cs index 9902a47062..8736f34bc9 100644 --- a/OpenRA.Mods.Common/Activities/Air/HeliAttack.cs +++ b/OpenRA.Mods.Common/Activities/Air/HeliAttack.cs @@ -83,7 +83,7 @@ namespace OpenRA.Mods.Common.Activities // 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))) { - QueueChild(self, new HeliReturnToBase(self, aircraft.Info.AbortOnResupply), true); + QueueChild(self, new ReturnToBase(self, aircraft.Info.AbortOnResupply), true); return this; } diff --git a/OpenRA.Mods.Common/Activities/Air/HeliReturnToBase.cs b/OpenRA.Mods.Common/Activities/Air/HeliReturnToBase.cs deleted file mode 100644 index 2a21b505f9..0000000000 --- a/OpenRA.Mods.Common/Activities/Air/HeliReturnToBase.cs +++ /dev/null @@ -1,127 +0,0 @@ -#region Copyright & License Information -/* - * Copyright 2007-2019 The OpenRA Developers (see AUTHORS) - * This file is part of OpenRA, which is free software. It is made - * available to you under the terms of the GNU General Public License - * as published by the Free Software Foundation, either version 3 of - * the License, or (at your option) any later version. For more - * information, see COPYING. - */ -#endregion - -using System.Collections.Generic; -using System.Linq; -using OpenRA.Activities; -using OpenRA.Mods.Common.Traits; -using OpenRA.Primitives; -using OpenRA.Traits; - -namespace OpenRA.Mods.Common.Activities -{ - public class HeliReturnToBase : Activity - { - readonly Aircraft aircraft; - readonly RepairableInfo repairableInfo; - readonly Rearmable rearmable; - readonly bool alwaysLand; - readonly bool abortOnResupply; - Actor dest; - - public HeliReturnToBase(Actor self, bool abortOnResupply, Actor dest = null, bool alwaysLand = true) - { - aircraft = self.Trait(); - repairableInfo = self.Info.TraitInfoOrDefault(); - rearmable = self.TraitOrDefault(); - this.alwaysLand = alwaysLand; - this.abortOnResupply = abortOnResupply; - this.dest = 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 (aircraft.ForceLanding) - return NextActivity; - - if (IsCanceling) - return NextActivity; - - if (dest == null || dest.IsDead || !Reservable.IsAvailableFor(dest, self)) - dest = ReturnToBase.ChooseResupplier(self, true); - - var initialFacing = aircraft.Info.InitialFacing; - - if (dest == null || dest.IsDead) - { - var nearestResupplier = ReturnToBase.ChooseResupplier(self, false); - - // If a heli was told to return and there's no (available) RearmBuilding, going to the probable next queued activity (HeliAttack) - // would be pointless (due to lack of ammo), and possibly even lead to an infinite loop due to HeliAttack.cs:L79. - if (nearestResupplier == null && aircraft.Info.LandWhenIdle) - { - if (aircraft.Info.TurnToLand) - return ActivityUtils.SequenceActivities(self, new Turn(self, initialFacing), new HeliLand(self, true)); - - return new HeliLand(self, true); - } - else if (nearestResupplier == null && !aircraft.Info.LandWhenIdle) - return null; - else - { - var distanceFromResupplier = (nearestResupplier.CenterPosition - self.CenterPosition).HorizontalLength; - var distanceLength = aircraft.Info.WaitDistanceFromResupplyBase.Length; - - // If no pad is available, move near one and wait - 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); - } - - return this; - } - } - - var landingProcedures = new List(); - var exit = dest.FirstExitOrDefault(null); - var offset = exit != null ? exit.Info.SpawnOffset : WVec.Zero; - - landingProcedures.Add(new HeliFly(self, Target.FromPos(dest.CenterPosition + offset))); - - if (ShouldLandAtBuilding(self, dest)) - { - aircraft.MakeReservation(dest); - - if (aircraft.Info.TurnToDock) - landingProcedures.Add(new Turn(self, initialFacing)); - - landingProcedures.Add(new HeliLand(self, false)); - landingProcedures.Add(new ResupplyAircraft(self)); - if (!abortOnResupply) - landingProcedures.Add(NextActivity); - } - else - landingProcedures.Add(NextActivity); - - return ActivityUtils.SequenceActivities(self, landingProcedures.ToArray()); - } - - bool ShouldLandAtBuilding(Actor self, Actor dest) - { - if (alwaysLand) - return true; - - if (repairableInfo != null && repairableInfo.RepairActors.Contains(dest.Info.Name) && self.GetDamageState() != DamageState.Undamaged) - return true; - - return rearmable != null && rearmable.Info.RearmActors.Contains(dest.Info.Name) - && rearmable.RearmableAmmoPools.Any(p => !p.FullAmmo()); - } - } -} diff --git a/OpenRA.Mods.Common/Activities/Air/ReturnToBase.cs b/OpenRA.Mods.Common/Activities/Air/ReturnToBase.cs index 11dee24742..1fb212cd4c 100644 --- a/OpenRA.Mods.Common/Activities/Air/ReturnToBase.cs +++ b/OpenRA.Mods.Common/Activities/Air/ReturnToBase.cs @@ -53,15 +53,16 @@ namespace OpenRA.Mods.Common.Activities .ClosestTo(self); } + // Calculates non-CanHover/non-VTOL approach vector and waypoints void Calculate(Actor self) { - if (dest == null || dest.IsDead || !Reservable.IsAvailableFor(dest, self)) - dest = ChooseResupplier(self, true); - if (dest == null) return; - var landPos = dest.CenterPosition; + var exit = dest.FirstExitOrDefault(null); + var offset = exit != null ? exit.Info.SpawnOffset : WVec.Zero; + + var landPos = dest.CenterPosition + offset; var altitude = aircraft.Info.CruiseAltitude.Length; // Distance required for descent. @@ -127,6 +128,9 @@ namespace OpenRA.Mods.Common.Activities if (IsCanceling || self.IsDead) return NextActivity; + if (dest == null || dest.IsDead || !Reservable.IsAvailableFor(dest, self)) + dest = ReturnToBase.ChooseResupplier(self, true); + if (!isCalculated) Calculate(self); @@ -135,10 +139,39 @@ namespace OpenRA.Mods.Common.Activities var nearestResupplier = ChooseResupplier(self, false); if (nearestResupplier != null) - return ActivityUtils.SequenceActivities(self, - new Fly(self, Target.FromActor(nearestResupplier), WDist.Zero, aircraft.Info.WaitDistanceFromResupplyBase, targetLineColor: Color.Green), - new FlyCircle(self, aircraft.Info.NumberOfTicksToVerifyAvailableAirport), - this); + { + if (aircraft.Info.VTOL) + { + var distanceFromResupplier = (nearestResupplier.CenterPosition - self.CenterPosition).HorizontalLength; + var distanceLength = aircraft.Info.WaitDistanceFromResupplyBase.Length; + + // If no pad is available, move near one and wait + 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); + } + + return this; + } + else + return ActivityUtils.SequenceActivities(self, + new Fly(self, Target.FromActor(nearestResupplier), WDist.Zero, aircraft.Info.WaitDistanceFromResupplyBase, targetLineColor: Color.Green), + new FlyCircle(self, aircraft.Info.NumberOfTicksToVerifyAvailableAirport), + this); + } + else if (nearestResupplier == null && aircraft.Info.VTOL && aircraft.Info.LandWhenIdle) + { + if (aircraft.Info.TurnToLand) + return ActivityUtils.SequenceActivities(self, new Turn(self, aircraft.Info.InitialFacing), new HeliLand(self, true)); + + return new HeliLand(self, true); + } else { // Prevent an infinite loop in case we'd return to the activity that called ReturnToBase in the first place. Go idle instead. @@ -147,21 +180,38 @@ namespace OpenRA.Mods.Common.Activities } } + var exit = dest.FirstExitOrDefault(null); + var offset = exit != null ? exit.Info.SpawnOffset : WVec.Zero; + List landingProcedures = new List(); - var turnRadius = Fly.CalculateTurnRadius(aircraft.Info.Speed, aircraft.Info.TurnSpeed); + if (aircraft.Info.CanHover) + landingProcedures.Add(new HeliFly(self, Target.FromPos(dest.CenterPosition + offset))); + 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))); + landingProcedures.Add(new Fly(self, Target.FromPos(w1), WDist.Zero, new WDist(turnRadius * 3))); + landingProcedures.Add(new Fly(self, Target.FromPos(w2))); - // Fix a problem when the airplane is send to resupply near the airport - landingProcedures.Add(new Fly(self, Target.FromPos(w3), WDist.Zero, new WDist(turnRadius / 2))); + // 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))); + } if (ShouldLandAtBuilding(self, dest)) { aircraft.MakeReservation(dest); - landingProcedures.Add(new Land(self, Target.FromActor(dest))); + if (aircraft.Info.VTOL) + { + if (aircraft.Info.TurnToDock) + landingProcedures.Add(new Turn(self, aircraft.Info.InitialFacing)); + + landingProcedures.Add(new HeliLand(self, false)); + } + else + landingProcedures.Add(new Land(self, Target.FromPos(dest.CenterPosition + offset))); + landingProcedures.Add(new ResupplyAircraft(self)); } diff --git a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj index c040e71a61..9fa51d17b4 100644 --- a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj +++ b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj @@ -75,7 +75,6 @@ - diff --git a/OpenRA.Mods.Common/Scripting/Properties/AircraftProperties.cs b/OpenRA.Mods.Common/Scripting/Properties/AircraftProperties.cs index b79b79d1cc..f91961f03c 100644 --- a/OpenRA.Mods.Common/Scripting/Properties/AircraftProperties.cs +++ b/OpenRA.Mods.Common/Scripting/Properties/AircraftProperties.cs @@ -41,10 +41,7 @@ namespace OpenRA.Mods.Common.Scripting [Desc("Return to the base, which is either the destination given, or an auto-selected one otherwise.")] public void ReturnToBase(Actor destination = null) { - if (!aircraftInfo.CanHover) - Self.QueueActivity(new ReturnToBase(Self, false, destination)); - else - Self.QueueActivity(new HeliReturnToBase(Self, false, destination)); + Self.QueueActivity(new ReturnToBase(Self, false, destination)); } [ScriptActorPropertyActivity] diff --git a/OpenRA.Mods.Common/Traits/Air/Aircraft.cs b/OpenRA.Mods.Common/Traits/Air/Aircraft.cs index ead3d2b774..fda0590fe4 100644 --- a/OpenRA.Mods.Common/Traits/Air/Aircraft.cs +++ b/OpenRA.Mods.Common/Traits/Air/Aircraft.cs @@ -844,10 +844,7 @@ namespace OpenRA.Mods.Common.Traits if (Reservable.IsAvailableFor(targetActor, self)) self.SetTargetLine(Target.FromActor(targetActor), Color.Green); - if (!Info.CanHover && !Info.VTOL) - self.QueueActivity(order.Queued, new ReturnToBase(self, Info.AbortOnResupply, targetActor)); - else - self.QueueActivity(order.Queued, new HeliReturnToBase(self, Info.AbortOnResupply, targetActor)); + self.QueueActivity(order.Queued, new ReturnToBase(self, Info.AbortOnResupply, targetActor)); } else if (order.OrderString == "Stop") { @@ -865,10 +862,7 @@ namespace OpenRA.Mods.Common.Traits if (!order.Queued) UnReserve(); - if (!Info.CanHover) - self.QueueActivity(order.Queued, new ReturnToBase(self, Info.AbortOnResupply, null, false)); - else - self.QueueActivity(order.Queued, new HeliReturnToBase(self, Info.AbortOnResupply, null, false)); + self.QueueActivity(order.Queued, new ReturnToBase(self, Info.AbortOnResupply, null, false)); } }