1
0

WeatherSystem.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using Content.Server.Administration;
  2. using Content.Shared.Administration;
  3. using Content.Shared.Weather;
  4. using Robust.Shared.Console;
  5. using Robust.Shared.GameStates;
  6. using Robust.Shared.Map;
  7. using System.Linq;
  8. namespace Content.Server.Weather;
  9. public sealed class WeatherSystem : SharedWeatherSystem
  10. {
  11. [Dependency] private readonly IConsoleHost _console = default!;
  12. [Dependency] private readonly SharedMapSystem _mapSystem = default!;
  13. public override void Initialize()
  14. {
  15. base.Initialize();
  16. SubscribeLocalEvent<WeatherComponent, ComponentGetState>(OnWeatherGetState);
  17. _console.RegisterCommand("weather",
  18. Loc.GetString("cmd-weather-desc"),
  19. Loc.GetString("cmd-weather-help"),
  20. WeatherTwo,
  21. WeatherCompletion);
  22. }
  23. private void OnWeatherGetState(EntityUid uid, WeatherComponent component, ref ComponentGetState args)
  24. {
  25. args.State = new WeatherComponentState(component.Weather);
  26. }
  27. /// <summary>
  28. /// Handles the "weather" admin console command to set the weather on a specified map, optionally for a set duration, and updates precipitation states for all entities with <c>WeatherNomadsComponent</c>.
  29. /// </summary>
  30. /// <param name="shell">The console shell executing the command.</param>
  31. /// <param name="argStr">The raw argument string.</param>
  32. /// <param name="args">
  33. /// Command arguments:
  34. /// <list type="number">
  35. /// <item><description>Map ID (integer).</description></item>
  36. /// <item><description>Weather prototype ID or "null" to clear weather.</description></item>
  37. /// <item><description>(Optional) Duration in seconds for the weather effect.</description></item>
  38. /// </list>
  39. /// </param>
  40. [AdminCommand(AdminFlags.Fun)]
  41. private void WeatherTwo(IConsoleShell shell, string argStr, string[] args)
  42. {
  43. if (args.Length < 2)
  44. {
  45. shell.WriteError(Loc.GetString("cmd-weather-error-no-arguments"));
  46. return;
  47. }
  48. if (!int.TryParse(args[0], out var mapInt))
  49. return;
  50. var mapId = new MapId(mapInt);
  51. if (!MapManager.MapExists(mapId))
  52. return;
  53. if (!_mapSystem.TryGetMap(mapId, out var mapUid))
  54. return;
  55. var weatherComp = EnsureComp<WeatherComponent>(mapUid.Value);
  56. //Weather Proto parsing
  57. WeatherPrototype? weather = null;
  58. if (!args[1].Equals("null"))
  59. {
  60. if (!ProtoMan.TryIndex(args[1], out weather))
  61. {
  62. shell.WriteError(Loc.GetString("cmd-weather-error-unknown-proto"));
  63. return;
  64. }
  65. }
  66. //Time parsing
  67. TimeSpan? endTime = null;
  68. if (args.Length == 3)
  69. {
  70. var curTime = Timing.CurTime;
  71. if (int.TryParse(args[2], out var durationInt))
  72. {
  73. endTime = curTime + TimeSpan.FromSeconds(durationInt);
  74. }
  75. else
  76. {
  77. shell.WriteError(Loc.GetString("cmd-weather-error-wrong-time"));
  78. }
  79. }
  80. var nomadsweather = EntityQueryEnumerator<WeatherNomadsComponent>();
  81. while (nomadsweather.MoveNext(out var uuid, out var weatherComponent))
  82. {
  83. var parsedprec = Precipitation.Dry;
  84. if (!args[1].Equals("null"))
  85. {
  86. if (args[1].Equals("Hail") || args[1].Equals("SandstormHeavy") || args[1].Equals("Storm") || args[1].Equals("SnowfallHeavy"))
  87. {
  88. parsedprec = Precipitation.Storm;
  89. }
  90. if (args[1].Equals("Rain") || args[1].Equals("SnowfallLight"))
  91. {
  92. parsedprec = Precipitation.LightWet;
  93. }
  94. if (args[1].Equals("SnowfallMedium"))
  95. {
  96. parsedprec = Precipitation.HeavyWet;
  97. }
  98. }
  99. weatherComponent.CurrentPrecipitation = parsedprec;
  100. }
  101. SetWeather(mapId, weather, endTime);
  102. }
  103. private CompletionResult WeatherCompletion(IConsoleShell shell, string[] args)
  104. {
  105. if (args.Length == 1)
  106. return CompletionResult.FromHintOptions(CompletionHelper.MapIds(EntityManager), "Map Id");
  107. var a = CompletionHelper.PrototypeIDs<WeatherPrototype>(true, ProtoMan);
  108. var b = a.Concat(new[] { new CompletionOption("null", Loc.GetString("cmd-weather-null")) });
  109. return CompletionResult.FromHintOptions(b, Loc.GetString("cmd-weather-hint"));
  110. }
  111. }