Merge pull request #9635 from RoosterDragon/occupy-space-perf

Speed up checks for IOccupySpace trait
This commit is contained in:
atlimit8
2015-10-22 18:28:57 -05:00
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 VisualBounds { get; private set; }
public IEffectiveOwner EffectiveOwner { get; private set; }
public IOccupySpace OccupiesSpace
{
get
{
if (occupySpace == null)
occupySpace = Trait<IOccupySpace>();
return occupySpace;
}
}
public IOccupySpace OccupiesSpace { get; private set; }
public bool IsIdle { get { return currentActivity == null; } }
public bool IsDead { get { return Disposed || (health != null && health.IsDead); } }
@@ -69,7 +61,6 @@ namespace OpenRA
}
}
IOccupySpace occupySpace;
readonly IFacing facing;
readonly IHealth health;
readonly IRenderModifier[] renderModifiers;
@@ -96,7 +87,14 @@ namespace OpenRA
Info = world.Map.Rules.Actors[name];
foreach (var trait in Info.TraitsInConstructOrder())
{
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();

View File

@@ -574,7 +574,7 @@ namespace OpenRA.Mods.Common.AI
// Pick something worth attacking owned by that player
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()));
if (target == null)

View File

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

View File

@@ -35,12 +35,9 @@ namespace OpenRA.Mods.Common.Traits
public readonly ProductionInfo Info;
public string Faction { get; private set; }
readonly bool occupiesSpace;
public Production(ActorInitializer init, ProductionInfo info)
{
Info = info;
occupiesSpace = init.Self.Info.HasTraitInfo<IOccupySpaceInfo>();
rp = Exts.Lazy(() => init.Self.IsDead ? null : init.Self.TraitOrDefault<RallyPoint>());
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),
};
if (occupiesSpace)
if (self.OccupiesSpace != null)
{
exit = self.Location + exitinfo.ExitCell;
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)
.FirstOrDefault(e => CanUseExit(self, producee, e));
if (exit != null || !occupiesSpace)
if (exit != null || self.OccupiesSpace == null)
{
DoProduction(self, producee, exit, factionVariant);
return true;

View File

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