Avoid delegate allocation in AutoTarget.
Extract a common method for deciding if an attack should happen, and avoid LINQ inside this method.
This commit is contained in:
@@ -119,8 +119,9 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
Aggressor = attacker;
|
Aggressor = attacker;
|
||||||
var allowMove = Info.AllowMovement && Stance != UnitStance.Defend;
|
|
||||||
if (attackFollows.All(a => a.IsTraitDisabled || !a.IsReachableTarget(a.Target, allowMove)))
|
bool allowMove;
|
||||||
|
if (ShouldAttack(out allowMove))
|
||||||
Attack(self, Aggressor, allowMove);
|
Attack(self, Aggressor, allowMove);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,11 +133,23 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
if (Stance < UnitStance.Defend || !Info.TargetWhenIdle)
|
if (Stance < UnitStance.Defend || !Info.TargetWhenIdle)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var allowMove = Info.AllowMovement && Stance != UnitStance.Defend;
|
bool allowMove;
|
||||||
if (attackFollows.All(a => a.IsTraitDisabled || !a.IsReachableTarget(a.Target, allowMove)))
|
if (ShouldAttack(out allowMove))
|
||||||
ScanAndAttack(self, allowMove);
|
ScanAndAttack(self, allowMove);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ShouldAttack(out bool allowMove)
|
||||||
|
{
|
||||||
|
allowMove = Info.AllowMovement && Stance != UnitStance.Defend;
|
||||||
|
|
||||||
|
// PERF: Avoid LINQ.
|
||||||
|
foreach (var attackFollow in attackFollows)
|
||||||
|
if (!attackFollow.IsTraitDisabled && attackFollow.IsReachableTarget(attackFollow.Target, allowMove))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public void Tick(Actor self)
|
public void Tick(Actor self)
|
||||||
{
|
{
|
||||||
if (IsTraitDisabled)
|
if (IsTraitDisabled)
|
||||||
|
|||||||
Reference in New Issue
Block a user