Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Health.InflictDamage performance #19173

Merged
merged 2 commits into from
Mar 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions OpenRA.Mods.Common/Traits/Health.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public DamageState DamageState
{
get
{
if (hp == MaxHP)
return DamageState.Undamaged;

if (hp <= 0)
return DamageState.Dead;

Expand All @@ -100,9 +103,6 @@ public DamageState DamageState
if (hp * 100L < MaxHP * 75L)
return DamageState.Medium;

if (hp == MaxHP)
return DamageState.Undamaged;

return DamageState.Light;
}
}
Expand Down Expand Up @@ -168,11 +168,24 @@ public void InflictDamage(Actor self, Actor attacker, Damage damage, bool ignore
// 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)
reaperrr marked this conversation as resolved.
Show resolved Hide resolved
{
var modifier = dm.GetDamageModifier(attacker, damage);
if (modifier != 100)
appliedDamage *= modifier / 100m;
}

foreach (var dm in damageModifiersPlayer)
{
var modifier = dm.GetDamageModifier(attacker, damage);
if (modifier != 100)
appliedDamage *= modifier / 100m;
}

damage = new Damage(Util.ApplyPercentageModifiers(damage.Value, modifiers), damage.DamageTypes);
damage = new Damage((int)appliedDamage, damage.DamageTypes);
}

hp = (hp - damage.Value).Clamp(0, MaxHP);
Expand Down