Merge pull request #7671 from reaperrr/random-optimisations

More constructor caching and minor random optimisations
This commit is contained in:
Oliver Brakmann
2015-03-27 23:40:11 +01:00
33 changed files with 230 additions and 141 deletions

View File

@@ -20,7 +20,7 @@ namespace OpenRA.Mods.Common.Traits
{
public void TickIdle(Actor self)
{
self.QueueActivity(new FlyOffMap());
self.QueueActivity(new FlyOffMap(self));
self.QueueActivity(new RemoveSelf());
}
}

View File

@@ -35,7 +35,8 @@ namespace OpenRA.Mods.Common.Traits
public class Helicopter : Aircraft, ITick, IResolveOrder, IMove
{
public HelicopterInfo Info;
public readonly HelicopterInfo Info;
readonly bool fallsToEarth;
Actor self;
bool firstTick = true;
public bool IsMoving { get { return self.CenterPosition.Z > 0; } set { } }
@@ -45,6 +46,7 @@ namespace OpenRA.Mods.Common.Traits
{
self = init.Self;
Info = info;
fallsToEarth = self.HasTrait<FallsToEarth>();
}
public void ResolveOrder(Actor self, Order order)
@@ -74,7 +76,7 @@ namespace OpenRA.Mods.Common.Traits
if (Info.TurnToLand)
self.QueueActivity(new Turn(self, Info.InitialFacing));
self.QueueActivity(new HeliLand(true));
self.QueueActivity(new HeliLand(self, true));
}
}
@@ -83,7 +85,7 @@ namespace OpenRA.Mods.Common.Traits
if (Reservable.IsReserved(order.TargetActor))
{
self.CancelActivity();
self.QueueActivity(new HeliReturn());
self.QueueActivity(new HeliReturn(self));
}
else
{
@@ -99,16 +101,16 @@ namespace OpenRA.Mods.Common.Traits
self.CancelActivity();
self.QueueActivity(new HeliFly(self, Target.FromPos(order.TargetActor.CenterPosition + offset)));
self.QueueActivity(new Turn(self, Info.InitialFacing));
self.QueueActivity(new HeliLand(false));
self.QueueActivity(new ResupplyAircraft());
self.QueueActivity(new TakeOff());
self.QueueActivity(new HeliLand(self, false));
self.QueueActivity(new ResupplyAircraft(self));
self.QueueActivity(new TakeOff(self));
}
}
if (order.OrderString == "ReturnToBase")
{
self.CancelActivity();
self.QueueActivity(new HeliReturn());
self.QueueActivity(new HeliReturn(self));
}
if (order.OrderString == "Stop")
@@ -120,7 +122,7 @@ namespace OpenRA.Mods.Common.Traits
if (Info.TurnToLand)
self.QueueActivity(new Turn(self, Info.InitialFacing));
self.QueueActivity(new HeliLand(true));
self.QueueActivity(new HeliLand(self, true));
}
}
}
@@ -130,14 +132,14 @@ namespace OpenRA.Mods.Common.Traits
if (firstTick)
{
firstTick = false;
if (!self.HasTrait<FallsToEarth>()) // TODO: Aircraft husks don't properly unreserve.
if (!fallsToEarth) // TODO: Aircraft husks don't properly unreserve.
ReserveSpawnBuilding();
var host = GetActorBelow();
if (host == null)
return;
self.QueueActivity(new TakeOff());
self.QueueActivity(new TakeOff(self));
}
Repulse();
@@ -155,7 +157,7 @@ namespace OpenRA.Mods.Common.Traits
return new HeliFly(self, Target.FromCell(self.World, cell, subCell));
}
public Activity MoveIntoTarget(Actor self, Target target) { return new HeliLand(false); }
public Activity MoveIntoTarget(Actor self, Target target) { return new HeliLand(self, false); }
public Activity MoveToTarget(Actor self, Target target)
{
return Util.SequenceActivities(new HeliFly(self, target), new Turn(self, Info.InitialFacing));

View File

@@ -26,6 +26,7 @@ namespace OpenRA.Mods.Common.Traits
public class Plane : Aircraft, IResolveOrder, IMove, ITick, ISync
{
public readonly PlaneInfo Info;
readonly bool fallsToEarth;
[Sync] public WPos RTBPathHash;
Actor self;
public bool IsMoving { get { return self.CenterPosition.Z > 0; } set { } }
@@ -35,6 +36,7 @@ namespace OpenRA.Mods.Common.Traits
{
self = init.Self;
Info = info;
fallsToEarth = self.HasTrait<FallsToEarth>();
}
bool firstTick = true;
@@ -43,14 +45,14 @@ namespace OpenRA.Mods.Common.Traits
if (firstTick)
{
firstTick = false;
if (!self.HasTrait<FallsToEarth>()) // TODO: Aircraft husks don't properly unreserve.
if (!fallsToEarth) // TODO: Aircraft husks don't properly unreserve.
ReserveSpawnBuilding();
var host = GetActorBelow();
if (host == null)
return;
self.QueueActivity(new TakeOff());
self.QueueActivity(new TakeOff(self));
}
Repulse();
@@ -89,7 +91,7 @@ namespace OpenRA.Mods.Common.Traits
self.SetTargetLine(target, Color.Green);
self.CancelActivity();
self.QueueActivity(new Fly(self, target));
self.QueueActivity(new FlyCircle());
self.QueueActivity(new FlyCircle(self));
}
else if (order.OrderString == "Enter")
{
@@ -101,7 +103,7 @@ namespace OpenRA.Mods.Common.Traits
self.CancelActivity();
self.QueueActivity(new ReturnToBase(self, order.TargetActor));
self.QueueActivity(new ResupplyAircraft());
self.QueueActivity(new ResupplyAircraft(self));
}
else if (order.OrderString == "Stop")
{
@@ -117,7 +119,7 @@ namespace OpenRA.Mods.Common.Traits
self.CancelActivity();
self.SetTargetLine(Target.FromActor(airfield), Color.Green);
self.QueueActivity(new ReturnToBase(self, airfield));
self.QueueActivity(new ResupplyAircraft());
self.QueueActivity(new ResupplyAircraft(self));
}
else
{
@@ -126,12 +128,12 @@ namespace OpenRA.Mods.Common.Traits
}
}
public Activity MoveTo(CPos cell, int nearEnough) { return Util.SequenceActivities(new Fly(self, Target.FromCell(self.World, cell)), new FlyCircle()); }
public Activity MoveTo(CPos cell, Actor ignoredActor) { return Util.SequenceActivities(new Fly(self, Target.FromCell(self.World, cell)), new FlyCircle()); }
public Activity MoveWithinRange(Target target, WRange range) { return Util.SequenceActivities(new Fly(self, target, WRange.Zero, range), new FlyCircle()); }
public Activity MoveTo(CPos cell, int nearEnough) { return Util.SequenceActivities(new Fly(self, Target.FromCell(self.World, cell)), new FlyCircle(self)); }
public Activity MoveTo(CPos cell, Actor ignoredActor) { return Util.SequenceActivities(new Fly(self, Target.FromCell(self.World, cell)), new FlyCircle(self)); }
public Activity MoveWithinRange(Target target, WRange range) { return Util.SequenceActivities(new Fly(self, target, WRange.Zero, range), new FlyCircle(self)); }
public Activity MoveWithinRange(Target target, WRange minRange, WRange maxRange)
{
return Util.SequenceActivities(new Fly(self, target, minRange, maxRange), new FlyCircle());
return Util.SequenceActivities(new Fly(self, target, minRange, maxRange), new FlyCircle(self));
}
public Activity MoveFollow(Actor self, Target target, WRange minRange, WRange maxRange) { return new FlyFollow(self, target, minRange, maxRange); }
@@ -144,6 +146,6 @@ namespace OpenRA.Mods.Common.Traits
}
public Activity MoveToTarget(Actor self, Target target) { return new Fly(self, target, WRange.FromCells(3), WRange.FromCells(5)); }
public Activity MoveIntoTarget(Actor self, Target target) { return new Land(target); }
public Activity MoveIntoTarget(Actor self, Target target) { return new Land(self, target); }
}
}

