1
0

CableMultitoolSystem.cs 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using Content.Server.NodeContainer;
  2. using Content.Server.Power.Components;
  3. using Content.Server.Power.NodeGroups;
  4. using Content.Server.Tools;
  5. using Content.Shared.Examine;
  6. using Content.Shared.Interaction;
  7. using Content.Shared.Tools.Systems;
  8. using Content.Shared.Verbs;
  9. using JetBrains.Annotations;
  10. using Robust.Shared.Utility;
  11. namespace Content.Server.Power.EntitySystems
  12. {
  13. [UsedImplicitly]
  14. public sealed class CableMultitoolSystem : EntitySystem
  15. {
  16. [Dependency] private readonly ToolSystem _toolSystem = default!;
  17. [Dependency] private readonly PowerNetSystem _pnSystem = default!;
  18. [Dependency] private readonly ExamineSystemShared _examineSystem = default!;
  19. public override void Initialize()
  20. {
  21. base.Initialize();
  22. SubscribeLocalEvent<CableComponent, GetVerbsEvent<ExamineVerb>>(OnGetExamineVerbs);
  23. SubscribeLocalEvent<CableComponent, AfterInteractUsingEvent>(OnAfterInteractUsing);
  24. }
  25. private void OnAfterInteractUsing(EntityUid uid, CableComponent component, AfterInteractUsingEvent args)
  26. {
  27. if (args.Handled || args.Target == null || !args.CanReach || !_toolSystem.HasQuality(args.Used, SharedToolSystem.PulseQuality))
  28. return;
  29. var markup = FormattedMessage.FromMarkupOrThrow(GenerateCableMarkup(uid));
  30. _examineSystem.SendExamineTooltip(args.User, uid, markup, false, false);
  31. args.Handled = true;
  32. }
  33. private void OnGetExamineVerbs(EntityUid uid, CableComponent component, GetVerbsEvent<ExamineVerb> args)
  34. {
  35. // Must be in details range to try this.
  36. // Theoretically there should be a separate range at which a multitool works, but this does just fine.
  37. if (_examineSystem.IsInDetailsRange(args.User, args.Target))
  38. {
  39. var held = args.Using;
  40. // Pulsing is hardcoded here because I don't think it needs to be more complex than that right now.
  41. // Update if I'm wrong.
  42. var enabled = held != null && _toolSystem.HasQuality(held.Value, SharedToolSystem.PulseQuality);
  43. var verb = new ExamineVerb
  44. {
  45. Disabled = !enabled,
  46. Message = Loc.GetString("cable-multitool-system-verb-tooltip"),
  47. Text = Loc.GetString("cable-multitool-system-verb-name"),
  48. Category = VerbCategory.Examine,
  49. Icon = new SpriteSpecifier.Texture(new ("/Textures/Interface/VerbIcons/zap.svg.192dpi.png")),
  50. Act = () =>
  51. {
  52. var markup = FormattedMessage.FromMarkupOrThrow(GenerateCableMarkup(uid));
  53. _examineSystem.SendExamineTooltip(args.User, uid, markup, false, false);
  54. }
  55. };
  56. args.Verbs.Add(verb);
  57. }
  58. }
  59. private string GenerateCableMarkup(EntityUid uid, NodeContainerComponent? nodeContainer = null)
  60. {
  61. if (!Resolve(uid, ref nodeContainer))
  62. return Loc.GetString("cable-multitool-system-internal-error-missing-component");
  63. foreach (var node in nodeContainer.Nodes)
  64. {
  65. if (!(node.Value.NodeGroup is IBasePowerNet))
  66. continue;
  67. var p = (IBasePowerNet) node.Value.NodeGroup;
  68. var ps = _pnSystem.GetNetworkStatistics(p.NetworkNode);
  69. float storageRatio = ps.InStorageCurrent / Math.Max(ps.InStorageMax, 1.0f);
  70. float outStorageRatio = ps.OutStorageCurrent / Math.Max(ps.OutStorageMax, 1.0f);
  71. return Loc.GetString("cable-multitool-system-statistics",
  72. ("supplyc", ps.SupplyCurrent),
  73. ("supplyb", ps.SupplyBatteries),
  74. ("supplym", ps.SupplyTheoretical),
  75. ("consumption", ps.Consumption),
  76. ("storagec", ps.InStorageCurrent),
  77. ("storager", storageRatio),
  78. ("storagem", ps.InStorageMax),
  79. ("storageoc", ps.OutStorageCurrent),
  80. ("storageor", outStorageRatio),
  81. ("storageom", ps.OutStorageMax)
  82. );
  83. }
  84. return Loc.GetString("cable-multitool-system-internal-error-no-power-node");
  85. }
  86. }
  87. }