Adds false alarm and updates events code (#2577)

* oops accedentaly ports how ss13 deals with event randomness. Also renames FakeEvent to FalseAlarm!

* thing

* greytide but it's implemented badly

* fixes&changies, also greytide!

* rng actualy exists now

* resync

* Naming Schemes

* Startup not init

* areas are dead

* very cool vsudio

* this does not exist, wtf

* Cleanup

* Nullables, fixables, and timings

Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
This commit is contained in:
Letter N
2021-01-18 18:14:53 +08:00
committed by GitHub
parent 51ad4f6c96
commit c30dc030c5
7 changed files with 226 additions and 140 deletions

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
#nullable enable
using System.Collections.Generic;
using System.Threading;
using Content.Server.GameObjects.Components.Power.ApcNetComponents;
using JetBrains.Annotations;
@@ -16,59 +17,54 @@ namespace Content.Server.StationEvents
public sealed class PowerGridCheck : StationEvent
{
public override string Name => "PowerGridCheck";
public override StationEventWeight Weight => StationEventWeight.Normal;
public override int? MaxOccurrences => 2;
protected override string StartAnnouncement => Loc.GetString(
public override float Weight => WeightNormal;
public override int? MaxOccurrences => 3;
public override string StartAnnouncement => Loc.GetString(
"Abnormal activity detected in the station's powernet. As a precautionary measure, the station's power will be shut off for an indeterminate duration.");
protected override string EndAnnouncement => Loc.GetString(
"Power has been restored to the station. We apologize for the inconvenience.");
public override string? StartAudio => "/Audio/Announcements/power_off.ogg";
private float _elapsedTime;
private int _failDuration;
// If you need EndAudio it's down below. Not set here because we can't play it at the normal time without spamming sounds.
/// <summary>
/// So we don't overlap the announcement with power-down sounds we'll delay it a few seconds.
/// </summary>
private bool _announced;
protected override float StartAfter => 12.0f;
private CancellationTokenSource _announceCancelToken;
private CancellationTokenSource? _announceCancelToken;
private readonly List<IEntity> _powered = new();
public override void Announce()
{
base.Announce();
EndAfter = IoCManager.Resolve<IRobustRandom>().Next(60, 120);
}
public override void Startup()
{
base.Startup();
_announced = false;
_elapsedTime = 0.0f;
_failDuration = IoCManager.Resolve<IRobustRandom>().Next(60, 120);
var componentManager = IoCManager.Resolve<IComponentManager>();
foreach (PowerReceiverComponent component in componentManager.EntityQuery<PowerReceiverComponent>())
foreach (var component in componentManager.EntityQuery<PowerReceiverComponent>())
{
component.PowerDisabled = true;
_powered.Add(component.Owner);
}
base.Startup();
}
public override void Shutdown()
{
base.Shutdown();
foreach (var entity in _powered)
{
if (entity.Deleted) continue;
if (entity.TryGetComponent(out PowerReceiverComponent powerReceiverComponent))
if (entity.TryGetComponent(out PowerReceiverComponent? powerReceiverComponent))
{
powerReceiverComponent.PowerDisabled = false;
}
}
// Can't use the default EndAudio
_announceCancelToken?.Cancel();
_announceCancelToken = new CancellationTokenSource();
Timer.Spawn(3000, () =>
@@ -76,29 +72,8 @@ namespace Content.Server.StationEvents
EntitySystem.Get<AudioSystem>().PlayGlobal("/Audio/Announcements/power_on.ogg");
}, _announceCancelToken.Token);
_powered.Clear();
}
public override void Update(float frameTime)
{
if (!Running)
{
return;
}
if (!_announced && _elapsedTime > 3.0f)
{
EntitySystem.Get<AudioSystem>().PlayGlobal("/Audio/Announcements/power_off.ogg");
_announced = true;
}
_elapsedTime += frameTime;
if (_elapsedTime < _failDuration)
{
return;
}
Running = false;
base.Shutdown();
}
}
}