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.
This commit is contained in:
RoosterDragon
2015-10-17 00:55:52 +01:00
parent 0fab3ec1b2
commit 16386873af

View File

@@ -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<Armament> ChooseArmamentsForTarget(Target t, bool forceAttack, bool onlyEnabled = true)
public IEnumerable<Armament> 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<Armament>();
// 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)