DamageableSystem.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. using System.Linq;
  2. using Content.Shared.CCVar;
  3. using Content.Shared.Chemistry;
  4. using Content.Shared.Damage.Prototypes;
  5. using Content.Shared.FixedPoint;
  6. using Content.Shared.Inventory;
  7. using Content.Shared.Mind.Components;
  8. using Content.Shared.Mobs.Components;
  9. using Content.Shared.Mobs.Systems;
  10. using Content.Shared.Radiation.Events;
  11. using Content.Shared.Rejuvenate;
  12. using Robust.Shared.Configuration;
  13. using Robust.Shared.GameStates;
  14. using Robust.Shared.Network;
  15. using Robust.Shared.Prototypes;
  16. using Robust.Shared.Utility;
  17. namespace Content.Shared.Damage
  18. {
  19. public sealed class DamageableSystem : EntitySystem
  20. {
  21. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  22. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  23. [Dependency] private readonly INetManager _netMan = default!;
  24. [Dependency] private readonly MobThresholdSystem _mobThreshold = default!;
  25. [Dependency] private readonly IConfigurationManager _config = default!;
  26. [Dependency] private readonly SharedChemistryGuideDataSystem _chemistryGuideData = default!;
  27. private EntityQuery<AppearanceComponent> _appearanceQuery;
  28. private EntityQuery<DamageableComponent> _damageableQuery;
  29. private EntityQuery<MindContainerComponent> _mindContainerQuery;
  30. public float UniversalAllDamageModifier { get; private set; } = 1f;
  31. public float UniversalAllHealModifier { get; private set; } = 1f;
  32. public float UniversalMeleeDamageModifier { get; private set; } = 1f;
  33. public float UniversalProjectileDamageModifier { get; private set; } = 1f;
  34. public float UniversalHitscanDamageModifier { get; private set; } = 1f;
  35. public float UniversalReagentDamageModifier { get; private set; } = 1f;
  36. public float UniversalReagentHealModifier { get; private set; } = 1f;
  37. public float UniversalExplosionDamageModifier { get; private set; } = 1f;
  38. public float UniversalThrownDamageModifier { get; private set; } = 1f;
  39. public float UniversalTopicalsHealModifier { get; private set; } = 1f;
  40. public override void Initialize()
  41. {
  42. SubscribeLocalEvent<DamageableComponent, ComponentInit>(DamageableInit);
  43. SubscribeLocalEvent<DamageableComponent, ComponentHandleState>(DamageableHandleState);
  44. SubscribeLocalEvent<DamageableComponent, ComponentGetState>(DamageableGetState);
  45. SubscribeLocalEvent<DamageableComponent, OnIrradiatedEvent>(OnIrradiated);
  46. SubscribeLocalEvent<DamageableComponent, RejuvenateEvent>(OnRejuvenate);
  47. _appearanceQuery = GetEntityQuery<AppearanceComponent>();
  48. _damageableQuery = GetEntityQuery<DamageableComponent>();
  49. _mindContainerQuery = GetEntityQuery<MindContainerComponent>();
  50. // Damage modifier CVars are updated and stored here to be queried in other systems.
  51. // Note that certain modifiers requires reloading the guidebook.
  52. Subs.CVar(_config, CCVars.PlaytestAllDamageModifier, value =>
  53. {
  54. UniversalAllDamageModifier = value;
  55. _chemistryGuideData.ReloadAllReagentPrototypes();
  56. }, true);
  57. Subs.CVar(_config, CCVars.PlaytestAllHealModifier, value =>
  58. {
  59. UniversalAllHealModifier = value;
  60. _chemistryGuideData.ReloadAllReagentPrototypes();
  61. }, true);
  62. Subs.CVar(_config, CCVars.PlaytestProjectileDamageModifier, value => UniversalProjectileDamageModifier = value, true);
  63. Subs.CVar(_config, CCVars.PlaytestMeleeDamageModifier, value => UniversalMeleeDamageModifier = value, true);
  64. Subs.CVar(_config, CCVars.PlaytestProjectileDamageModifier, value => UniversalProjectileDamageModifier = value, true);
  65. Subs.CVar(_config, CCVars.PlaytestHitscanDamageModifier, value => UniversalHitscanDamageModifier = value, true);
  66. Subs.CVar(_config, CCVars.PlaytestReagentDamageModifier, value =>
  67. {
  68. UniversalReagentDamageModifier = value;
  69. _chemistryGuideData.ReloadAllReagentPrototypes();
  70. }, true);
  71. Subs.CVar(_config, CCVars.PlaytestReagentHealModifier, value =>
  72. {
  73. UniversalReagentHealModifier = value;
  74. _chemistryGuideData.ReloadAllReagentPrototypes();
  75. }, true);
  76. Subs.CVar(_config, CCVars.PlaytestExplosionDamageModifier, value => UniversalExplosionDamageModifier = value, true);
  77. Subs.CVar(_config, CCVars.PlaytestThrownDamageModifier, value => UniversalThrownDamageModifier = value, true);
  78. Subs.CVar(_config, CCVars.PlaytestTopicalsHealModifier, value => UniversalTopicalsHealModifier = value, true);
  79. }
  80. /// <summary>
  81. /// Initialize a damageable component
  82. /// </summary>
  83. private void DamageableInit(EntityUid uid, DamageableComponent component, ComponentInit _)
  84. {
  85. if (component.DamageContainerID != null &&
  86. _prototypeManager.TryIndex<DamageContainerPrototype>(component.DamageContainerID,
  87. out var damageContainerPrototype))
  88. {
  89. // Initialize damage dictionary, using the types and groups from the damage
  90. // container prototype
  91. foreach (var type in damageContainerPrototype.SupportedTypes)
  92. {
  93. component.Damage.DamageDict.TryAdd(type, FixedPoint2.Zero);
  94. }
  95. foreach (var groupId in damageContainerPrototype.SupportedGroups)
  96. {
  97. var group = _prototypeManager.Index<DamageGroupPrototype>(groupId);
  98. foreach (var type in group.DamageTypes)
  99. {
  100. component.Damage.DamageDict.TryAdd(type, FixedPoint2.Zero);
  101. }
  102. }
  103. }
  104. else
  105. {
  106. // No DamageContainerPrototype was given. So we will allow the container to support all damage types
  107. foreach (var type in _prototypeManager.EnumeratePrototypes<DamageTypePrototype>())
  108. {
  109. component.Damage.DamageDict.TryAdd(type.ID, FixedPoint2.Zero);
  110. }
  111. }
  112. component.Damage.GetDamagePerGroup(_prototypeManager, component.DamagePerGroup);
  113. component.TotalDamage = component.Damage.GetTotal();
  114. }
  115. /// <summary>
  116. /// Directly sets the damage specifier of a damageable component.
  117. /// </summary>
  118. /// <remarks>
  119. /// Useful for some unfriendly folk. Also ensures that cached values are updated and that a damage changed
  120. /// event is raised.
  121. /// </remarks>
  122. public void SetDamage(EntityUid uid, DamageableComponent damageable, DamageSpecifier damage)
  123. {
  124. damageable.Damage = damage;
  125. DamageChanged(uid, damageable);
  126. }
  127. /// <summary>
  128. /// If the damage in a DamageableComponent was changed, this function should be called.
  129. /// </summary>
  130. /// <remarks>
  131. /// This updates cached damage information, flags the component as dirty, and raises a damage changed event.
  132. /// The damage changed event is used by other systems, such as damage thresholds.
  133. /// </remarks>
  134. public void DamageChanged(EntityUid uid, DamageableComponent component, DamageSpecifier? damageDelta = null,
  135. bool interruptsDoAfters = true, EntityUid? origin = null)
  136. {
  137. component.Damage.GetDamagePerGroup(_prototypeManager, component.DamagePerGroup);
  138. component.TotalDamage = component.Damage.GetTotal();
  139. Dirty(uid, component);
  140. if (_appearanceQuery.TryGetComponent(uid, out var appearance) && damageDelta != null)
  141. {
  142. var data = new DamageVisualizerGroupData(component.DamagePerGroup.Keys.ToList());
  143. _appearance.SetData(uid, DamageVisualizerKeys.DamageUpdateGroups, data, appearance);
  144. }
  145. RaiseLocalEvent(uid, new DamageChangedEvent(component, damageDelta, interruptsDoAfters, origin));
  146. }
  147. /// <summary>
  148. /// Applies damage specified via a <see cref="DamageSpecifier"/>.
  149. /// </summary>
  150. /// <remarks>
  151. /// <see cref="DamageSpecifier"/> is effectively just a dictionary of damage types and damage values. This
  152. /// function just applies the container's resistances (unless otherwise specified) and then changes the
  153. /// stored damage data. Division of group damage into types is managed by <see cref="DamageSpecifier"/>.
  154. /// </remarks>
  155. /// <returns>
  156. /// Returns a <see cref="DamageSpecifier"/> with information about the actual damage changes. This will be
  157. /// null if the user had no applicable components that can take damage.
  158. /// </returns>
  159. public DamageSpecifier? TryChangeDamage(EntityUid? uid, DamageSpecifier damage, bool ignoreResistances = false,
  160. bool interruptsDoAfters = true, DamageableComponent? damageable = null, EntityUid? origin = null)
  161. {
  162. if (!uid.HasValue || !_damageableQuery.Resolve(uid.Value, ref damageable, false))
  163. {
  164. // TODO BODY SYSTEM pass damage onto body system
  165. return null;
  166. }
  167. if (damage.Empty)
  168. {
  169. return damage;
  170. }
  171. var before = new BeforeDamageChangedEvent(damage, origin);
  172. RaiseLocalEvent(uid.Value, ref before);
  173. if (before.Cancelled)
  174. return null;
  175. // Apply resistances
  176. if (!ignoreResistances)
  177. {
  178. if (damageable.DamageModifierSetId != null &&
  179. _prototypeManager.TryIndex<DamageModifierSetPrototype>(damageable.DamageModifierSetId, out var modifierSet))
  180. {
  181. // TODO DAMAGE PERFORMANCE
  182. // use a local private field instead of creating a new dictionary here..
  183. damage = DamageSpecifier.ApplyModifierSet(damage, modifierSet);
  184. }
  185. var ev = new DamageModifyEvent(damage, origin);
  186. RaiseLocalEvent(uid.Value, ev);
  187. damage = ev.Damage;
  188. if (damage.Empty)
  189. {
  190. return damage;
  191. }
  192. }
  193. damage = ApplyUniversalAllModifiers(damage);
  194. // TODO DAMAGE PERFORMANCE
  195. // Consider using a local private field instead of creating a new dictionary here.
  196. // Would need to check that nothing ever tries to cache the delta.
  197. var delta = new DamageSpecifier();
  198. delta.DamageDict.EnsureCapacity(damage.DamageDict.Count);
  199. var dict = damageable.Damage.DamageDict;
  200. foreach (var (type, value) in damage.DamageDict)
  201. {
  202. // CollectionsMarshal my beloved.
  203. if (!dict.TryGetValue(type, out var oldValue))
  204. continue;
  205. var newValue = FixedPoint2.Max(FixedPoint2.Zero, oldValue + value);
  206. if (newValue == oldValue)
  207. continue;
  208. dict[type] = newValue;
  209. delta.DamageDict[type] = newValue - oldValue;
  210. }
  211. if (delta.DamageDict.Count > 0)
  212. DamageChanged(uid.Value, damageable, delta, interruptsDoAfters, origin);
  213. return delta;
  214. }
  215. /// <summary>
  216. /// Applies the two univeral "All" modifiers, if set.
  217. /// Individual damage source modifiers are set in their respective code.
  218. /// </summary>
  219. /// <param name="damage">The damage to be changed.</param>
  220. public DamageSpecifier ApplyUniversalAllModifiers(DamageSpecifier damage)
  221. {
  222. // Checks for changes first since they're unlikely in normal play.
  223. if (UniversalAllDamageModifier == 1f && UniversalAllHealModifier == 1f)
  224. return damage;
  225. foreach (var (key, value) in damage.DamageDict)
  226. {
  227. if (value == 0)
  228. continue;
  229. if (value > 0)
  230. {
  231. damage.DamageDict[key] *= UniversalAllDamageModifier;
  232. continue;
  233. }
  234. if (value < 0)
  235. {
  236. damage.DamageDict[key] *= UniversalAllHealModifier;
  237. }
  238. }
  239. return damage;
  240. }
  241. /// <summary>
  242. /// Sets all damage types supported by a <see cref="DamageableComponent"/> to the specified value.
  243. /// </summary>
  244. /// <remakrs>
  245. /// Does nothing If the given damage value is negative.
  246. /// </remakrs>
  247. public void SetAllDamage(EntityUid uid, DamageableComponent component, FixedPoint2 newValue)
  248. {
  249. if (newValue < 0)
  250. {
  251. // invalid value
  252. return;
  253. }
  254. foreach (var type in component.Damage.DamageDict.Keys)
  255. {
  256. component.Damage.DamageDict[type] = newValue;
  257. }
  258. // Setting damage does not count as 'dealing' damage, even if it is set to a larger value, so we pass an
  259. // empty damage delta.
  260. DamageChanged(uid, component, new DamageSpecifier());
  261. }
  262. public void SetDamageModifierSetId(EntityUid uid, string damageModifierSetId, DamageableComponent? comp = null)
  263. {
  264. if (!_damageableQuery.Resolve(uid, ref comp))
  265. return;
  266. comp.DamageModifierSetId = damageModifierSetId;
  267. Dirty(uid, comp);
  268. }
  269. private void DamageableGetState(EntityUid uid, DamageableComponent component, ref ComponentGetState args)
  270. {
  271. if (_netMan.IsServer)
  272. {
  273. args.State = new DamageableComponentState(component.Damage.DamageDict, component.DamageContainerID, component.DamageModifierSetId, component.HealthBarThreshold);
  274. }
  275. else
  276. {
  277. // avoid mispredicting damage on newly spawned entities.
  278. args.State = new DamageableComponentState(component.Damage.DamageDict.ShallowClone(), component.DamageContainerID, component.DamageModifierSetId, component.HealthBarThreshold);
  279. }
  280. }
  281. private void OnIrradiated(EntityUid uid, DamageableComponent component, OnIrradiatedEvent args)
  282. {
  283. var damageValue = FixedPoint2.New(args.TotalRads);
  284. // Radiation should really just be a damage group instead of a list of types.
  285. DamageSpecifier damage = new();
  286. foreach (var typeId in component.RadiationDamageTypeIDs)
  287. {
  288. damage.DamageDict.Add(typeId, damageValue);
  289. }
  290. TryChangeDamage(uid, damage, interruptsDoAfters: false);
  291. }
  292. private void OnRejuvenate(EntityUid uid, DamageableComponent component, RejuvenateEvent args)
  293. {
  294. TryComp<MobThresholdsComponent>(uid, out var thresholds);
  295. _mobThreshold.SetAllowRevives(uid, true, thresholds); // do this so that the state changes when we set the damage
  296. SetAllDamage(uid, component, 0);
  297. _mobThreshold.SetAllowRevives(uid, false, thresholds);
  298. }
  299. private void DamageableHandleState(EntityUid uid, DamageableComponent component, ref ComponentHandleState args)
  300. {
  301. if (args.Current is not DamageableComponentState state)
  302. {
  303. return;
  304. }
  305. component.DamageContainerID = state.DamageContainerId;
  306. component.DamageModifierSetId = state.ModifierSetId;
  307. component.HealthBarThreshold = state.HealthBarThreshold;
  308. // Has the damage actually changed?
  309. DamageSpecifier newDamage = new() { DamageDict = new(state.DamageDict) };
  310. var delta = component.Damage - newDamage;
  311. delta.TrimZeros();
  312. if (!delta.Empty)
  313. {
  314. component.Damage = newDamage;
  315. DamageChanged(uid, component, delta);
  316. }
  317. }
  318. }
  319. /// <summary>
  320. /// Raised before damage is done, so stuff can cancel it if necessary.
  321. /// </summary>
  322. [ByRefEvent]
  323. public record struct BeforeDamageChangedEvent(DamageSpecifier Damage, EntityUid? Origin = null, bool Cancelled = false);
  324. /// <summary>
  325. /// Raised on an entity when damage is about to be dealt,
  326. /// in case anything else needs to modify it other than the base
  327. /// damageable component.
  328. ///
  329. /// For example, armor.
  330. /// </summary>
  331. public sealed class DamageModifyEvent : EntityEventArgs, IInventoryRelayEvent
  332. {
  333. // Whenever locational damage is a thing, this should just check only that bit of armour.
  334. public SlotFlags TargetSlots { get; } = ~SlotFlags.POCKET;
  335. public readonly DamageSpecifier OriginalDamage;
  336. public DamageSpecifier Damage;
  337. public EntityUid? Origin;
  338. public DamageModifyEvent(DamageSpecifier damage, EntityUid? origin = null)
  339. {
  340. OriginalDamage = damage;
  341. Damage = damage;
  342. Origin = origin;
  343. }
  344. }
  345. public sealed class DamageChangedEvent : EntityEventArgs
  346. {
  347. /// <summary>
  348. /// This is the component whose damage was changed.
  349. /// </summary>
  350. /// <remarks>
  351. /// Given that nearly every component that cares about a change in the damage, needs to know the
  352. /// current damage values, directly passing this information prevents a lot of duplicate
  353. /// Owner.TryGetComponent() calls.
  354. /// </remarks>
  355. public readonly DamageableComponent Damageable;
  356. /// <summary>
  357. /// The amount by which the damage has changed. If the damage was set directly to some number, this will be
  358. /// null.
  359. /// </summary>
  360. public readonly DamageSpecifier? DamageDelta;
  361. /// <summary>
  362. /// Was any of the damage change dealing damage, or was it all healing?
  363. /// </summary>
  364. public readonly bool DamageIncreased;
  365. /// <summary>
  366. /// Does this event interrupt DoAfters?
  367. /// Note: As provided in the constructor, this *does not* account for DamageIncreased.
  368. /// As written into the event, this *does* account for DamageIncreased.
  369. /// </summary>
  370. public readonly bool InterruptsDoAfters;
  371. /// <summary>
  372. /// Contains the entity which caused the change in damage, if any was responsible.
  373. /// </summary>
  374. public readonly EntityUid? Origin;
  375. public DamageChangedEvent(DamageableComponent damageable, DamageSpecifier? damageDelta, bool interruptsDoAfters, EntityUid? origin)
  376. {
  377. Damageable = damageable;
  378. DamageDelta = damageDelta;
  379. Origin = origin;
  380. if (DamageDelta == null)
  381. return;
  382. foreach (var damageChange in DamageDelta.DamageDict.Values)
  383. {
  384. if (damageChange > 0)
  385. {
  386. DamageIncreased = true;
  387. break;
  388. }
  389. }
  390. InterruptsDoAfters = interruptsDoAfters && DamageIncreased;
  391. }
  392. }
  393. }