diff --git a/OpenRA.Mods.Common/Traits/Cargo.cs b/OpenRA.Mods.Common/Traits/Cargo.cs index 7347dbc921..e9c926c62b 100644 --- a/OpenRA.Mods.Common/Traits/Cargo.cs +++ b/OpenRA.Mods.Common/Traits/Cargo.cs @@ -14,6 +14,7 @@ using System.Collections.Generic; using System.Linq; using OpenRA.Mods.Common.Activities; using OpenRA.Mods.Common.Orders; +using OpenRA.Mods.Common.Widgets; using OpenRA.Primitives; using OpenRA.Traits; @@ -88,7 +89,7 @@ namespace OpenRA.Mods.Common.Traits } public class Cargo : IPips, IIssueOrder, IResolveOrder, IOrderVoice, INotifyCreated, INotifyKilled, - INotifyOwnerChanged, INotifyAddedToWorld, ITick, INotifySold, INotifyActorDisposing, IIssueDeployOrder, + INotifyOwnerChanged, ITick, INotifySold, INotifyActorDisposing, IIssueDeployOrder, ITransformActorInitModifier { public readonly CargoInfo Info; @@ -106,9 +107,13 @@ namespace OpenRA.Mods.Common.Traits int loadingToken = ConditionManager.InvalidConditionToken; Stack loadedTokens = new Stack(); bool takeOffAfterLoad; + readonly CachedTransform> currentAdjacentCells; + + public IEnumerable CurrentAdjacentCells + { + get { return currentAdjacentCells.Update(self.Location); } + } - CPos currentCell; - public IEnumerable CurrentAdjacentCells { get; private set; } public IEnumerable Passengers { get { return cargo; } } public int PassengerCount { get { return cargo.Count; } } @@ -121,6 +126,11 @@ namespace OpenRA.Mods.Common.Traits Info = info; checkTerrainType = info.UnloadTerrainTypes.Count > 0; + currentAdjacentCells = new CachedTransform>(loc => + { + return Util.AdjacentCells(self.World, Target.FromActor(self)).Where(c => loc != c); + }); + if (init.Contains()) { cargo = new List(init.Get()); @@ -210,11 +220,6 @@ namespace OpenRA.Mods.Common.Traits } } - IEnumerable GetAdjacentCells() - { - return Util.AdjacentCells(self.World, Target.FromActor(self)).Where(c => self.Location != c); - } - public bool CanUnload(BlockedByActor check = BlockedByActor.None) { if (checkTerrainType) @@ -490,13 +495,6 @@ namespace OpenRA.Mods.Common.Traits p.ChangeOwner(newOwner); } - void INotifyAddedToWorld.AddedToWorld(Actor self) - { - // Force location update to avoid issues when initial spawn is outside map - currentCell = self.Location; - CurrentAdjacentCells = GetAdjacentCells(); - } - bool initialized; void ITick.Tick(Actor self) { @@ -516,13 +514,6 @@ namespace OpenRA.Mods.Common.Traits initialized = true; } - - var cell = self.World.Map.CellContaining(self.CenterPosition); - if (currentCell != cell) - { - currentCell = cell; - CurrentAdjacentCells = GetAdjacentCells(); - } } void ITransformActorInitModifier.ModifyTransformActorInit(Actor self, TypeDictionary init)