Use a BitSet for representing target types.

- Rename Bits<T> to BitSet<T>.
- Implement set based helpers for BitSet<T>.
- When representing TargetTypes of ITargetable in various traits, use a BitSet<TargetableType> instead of HashSet<string> for better performance & reduced memory usage.
- Fix FieldLoader to trim input values when generating a BitSet<T>.
- Require T in BitSet<T> and BitSetAllocator<T> to be a class since it's just a marker value. This allows the JIT to instantiate generic code for these classes once, as they don't benefit from specialized code for T. (Typically JITs will generate shared code for all reference types, and unique code for every value type encountered).
This commit is contained in:
RoosterDragon
2018-03-18 18:45:16 +00:00
committed by abcdefg30
parent 8fa94c5301
commit 5bd5a384b7
36 changed files with 250 additions and 134 deletions

View File

@@ -41,7 +41,7 @@ SDK ?=
CSC = mcs $(SDK)
CSFLAGS = -nologo -warn:4 -codepage:utf8 -langversion:5 -unsafe -warnaserror
DEFINE = TRACE
COMMON_LIBS = System.dll System.Core.dll System.Data.dll System.Data.DataSetExtensions.dll System.Drawing.dll System.Xml.dll thirdparty/download/ICSharpCode.SharpZipLib.dll thirdparty/download/FuzzyLogicLibrary.dll thirdparty/download/MaxMind.Db.dll thirdparty/download/Eluant.dll thirdparty/download/rix0rrr.BeaconLib.dll
COMMON_LIBS = System.dll System.Core.dll System.Data.dll System.Data.DataSetExtensions.dll System.Drawing.dll System.Numerics.dll System.Xml.dll thirdparty/download/ICSharpCode.SharpZipLib.dll thirdparty/download/FuzzyLogicLibrary.dll thirdparty/download/MaxMind.Db.dll thirdparty/download/Eluant.dll thirdparty/download/rix0rrr.BeaconLib.dll
NUNIT_LIBS_PATH :=
NUNIT_LIBS := $(NUNIT_LIBS_PATH)nunit.framework.dll

View File

@@ -339,21 +339,23 @@ namespace OpenRA
return defaultVisibility.IsVisible(this, player);
}
public IEnumerable<string> GetAllTargetTypes()
public BitSet<TargetableType> GetAllTargetTypes()
{
// PERF: Avoid LINQ.
var targetTypes = new BitSet<TargetableType>();
foreach (var targetable in Targetables)
foreach (var targetType in targetable.TargetTypes)
yield return targetType;
targetTypes = targetTypes.Union(targetable.TargetTypes);
return targetTypes;
}
public IEnumerable<string> GetEnabledTargetTypes()
public BitSet<TargetableType> GetEnabledTargetTypes()
{
// PERF: Avoid LINQ.
var targetTypes = new BitSet<TargetableType>();
foreach (var targetable in Targetables)
if (targetable.IsTraitEnabled())
foreach (var targetType in targetable.TargetTypes)
yield return targetType;
targetTypes = targetTypes.Union(targetable.TargetTypes);
return targetTypes;
}
public bool IsTargetableBy(Actor byActor)

View File

