From 0677c309f3a3698c0b155cf4bab83da4d02d3d72 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sat, 13 Jun 2015 18:27:00 +0100 Subject: [PATCH] Move Shroud.IsTargetable to Player. --- OpenRA.Game/Player.cs | 18 +++++++++++++++++ OpenRA.Game/Traits/World/Shroud.cs | 20 ------------------- OpenRA.Mods.Common/Activities/Attack.cs | 2 +- OpenRA.Mods.Common/Traits/AutoTarget.cs | 2 +- .../Traits/Crates/HideMapCrateAction.cs | 2 +- .../Infiltration/InfiltrateForExploration.cs | 2 +- .../Traits/SupportPowers/ChronoshiftPower.cs | 8 ++++---- 7 files changed, 26 insertions(+), 28 deletions(-) diff --git a/OpenRA.Game/Player.cs b/OpenRA.Game/Player.cs index f54bc40cbf..6dfd91b9fe 100644 --- a/OpenRA.Game/Player.cs +++ b/OpenRA.Game/Player.cs @@ -49,6 +49,8 @@ namespace OpenRA public Shroud Shroud; public World World { get; private set; } + readonly IFogVisibilityModifier[] fogVisibilities; + CountryInfo ChooseCountry(World world, string name, bool requireSelectable = true) { var selectableCountries = world.Map.Rules.Actors["world"].Traits @@ -113,6 +115,9 @@ namespace OpenRA PlayerActor = world.CreateActor("Player", new TypeDictionary { new OwnerInit(this) }); Shroud = PlayerActor.Trait(); + fogVisibilities = PlayerActor.TraitsImplementing() + .ToArray(); + // Enable the bot logic on the host IsBot = botType != null; if (IsBot && Game.IsHost) @@ -157,6 +162,19 @@ namespace OpenRA return a.Trait().IsVisible(a, this); } + public bool CanTargetActor(Actor a) + { + if (HasFogVisibility) + return true; + + if (a.TraitsImplementing().Any(t => !t.IsVisible(a, this))) + return false; + + return a.Trait().IsVisible(a, this); + } + + public bool HasFogVisibility { get { return fogVisibilities.Any(f => f.HasFogVisibility(this)); } } + #region Scripting interface Lazy luaInterface; diff --git a/OpenRA.Game/Traits/World/Shroud.cs b/OpenRA.Game/Traits/World/Shroud.cs index ac670d5273..ad8a58a5b3 100644 --- a/OpenRA.Game/Traits/World/Shroud.cs +++ b/OpenRA.Game/Traits/World/Shroud.cs @@ -33,8 +33,6 @@ namespace OpenRA.Traits readonly CellLayer generatedShroudCount; readonly CellLayer explored; - readonly Lazy fogVisibilities; - // Cache of visibility that was added, so no matter what crazy trait code does, it // can't make us invalid. readonly Dictionary visibility = new Dictionary(); @@ -62,8 +60,6 @@ namespace OpenRA.Traits self.World.ActorAdded += a => { CPos[] shrouded = null; AddShroudGeneration(a, ref shrouded); }; self.World.ActorRemoved += RemoveShroudGeneration; - fogVisibilities = Exts.Lazy(() => self.TraitsImplementing().ToArray()); - shroudEdgeTest = map.Contains; isExploredTest = IsExploredCore; isVisibleTest = IsVisibleCore; @@ -389,22 +385,6 @@ namespace OpenRA.Traits } } - public bool IsTargetable(Actor a) - { - if (HasFogVisibility()) - return true; - - if (a.TraitsImplementing().Any(t => !t.IsVisible(a, self.Owner))) - return false; - - return GetVisOrigins(a).Any(IsVisible); - } - - public bool HasFogVisibility() - { - return fogVisibilities.Value.Any(f => f.HasFogVisibility(self.Owner)); - } - public bool Contains(MPos uv) { // Check that uv is inside the map area. There is nothing special diff --git a/OpenRA.Mods.Common/Activities/Attack.cs b/OpenRA.Mods.Common/Activities/Attack.cs index 41cdff5ffc..cae126458a 100644 --- a/OpenRA.Mods.Common/Activities/Attack.cs +++ b/OpenRA.Mods.Common/Activities/Attack.cs @@ -61,7 +61,7 @@ namespace OpenRA.Mods.Common.Activities // HACK: This would otherwise break targeting frozen actors // The problem is that Shroud.IsTargetable returns false (as it should) for // frozen actors, but we do want to explicitly target the underlying actor here. - if (!attack.Info.IgnoresVisibility && type == TargetType.Actor && !Target.Actor.HasTrait() && !self.Owner.Shroud.IsTargetable(Target.Actor)) + if (!attack.Info.IgnoresVisibility && type == TargetType.Actor && !Target.Actor.HasTrait() && !self.Owner.CanTargetActor(Target.Actor)) return NextActivity; // Try to move within range diff --git a/OpenRA.Mods.Common/Traits/AutoTarget.cs b/OpenRA.Mods.Common/Traits/AutoTarget.cs index ba962231fa..436be5f80b 100644 --- a/OpenRA.Mods.Common/Traits/AutoTarget.cs +++ b/OpenRA.Mods.Common/Traits/AutoTarget.cs @@ -156,7 +156,7 @@ namespace OpenRA.Mods.Common.Traits a.AppearsHostileTo(self) && !a.HasTrait() && attack.HasAnyValidWeapons(Target.FromActor(a)) && - self.Owner.Shroud.IsTargetable(a)) + self.Owner.CanTargetActor(a)) .ClosestTo(self); } } diff --git a/OpenRA.Mods.Common/Traits/Crates/HideMapCrateAction.cs b/OpenRA.Mods.Common/Traits/Crates/HideMapCrateAction.cs index c102b3321a..97c5485f0d 100644 --- a/OpenRA.Mods.Common/Traits/Crates/HideMapCrateAction.cs +++ b/OpenRA.Mods.Common/Traits/Crates/HideMapCrateAction.cs @@ -24,7 +24,7 @@ namespace OpenRA.Mods.Common.Traits public override int GetSelectionShares(Actor collector) { // don't ever hide the map for people who have GPS. - if (collector.Owner.Shroud.HasFogVisibility()) + if (collector.Owner.HasFogVisibility) return 0; return base.GetSelectionShares(collector); diff --git a/OpenRA.Mods.RA/Traits/Infiltration/InfiltrateForExploration.cs b/OpenRA.Mods.RA/Traits/Infiltration/InfiltrateForExploration.cs index dd8ea887a8..cf810e0aa0 100644 --- a/OpenRA.Mods.RA/Traits/Infiltration/InfiltrateForExploration.cs +++ b/OpenRA.Mods.RA/Traits/Infiltration/InfiltrateForExploration.cs @@ -20,7 +20,7 @@ namespace OpenRA.Mods.RA.Traits public void Infiltrated(Actor self, Actor infiltrator) { infiltrator.Owner.Shroud.Explore(self.Owner.Shroud); - if (!self.Owner.Shroud.HasFogVisibility()) + if (!self.Owner.HasFogVisibility) self.Owner.Shroud.ResetExploration(); } } diff --git a/OpenRA.Mods.RA/Traits/SupportPowers/ChronoshiftPower.cs b/OpenRA.Mods.RA/Traits/SupportPowers/ChronoshiftPower.cs index 642c4983d9..3e33e74ff9 100644 --- a/OpenRA.Mods.RA/Traits/SupportPowers/ChronoshiftPower.cs +++ b/OpenRA.Mods.RA/Traits/SupportPowers/ChronoshiftPower.cs @@ -137,7 +137,7 @@ namespace OpenRA.Mods.RA.Traits var targetUnits = power.UnitsInRange(xy).Where(a => !world.FogObscures(a)); foreach (var unit in targetUnits) - if (manager.Self.Owner.Shroud.IsTargetable(unit)) + if (manager.Self.Owner.CanTargetActor(unit)) yield return new SelectionBoxRenderable(unit, Color.Red); } @@ -217,7 +217,7 @@ namespace OpenRA.Mods.RA.Traits public IEnumerable RenderAfterWorld(WorldRenderer wr, World world) { foreach (var unit in power.UnitsInRange(sourceLocation)) - if (manager.Self.Owner.Shroud.IsTargetable(unit)) + if (manager.Self.Owner.CanTargetActor(unit)) yield return new SelectionBoxRenderable(unit, Color.Red); } @@ -238,7 +238,7 @@ namespace OpenRA.Mods.RA.Traits foreach (var unit in power.UnitsInRange(sourceLocation)) { var offset = world.Map.CenterOfCell(xy) - world.Map.CenterOfCell(sourceLocation); - if (manager.Self.Owner.Shroud.IsTargetable(unit)) + if (manager.Self.Owner.CanTargetActor(unit)) foreach (var r in unit.Render(wr)) yield return r.OffsetBy(offset); } @@ -246,7 +246,7 @@ namespace OpenRA.Mods.RA.Traits // Unit tiles foreach (var unit in power.UnitsInRange(sourceLocation)) { - if (manager.Self.Owner.Shroud.IsTargetable(unit)) + if (manager.Self.Owner.CanTargetActor(unit)) { var targetCell = unit.Location + (xy - sourceLocation); var canEnter = manager.Self.Owner.Shroud.IsExplored(targetCell) &&