SleepingSystem.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. using Content.Shared.Actions;
  2. using Content.Shared.Buckle.Components;
  3. using Content.Shared.Damage;
  4. using Content.Shared.Damage.Events;
  5. using Content.Shared.Damage.ForceSay;
  6. using Content.Shared.Emoting;
  7. using Content.Shared.Examine;
  8. using Content.Shared.Eye.Blinding.Systems;
  9. using Content.Shared.IdentityManagement;
  10. using Content.Shared.Interaction;
  11. using Content.Shared.Interaction.Events;
  12. using Content.Shared.Mobs;
  13. using Content.Shared.Mobs.Components;
  14. using Content.Shared.Pointing;
  15. using Content.Shared.Popups;
  16. using Content.Shared.Slippery;
  17. using Content.Shared.Sound;
  18. using Content.Shared.Sound.Components;
  19. using Content.Shared.Speech;
  20. using Content.Shared.StatusEffect;
  21. using Content.Shared.Stunnable;
  22. using Content.Shared.Traits.Assorted;
  23. using Content.Shared.Verbs;
  24. using Robust.Shared.Audio.Systems;
  25. using Robust.Shared.Prototypes;
  26. using Robust.Shared.Timing;
  27. using Content.Shared.Civ14.SleepZone;
  28. namespace Content.Shared.Bed.Sleep;
  29. public sealed partial class SleepingSystem : EntitySystem
  30. {
  31. [Dependency] private readonly IGameTiming _gameTiming = default!;
  32. [Dependency] private readonly SharedActionsSystem _actionsSystem = default!;
  33. [Dependency] private readonly BlindableSystem _blindableSystem = default!;
  34. [Dependency] private readonly SharedPopupSystem _popupSystem = default!;
  35. [Dependency] private readonly SharedAudioSystem _audio = default!;
  36. [Dependency] private readonly SharedEmitSoundSystem _emitSound = default!;
  37. [Dependency] private readonly StatusEffectsSystem _statusEffectsSystem = default!;
  38. [Dependency] private readonly SleepZoneSystem _sleepzone = default!;
  39. [Dependency] private readonly ILogManager _log = default!;
  40. public static readonly EntProtoId SleepActionId = "ActionSleep";
  41. public static readonly EntProtoId WakeActionId = "ActionWake";
  42. private ISawmill _sawmill = default!;
  43. public override void Initialize()
  44. {
  45. base.Initialize();
  46. SubscribeLocalEvent<ActionsContainerComponent, SleepActionEvent>(OnBedSleepAction);
  47. SubscribeLocalEvent<MobStateComponent, SleepStateChangedEvent>(OnSleepStateChanged);
  48. SubscribeLocalEvent<MobStateComponent, WakeActionEvent>(OnWakeAction);
  49. SubscribeLocalEvent<MobStateComponent, SleepActionEvent>(OnSleepAction);
  50. SubscribeLocalEvent<SleepingComponent, DamageChangedEvent>(OnDamageChanged);
  51. SubscribeLocalEvent<SleepingComponent, MobStateChangedEvent>(OnMobStateChanged);
  52. SubscribeLocalEvent<SleepingComponent, MapInitEvent>(OnMapInit);
  53. SubscribeLocalEvent<SleepingComponent, SpeakAttemptEvent>(OnSpeakAttempt);
  54. SubscribeLocalEvent<SleepingComponent, CanSeeAttemptEvent>(OnSeeAttempt);
  55. SubscribeLocalEvent<SleepingComponent, PointAttemptEvent>(OnPointAttempt);
  56. SubscribeLocalEvent<SleepingComponent, SlipAttemptEvent>(OnSlip);
  57. SubscribeLocalEvent<SleepingComponent, ConsciousAttemptEvent>(OnConsciousAttempt);
  58. SubscribeLocalEvent<SleepingComponent, ExaminedEvent>(OnExamined);
  59. SubscribeLocalEvent<SleepingComponent, GetVerbsEvent<AlternativeVerb>>(AddWakeVerb);
  60. SubscribeLocalEvent<SleepingComponent, InteractHandEvent>(OnInteractHand);
  61. SubscribeLocalEvent<ForcedSleepingComponent, ComponentInit>(OnInit);
  62. SubscribeLocalEvent<SleepingComponent, UnbuckleAttemptEvent>(OnUnbuckleAttempt);
  63. SubscribeLocalEvent<SleepingComponent, EmoteAttemptEvent>(OnEmoteAttempt);
  64. SubscribeLocalEvent<SleepingComponent, BeforeForceSayEvent>(OnChangeForceSay, after: new[] { typeof(PainNumbnessSystem) });
  65. _sawmill = _log.GetSawmill("sleeping");
  66. }
  67. private void OnUnbuckleAttempt(Entity<SleepingComponent> ent, ref UnbuckleAttemptEvent args)
  68. {
  69. // TODO is this necessary?
  70. // Shouldn't the interaction have already been blocked by a general interaction check?
  71. if (ent.Owner == args.User)
  72. //if mob has the sleepzone component, do not wake up when moved
  73. if (!TryComp<SleepZoneComponent>(ent, out var sleepZone))
  74. {
  75. args.Cancelled = true;
  76. }
  77. }
  78. private void OnBedSleepAction(Entity<ActionsContainerComponent> ent, ref SleepActionEvent args)
  79. {
  80. TrySleeping(args.Performer);
  81. }
  82. private void OnWakeAction(Entity<MobStateComponent> ent, ref WakeActionEvent args)
  83. {
  84. if (TryWakeWithCooldown(ent.Owner))
  85. args.Handled = true;
  86. }
  87. private void OnSleepAction(Entity<MobStateComponent> ent, ref SleepActionEvent args)
  88. {
  89. TrySleeping((ent, ent.Comp));
  90. }
  91. /// <summary>
  92. /// when sleeping component is added or removed, we do some stuff with other components.
  93. /// </summary>
  94. private void OnSleepStateChanged(Entity<MobStateComponent> ent, ref SleepStateChangedEvent args)
  95. {
  96. if (args.FellAsleep)
  97. {
  98. // Expiring status effects would remove the components needed for sleeping
  99. _statusEffectsSystem.TryRemoveStatusEffect(ent.Owner, "Stun");
  100. _statusEffectsSystem.TryRemoveStatusEffect(ent.Owner, "KnockedDown");
  101. EnsureComp<StunnedComponent>(ent);
  102. EnsureComp<KnockedDownComponent>(ent);
  103. if (TryComp<SleepEmitSoundComponent>(ent, out var sleepSound))
  104. {
  105. var emitSound = EnsureComp<SpamEmitSoundComponent>(ent);
  106. if (HasComp<SnoringComponent>(ent))
  107. {
  108. emitSound.Sound = sleepSound.Snore;
  109. }
  110. emitSound.MinInterval = sleepSound.Interval;
  111. emitSound.MaxInterval = sleepSound.MaxInterval;
  112. emitSound.PopUp = sleepSound.PopUp;
  113. Dirty(ent.Owner, emitSound);
  114. }
  115. return;
  116. }
  117. RemComp<StunnedComponent>(ent);
  118. RemComp<KnockedDownComponent>(ent);
  119. RemComp<SpamEmitSoundComponent>(ent);
  120. }
  121. private void OnSpeakAttempt(Entity<SleepingComponent> ent, ref SpeakAttemptEvent args)
  122. {
  123. // TODO reduce duplication of this behavior with MobStateSystem somehow
  124. if (HasComp<AllowNextCritSpeechComponent>(ent))
  125. {
  126. RemCompDeferred<AllowNextCritSpeechComponent>(ent);
  127. return;
  128. }
  129. args.Cancel();
  130. }
  131. private void OnSeeAttempt(Entity<SleepingComponent> ent, ref CanSeeAttemptEvent args)
  132. {
  133. if (ent.Comp.LifeStage <= ComponentLifeStage.Running)
  134. args.Cancel();
  135. }
  136. private void OnPointAttempt(Entity<SleepingComponent> ent, ref PointAttemptEvent args)
  137. {
  138. args.Cancel();
  139. }
  140. private void OnSlip(Entity<SleepingComponent> ent, ref SlipAttemptEvent args)
  141. {
  142. args.NoSlip = true;
  143. }
  144. private void OnConsciousAttempt(Entity<SleepingComponent> ent, ref ConsciousAttemptEvent args)
  145. {
  146. args.Cancelled = true;
  147. }
  148. private void OnExamined(Entity<SleepingComponent> ent, ref ExaminedEvent args)
  149. {
  150. if (args.IsInDetailsRange)
  151. {
  152. args.PushMarkup(Loc.GetString("sleep-examined", ("target", Identity.Entity(ent, EntityManager))));
  153. }
  154. }
  155. private void AddWakeVerb(Entity<SleepingComponent> ent, ref GetVerbsEvent<AlternativeVerb> args)
  156. {
  157. if (!args.CanInteract || !args.CanAccess)
  158. return;
  159. var target = args.Target;
  160. var user = args.User;
  161. AlternativeVerb verb = new()
  162. {
  163. Act = () =>
  164. {
  165. TryWakeWithCooldown((ent, ent.Comp), user: user);
  166. },
  167. Text = Loc.GetString("action-name-wake"),
  168. Priority = 2
  169. };
  170. args.Verbs.Add(verb);
  171. }
  172. /// <summary>
  173. /// When you click on a sleeping person with an empty hand, try to wake them.
  174. /// </summary>
  175. private void OnInteractHand(Entity<SleepingComponent> ent, ref InteractHandEvent args)
  176. {
  177. args.Handled = true;
  178. TryWakeWithCooldown((ent, ent.Comp), args.User);
  179. }
  180. /// <summary>
  181. /// Wake up on taking an instance of damage at least the value of WakeThreshold.
  182. /// </summary>
  183. private void OnDamageChanged(Entity<SleepingComponent> ent, ref DamageChangedEvent args)
  184. {
  185. if (!args.DamageIncreased || args.DamageDelta == null)
  186. return;
  187. /* Shitmed Change Start - Surgery needs this, sorry! If the nocturine gamers get too feisty
  188. I'll probably just increase the threshold */
  189. if (args.DamageDelta.GetTotal() >= ent.Comp.WakeThreshold
  190. && !HasComp<ForcedSleepingComponent>(ent))
  191. TryWaking((ent, ent.Comp));
  192. // Shitmed Change End
  193. }
  194. /// <summary>
  195. /// In crit, we wake up if we are not being forced to sleep.
  196. /// And, you can't sleep when dead...
  197. /// </summary>
  198. private void OnMobStateChanged(Entity<SleepingComponent> ent, ref MobStateChangedEvent args)
  199. {
  200. if (args.NewMobState == MobState.Dead)
  201. {
  202. RemComp<SpamEmitSoundComponent>(ent);
  203. RemComp<SleepingComponent>(ent);
  204. return;
  205. }
  206. if (TryComp<SpamEmitSoundComponent>(ent, out var spam))
  207. _emitSound.SetEnabled((ent, spam), args.NewMobState == MobState.Alive);
  208. }
  209. private void OnInit(Entity<ForcedSleepingComponent> ent, ref ComponentInit args)
  210. {
  211. TrySleeping(ent.Owner);
  212. }
  213. private void Wake(Entity<SleepingComponent> ent)
  214. {
  215. RemComp<SleepingComponent>(ent);
  216. _actionsSystem.RemoveAction(ent, ent.Comp.WakeAction);
  217. var ev = new SleepStateChangedEvent(false);
  218. RaiseLocalEvent(ent, ref ev);
  219. _blindableSystem.UpdateIsBlind(ent.Owner);
  220. }
  221. /// <summary>
  222. /// Try sleeping. Only mobs can sleep.
  223. /// </summary>
  224. public bool TrySleeping(Entity<MobStateComponent?> ent)
  225. {
  226. // Use Resolve to get the MobStateComponent if it exists, otherwise return false.
  227. if (!Resolve(ent, ref ent.Comp, logMissing: false))
  228. return false;
  229. // Raise an event to see if anything wants to prevent sleeping.
  230. var tryingToSleepEvent = new TryingToSleepEvent(ent);
  231. RaiseLocalEvent(ent.Owner, ref tryingToSleepEvent); // Use EntityUid (ent.Owner)
  232. if (tryingToSleepEvent.Cancelled)
  233. return false;
  234. // Specific logic if the entity also has a SleepZoneComponent.
  235. // Use TryComp with the original Entity<T> struct 'ent'.
  236. if (TryComp<SleepZoneComponent>(ent, out var sleepZone))
  237. {
  238. _sleepzone.StartSleep(ent);
  239. }
  240. // Ensure the SleepingComponent exists and get a reference to it.
  241. // Use ent.Owner (the EntityUid) with EnsureComp.
  242. var sleepComp = EnsureComp<SleepingComponent>(ent.Owner);
  243. // Raise the state changed event *after* the component is added. Use ent.Owner (the EntityUid).
  244. var ev = new SleepStateChangedEvent(true);
  245. RaiseLocalEvent(ent.Owner, ref ev);
  246. _blindableSystem.UpdateIsBlind(ent.Owner);
  247. _actionsSystem.AddAction(ent.Owner, ref sleepComp.WakeAction, WakeActionId, ent.Owner);
  248. return true;
  249. }
  250. private void OnMapInit(Entity<SleepingComponent> ent, ref MapInitEvent args)
  251. {
  252. var ev = new SleepStateChangedEvent(true);
  253. RaiseLocalEvent(ent, ref ev);
  254. _blindableSystem.UpdateIsBlind(ent.Owner);
  255. _actionsSystem.AddAction(ent, ref ent.Comp.WakeAction, WakeActionId, ent);
  256. }
  257. /// <summary>
  258. /// Tries to wake up <paramref name="ent"/>, with a cooldown between attempts to prevent spam.
  259. /// </summary>
  260. public bool TryWakeWithCooldown(Entity<SleepingComponent?> ent, EntityUid? user = null)
  261. {
  262. if (!Resolve(ent, ref ent.Comp, false))
  263. return false;
  264. var curTime = _gameTiming.CurTime;
  265. if (curTime < ent.Comp.CooldownEnd)
  266. return false;
  267. ent.Comp.CooldownEnd = curTime + ent.Comp.Cooldown;
  268. Dirty(ent, ent.Comp);
  269. return TryWaking(ent, user: user);
  270. }
  271. /// <summary>
  272. /// Try to wake up <paramref name="ent"/>.
  273. /// </summary>
  274. public bool TryWaking(Entity<SleepingComponent?> ent, bool force = false, EntityUid? user = null)
  275. {
  276. if (!Resolve(ent, ref ent.Comp, false))
  277. return false;
  278. if (!force && HasComp<ForcedSleepingComponent>(ent))
  279. {
  280. if (user != null)
  281. {
  282. _audio.PlayPredicted(ent.Comp.WakeAttemptSound, ent, user);
  283. _popupSystem.PopupClient(Loc.GetString("wake-other-failure", ("target", Identity.Entity(ent, EntityManager))), ent, user, PopupType.SmallCaution);
  284. }
  285. return false;
  286. }
  287. if (user != null)
  288. {
  289. _audio.PlayPredicted(ent.Comp.WakeAttemptSound, ent, user);
  290. _popupSystem.PopupClient(Loc.GetString("wake-other-success", ("target", Identity.Entity(ent, EntityManager))), ent, user);
  291. }
  292. Wake((ent, ent.Comp));
  293. if (TryComp<SleepZoneComponent>(ent, out var sleepZone))
  294. {
  295. _sleepzone.WakeUp(ent);
  296. }
  297. return true;
  298. }
  299. /// <summary>
  300. /// Prevents the use of emote actions while sleeping
  301. /// </summary>
  302. public void OnEmoteAttempt(Entity<SleepingComponent> ent, ref EmoteAttemptEvent args)
  303. {
  304. args.Cancel();
  305. }
  306. private void OnChangeForceSay(Entity<SleepingComponent> ent, ref BeforeForceSayEvent args)
  307. {
  308. args.Prefix = ent.Comp.ForceSaySleepDataset;
  309. }
  310. }
  311. public sealed partial class SleepActionEvent : InstantActionEvent;
  312. public sealed partial class WakeActionEvent : InstantActionEvent;
  313. /// <summary>
  314. /// Raised on an entity when they fall asleep or wake up.
  315. /// </summary>
  316. [ByRefEvent]
  317. public record struct SleepStateChangedEvent(bool FellAsleep);