CableSystem.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using Content.Server.Administration.Logs;
  2. using Content.Server.Electrocution;
  3. using Content.Server.Power.Components;
  4. using Content.Server.Stack;
  5. using Content.Shared.Database;
  6. using Content.Shared.DoAfter;
  7. using Content.Shared.Interaction;
  8. using Robust.Shared.Map;
  9. using CableCuttingFinishedEvent = Content.Shared.Tools.Systems.CableCuttingFinishedEvent;
  10. using SharedToolSystem = Content.Shared.Tools.Systems.SharedToolSystem;
  11. namespace Content.Server.Power.EntitySystems;
  12. public sealed partial class CableSystem : EntitySystem
  13. {
  14. [Dependency] private readonly ITileDefinitionManager _tileManager = default!;
  15. [Dependency] private readonly SharedToolSystem _toolSystem = default!;
  16. [Dependency] private readonly StackSystem _stack = default!;
  17. [Dependency] private readonly ElectrocutionSystem _electrocutionSystem = default!;
  18. public override void Initialize()
  19. {
  20. base.Initialize();
  21. InitializeCablePlacer();
  22. SubscribeLocalEvent<CableComponent, InteractUsingEvent>(OnInteractUsing);
  23. SubscribeLocalEvent<CableComponent, CableCuttingFinishedEvent>(OnCableCut);
  24. // Shouldn't need re-anchoring.
  25. SubscribeLocalEvent<CableComponent, AnchorStateChangedEvent>(OnAnchorChanged);
  26. }
  27. private void OnInteractUsing(EntityUid uid, CableComponent cable, InteractUsingEvent args)
  28. {
  29. if (args.Handled)
  30. return;
  31. if (cable.CuttingQuality != null)
  32. {
  33. args.Handled = _toolSystem.UseTool(args.Used, args.User, uid, cable.CuttingDelay, cable.CuttingQuality, new CableCuttingFinishedEvent());
  34. }
  35. }
  36. private void OnCableCut(EntityUid uid, CableComponent cable, DoAfterEvent args)
  37. {
  38. if (args.Cancelled)
  39. return;
  40. var xform = Transform(uid);
  41. var ev = new CableAnchorStateChangedEvent(xform);
  42. RaiseLocalEvent(uid, ref ev);
  43. if (_electrocutionSystem.TryDoElectrifiedAct(uid, args.User))
  44. return;
  45. _adminLogger.Add(LogType.CableCut, LogImpact.Medium, $"The {ToPrettyString(uid)} at {xform.Coordinates} was cut by {ToPrettyString(args.User)}.");
  46. Spawn(cable.CableDroppedOnCutPrototype, xform.Coordinates);
  47. QueueDel(uid);
  48. }
  49. private void OnAnchorChanged(EntityUid uid, CableComponent cable, ref AnchorStateChangedEvent args)
  50. {
  51. var ev = new CableAnchorStateChangedEvent(args.Transform, args.Detaching);
  52. RaiseLocalEvent(uid, ref ev);
  53. if (args.Anchored)
  54. return; // huh? it wasn't anchored?
  55. // anchor state can change as a result of deletion (detach to null).
  56. // We don't want to spawn an entity when deleted.
  57. if (!TryLifeStage(uid, out var life) || life >= EntityLifeStage.Terminating)
  58. return;
  59. // This entity should not be un-anchorable. But this can happen if the grid-tile is deleted (RCD, explosion,
  60. // etc). In that case: behave as if the cable had been cut.
  61. Spawn(cable.CableDroppedOnCutPrototype, Transform(uid).Coordinates);
  62. QueueDel(uid);
  63. }
  64. }