1
0

SuicideSystem.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using Content.Server.Ghost;
  2. using Content.Shared.Administration.Logs;
  3. using Content.Shared.Chat;
  4. using Content.Shared.Damage;
  5. using Content.Shared.Database;
  6. using Content.Shared.Hands.Components;
  7. using Content.Shared.Interaction.Events;
  8. using Content.Shared.Item;
  9. using Content.Shared.Mind;
  10. using Content.Shared.Mind.Components;
  11. using Content.Shared.Mobs.Components;
  12. using Content.Shared.Mobs.Systems;
  13. using Content.Shared.Popups;
  14. using Content.Shared.Tag;
  15. using Robust.Shared.Player;
  16. namespace Content.Server.Chat;
  17. public sealed class SuicideSystem : EntitySystem
  18. {
  19. [Dependency] private readonly EntityLookupSystem _entityLookupSystem = default!;
  20. [Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
  21. [Dependency] private readonly TagSystem _tagSystem = default!;
  22. [Dependency] private readonly MobStateSystem _mobState = default!;
  23. [Dependency] private readonly SharedPopupSystem _popup = default!;
  24. [Dependency] private readonly GhostSystem _ghostSystem = default!;
  25. [Dependency] private readonly SharedSuicideSystem _suicide = default!;
  26. public override void Initialize()
  27. {
  28. base.Initialize();
  29. SubscribeLocalEvent<DamageableComponent, SuicideEvent>(OnDamageableSuicide);
  30. SubscribeLocalEvent<MobStateComponent, SuicideEvent>(OnEnvironmentalSuicide);
  31. SubscribeLocalEvent<MindContainerComponent, SuicideGhostEvent>(OnSuicideGhost);
  32. }
  33. /// <summary>
  34. /// Calling this function will attempt to kill the user by suiciding on objects in the surrounding area
  35. /// or by applying a lethal amount of damage to the user with the default method.
  36. /// Used when writing /suicide
  37. /// </summary>
  38. public bool Suicide(EntityUid victim)
  39. {
  40. // Can't suicide if we're already dead
  41. if (!TryComp<MobStateComponent>(victim, out var mobState) || _mobState.IsDead(victim, mobState))
  42. return false;
  43. _adminLogger.Add(LogType.Mind, $"{EntityManager.ToPrettyString(victim):player} is attempting to suicide");
  44. ICommonSession? session = null;
  45. if (TryComp<ActorComponent>(victim, out var actor))
  46. session = actor.PlayerSession;
  47. var suicideGhostEvent = new SuicideGhostEvent(victim);
  48. RaiseLocalEvent(victim, suicideGhostEvent);
  49. // Suicide is considered a fail if the user wasn't able to ghost
  50. // Suiciding with the CannotSuicide tag will ghost the player but not kill the body
  51. if (!suicideGhostEvent.Handled || _tagSystem.HasTag(victim, "CannotSuicide"))
  52. return false;
  53. var suicideEvent = new SuicideEvent(victim);
  54. RaiseLocalEvent(victim, suicideEvent);
  55. // Since the player is already dead the log will not contain their username.
  56. if (session != null)
  57. {
  58. _adminLogger.Add(LogType.Mind, $"{session:player} suicided.");
  59. }
  60. else
  61. {
  62. _adminLogger.Add(LogType.Mind, $"{EntityManager.ToPrettyString(victim):player} suicided.");
  63. }
  64. return true;
  65. }
  66. /// <summary>
  67. /// Event subscription created to handle the ghosting aspect relating to suicides
  68. /// Mainly useful when you can raise an event in Shared and can't call Suicide() directly
  69. /// </summary>
  70. private void OnSuicideGhost(Entity<MindContainerComponent> victim, ref SuicideGhostEvent args)
  71. {
  72. if (args.Handled)
  73. return;
  74. if (victim.Comp.Mind == null)
  75. return;
  76. if (!TryComp<MindComponent>(victim.Comp.Mind, out var mindComponent))
  77. return;
  78. // CannotSuicide tag will allow the user to ghost, but also return to their mind
  79. // This is kind of weird, not sure what it applies to?
  80. if (_tagSystem.HasTag(victim, "CannotSuicide"))
  81. args.CanReturnToBody = true;
  82. if (_ghostSystem.OnGhostAttempt(victim.Comp.Mind.Value, args.CanReturnToBody, mind: mindComponent))
  83. args.Handled = true;
  84. }
  85. /// <summary>
  86. /// Raise event to attempt to use held item, or surrounding entities to attempt to commit suicide
  87. /// </summary>
  88. private void OnEnvironmentalSuicide(Entity<MobStateComponent> victim, ref SuicideEvent args)
  89. {
  90. if (args.Handled || _mobState.IsCritical(victim))
  91. return;
  92. var suicideByEnvironmentEvent = new SuicideByEnvironmentEvent(victim);
  93. // Try to suicide by raising an event on the held item
  94. if (EntityManager.TryGetComponent(victim, out HandsComponent? handsComponent)
  95. && handsComponent.ActiveHandEntity is { } item)
  96. {
  97. RaiseLocalEvent(item, suicideByEnvironmentEvent);
  98. if (suicideByEnvironmentEvent.Handled)
  99. {
  100. args.Handled = suicideByEnvironmentEvent.Handled;
  101. return;
  102. }
  103. }
  104. // Try to suicide by nearby entities, like Microwaves or Crematoriums, by raising an event on it
  105. // Returns upon being handled by any entity
  106. var itemQuery = GetEntityQuery<ItemComponent>();
  107. foreach (var entity in _entityLookupSystem.GetEntitiesInRange(victim, 1, LookupFlags.Approximate | LookupFlags.Static))
  108. {
  109. // Skip any nearby items that can be picked up, we already checked the active held item above
  110. if (itemQuery.HasComponent(entity))
  111. continue;
  112. RaiseLocalEvent(entity, suicideByEnvironmentEvent);
  113. if (!suicideByEnvironmentEvent.Handled)
  114. continue;
  115. args.Handled = suicideByEnvironmentEvent.Handled;
  116. return;
  117. }
  118. }
  119. /// <summary>
  120. /// Default suicide behavior for any kind of entity that can take damage
  121. /// </summary>
  122. private void OnDamageableSuicide(Entity<DamageableComponent> victim, ref SuicideEvent args)
  123. {
  124. if (args.Handled)
  125. return;
  126. var othersMessage = Loc.GetString("suicide-command-default-text-others", ("name", victim));
  127. _popup.PopupEntity(othersMessage, victim, Filter.PvsExcept(victim), true);
  128. var selfMessage = Loc.GetString("suicide-command-default-text-self");
  129. _popup.PopupEntity(selfMessage, victim, victim);
  130. if (args.DamageSpecifier != null)
  131. {
  132. _suicide.ApplyLethalDamage(victim, args.DamageSpecifier);
  133. args.Handled = true;
  134. return;
  135. }
  136. args.DamageType ??= "Bloodloss";
  137. _suicide.ApplyLethalDamage(victim, args.DamageType);
  138. args.Handled = true;
  139. }
  140. }