From fbb3a06b3df6b1a11b30cd28efd85c26115d2e24 Mon Sep 17 00:00:00 2001 From: Tornado Tech <54727692+Tornado-Technology@users.noreply.github.com> Date: Tue, 31 Dec 2024 00:09:57 +1000 Subject: [PATCH] Fixed AutoCryoSleep --- .../AutoCryoSleep/AutoCryoSleepComponent.cs | 13 ++++ .../AutoCryoSleep/AutoCryoSleepSystem.cs | 65 ++++++++++++++----- .../AutoCryoSleepableComponent.cs | 4 ++ Content.Shared/_CorvaxNext/NextVars.cs | 10 +++ 4 files changed, 75 insertions(+), 17 deletions(-) create mode 100644 Content.Server/_CorvaxNext/AutoCryoSleep/AutoCryoSleepComponent.cs create mode 100644 Content.Server/_CorvaxNext/AutoCryoSleep/AutoCryoSleepableComponent.cs diff --git a/Content.Server/_CorvaxNext/AutoCryoSleep/AutoCryoSleepComponent.cs b/Content.Server/_CorvaxNext/AutoCryoSleep/AutoCryoSleepComponent.cs new file mode 100644 index 00000000000..18ab7e450c9 --- /dev/null +++ b/Content.Server/_CorvaxNext/AutoCryoSleep/AutoCryoSleepComponent.cs @@ -0,0 +1,13 @@ +using Robust.Shared.Prototypes; + +namespace Content.Server._CorvaxNext.AutoCryoSleep; + +[RegisterComponent] +public sealed partial class AutoCryoSleepComponent : Component +{ + [ViewVariables(VVAccess.ReadWrite)] + public TimeSpan Disconnected; + + [ViewVariables(VVAccess.ReadWrite)] + public EntProtoId? EffectId = "JetpackEffect"; +} diff --git a/Content.Server/_CorvaxNext/AutoCryoSleep/AutoCryoSleepSystem.cs b/Content.Server/_CorvaxNext/AutoCryoSleep/AutoCryoSleepSystem.cs index 30028efc61f..8660022aa5a 100644 --- a/Content.Server/_CorvaxNext/AutoCryoSleep/AutoCryoSleepSystem.cs +++ b/Content.Server/_CorvaxNext/AutoCryoSleep/AutoCryoSleepSystem.cs @@ -4,44 +4,74 @@ */ using System.Diagnostics.CodeAnalysis; -using Content.Server.Afk.Events; -using Content.Server.Bed.Cryostorage; -using Content.Server.Mind; +using Content.Shared._CorvaxNext.NextVars; using Content.Shared.Bed.Cryostorage; using Content.Shared.Station.Components; using Robust.Server.Containers; +using Robust.Shared.Configuration; +using Robust.Shared.Player; +using Robust.Shared.Prototypes; using Robust.Shared.Timing; namespace Content.Server._CorvaxNext.AutoCryoSleep; public sealed class AutoCryoSleepSystem : EntitySystem { + [Dependency] private readonly IConfigurationManager _config = default!; [Dependency] private readonly IGameTiming _timing = default!; - [Dependency] private readonly CryostorageSystem _cryostorage = default!; [Dependency] private readonly ContainerSystem _container = default!; - [Dependency] private readonly MindSystem _mind = default!; + + private bool _enabled; + private TimeSpan _disconnectedTime; public override void Initialize() { base.Initialize(); - SubscribeLocalEvent(OnAFK); + SubscribeLocalEvent(OnPlayerAttached); + SubscribeLocalEvent(OnPlayerDetached); + + Subs.CVar(_config, NextVars.AutoCryoSleepEnabled, value => _enabled = value, true); + Subs.CVar(_config, NextVars.AutoCryoSleepTime, value => _disconnectedTime = TimeSpan.FromSeconds(value), true); } - private void OnAFK(ref AFKEvent ev) + public override void Update(float frameTime) { - if (ev.Session.AttachedEntity is not { } entityUid) + if (!_enabled) return; - TryCryoSleep(entityUid); + base.Update(frameTime); + + var disconnectedQuery = EntityQueryEnumerator(); + while (disconnectedQuery.MoveNext(out var uid, out var component)) + { + if (_timing.CurTime < component.Disconnected + _disconnectedTime) + continue; + + TryCryoSleep(uid, component.EffectId); + } } - private void TryCryoSleep(EntityUid targetUid) + private void OnPlayerAttached(Entity ent, ref PlayerAttachedEvent args) { - if (HasComp(targetUid)) + if (!_enabled) + return; + + RemCompDeferred(ent); + } + + private void OnPlayerDetached(Entity ent, ref PlayerDetachedEvent args) + { + if (!_enabled) return; - if (!_mind.TryGetMind(targetUid, out _, out var mindComponent)) + var comp = EnsureComp(ent); + comp.Disconnected = _timing.CurTime; + } + + private void TryCryoSleep(EntityUid targetUid, EntProtoId? effectId = null) + { + if (HasComp(targetUid)) return; if (!TryGetStation(targetUid, out var stationUid)) @@ -53,14 +83,15 @@ private void TryCryoSleep(EntityUid targetUid) continue; // We need only empty cryo sleeps - if (container.ContainedEntities.Count != 0) + if (!_container.CanInsert(targetUid, container)) continue; - var cryostorageContained = AddComp(targetUid); - cryostorageContained.Cryostorage = cryostorageEntity; - cryostorageContained.GracePeriodEndTime = _timing.CurTime; + if (effectId is not null) + Spawn(effectId, Transform(targetUid).Coordinates); + + _container.Insert(targetUid, container); - _cryostorage.HandleEnterCryostorage((targetUid, cryostorageContained), mindComponent.UserId); + RemCompDeferred(targetUid); } } diff --git a/Content.Server/_CorvaxNext/AutoCryoSleep/AutoCryoSleepableComponent.cs b/Content.Server/_CorvaxNext/AutoCryoSleep/AutoCryoSleepableComponent.cs new file mode 100644 index 00000000000..0bbbf3e8fdd --- /dev/null +++ b/Content.Server/_CorvaxNext/AutoCryoSleep/AutoCryoSleepableComponent.cs @@ -0,0 +1,4 @@ +namespace Content.Server._CorvaxNext.AutoCryoSleep; + +[RegisterComponent] +public sealed partial class AutoCryoSleepableComponent : Component; diff --git a/Content.Shared/_CorvaxNext/NextVars.cs b/Content.Shared/_CorvaxNext/NextVars.cs index 2effecae306..0ced0520cdf 100644 --- a/Content.Shared/_CorvaxNext/NextVars.cs +++ b/Content.Shared/_CorvaxNext/NextVars.cs @@ -9,6 +9,16 @@ namespace Content.Shared._CorvaxNext.NextVars; // ReSharper disable once InconsistentNaming public sealed class NextVars { + /** + * Auto cryo sleep + */ + + public static readonly CVarDef AutoCryoSleepEnabled = + CVarDef.Create("auto_cryo_sleep.enabled", true, CVar.SERVER); + + public static readonly CVarDef AutoCryoSleepTime = + CVarDef.Create("auto_cryo_sleep.time", 500, CVar.SERVER); + /// /// Offer item. ///