1
0

StaminaSystem.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using System.Linq;
  2. using Content.Shared.Administration.Logs;
  3. using Content.Shared.Alert;
  4. using Content.Shared.CombatMode;
  5. using Content.Shared.Damage.Components;
  6. using Content.Shared.Damage.Events;
  7. using Content.Shared.Database;
  8. using Content.Shared.Effects;
  9. using Content.Shared.IdentityManagement;
  10. using Content.Shared.Popups;
  11. using Content.Shared.Projectiles;
  12. using Content.Shared.Rejuvenate;
  13. using Content.Shared.Rounding;
  14. using Content.Shared.Stunnable;
  15. using Content.Shared.Throwing;
  16. using Content.Shared.Weapons.Melee.Events;
  17. using JetBrains.Annotations;
  18. using Robust.Shared.Audio;
  19. using Robust.Shared.Audio.Systems;
  20. using Robust.Shared.Network;
  21. using Robust.Shared.Player;
  22. using Robust.Shared.Random;
  23. using Robust.Shared.Timing;
  24. namespace Content.Shared.Damage.Systems;
  25. public sealed partial class StaminaSystem : EntitySystem
  26. {
  27. [Dependency] private readonly IGameTiming _timing = default!;
  28. [Dependency] private readonly INetManager _net = default!;
  29. [Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
  30. [Dependency] private readonly AlertsSystem _alerts = default!;
  31. [Dependency] private readonly MetaDataSystem _metadata = default!;
  32. [Dependency] private readonly SharedColorFlashEffectSystem _color = default!;
  33. [Dependency] private readonly SharedStunSystem _stunSystem = default!;
  34. [Dependency] private readonly SharedAudioSystem _audio = default!;
  35. /// <summary>
  36. /// How much of a buffer is there between the stun duration and when stuns can be re-applied.
  37. /// </summary>
  38. private static readonly TimeSpan StamCritBufferTime = TimeSpan.FromSeconds(3f);
  39. public override void Initialize()
  40. {
  41. base.Initialize();
  42. InitializeModifier();
  43. SubscribeLocalEvent<StaminaComponent, ComponentStartup>(OnStartup);
  44. SubscribeLocalEvent<StaminaComponent, ComponentShutdown>(OnShutdown);
  45. SubscribeLocalEvent<StaminaComponent, AfterAutoHandleStateEvent>(OnStamHandleState);
  46. SubscribeLocalEvent<StaminaComponent, DisarmedEvent>(OnDisarmed);
  47. SubscribeLocalEvent<StaminaComponent, RejuvenateEvent>(OnRejuvenate);
  48. SubscribeLocalEvent<StaminaDamageOnEmbedComponent, EmbedEvent>(OnProjectileEmbed);
  49. SubscribeLocalEvent<StaminaDamageOnCollideComponent, ProjectileHitEvent>(OnProjectileHit);
  50. SubscribeLocalEvent<StaminaDamageOnCollideComponent, ThrowDoHitEvent>(OnThrowHit);
  51. SubscribeLocalEvent<StaminaDamageOnHitComponent, MeleeHitEvent>(OnMeleeHit);
  52. }
  53. private void OnStamHandleState(EntityUid uid, StaminaComponent component, ref AfterAutoHandleStateEvent args)
  54. {
  55. if (component.Critical)
  56. EnterStamCrit(uid, component);
  57. else
  58. {
  59. if (component.StaminaDamage > 0f)
  60. EnsureComp<ActiveStaminaComponent>(uid);
  61. ExitStamCrit(uid, component);
  62. }
  63. }
  64. private void OnShutdown(EntityUid uid, StaminaComponent component, ComponentShutdown args)
  65. {
  66. if (MetaData(uid).EntityLifeStage < EntityLifeStage.Terminating)
  67. {
  68. RemCompDeferred<ActiveStaminaComponent>(uid);
  69. }
  70. _alerts.ClearAlert(uid, component.StaminaAlert);
  71. }
  72. private void OnStartup(EntityUid uid, StaminaComponent component, ComponentStartup args)
  73. {
  74. SetStaminaAlert(uid, component);
  75. }
  76. [PublicAPI]
  77. public float GetStaminaDamage(EntityUid uid, StaminaComponent? component = null)
  78. {
  79. if (!Resolve(uid, ref component))
  80. return 0f;
  81. var curTime = _timing.CurTime;
  82. var pauseTime = _metadata.GetPauseTime(uid);
  83. return MathF.Max(0f, component.StaminaDamage - MathF.Max(0f, (float)(curTime - (component.NextUpdate + pauseTime)).TotalSeconds * component.Decay));
  84. }
  85. private void OnRejuvenate(EntityUid uid, StaminaComponent component, RejuvenateEvent args)
  86. {
  87. if (component.StaminaDamage >= component.CritThreshold)
  88. {
  89. ExitStamCrit(uid, component);
  90. }
  91. component.StaminaDamage = 0;
  92. RemComp<ActiveStaminaComponent>(uid);
  93. SetStaminaAlert(uid, component);
  94. Dirty(uid, component);
  95. }
  96. private void OnDisarmed(EntityUid uid, StaminaComponent component, DisarmedEvent args)
  97. {
  98. if (args.Handled)
  99. return;
  100. if (component.Critical)
  101. return;
  102. var damage = args.PushProbability * component.CritThreshold;
  103. TakeStaminaDamage(uid, damage, component, source: args.Source);
  104. args.PopupPrefix = "disarm-action-shove-";
  105. args.IsStunned = component.Critical;
  106. args.Handled = true;
  107. }
  108. private void OnMeleeHit(EntityUid uid, StaminaDamageOnHitComponent component, MeleeHitEvent args)
  109. {
  110. if (!args.IsHit ||
  111. !args.HitEntities.Any() ||
  112. component.Damage <= 0f)
  113. {
  114. return;
  115. }
  116. var ev = new StaminaDamageOnHitAttemptEvent();
  117. RaiseLocalEvent(uid, ref ev);
  118. if (ev.Cancelled)
  119. return;
  120. var stamQuery = GetEntityQuery<StaminaComponent>();
  121. var toHit = new List<(EntityUid Entity, StaminaComponent Component)>();
  122. // Split stamina damage between all eligible targets.
  123. foreach (var ent in args.HitEntities)
  124. {
  125. if (!stamQuery.TryGetComponent(ent, out var stam))
  126. continue;
  127. toHit.Add((ent, stam));
  128. }
  129. var hitEvent = new StaminaMeleeHitEvent(toHit);
  130. RaiseLocalEvent(uid, hitEvent);
  131. if (hitEvent.Handled)
  132. return;
  133. var damage = component.Damage;
  134. damage *= hitEvent.Multiplier;
  135. damage += hitEvent.FlatModifier;
  136. foreach (var (ent, comp) in toHit)
  137. {
  138. TakeStaminaDamage(ent, damage / toHit.Count, comp, source: args.User, with: args.Weapon, sound: component.Sound);
  139. }
  140. }
  141. private void OnProjectileHit(EntityUid uid, StaminaDamageOnCollideComponent component, ref ProjectileHitEvent args)
  142. {
  143. OnCollide(uid, component, args.Target);
  144. }
  145. private void OnProjectileEmbed(EntityUid uid, StaminaDamageOnEmbedComponent component, ref EmbedEvent args)
  146. {
  147. if (!TryComp<StaminaComponent>(args.Embedded, out var stamina))
  148. return;
  149. TakeStaminaDamage(args.Embedded, component.Damage, stamina, source: uid);
  150. }
  151. private void OnThrowHit(EntityUid uid, StaminaDamageOnCollideComponent component, ThrowDoHitEvent args)
  152. {
  153. OnCollide(uid, component, args.Target);
  154. }
  155. private void OnCollide(EntityUid uid, StaminaDamageOnCollideComponent component, EntityUid target)
  156. {
  157. // you can't inflict stamina damage on things with no stamina component
  158. // this prevents stun batons from using up charges when throwing it at lockers or lights
  159. if (!HasComp<StaminaComponent>(target))
  160. return;
  161. var ev = new StaminaDamageOnHitAttemptEvent();
  162. RaiseLocalEvent(uid, ref ev);
  163. if (ev.Cancelled)
  164. return;
  165. TakeStaminaDamage(target, component.Damage, source: uid, sound: component.Sound);
  166. }
  167. private void SetStaminaAlert(EntityUid uid, StaminaComponent? component = null)
  168. {
  169. if (!Resolve(uid, ref component, false) || component.Deleted)
  170. return;
  171. var severity = ContentHelpers.RoundToLevels(MathF.Max(0f, component.CritThreshold - component.StaminaDamage), component.CritThreshold, 7);
  172. _alerts.ShowAlert(uid, component.StaminaAlert, (short)severity);
  173. }
  174. /// <summary>
  175. /// Tries to take stamina damage without raising the entity over the crit threshold.
  176. /// </summary>
  177. public bool TryTakeStamina(EntityUid uid, float value, StaminaComponent? component = null, EntityUid? source = null, EntityUid? with = null)
  178. {
  179. // Something that has no Stamina component automatically passes stamina checks
  180. if (!Resolve(uid, ref component, false))
  181. return true;
  182. var oldStam = component.StaminaDamage;
  183. if (oldStam + value > component.CritThreshold || component.Critical)
  184. return false;
  185. TakeStaminaDamage(uid, value, component, source, with, visual: false);
  186. return true;
  187. }
  188. public void TakeStaminaDamage(EntityUid uid, float value, StaminaComponent? component = null,
  189. EntityUid? source = null, EntityUid? with = null, bool visual = true, SoundSpecifier? sound = null,
  190. bool shouldLog = true) // stalker-changes
  191. {
  192. if (!Resolve(uid, ref component, false))
  193. return;
  194. var ev = new BeforeStaminaDamageEvent(value);
  195. RaiseLocalEvent(uid, ref ev);
  196. if (ev.Cancelled)
  197. return;
  198. // Have we already reached the point of max stamina damage?
  199. if (component.Critical)
  200. return;
  201. var oldDamage = component.StaminaDamage;
  202. component.StaminaDamage = MathF.Max(0f, component.StaminaDamage + value);
  203. // Reset the decay cooldown upon taking damage.
  204. if (oldDamage < component.StaminaDamage)
  205. {
  206. var nextUpdate = _timing.CurTime + TimeSpan.FromSeconds(component.Cooldown);
  207. if (component.NextUpdate < nextUpdate)
  208. component.NextUpdate = nextUpdate;
  209. }
  210. var slowdownThreshold = component.SlowdownThreshold; // stalker-changes
  211. // If we go above n% then apply slowdown
  212. if (oldDamage < slowdownThreshold &&
  213. component.StaminaDamage > slowdownThreshold)
  214. {
  215. _stunSystem.TrySlowdown(uid, TimeSpan.FromSeconds(3), true, 0.8f, 0.8f);
  216. }
  217. SetStaminaAlert(uid, component);
  218. if (!component.Critical)
  219. {
  220. if (component.StaminaDamage >= component.CritThreshold)
  221. {
  222. EnterStamCrit(uid, component);
  223. }
  224. }
  225. else
  226. {
  227. if (component.StaminaDamage < component.CritThreshold)
  228. {
  229. ExitStamCrit(uid, component);
  230. }
  231. }
  232. EnsureComp<ActiveStaminaComponent>(uid);
  233. Dirty(uid, component);
  234. if (value <= 0)
  235. return;
  236. if (source != null && shouldLog) // stalker-changes
  237. {
  238. _adminLogger.Add(LogType.Stamina, $"{ToPrettyString(source.Value):user} caused {value} stamina damage to {ToPrettyString(uid):target}{(with != null ? $" using {ToPrettyString(with.Value):using}" : "")}");
  239. }
  240. else if (shouldLog) // stalker-changes
  241. {
  242. _adminLogger.Add(LogType.Stamina, $"{ToPrettyString(uid):target} took {value} stamina damage");
  243. }
  244. if (visual)
  245. {
  246. _color.RaiseEffect(Color.Aqua, new List<EntityUid>() { uid }, Filter.Pvs(uid, entityManager: EntityManager));
  247. }
  248. if (_net.IsServer)
  249. {
  250. _audio.PlayPvs(sound, uid);
  251. }
  252. }
  253. public override void Update(float frameTime)
  254. {
  255. base.Update(frameTime);
  256. if (!_timing.IsFirstTimePredicted)
  257. return;
  258. var stamQuery = GetEntityQuery<StaminaComponent>();
  259. var query = EntityQueryEnumerator<ActiveStaminaComponent>();
  260. var curTime = _timing.CurTime;
  261. while (query.MoveNext(out var uid, out _))
  262. {
  263. // Just in case we have active but not stamina we'll check and account for it.
  264. if (!stamQuery.TryGetComponent(uid, out var comp) ||
  265. comp.StaminaDamage <= 0f && !comp.Critical)
  266. {
  267. RemComp<ActiveStaminaComponent>(uid);
  268. continue;
  269. }
  270. // Shouldn't need to consider paused time as we're only iterating non-paused stamina components.
  271. var nextUpdate = comp.NextUpdate;
  272. if (nextUpdate > curTime)
  273. continue;
  274. // We were in crit so come out of it and continue.
  275. if (comp.Critical)
  276. {
  277. ExitStamCrit(uid, comp);
  278. continue;
  279. }
  280. comp.NextUpdate += TimeSpan.FromSeconds(1f);
  281. TakeStaminaDamage(uid, -comp.Decay, comp);
  282. Dirty(uid, comp);
  283. }
  284. }
  285. private void EnterStamCrit(EntityUid uid, StaminaComponent? component = null)
  286. {
  287. if (!Resolve(uid, ref component) ||
  288. component.Critical)
  289. {
  290. return;
  291. }
  292. // To make the difference between a stun and a stamcrit clear
  293. // TODO: Mask?
  294. component.Critical = true;
  295. component.StaminaDamage = component.CritThreshold;
  296. _stunSystem.TryParalyze(uid, component.StunTime, true);
  297. // Give them buffer before being able to be re-stunned
  298. component.NextUpdate = _timing.CurTime + component.StunTime + StamCritBufferTime;
  299. EnsureComp<ActiveStaminaComponent>(uid);
  300. Dirty(uid, component);
  301. _adminLogger.Add(LogType.Stamina, LogImpact.Medium, $"{ToPrettyString(uid):user} entered stamina crit");
  302. }
  303. private void ExitStamCrit(EntityUid uid, StaminaComponent? component = null)
  304. {
  305. if (!Resolve(uid, ref component) ||
  306. !component.Critical)
  307. {
  308. return;
  309. }
  310. component.Critical = false;
  311. component.StaminaDamage = 0f;
  312. component.NextUpdate = _timing.CurTime;
  313. SetStaminaAlert(uid, component);
  314. RemComp<ActiveStaminaComponent>(uid);
  315. Dirty(uid, component);
  316. _adminLogger.Add(LogType.Stamina, LogImpact.Low, $"{ToPrettyString(uid):user} recovered from stamina crit");
  317. }
  318. }
  319. /// <summary>
  320. /// Raised before stamina damage is dealt to allow other systems to cancel it.
  321. /// </summary>
  322. [ByRefEvent]
  323. public record struct BeforeStaminaDamageEvent(float Value, bool Cancelled = false);