SolarFlareRule.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using Content.Server.GameTicking.Rules.Components;
  2. using Content.Server.Radio;
  3. using Robust.Shared.Random;
  4. using Content.Server.Light.EntitySystems;
  5. using Content.Server.Light.Components;
  6. using Content.Server.StationEvents.Components;
  7. using Content.Shared.Radio.Components;
  8. using Content.Shared.Doors.Components;
  9. using Content.Shared.Doors.Systems;
  10. using Content.Shared.GameTicking.Components;
  11. namespace Content.Server.StationEvents.Events;
  12. public sealed class SolarFlareRule : StationEventSystem<SolarFlareRuleComponent>
  13. {
  14. [Dependency] private readonly PoweredLightSystem _poweredLight = default!;
  15. [Dependency] private readonly SharedDoorSystem _door = default!;
  16. private float _effectTimer = 0;
  17. public override void Initialize()
  18. {
  19. base.Initialize();
  20. SubscribeLocalEvent<RadioReceiveAttemptEvent>(OnRadioReceiveAttempt);
  21. }
  22. protected override void Started(EntityUid uid, SolarFlareRuleComponent comp, GameRuleComponent gameRule, GameRuleStartedEvent args)
  23. {
  24. base.Started(uid, comp, gameRule, args);
  25. for (var i = 0; i < comp.ExtraCount; i++)
  26. {
  27. var channel = RobustRandom.Pick(comp.ExtraChannels);
  28. comp.AffectedChannels.Add(channel);
  29. }
  30. }
  31. protected override void ActiveTick(EntityUid uid, SolarFlareRuleComponent component, GameRuleComponent gameRule, float frameTime)
  32. {
  33. base.ActiveTick(uid, component, gameRule, frameTime);
  34. _effectTimer -= frameTime;
  35. if (_effectTimer < 0)
  36. {
  37. _effectTimer += 1;
  38. var lightQuery = EntityQueryEnumerator<PoweredLightComponent>();
  39. while (lightQuery.MoveNext(out var lightEnt, out var light))
  40. {
  41. if (RobustRandom.Prob(component.LightBreakChancePerSecond))
  42. _poweredLight.TryDestroyBulb(lightEnt, light);
  43. }
  44. var airlockQuery = EntityQueryEnumerator<AirlockComponent, DoorComponent>();
  45. while (airlockQuery.MoveNext(out var airlockEnt, out var airlock, out var door))
  46. {
  47. if (airlock.AutoClose && RobustRandom.Prob(component.DoorToggleChancePerSecond))
  48. _door.TryToggleDoor(airlockEnt, door);
  49. }
  50. }
  51. }
  52. private void OnRadioReceiveAttempt(ref RadioReceiveAttemptEvent args)
  53. {
  54. var query = EntityQueryEnumerator<SolarFlareRuleComponent, GameRuleComponent>();
  55. while (query.MoveNext(out var uid, out var flare, out var gameRule))
  56. {
  57. if (!GameTicker.IsGameRuleActive(uid, gameRule))
  58. continue;
  59. if (!flare.AffectedChannels.Contains(args.Channel.ID))
  60. continue;
  61. if (!flare.OnlyJamHeadsets || (HasComp<HeadsetComponent>(args.RadioReceiver) || HasComp<HeadsetComponent>(args.RadioSource)))
  62. args.Cancelled = true;
  63. }
  64. }
  65. }