View File

@@ -29,7 +29,7 @@ namespace OpenRA.Mods.Common.Traits
if (airfield != null)
{
self.QueueActivity(new ReturnToBase(self, airfield));
self.QueueActivity(new ResupplyAircraft());
self.QueueActivity(new ResupplyAircraft(self));
}
else
{
@@ -49,13 +49,13 @@ namespace OpenRA.Mods.Common.Traits
if (someBuilding == null)
{
// ... going down the garden to eat worms ...
self.QueueActivity(new FlyOffMap());
self.QueueActivity(new FlyOffMap(self));
self.QueueActivity(new RemoveSelf());
return;
}
self.QueueActivity(new Fly(self, Target.FromActor(someBuilding)));
self.QueueActivity(new FlyCircle());
self.QueueActivity(new FlyCircle(self));
}
}
}

View File

@@ -25,8 +25,8 @@ namespace OpenRA.Mods.Common.Traits
class Burns : ITick, ISync
{
readonly BurnsInfo info;
[Sync] int ticks;
BurnsInfo info;
public Burns(Actor self, BurnsInfo info)
{

View File

@@ -120,7 +120,7 @@ namespace OpenRA.Mods.Common.Traits
Unloading = true;
self.CancelActivity();
if (helicopter != null)
self.QueueActivity(new HeliLand(true));
self.QueueActivity(new HeliLand(self, true));
self.QueueActivity(new UnloadCargo(self, true));
}
}

