Small refactoring to AutoTarget

- Move a check for AutoTargetIgnore up the chain
 - Simplify some LINQs to a single .FirstOrDefault()
 - Rename local variable
This commit is contained in:
Pavel Penev
2015-10-18 15:05:47 +03:00
parent 1e890a921f
commit f68da6ada7

View File

@@ -157,14 +157,15 @@ namespace OpenRA.Mods.Common.Traits
Actor ChooseTarget(Actor self, WDist range, bool allowMove) Actor ChooseTarget(Actor self, WDist range, bool allowMove)
{ {
var inRange = self.World.FindActorsInCircle(self.CenterPosition, range); var inRange = self.World.FindActorsInCircle(self.CenterPosition, range)
.Where(a => !a.Info.HasTraitInfo<AutoTargetIgnoreInfo>());
// Armaments are enumerated in attack.Armaments in construct order // Armaments are enumerated in attack.Armaments in construct order
// When autotargeting, first choose targets according to the used armament construct order // When autotargeting, first choose targets according to the used armament construct order
// And then according to distance from actor // And then according to distance from actor
// This enables preferential treatment of certain armaments // This enables preferential treatment of certain armaments
// (e.g. tesla trooper's tesla zap should have precedence over tesla charge) // (e.g. tesla trooper's tesla zap should have precedence over tesla charge)
var actrarms = inRange var actorByArmament = inRange
// Select only the first compatible armament for each actor: if this actor is selected // Select only the first compatible armament for each actor: if this actor is selected
// it will be thanks to the first armament anyways, since that is the first selection // it will be thanks to the first armament anyways, since that is the first selection
@@ -174,20 +175,19 @@ namespace OpenRA.Mods.Common.Traits
var target = Target.FromActor(a); var target = Target.FromActor(a);
return new KeyValuePair<Armament, Actor>( return new KeyValuePair<Armament, Actor>(
attack.ChooseArmamentsForTarget(target, false) attack.ChooseArmamentsForTarget(target, false)
.Where(arm => allowMove .FirstOrDefault(arm => allowMove
|| (target.IsInRange(self.CenterPosition, arm.MaxRange()) || (target.IsInRange(self.CenterPosition, arm.MaxRange())
&& !target.IsInRange(self.CenterPosition, arm.Weapon.MinRange))) && !target.IsInRange(self.CenterPosition, arm.Weapon.MinRange))), a);
.FirstOrDefault(), a);
}) })
.Where(kv => kv.Key != null && kv.Value.TraitOrDefault<AutoTargetIgnore>() == null) .Where(kv => kv.Key != null)
.GroupBy(kv => kv.Key, kv => kv.Value) .GroupBy(kv => kv.Key, kv => kv.Value)
.ToDictionary(kv => kv.Key, kv => kv.ClosestTo(self)); .ToDictionary(kv => kv.Key, kv => kv.ClosestTo(self));
foreach (var arm in attack.Armaments) foreach (var arm in attack.Armaments)
{ {
Actor actor; Actor actor;
if (actrarms.TryGetValue(arm, out actor)) if (actorByArmament.TryGetValue(arm, out actor))
return actor; return actor;
} }