Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cryosleep portals #209

Merged
merged 6 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Author: TornadoTech
* License: AGPL
*/

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";
}
141 changes: 141 additions & 0 deletions Content.Server/_CorvaxNext/AutoCryoSleep/AutoCryoSleepSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Author: TornadoTech
* License: AGPL
*/

using System.Diagnostics.CodeAnalysis;
using Content.Shared._CorvaxNext.NextVars;
using Content.Shared.Bed.Cryostorage;
using Content.Shared.Mobs.Systems;
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 ContainerSystem _container = default!;
[Dependency] private readonly MobStateSystem _mobState = default!;

private bool _enabled;
private TimeSpan _disconnectedTime;
private TimeSpan _updateTime;

private TimeSpan _nextUpdate = TimeSpan.Zero;

public override void Initialize()
{
base.Initialize();

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);
Subs.CVar(_config, NextVars.AutoCryoSleepUpdateTime, value => _updateTime = TimeSpan.FromSeconds(value), true);
}

public override void Update(float frameTime)
{
base.Update(frameTime);

if (!_enabled)
return;

if (_timing.CurTime < _nextUpdate)
return;

_nextUpdate = _timing.CurTime + _updateTime;

var disconnectedQuery = EntityQueryEnumerator<AutoCryoSleepComponent>();
while (disconnectedQuery.MoveNext(out var uid, out var component))
{
if (_timing.CurTime - component.Disconnected < _disconnectedTime)
continue;

if (!_mobState.IsAlive(uid))
{
RemCompDeferred<AutoCryoSleepComponent>(uid);
continue;
}

TryCryoSleep(uid, component.EffectId);
}
}

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

RemCompDeferred<AutoCryoSleepComponent>(ent);
}

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

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))
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.CanInsert(targetUid, container))
continue;

if (effectId is not null)
Spawn(effectId, Transform(targetUid).Coordinates);

_container.Insert(targetUid, container);

RemCompDeferred<AutoCryoSleepComponent>(targetUid);
}
}

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;
Vonsant marked this conversation as resolved.
Show resolved Hide resolved

stationUid = stationMemberComponent.Station;
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Author: TornadoTech
* License: AGPL
*/

namespace Content.Server._CorvaxNext.AutoCryoSleep;

[RegisterComponent]
public sealed partial class AutoCryoSleepableComponent : Component;
13 changes: 13 additions & 0 deletions Content.Shared/_CorvaxNext/NextVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ 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);

public static readonly CVarDef<int> AutoCryoSleepUpdateTime =
CVarDef.Create("auto_cryo_sleep.update_time", 120, CVar.SERVER);

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