PowerGridCheckRule.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System.Threading;
  2. using Content.Server.Power.Components;
  3. using Content.Server.Power.EntitySystems;
  4. using Content.Server.StationEvents.Components;
  5. using Content.Shared.GameTicking.Components;
  6. using Content.Shared.Station.Components;
  7. using JetBrains.Annotations;
  8. using Robust.Shared.Audio;
  9. using Robust.Shared.Player;
  10. using Robust.Shared.Utility;
  11. using Timer = Robust.Shared.Timing.Timer;
  12. namespace Content.Server.StationEvents.Events
  13. {
  14. [UsedImplicitly]
  15. public sealed class PowerGridCheckRule : StationEventSystem<PowerGridCheckRuleComponent>
  16. {
  17. [Dependency] private readonly ApcSystem _apcSystem = default!;
  18. protected override void Started(EntityUid uid, PowerGridCheckRuleComponent component, GameRuleComponent gameRule, GameRuleStartedEvent args)
  19. {
  20. base.Started(uid, component, gameRule, args);
  21. if (!TryGetRandomStation(out var chosenStation))
  22. return;
  23. component.AffectedStation = chosenStation.Value;
  24. var query = AllEntityQuery<ApcComponent, TransformComponent>();
  25. while (query.MoveNext(out var apcUid ,out var apc, out var transform))
  26. {
  27. if (apc.MainBreakerEnabled && CompOrNull<StationMemberComponent>(transform.GridUid)?.Station == chosenStation)
  28. component.Powered.Add(apcUid);
  29. }
  30. RobustRandom.Shuffle(component.Powered);
  31. component.NumberPerSecond = Math.Max(1, (int)(component.Powered.Count / component.SecondsUntilOff)); // Number of APCs to turn off every second. At least one.
  32. }
  33. protected override void Ended(EntityUid uid, PowerGridCheckRuleComponent component, GameRuleComponent gameRule, GameRuleEndedEvent args)
  34. {
  35. base.Ended(uid, component, gameRule, args);
  36. foreach (var entity in component.Unpowered)
  37. {
  38. if (Deleted(entity))
  39. continue;
  40. if (TryComp(entity, out ApcComponent? apcComponent))
  41. {
  42. if(!apcComponent.MainBreakerEnabled)
  43. _apcSystem.ApcToggleBreaker(entity, apcComponent);
  44. }
  45. }
  46. // Can't use the default EndAudio
  47. component.AnnounceCancelToken?.Cancel();
  48. component.AnnounceCancelToken = new CancellationTokenSource();
  49. Timer.Spawn(3000, () =>
  50. {
  51. Audio.PlayGlobal(component.PowerOnSound, Filter.Broadcast(), true);
  52. }, component.AnnounceCancelToken.Token);
  53. component.Unpowered.Clear();
  54. }
  55. protected override void ActiveTick(EntityUid uid, PowerGridCheckRuleComponent component, GameRuleComponent gameRule, float frameTime)
  56. {
  57. base.ActiveTick(uid, component, gameRule, frameTime);
  58. var updates = 0;
  59. component.FrameTimeAccumulator += frameTime;
  60. if (component.FrameTimeAccumulator > component.UpdateRate)
  61. {
  62. updates = (int) (component.FrameTimeAccumulator / component.UpdateRate);
  63. component.FrameTimeAccumulator -= component.UpdateRate * updates;
  64. }
  65. for (var i = 0; i < updates; i++)
  66. {
  67. if (component.Powered.Count == 0)
  68. break;
  69. var selected = component.Powered.Pop();
  70. if (Deleted(selected))
  71. continue;
  72. if (TryComp<ApcComponent>(selected, out var apcComponent))
  73. {
  74. if (apcComponent.MainBreakerEnabled)
  75. _apcSystem.ApcToggleBreaker(selected, apcComponent);
  76. }
  77. component.Unpowered.Add(selected);
  78. }
  79. }
  80. }
  81. }