Only apply non-100 damage modifiers in InflictDamage

Profiling has shown that filtering them out early is cheaper
than applying those percentage modifiers anyway.

Additionally, profiling shows foreach loops to be cheaper
than LINQ here as well.
This commit is contained in:
reaperrr
2021-02-20 20:25:35 +01:00
committed by teinarss
parent 2528b79610
commit c240c0a24b

View File

@@ -168,11 +168,24 @@ namespace OpenRA.Mods.Common.Traits
// Apply any damage modifiers
if (!ignoreModifiers && damage.Value > 0)
{
var modifiers = damageModifiers
.Concat(damageModifiersPlayer)
.Select(t => t.GetDamageModifier(attacker, damage));
// PERF: Util.ApplyPercentageModifiers has been manually inlined to
// avoid unnecessary loop enumerations and allocations
var appliedDamage = (decimal)damage.Value;
foreach (var dm in damageModifiers)
{
var modifier = dm.GetDamageModifier(attacker, damage);
if (modifier != 100)
appliedDamage *= modifier / 100m;
}
damage = new Damage(Util.ApplyPercentageModifiers(damage.Value, modifiers), damage.DamageTypes);
foreach (var dm in damageModifiersPlayer)
{
var modifier = dm.GetDamageModifier(attacker, damage);
if (modifier != 100)
appliedDamage *= modifier / 100m;
}
damage = new Damage((int)appliedDamage, damage.DamageTypes);
}
hp = (hp - damage.Value).Clamp(0, MaxHP);