View File

@@ -51,21 +51,20 @@ namespace OpenRA.Mods.Common.Traits
public class ExternalCapturable : ITick
{
readonly Building building;
[Sync] public int CaptureProgressTime = 0;
[Sync] public Actor Captor;
private Actor self;
public ExternalCapturableInfo Info;
public bool CaptureInProgress { get { return Captor != null; } }
public ExternalCapturable(Actor self, ExternalCapturableInfo info)
{
this.self = self;
Info = info;
building = self.TraitOrDefault<Building>();
}
public void BeginCapture(Actor captor)
{
var building = self.TraitOrDefault<Building>();
if (building != null)
building.Lock();
@@ -74,7 +73,6 @@ namespace OpenRA.Mods.Common.Traits
public void EndCapture()
{
var building = self.TraitOrDefault<Building>();
if (building != null)
building.Unlock();

View File

@@ -99,7 +99,7 @@ namespace OpenRA.Mods.Common.Traits
self.CancelActivity();
self.SetTargetLine(target, Color.Red);
self.QueueActivity(new ExternalCaptureActor(target));
self.QueueActivity(new ExternalCaptureActor(self, target));
}
}

View File

@@ -30,6 +30,7 @@ namespace OpenRA.Mods.Common.Traits
class ScaredyCat : ITick, INotifyIdle, INotifyDamage, INotifyAttack, ISpeedModifier, ISync, IRenderInfantrySequenceModifier
{
readonly ScaredyCatInfo info;
readonly Mobile mobile;
[Sync] readonly Actor self;
[Sync] int panicStartedTick;
bool Panicking { get { return panicStartedTick > 0; } }
@@ -41,6 +42,7 @@ namespace OpenRA.Mods.Common.Traits
{
this.self = self;
this.info = info;
mobile = self.Trait<Mobile>();
}
public void Panic()
@@ -68,7 +70,7 @@ namespace OpenRA.Mods.Common.Traits
if (!Panicking)
return;
self.Trait<Mobile>().Nudge(self, self, true);
mobile.Nudge(self, self, true);
}
public void Damaged(Actor self, AttackInfo e)

View File

@@ -27,21 +27,22 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Custom palette name")]
public readonly string Palette = "effect";
public object Create(ActorInitializer init) { return new WithBuildingExplosion(this); }
public object Create(ActorInitializer init) { return new WithBuildingExplosion(init.Self, this); }
}
class WithBuildingExplosion : INotifyKilled
{
WithBuildingExplosionInfo info;
BuildingInfo buildingInfo;
public WithBuildingExplosion(WithBuildingExplosionInfo info)
public WithBuildingExplosion(Actor self, WithBuildingExplosionInfo info)
{
this.info = info;
buildingInfo = self.Info.Traits.Get<BuildingInfo>();
}
public void Killed(Actor self, AttackInfo e)
{
var buildingInfo = self.Info.Traits.Get<BuildingInfo>();
var cells = FootprintUtils.UnpathableTiles(self.Info.Name, buildingInfo, self.Location);
if (info.Delay > 0)

View File

@@ -29,8 +29,14 @@ namespace OpenRA.Mods.Common.Traits
{
readonly Actor self;
readonly RepairableNearInfo info;
readonly IMove movement;
public RepairableNear(Actor self, RepairableNearInfo info) { this.self = self; this.info = info; }
public RepairableNear(Actor self, RepairableNearInfo info)
{
this.self = self;
this.info = info;
movement = self.Trait<IMove>();
}
public IEnumerable<IOrderTargeter> Orders
{
@@ -63,7 +69,6 @@ namespace OpenRA.Mods.Common.Traits
{
if (order.OrderString == "RepairNear" && CanRepairAt(order.TargetActor) && ShouldRepair())
{
var movement = self.Trait<IMove>();
var target = Target.FromOrder(self.World, order);
self.CancelActivity();

View File

@@ -29,12 +29,18 @@ namespace OpenRA.Mods.Common.Traits
{
readonly Actor self;
readonly Lazy<Health> health;
readonly SellableInfo info;
readonly Building building;
readonly WithMakeAnimation makeAnimation;
public Sellable(Actor self, SellableInfo info)
: base(info)
{
this.self = self;
this.info = info;
health = Exts.Lazy(() => self.TraitOrDefault<Health>());
building = self.TraitOrDefault<Building>();
makeAnimation = self.TraitOrDefault<WithMakeAnimation>();
}
public void ResolveOrder(Actor self, Order order)
@@ -48,23 +54,21 @@ namespace OpenRA.Mods.Common.Traits
if (IsTraitDisabled)
return;
var building = self.TraitOrDefault<Building>();
if (building != null && !building.Lock())
return;
self.CancelActivity();
foreach (var s in Info.SellSounds)
foreach (var s in info.SellSounds)
Sound.PlayToPlayer(self.Owner, s, self.CenterPosition);
foreach (var ns in self.TraitsImplementing<INotifySold>())
ns.Selling(self);
var makeAnimation = self.TraitOrDefault<WithMakeAnimation>();
if (makeAnimation != null)
makeAnimation.Reverse(self, new Sell(), false);
makeAnimation.Reverse(self, new Sell(self), false);
else
self.QueueActivity(false, new Sell());
self.QueueActivity(false, new Sell(self));
}
public bool IsTooltipVisible(Player forPlayer)
@@ -78,7 +82,7 @@ namespace OpenRA.Mods.Common.Traits
{
get
{
var sellValue = self.GetSellValue() * Info.RefundPercent / 100;
var sellValue = self.GetSellValue() * info.RefundPercent / 100;
if (health.Value != null)
{
sellValue *= health.Value.HP;

View File

@@ -46,14 +46,18 @@ namespace OpenRA.Mods.Common.Traits
{
readonly Actor self;
readonly TransformsInfo info;
readonly BuildingInfo bi;
readonly Building building;
readonly BuildingInfo buildingInfo;
readonly string race;
readonly WithMakeAnimation makeAnimation;
public Transforms(ActorInitializer init, TransformsInfo info)
{
self = init.Self;
this.info = info;
bi = self.World.Map.Rules.Actors[info.IntoActor].Traits.GetOrDefault<BuildingInfo>();
buildingInfo = self.World.Map.Rules.Actors[info.IntoActor].Traits.GetOrDefault<BuildingInfo>();
building = self.TraitOrDefault<Building>();
makeAnimation = self.TraitOrDefault<WithMakeAnimation>();
race = init.Contains<RaceInit>() ? init.Get<RaceInit, string>() : self.Owner.Country.Race;
}
@@ -64,11 +68,10 @@ namespace OpenRA.Mods.Common.Traits
bool CanDeploy()
{
var b = self.TraitOrDefault<Building>();
if (b != null && b.Locked)
if (building != null && building.Locked)
return false;
return bi == null || self.World.CanPlaceBuilding(info.IntoActor, bi, self.Location + info.Offset, self);
return buildingInfo == null || self.World.CanPlaceBuilding(info.IntoActor, buildingInfo, self.Location + info.Offset, self);
}
public IEnumerable<IOrderTargeter> Orders
@@ -86,9 +89,7 @@ namespace OpenRA.Mods.Common.Traits
public void DeployTransform(bool queued)
{
var b = self.TraitOrDefault<Building>();
if (!CanDeploy() || (b != null && !b.Lock()))
if (!CanDeploy() || (building != null && !building.Lock()))
{
foreach (var s in info.NoTransformSounds)
Sound.PlayToPlayer(self.Owner, s);
@@ -116,7 +117,6 @@ namespace OpenRA.Mods.Common.Traits
Race = race
};
var makeAnimation = self.TraitOrDefault<WithMakeAnimation>();
if (makeAnimation != null)
makeAnimation.Reverse(self, transform);
else