diff --git a/OpenRA.Game/TraitDictionary.cs b/OpenRA.Game/TraitDictionary.cs index a9201fc085..cad0cc5126 100644 --- a/OpenRA.Game/TraitDictionary.cs +++ b/OpenRA.Game/TraitDictionary.cs @@ -255,7 +255,7 @@ namespace OpenRA public void Reset() { index = -1; } public bool MoveNext() { return ++index < actors.Count; } - public TraitPair Current { get { return new TraitPair { Actor = actors[index], Trait = traits[index] }; } } + public TraitPair Current { get { return new TraitPair(actors[index], traits[index]); } } object System.Collections.IEnumerator.Current { get { return Current; } } public void Dispose() { } } diff --git a/OpenRA.Game/World.cs b/OpenRA.Game/World.cs index 1af06e23a5..eab43ae99e 100644 --- a/OpenRA.Game/World.cs +++ b/OpenRA.Game/World.cs @@ -408,14 +408,21 @@ namespace OpenRA } } - public struct TraitPair + public struct TraitPair : IEquatable> { - public Actor Actor; - public T Trait; + public readonly Actor Actor; + public readonly T Trait; - public override string ToString() - { - return "{0}->{1}".F(Actor.Info.Name, Trait.GetType().Name); - } + public TraitPair(Actor actor, T trait) { Actor = actor; Trait = trait; } + + public static bool operator ==(TraitPair me, TraitPair other) { return me.Actor == other.Actor && Equals(me.Trait, other.Trait); } + public static bool operator !=(TraitPair me, TraitPair other) { return !(me == other); } + + public override int GetHashCode() { return Actor.GetHashCode() ^ Trait.GetHashCode(); } + + public bool Equals(TraitPair other) { return this == other; } + public override bool Equals(object obj) { return obj is TraitPair && Equals((TraitPair)obj); } + + public override string ToString() { return "{0}->{1}".F(Actor.Info.Name, Trait.GetType().Name); } } } diff --git a/OpenRA.Mods.Common/Orders/PlaceBuildingOrderGenerator.cs b/OpenRA.Mods.Common/Orders/PlaceBuildingOrderGenerator.cs index e3a8dec8ae..a81b295f7f 100644 --- a/OpenRA.Mods.Common/Orders/PlaceBuildingOrderGenerator.cs +++ b/OpenRA.Mods.Common/Orders/PlaceBuildingOrderGenerator.cs @@ -236,7 +236,7 @@ namespace OpenRA.Mods.Common.Orders var blockers = allTiles.SelectMany(world.ActorMap.GetActorsAt) .Where(a => a.Owner == queue.Actor.Owner && a.IsIdle) - .Select(a => new TraitPair { Actor = a, Trait = a.TraitOrDefault() }); + .Select(a => new TraitPair(a, a.TraitOrDefault())); foreach (var blocker in blockers.Where(x => x.Trait != null)) { diff --git a/OpenRA.Mods.Common/Traits/Player/ProductionQueue.cs b/OpenRA.Mods.Common/Traits/Player/ProductionQueue.cs index b073884f95..a570446ae1 100644 --- a/OpenRA.Mods.Common/Traits/Player/ProductionQueue.cs +++ b/OpenRA.Mods.Common/Traits/Player/ProductionQueue.cs @@ -361,7 +361,7 @@ namespace OpenRA.Mods.Common.Traits public virtual TraitPair MostLikelyProducer() { var trait = self.TraitsImplementing().FirstOrDefault(p => p.Info.Produces.Contains(Info.Type)); - return new TraitPair { Actor = self, Trait = trait }; + return new TraitPair(self, trait); } // Builds a unit from the actor that holds this queue (1 queue per building)