Merge pull request #10189 from reaperrr/less-lazy1

AttackBase and Armament optimizations
This commit is contained in:
Matthias Mailänder
2015-12-25 14:04:46 +01:00
2 changed files with 27 additions and 28 deletions

View File

@@ -87,17 +87,17 @@ namespace OpenRA.Mods.Common.Traits
} }
} }
public class Armament : UpgradableTrait<ArmamentInfo>, ITick, IExplodeModifier public class Armament : UpgradableTrait<ArmamentInfo>, INotifyCreated, ITick, IExplodeModifier
{ {
public readonly WeaponInfo Weapon; public readonly WeaponInfo Weapon;
public readonly Barrel[] Barrels; public readonly Barrel[] Barrels;
readonly Actor self; readonly Actor self;
Lazy<Turreted> turret; Turreted turret;
Lazy<BodyOrientation> coords; AmmoPool ammoPool;
Lazy<AmmoPool> ammoPool; BodyOrientation coords;
IEnumerable<int> rangeModifiers;
List<Pair<int, Action>> delayedActions = new List<Pair<int, Action>>(); List<Pair<int, Action>> delayedActions = new List<Pair<int, Action>>();
Lazy<IEnumerable<int>> rangeModifiers;
public WDist Recoil; public WDist Recoil;
public int FireDelay { get; private set; } public int FireDelay { get; private set; }
@@ -108,12 +108,6 @@ namespace OpenRA.Mods.Common.Traits
{ {
this.self = self; this.self = self;
// We can't resolve these until runtime
turret = Exts.Lazy(() => self.TraitsImplementing<Turreted>().FirstOrDefault(t => t.Name == info.Turret));
coords = Exts.Lazy(() => self.Trait<BodyOrientation>());
ammoPool = Exts.Lazy(() => self.TraitsImplementing<AmmoPool>().FirstOrDefault(la => la.Info.Name == info.AmmoPoolName));
rangeModifiers = Exts.Lazy(() => self.TraitsImplementing<IRangeModifier>().ToArray().Select(m => m.GetRangeModifier()));
Weapon = info.WeaponInfo; Weapon = info.WeaponInfo;
Burst = Weapon.Burst; Burst = Weapon.Burst;
@@ -135,7 +129,15 @@ namespace OpenRA.Mods.Common.Traits
public WDist MaxRange() public WDist MaxRange()
{ {
return new WDist(Util.ApplyPercentageModifiers(Weapon.Range.Length, rangeModifiers.Value)); return new WDist(Util.ApplyPercentageModifiers(Weapon.Range.Length, rangeModifiers));
}
public void Created(Actor self)
{
turret = self.TraitsImplementing<Turreted>().FirstOrDefault(t => t.Name == Info.Turret);
ammoPool = self.TraitsImplementing<AmmoPool>().FirstOrDefault(la => la.Info.Name == Info.AmmoPoolName);
coords = self.Trait<BodyOrientation>();
rangeModifiers = self.TraitsImplementing<IRangeModifier>().ToArray().Select(m => m.GetRangeModifier());
} }
public void Tick(Actor self) public void Tick(Actor self)
@@ -174,7 +176,7 @@ namespace OpenRA.Mods.Common.Traits
if (IsReloading) if (IsReloading)
return null; return null;
if (ammoPool.Value != null && !ammoPool.Value.HasAmmo()) if (ammoPool != null && !ammoPool.HasAmmo())
return null; return null;
if (!target.IsInRange(self.CenterPosition, MaxRange())) if (!target.IsInRange(self.CenterPosition, MaxRange()))
@@ -247,23 +249,23 @@ namespace OpenRA.Mods.Common.Traits
public WVec MuzzleOffset(Actor self, Barrel b) public WVec MuzzleOffset(Actor self, Barrel b)
{ {
var bodyOrientation = coords.Value.QuantizeOrientation(self, self.Orientation); var bodyOrientation = coords.QuantizeOrientation(self, self.Orientation);
var localOffset = b.Offset + new WVec(-Recoil, WDist.Zero, WDist.Zero); var localOffset = b.Offset + new WVec(-Recoil, WDist.Zero, WDist.Zero);
if (turret.Value != null) if (turret != null)
{ {
var turretOrientation = coords.Value.QuantizeOrientation(self, turret.Value.LocalOrientation(self)); var turretOrientation = coords.QuantizeOrientation(self, turret.LocalOrientation(self));
localOffset = localOffset.Rotate(turretOrientation); localOffset = localOffset.Rotate(turretOrientation);
localOffset += turret.Value.Offset; localOffset += turret.Offset;
} }
return coords.Value.LocalToWorld(localOffset.Rotate(bodyOrientation)); return coords.LocalToWorld(localOffset.Rotate(bodyOrientation));
} }
public WRot MuzzleOrientation(Actor self, Barrel b) public WRot MuzzleOrientation(Actor self, Barrel b)
{ {
var orientation = self.Orientation + WRot.FromYaw(b.Yaw); var orientation = self.Orientation + WRot.FromYaw(b.Yaw);
if (turret.Value != null) if (turret != null)
orientation += turret.Value.LocalOrientation(self); orientation += turret.LocalOrientation(self);
return orientation; return orientation;
} }

View File

@@ -18,7 +18,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
{ {
public abstract class AttackBaseInfo : UpgradableTraitInfo, ITraitInfo public abstract class AttackBaseInfo : UpgradableTraitInfo
{ {
[Desc("Armament names")] [Desc("Armament names")]
public readonly string[] Armaments = { "primary", "secondary" }; public readonly string[] Armaments = { "primary", "secondary" };
@@ -70,7 +70,10 @@ namespace OpenRA.Mods.Common.Traits
protected virtual bool CanAttack(Actor self, Target target) protected virtual bool CanAttack(Actor self, Target target)
{ {
if (!self.IsInWorld || IsTraitDisabled) if (!self.IsInWorld || IsTraitDisabled || self.IsDisabled())
return false;
if (!target.IsValidFor(self))
return false; return false;
if (!HasAnyValidWeapons(target)) if (!HasAnyValidWeapons(target))
@@ -80,15 +83,9 @@ namespace OpenRA.Mods.Common.Traits
if (building.Value != null && !building.Value.BuildComplete) if (building.Value != null && !building.Value.BuildComplete)
return false; return false;
if (!target.IsValidFor(self))
return false;
if (Armaments.All(a => a.IsReloading)) if (Armaments.All(a => a.IsReloading))
return false; return false;
if (self.IsDisabled())
return false;
if (target.Type == TargetType.Actor && !self.Owner.CanTargetActor(target.Actor)) if (target.Type == TargetType.Actor && !self.Owner.CanTargetActor(target.Actor))
return false; return false;