1
0

TechAnomalySystem.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using Content.Server.Anomaly.Components;
  2. using Content.Server.Beam;
  3. using Content.Server.DeviceLinking.Systems;
  4. using Content.Shared.Anomaly.Components;
  5. using Content.Shared.DeviceLinking;
  6. using Content.Shared.Emag.Systems;
  7. using Robust.Shared.Random;
  8. using Robust.Shared.Timing;
  9. namespace Content.Server.Anomaly.Effects;
  10. public sealed class TechAnomalySystem : EntitySystem
  11. {
  12. [Dependency] private readonly DeviceLinkSystem _signal = default!;
  13. [Dependency] private readonly EntityLookupSystem _lookup = default!;
  14. [Dependency] private readonly IRobustRandom _random = default!;
  15. [Dependency] private readonly BeamSystem _beam = default!;
  16. [Dependency] private readonly IGameTiming _timing = default!;
  17. [Dependency] private readonly EmagSystem _emag = default!;
  18. public override void Initialize()
  19. {
  20. base.Initialize();
  21. SubscribeLocalEvent<TechAnomalyComponent, MapInitEvent>(OnTechMapInit);
  22. SubscribeLocalEvent<TechAnomalyComponent, AnomalyPulseEvent>(OnPulse);
  23. SubscribeLocalEvent<TechAnomalyComponent, AnomalySupercriticalEvent>(OnSupercritical);
  24. SubscribeLocalEvent<TechAnomalyComponent, AnomalyStabilityChangedEvent>(OnStabilityChanged);
  25. }
  26. private void OnTechMapInit(Entity<TechAnomalyComponent> ent, ref MapInitEvent args)
  27. {
  28. ent.Comp.NextTimer = _timing.CurTime;
  29. }
  30. public override void Update(float frameTime)
  31. {
  32. base.Update(frameTime);
  33. var query = EntityQueryEnumerator<TechAnomalyComponent, AnomalyComponent>();
  34. while (query.MoveNext(out var uid, out var tech, out var anom))
  35. {
  36. if (_timing.CurTime < tech.NextTimer)
  37. continue;
  38. tech.NextTimer += TimeSpan.FromSeconds(tech.TimerFrequency);
  39. _signal.InvokePort(uid, tech.TimerPort);
  40. }
  41. }
  42. private void OnStabilityChanged(Entity<TechAnomalyComponent> tech, ref AnomalyStabilityChangedEvent args)
  43. {
  44. var links = MathHelper.Lerp(tech.Comp.LinkCountPerPulse.Min, tech.Comp.LinkCountPerPulse.Max, args.Severity);
  45. CreateNewRandomLink(tech, (int)links);
  46. }
  47. private void CreateNewRandomLink(Entity<TechAnomalyComponent> tech, int count)
  48. {
  49. if (!TryComp<AnomalyComponent>(tech, out var anomaly))
  50. return;
  51. if (!TryComp<DeviceLinkSourceComponent>(tech, out var sourceComp))
  52. return;
  53. var range = MathHelper.Lerp(tech.Comp.LinkRadius.Min, tech.Comp.LinkRadius.Max, anomaly.Severity);
  54. var devices = _lookup.GetEntitiesInRange<DeviceLinkSinkComponent>(Transform(tech).Coordinates, range);
  55. if (devices.Count < 1)
  56. return;
  57. for (var i = 0; i < count; i++)
  58. {
  59. var device = _random.Pick(devices);
  60. CreateNewLink(tech, (tech, sourceComp), device);
  61. }
  62. }
  63. private void CreateNewLink(Entity<TechAnomalyComponent> tech, Entity<DeviceLinkSourceComponent> source, Entity<DeviceLinkSinkComponent> target)
  64. {
  65. var sourcePort = _random.Pick(source.Comp.Ports);
  66. var sinkPort = _random.Pick(target.Comp.Ports);
  67. _signal.SaveLinks(null, source, target,new()
  68. {
  69. (sourcePort, sinkPort),
  70. });
  71. _beam.TryCreateBeam(source, target, tech.Comp.LinkBeamProto);
  72. }
  73. private void OnSupercritical(Entity<TechAnomalyComponent> tech, ref AnomalySupercriticalEvent args)
  74. {
  75. // We remove the component so that the anomaly does not bind itself to other devices before self destroy.
  76. RemComp<DeviceLinkSourceComponent>(tech);
  77. var sources =
  78. _lookup.GetEntitiesInRange<DeviceLinkSourceComponent>(Transform(tech).Coordinates,
  79. tech.Comp.LinkRadius.Max);
  80. var sinks =
  81. _lookup.GetEntitiesInRange<DeviceLinkSinkComponent>(Transform(tech).Coordinates,
  82. tech.Comp.LinkRadius.Max);
  83. for (var i = 0; i < tech.Comp.LinkCountSupercritical; i++)
  84. {
  85. if (sources.Count < 1)
  86. return;
  87. if (sinks.Count < 1)
  88. return;
  89. var source = _random.Pick(sources);
  90. sources.Remove(source);
  91. var sink = _random.Pick(sinks);
  92. sinks.Remove(sink);
  93. if (_random.Prob(tech.Comp.EmagSupercritProbability))
  94. {
  95. var sourceEv = new GotEmaggedEvent(tech, EmagType.Access | EmagType.Interaction);
  96. RaiseLocalEvent(source, ref sourceEv);
  97. var sinkEv = new GotEmaggedEvent(tech, EmagType.Access | EmagType.Interaction);
  98. RaiseLocalEvent(sink, ref sinkEv);
  99. }
  100. CreateNewLink(tech, source, sink);
  101. }
  102. }
  103. private void OnPulse(Entity<TechAnomalyComponent> tech, ref AnomalyPulseEvent args)
  104. {
  105. _signal.InvokePort(tech, tech.Comp.PulsePort);
  106. }
  107. }