From 909c5c703732ae76562f2a53a90e1d2fe75549e2 Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Sat, 14 Jun 2014 08:17:26 +0100 Subject: [PATCH] Some minor cleanup in TraitDictionary and affected callsites. --- OpenRA.Game/Graphics/WorldRenderer.cs | 2 +- OpenRA.Game/TraitDictionary.cs | 41 ++++++++++++++------------- OpenRA.Game/World.cs | 14 ++++----- OpenRA.Mods.RA/Effects/Missile.cs | 2 +- OpenRA.Mods.RA/PrimaryBuilding.cs | 7 +++-- 5 files changed, 34 insertions(+), 32 deletions(-) diff --git a/OpenRA.Game/Graphics/WorldRenderer.cs b/OpenRA.Game/Graphics/WorldRenderer.cs index 38ca787d67..f5f297a7e5 100644 --- a/OpenRA.Game/Graphics/WorldRenderer.cs +++ b/OpenRA.Game/Graphics/WorldRenderer.cs @@ -48,7 +48,7 @@ namespace OpenRA.Graphics palette = new HardwarePalette(); palettes = new Cache(CreatePaletteReference); - foreach (var pal in world.traitDict.ActorsWithTraitMultiple(world)) + foreach (var pal in world.traitDict.ActorsWithTrait()) pal.Trait.InitPalette(this); palette.Initialize(); diff --git a/OpenRA.Game/TraitDictionary.cs b/OpenRA.Game/TraitDictionary.cs index b6dace9b02..06086b317e 100755 --- a/OpenRA.Game/TraitDictionary.cs +++ b/OpenRA.Game/TraitDictionary.cs @@ -1,6 +1,6 @@ #region Copyright & License Information /* - * Copyright 2007-2011 The OpenRA Developers (see AUTHORS) + * Copyright 2007-2014 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, @@ -21,17 +21,14 @@ namespace OpenRA { var start = 0; var end = list.Count; - var mid = 0; while (start != end) { - mid = (start + end) / 2; - var c = list[mid].ActorID.CompareTo(searchFor); - if (c < 0) + var mid = (start + end) / 2; + if (list[mid].ActorID < searchFor) start = mid + 1; else end = mid; } - return start; } } @@ -53,6 +50,11 @@ namespace OpenRA return traits.GetOrAdd(t, doCreateTraitContainer); } + TraitContainer InnerGet() + { + return (TraitContainer)InnerGet(typeof(T)); + } + public void PrintReport() { Log.AddChannel("traitreport", "traitreport.log"); @@ -84,30 +86,30 @@ namespace OpenRA public bool Contains(Actor actor) { CheckDestroyed(actor); - return ((TraitContainer)InnerGet(typeof(T))).GetMultiple(actor.ActorID).Any(); + return InnerGet().GetMultiple(actor.ActorID).Any(); } public T Get(Actor actor) { CheckDestroyed(actor); - return ((TraitContainer)InnerGet(typeof(T))).Get(actor.ActorID); + return InnerGet().Get(actor.ActorID); } public T GetOrDefault(Actor actor) { CheckDestroyed(actor); - return ((TraitContainer)InnerGet(typeof(T))).GetOrDefault(actor.ActorID); + return InnerGet().GetOrDefault(actor.ActorID); } public IEnumerable WithInterface(Actor actor) { CheckDestroyed(actor); - return ((TraitContainer)InnerGet(typeof(T))).GetMultiple(actor.ActorID); + return InnerGet().GetMultiple(actor.ActorID); } - public IEnumerable> ActorsWithTraitMultiple(World world) + public IEnumerable> ActorsWithTrait() { - return ((TraitContainer)InnerGet(typeof(T))).All(); + return InnerGet().All(); } public void RemoveActor(Actor a) @@ -126,11 +128,10 @@ namespace OpenRA class TraitContainer : ITraitContainer { - List actors = new List(); - List traits = new List(); - int queries; + readonly List actors = new List(); + readonly List traits = new List(); - public int Queries { get { return queries; } } + public int Queries { get; private set; } public void Add(Actor actor, object trait) { @@ -142,14 +143,14 @@ namespace OpenRA public T Get(uint actor) { var result = GetOrDefault(actor); - if ((object)result == null) + if (result == null) throw new InvalidOperationException("Actor does not have trait of type `{0}`".F(typeof(T))); return result; } public T GetOrDefault(uint actor) { - ++queries; + ++Queries; var index = actors.BinarySearchMany(actor); if (index >= actors.Count || actors[index].ActorID != actor) return default(T); @@ -160,7 +161,7 @@ namespace OpenRA public IEnumerable GetMultiple(uint actor) { - ++queries; + ++Queries; var index = actors.BinarySearchMany(actor); while (index < actors.Count && actors[index].ActorID == actor) { @@ -171,7 +172,7 @@ namespace OpenRA public IEnumerable> All() { - ++queries; + ++Queries; for (var i = 0; i < actors.Count; i++) yield return new TraitPair { Actor = actors[i], Trait = traits[i] }; } diff --git a/OpenRA.Game/World.cs b/OpenRA.Game/World.cs index 5ada365b07..6e8d2c424d 100644 --- a/OpenRA.Game/World.cs +++ b/OpenRA.Game/World.cs @@ -25,17 +25,17 @@ namespace OpenRA { public class World { - internal TraitDictionary traitDict = new TraitDictionary(); - HashSet actors = new HashSet(); - List effects = new List(); - Queue> frameEndActions = new Queue>(); + internal readonly TraitDictionary traitDict = new TraitDictionary(); + readonly HashSet actors = new HashSet(); + readonly List effects = new List(); + readonly Queue> frameEndActions = new Queue>(); public int Timestep; internal readonly OrderManager orderManager; public Session LobbyInfo { get { return orderManager.LobbyInfo; } } - public MersenneTwister SharedRandom; + public readonly MersenneTwister SharedRandom; public readonly List Players = new List(); @@ -285,7 +285,7 @@ namespace OpenRA ret += n++ * (int)(1 + a.ActorID) * Sync.CalculateSyncHash(a); // hash all the traits that tick - foreach (var x in traitDict.ActorsWithTraitMultiple(this)) + foreach (var x in ActorsWithTrait()) ret += n++ * (int)(1 + x.Actor.ActorID) * Sync.CalculateSyncHash(x.Trait); // TODO: don't go over all effects @@ -305,7 +305,7 @@ namespace OpenRA public IEnumerable> ActorsWithTrait() { - return traitDict.ActorsWithTraitMultiple(this); + return traitDict.ActorsWithTrait(); } public void OnPlayerWinStateChanged(Player player) diff --git a/OpenRA.Mods.RA/Effects/Missile.cs b/OpenRA.Mods.RA/Effects/Missile.cs index 424f006f6d..2969933e32 100755 --- a/OpenRA.Mods.RA/Effects/Missile.cs +++ b/OpenRA.Mods.RA/Effects/Missile.cs @@ -121,7 +121,7 @@ namespace OpenRA.Mods.RA.Effects var dist = targetPosition + offset - pos; var desiredFacing = Traits.Util.GetFacing(dist, facing); var desiredAltitude = targetPosition.Z; - var jammed = info.Jammable && world.ActorsWithTrait().Any(j => JammedBy(j)); + var jammed = info.Jammable && world.ActorsWithTrait().Any(JammedBy); if (jammed) { diff --git a/OpenRA.Mods.RA/PrimaryBuilding.cs b/OpenRA.Mods.RA/PrimaryBuilding.cs index 6dc94fac21..6cf53664a3 100755 --- a/OpenRA.Mods.RA/PrimaryBuilding.cs +++ b/OpenRA.Mods.RA/PrimaryBuilding.cs @@ -69,9 +69,10 @@ namespace OpenRA.Mods.RA foreach (var p in self.Info.Traits.Get().Produces) foreach (var b in self.World .ActorsWithTrait() - .Where(a => a.Actor.Owner == self.Owner) - .Where(x => x.Trait.IsPrimary - && x.Actor.Info.Traits.Get().Produces.Contains(p))) + .Where(a => + a.Actor.Owner == self.Owner && + a.Trait.IsPrimary && + a.Actor.Info.Traits.Get().Produces.Contains(p))) b.Trait.SetPrimaryProducer(b.Actor, false); isPrimary = true;