Some minor cleanup in TraitDictionary and affected callsites.

This commit is contained in:
RoosterDragon
2014-06-14 08:17:26 +01:00
parent 50d3929862
commit 909c5c7037
5 changed files with 34 additions and 32 deletions

View File

@@ -48,7 +48,7 @@ namespace OpenRA.Graphics
palette = new HardwarePalette(); palette = new HardwarePalette();
palettes = new Cache<string, PaletteReference>(CreatePaletteReference); palettes = new Cache<string, PaletteReference>(CreatePaletteReference);
foreach (var pal in world.traitDict.ActorsWithTraitMultiple<IPalette>(world)) foreach (var pal in world.traitDict.ActorsWithTrait<IPalette>())
pal.Trait.InitPalette(this); pal.Trait.InitPalette(this);
palette.Initialize(); palette.Initialize();

View File

@@ -1,6 +1,6 @@
#region Copyright & License Information #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 * 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 * available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information, * as published by the Free Software Foundation. For more information,
@@ -21,17 +21,14 @@ namespace OpenRA
{ {
var start = 0; var start = 0;
var end = list.Count; var end = list.Count;
var mid = 0;
while (start != end) while (start != end)
{ {
mid = (start + end) / 2; var mid = (start + end) / 2;
var c = list[mid].ActorID.CompareTo(searchFor); if (list[mid].ActorID < searchFor)
if (c < 0)
start = mid + 1; start = mid + 1;
else else
end = mid; end = mid;
} }
return start; return start;
} }
} }
@@ -53,6 +50,11 @@ namespace OpenRA
return traits.GetOrAdd(t, doCreateTraitContainer); return traits.GetOrAdd(t, doCreateTraitContainer);
} }
TraitContainer<T> InnerGet<T>()
{
return (TraitContainer<T>)InnerGet(typeof(T));
}
public void PrintReport() public void PrintReport()
{ {
Log.AddChannel("traitreport", "traitreport.log"); Log.AddChannel("traitreport", "traitreport.log");
@@ -84,30 +86,30 @@ namespace OpenRA
public bool Contains<T>(Actor actor) public bool Contains<T>(Actor actor)
{ {
CheckDestroyed(actor); CheckDestroyed(actor);
return ((TraitContainer<T>)InnerGet(typeof(T))).GetMultiple(actor.ActorID).Any(); return InnerGet<T>().GetMultiple(actor.ActorID).Any();
} }
public T Get<T>(Actor actor) public T Get<T>(Actor actor)
{ {
CheckDestroyed(actor); CheckDestroyed(actor);
return ((TraitContainer<T>)InnerGet(typeof(T))).Get(actor.ActorID); return InnerGet<T>().Get(actor.ActorID);
} }
public T GetOrDefault<T>(Actor actor) public T GetOrDefault<T>(Actor actor)
{ {
CheckDestroyed(actor); CheckDestroyed(actor);
return ((TraitContainer<T>)InnerGet(typeof(T))).GetOrDefault(actor.ActorID); return InnerGet<T>().GetOrDefault(actor.ActorID);
} }
public IEnumerable<T> WithInterface<T>(Actor actor) public IEnumerable<T> WithInterface<T>(Actor actor)
{ {
CheckDestroyed(actor); CheckDestroyed(actor);
return ((TraitContainer<T>)InnerGet(typeof(T))).GetMultiple(actor.ActorID); return InnerGet<T>().GetMultiple(actor.ActorID);
} }
public IEnumerable<TraitPair<T>> ActorsWithTraitMultiple<T>(World world) public IEnumerable<TraitPair<T>> ActorsWithTrait<T>()
{ {
return ((TraitContainer<T>)InnerGet(typeof(T))).All(); return InnerGet<T>().All();
} }
public void RemoveActor(Actor a) public void RemoveActor(Actor a)
@@ -126,11 +128,10 @@ namespace OpenRA
class TraitContainer<T> : ITraitContainer class TraitContainer<T> : ITraitContainer
{ {
List<Actor> actors = new List<Actor>(); readonly List<Actor> actors = new List<Actor>();
List<T> traits = new List<T>(); readonly List<T> traits = new List<T>();
int queries;
public int Queries { get { return queries; } } public int Queries { get; private set; }
public void Add(Actor actor, object trait) public void Add(Actor actor, object trait)
{ {
@@ -142,14 +143,14 @@ namespace OpenRA
public T Get(uint actor) public T Get(uint actor)
{ {
var result = GetOrDefault(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))); throw new InvalidOperationException("Actor does not have trait of type `{0}`".F(typeof(T)));
return result; return result;
} }
public T GetOrDefault(uint actor) public T GetOrDefault(uint actor)
{ {
++queries; ++Queries;
var index = actors.BinarySearchMany(actor); var index = actors.BinarySearchMany(actor);
if (index >= actors.Count || actors[index].ActorID != actor) if (index >= actors.Count || actors[index].ActorID != actor)
return default(T); return default(T);
@@ -160,7 +161,7 @@ namespace OpenRA
public IEnumerable<T> GetMultiple(uint actor) public IEnumerable<T> GetMultiple(uint actor)
{ {
++queries; ++Queries;
var index = actors.BinarySearchMany(actor); var index = actors.BinarySearchMany(actor);
while (index < actors.Count && actors[index].ActorID == actor) while (index < actors.Count && actors[index].ActorID == actor)
{ {
@@ -171,7 +172,7 @@ namespace OpenRA
public IEnumerable<TraitPair<T>> All() public IEnumerable<TraitPair<T>> All()
{ {
++queries; ++Queries;
for (var i = 0; i < actors.Count; i++) for (var i = 0; i < actors.Count; i++)
yield return new TraitPair<T> { Actor = actors[i], Trait = traits[i] }; yield return new TraitPair<T> { Actor = actors[i], Trait = traits[i] };
} }

View File

@@ -25,17 +25,17 @@ namespace OpenRA
{ {
public class World public class World
{ {
internal TraitDictionary traitDict = new TraitDictionary(); internal readonly TraitDictionary traitDict = new TraitDictionary();
HashSet<Actor> actors = new HashSet<Actor>(); readonly HashSet<Actor> actors = new HashSet<Actor>();
List<IEffect> effects = new List<IEffect>(); readonly List<IEffect> effects = new List<IEffect>();
Queue<Action<World>> frameEndActions = new Queue<Action<World>>(); readonly Queue<Action<World>> frameEndActions = new Queue<Action<World>>();
public int Timestep; public int Timestep;
internal readonly OrderManager orderManager; internal readonly OrderManager orderManager;
public Session LobbyInfo { get { return orderManager.LobbyInfo; } } public Session LobbyInfo { get { return orderManager.LobbyInfo; } }
public MersenneTwister SharedRandom; public readonly MersenneTwister SharedRandom;
public readonly List<Player> Players = new List<Player>(); public readonly List<Player> Players = new List<Player>();
@@ -285,7 +285,7 @@ namespace OpenRA
ret += n++ * (int)(1 + a.ActorID) * Sync.CalculateSyncHash(a); ret += n++ * (int)(1 + a.ActorID) * Sync.CalculateSyncHash(a);
// hash all the traits that tick // hash all the traits that tick
foreach (var x in traitDict.ActorsWithTraitMultiple<ISync>(this)) foreach (var x in ActorsWithTrait<ISync>())
ret += n++ * (int)(1 + x.Actor.ActorID) * Sync.CalculateSyncHash(x.Trait); ret += n++ * (int)(1 + x.Actor.ActorID) * Sync.CalculateSyncHash(x.Trait);
// TODO: don't go over all effects // TODO: don't go over all effects
@@ -305,7 +305,7 @@ namespace OpenRA
public IEnumerable<TraitPair<T>> ActorsWithTrait<T>() public IEnumerable<TraitPair<T>> ActorsWithTrait<T>()
{ {
return traitDict.ActorsWithTraitMultiple<T>(this); return traitDict.ActorsWithTrait<T>();
} }
public void OnPlayerWinStateChanged(Player player) public void OnPlayerWinStateChanged(Player player)

View File

@@ -121,7 +121,7 @@ namespace OpenRA.Mods.RA.Effects
var dist = targetPosition + offset - pos; var dist = targetPosition + offset - pos;
var desiredFacing = Traits.Util.GetFacing(dist, facing); var desiredFacing = Traits.Util.GetFacing(dist, facing);
var desiredAltitude = targetPosition.Z; var desiredAltitude = targetPosition.Z;
var jammed = info.Jammable && world.ActorsWithTrait<JamsMissiles>().Any(j => JammedBy(j)); var jammed = info.Jammable && world.ActorsWithTrait<JamsMissiles>().Any(JammedBy);
if (jammed) if (jammed)
{ {

View File

@@ -69,9 +69,10 @@ namespace OpenRA.Mods.RA
foreach (var p in self.Info.Traits.Get<ProductionInfo>().Produces) foreach (var p in self.Info.Traits.Get<ProductionInfo>().Produces)
foreach (var b in self.World foreach (var b in self.World
.ActorsWithTrait<PrimaryBuilding>() .ActorsWithTrait<PrimaryBuilding>()
.Where(a => a.Actor.Owner == self.Owner) .Where(a =>
.Where(x => x.Trait.IsPrimary a.Actor.Owner == self.Owner &&
&& x.Actor.Info.Traits.Get<ProductionInfo>().Produces.Contains(p))) a.Trait.IsPrimary &&
a.Actor.Info.Traits.Get<ProductionInfo>().Produces.Contains(p)))
b.Trait.SetPrimaryProducer(b.Actor, false); b.Trait.SetPrimaryProducer(b.Actor, false);
isPrimary = true; isPrimary = true;