Make power grid check event force APCs off (#17935)

* replace query with enumerator

* remove unnecessary imports

* prevent from toggling APC during event

* all entity query i guess
This commit is contained in:
Slava0135
2023-07-19 00:11:43 +03:00
committed by GitHub
parent ca521183a1
commit d21e4a1d4e
2 changed files with 29 additions and 5 deletions

View File

@@ -0,0 +1,11 @@
using Content.Server.StationEvents.Events;
namespace Content.Server.StationEvents.Components;
/// <summary>
/// Added to APCs to prevent them from turning on until event end
/// </summary>
[RegisterComponent, Access(typeof(PowerGridCheckRule))]
public sealed class PowerGridCheckDisabledComponent : Component
{
}

View File

@@ -6,9 +6,7 @@ using Robust.Shared.Utility;
using System.Threading; using System.Threading;
using Content.Server.Power.EntitySystems; using Content.Server.Power.EntitySystems;
using Timer = Robust.Shared.Timing.Timer; using Timer = Robust.Shared.Timing.Timer;
using System.Linq;
using Content.Server.GameTicking.Rules.Components; using Content.Server.GameTicking.Rules.Components;
using Robust.Shared.Random;
using Content.Server.Station.Components; using Content.Server.Station.Components;
using Content.Server.StationEvents.Components; using Content.Server.StationEvents.Components;
@@ -19,6 +17,13 @@ namespace Content.Server.StationEvents.Events
{ {
[Dependency] private readonly ApcSystem _apcSystem = default!; [Dependency] private readonly ApcSystem _apcSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<PowerGridCheckDisabledComponent, ApcToggleMainBreakerAttemptEvent>(OnApcToggleMainBreaker);
}
protected override void Started(EntityUid uid, PowerGridCheckRuleComponent component, GameRuleComponent gameRule, GameRuleStartedEvent args) protected override void Started(EntityUid uid, PowerGridCheckRuleComponent component, GameRuleComponent gameRule, GameRuleStartedEvent args)
{ {
base.Started(uid, component, gameRule, args); base.Started(uid, component, gameRule, args);
@@ -26,10 +31,11 @@ namespace Content.Server.StationEvents.Events
if (!TryGetRandomStation(out var chosenStation)) if (!TryGetRandomStation(out var chosenStation))
return; return;
foreach (var (apc, transform) in EntityQuery<ApcComponent, TransformComponent>(true)) var query = AllEntityQuery<ApcComponent, TransformComponent>();
while (query.MoveNext(out var target, out var apc, out var transform))
{ {
if (apc.MainBreakerEnabled && CompOrNull<StationMemberComponent>(transform.GridUid)?.Station == chosenStation) if (apc.MainBreakerEnabled && CompOrNull<StationMemberComponent>(transform.GridUid)?.Station == chosenStation)
component.Powered.Add(apc.Owner); component.Powered.Add(target);
} }
RobustRandom.Shuffle(component.Powered); RobustRandom.Shuffle(component.Powered);
@@ -51,6 +57,7 @@ namespace Content.Server.StationEvents.Events
if (!apcComponent.MainBreakerEnabled) if (!apcComponent.MainBreakerEnabled)
_apcSystem.ApcToggleBreaker(entity, apcComponent); _apcSystem.ApcToggleBreaker(entity, apcComponent);
} }
RemComp<PowerGridCheckDisabledComponent>(entity);
} }
// Can't use the default EndAudio // Can't use the default EndAudio
@@ -88,8 +95,14 @@ namespace Content.Server.StationEvents.Events
if (apcComponent.MainBreakerEnabled) if (apcComponent.MainBreakerEnabled)
_apcSystem.ApcToggleBreaker(selected, apcComponent); _apcSystem.ApcToggleBreaker(selected, apcComponent);
} }
EnsureComp<PowerGridCheckDisabledComponent>(selected);
component.Unpowered.Add(selected); component.Unpowered.Add(selected);
} }
} }
private void OnApcToggleMainBreaker(EntityUid uid, PowerGridCheckDisabledComponent component, ref ApcToggleMainBreakerAttemptEvent args)
{
args.Cancelled = true;
}
} }
} }