CatStation/Content.Shared/Damage/Systems/DamageableSystem.cs
ArtisticRoomba 376f16504c
Content.Shared EntityQuery<T> dependency injection refactor (#43498)
* part 1

* second pass

* final fix

* last fix i swear

* an even bigger genius

* regex missed this

* should be all of em?

* oops

* yeah

* Revert "should be all of em?"

This reverts commit 4dd54d1b9a76977b4cf8d6008358e4dacdee6eb1.

---------

Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com>
2026-04-11 21:30:36 +00:00

97 lines
4.5 KiB
C#

using System.Linq;
using Content.Shared.Chemistry;
using Content.Shared.Damage.Components;
using Content.Shared.Damage.Prototypes;
using Content.Shared.Explosion.EntitySystems;
using Content.Shared.FixedPoint;
using Content.Shared.Mobs.Systems;
using Robust.Shared.Configuration;
using Robust.Shared.GameStates;
using Robust.Shared.Network;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Content.Shared.Damage.Systems;
public sealed partial class DamageableSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly INetManager _netMan = default!;
[Dependency] private readonly MobThresholdSystem _mobThreshold = default!;
[Dependency] private readonly IConfigurationManager _config = default!;
[Dependency] private readonly SharedChemistryGuideDataSystem _chemistryGuideData = default!;
[Dependency] private readonly SharedExplosionSystem _explosion = default!;
[Dependency] private readonly EntityQuery<AppearanceComponent> _appearanceQuery = default!;
[Dependency] private readonly EntityQuery<DamageableComponent> _damageableQuery = default!;
public float UniversalAllDamageModifier { get; private set; } = 1f;
public float UniversalAllHealModifier { get; private set; } = 1f;
public float UniversalMeleeDamageModifier { get; private set; } = 1f;
public float UniversalProjectileDamageModifier { get; private set; } = 1f;
public float UniversalHitscanDamageModifier { get; private set; } = 1f;
public float UniversalReagentDamageModifier { get; private set; } = 1f;
public float UniversalReagentHealModifier { get; private set; } = 1f;
public float UniversalExplosionDamageModifier { get; private set; } = 1f;
public float UniversalThrownDamageModifier { get; private set; } = 1f;
public float UniversalTopicalsHealModifier { get; private set; } = 1f;
public float UniversalMobDamageModifier { get; private set; } = 1f;
private Dictionary<ProtoId<DamageContainerPrototype>, HashSet<ProtoId<DamageTypePrototype>>> _supportedTypesByContainer = new();
/// <summary>
/// If the damage in a DamageableComponent was changed this function should be called.
/// </summary>
/// <remarks>
/// This updates cached damage information, flags the component as dirty, and raises a damage changed event.
/// The damage changed event is used by other systems, such as damage thresholds.
/// </remarks>
private void OnEntityDamageChanged(
Entity<DamageableComponent> ent,
DamageSpecifier? damageDelta = null,
bool interruptsDoAfters = true,
EntityUid? origin = null
)
{
ent.Comp.Damage.GetDamagePerGroup(_prototypeManager, ent.Comp.DamagePerGroup);
ent.Comp.TotalDamage = ent.Comp.Damage.GetTotal();
Dirty(ent);
if (damageDelta != null && _appearanceQuery.TryGetComponent(ent, out var appearance))
{
_appearance.SetData(
ent,
DamageVisualizerKeys.DamageUpdateGroups,
new DamageVisualizerGroupData(ent.Comp.DamagePerGroup.Keys.ToList()),
appearance
);
}
// TODO DAMAGE
// byref struct event.
RaiseLocalEvent(ent, new DamageChangedEvent(ent.Comp, damageDelta, interruptsDoAfters, origin));
}
/// <summary>
/// Goes through an entity damage's and saves them inside a dictionary if the value is higher than 0
/// The dictionary is structured with a string for the name of the damage type, and a FixedPoint2 for the numeric damage value
/// </summary>
public Dictionary<ProtoId<DamageTypePrototype>, FixedPoint2> GetDamages(Dictionary<ProtoId<DamageGroupPrototype>, FixedPoint2> damagePerGroup, DamageSpecifier damage)
{
var damageTypes = new Dictionary<ProtoId<DamageTypePrototype>, FixedPoint2>();
foreach (var (damageGroupId, _) in damagePerGroup) //go through each group
{
var group = _prototypeManager.Index<DamageGroupPrototype>(damageGroupId); //get group
foreach (var type in group.DamageTypes) //go through each type inside that group
{
if (!damage.DamageDict.TryGetValue(type, out var damageValue) || damageValue == 0) //get value and make sure it isn't 0
continue;
damageTypes.Add(type, damageValue);
}
}
return damageTypes;
}
}