NetProbeCartridgeSystem.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Content.Server.DeviceNetwork;
  2. using Content.Server.DeviceNetwork.Components;
  3. using Content.Shared.CartridgeLoader;
  4. using Content.Shared.CartridgeLoader.Cartridges;
  5. using Content.Shared.Popups;
  6. using Robust.Shared.Audio;
  7. using Robust.Shared.Audio.Systems;
  8. using Robust.Shared.Player;
  9. using Robust.Shared.Random;
  10. namespace Content.Server.CartridgeLoader.Cartridges;
  11. public sealed class NetProbeCartridgeSystem : EntitySystem
  12. {
  13. [Dependency] private readonly CartridgeLoaderSystem? _cartridgeLoaderSystem = default!;
  14. [Dependency] private readonly IRobustRandom _random = default!;
  15. [Dependency] private readonly SharedPopupSystem _popupSystem = default!;
  16. [Dependency] private readonly SharedAudioSystem _audioSystem = default!;
  17. public override void Initialize()
  18. {
  19. base.Initialize();
  20. SubscribeLocalEvent<NetProbeCartridgeComponent, CartridgeUiReadyEvent>(OnUiReady);
  21. SubscribeLocalEvent<NetProbeCartridgeComponent, CartridgeAfterInteractEvent>(AfterInteract);
  22. }
  23. /// <summary>
  24. /// The <see cref="CartridgeAfterInteractEvent" /> gets relayed to this system if the cartridge loader is running
  25. /// the NetProbe program and someone clicks on something with it. <br/>
  26. /// <br/>
  27. /// Saves name, address... etc. of the device that was clicked into a list on the component when the device isn't already present in that list
  28. /// </summary>
  29. private void AfterInteract(EntityUid uid, NetProbeCartridgeComponent component, CartridgeAfterInteractEvent args)
  30. {
  31. if (args.InteractEvent.Handled || !args.InteractEvent.CanReach || !args.InteractEvent.Target.HasValue)
  32. return;
  33. var target = args.InteractEvent.Target.Value;
  34. DeviceNetworkComponent? networkComponent = default;
  35. if (!Resolve(target, ref networkComponent, false))
  36. return;
  37. //Ceck if device is already present in list
  38. foreach (var probedDevice in component.ProbedDevices)
  39. {
  40. if (probedDevice.Address == networkComponent.Address)
  41. return;
  42. }
  43. //Play scanning sound with slightly randomized pitch
  44. //Why is there no NextFloat(float min, float max)???
  45. var audioParams = AudioParams.Default.WithVolume(-2f).WithPitchScale((float)_random.Next(12, 21) / 10);
  46. _audioSystem.PlayEntity(component.SoundScan, args.InteractEvent.User, target, audioParams);
  47. _popupSystem.PopupCursor(Loc.GetString("net-probe-scan", ("device", target)), args.InteractEvent.User);
  48. //Limit the amount of saved probe results to 9
  49. //This is hardcoded because the UI doesn't support a dynamic number of results
  50. if (component.ProbedDevices.Count >= component.MaxSavedDevices)
  51. component.ProbedDevices.RemoveAt(0);
  52. var device = new ProbedNetworkDevice(
  53. Name(target),
  54. networkComponent.Address,
  55. networkComponent.ReceiveFrequency?.FrequencyToString() ?? string.Empty,
  56. networkComponent.DeviceNetId.DeviceNetIdToLocalizedName()
  57. );
  58. component.ProbedDevices.Add(device);
  59. UpdateUiState(uid, args.Loader, component);
  60. }
  61. /// <summary>
  62. /// This gets called when the ui fragment needs to be updated for the first time after activating
  63. /// </summary>
  64. private void OnUiReady(EntityUid uid, NetProbeCartridgeComponent component, CartridgeUiReadyEvent args)
  65. {
  66. UpdateUiState(uid, args.Loader, component);
  67. }
  68. private void UpdateUiState(EntityUid uid, EntityUid loaderUid, NetProbeCartridgeComponent? component)
  69. {
  70. if (!Resolve(uid, ref component))
  71. return;
  72. var state = new NetProbeUiState(component.ProbedDevices);
  73. _cartridgeLoaderSystem?.UpdateCartridgeUiState(loaderUid, state);
  74. }
  75. }