diff --git a/OpenRA.Mods.Common/Traits/Armament.cs b/OpenRA.Mods.Common/Traits/Armament.cs index 3c64116f34..5bf642ef11 100644 --- a/OpenRA.Mods.Common/Traits/Armament.cs +++ b/OpenRA.Mods.Common/Traits/Armament.cs @@ -87,17 +87,17 @@ namespace OpenRA.Mods.Common.Traits } } - public class Armament : UpgradableTrait, ITick, IExplodeModifier + public class Armament : UpgradableTrait, INotifyCreated, ITick, IExplodeModifier { public readonly WeaponInfo Weapon; public readonly Barrel[] Barrels; readonly Actor self; - Lazy turret; - Lazy coords; - Lazy ammoPool; + Turreted turret; + AmmoPool ammoPool; + BodyOrientation coords; + IEnumerable rangeModifiers; List> delayedActions = new List>(); - Lazy> rangeModifiers; public WDist Recoil; public int FireDelay { get; private set; } @@ -108,12 +108,6 @@ namespace OpenRA.Mods.Common.Traits { this.self = self; - // We can't resolve these until runtime - turret = Exts.Lazy(() => self.TraitsImplementing().FirstOrDefault(t => t.Name == info.Turret)); - coords = Exts.Lazy(() => self.Trait()); - ammoPool = Exts.Lazy(() => self.TraitsImplementing().FirstOrDefault(la => la.Info.Name == info.AmmoPoolName)); - rangeModifiers = Exts.Lazy(() => self.TraitsImplementing().ToArray().Select(m => m.GetRangeModifier())); - Weapon = info.WeaponInfo; Burst = Weapon.Burst; @@ -135,7 +129,15 @@ namespace OpenRA.Mods.Common.Traits 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().FirstOrDefault(t => t.Name == Info.Turret); + ammoPool = self.TraitsImplementing().FirstOrDefault(la => la.Info.Name == Info.AmmoPoolName); + coords = self.Trait(); + rangeModifiers = self.TraitsImplementing().ToArray().Select(m => m.GetRangeModifier()); } public void Tick(Actor self) @@ -174,7 +176,7 @@ namespace OpenRA.Mods.Common.Traits if (IsReloading) return null; - if (ammoPool.Value != null && !ammoPool.Value.HasAmmo()) + if (ammoPool != null && !ammoPool.HasAmmo()) return null; if (!target.IsInRange(self.CenterPosition, MaxRange())) @@ -247,23 +249,23 @@ namespace OpenRA.Mods.Common.Traits 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); - 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 += 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) { var orientation = self.Orientation + WRot.FromYaw(b.Yaw); - if (turret.Value != null) - orientation += turret.Value.LocalOrientation(self); + if (turret != null) + orientation += turret.LocalOrientation(self); return orientation; } diff --git a/OpenRA.Mods.Common/Traits/Attack/AttackBase.cs b/OpenRA.Mods.Common/Traits/Attack/AttackBase.cs index 3580e2cacd..c941d439ce 100644 --- a/OpenRA.Mods.Common/Traits/Attack/AttackBase.cs +++ b/OpenRA.Mods.Common/Traits/Attack/AttackBase.cs @@ -18,7 +18,7 @@ using OpenRA.Traits; namespace OpenRA.Mods.Common.Traits { - public abstract class AttackBaseInfo : UpgradableTraitInfo, ITraitInfo + public abstract class AttackBaseInfo : UpgradableTraitInfo { [Desc("Armament names")] public readonly string[] Armaments = { "primary", "secondary" }; @@ -70,7 +70,10 @@ namespace OpenRA.Mods.Common.Traits 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; if (!HasAnyValidWeapons(target)) @@ -80,15 +83,9 @@ namespace OpenRA.Mods.Common.Traits if (building.Value != null && !building.Value.BuildComplete) return false; - if (!target.IsValidFor(self)) - return false; - if (Armaments.All(a => a.IsReloading)) return false; - if (self.IsDisabled()) - return false; - if (target.Type == TargetType.Actor && !self.Owner.CanTargetActor(target.Actor)) return false;