@@ -607,14 +607,13 @@ namespace OpenRA
return InvalidValueAction(value, fieldType, fieldName);
}
else if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(Bits<>))
else if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(BitSet<>))
{
if (value != null)
{
var parts = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
var argTypes = new Type[] { typeof(string[]) };
var argValues = new object[] { parts };
return fieldType.GetConstructor(argTypes).Invoke(argValues);
var ctor = fieldType.GetConstructor(new[] { typeof(string[]) });
return ctor.Invoke(new object[] { parts.Select(p => p.Trim()).ToArray() });
}
return InvalidValueAction(value, fieldType, fieldName);

View File

@@ -18,6 +18,7 @@ using System.Globalization;
using System.Linq;
using System.Reflection;
using OpenRA.Graphics;
using OpenRA.Primitives;
namespace OpenRA
{
@@ -98,6 +99,11 @@ namespace OpenRA
return "{0},{1},{2},{3}".F(r.X, r.Y, r.Width, r.Height);
}
if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(BitSet<>))
{
return ((IEnumerable<string>)v).Select(FormatValue).JoinWith(", ");
}
if (t.IsArray && t.GetArrayRank() == 1)
{
return ((Array)v).Cast<object>().Select(FormatValue).JoinWith(", ");

View File

@@ -162,5 +162,14 @@ namespace OpenRA
public T TraitInfo<T>() where T : ITraitInfoInterface { return traits.Get<T>(); }
public T TraitInfoOrDefault<T>() where T : ITraitInfoInterface { return traits.GetOrDefault<T>(); }
public IEnumerable<T> TraitInfos<T>() where T : ITraitInfoInterface { return traits.WithInterface<T>(); }
public BitSet<TargetableType> GetAllTargetTypes()
{
// PERF: Avoid LINQ.
var targetTypes = new BitSet<TargetableType>();
foreach (var targetable in TraitInfos<ITargetableInfo>())
targetTypes = targetTypes.Union(targetable.GetTargetTypes());
return targetTypes;
}
}
}

View File

@@ -13,6 +13,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Effects;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.GameRules
@@ -64,10 +65,10 @@ namespace OpenRA.GameRules
public readonly int Burst = 1;
[Desc("What types of targets are affected.")]
public readonly HashSet<string> ValidTargets = new HashSet<string> { "Ground", "Water" };
public readonly BitSet<TargetableType> ValidTargets = new BitSet<TargetableType>("Ground", "Water");
[Desc("What types of targets are unaffected.", "Overrules ValidTargets.")]
public readonly HashSet<string> InvalidTargets = new HashSet<string>();
public readonly BitSet<TargetableType> InvalidTargets;
[Desc("Delay in ticks between firing shots from the same ammo magazine. If one entry, it will be used for all bursts.",
"If multiple entries, their number needs to match Burst - 1.")]
@@ -116,7 +117,7 @@ namespace OpenRA.GameRules
return retList;
}
public bool IsValidTarget(IEnumerable<string> targetTypes)
public bool IsValidTarget(BitSet<TargetableType> targetTypes)
{
return ValidTargets.Overlaps(targetTypes) && !InvalidTargets.Overlaps(targetTypes);
}

View File

