From a27e3406b77a88255514d4098b7acb23e21cf0bb Mon Sep 17 00:00:00 2001 From: Tornado Tech <54727692+Tornado-Technology@users.noreply.github.com> Date: Sun, 29 Dec 2024 00:09:12 +1000 Subject: [PATCH] Create AutoCryoSleep --- .../AutoCryoSleep/AutoCryoSleepSystem.cs | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 Content.Server/_CorvaxNext/AutoCryoSleep/AutoCryoSleepSystem.cs diff --git a/Content.Server/_CorvaxNext/AutoCryoSleep/AutoCryoSleepSystem.cs b/Content.Server/_CorvaxNext/AutoCryoSleep/AutoCryoSleepSystem.cs new file mode 100644 index 00000000000..30028efc61f --- /dev/null +++ b/Content.Server/_CorvaxNext/AutoCryoSleep/AutoCryoSleepSystem.cs @@ -0,0 +1,93 @@ +/* + * Author: TornadoTech + * License: AGPL + */ + +using System.Diagnostics.CodeAnalysis; +using Content.Server.Afk.Events; +using Content.Server.Bed.Cryostorage; +using Content.Server.Mind; +using Content.Shared.Bed.Cryostorage; +using Content.Shared.Station.Components; +using Robust.Server.Containers; +using Robust.Shared.Timing; + +namespace Content.Server._CorvaxNext.AutoCryoSleep; + +public sealed class AutoCryoSleepSystem : EntitySystem +{ + [Dependency] private readonly IGameTiming _timing = default!; + [Dependency] private readonly CryostorageSystem _cryostorage = default!; + [Dependency] private readonly ContainerSystem _container = default!; + [Dependency] private readonly MindSystem _mind = default!; + + public override void Initialize() + { + base.Initialize(); + + SubscribeLocalEvent(OnAFK); + } + + private void OnAFK(ref AFKEvent ev) + { + if (ev.Session.AttachedEntity is not { } entityUid) + return; + + TryCryoSleep(entityUid); + } + + private void TryCryoSleep(EntityUid targetUid) + { + if (HasComp(targetUid)) + return; + + if (!_mind.TryGetMind(targetUid, out _, out var mindComponent)) + return; + + if (!TryGetStation(targetUid, out var stationUid)) + return; + + foreach (var cryostorageEntity in EnumerateCryostorageInSameStation(stationUid.Value)) + { + if (!_container.TryGetContainer(cryostorageEntity, cryostorageEntity.Comp.ContainerId, out var container)) + continue; + + // We need only empty cryo sleeps + if (container.ContainedEntities.Count != 0) + continue; + + var cryostorageContained = AddComp(targetUid); + cryostorageContained.Cryostorage = cryostorageEntity; + cryostorageContained.GracePeriodEndTime = _timing.CurTime; + + _cryostorage.HandleEnterCryostorage((targetUid, cryostorageContained), mindComponent.UserId); + } + } + + private IEnumerable> EnumerateCryostorageInSameStation(EntityUid stationUid) + { + var query = AllEntityQuery(); + while (query.MoveNext(out var entityUid, out var cryostorageComponent)) + { + if (!TryGetStation(entityUid, out var cryoStationUid)) + continue; + + if (stationUid != cryoStationUid) + continue; + + yield return (entityUid, cryostorageComponent); + } + } + + private bool TryGetStation(EntityUid entityUid, [NotNullWhen(true)] out EntityUid? stationUid) + { + stationUid = null; + var gridUid = Transform(entityUid).GridUid; + + if (!TryComp(gridUid, out var stationMemberComponent)) + return false; + + stationUid = stationMemberComponent.Station; + return true; + } +}