misc optimization (#6436)

This commit is contained in:
Leon Friedrich
2022-02-03 18:40:22 +13:00
committed by GitHub
parent 19ceda04b5
commit 0da74b1bfb
7 changed files with 138 additions and 15 deletions

View File

@@ -22,7 +22,7 @@ namespace Content.Shared.Damage
/// functions to apply resistance sets and supports basic math operations to modify this dictionary.
/// </remarks>
[DataDefinition]
public class DamageSpecifier
public class DamageSpecifier : IEquatable<DamageSpecifier>
{
[JsonPropertyName("types")]
[DataField("types", customTypeSerializer: typeof(PrototypeIdDictionarySerializer<FixedPoint2, DamageTypePrototype>))]
@@ -384,7 +384,21 @@ namespace Content.Shared.Damage
return newDamage;
}
public static DamageSpecifier operator -(DamageSpecifier damageSpecA, DamageSpecifier damageSpecB) => damageSpecA + -damageSpecB;
// Here we define the subtraction operator explicitly, rather than implicitly via something like X + (-1 * Y).
// This is faster because FixedPoint2 multiplication is somewhat involved.
public static DamageSpecifier operator -(DamageSpecifier damageSpecA, DamageSpecifier damageSpecB)
{
DamageSpecifier newDamage = new(damageSpecA);
foreach (var entry in damageSpecB.DamageDict)
{
if (!newDamage.DamageDict.TryAdd(entry.Key, -entry.Value))
{
newDamage.DamageDict[entry.Key] -= entry.Value;
}
}
return newDamage;
}
public static DamageSpecifier operator +(DamageSpecifier damageSpec) => damageSpec;
@@ -393,6 +407,20 @@ namespace Content.Shared.Damage
public static DamageSpecifier operator *(float factor, DamageSpecifier damageSpec) => damageSpec * factor;
public static DamageSpecifier operator *(FixedPoint2 factor, DamageSpecifier damageSpec) => damageSpec * factor;
public bool Equals(DamageSpecifier? other)
{
if (other == null || DamageDict.Count != other.DamageDict.Count)
return false;
foreach (var (key, value) in DamageDict)
{
if (!other.DamageDict.TryGetValue(key, out var otherValue) || value != otherValue)
return false;
}
return true;
}
}
#endregion
}