From d1960258dbc3d1e8eb07129547f3bafc12ea9842 Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Sun, 6 Sep 2015 21:35:20 +0100 Subject: [PATCH] Speed up Player.CanViewActor. Create Actor.CanBeViewedByPlayer and simply call this instead. The actor can cache all trait lookups on construction to avoid them being repeated for every visibility check. --- OpenRA.Game/Actor.cs | 13 +++++++++++++ OpenRA.Game/Player.cs | 10 ++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/OpenRA.Game/Actor.cs b/OpenRA.Game/Actor.cs index e8d704cf59..e9e3c9d304 100644 --- a/OpenRA.Game/Actor.cs +++ b/OpenRA.Game/Actor.cs @@ -71,6 +71,8 @@ namespace OpenRA readonly IRenderModifier[] renderModifiers; readonly IRender[] renders; readonly IDisable[] disables; + readonly IVisibilityModifier[] visibilityModifiers; + readonly IDefaultVisibility defaultVisibility; internal Actor(World world, string name, TypeDictionary initDict) { @@ -130,6 +132,8 @@ namespace OpenRA renderModifiers = TraitsImplementing().ToArray(); renders = TraitsImplementing().ToArray(); disables = TraitsImplementing().ToArray(); + visibilityModifiers = TraitsImplementing().ToArray(); + defaultVisibility = Trait(); } public void Tick() @@ -295,6 +299,15 @@ namespace OpenRA return false; } + public bool CanBeViewedByPlayer(Player player) + { + foreach (var visibilityModifier in visibilityModifiers) + if (!visibilityModifier.IsVisible(this, player)) + return false; + + return defaultVisibility.IsVisible(this, player); + } + #region Scripting interface Lazy luaInterface; diff --git a/OpenRA.Game/Player.cs b/OpenRA.Game/Player.cs index 4be30514ce..e0c35589e8 100644 --- a/OpenRA.Game/Player.cs +++ b/OpenRA.Game/Player.cs @@ -154,10 +154,7 @@ namespace OpenRA public bool CanViewActor(Actor a) { - if (a.TraitsImplementing().Any(t => !t.IsVisible(a, this))) - return false; - - return a.Trait().IsVisible(a, this); + return a.CanBeViewedByPlayer(this); } public bool CanTargetActor(Actor a) @@ -165,10 +162,7 @@ namespace OpenRA if (HasFogVisibility) return true; - if (a.TraitsImplementing().Any(t => !t.IsVisible(a, this))) - return false; - - return a.Trait().IsVisible(a, this); + return CanViewActor(a); } public bool HasFogVisibility { get { return fogVisibilities.Any(f => f.HasFogVisibility(this)); } }