From 71a1060ecbc74bb954942cbf77bfd398884d4faf Mon Sep 17 00:00:00 2001 From: tovl Date: Sun, 7 Jul 2019 21:40:02 +0200 Subject: [PATCH] Prevent carryalls picking up non-existent units. --- OpenRA.Mods.Common/Activities/PickupUnit.cs | 32 ++++++++++++++++----- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/OpenRA.Mods.Common/Activities/PickupUnit.cs b/OpenRA.Mods.Common/Activities/PickupUnit.cs index 4937e77356..b4873b1d4d 100644 --- a/OpenRA.Mods.Common/Activities/PickupUnit.cs +++ b/OpenRA.Mods.Common/Activities/PickupUnit.cs @@ -101,7 +101,7 @@ namespace OpenRA.Mods.Common.Activities QueueChild(new Wait(delay, false)); // Remove our carryable from world - QueueChild(new CallFunc(() => Attach(self))); + QueueChild(new AttachUnit(self, cargo)); QueueChild(new TakeOff(self)); return false; } @@ -110,14 +110,32 @@ namespace OpenRA.Mods.Common.Activities return true; } - void Attach(Actor self) + class AttachUnit : Activity { - self.World.AddFrameEndTask(w => + readonly Actor cargo; + readonly Carryable carryable; + readonly Carryall carryall; + + public AttachUnit(Actor self, Actor cargo) { - cargo.World.Remove(cargo); - carryable.Attached(cargo); - carryall.AttachCarryable(self, cargo); - }); + this.cargo = cargo; + carryable = cargo.Trait(); + carryall = self.Trait(); + } + + protected override void OnFirstRun(Actor self) + { + // The cargo might have become invalid while we were moving towards it. + if (cargo == null || cargo.IsDead || carryable.IsTraitDisabled || !cargo.AppearsFriendlyTo(self)) + return; + + self.World.AddFrameEndTask(w => + { + cargo.World.Remove(cargo); + carryable.Attached(cargo); + carryall.AttachCarryable(self, cargo); + }); + } } } }