@@ -15,6 +15,7 @@ using System.IO;
using System.Linq;
using OpenRA.FileSystem;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA
{
@@ -60,7 +61,7 @@ namespace OpenRA
static readonly TerrainTypeInfo Default = new TerrainTypeInfo();
public readonly string Type;
public readonly HashSet<string> TargetTypes = new HashSet<string>();
public readonly BitSet<TargetableType> TargetTypes;
public readonly HashSet<string> AcceptsSmudgeType = new HashSet<string>();
public readonly Color Color;
public readonly bool RestrictPlayerColor = false;

View File

@@ -98,6 +98,7 @@
<HintPath>..\thirdparty\download\MaxMind.Db.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System.Numerics" />
</ItemGroup>
<ItemGroup>
<Compile Include="Activities\Activity.cs" />
@@ -272,7 +273,7 @@
<Compile Include="Graphics\Vertex.cs" />
<Compile Include="FileFormats\PngLoader.cs" />
<Compile Include="Primitives\ActionQueue.cs" />
<Compile Include="Primitives\Bits.cs" />
<Compile Include="Primitives\BitSet.cs" />
<Compile Include="Primitives\Cache.cs" />
<Compile Include="Primitives\DisposableAction.cs" />
<Compile Include="Primitives\float2.cs" />

View File

@@ -0,0 +1,151 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using BitSetIndex = System.Numerics.BigInteger;
namespace OpenRA.Primitives
{
static class BitSetAllocator<T> where T : class
{
static readonly Cache<string, BitSetIndex> Bits = new Cache<string, BitSetIndex>(Allocate);
static BitSetIndex nextBits = 1;
static BitSetIndex Allocate(string value)
{
lock (Bits)
{
var bits = nextBits;
nextBits <<= 1;
return bits;
}
}
public static BitSetIndex GetBits(string[] values)
{
BitSetIndex bits = 0;
lock (Bits)
foreach (var value in values)
bits |= Bits[value];
return bits;
}
public static IEnumerable<string> GetStrings(BitSetIndex bits)
{
var values = new List<string>();
lock (Bits)
foreach (var kvp in Bits)
if ((kvp.Value & bits) != 0)
values.Add(kvp.Key);
return values;
}
public static bool BitsContainString(BitSetIndex bits, string value)
{
BitSetIndex valueBit;
lock (Bits)
if (!Bits.TryGetValue(value, out valueBit))
return false;
return (bits & valueBit) != 0;
}
}
public struct BitSet<T> : IEnumerable<string>, IEquatable<BitSet<T>> where T : class
{
readonly BitSetIndex bits;
public BitSet(params string[] values) : this(BitSetAllocator<T>.GetBits(values)) { }
BitSet(BitSetIndex bits) { this.bits = bits; }
public override string ToString()
{
return BitSetAllocator<T>.GetStrings(bits).JoinWith(",");
}
public static bool operator ==(BitSet<T> me, BitSet<T> other) { return me.bits == other.bits; }
public static bool operator !=(BitSet<T> me, BitSet<T> other) { return !(me == other); }
public bool Equals(BitSet<T> other) { return other == this; }
public override bool Equals(object obj) { return obj is BitSet<T> && Equals((BitSet<T>)obj); }
public override int GetHashCode() { return bits.GetHashCode(); }
public bool IsEmpty { get { return bits == 0; } }
public bool IsProperSubsetOf(BitSet<T> other)
{
return IsSubsetOf(other) && !SetEquals(other);
}
public bool IsProperSupersetOf(BitSet<T> other)
{
return IsSupersetOf(other) && !SetEquals(other);
}
public bool IsSubsetOf(BitSet<T> other)
{
return (bits | other.bits) == other.bits;
}
public bool IsSupersetOf(BitSet<T> other)
{
return (bits | other.bits) == bits;
}
public bool Overlaps(BitSet<T> other)
{
return (bits & other.bits) != 0;
}
public bool SetEquals(BitSet<T> other)
{
return bits == other.bits;
}
public bool Contains(string value)
{
return BitSetAllocator<T>.BitsContainString(bits, value);
}
public IEnumerator<string> GetEnumerator()
{
return BitSetAllocator<T>.GetStrings(bits).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public BitSet<T> Except(BitSet<T> other)
{
return new BitSet<T>(bits & ~other.bits);
}
public BitSet<T> Intersect(BitSet<T> other)
{
return new BitSet<T>(bits & other.bits);
}
public BitSet<T> SymmetricExcept(BitSet<T> other)
{
return new BitSet<T>(bits ^ other.bits);
}
public BitSet<T> Union(BitSet<T> other)
{
return new BitSet<T>(bits | other.bits);
}
}
}

View File

@@ -1,69 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
namespace OpenRA.Primitives
{
static class BitAllocator<T> where T : struct
{
static int nextVal = 1;
static Cache<string, int> bits = new Cache<string, int>(_ => Allocate());
static int Allocate()
{
if (nextVal == 0)
throw new InvalidOperationException(
"Too many values in BitAllocator<{0}>".F(typeof(T).Name));
var val = nextVal;
nextVal <<= 1;
return val;
}
public static int GetValue(string[] val)
{
return val.Select(a => bits[a]).Aggregate(0, (a, b) => a | b);
}
public static IEnumerable<string> GetStrings(int val)
{
for (var i = 0; i < 32; i++)
{
var x = 1 << i;
if ((val & x) != 0)
yield return bits.Single(a => a.Value == x).Key;
}
}
}
public struct Bits<T> : IEquatable<Bits<T>> where T : struct
{
public int Value;
public Bits(string[] val) { Value = BitAllocator<T>.GetValue(val); }
public Bits(Bits<T> other) { Value = other.Value; }
public override string ToString()
{
return BitAllocator<T>.GetStrings(Value).JoinWith(",");
}
public static bool operator ==(Bits<T> me, Bits<T> other) { return me.Value == other.Value; }
public static bool operator !=(Bits<T> me, Bits<T> other) { return !(me == other); }
public bool Equals(Bits<T> other) { return other == this; }
public override bool Equals(object obj) { return obj is Bits<T> && Equals((Bits<T>)obj); }
public override int GetHashCode() { return Value.GetHashCode(); }
}
}

View File

@@ -36,7 +36,7 @@ namespace OpenRA.Traits
readonly Shroud shroud;
public Player Owner { get; private set; }
public HashSet<string> TargetTypes { get; private set; }
public BitSet<TargetableType> TargetTypes { get; private set; }
public ITooltipInfo TooltipInfo { get; private set; }
public Player TooltipOwner { get; private set; }
@@ -84,7 +84,6 @@ namespace OpenRA.Traits
footprint.Select(p => shroud.Contains(p).ToString()).JoinWith("|")));
CenterPosition = actor.CenterPosition;
TargetTypes = new HashSet<string>();
tooltips = actor.TraitsImplementing<ITooltip>().ToArray();
health = actor.TraitOrDefault<IHealth>();
@@ -101,10 +100,7 @@ namespace OpenRA.Traits
public void RefreshState()
{
Owner = actor.Owner;
// PERF: Reuse collection to avoid allocations.
TargetTypes.Clear();
TargetTypes.UnionWith(actor.GetEnabledTargetTypes());
TargetTypes = actor.GetEnabledTargetTypes();
if (health != null)
{

View File

@@ -365,15 +365,20 @@ namespace OpenRA.Traits
bool SpatiallyPartitionable { get; }
}
/// <summary>
/// Indicates target types as defined on <see cref="Traits.ITargetable"/> are present in a <see cref="Primitives.BitSet{T}"/>.
/// </summary>
public sealed class TargetableType { TargetableType() { } }
public interface ITargetableInfo : ITraitInfoInterface
{
HashSet<string> GetTargetTypes();
BitSet<TargetableType> GetTargetTypes();
}
public interface ITargetable
{
// Check IsTraitEnabled or !IsTraitDisabled first
HashSet<string> TargetTypes { get; }
BitSet<TargetableType> TargetTypes { get; }
bool TargetableBy(Actor self, Actor byActor);
bool RequiresForceFire { get; }
}

View File

@@ -16,6 +16,7 @@ using System.Linq;
using OpenRA.Mods.Common.Orders;
using OpenRA.Mods.Common.Traits;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Cnc.Traits
@@ -84,7 +85,7 @@ namespace OpenRA.Mods.Cnc.Traits
public readonly Stance ValidStances = Stance.Ally | Stance.Neutral | Stance.Enemy;
[Desc("Target types of actors that this actor disguise as.")]
public readonly HashSet<string> TargetTypes = new HashSet<string> { "Disguise" };
public readonly BitSet<TargetableType> TargetTypes = new BitSet<TargetableType>("Disguise");
[Desc("Triggers which cause the actor to drop it's disguise. Possible values: None, Attack, Damaged,",
"Unload, Infiltrate, Demolish, Move.")]
@@ -317,7 +318,7 @@ namespace OpenRA.Mods.Cnc.Traits
if (!info.ValidStances.HasStance(stance))
return false;
return info.TargetTypes.Overlaps(target.Info.TraitInfos<ITargetableInfo>().SelectMany(ti => ti.GetTargetTypes()));
return info.TargetTypes.Overlaps(target.Info.GetAllTargetTypes());
}
}
}

