Speed up checks for IOccupySpace trait.

Eagerly load the trait (if it exists) in Actor, and use this reference to avoid having to perform self.Info.HasTraitInfo<IOccupySpaceInfo>() checks.
This commit is contained in:
RoosterDragon
2015-10-14 20:36:32 +01:00
parent 410f121a1e
commit 262ab408b5
5 changed files with 13 additions and 18 deletions

View File

@@ -43,15 +43,7 @@ namespace OpenRA
public Rectangle Bounds { get; private set; } public Rectangle Bounds { get; private set; }
public Rectangle VisualBounds { get; private set; } public Rectangle VisualBounds { get; private set; }
public IEffectiveOwner EffectiveOwner { get; private set; } public IEffectiveOwner EffectiveOwner { get; private set; }
public IOccupySpace OccupiesSpace public IOccupySpace OccupiesSpace { get; private set; }
{
get
{
if (occupySpace == null)
occupySpace = Trait<IOccupySpace>();
return occupySpace;
}
}
public bool IsIdle { get { return currentActivity == null; } } public bool IsIdle { get { return currentActivity == null; } }
public bool IsDead { get { return Disposed || (health != null && health.IsDead); } } public bool IsDead { get { return Disposed || (health != null && health.IsDead); } }
@@ -69,7 +61,6 @@ namespace OpenRA
} }
} }
IOccupySpace occupySpace;
readonly IFacing facing; readonly IFacing facing;
readonly Health health; readonly Health health;
readonly IRenderModifier[] renderModifiers; readonly IRenderModifier[] renderModifiers;
@@ -96,7 +87,14 @@ namespace OpenRA
Info = world.Map.Rules.Actors[name]; Info = world.Map.Rules.Actors[name];
foreach (var trait in Info.TraitsInConstructOrder()) foreach (var trait in Info.TraitsInConstructOrder())
{
AddTrait(trait.Create(init)); AddTrait(trait.Create(init));
// Some traits rely on properties provided by IOccupySpace in their initialization,
// so we must ready it now, we cannot wait until all traits have finished construction.
if (trait is IOccupySpaceInfo)
OccupiesSpace = Trait<IOccupySpace>();
}
} }
Bounds = DetermineBounds(); Bounds = DetermineBounds();

View File

@@ -574,7 +574,7 @@ namespace OpenRA.Mods.Common.AI
// Pick something worth attacking owned by that player // Pick something worth attacking owned by that player
var target = World.Actors var target = World.Actors
.Where(a => a.Owner == enemy && a.Info.HasTraitInfo<IOccupySpaceInfo>()) .Where(a => a.Owner == enemy && a.OccupiesSpace != null)
.ClosestTo(World.Map.CenterOfCell(GetRandomBaseCenter())); .ClosestTo(World.Map.CenterOfCell(GetRandomBaseCenter()));
if (target == null) if (target == null)

View File

@@ -23,7 +23,7 @@ namespace OpenRA.Mods.Common
if (self.IsDead) if (self.IsDead)
return false; return false;
if (!self.Info.HasTraitInfo<IOccupySpaceInfo>()) if (self.OccupiesSpace == null)
return false; return false;
if (!self.IsInWorld) if (!self.IsInWorld)

View File

@@ -35,12 +35,9 @@ namespace OpenRA.Mods.Common.Traits
public readonly ProductionInfo Info; public readonly ProductionInfo Info;
public string Faction { get; private set; } public string Faction { get; private set; }
readonly bool occupiesSpace;
public Production(ActorInitializer init, ProductionInfo info) public Production(ActorInitializer init, ProductionInfo info)
{ {
Info = info; Info = info;
occupiesSpace = init.Self.Info.HasTraitInfo<IOccupySpaceInfo>();
rp = Exts.Lazy(() => init.Self.IsDead ? null : init.Self.TraitOrDefault<RallyPoint>()); rp = Exts.Lazy(() => init.Self.IsDead ? null : init.Self.TraitOrDefault<RallyPoint>());
Faction = init.Contains<FactionInit>() ? init.Get<FactionInit, string>() : init.Self.Owner.Faction.InternalName; Faction = init.Contains<FactionInit>() ? init.Get<FactionInit, string>() : init.Self.Owner.Faction.InternalName;
} }
@@ -60,7 +57,7 @@ namespace OpenRA.Mods.Common.Traits
new OwnerInit(self.Owner), new OwnerInit(self.Owner),
}; };
if (occupiesSpace) if (self.OccupiesSpace != null)
{ {
exit = self.Location + exitinfo.ExitCell; exit = self.Location + exitinfo.ExitCell;
var spawn = self.CenterPosition + exitinfo.SpawnOffset; var spawn = self.CenterPosition + exitinfo.SpawnOffset;
@@ -122,7 +119,7 @@ namespace OpenRA.Mods.Common.Traits
var exit = self.Info.TraitInfos<ExitInfo>().Shuffle(self.World.SharedRandom) var exit = self.Info.TraitInfos<ExitInfo>().Shuffle(self.World.SharedRandom)
.FirstOrDefault(e => CanUseExit(self, producee, e)); .FirstOrDefault(e => CanUseExit(self, producee, e));
if (exit != null || !occupiesSpace) if (exit != null || self.OccupiesSpace == null)
{ {
DoProduction(self, producee, exit, factionVariant); DoProduction(self, producee, exit, factionVariant);
return true; return true;

View File

@@ -25,7 +25,7 @@ namespace OpenRA.Mods.Common.Traits
{ {
public AmbientSound(Actor self, AmbientSoundInfo info) public AmbientSound(Actor self, AmbientSoundInfo info)
{ {
if (self.Info.HasTraitInfo<IOccupySpaceInfo>()) if (self.OccupiesSpace != null)
Game.Sound.PlayLooped(info.SoundFile, self.CenterPosition); Game.Sound.PlayLooped(info.SoundFile, self.CenterPosition);
else else
Game.Sound.PlayLooped(info.SoundFile); Game.Sound.PlayLooped(info.SoundFile);