Skip to content

Commit

Permalink
Cryosleep portals (#209)
Browse files Browse the repository at this point in the history
* Create AutoCryoSleep

* Fixed AutoCryoSleep

* Added delay

* Fixed is't alive mobs

* Added tags

* Update Content.Server/_CorvaxNext/AutoCryoSleep/AutoCryoSleepSystem.cs

Co-authored-by: FN <[email protected]>

---------

Co-authored-by: FN <[email protected]>
  • Loading branch information
Tornado-Technology and FireNameFN authored Dec 30, 2024
1 parent a4b6aa7 commit 7948f46
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Content.Server/_CorvaxNext/AutoCryoSleep/AutoCryoSleepComponent.cs
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;

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

0 comments on commit 7948f46

Please sign in to comment.