LightningSystem.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System.Linq;
  2. using Content.Server.Beam;
  3. using Content.Server.Beam.Components;
  4. using Content.Server.Lightning.Components;
  5. using Content.Shared.Lightning;
  6. using Robust.Server.GameObjects;
  7. using Robust.Shared.Random;
  8. namespace Content.Server.Lightning;
  9. // TheShuEd:
  10. //I've redesigned the lightning system to be more optimized.
  11. //Previously, each lightning element, when it touched something, would try to branch into nearby entities.
  12. //So if a lightning bolt was 20 entities long, each one would check its surroundings and have a chance to create additional lightning...
  13. //which could lead to recursive creation of more and more lightning bolts and checks.
  14. //I redesigned so that lightning branches can only be created from the point where the lightning struck, no more collide checks
  15. //and the number of these branches is explicitly controlled in the new function.
  16. public sealed class LightningSystem : SharedLightningSystem
  17. {
  18. [Dependency] private readonly BeamSystem _beam = default!;
  19. [Dependency] private readonly IRobustRandom _random = default!;
  20. [Dependency] private readonly EntityLookupSystem _lookup = default!;
  21. [Dependency] private readonly TransformSystem _transform = default!;
  22. public override void Initialize()
  23. {
  24. base.Initialize();
  25. SubscribeLocalEvent<LightningComponent, ComponentRemove>(OnRemove);
  26. }
  27. private void OnRemove(EntityUid uid, LightningComponent component, ComponentRemove args)
  28. {
  29. if (!TryComp<BeamComponent>(uid, out var lightningBeam) || !TryComp<BeamComponent>(lightningBeam.VirtualBeamController, out var beamController))
  30. {
  31. return;
  32. }
  33. beamController.CreatedBeams.Remove(uid);
  34. }
  35. /// <summary>
  36. /// Fires lightning from user to target
  37. /// </summary>
  38. /// <param name="user">Where the lightning fires from</param>
  39. /// <param name="target">Where the lightning fires to</param>
  40. /// <param name="lightningPrototype">The prototype for the lightning to be created</param>
  41. /// <param name="triggerLightningEvents">if the lightnings being fired should trigger lightning events.</param>
  42. public void ShootLightning(EntityUid user, EntityUid target, string lightningPrototype = "Lightning", bool triggerLightningEvents = true)
  43. {
  44. var spriteState = LightningRandomizer();
  45. _beam.TryCreateBeam(user, target, lightningPrototype, spriteState);
  46. if (triggerLightningEvents) // we don't want certain prototypes to trigger lightning level events
  47. {
  48. var ev = new HitByLightningEvent(user, target);
  49. RaiseLocalEvent(target, ref ev);
  50. }
  51. }
  52. /// <summary>
  53. /// Looks for objects with a LightningTarget component in the radius, prioritizes them, and hits the highest priority targets with lightning.
  54. /// </summary>
  55. /// <param name="user">Where the lightning fires from</param>
  56. /// <param name="range">Targets selection radius</param>
  57. /// <param name="boltCount">Number of lightning bolts</param>
  58. /// <param name="lightningPrototype">The prototype for the lightning to be created</param>
  59. /// <param name="arcDepth">how many times to recursively fire lightning bolts from the target points of the first shot.</param>
  60. /// <param name="triggerLightningEvents">if the lightnings being fired should trigger lightning events.</param>
  61. public void ShootRandomLightnings(EntityUid user, float range, int boltCount, string lightningPrototype = "Lightning", int arcDepth = 0, bool triggerLightningEvents = true)
  62. {
  63. //TODO: add support to different priority target tablem for different lightning types
  64. //TODO: Remove Hardcode LightningTargetComponent (this should be a parameter of the SharedLightningComponent)
  65. //TODO: This is still pretty bad for perf but better than before and at least it doesn't re-allocate
  66. // several hashsets every time
  67. var targets = _lookup.GetEntitiesInRange<LightningTargetComponent>(_transform.GetMapCoordinates(user), range).ToList();
  68. _random.Shuffle(targets);
  69. targets.Sort((x, y) => y.Comp.Priority.CompareTo(x.Comp.Priority));
  70. int shootedCount = 0;
  71. int count = -1;
  72. while(shootedCount < boltCount)
  73. {
  74. count++;
  75. if (count >= targets.Count) { break; }
  76. var curTarget = targets[count];
  77. if (!_random.Prob(curTarget.Comp.HitProbability)) //Chance to ignore target
  78. continue;
  79. ShootLightning(user, targets[count].Owner, lightningPrototype, triggerLightningEvents);
  80. if (arcDepth - targets[count].Comp.LightningResistance > 0)
  81. {
  82. ShootRandomLightnings(targets[count].Owner, range, 1, lightningPrototype, arcDepth - targets[count].Comp.LightningResistance, triggerLightningEvents);
  83. }
  84. shootedCount++;
  85. }
  86. }
  87. }
  88. /// <summary>
  89. /// Raised directed on the target when an entity becomes the target of a lightning strike (not when touched)
  90. /// </summary>
  91. /// <param name="Source">The entity that created the lightning</param>
  92. /// <param name="Target">The entity that was struck by lightning.</param>
  93. [ByRefEvent]
  94. public readonly record struct HitByLightningEvent(EntityUid Source, EntityUid Target);