forked from space-syndicate/space-station-14
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e8a92f9
commit a27e340
Showing
1 changed file
with
93 additions
and
0 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
Content.Server/_CorvaxNext/AutoCryoSleep/AutoCryoSleepSystem.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<AFKEvent>(OnAFK); | ||
} | ||
|
||
private void OnAFK(ref AFKEvent ev) | ||
{ | ||
if (ev.Session.AttachedEntity is not { } entityUid) | ||
return; | ||
|
||
TryCryoSleep(entityUid); | ||
} | ||
|
||
private void TryCryoSleep(EntityUid targetUid) | ||
{ | ||
if (HasComp<CryostorageContainedComponent>(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<CryostorageContainedComponent>(targetUid); | ||
cryostorageContained.Cryostorage = cryostorageEntity; | ||
cryostorageContained.GracePeriodEndTime = _timing.CurTime; | ||
|
||
_cryostorage.HandleEnterCryostorage((targetUid, cryostorageContained), mindComponent.UserId); | ||
} | ||
} | ||
|
||
private IEnumerable<Entity<CryostorageComponent>> EnumerateCryostorageInSameStation(EntityUid stationUid) | ||
{ | ||
var query = AllEntityQuery<CryostorageComponent>(); | ||
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<StationMemberComponent>(gridUid, out var stationMemberComponent)) | ||
return false; | ||
|
||
stationUid = stationMemberComponent.Station; | ||
return true; | ||
} | ||
} |