Files
tbd-station-14/Content.Shared/Chat/SharedSuicideSystem.cs
Hannah Giovanna Dawson cdbe92d37d Update DamageableSystem to modern standards (#39417)
* Update DamageableSystem to modern standards

* DamageContainerId -> DamageContainerID with lint flag

* Replace strings with protoids

* Make CVar subscription declarations all consistently whitespaced

* ChangeDamage -> TryChangeDamage, cope with C# jank

* Revert event signature changes

* Restore a comment

* Re-add two queries

* Init the queries

* Use appearanceQuery in DamageChanged

* Use damageableQuery in TryChangeDamage

* Use damageableQuery in SetDamageModifierSetId

* Final cleanup, fix sandboxing

* Rectify ExplosionSystem:::ProcessEntity's call to TryChangeDamage

* Re-organize DamageableSystem

* first big fuck you breaking change.

* THATS A LOT OF DAMAGE!!!

* Fix test fails

* test fixes 2

* push it

---------

Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com>
2025-10-27 19:53:04 +00:00

72 lines
3.5 KiB
C#

using System.Linq;
using Content.Shared.Damage;
using Content.Shared.Damage.Components;
using Content.Shared.Damage.Prototypes;
using Content.Shared.Damage.Systems;
using Content.Shared.Mobs.Components;
using Robust.Shared.Prototypes;
namespace Content.Shared.Chat;
public sealed class SharedSuicideSystem : EntitySystem
{
private static readonly ProtoId<DamageTypePrototype> FallbackDamageType = "Blunt";
[Dependency] private readonly DamageableSystem _damageableSystem = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
/// <summary>
/// Applies lethal damage spread out across the damage types given.
/// </summary>
public void ApplyLethalDamage(Entity<DamageableComponent> target, DamageSpecifier damageSpecifier)
{
// Create a new damageSpecifier so that we don't make alterations to the original DamageSpecifier
// Failing to do this will permanently change a weapon's damage making it insta-kill people
var appliedDamageSpecifier = new DamageSpecifier(damageSpecifier);
if (!TryComp<MobThresholdsComponent>(target, out var mobThresholds))
return;
// Mob thresholds are sorted from alive -> crit -> dead,
// grabbing the last key will give us how much damage is needed to kill a target from zero
// The exact lethal damage amount is adjusted based on their current damage taken
var lethalAmountOfDamage = mobThresholds.Thresholds.Keys.Last() - target.Comp.TotalDamage;
var totalDamage = appliedDamageSpecifier.GetTotal();
// Removing structural because it causes issues against entities that cannot take structural damage,
// then getting the total to use in calculations for spreading out damage.
appliedDamageSpecifier.DamageDict.Remove("Structural");
// Split the total amount of damage needed to kill the target by every damage type in the DamageSpecifier
foreach (var (key, value) in appliedDamageSpecifier.DamageDict)
{
appliedDamageSpecifier.DamageDict[key] = Math.Ceiling((double) (value * lethalAmountOfDamage / totalDamage));
}
_damageableSystem.ChangeDamage(target.AsNullable(), appliedDamageSpecifier, true, origin: target);
}
/// <summary>
/// Applies lethal damage in a single type, specified by a single damage type.
/// </summary>
public void ApplyLethalDamage(Entity<DamageableComponent> target, ProtoId<DamageTypePrototype>? damageType)
{
if (!TryComp<MobThresholdsComponent>(target, out var mobThresholds))
return;
// Mob thresholds are sorted from alive -> crit -> dead,
// grabbing the last key will give us how much damage is needed to kill a target from zero
// The exact lethal damage amount is adjusted based on their current damage taken
var lethalAmountOfDamage = mobThresholds.Thresholds.Keys.Last() - target.Comp.TotalDamage;
// We don't want structural damage for the same reasons listed above
if (!_prototypeManager.TryIndex(damageType, out var damagePrototype) || damagePrototype.ID == "Structural")
{
Log.Error($"{nameof(SharedSuicideSystem)} could not find the damage type prototype associated with {damageType}. Falling back to {FallbackDamageType}");
damagePrototype = _prototypeManager.Index(FallbackDamageType);
}
var damage = new DamageSpecifier(damagePrototype, lethalAmountOfDamage);
_damageableSystem.ChangeDamage(target.AsNullable(), damage, true, origin: target);
}
}