View File

@@ -13,6 +13,7 @@ using System;
using System.Collections.Generic;
using OpenRA.Mods.Common.Effects;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Cnc.Traits
@@ -20,7 +21,7 @@ namespace OpenRA.Mods.Cnc.Traits
[Desc("This structure can be infiltrated causing funds to be stolen.")]
class InfiltrateForCashInfo : ITraitInfo
{
public readonly HashSet<string> Types = new HashSet<string>();
public readonly BitSet<TargetableType> Types;
[Desc("Percentage of the victim's resources that will be stolen.")]
public readonly int Percentage = 100;
@@ -47,7 +48,7 @@ namespace OpenRA.Mods.Cnc.Traits
public InfiltrateForCash(InfiltrateForCashInfo info) { this.info = info; }
void INotifyInfiltrated.Infiltrated(Actor self, Actor infiltrator, HashSet<string> types)
void INotifyInfiltrated.Infiltrated(Actor self, Actor infiltrator, BitSet<TargetableType> types)
{
if (!info.Types.Overlaps(types))
return;

View File

@@ -13,6 +13,7 @@ using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Traits;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Cnc.Traits
@@ -20,7 +21,7 @@ namespace OpenRA.Mods.Cnc.Traits
[Desc("Reveals a decoration sprite to the indicated players when infiltrated.")]
class InfiltrateForDecorationInfo : WithDecorationInfo
{
public readonly HashSet<string> Types = new HashSet<string>();
public readonly BitSet<TargetableType> Types;
public override object Create(ActorInitializer init) { return new InfiltrateForDecoration(init.Self, this); }
}
@@ -36,7 +37,7 @@ namespace OpenRA.Mods.Cnc.Traits
this.info = info;
}
void INotifyInfiltrated.Infiltrated(Actor self, Actor infiltrator, HashSet<string> types)
void INotifyInfiltrated.Infiltrated(Actor self, Actor infiltrator, BitSet<TargetableType> types)
{
if (!info.Types.Overlaps(types))
return;

View File

@@ -12,6 +12,7 @@
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Cnc.Traits
@@ -19,7 +20,7 @@ namespace OpenRA.Mods.Cnc.Traits
[Desc("Steal and reset the owner's exploration.")]
class InfiltrateForExplorationInfo : ITraitInfo
{
public readonly HashSet<string> Types = new HashSet<string>();
public readonly BitSet<TargetableType> Types;
public object Create(ActorInitializer init) { return new InfiltrateForExploration(init.Self, this); }
}
@@ -33,7 +34,7 @@ namespace OpenRA.Mods.Cnc.Traits
this.info = info;
}
void INotifyInfiltrated.Infiltrated(Actor self, Actor infiltrator, HashSet<string> types)
void INotifyInfiltrated.Infiltrated(Actor self, Actor infiltrator, BitSet<TargetableType> types)
{
if (!info.Types.Overlaps(types))
return;

View File

@@ -11,13 +11,14 @@
using System.Collections.Generic;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Cnc.Traits
{
class InfiltrateForPowerOutageInfo : ITraitInfo
{
public readonly HashSet<string> Types = new HashSet<string>();
public readonly BitSet<TargetableType> Types;
public readonly int Duration = 25 * 20;
@@ -35,7 +36,7 @@ namespace OpenRA.Mods.Cnc.Traits
playerPower = self.Owner.PlayerActor.Trait<PowerManager>();
}
void INotifyInfiltrated.Infiltrated(Actor self, Actor infiltrator, HashSet<string> types)
void INotifyInfiltrated.Infiltrated(Actor self, Actor infiltrator, BitSet<TargetableType> types)
{
if (!info.Types.Overlaps(types))
return;

View File

@@ -20,7 +20,7 @@ namespace OpenRA.Mods.Cnc.Traits
{
[ActorReference, FieldLoader.Require] public readonly string Proxy = null;
public readonly HashSet<string> Types = new HashSet<string>();
public readonly BitSet<TargetableType> Types;
public object Create(ActorInitializer init) { return new InfiltrateForSupportPower(this); }
}
@@ -34,7 +34,7 @@ namespace OpenRA.Mods.Cnc.Traits
this.info = info;
}
void INotifyInfiltrated.Infiltrated(Actor self, Actor infiltrator, HashSet<string> types)
void INotifyInfiltrated.Infiltrated(Actor self, Actor infiltrator, BitSet<TargetableType> types)
{
if (!info.Types.Overlaps(types))
return;

View File

@@ -17,13 +17,14 @@ using OpenRA.Mods.Common;
using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Orders;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Cnc.Traits
{
public class InfiltratesInfo : ConditionalTraitInfo
{
public readonly HashSet<string> Types = new HashSet<string>();
public readonly BitSet<TargetableType> Types;
[VoiceReference] public readonly string Voice = "Action";
@@ -72,14 +73,14 @@ namespace OpenRA.Mods.Cnc.Traits
if (IsTraitDisabled)
return false;
IEnumerable<string> targetTypes = null;
var targetTypes = new BitSet<TargetableType>();
if (order.Target.Type == TargetType.FrozenActor)
targetTypes = order.Target.FrozenActor.TargetTypes;
if (order.Target.Type == TargetType.Actor)
targetTypes = order.TargetActor.GetEnabledTargetTypes();
return targetTypes != null && Info.Types.Overlaps(targetTypes);
return Info.Types.Overlaps(targetTypes);
}
public string VoicePhraseForOrder(Actor self, Order order)
@@ -133,7 +134,7 @@ namespace OpenRA.Mods.Cnc.Traits
if (!info.ValidStances.HasStance(stance))
return false;
return info.Types.Overlaps(target.Info.TraitInfos<ITargetableInfo>().SelectMany(ti => ti.GetTargetTypes()));
return info.Types.Overlaps(target.Info.GetAllTargetTypes());
}
}
}

View File

@@ -114,7 +114,7 @@ namespace OpenRA.Mods.Cnc.Traits
{
get
{
yield return new TargetTypeOrderTargeter(new HashSet<string> { "DetonateAttack" }, "DetonateAttack", 5, "attack", true, false) { ForceAttack = false };
yield return new TargetTypeOrderTargeter(new BitSet<TargetableType>("DetonateAttack"), "DetonateAttack", 5, "attack", true, false) { ForceAttack = false };
yield return new DeployOrderTargeter("Detonate", 5);
}
}

View File

@@ -13,13 +13,14 @@ using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.AI
{
abstract class AirStateBase : StateBase
{
static readonly string[] AirTargetTypes = new[] { "Air" };
static readonly BitSet<TargetableType> AirTargetTypes = new BitSet<TargetableType>("Air");
protected const int MissileUnitMultiplier = 3;

View File

@@ -105,7 +105,7 @@ namespace OpenRA.Mods.Common.AI
else
{
var enemies = owner.World.FindActorsInCircle(leader.CenterPosition, WDist.FromCells(owner.Bot.Info.AttackScanRadius))
.Where(a => !a.IsDead && leader.Owner.Stances[a.Owner] == Stance.Enemy && a.GetEnabledTargetTypes().Any());
.Where(a => !a.IsDead && leader.Owner.Stances[a.Owner] == Stance.Enemy && !a.GetEnabledTargetTypes().IsEmpty);
var target = enemies.ClosestTo(leader.CenterPosition);
if (target != null)
{

View File

@@ -132,7 +132,7 @@ namespace OpenRA.Mods.Common.AI
else
{
var enemies = owner.World.FindActorsInCircle(leader.CenterPosition, WDist.FromCells(owner.Bot.Info.AttackScanRadius))
.Where(a => !a.IsDead && leader.Owner.Stances[a.Owner] == Stance.Enemy && a.GetEnabledTargetTypes().Any());
.Where(a => !a.IsDead && leader.Owner.Stances[a.Owner] == Stance.Enemy && !a.GetEnabledTargetTypes().IsEmpty);
var target = enemies.ClosestTo(leader.CenterPosition);
if (target != null)
{

View File

@@ -65,7 +65,7 @@ namespace OpenRA.Mods.Common.AI
return false;
var targetTypes = target.GetEnabledTargetTypes();
if (!targetTypes.Any())
if (targetTypes.IsEmpty)
return false;
var arms = a.TraitsImplementing<Armament>();

View File

@@ -11,6 +11,7 @@
using System.Collections.Generic;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.AI
@@ -122,7 +123,7 @@ namespace OpenRA.Mods.Common.AI
public readonly Stance Against = Stance.Enemy;
[Desc("What types should the desired targets of this power be?")]
public readonly HashSet<string> Types = new HashSet<string> { "Air", "Ground", "Water" };
public readonly BitSet<TargetableType> Types = new BitSet<TargetableType>("Air", "Ground", "Water");
[Desc("How attractive are these types of targets?")]
public readonly int Attractiveness = 100;

View File

@@ -35,8 +35,8 @@ namespace OpenRA.Mods.Common.Lint
if (!deathTypes.Any() || spawnActorOnAnyDeathType)
continue;
var targetable = actorInfo.Value.TraitInfos<ITargetableInfo>().SelectMany(x => x.GetTargetTypes()).ToList();
if (!targetable.Any())
var targetable = actorInfo.Value.GetAllTargetTypes();
if (targetable.IsEmpty)
continue;
foreach (var weaponInfo in rules.Weapons)

View File

@@ -10,6 +10,7 @@
#endregion
using System.Collections.Generic;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Orders
@@ -67,9 +68,9 @@ namespace OpenRA.Mods.Common.Orders
public class TargetTypeOrderTargeter : UnitOrderTargeter
{
readonly HashSet<string> targetTypes;
readonly BitSet<TargetableType> targetTypes;
public TargetTypeOrderTargeter(HashSet<string> targetTypes, string order, int priority, string cursor, bool targetEnemyUnits, bool targetAllyUnits)
public TargetTypeOrderTargeter(BitSet<TargetableType> targetTypes, string order, int priority, string cursor, bool targetEnemyUnits, bool targetAllyUnits)
: base(order, priority, cursor, targetEnemyUnits, targetAllyUnits)
{
this.targetTypes = targetTypes;

View File

@@ -13,6 +13,7 @@ using System;
using System.Collections.Generic;
using Eluant;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Scripting;
using OpenRA.Traits;
@@ -302,7 +303,7 @@ namespace OpenRA.Mods.Common.Scripting
OnCapturedInternal(self);
}
void INotifyInfiltrated.Infiltrated(Actor self, Actor infiltrator, HashSet<string> types)
void INotifyInfiltrated.Infiltrated(Actor self, Actor infiltrator, BitSet<TargetableType> types)
{
if (world.Disposing)
return;

View File

@@ -13,6 +13,7 @@ using System.Collections.Generic;
using System.Drawing;
using OpenRA.Activities;
using OpenRA.Mods.Common.Orders;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
@@ -45,7 +46,7 @@ namespace OpenRA.Mods.Common.Traits
if (IsTraitDisabled)
yield break;
yield return new TargetTypeOrderTargeter(new HashSet<string> { "DetonateAttack" }, "DetonateAttack", 5, "attack", true, false) { ForceAttack = false };
yield return new TargetTypeOrderTargeter(new BitSet<TargetableType>("DetonateAttack"), "DetonateAttack", 5, "attack", true, false) { ForceAttack = false };
yield return new DeployOrderTargeter("Detonate", 5);
}
}

View File

@@ -284,7 +284,7 @@ namespace OpenRA.Mods.Common.Traits
continue;
// Check whether we can auto-target this actor
var targetTypes = actor.GetEnabledTargetTypes().ToArray();
var targetTypes = actor.GetEnabledTargetTypes();
var validPriorities = activePriorities.Where(ati =>
{
// Already have a higher priority target

View File

@@ -9,7 +9,7 @@
*/
#endregion
using System.Collections.Generic;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
@@ -18,10 +18,10 @@ namespace OpenRA.Mods.Common.Traits
public class AutoTargetPriorityInfo : ConditionalTraitInfo, Requires<AutoTargetInfo>
{
[Desc("Target types that can be AutoTargeted.")]
public readonly HashSet<string> ValidTargets = new HashSet<string> { "Ground", "Water", "Air" };
public readonly BitSet<TargetableType> ValidTargets = new BitSet<TargetableType>("Ground", "Water", "Air");
[Desc("Target types that can't be AutoTargeted.", "Overrules ValidTargets.")]
public readonly HashSet<string> InvalidTargets = new HashSet<string>();
public readonly BitSet<TargetableType> InvalidTargets;
[Desc("ValidTargets with larger priorities will be AutoTargeted before lower priorities.")]
public readonly int Priority = 1;

View File

@@ -33,7 +33,7 @@ namespace OpenRA.Mods.Common.Traits
public readonly int MaxRadius = 4;
[Desc("The list of unit target types we are allowed to duplicate.")]
public readonly HashSet<string> ValidTargets = new HashSet<string> { "Ground", "Water" };
public readonly BitSet<TargetableType> ValidTargets = new BitSet<TargetableType>("Ground", "Water");
[Desc("Which factions this crate action can occur for.")]
public readonly HashSet<string> ValidFactions = new HashSet<string>();

View File

@@ -11,6 +11,7 @@
using System.Collections.Generic;
using System.Linq;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
@@ -19,8 +20,8 @@ namespace OpenRA.Mods.Common.Traits
public class TargetableInfo : ConditionalTraitInfo, ITargetableInfo
{
[Desc("Target type. Used for filtering (in)valid targets.")]
public readonly HashSet<string> TargetTypes = new HashSet<string>();
public HashSet<string> GetTargetTypes() { return TargetTypes; }
public readonly BitSet<TargetableType> TargetTypes;
public BitSet<TargetableType> GetTargetTypes() { return TargetTypes; }
public bool RequiresForceFire = false;
@@ -53,7 +54,7 @@ namespace OpenRA.Mods.Common.Traits
return cloaks.All(c => c.IsTraitDisabled || c.IsVisible(self, viewer.Owner));
}
public virtual HashSet<string> TargetTypes { get { return Info.TargetTypes; } }
public virtual BitSet<TargetableType> TargetTypes { get { return Info.TargetTypes; } }
public bool RequiresForceFire { get { return Info.RequiresForceFire; } }
}

View File

@@ -118,7 +118,7 @@ namespace OpenRA.Mods.Common.Traits
public interface ISeedableResource { void Seed(Actor self); }
[RequireExplicitImplementation]
public interface INotifyInfiltrated { void Infiltrated(Actor self, Actor infiltrator, HashSet<string> types); }
public interface INotifyInfiltrated { void Infiltrated(Actor self, Actor infiltrator, BitSet<TargetableType> types); }
[RequireExplicitImplementation]
public interface INotifyBlockingMove { void OnNotifyBlockingMove(Actor self, Actor blocking); }

View File

@@ -14,6 +14,7 @@ using System.Linq;
using OpenRA.GameRules;
using OpenRA.Mods.Common.Effects;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Warheads
@@ -48,7 +49,7 @@ namespace OpenRA.Mods.Common.Warheads
[Desc("Whether to consider actors in determining whether the explosion should happen. If false, only terrain will be considered.")]
public readonly bool ImpactActors = true;
static readonly string[] TargetTypeAir = new string[] { "Air" };
static readonly BitSet<TargetableType> TargetTypeAir = new BitSet<TargetableType>("Air");
public ImpactType GetImpactType(World world, CPos cell, WPos pos, Actor firedBy)
{

View File

@@ -12,6 +12,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Warheads
@@ -35,10 +36,10 @@ namespace OpenRA.Mods.Common.Warheads
public abstract class Warhead : IWarhead
{
[Desc("What types of targets are affected.")]
public readonly HashSet<string> ValidTargets = new HashSet<string> { "Ground", "Water" };
public readonly BitSet<TargetableType> ValidTargets = new BitSet<TargetableType>("Ground", "Water");
[Desc("What types of targets are unaffected.", "Overrules ValidTargets.")]
public readonly HashSet<string> InvalidTargets = new HashSet<string>();
public readonly BitSet<TargetableType> InvalidTargets;
[Desc("What diplomatic stances are affected.")]
public readonly Stance ValidStances = Stance.Ally | Stance.Neutral | Stance.Enemy;
@@ -54,7 +55,7 @@ namespace OpenRA.Mods.Common.Warheads
[Desc("The color used for this warhead's visualization in the world's `WarheadDebugOverlay` trait.")]
public readonly Color DebugOverlayColor = Color.Red;
public bool IsValidTarget(IEnumerable<string> targetTypes)
public bool IsValidTarget(BitSet<TargetableType> targetTypes)
{
return ValidTargets.Overlaps(targetTypes) && !InvalidTargets.Overlaps(targetTypes);
}