From d70eea5adc073e5093341f9919d12454c084d740 Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Sun, 6 Sep 2015 22:41:04 +0100 Subject: [PATCH] Refactor occupied cells visibility checks. Provide some common extension methods to reduce code duplication, and also use loops instead of LINQ to reduce allocations and provide a minor speedup. --- OpenRA.Mods.Common/OpenRA.Mods.Common.csproj | 1 + OpenRA.Mods.Common/ShroudExts.cs | 39 +++++++++++++++++++ .../Traits/Modifiers/FrozenUnderFog.cs | 3 +- .../Traits/Modifiers/HiddenUnderFog.cs | 3 +- .../Traits/Modifiers/HiddenUnderShroud.cs | 3 +- 5 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 OpenRA.Mods.Common/ShroudExts.cs diff --git a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj index acddc69b1d..7e29d02214 100644 --- a/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj +++ b/OpenRA.Mods.Common/OpenRA.Mods.Common.csproj @@ -194,6 +194,7 @@ + diff --git a/OpenRA.Mods.Common/ShroudExts.cs b/OpenRA.Mods.Common/ShroudExts.cs new file mode 100644 index 0000000000..8f418a8caa --- /dev/null +++ b/OpenRA.Mods.Common/ShroudExts.cs @@ -0,0 +1,39 @@ +#region Copyright & License Information +/* + * Copyright 2007-2015 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation. For more information, + * see COPYING. + */ +#endregion + +using System.Collections.Generic; +using OpenRA.Primitives; +using OpenRA.Traits; + +namespace OpenRA.Mods.Common +{ + using OccupiedCells = IEnumerable>; + + public static class ShroudExts + { + public static bool AnyExplored(this Shroud shroud, OccupiedCells cells) + { + foreach (var cell in cells) + if (shroud.IsExplored(cell.First)) + return true; + + return false; + } + + public static bool AnyVisible(this Shroud shroud, OccupiedCells cells) + { + foreach (var cell in cells) + if (shroud.IsVisible(cell.First)) + return true; + + return false; + } + } +} diff --git a/OpenRA.Mods.Common/Traits/Modifiers/FrozenUnderFog.cs b/OpenRA.Mods.Common/Traits/Modifiers/FrozenUnderFog.cs index b1b5c0706b..c211285bf9 100644 --- a/OpenRA.Mods.Common/Traits/Modifiers/FrozenUnderFog.cs +++ b/OpenRA.Mods.Common/Traits/Modifiers/FrozenUnderFog.cs @@ -70,8 +70,7 @@ namespace OpenRA.Mods.Common.Traits { // If fog is disabled visibility is determined by shroud if (!byPlayer.Shroud.FogEnabled) - return self.OccupiesSpace.OccupiedCells() - .Any(o => byPlayer.Shroud.IsExplored(o.First)); + return byPlayer.Shroud.AnyExplored(self.OccupiesSpace.OccupiedCells()); return initialized && stateByPlayer[byPlayer].IsVisible; } diff --git a/OpenRA.Mods.Common/Traits/Modifiers/HiddenUnderFog.cs b/OpenRA.Mods.Common/Traits/Modifiers/HiddenUnderFog.cs index 1b190d9765..f04d6111e7 100644 --- a/OpenRA.Mods.Common/Traits/Modifiers/HiddenUnderFog.cs +++ b/OpenRA.Mods.Common/Traits/Modifiers/HiddenUnderFog.cs @@ -33,8 +33,7 @@ namespace OpenRA.Mods.Common.Traits return base.IsVisibleInner(self, byPlayer); if (Info.Type == VisibilityType.Footprint) - return self.OccupiesSpace.OccupiedCells() - .Any(o => byPlayer.Shroud.IsVisible(o.First)); + return byPlayer.Shroud.AnyVisible(self.OccupiesSpace.OccupiedCells()); return byPlayer.Shroud.IsVisible(self.CenterPosition); } diff --git a/OpenRA.Mods.Common/Traits/Modifiers/HiddenUnderShroud.cs b/OpenRA.Mods.Common/Traits/Modifiers/HiddenUnderShroud.cs index 9d45976aef..75e4a6960e 100644 --- a/OpenRA.Mods.Common/Traits/Modifiers/HiddenUnderShroud.cs +++ b/OpenRA.Mods.Common/Traits/Modifiers/HiddenUnderShroud.cs @@ -42,8 +42,7 @@ namespace OpenRA.Mods.Common.Traits protected virtual bool IsVisibleInner(Actor self, Player byPlayer) { if (Info.Type == VisibilityType.Footprint) - return self.OccupiesSpace.OccupiedCells() - .Any(o => byPlayer.Shroud.IsExplored(o.First)); + return byPlayer.Shroud.AnyExplored(self.OccupiesSpace.OccupiedCells()); return byPlayer.Shroud.IsExplored(self.CenterPosition); }