Files
wwdpublic/Content.Server/StationEvents/Events/PowerGridCheckRule.cs
VMSolidus 08822e34ba Cherry-Pick Antag Refactor (#734)
This cherry-pick's Wizden's Antag Refactor, which is needed for all
future antag updates, as well as for me to cherry-pick over Cultists(Who
are going to need some editing to fit the antag refactor), Changelings,
Heretics, and Wizards. I actually selected the White-Dream-Public
version of the Antag Refactor, due to it having commits made tailored to
our repository, so it comes prepackaged with all the changes necessary
for our repo-specific content.

https://github.com/frosty-dev/ss14-wwdp/pull/10

Signed-off-by: Timemaster99 <57200767+Timemaster99@users.noreply.github.com>
Co-authored-by: ThereDrD <88589686+ThereDrD0@users.noreply.github.com>
Co-authored-by: Jeff <velcroboy333@hotmail.com>
Co-authored-by: Timemaster99 <57200767+Timemaster99@users.noreply.github.com>
Co-authored-by: Timemaster99 <elijahrobot@gmail.com>
Co-authored-by: luckywill339@gmail.com <luckywill339@gmail.com>
Co-authored-by: Danger Revolution! <142105406+DangerRevolution@users.noreply.github.com>
Co-authored-by: Azzy <azzydev@icloud.com>
2024-08-15 10:11:48 -07:00

91 lines
3.4 KiB
C#

using System.Threading;
using Content.Server.GameTicking.Components;
using Content.Server.GameTicking.Rules.Components;
using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems;
using Content.Server.Station.Components;
using Content.Server.StationEvents.Components;
using JetBrains.Annotations;
using Robust.Shared.Audio;
using Robust.Shared.Player;
using Robust.Shared.Utility;
using Timer = Robust.Shared.Timing.Timer;
namespace Content.Server.StationEvents.Events
{
[UsedImplicitly]
public sealed class PowerGridCheckRule : StationEventSystem<PowerGridCheckRuleComponent>
{
[Dependency] private readonly ApcSystem _apcSystem = default!;
protected override void Started(EntityUid uid, PowerGridCheckRuleComponent component, GameRuleComponent gameRule, GameRuleStartedEvent args)
{
base.Started(uid, component, gameRule, args);
if (!TryGetRandomStation(out var chosenStation))
return;
component.AffectedStation = chosenStation.Value;
var query = AllEntityQuery<ApcComponent, TransformComponent>();
while (query.MoveNext(out var apcUid ,out var apc, out var transform))
{
if (apc.MainBreakerEnabled && CompOrNull<StationMemberComponent>(transform.GridUid)?.Station == chosenStation)
component.Powered.Add(apcUid);
}
RobustRandom.Shuffle(component.Powered);
component.NumberPerSecond = Math.Max(1, (int)(component.Powered.Count / component.SecondsUntilOff)); // Number of APCs to turn off every second. At least one.
}
protected override void Ended(EntityUid uid, PowerGridCheckRuleComponent component, GameRuleComponent gameRule, GameRuleEndedEvent args)
{
base.Ended(uid, component, gameRule, args);
foreach (var entity in component.Unpowered)
{
if (Deleted(entity))
continue;
if (TryComp(entity, out ApcComponent? apcComponent))
{
if(!apcComponent.MainBreakerEnabled)
_apcSystem.ApcToggleBreaker(entity, apcComponent);
}
}
component.Unpowered.Clear();
}
protected override void ActiveTick(EntityUid uid, PowerGridCheckRuleComponent component, GameRuleComponent gameRule, float frameTime)
{
base.ActiveTick(uid, component, gameRule, frameTime);
var updates = 0;
component.FrameTimeAccumulator += frameTime;
if (component.FrameTimeAccumulator > component.UpdateRate)
{
updates = (int) (component.FrameTimeAccumulator / component.UpdateRate);
component.FrameTimeAccumulator -= component.UpdateRate * updates;
}
for (var i = 0; i < updates; i++)
{
if (component.Powered.Count == 0)
break;
var selected = component.Powered.Pop();
if (Deleted(selected))
continue;
if (TryComp<ApcComponent>(selected, out var apcComponent))
{
if (apcComponent.MainBreakerEnabled)
_apcSystem.ApcToggleBreaker(selected, apcComponent);
}
component.Unpowered.Add(selected);
}
}
}
}