From 2937a31463a6e8504ab8c758ffd61346c3d1e310 Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Mon, 20 Apr 2015 22:12:30 +0100 Subject: [PATCH] Make IsDisabled a method on Actor. This allows us to cache the disabled traits, which simplifies life for some callers since we relieve them of having to cache it, as well as improving perf for all IsDisabled calls. --- OpenRA.Game/Actor.cs | 10 ++++++++++ OpenRA.Game/Traits/CreatesShroud.cs | 2 +- OpenRA.Game/Traits/TraitsInterfaces.cs | 8 -------- OpenRA.Mods.Common/Traits/Render/RenderBuilding.cs | 7 ++----- .../Traits/Render/WithActiveAnimation.cs | 4 +--- OpenRA.Mods.Common/Traits/Render/WithIdleOverlay.cs | 3 +-- .../Traits/Render/WithRepairAnimation.cs | 4 +--- OpenRA.Mods.Common/Traits/Render/WithRepairOverlay.cs | 3 +-- .../Traits/SupportPowers/SupportPowerManager.cs | 2 +- 9 files changed, 18 insertions(+), 25 deletions(-) diff --git a/OpenRA.Game/Actor.cs b/OpenRA.Game/Actor.cs index 1f45274035..4c7b5b8eb9 100644 --- a/OpenRA.Game/Actor.cs +++ b/OpenRA.Game/Actor.cs @@ -85,6 +85,7 @@ namespace OpenRA readonly IRenderModifier[] renderModifiers; readonly IRender[] renders; + readonly IDisable[] disables; internal Actor(World world, string name, TypeDictionary initDict) { @@ -128,6 +129,7 @@ namespace OpenRA renderModifiers = TraitsImplementing().ToArray(); renders = TraitsImplementing().ToArray(); + disables = TraitsImplementing().ToArray(); } public void Tick() @@ -277,6 +279,14 @@ namespace OpenRA health.Value.InflictDamage(this, attacker, health.Value.MaxHP, null, true); } + public bool IsDisabled() + { + foreach (var disable in disables) + if (disable.Disabled) + return true; + return false; + } + #region Scripting interface Lazy luaInterface; diff --git a/OpenRA.Game/Traits/CreatesShroud.cs b/OpenRA.Game/Traits/CreatesShroud.cs index 6adcb682aa..2b6a28b5fc 100644 --- a/OpenRA.Game/Traits/CreatesShroud.cs +++ b/OpenRA.Game/Traits/CreatesShroud.cs @@ -37,7 +37,7 @@ namespace OpenRA.Traits if (lobbyShroudFogDisabled) return; - var disabled = self.TraitsImplementing().Any(d => d.Disabled); + var disabled = self.IsDisabled(); if (cachedLocation != self.Location || cachedDisabled != disabled) { cachedLocation = self.Location; diff --git a/OpenRA.Game/Traits/TraitsInterfaces.cs b/OpenRA.Game/Traits/TraitsInterfaces.cs index 015632cef0..253ddc38dc 100644 --- a/OpenRA.Game/Traits/TraitsInterfaces.cs +++ b/OpenRA.Game/Traits/TraitsInterfaces.cs @@ -329,14 +329,6 @@ namespace OpenRA.Traits void OnObjectiveFailed(Player player, int objectiveID); } - public static class DisableExts - { - public static bool IsDisabled(this Actor a) - { - return a.TraitsImplementing().Any(d => d.Disabled); - } - } - public interface ILegacyEditorRenderInfo { string EditorPalette { get; } diff --git a/OpenRA.Mods.Common/Traits/Render/RenderBuilding.cs b/OpenRA.Mods.Common/Traits/Render/RenderBuilding.cs index fce74db005..f5a36c0daf 100644 --- a/OpenRA.Mods.Common/Traits/Render/RenderBuilding.cs +++ b/OpenRA.Mods.Common/Traits/Render/RenderBuilding.cs @@ -54,11 +54,8 @@ namespace OpenRA.Mods.Common.Traits DefaultAnimation.PlayRepeating(NormalizeSequence(self, info.Sequence)); if (info.PauseOnLowPower) - { - var disabled = self.TraitsImplementing(); - DefaultAnimation.Paused = () => disabled.Any(d => d.Disabled) - && DefaultAnimation.CurrentSequence.Name == NormalizeSequence(self, info.Sequence); - } + DefaultAnimation.Paused = () => + self.IsDisabled() && DefaultAnimation.CurrentSequence.Name == NormalizeSequence(self, info.Sequence); } public void PlayCustomAnimThen(Actor self, string name, Action a) diff --git a/OpenRA.Mods.Common/Traits/Render/WithActiveAnimation.cs b/OpenRA.Mods.Common/Traits/Render/WithActiveAnimation.cs index a372ee24d9..6a4d93cd75 100644 --- a/OpenRA.Mods.Common/Traits/Render/WithActiveAnimation.cs +++ b/OpenRA.Mods.Common/Traits/Render/WithActiveAnimation.cs @@ -30,13 +30,11 @@ namespace OpenRA.Mods.Common.Traits public class WithActiveAnimation : ITick, INotifyBuildComplete, INotifySold { - readonly IEnumerable disabled; readonly WithActiveAnimationInfo info; readonly RenderBuilding renderBuilding; public WithActiveAnimation(Actor self, WithActiveAnimationInfo info) { - disabled = self.TraitsImplementing(); renderBuilding = self.Trait(); this.info = info; } @@ -49,7 +47,7 @@ namespace OpenRA.Mods.Common.Traits if (--ticks <= 0) { - if (!(info.PauseOnLowPower && disabled.Any(d => d.Disabled))) + if (!(info.PauseOnLowPower && self.IsDisabled())) renderBuilding.PlayCustomAnim(self, info.Sequence); ticks = info.Interval; } diff --git a/OpenRA.Mods.Common/Traits/Render/WithIdleOverlay.cs b/OpenRA.Mods.Common/Traits/Render/WithIdleOverlay.cs index 41d427f999..75b381cdd8 100644 --- a/OpenRA.Mods.Common/Traits/Render/WithIdleOverlay.cs +++ b/OpenRA.Mods.Common/Traits/Render/WithIdleOverlay.cs @@ -61,7 +61,6 @@ namespace OpenRA.Mods.Common.Traits { var rs = self.Trait(); var body = self.Trait(); - var disabled = self.TraitsImplementing(); buildComplete = !self.HasTrait(); // always render instantly for units overlay = new Animation(self.World, rs.GetImage(self)); @@ -70,7 +69,7 @@ namespace OpenRA.Mods.Common.Traits new AnimationWithOffset(overlay, () => body.LocalToWorld(info.Offset.Rotate(body.QuantizeOrientation(self, self.Orientation))), () => IsTraitDisabled || !buildComplete, - () => info.PauseOnLowPower && disabled.Any(d => d.Disabled), + () => info.PauseOnLowPower && self.IsDisabled(), p => WithTurret.ZOffsetFromCenter(self, p, 1)), info.Palette, info.IsPlayerPalette); } diff --git a/OpenRA.Mods.Common/Traits/Render/WithRepairAnimation.cs b/OpenRA.Mods.Common/Traits/Render/WithRepairAnimation.cs index 7acfa409fa..d38612c4f6 100644 --- a/OpenRA.Mods.Common/Traits/Render/WithRepairAnimation.cs +++ b/OpenRA.Mods.Common/Traits/Render/WithRepairAnimation.cs @@ -27,19 +27,17 @@ namespace OpenRA.Mods.Common.Traits public class WithRepairAnimation : INotifyRepair { - IEnumerable disabled; WithRepairAnimationInfo info; public WithRepairAnimation(Actor self, WithRepairAnimationInfo info) { - disabled = self.TraitsImplementing(); this.info = info; } public void Repairing(Actor self, Actor host) { var building = host.TraitOrDefault(); - if (building != null && !(info.PauseOnLowPower && disabled.Any(d => d.Disabled))) + if (building != null && !(info.PauseOnLowPower && self.IsDisabled())) building.PlayCustomAnim(host, info.Sequence); } } diff --git a/OpenRA.Mods.Common/Traits/Render/WithRepairOverlay.cs b/OpenRA.Mods.Common/Traits/Render/WithRepairOverlay.cs index 205ed29758..92746f31f1 100644 --- a/OpenRA.Mods.Common/Traits/Render/WithRepairOverlay.cs +++ b/OpenRA.Mods.Common/Traits/Render/WithRepairOverlay.cs @@ -44,7 +44,6 @@ namespace OpenRA.Mods.Common.Traits { var rs = self.Trait(); var body = self.Trait(); - var disabled = self.TraitsImplementing(); buildComplete = !self.HasTrait(); // always render instantly for units overlay = new Animation(self.World, rs.GetImage(self)); @@ -53,7 +52,7 @@ namespace OpenRA.Mods.Common.Traits new AnimationWithOffset(overlay, () => body.LocalToWorld(info.Offset.Rotate(body.QuantizeOrientation(self, self.Orientation))), () => !buildComplete, - () => info.PauseOnLowPower && disabled.Any(d => d.Disabled), + () => info.PauseOnLowPower && self.IsDisabled(), p => WithTurret.ZOffsetFromCenter(self, p, 1)), info.Palette, info.IsPlayerPalette); } diff --git a/OpenRA.Mods.Common/Traits/SupportPowers/SupportPowerManager.cs b/OpenRA.Mods.Common/Traits/SupportPowers/SupportPowerManager.cs index 6f49b1c926..7c9dafe0c4 100644 --- a/OpenRA.Mods.Common/Traits/SupportPowers/SupportPowerManager.cs +++ b/OpenRA.Mods.Common/Traits/SupportPowers/SupportPowerManager.cs @@ -179,7 +179,7 @@ namespace OpenRA.Mods.Common.Traits static bool InstanceDisabled(SupportPower sp) { - return sp.Self.TraitsImplementing().Any(d => d.Disabled); + return sp.Self.IsDisabled(); } bool notifiedCharging;