Files
crystall-punk-14/Content.Server/StationEvents/Events/PowerGridCheck.cs
Kara b9a0894d7c Event refactor (#9589)
* Station event refactor

* Remove clientside `IStationEventManager`

we can just use prototypes

* Basic API idea

* Cruft

* first attempt at epicness

* okay yeah this shit is really clean

* sort out minor stuff

* Convert `BreakerFlip`

* `BureaucraticError` + general cleanup

* `DiseaseOutbreak`

* `FalseAlarm`

* `GasLeak`

* `KudzuGrowth`

* `MeteorSwarm`

* `MouseMigration`

* misc errors

* `PowerGridCheck`

* `RandomSentience`

* `VentClog`

* `VentCritters`

* `ZombieOutbreak`

* Rewrite basic event scheduler

* Minor fixes and logging

* ooooops

* errors + fix

* linter

* completions, `RuleStarted` property, update loop fixes

* Tweaks

* Fix #9462

* Basic scheduler update fix, and fixes #8174

* Add test

* UI cleanup

* really this was just for testing
2022-07-10 20:48:41 -05:00

112 lines
3.4 KiB
C#

using System.Threading;
using Content.Server.Power.Components;
using Content.Shared.Sound;
using JetBrains.Annotations;
using Robust.Shared.Audio;
using Robust.Shared.Player;
using Robust.Shared.Random;
using Robust.Shared.Utility;
using Timer = Robust.Shared.Timing.Timer;
namespace Content.Server.StationEvents.Events
{
[UsedImplicitly]
public sealed class PowerGridCheck : StationEventSystem
{
public override string Prototype => "PowerGridCheck";
private CancellationTokenSource? _announceCancelToken;
private readonly List<EntityUid> _powered = new();
private readonly List<EntityUid> _unpowered = new();
private const float SecondsUntilOff = 30.0f;
private int _numberPerSecond = 0;
private float UpdateRate => 1.0f / _numberPerSecond;
private float _frameTimeAccumulator = 0.0f;
private float _endAfter = 0.0f;
public override void Added()
{
base.Added();
_endAfter = RobustRandom.Next(60, 120);
}
public override void Started()
{
foreach (var component in EntityManager.EntityQuery<ApcPowerReceiverComponent>(true))
{
if (!component.PowerDisabled)
_powered.Add(component.Owner);
}
RobustRandom.Shuffle(_powered);
_numberPerSecond = Math.Max(1, (int)(_powered.Count / SecondsUntilOff)); // Number of APCs to turn off every second. At least one.
base.Started();
}
public override void Update(float frameTime)
{
base.Update(frameTime);
if (!RuleStarted)
return;
if (Elapsed > _endAfter)
{
ForceEndSelf();
return;
}
var updates = 0;
_frameTimeAccumulator += frameTime;
if (_frameTimeAccumulator > UpdateRate)
{
updates = (int) (_frameTimeAccumulator / UpdateRate);
_frameTimeAccumulator -= UpdateRate * updates;
}
for (var i = 0; i < updates; i++)
{
if (_powered.Count == 0)
break;
var selected = _powered.Pop();
if (EntityManager.Deleted(selected)) continue;
if (EntityManager.TryGetComponent<ApcPowerReceiverComponent>(selected, out var powerReceiverComponent))
{
powerReceiverComponent.PowerDisabled = true;
}
_unpowered.Add(selected);
}
}
public override void Ended()
{
foreach (var entity in _unpowered)
{
if (EntityManager.Deleted(entity)) continue;
if (EntityManager.TryGetComponent(entity, out ApcPowerReceiverComponent? powerReceiverComponent))
{
powerReceiverComponent.PowerDisabled = false;
}
}
// Can't use the default EndAudio
_announceCancelToken?.Cancel();
_announceCancelToken = new CancellationTokenSource();
Timer.Spawn(3000, () =>
{
SoundSystem.Play("/Audio/Announcements/power_on.ogg", Filter.Broadcast(), AudioParams.Default);
}, _announceCancelToken.Token);
_unpowered.Clear();
base.Ended();
}
}
}