From 985020b4ade14b744bfb32a30b1518b8a408eeda Mon Sep 17 00:00:00 2001 From: tovl Date: Sun, 30 Jun 2019 20:53:36 +0200 Subject: [PATCH] Simplify special exits of several acitivities. --- .../Activities/Air/FlyAttack.cs | 4 +-- OpenRA.Mods.Common/Activities/Air/Land.cs | 3 +-- .../Activities/Air/ReturnToBase.cs | 25 +++++-------------- .../Activities/DeliverResources.cs | 14 +++-------- .../Activities/DeployForGrantedCondition.cs | 7 ++---- OpenRA.Mods.Common/Activities/UnloadCargo.cs | 4 +-- .../Properties/AircraftProperties.cs | 2 +- OpenRA.Mods.Common/Traits/Air/Aircraft.cs | 4 +-- OpenRA.Mods.Common/Traits/Air/ReturnOnIdle.cs | 2 +- 9 files changed, 20 insertions(+), 45 deletions(-) diff --git a/OpenRA.Mods.Common/Activities/Air/FlyAttack.cs b/OpenRA.Mods.Common/Activities/Air/FlyAttack.cs index 73e3c0542f..1c3419d52b 100644 --- a/OpenRA.Mods.Common/Activities/Air/FlyAttack.cs +++ b/OpenRA.Mods.Common/Activities/Air/FlyAttack.cs @@ -114,8 +114,8 @@ 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(new ReturnToBase(self, aircraft.Info.AbortOnResupply)); - return false; + QueueChild(new ReturnToBase(self)); + return aircraft.Info.AbortOnResupply; } var pos = self.CenterPosition; diff --git a/OpenRA.Mods.Common/Activities/Air/Land.cs b/OpenRA.Mods.Common/Activities/Air/Land.cs index 0d7d934526..49b6ebc0e3 100644 --- a/OpenRA.Mods.Common/Activities/Air/Land.cs +++ b/OpenRA.Mods.Common/Activities/Air/Land.cs @@ -117,9 +117,8 @@ namespace OpenRA.Mods.Common.Activities // Cannot land so fly towards the last target location instead. if (!newLocation.HasValue) { - Cancel(self, true); QueueChild(aircraft.MoveTo(landingCell, 0)); - return false; + return true; } if (newLocation.Value != landingCell) diff --git a/OpenRA.Mods.Common/Activities/Air/ReturnToBase.cs b/OpenRA.Mods.Common/Activities/Air/ReturnToBase.cs index b21fcc8ecf..bbfe41e15e 100644 --- a/OpenRA.Mods.Common/Activities/Air/ReturnToBase.cs +++ b/OpenRA.Mods.Common/Activities/Air/ReturnToBase.cs @@ -25,16 +25,13 @@ namespace OpenRA.Mods.Common.Activities readonly RepairableInfo repairableInfo; readonly Rearmable rearmable; readonly bool alwaysLand; - readonly bool abortOnResupply; - bool resupplied; Actor dest; int facing = -1; - public ReturnToBase(Actor self, bool abortOnResupply, Actor dest = null, bool alwaysLand = true) + public ReturnToBase(Actor self, Actor dest = null, bool alwaysLand = true) { this.dest = dest; this.alwaysLand = alwaysLand; - this.abortOnResupply = abortOnResupply; aircraft = self.Trait(); repairableInfo = self.Info.TraitInfoOrDefault(); rearmable = self.TraitOrDefault(); @@ -73,17 +70,7 @@ namespace OpenRA.Mods.Common.Activities if (aircraft.ForceLanding) return true; - // 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) - self.CancelActivity(); - - if (resupplied || IsCanceling || self.IsDead) + if (IsCanceling || self.IsDead) return true; if (dest == null || dest.IsDead || !Reservable.IsAvailableFor(dest, self)) @@ -136,11 +123,11 @@ namespace OpenRA.Mods.Common.Activities QueueChild(new Resupply(self, dest, WDist.Zero)); if (aircraft.Info.TakeOffOnResupply && !alwaysLand) QueueChild(new TakeOff(self)); - } - else - QueueChild(new Fly(self, Target.FromActor(dest))); - resupplied = true; + return true; + } + + QueueChild(new Fly(self, Target.FromActor(dest))); return false; } } diff --git a/OpenRA.Mods.Common/Activities/DeliverResources.cs b/OpenRA.Mods.Common/Activities/DeliverResources.cs index 1f338a16af..4d263b6cf9 100644 --- a/OpenRA.Mods.Common/Activities/DeliverResources.cs +++ b/OpenRA.Mods.Common/Activities/DeliverResources.cs @@ -22,8 +22,6 @@ namespace OpenRA.Mods.Common.Activities readonly Harvester harv; readonly Actor targetActor; - bool isDocking; - public DeliverResources(Actor self, Actor targetActor = null) { movement = self.Trait(); @@ -39,7 +37,7 @@ namespace OpenRA.Mods.Common.Activities public override bool Tick(Actor self) { - if (IsCanceling || isDocking) + if (IsCanceling) return true; // Find the nearest best refinery if not explicitly ordered to a specific refinery: @@ -66,14 +64,8 @@ namespace OpenRA.Mods.Common.Activities return false; } - if (!isDocking) - { - QueueChild(new Wait(10)); - isDocking = true; - iao.OnDock(self, this); - return false; - } - + QueueChild(new Wait(10)); + iao.OnDock(self, this); return true; } } diff --git a/OpenRA.Mods.Common/Activities/DeployForGrantedCondition.cs b/OpenRA.Mods.Common/Activities/DeployForGrantedCondition.cs index b844bef27f..826f90ce6d 100644 --- a/OpenRA.Mods.Common/Activities/DeployForGrantedCondition.cs +++ b/OpenRA.Mods.Common/Activities/DeployForGrantedCondition.cs @@ -20,7 +20,6 @@ namespace OpenRA.Mods.Common.Activities readonly GrantConditionOnDeploy deploy; readonly bool canTurn; readonly bool moving; - bool initiated; public DeployForGrantedCondition(Actor self, GrantConditionOnDeploy deploy, bool moving = false) { @@ -38,13 +37,11 @@ namespace OpenRA.Mods.Common.Activities public override bool Tick(Actor self) { - if (IsCanceling || initiated || (deploy.DeployState != DeployState.Deployed && moving)) + if (IsCanceling || (deploy.DeployState != DeployState.Deployed && moving)) return true; QueueChild(new DeployInner(self, deploy)); - - initiated = true; - return false; + return true; } } diff --git a/OpenRA.Mods.Common/Activities/UnloadCargo.cs b/OpenRA.Mods.Common/Activities/UnloadCargo.cs index 6761c77b8a..dccdbca23d 100644 --- a/OpenRA.Mods.Common/Activities/UnloadCargo.cs +++ b/OpenRA.Mods.Common/Activities/UnloadCargo.cs @@ -129,13 +129,13 @@ namespace OpenRA.Mods.Common.Activities if (!unloadAll || !cargo.CanUnload()) { - Cancel(self, true); - if (cargo.Info.AfterUnloadDelay > 0) QueueChild(new Wait(cargo.Info.AfterUnloadDelay, false)); if (takeOffAfterUnload) QueueChild(new TakeOff(self)); + + return true; } return false; diff --git a/OpenRA.Mods.Common/Scripting/Properties/AircraftProperties.cs b/OpenRA.Mods.Common/Scripting/Properties/AircraftProperties.cs index 5c773ab507..1c95c7489b 100644 --- a/OpenRA.Mods.Common/Scripting/Properties/AircraftProperties.cs +++ b/OpenRA.Mods.Common/Scripting/Properties/AircraftProperties.cs @@ -38,7 +38,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) { - Self.QueueActivity(new ReturnToBase(Self, false, destination)); + Self.QueueActivity(new ReturnToBase(Self, destination)); } [ScriptActorPropertyActivity] diff --git a/OpenRA.Mods.Common/Traits/Air/Aircraft.cs b/OpenRA.Mods.Common/Traits/Air/Aircraft.cs index 33f70540a7..08e78748e8 100644 --- a/OpenRA.Mods.Common/Traits/Air/Aircraft.cs +++ b/OpenRA.Mods.Common/Traits/Air/Aircraft.cs @@ -1012,7 +1012,7 @@ namespace OpenRA.Mods.Common.Traits // them to land on a resupplier. For aircraft without it, it makes more sense to land than to idle above a // free resupplier. var forceLand = orderString == "ForceEnter" || !Info.TakeOffOnResupply; - self.QueueActivity(order.Queued, new ReturnToBase(self, Info.AbortOnResupply, targetActor, forceLand)); + self.QueueActivity(order.Queued, new ReturnToBase(self, targetActor, forceLand)); } else if (orderString == "Stop") { @@ -1036,7 +1036,7 @@ namespace OpenRA.Mods.Common.Traits // Aircraft with TakeOffOnResupply would immediately take off again, so there's no point in forcing them to land // on a resupplier. For aircraft without it, it makes more sense to land than to idle above a free resupplier. - self.QueueActivity(order.Queued, new ReturnToBase(self, Info.AbortOnResupply, null, !Info.TakeOffOnResupply)); + self.QueueActivity(order.Queued, new ReturnToBase(self, null, !Info.TakeOffOnResupply)); } else if (orderString == "Scatter") Nudge(self); diff --git a/OpenRA.Mods.Common/Traits/Air/ReturnOnIdle.cs b/OpenRA.Mods.Common/Traits/Air/ReturnOnIdle.cs index ddae658832..2800f4db70 100644 --- a/OpenRA.Mods.Common/Traits/Air/ReturnOnIdle.cs +++ b/OpenRA.Mods.Common/Traits/Air/ReturnOnIdle.cs @@ -38,7 +38,7 @@ namespace OpenRA.Mods.Common.Traits var resupplier = ReturnToBase.ChooseResupplier(self, true); if (resupplier != null) - self.QueueActivity(new ReturnToBase(self, aircraftInfo.AbortOnResupply, resupplier)); + self.QueueActivity(new ReturnToBase(self, resupplier)); else { // nowhere to land, pick something friendly and circle over it.