Move logic from EvenHealthChangeEntityEffectSystem to the damage system API (#41684)

* add two methods

* move stuff to damage system api

* use TryIndex

* simplify

* minor fix

* add helper functions

* fix

* remove random new line

* simplify

* remove unnecessary lines

* rename to GetDamage

* Got it working

* make more clear

* why backwards

* value should be the amount to heal

* fix

* fix all dumb fixedpoint2 edge cases I hope

* One more thing

* fix

* make it more simple

* ops it was backwards

* valueHeal can't be more than remaining

* add all keys beforehand and no need to check and add them inside the loop

* break for loop in case remaining is zero

* comment was wrong

* optimized, works

* remove random spaces

---------

Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com>
This commit is contained in:
Samuka
2026-04-04 16:56:52 +02:00
committed by BarryNorfolk
co-authored by Princess Cheeseballs
parent c9d52d8a03
commit 29aceb4e03
5 changed files with 293 additions and 75 deletions
@@ -1,3 +1,5 @@
using System.Linq;
using System.Net.Sockets;
using Content.Shared.Damage.Components;
using Content.Shared.Damage.Prototypes;
using Content.Shared.FixedPoint;
@@ -230,6 +232,185 @@ public sealed partial class DamageableSystem
return damageDone;
}
/// <summary>
/// Will reduce the damage on the entity exactly by <see cref="amount"/> as close as equally distributed among all damage types the entity has.
/// If one of the damage types of the entity is too low. it will heal that completly and distribute the excess healing among the other damage types.
/// If the <see cref="amount"/> is larger than the total damage of the entity then it just clears all damage.
/// </summary>
/// <param name="ent">entity to be healed</param>
/// <param name="amount">how much to heal. value has to be negative to heal</param>
/// <param name="group">from which group to heal. if null, heal from all groups</param>
/// <param name="origin">who did the healing</param>
public DamageSpecifier HealEvenly(
Entity<DamageableComponent?> ent,
FixedPoint2 amount,
ProtoId<DamageGroupPrototype>? group = null,
EntityUid? origin = null,
// Begin DeltaV additions - Adapt to shitmed changes
TargetBodyPart? targetPart = null,
bool doPartDamage = true,
bool onlyDamageParts = false // Fix EvenHealing on Limbs && Standardize PartDamage.
// End DeltaV additions - Adapt to shitmed changes
)
{
var damageChange = new DamageSpecifier();
if (!_damageableQuery.Resolve(ent, ref ent.Comp, false) || amount >= 0)
return damageChange;
// Get our total damage, or heal if we're below a certain amount.
if (!TryGetDamageGreaterThan((ent, ent.Comp), -amount, out var damage, group))
return ChangeDamage(ent, -damage, true, false, origin);
// make sure damageChange has the same damage types as damage
damageChange.DamageDict.EnsureCapacity(damage.DamageDict.Count);
foreach (var type in damage.DamageDict.Keys)
{
damageChange.DamageDict.Add(type, FixedPoint2.Zero);
}
var remaining = -amount;
var keys = damage.DamageDict.Keys.ToList();
while (remaining > 0)
{
var count = keys.Count;
// We do this to ensure that we always round up when dividing to avoid excess loops.
// We already have logic to prevent healing more than we have.
var maxHeal = count == 1 ? remaining : (remaining + FixedPoint2.Epsilon * (count - 1)) / count;
// Iterate backwards since we're removing items.
for (var i = count - 1; i >= 0; i--)
{
var type = keys[i];
// This is the amount we're trying to heal, capped by maxHeal
var heal = damage.DamageDict[type] + damageChange.DamageDict[type];
// Don't go above max, if we don't go above max
if (heal > maxHeal)
heal = maxHeal;
// If we're not above max, we will heal it fully and don't need to enumerate anymore!
else
keys.RemoveAt(i);
if (heal >= remaining)
{
// Don't remove more than we can remove. Prevents us from healing more than we'd expect...
damageChange.DamageDict[type] -= remaining;
remaining = FixedPoint2.Zero;
break;
}
remaining -= heal;
damageChange.DamageDict[type] -= heal;
}
}
return ChangeDamage(ent, damageChange, true, false, origin, targetPart: targetPart, doPartDamage: doPartDamage, onlyDamageParts: onlyDamageParts); // DeltaV - Adapt to shitmed
}
/// <summary>
/// Will reduce the damage on the entity exactly by <see cref="amount"/> distributed by weight among all damage types the entity has.
/// (the weight is how much damage of the type there is)
/// If the <see cref="amount"/> is larger than the total damage of the entity then it just clears all damage.
/// </summary>
/// <param name="ent">entity to be healed</param>
/// <param name="amount">how much to heal. value has to be negative to heal</param>
/// <param name="group">from which group to heal. if null, heal from all groups</param>
/// <param name="origin">who did the healing</param>
public DamageSpecifier HealDistributed(
Entity<DamageableComponent?> ent,
FixedPoint2 amount,
ProtoId<DamageGroupPrototype>? group = null,
EntityUid? origin = null)
{
var damageChange = new DamageSpecifier();
if (!_damageableQuery.Resolve(ent, ref ent.Comp, false) || amount >= 0)
return damageChange;
// Get our total damage, or heal if we're below a certain amount.
if (!TryGetDamageGreaterThan((ent, ent.Comp), -amount, out var damage, group))
return ChangeDamage(ent, -damage, true, false, origin);
// make sure damageChange has the same damage types as damageEntity
damageChange.DamageDict.EnsureCapacity(damage.DamageDict.Count);
var total = damage.GetTotal();
// heal weighted by the damage of that type
foreach (var (type, value) in damage.DamageDict)
{
damageChange.DamageDict.Add(type, value / total * amount);
}
return ChangeDamage(ent, damageChange, true, false, origin);
}
/// <summary>
/// Tries to get damage from an entity with an optional group specifier.
/// </summary>
/// <param name="ent">Entity we're checking the damage on</param>
/// <param name="amount">Amount we want the damage to be greater than ideally</param>
/// <param name="damage">Damage specifier we're returning with</param>
/// <param name="group">An optional group, note that if it fails to index it will just use all damage.</param>
/// <returns>True if the total damage is greater than the specified amount</returns>
public bool TryGetDamageGreaterThan(Entity<DamageableComponent> ent,
FixedPoint2 amount,
out DamageSpecifier damage,
ProtoId<DamageGroupPrototype>? group = null)
{
// get the damage should be healed (either all or only from one group)
damage = group == null ? GetDamage(ent) : GetDamage(ent, group.Value);
// If trying to heal more than the total damage of damageEntity just heal everything
return damage.GetTotal() > amount;
}
/// <summary>
/// Returns a <see cref="DamageSpecifier"/> with all positive damage of the entity from the group specified
/// </summary>
/// <param name="ent">entity with damage</param>
/// <param name="group">group of damage to get values from</param>
/// <returns></returns>
public DamageSpecifier GetDamage(Entity<DamageableComponent> ent, ProtoId<DamageGroupPrototype> group)
{
// No damage if no group exists...
if (!_prototypeManager.Resolve(group, out var groupProto))
return new DamageSpecifier();
var damage = new DamageSpecifier();
damage.DamageDict.EnsureCapacity(groupProto.DamageTypes.Count);
foreach (var damageId in groupProto.DamageTypes)
{
if (!ent.Comp.Damage.DamageDict.TryGetValue(damageId, out var value))
continue;
if (value > FixedPoint2.Zero)
damage.DamageDict.Add(damageId, value);
}
return damage;
}
/// <summary>
/// Returns a <see cref="DamageSpecifier"/> with all positive damage of the entity
/// </summary>
/// <param name="ent">entity with damage</param>
/// <returns></returns>
public DamageSpecifier GetDamage(Entity<DamageableComponent> ent)
{
var damage = new DamageSpecifier();
damage.DamageDict.EnsureCapacity(ent.Comp.Damage.DamageDict.Count);
foreach (var (damageId, value) in ent.Comp.Damage.DamageDict)
{
if (value > FixedPoint2.Zero)
damage.DamageDict.Add(damageId, value);
}
return damage;
}
/// <summary>
/// Applies the two universal "All" modifiers, if set.
/// Individual damage source modifiers are set in their respective code.