From 16386873afaa23810375774743e34474ed3aad6a Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Sat, 17 Oct 2015 00:55:52 +0100 Subject: [PATCH] Optimize AttackBase.ChooseArmamentsForTarget. Remove the unused onlyEnabled parameter to avoid it needlessly being captured in the closure, and simplify the logic. Evaluate RequiresForceFire and Weapon.IsValidAgainst last in their respective logic chains as these are the most expensive operations and benefit from short-circuiting being able to skip their evaluation. --- OpenRA.Mods.Common/Traits/Attack/AttackBase.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/OpenRA.Mods.Common/Traits/Attack/AttackBase.cs b/OpenRA.Mods.Common/Traits/Attack/AttackBase.cs index 8acfa94f73..8e7fa78869 100644 --- a/OpenRA.Mods.Common/Traits/Attack/AttackBase.cs +++ b/OpenRA.Mods.Common/Traits/Attack/AttackBase.cs @@ -233,17 +233,17 @@ namespace OpenRA.Mods.Common.Traits } // Enumerates all armaments, that this actor possesses, that can be used against Target t - public IEnumerable ChooseArmamentsForTarget(Target t, bool forceAttack, bool onlyEnabled = true) + public IEnumerable ChooseArmamentsForTarget(Target t, bool forceAttack) { // If force-fire is not used, and the target requires force-firing or the target is // terrain or invalid, no armaments can be used - if (!forceAttack && (t.RequiresForceFire || t.Type == TargetType.Terrain || t.Type == TargetType.Invalid)) + if (!forceAttack && (t.Type == TargetType.Terrain || t.Type == TargetType.Invalid || t.RequiresForceFire)) return Enumerable.Empty(); // Get target's owner; in case of terrain or invalid target there will be no problems // with owner == null since forceFire will have to be true in this part of the method // (short-circuiting in the logical expression below) - var owner = null as Player; + Player owner = null; if (t.Type == TargetType.FrozenActor) { owner = t.FrozenActor.Owner; @@ -260,10 +260,11 @@ namespace OpenRA.Mods.Common.Traits owner = t.Actor.Owner; } - return Armaments.Where(a => (!a.IsTraitDisabled || !onlyEnabled) - && a.Weapon.IsValidAgainst(t, self.World, self) + return Armaments.Where(a => + !a.IsTraitDisabled && (owner == null || (forceAttack ? a.Info.ForceTargetStances : a.Info.TargetStances) - .HasStance(self.Owner.Stances[owner]))); + .HasStance(self.Owner.Stances[owner])) + && a.Weapon.IsValidAgainst(t, self.World, self)); } public void AttackTarget(Target target, bool queued, bool allowMove, bool forceAttack = false)