DamageableSystem.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. // SPDX-FileCopyrightText: 2021 20kdc <asdd2808@gmail.com>
  2. // SPDX-FileCopyrightText: 2021 Acruid <shatter66@gmail.com>
  3. // SPDX-FileCopyrightText: 2021 DrSmugleaf <DrSmugleaf@users.noreply.github.com>
  4. // SPDX-FileCopyrightText: 2021 Javier Guardia Fernández <DrSmugleaf@users.noreply.github.com>
  5. // SPDX-FileCopyrightText: 2021 Vera Aguilera Puerto <6766154+Zumorica@users.noreply.github.com>
  6. // SPDX-FileCopyrightText: 2021 Vera Aguilera Puerto <gradientvera@outlook.com>
  7. // SPDX-FileCopyrightText: 2022 Alex Evgrashin <aevgrashin@yandex.ru>
  8. // SPDX-FileCopyrightText: 2022 CommieFlowers <rasmus.cedergren@hotmail.com>
  9. // SPDX-FileCopyrightText: 2022 EmoGarbage404 <98561806+EmoGarbage404@users.noreply.github.com>
  10. // SPDX-FileCopyrightText: 2022 Flipp Syder <76629141+vulppine@users.noreply.github.com>
  11. // SPDX-FileCopyrightText: 2022 Moony <moonheart08@users.noreply.github.com>
  12. // SPDX-FileCopyrightText: 2022 Paul Ritter <ritter.paul1@googlemail.com>
  13. // SPDX-FileCopyrightText: 2022 Rane <60792108+Elijahrane@users.noreply.github.com>
  14. // SPDX-FileCopyrightText: 2022 ShadowCommander <10494922+ShadowCommander@users.noreply.github.com>
  15. // SPDX-FileCopyrightText: 2022 Visne <39844191+Visne@users.noreply.github.com>
  16. // SPDX-FileCopyrightText: 2022 mirrorcult <lunarautomaton6@gmail.com>
  17. // SPDX-FileCopyrightText: 2022 moonheart08 <moonheart08@users.noreply.github.com>
  18. // SPDX-FileCopyrightText: 2022 rolfero <45628623+rolfero@users.noreply.github.com>
  19. // SPDX-FileCopyrightText: 2022 wrexbe <81056464+wrexbe@users.noreply.github.com>
  20. // SPDX-FileCopyrightText: 2023 DrSmugleaf <drsmugleaf@gmail.com>
  21. // SPDX-FileCopyrightText: 2023 Jezithyr <jezithyr@gmail.com>
  22. // SPDX-FileCopyrightText: 2023 Kara <lunarautomaton6@gmail.com>
  23. // SPDX-FileCopyrightText: 2023 Leon Friedrich <60421075+ElectroJr@users.noreply.github.com>
  24. // SPDX-FileCopyrightText: 2023 Nemanja <98561806+EmoGarbage404@users.noreply.github.com>
  25. // SPDX-FileCopyrightText: 2023 PixelTK <85175107+PixelTheKermit@users.noreply.github.com>
  26. // SPDX-FileCopyrightText: 2023 Slava0135 <40753025+Slava0135@users.noreply.github.com>
  27. // SPDX-FileCopyrightText: 2023 metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
  28. // SPDX-FileCopyrightText: 2023 metalgearsloth <comedian_vs_clown@hotmail.com>
  29. // SPDX-FileCopyrightText: 2024 0x6273 <0x40@keemail.me>
  30. // SPDX-FileCopyrightText: 2024 DrSmugleaf <10968691+DrSmugleaf@users.noreply.github.com>
  31. // SPDX-FileCopyrightText: 2024 Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
  32. // SPDX-FileCopyrightText: 2025 ActiveMammmoth <140334666+ActiveMammmoth@users.noreply.github.com>
  33. // SPDX-FileCopyrightText: 2025 ActiveMammmoth <kmcsmooth@gmail.com>
  34. // SPDX-FileCopyrightText: 2025 Aiden <28298836+Aidenkrz@users.noreply.github.com>
  35. // SPDX-FileCopyrightText: 2025 Aidenkrz <aiden@djkraz.com>
  36. // SPDX-FileCopyrightText: 2025 Aineias1 <dmitri.s.kiselev@gmail.com>
  37. // SPDX-FileCopyrightText: 2025 Aviu00 <aviu00@protonmail.com>
  38. // SPDX-FileCopyrightText: 2025 FaDeOkno <143940725+FaDeOkno@users.noreply.github.com>
  39. // SPDX-FileCopyrightText: 2025 GoobBot <uristmchands@proton.me>
  40. // SPDX-FileCopyrightText: 2025 Ilya246 <57039557+Ilya246@users.noreply.github.com>
  41. // SPDX-FileCopyrightText: 2025 McBosserson <148172569+McBosserson@users.noreply.github.com>
  42. // SPDX-FileCopyrightText: 2025 Milon <plmilonpl@gmail.com>
  43. // SPDX-FileCopyrightText: 2025 Piras314 <p1r4s@proton.me>
  44. // SPDX-FileCopyrightText: 2025 Rouden <149893554+Roudenn@users.noreply.github.com>
  45. // SPDX-FileCopyrightText: 2025 SlamBamActionman <83650252+SlamBamActionman@users.noreply.github.com>
  46. // SPDX-FileCopyrightText: 2025 Solstice <solsticeofthewinter@gmail.com>
  47. // SPDX-FileCopyrightText: 2025 TheBorzoiMustConsume <197824988+TheBorzoiMustConsume@users.noreply.github.com>
  48. // SPDX-FileCopyrightText: 2025 Unlumination <144041835+Unlumy@users.noreply.github.com>
  49. // SPDX-FileCopyrightText: 2025 coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  50. // SPDX-FileCopyrightText: 2025 deltanedas <39013340+deltanedas@users.noreply.github.com>
  51. // SPDX-FileCopyrightText: 2025 deltanedas <@deltanedas:kde.org>
  52. // SPDX-FileCopyrightText: 2025 gluesniffler <159397573+gluesniffler@users.noreply.github.com>
  53. // SPDX-FileCopyrightText: 2025 gluesniffler <linebarrelerenthusiast@gmail.com>
  54. // SPDX-FileCopyrightText: 2025 gus <august.eymann@gmail.com>
  55. // SPDX-FileCopyrightText: 2025 keronshb <54602815+keronshb@users.noreply.github.com>
  56. // SPDX-FileCopyrightText: 2025 username <113782077+whateverusername0@users.noreply.github.com>
  57. // SPDX-FileCopyrightText: 2025 whateverusername0 <whateveremail>
  58. //
  59. // SPDX-License-Identifier: AGPL-3.0-or-later
  60. using System.Linq;
  61. using Content.Shared.CCVar;
  62. using Content.Shared.Chemistry;
  63. using Content.Shared.Damage.Prototypes;
  64. using Content.Shared.FixedPoint;
  65. using Content.Shared.Inventory;
  66. using Content.Shared.Mind.Components;
  67. using Content.Shared.Mobs.Components;
  68. using Content.Shared.Mobs.Systems;
  69. using Content.Shared.Radiation.Events;
  70. using Content.Shared.Rejuvenate;
  71. using Robust.Shared.Configuration;
  72. using Robust.Shared.GameStates;
  73. using Robust.Shared.Network;
  74. using Robust.Shared.Prototypes;
  75. using Robust.Shared.Utility;
  76. // Shitmed Change
  77. using Content.Shared.Body.Systems;
  78. using Content.Shared._Shitmed.Targeting;
  79. using Robust.Shared.Random;
  80. namespace Content.Shared.Damage
  81. {
  82. public sealed class DamageableSystem : EntitySystem
  83. {
  84. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  85. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  86. [Dependency] private readonly INetManager _netMan = default!;
  87. [Dependency] private readonly SharedBodySystem _body = default!; // Shitmed Change
  88. [Dependency] private readonly IRobustRandom _random = default!; // Shitmed Change
  89. [Dependency] private readonly MobThresholdSystem _mobThreshold = default!;
  90. [Dependency] private readonly IConfigurationManager _config = default!;
  91. [Dependency] private readonly SharedChemistryGuideDataSystem _chemistryGuideData = default!;
  92. private EntityQuery<AppearanceComponent> _appearanceQuery;
  93. private EntityQuery<DamageableComponent> _damageableQuery;
  94. private EntityQuery<MindContainerComponent> _mindContainerQuery;
  95. public float UniversalAllDamageModifier { get; private set; } = 1f;
  96. public float UniversalAllHealModifier { get; private set; } = 1f;
  97. public float UniversalMeleeDamageModifier { get; private set; } = 1f;
  98. public float UniversalProjectileDamageModifier { get; private set; } = 1f;
  99. public float UniversalHitscanDamageModifier { get; private set; } = 1f;
  100. public float UniversalReagentDamageModifier { get; private set; } = 1f;
  101. public float UniversalReagentHealModifier { get; private set; } = 1f;
  102. public float UniversalExplosionDamageModifier { get; private set; } = 1f;
  103. public float UniversalThrownDamageModifier { get; private set; } = 1f;
  104. public float UniversalTopicalsHealModifier { get; private set; } = 1f;
  105. public override void Initialize()
  106. {
  107. SubscribeLocalEvent<DamageableComponent, ComponentInit>(DamageableInit);
  108. SubscribeLocalEvent<DamageableComponent, ComponentHandleState>(DamageableHandleState);
  109. SubscribeLocalEvent<DamageableComponent, ComponentGetState>(DamageableGetState);
  110. SubscribeLocalEvent<DamageableComponent, OnIrradiatedEvent>(OnIrradiated);
  111. SubscribeLocalEvent<DamageableComponent, RejuvenateEvent>(OnRejuvenate);
  112. _appearanceQuery = GetEntityQuery<AppearanceComponent>();
  113. _damageableQuery = GetEntityQuery<DamageableComponent>();
  114. _mindContainerQuery = GetEntityQuery<MindContainerComponent>();
  115. // Damage modifier CVars are updated and stored here to be queried in other systems.
  116. // Note that certain modifiers requires reloading the guidebook.
  117. Subs.CVar(_config, CCVars.PlaytestAllDamageModifier, value =>
  118. {
  119. UniversalAllDamageModifier = value;
  120. _chemistryGuideData.ReloadAllReagentPrototypes();
  121. }, true);
  122. Subs.CVar(_config, CCVars.PlaytestAllHealModifier, value =>
  123. {
  124. UniversalAllHealModifier = value;
  125. _chemistryGuideData.ReloadAllReagentPrototypes();
  126. }, true);
  127. Subs.CVar(_config, CCVars.PlaytestProjectileDamageModifier, value => UniversalProjectileDamageModifier = value, true);
  128. Subs.CVar(_config, CCVars.PlaytestMeleeDamageModifier, value => UniversalMeleeDamageModifier = value, true);
  129. Subs.CVar(_config, CCVars.PlaytestProjectileDamageModifier, value => UniversalProjectileDamageModifier = value, true);
  130. Subs.CVar(_config, CCVars.PlaytestHitscanDamageModifier, value => UniversalHitscanDamageModifier = value, true);
  131. Subs.CVar(_config, CCVars.PlaytestReagentDamageModifier, value =>
  132. {
  133. UniversalReagentDamageModifier = value;
  134. _chemistryGuideData.ReloadAllReagentPrototypes();
  135. }, true);
  136. Subs.CVar(_config, CCVars.PlaytestReagentHealModifier, value =>
  137. {
  138. UniversalReagentHealModifier = value;
  139. _chemistryGuideData.ReloadAllReagentPrototypes();
  140. }, true);
  141. Subs.CVar(_config, CCVars.PlaytestExplosionDamageModifier, value => UniversalExplosionDamageModifier = value, true);
  142. Subs.CVar(_config, CCVars.PlaytestThrownDamageModifier, value => UniversalThrownDamageModifier = value, true);
  143. Subs.CVar(_config, CCVars.PlaytestTopicalsHealModifier, value => UniversalTopicalsHealModifier = value, true);
  144. }
  145. /// <summary>
  146. /// Initialize a damageable component
  147. /// </summary>
  148. private void DamageableInit(EntityUid uid, DamageableComponent component, ComponentInit _)
  149. {
  150. if (component.DamageContainerID != null &&
  151. _prototypeManager.TryIndex<DamageContainerPrototype>(component.DamageContainerID,
  152. out var damageContainerPrototype))
  153. {
  154. // Initialize damage dictionary, using the types and groups from the damage
  155. // container prototype
  156. foreach (var type in damageContainerPrototype.SupportedTypes)
  157. {
  158. component.Damage.DamageDict.TryAdd(type, FixedPoint2.Zero);
  159. }
  160. foreach (var groupId in damageContainerPrototype.SupportedGroups)
  161. {
  162. var group = _prototypeManager.Index<DamageGroupPrototype>(groupId);
  163. foreach (var type in group.DamageTypes)
  164. {
  165. component.Damage.DamageDict.TryAdd(type, FixedPoint2.Zero);
  166. }
  167. }
  168. }
  169. else
  170. {
  171. // No DamageContainerPrototype was given. So we will allow the container to support all damage types
  172. foreach (var type in _prototypeManager.EnumeratePrototypes<DamageTypePrototype>())
  173. {
  174. component.Damage.DamageDict.TryAdd(type.ID, FixedPoint2.Zero);
  175. }
  176. }
  177. component.Damage.GetDamagePerGroup(_prototypeManager, component.DamagePerGroup);
  178. component.TotalDamage = component.Damage.GetTotal();
  179. }
  180. /// <summary>
  181. /// Directly sets the damage specifier of a damageable component.
  182. /// </summary>
  183. /// <remarks>
  184. /// Useful for some unfriendly folk. Also ensures that cached values are updated and that a damage changed
  185. /// event is raised.
  186. /// </remarks>
  187. public void SetDamage(EntityUid uid, DamageableComponent damageable, DamageSpecifier damage)
  188. {
  189. damageable.Damage = damage;
  190. DamageChanged(uid, damageable);
  191. }
  192. /// <summary>
  193. /// If the damage in a DamageableComponent was changed, this function should be called.
  194. /// </summary>
  195. /// <remarks>
  196. /// This updates cached damage information, flags the component as dirty, and raises a damage changed event.
  197. /// The damage changed event is used by other systems, such as damage thresholds.
  198. /// </remarks>
  199. public void DamageChanged(EntityUid uid, DamageableComponent component, DamageSpecifier? damageDelta = null,
  200. bool interruptsDoAfters = true, EntityUid? origin = null, bool? canSever = null) // Shitmed Change
  201. {
  202. component.Damage.GetDamagePerGroup(_prototypeManager, component.DamagePerGroup);
  203. component.TotalDamage = component.Damage.GetTotal();
  204. Dirty(uid, component);
  205. if (_appearanceQuery.TryGetComponent(uid, out var appearance) && damageDelta != null)
  206. {
  207. var data = new DamageVisualizerGroupData(component.DamagePerGroup.Keys.ToList());
  208. _appearance.SetData(uid, DamageVisualizerKeys.DamageUpdateGroups, data, appearance);
  209. }
  210. RaiseLocalEvent(uid, new DamageChangedEvent(component, damageDelta, interruptsDoAfters, origin, canSever ?? true)); // Shitmed Change
  211. }
  212. /// <summary>
  213. /// Applies damage specified via a <see cref="DamageSpecifier"/>.
  214. /// </summary>
  215. /// <remarks>
  216. /// <see cref="DamageSpecifier"/> is effectively just a dictionary of damage types and damage values. This
  217. /// function just applies the container's resistances (unless otherwise specified) and then changes the
  218. /// stored damage data. Division of group damage into types is managed by <see cref="DamageSpecifier"/>.
  219. /// </remarks>
  220. /// <returns>
  221. /// Returns a <see cref="DamageSpecifier"/> with information about the actual damage changes. This will be
  222. /// null if the user had no applicable components that can take damage.
  223. /// </returns>
  224. public DamageSpecifier? TryChangeDamage(EntityUid? uid, DamageSpecifier damage, bool ignoreResistances = false,
  225. bool interruptsDoAfters = true, DamageableComponent? damageable = null, EntityUid? origin = null,
  226. // Shitmed Change
  227. bool? canSever = true, bool? canEvade = false, float? partMultiplier = 1.00f, TargetBodyPart? targetPart = null,
  228. float armorPenetration = 0f,
  229. // Goobstation
  230. bool heavyAttack = false)
  231. {
  232. if (!uid.HasValue || !_damageableQuery.Resolve(uid.Value, ref damageable, false))
  233. {
  234. // TODO BODY SYSTEM pass damage onto body system
  235. return null;
  236. }
  237. if (damage.Empty)
  238. {
  239. return damage;
  240. }
  241. var before = new BeforeDamageChangedEvent(damage, origin, targetPart, canEvade ?? false, heavyAttack); // Shitmed Change // Goobstation
  242. RaiseLocalEvent(uid.Value, ref before);
  243. if (before.Cancelled)
  244. return null;
  245. // Shitmed Change Start
  246. var partDamage = new TryChangePartDamageEvent(damage, origin, targetPart, ignoreResistances, armorPenetration, canSever ?? true, canEvade ?? false, partMultiplier ?? 1.00f);
  247. RaiseLocalEvent(uid.Value, ref partDamage);
  248. if (partDamage.Evaded || partDamage.Cancelled)
  249. return null;
  250. // Shitmed Change End
  251. // Apply resistances
  252. if (!ignoreResistances)
  253. {
  254. if (damageable.DamageModifierSetId != null &&
  255. _prototypeManager.TryIndex<DamageModifierSetPrototype>(damageable.DamageModifierSetId, out var modifierSet))
  256. {
  257. // TODO DAMAGE PERFORMANCE
  258. // use a local private field instead of creating a new dictionary here..
  259. // TODO: We need to add a check to see if the given armor covers the targeted part (if any) to modify or not.
  260. damage = DamageSpecifier.ApplyModifierSet(damage, modifierSet);
  261. }
  262. var ev = new DamageModifyEvent(uid.Value, damage, origin, targetPart, armorPenetration); // Shitmed + Goobstation Change
  263. RaiseLocalEvent(uid.Value, ev);
  264. damage = ev.Damage;
  265. if (damage.Empty)
  266. {
  267. return damage;
  268. }
  269. }
  270. damage = ApplyUniversalAllModifiers(damage);
  271. // TODO DAMAGE PERFORMANCE
  272. // Consider using a local private field instead of creating a new dictionary here.
  273. // Would need to check that nothing ever tries to cache the delta.
  274. var delta = new DamageSpecifier();
  275. delta.DamageDict.EnsureCapacity(damage.DamageDict.Count);
  276. var dict = damageable.Damage.DamageDict;
  277. foreach (var (type, value) in damage.DamageDict)
  278. {
  279. // CollectionsMarshal my beloved.
  280. if (!dict.TryGetValue(type, out var oldValue))
  281. continue;
  282. var newValue = FixedPoint2.Max(FixedPoint2.Zero, oldValue + value);
  283. if (newValue == oldValue)
  284. continue;
  285. dict[type] = newValue;
  286. delta.DamageDict[type] = newValue - oldValue;
  287. }
  288. if (delta.DamageDict.Count > 0)
  289. DamageChanged(uid.Value, damageable, delta, interruptsDoAfters, origin, canSever); // Shitmed Change
  290. return delta;
  291. }
  292. /// <summary>
  293. /// Applies the two univeral "All" modifiers, if set.
  294. /// Individual damage source modifiers are set in their respective code.
  295. /// </summary>
  296. /// <param name="damage">The damage to be changed.</param>
  297. public DamageSpecifier ApplyUniversalAllModifiers(DamageSpecifier damage)
  298. {
  299. // Checks for changes first since they're unlikely in normal play.
  300. if (UniversalAllDamageModifier == 1f && UniversalAllHealModifier == 1f)
  301. return damage;
  302. foreach (var (key, value) in damage.DamageDict)
  303. {
  304. if (value == 0)
  305. continue;
  306. if (value > 0)
  307. {
  308. damage.DamageDict[key] *= UniversalAllDamageModifier;
  309. continue;
  310. }
  311. if (value < 0)
  312. {
  313. damage.DamageDict[key] *= UniversalAllHealModifier;
  314. }
  315. }
  316. return damage;
  317. }
  318. /// <summary>
  319. /// Sets all damage types supported by a <see cref="DamageableComponent"/> to the specified value.
  320. /// </summary>
  321. /// <remakrs>
  322. /// Does nothing If the given damage value is negative.
  323. /// </remakrs>
  324. public void SetAllDamage(EntityUid uid, DamageableComponent component, FixedPoint2 newValue)
  325. {
  326. if (newValue < 0)
  327. {
  328. // invalid value
  329. return;
  330. }
  331. foreach (var type in component.Damage.DamageDict.Keys)
  332. {
  333. component.Damage.DamageDict[type] = newValue;
  334. }
  335. // Setting damage does not count as 'dealing' damage, even if it is set to a larger value, so we pass an
  336. // empty damage delta.
  337. DamageChanged(uid, component, new DamageSpecifier());
  338. // Shitmed Change Start
  339. if (HasComp<TargetingComponent>(uid))
  340. {
  341. foreach (var (part, _) in _body.GetBodyChildren(uid))
  342. {
  343. if (!TryComp(part, out DamageableComponent? damageComp))
  344. continue;
  345. SetAllDamage(part, damageComp, newValue);
  346. }
  347. }
  348. // Shitmed Change End
  349. }
  350. public void SetDamageModifierSetId(EntityUid uid, string damageModifierSetId, DamageableComponent? comp = null)
  351. {
  352. if (!_damageableQuery.Resolve(uid, ref comp))
  353. return;
  354. comp.DamageModifierSetId = damageModifierSetId;
  355. Dirty(uid, comp);
  356. }
  357. private void DamageableGetState(EntityUid uid, DamageableComponent component, ref ComponentGetState args)
  358. {
  359. if (_netMan.IsServer)
  360. {
  361. args.State = new DamageableComponentState(component.Damage.DamageDict, component.DamageContainerID, component.DamageModifierSetId, component.HealthBarThreshold);
  362. }
  363. else
  364. {
  365. // avoid mispredicting damage on newly spawned entities.
  366. args.State = new DamageableComponentState(component.Damage.DamageDict.ShallowClone(), component.DamageContainerID, component.DamageModifierSetId, component.HealthBarThreshold);
  367. }
  368. }
  369. private void OnIrradiated(EntityUid uid, DamageableComponent component, OnIrradiatedEvent args)
  370. {
  371. var damageValue = FixedPoint2.New(args.TotalRads);
  372. // Radiation should really just be a damage group instead of a list of types.
  373. DamageSpecifier damage = new();
  374. foreach (var typeId in component.RadiationDamageTypeIDs)
  375. {
  376. damage.DamageDict.Add(typeId, damageValue);
  377. }
  378. TryChangeDamage(uid, damage, interruptsDoAfters: false);
  379. }
  380. private void OnRejuvenate(EntityUid uid, DamageableComponent component, RejuvenateEvent args)
  381. {
  382. TryComp<MobThresholdsComponent>(uid, out var thresholds);
  383. _mobThreshold.SetAllowRevives(uid, true, thresholds); // do this so that the state changes when we set the damage
  384. SetAllDamage(uid, component, 0);
  385. _mobThreshold.SetAllowRevives(uid, false, thresholds);
  386. }
  387. private void DamageableHandleState(EntityUid uid, DamageableComponent component, ref ComponentHandleState args)
  388. {
  389. if (args.Current is not DamageableComponentState state)
  390. {
  391. return;
  392. }
  393. component.DamageContainerID = state.DamageContainerId;
  394. component.DamageModifierSetId = state.ModifierSetId;
  395. component.HealthBarThreshold = state.HealthBarThreshold;
  396. // Has the damage actually changed?
  397. DamageSpecifier newDamage = new() { DamageDict = new(state.DamageDict) };
  398. var delta = component.Damage - newDamage;
  399. delta.TrimZeros();
  400. if (!delta.Empty)
  401. {
  402. component.Damage = newDamage;
  403. DamageChanged(uid, component, delta);
  404. }
  405. }
  406. }
  407. /// <summary>
  408. /// Raised before damage is done, so stuff can cancel it if necessary.
  409. /// </summary>
  410. [ByRefEvent]
  411. public record struct BeforeDamageChangedEvent(
  412. DamageSpecifier Damage,
  413. EntityUid? Origin = null,
  414. TargetBodyPart? TargetPart = null, // Shitmed Change
  415. bool CanEvade = false, // Lavaland Change
  416. bool HeavyAttack = false, // Goobstation
  417. bool Cancelled = false);
  418. /// <summary>
  419. /// Shitmed Change: Raised on parts before damage is done so we can cancel the damage if they evade.
  420. /// </summary>
  421. [ByRefEvent]
  422. public record struct TryChangePartDamageEvent(
  423. DamageSpecifier Damage,
  424. EntityUid? Origin = null,
  425. TargetBodyPart? TargetPart = null,
  426. bool IgnoreResistances = false,
  427. float ArmorPenetration = 0f,
  428. bool CanSever = true,
  429. bool CanEvade = false,
  430. float PartMultiplier = 1.00f,
  431. bool Evaded = false,
  432. bool Cancelled = false);
  433. /// <summary>
  434. /// Raised on an entity when damage is about to be dealt,
  435. /// in case anything else needs to modify it other than the base
  436. /// damageable component.
  437. ///
  438. /// For example, armor.
  439. /// </summary>
  440. public sealed class DamageModifyEvent : EntityEventArgs, IInventoryRelayEvent
  441. {
  442. // Whenever locational damage is a thing, this should just check only that bit of armour.
  443. public SlotFlags TargetSlots { get; } = ~SlotFlags.POCKET;
  444. public readonly EntityUid Target; // Goobstation
  445. public readonly DamageSpecifier OriginalDamage;
  446. public DamageSpecifier Damage;
  447. public EntityUid? Origin;
  448. public readonly TargetBodyPart? TargetPart; // Shitmed Change
  449. public float ArmorPenetration; // Goobstation
  450. public DamageModifyEvent(EntityUid target, DamageSpecifier damage, EntityUid? origin = null, TargetBodyPart? targetPart = null, float armorPenetration = 0) // Shitmed + Goobstation Change
  451. {
  452. Target = target; // Goobstation
  453. OriginalDamage = damage;
  454. Damage = damage;
  455. Origin = origin;
  456. TargetPart = targetPart; // Shitmed Change
  457. ArmorPenetration = armorPenetration; // Goobstation
  458. }
  459. }
  460. public sealed class DamageChangedEvent : EntityEventArgs
  461. {
  462. /// <summary>
  463. /// This is the component whose damage was changed.
  464. /// </summary>
  465. /// <remarks>
  466. /// Given that nearly every component that cares about a change in the damage, needs to know the
  467. /// current damage values, directly passing this information prevents a lot of duplicate
  468. /// Owner.TryGetComponent() calls.
  469. /// </remarks>
  470. public readonly DamageableComponent Damageable;
  471. /// <summary>
  472. /// The amount by which the damage has changed. If the damage was set directly to some number, this will be
  473. /// null.
  474. /// </summary>
  475. public readonly DamageSpecifier? DamageDelta;
  476. /// <summary>
  477. /// Was any of the damage change dealing damage, or was it all healing?
  478. /// </summary>
  479. public readonly bool DamageIncreased;
  480. /// <summary>
  481. /// Does this event interrupt DoAfters?
  482. /// Note: As provided in the constructor, this *does not* account for DamageIncreased.
  483. /// As written into the event, this *does* account for DamageIncreased.
  484. /// </summary>
  485. public readonly bool InterruptsDoAfters;
  486. /// <summary>
  487. /// Contains the entity which caused the change in damage, if any was responsible.
  488. /// </summary>
  489. public readonly EntityUid? Origin;
  490. /// <summary>
  491. /// Shitmed Change: Can this damage event sever parts?
  492. /// </summary>
  493. public readonly bool CanSever;
  494. public DamageChangedEvent(DamageableComponent damageable, DamageSpecifier? damageDelta, bool interruptsDoAfters, EntityUid? origin, bool canSever = true) // Shitmed Change
  495. {
  496. Damageable = damageable;
  497. DamageDelta = damageDelta;
  498. Origin = origin;
  499. CanSever = canSever; // Shitmed Change
  500. if (DamageDelta == null)
  501. return;
  502. foreach (var damageChange in DamageDelta.DamageDict.Values)
  503. {
  504. if (damageChange > 0)
  505. {
  506. DamageIncreased = true;
  507. break;
  508. }
  509. }
  510. InterruptsDoAfters = interruptsDoAfters && DamageIncreased;
  511. }
  512. }
  513. }