Improve performance of target validation.
The Warhead and WeaponInfo classes now maintain preconstructed sets of valid and invalid targets to speed up validation checks since LINQ Intersect no longer has to be called (which recreates the sets internally each time). Fixed minor bugs in the AI where it was repeating the validation logic but failing to account for invalid targets.
This commit is contained in:
@@ -33,6 +33,18 @@ namespace OpenRA.GameRules
|
||||
[Desc("Delay in ticks before applying the warhead effect.", "0 = instant (old model).")]
|
||||
public readonly int Delay = 0;
|
||||
|
||||
HashSet<string> validTargetSet;
|
||||
HashSet<string> invalidTargetSet;
|
||||
|
||||
public bool IsValidTarget(IEnumerable<string> targetTypes)
|
||||
{
|
||||
if (validTargetSet == null)
|
||||
validTargetSet = new HashSet<string>(ValidTargets);
|
||||
if (invalidTargetSet == null)
|
||||
invalidTargetSet = new HashSet<string>(InvalidTargets);
|
||||
return validTargetSet.Overlaps(targetTypes) && !invalidTargetSet.Overlaps(targetTypes);
|
||||
}
|
||||
|
||||
/// <summary>Applies the warhead's effect against the target.</summary>
|
||||
public abstract void DoImpact(Target target, Actor firedBy, IEnumerable<int> damageModifiers);
|
||||
|
||||
@@ -52,8 +64,7 @@ namespace OpenRA.GameRules
|
||||
return false;
|
||||
|
||||
var cellInfo = world.Map.GetTerrainInfo(cell);
|
||||
if (!ValidTargets.Intersect(cellInfo.TargetTypes).Any()
|
||||
|| InvalidTargets.Intersect(cellInfo.TargetTypes).Any())
|
||||
if (!IsValidTarget(cellInfo.TargetTypes))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
@@ -81,8 +92,7 @@ namespace OpenRA.GameRules
|
||||
|
||||
// A target type is valid if it is in the valid targets list, and not in the invalid targets list.
|
||||
var targetable = victim.TraitOrDefault<ITargetable>();
|
||||
if (targetable == null || !ValidTargets.Intersect(targetable.TargetTypes).Any()
|
||||
|| InvalidTargets.Intersect(targetable.TargetTypes).Any())
|
||||
if (targetable == null || !IsValidTarget(targetable.TargetTypes))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
@@ -101,8 +111,7 @@ namespace OpenRA.GameRules
|
||||
|
||||
// A target type is valid if it is in the valid targets list, and not in the invalid targets list.
|
||||
var targetable = victim.Info.Traits.GetOrDefault<ITargetableInfo>();
|
||||
if (targetable == null || !ValidTargets.Intersect(targetable.GetTargetTypes()).Any()
|
||||
|| InvalidTargets.Intersect(targetable.GetTargetTypes()).Any())
|
||||
if (targetable == null || !IsValidTarget(targetable.GetTargetTypes()))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace OpenRA.GameRules
|
||||
|
||||
public interface IProjectileInfo { IEffect Create(ProjectileArgs args); }
|
||||
|
||||
public class WeaponInfo
|
||||
public sealed class WeaponInfo
|
||||
{
|
||||
[Desc("The maximum range the weapon can fire.")]
|
||||
public readonly WRange Range = WRange.Zero;
|
||||
@@ -64,9 +64,14 @@ namespace OpenRA.GameRules
|
||||
[FieldLoader.LoadUsing("LoadWarheads")]
|
||||
public readonly List<Warhead> Warheads = new List<Warhead>();
|
||||
|
||||
readonly HashSet<string> validTargetSet;
|
||||
readonly HashSet<string> invalidTargetSet;
|
||||
|
||||
public WeaponInfo(string name, MiniYaml content)
|
||||
{
|
||||
FieldLoader.Load(this, content);
|
||||
validTargetSet = new HashSet<string>(ValidTargets);
|
||||
invalidTargetSet = new HashSet<string>(InvalidTargets);
|
||||
}
|
||||
|
||||
static object LoadProjectile(MiniYaml yaml)
|
||||
@@ -92,6 +97,11 @@ namespace OpenRA.GameRules
|
||||
return retList;
|
||||
}
|
||||
|
||||
public bool IsValidTarget(IEnumerable<string> targetTypes)
|
||||
{
|
||||
return validTargetSet.Overlaps(targetTypes) && !invalidTargetSet.Overlaps(targetTypes);
|
||||
}
|
||||
|
||||
/// <summary>Checks if the weapon is valid against (can target) the target.</summary>
|
||||
public bool IsValidAgainst(Target target, World world, Actor firedBy)
|
||||
{
|
||||
@@ -108,8 +118,7 @@ namespace OpenRA.GameRules
|
||||
return false;
|
||||
|
||||
var cellInfo = world.Map.GetTerrainInfo(cell);
|
||||
if (!ValidTargets.Intersect(cellInfo.TargetTypes).Any()
|
||||
|| InvalidTargets.Intersect(cellInfo.TargetTypes).Any())
|
||||
if (!IsValidTarget(cellInfo.TargetTypes))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
@@ -122,8 +131,7 @@ namespace OpenRA.GameRules
|
||||
public bool IsValidAgainst(Actor victim, Actor firedBy)
|
||||
{
|
||||
var targetable = victim.TraitOrDefault<ITargetable>();
|
||||
if (targetable == null || !ValidTargets.Intersect(targetable.TargetTypes).Any()
|
||||
|| InvalidTargets.Intersect(targetable.TargetTypes).Any())
|
||||
if (targetable == null || !IsValidTarget(targetable.TargetTypes))
|
||||
return false;
|
||||
|
||||
if (!Warheads.Any(w => w.IsValidAgainst(victim, firedBy)))
|
||||
@@ -136,8 +144,7 @@ namespace OpenRA.GameRules
|
||||
public bool IsValidAgainst(FrozenActor victim, Actor firedBy)
|
||||
{
|
||||
var targetable = victim.Info.Traits.GetOrDefault<ITargetableInfo>();
|
||||
if (targetable == null || !ValidTargets.Intersect(targetable.GetTargetTypes()).Any()
|
||||
|| InvalidTargets.Intersect(targetable.GetTargetTypes()).Any())
|
||||
if (targetable == null || !IsValidTarget(targetable.GetTargetTypes()))
|
||||
return false;
|
||||
|
||||
if (!Warheads.Any(w => w.IsValidAgainst(victim, firedBy)))
|
||||
|
||||
@@ -18,6 +18,8 @@ namespace OpenRA.Mods.Common.AI
|
||||
{
|
||||
abstract class AirStateBase : StateBase
|
||||
{
|
||||
static readonly string[] AirTargetTypes = new[] { "Air" };
|
||||
|
||||
protected const int MissileUnitMultiplier = 3;
|
||||
|
||||
protected static int CountAntiAirUnits(IEnumerable<Actor> units)
|
||||
@@ -34,7 +36,7 @@ namespace OpenRA.Mods.Common.AI
|
||||
var arms = unit.TraitsImplementing<Armament>();
|
||||
foreach (var a in arms)
|
||||
{
|
||||
if (a.Weapon.ValidTargets.Contains("Air"))
|
||||
if (a.Weapon.IsValidTarget(AirTargetTypes))
|
||||
{
|
||||
missileUnitsCount++;
|
||||
break;
|
||||
|
||||
@@ -69,7 +69,7 @@ namespace OpenRA.Mods.Common.AI
|
||||
|
||||
var arms = a.TraitsImplementing<Armament>();
|
||||
foreach (var arm in arms)
|
||||
if (arm.Weapon.ValidTargets.Intersect(targetable.TargetTypes).Any())
|
||||
if (arm.Weapon.IsValidTarget(targetable.TargetTypes))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user