Add a method to look up actors by ID.

This can be used to speed up some methods.
This commit is contained in:
RoosterDragon
2015-07-23 00:13:29 +01:00
parent d8ca66bec5
commit a0117a3890
3 changed files with 20 additions and 27 deletions

View File

@@ -72,7 +72,7 @@ namespace OpenRA
var subjectId = r.ReadUInt32();
var flags = (OrderFields)r.ReadByte();
var targetActorId = flags.HasField(OrderFields.TargetActor) ? r.ReadUInt32() : 0xffffffff;
var targetActorId = flags.HasField(OrderFields.TargetActor) ? r.ReadUInt32() : uint.MaxValue;
var targetLocation = (CPos)(flags.HasField(OrderFields.TargetLocation) ? r.ReadInt2() : int2.Zero);
var targetString = flags.HasField(OrderFields.TargetString) ? r.ReadString() : null;
var queued = flags.HasField(OrderFields.Queued);
@@ -104,28 +104,20 @@ namespace OpenRA
static uint UIntFromActor(Actor a)
{
if (a == null) return 0xffffffff;
if (a == null) return uint.MaxValue;
return a.ActorID;
}
static bool TryGetActorFromUInt(World world, uint aID, out Actor ret)
{
if (aID == 0xFFFFFFFF)
if (aID == uint.MaxValue)
{
ret = null;
return true;
}
else
{
foreach (var a in world.Actors.Where(x => x.ActorID == aID))
{
ret = a;
return true;
}
ret = null;
return false;
}
ret = world.GetActorById(aID);
return ret != null;
}
// Named constructors for Orders.

View File

@@ -26,15 +26,8 @@ namespace OpenRA
public sealed class World : IDisposable
{
class ActorIDComparer : IComparer<Actor>
{
public static readonly ActorIDComparer Instance = new ActorIDComparer();
ActorIDComparer() { }
public int Compare(Actor x, Actor y) { return x.ActorID.CompareTo(y.ActorID); }
}
internal readonly TraitDictionary TraitDict = new TraitDictionary();
readonly SortedSet<Actor> actors = new SortedSet<Actor>(ActorIDComparer.Instance);
readonly SortedDictionary<uint, Actor> actors = new SortedDictionary<uint, Actor>();
readonly List<IEffect> effects = new List<IEffect>();
readonly Queue<Action<World>> frameEndActions = new Queue<Action<World>>();
@@ -225,7 +218,7 @@ namespace OpenRA
public void Add(Actor a)
{
a.IsInWorld = true;
actors.Add(a);
actors.Add(a.ActorID, a);
ActorAdded(a);
foreach (var t in a.TraitsImplementing<INotifyAddedToWorld>())
@@ -235,7 +228,7 @@ namespace OpenRA
public void Remove(Actor a)
{
a.IsInWorld = false;
actors.Remove(a);
actors.Remove(a.ActorID);
ActorRemoved(a);
foreach (var t in a.TraitsImplementing<INotifyRemovedFromWorld>())
@@ -283,7 +276,7 @@ namespace OpenRA
ni.Trait.TickIdle(ni.Actor);
using (new PerfSample("tick_activities"))
foreach (var a in actors)
foreach (var a in actors.Values)
a.Tick();
ActorsWithTrait<ITick>().DoTimed(x => x.Trait.Tick(x.Actor), "Trait");
@@ -301,9 +294,17 @@ namespace OpenRA
ActorsWithTrait<ITickRender>().DoTimed(x => x.Trait.TickRender(wr, x.Actor), "Render");
}
public IEnumerable<Actor> Actors { get { return actors; } }
public IEnumerable<Actor> Actors { get { return actors.Values; } }
public IEnumerable<IEffect> Effects { get { return effects; } }
public Actor GetActorById(uint actorId)
{
Actor a;
if (actors.TryGetValue(actorId, out a))
return a;
return null;
}
uint nextAID = 0;
internal uint NextAID()
{
@@ -367,7 +368,7 @@ namespace OpenRA
Sound.StopVideo();
// Dispose newer actors first, and the world actor last
foreach (var a in actors.Reverse())
foreach (var a in actors.Values.Reverse())
a.Dispose();
// Actor disposals are done in a FrameEndTask

View File

@@ -26,7 +26,7 @@ namespace OpenRA.Mods.Common.Traits
/* create a group */
var actors = order.TargetString.Split(',')
.Select(id => uint.Parse(id, NumberStyles.Any, NumberFormatInfo.InvariantInfo))
.Select(id => self.World.Actors.FirstOrDefault(a => a.ActorID == id))
.Select(id => self.World.GetActorById(id))
.Where(a => a != null);
new Group(actors);