using System; using System.Collections.Generic; using System.Linq; using OpenRa.GameRules; using OpenRa.Traits; namespace OpenRa { public static class Exts { public static bool HasModifier(this Modifiers k, Modifiers mod) { return (k & mod) == mod; } public static IEnumerable SymmetricDifference(this IEnumerable xs, IEnumerable ys) { // this is probably a shockingly-slow way to do this, but it's concise. return xs.Except(ys).Concat(ys.Except(xs)); } public static float Product(this IEnumerable xs) { return xs.Aggregate(1f, (a, x) => a * x); } public static WeaponInfo GetPrimaryWeapon(this Actor self) { var info = self.Info.Traits.GetOrDefault(); if (info == null) return null; var weapon = info.PrimaryWeapon; if (weapon == null) return null; return Rules.WeaponInfo[weapon]; } public static WeaponInfo GetSecondaryWeapon(this Actor self) { var info = self.Info.Traits.GetOrDefault(); if (info == null) return null; var weapon = info.SecondaryWeapon; if (weapon == null) return null; return Rules.WeaponInfo[weapon]; } public static int GetMaxHP(this Actor self) { var oai = self.Info.Traits.GetOrDefault(); if (oai == null) return 0; return oai.HP; } public static V GetOrAdd( this Dictionary d, K k ) where V : new() { return d.GetOrAdd( k, _ => new V() ); } public static V GetOrAdd( this Dictionary d, K k, Func createFn ) { V ret; if( !d.TryGetValue( k, out ret ) ) d.Add( k, ret = createFn( k ) ); return ret; } } }