/// <summary> /// [Extension method] Compares this instance with a specified <seealso cref="DamageKeywordDef"/> /// object and indicates whether this instance precedes, follows, or appears in the same position /// in the sort order as the specified <seealso cref="DamageKeywordDef"/>. /// </summary> /// <param name="dkdB">The <seealso cref="DamageKeywordDef"/> to compare to this instance</param> /// <returns>-1 if A comes first, 1 if B comes first, 0 if they sort in the same place.</returns> public static int CompareTo(this DamageKeywordDef dkdA, DamageKeywordDef dkdB) { // Setup string nameA = dkdA.name; string nameB = dkdB.name; int posA = Array.FindIndex(damageKeywordDefSortSpecial, x => x == nameA); int posB = Array.FindIndex(damageKeywordDefSortSpecial, x => x == nameB); if (posA != -1 && posB != -1) { // If both ar in the list, do a comparison on their index return(posA - posB); } else if (posA != -1) { // If A is in the list but not B, A should be first return(-1); } else if (posB != -1) { // If B is in the list bit not A, B should be first return(1); } else { // Otherwise fallback on the general comparison return(nameA.CompareTo(nameB)); } }
private static float SyncDamage(this WeaponDef weapon, DamageKeywordDef damageType, Func <float, float> handler) { lock ( weapon ) { if (weapon == null) { throw new ArgumentNullException(nameof(weapon)); } if (damageType == null) { throw new ArgumentNullException(nameof(damageType)); } var list = weapon.DamagePayload.DamageKeywords; var pair = list.FirstOrDefault(e => e.DamageKeywordDef == damageType); var orig = pair?.Value ?? 0; if (handler == null) { return(orig); } var value = handler(orig); if (value == orig) { return(orig); } object[] logArg = new object[] { weapon.name, damageType.name, orig, value }; if (value > 0) { if (pair == null) { ZyMod.ApiLog(TraceEventType.Start, "Adding {1} {3} to {0}", logArg); list.Add(pair = new DamageKeywordPair { DamageKeywordDef = damageType, Value = value }); } else { ZyMod.ApiLog(TraceEventType.Start, "Update {0} {1} {2} to {3}", logArg); pair.Value = value; } } else if (pair != null) { ZyMod.ApiLog(TraceEventType.Start, "Removing {1} {2} from {0}", logArg); list.Remove(pair); } return(orig); } }
public static float CompareExchangeDamage(this WeaponDef weapon, DamageKeywordDef type, float ifMatch, float setTo) => SyncDamage(weapon, type, (orig) => orig == ifMatch ? setTo : orig);
public static float ExchangeDamage(this WeaponDef weapon, DamageKeywordDef type, float value) => SyncDamage(weapon, type, (_) => value);
public static float AddDamage(this WeaponDef weapon, DamageKeywordDef type, float value) => SyncDamage(weapon, type, (orig) => orig + value);
public static WeaponDef SetDamage(this WeaponDef weapon, DamageKeywordDef type, float value) { ExchangeDamage(weapon, type, value); return(weapon); }
public static float GetDamage(this WeaponDef weapon, DamageKeywordDef type) => SyncDamage(weapon, type, null);