diff --git a/OpenRA.Mods.Common/Traits/Air/Aircraft.cs b/OpenRA.Mods.Common/Traits/Air/Aircraft.cs index 0e58b8a1a5..b82dcdb55f 100644 --- a/OpenRA.Mods.Common/Traits/Air/Aircraft.cs +++ b/OpenRA.Mods.Common/Traits/Air/Aircraft.cs @@ -97,6 +97,7 @@ namespace OpenRA.Mods.Common.Traits UpgradeManager um; IDisposable reservation; + IEnumerable speedModifiers; [Sync] public int Facing { get; set; } [Sync] public WPos CenterPosition { get; private set; } @@ -124,7 +125,11 @@ namespace OpenRA.Mods.Common.Traits IsPlane = !info.CanHover; } - public void Created(Actor self) { um = self.TraitOrDefault(); } + public void Created(Actor self) + { + um = self.TraitOrDefault(); + speedModifiers = self.TraitsImplementing().ToArray().Select(sm => sm.GetSpeedModifier()); + } public void AddedToWorld(Actor self) { @@ -183,17 +188,19 @@ namespace OpenRA.Mods.Common.Traits if (altitude != Info.CruiseAltitude.Length) return WVec.Zero; - var repulsionForce = self.World.FindActorsInCircle(self.CenterPosition, Info.IdealSeparation) - .Where(a => - { - if (a.IsDead) - return false; + // PERF: Avoid LINQ. + var repulsionForce = WVec.Zero; + foreach (var actor in self.World.FindActorsInCircle(self.CenterPosition, Info.IdealSeparation)) + { + if (actor.IsDead) + continue; - var ai = a.Info.TraitInfoOrDefault(); - return ai != null && ai.Repulsable && ai.CruiseAltitude == Info.CruiseAltitude; - }) - .Select(GetRepulsionForce) - .Aggregate(WVec.Zero, (a, b) => a + b); + var ai = actor.Info.TraitInfoOrDefault(); + if (ai == null || !ai.Repulsable || ai.CruiseAltitude != Info.CruiseAltitude) + continue; + + repulsionForce += GetRepulsionForce(actor); + } if (Info.CanHover) return repulsionForce; @@ -279,12 +286,7 @@ namespace OpenRA.Mods.Common.Traits public int MovementSpeed { - get - { - var modifiers = self.TraitsImplementing() - .Select(m => m.GetSpeedModifier()); - return Util.ApplyPercentageModifiers(Info.Speed, modifiers); - } + get { return Util.ApplyPercentageModifiers(Info.Speed, speedModifiers); } } public IEnumerable> OccupiedCells() { return NoCells; }