Skip to content

Commit

Permalink
Fixed AutoCryoSleep
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornado-Technology committed Dec 30, 2024
1 parent a27e340 commit fbb3a06
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 17 deletions.
13 changes: 13 additions & 0 deletions Content.Server/_CorvaxNext/AutoCryoSleep/AutoCryoSleepComponent.cs
Original file line number Diff line number Diff line change
@@ -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";
}
65 changes: 48 additions & 17 deletions Content.Server/_CorvaxNext/AutoCryoSleep/AutoCryoSleepSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AFKEvent>(OnAFK);
SubscribeLocalEvent<AutoCryoSleepableComponent, PlayerAttachedEvent>(OnPlayerAttached);
SubscribeLocalEvent<AutoCryoSleepableComponent, PlayerDetachedEvent>(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<AutoCryoSleepComponent>();
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<AutoCryoSleepableComponent> ent, ref PlayerAttachedEvent args)
{
if (HasComp<CryostorageContainedComponent>(targetUid))
if (!_enabled)
return;

RemCompDeferred<AutoCryoSleepComponent>(ent);
}

private void OnPlayerDetached(Entity<AutoCryoSleepableComponent> ent, ref PlayerDetachedEvent args)
{
if (!_enabled)
return;

if (!_mind.TryGetMind(targetUid, out _, out var mindComponent))
var comp = EnsureComp<AutoCryoSleepComponent>(ent);
comp.Disconnected = _timing.CurTime;
}

private void TryCryoSleep(EntityUid targetUid, EntProtoId? effectId = null)
{
if (HasComp<CryostorageContainedComponent>(targetUid))
return;

if (!TryGetStation(targetUid, out var stationUid))
Expand All @@ -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<CryostorageContainedComponent>(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<AutoCryoSleepComponent>(targetUid);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Content.Server._CorvaxNext.AutoCryoSleep;

[RegisterComponent]
public sealed partial class AutoCryoSleepableComponent : Component;
10 changes: 10 additions & 0 deletions Content.Shared/_CorvaxNext/NextVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ namespace Content.Shared._CorvaxNext.NextVars;
// ReSharper disable once InconsistentNaming
public sealed class NextVars
{
/**
* Auto cryo sleep
*/

public static readonly CVarDef<bool> AutoCryoSleepEnabled =
CVarDef.Create("auto_cryo_sleep.enabled", true, CVar.SERVER);

public static readonly CVarDef<int> AutoCryoSleepTime =
CVarDef.Create("auto_cryo_sleep.time", 500, CVar.SERVER);

/// <summary>
/// Offer item.
/// </summary>
Expand Down

0 comments on commit fbb3a06

Please sign in to comment.