1
0

RadiationSystem.Debug.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System.Linq;
  2. using Content.Server.Administration;
  3. using Content.Server.Radiation.Components;
  4. using Content.Shared.Administration;
  5. using Content.Shared.Radiation.Events;
  6. using Content.Shared.Radiation.Systems;
  7. using Robust.Shared.Console;
  8. using Robust.Shared.Debugging;
  9. using Robust.Shared.Enums;
  10. using Robust.Shared.Map.Components;
  11. using Robust.Shared.Player;
  12. namespace Content.Server.Radiation.Systems;
  13. // radiation overlay debug logic
  14. // rad rays send only to clients that enabled debug overlay
  15. public partial class RadiationSystem
  16. {
  17. private readonly HashSet<ICommonSession> _debugSessions = new();
  18. /// <summary>
  19. /// Toggle radiation debug overlay for selected player.
  20. /// </summary>
  21. public void ToggleDebugView(ICommonSession session)
  22. {
  23. bool isEnabled;
  24. if (_debugSessions.Add(session))
  25. {
  26. isEnabled = true;
  27. }
  28. else
  29. {
  30. _debugSessions.Remove(session);
  31. isEnabled = false;
  32. }
  33. var ev = new OnRadiationOverlayToggledEvent(isEnabled);
  34. RaiseNetworkEvent(ev, session.Channel);
  35. }
  36. /// <summary>
  37. /// Send new information for radiation overlay.
  38. /// </summary>
  39. private void UpdateDebugOverlay(EntityEventArgs ev)
  40. {
  41. foreach (var session in _debugSessions)
  42. {
  43. if (session.Status != SessionStatus.InGame)
  44. _debugSessions.Remove(session);
  45. else
  46. RaiseNetworkEvent(ev, session);
  47. }
  48. }
  49. private void UpdateResistanceDebugOverlay()
  50. {
  51. if (_debugSessions.Count == 0)
  52. return;
  53. var dict = new Dictionary<NetEntity, Dictionary<Vector2i, float>>();
  54. var gridQuery = AllEntityQuery<MapGridComponent, RadiationGridResistanceComponent>();
  55. while (gridQuery.MoveNext(out var gridUid, out _, out var resistance))
  56. {
  57. var resMap = resistance.ResistancePerTile;
  58. dict.Add(GetNetEntity(gridUid), resMap);
  59. }
  60. var ev = new OnRadiationOverlayResistanceUpdateEvent(dict);
  61. UpdateDebugOverlay(ev);
  62. }
  63. private void UpdateGridcastDebugOverlay(
  64. double elapsedTime,
  65. int totalSources,
  66. int totalReceivers,
  67. List<DebugRadiationRay>? rays)
  68. {
  69. if (_debugSessions.Count == 0)
  70. return;
  71. var ev = new OnRadiationOverlayUpdateEvent(elapsedTime, totalSources, totalReceivers, rays ?? new());
  72. UpdateDebugOverlay(ev);
  73. }
  74. }
  75. /// <summary>
  76. /// Toggle visibility of radiation rays coming from rad sources.
  77. /// </summary>
  78. [AdminCommand(AdminFlags.Admin)]
  79. public sealed class RadiationViewCommand : IConsoleCommand
  80. {
  81. public string Command => "showradiation";
  82. public string Description => Loc.GetString("radiation-command-description");
  83. public string Help => Loc.GetString("radiation-command-help");
  84. public void Execute(IConsoleShell shell, string argStr, string[] args)
  85. {
  86. var session = shell.Player;
  87. if (session == null)
  88. return;
  89. var entityManager = IoCManager.Resolve<IEntityManager>();
  90. entityManager.System<RadiationSystem>().ToggleDebugView(session);
  91. }
  92. }