RandomWeatherRuleSystem.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using Content.Server.GameTicking.Rules.Components;
  2. using Content.Shared.GameTicking.Components;
  3. using Robust.Shared.Random;
  4. using Content.Shared.Weather;
  5. using Robust.Shared.Timing;
  6. using System; // For TimeSpan
  7. using Robust.Shared.Prototypes;
  8. using Robust.Shared.Map;
  9. using System.Collections.Generic;
  10. using Robust.Shared.Map.Components;
  11. namespace Content.Server.GameTicking.Rules;
  12. /// <summary>
  13. /// This handles the weather for a specific map
  14. /// </summary>
  15. public sealed class RandomWeatherRuleSystem : GameRuleSystem<RandomWeatherRuleComponent>
  16. {
  17. [Dependency] private readonly IRobustRandom _random = default!;
  18. [Dependency] private readonly ILogManager _logManager = default!;
  19. [Dependency] private readonly SharedWeatherSystem _weather = default!;
  20. [Dependency] private readonly IPrototypeManager _protoManager = default!;
  21. [Dependency] private readonly IGameTiming _gameTiming = default!;
  22. [Dependency] private readonly IMapManager _mapManager = default!;
  23. [Dependency] private readonly IEntityManager _entManager = default!;
  24. private ISawmill _sawmill = default!;
  25. /// <inheritdoc/>
  26. public override void Initialize()
  27. {
  28. base.Initialize();
  29. _sawmill = _logManager.GetSawmill("random-weather-rule");
  30. // The base GameRuleSystem<T> already subscribes to GameRuleStartedEvent
  31. // and calls the virtual Started() method. No need to subscribe manually here.
  32. }
  33. protected override void Started(EntityUid uid, RandomWeatherRuleComponent component, GameRuleComponent gameRule, GameRuleStartedEvent args)
  34. {
  35. base.Started(uid, component, gameRule, args); // It's good practice to call the base method.
  36. PickRandomWeather(uid, component);
  37. PickRandomDaytime(uid, component);
  38. }
  39. /// <summary>
  40. /// Picks a random weather from the component's AllowedWeathers list and sets it as the CurrentWeather.
  41. /// <summary>
  42. /// Selects and applies a random weather type from the allowed list to all maps, setting the weather for one hour if it has not already been initialised.
  43. /// </summary>
  44. /// <remarks>
  45. /// If the allowed weather list is empty, no weather is set. If the selected weather is not recognised, the operation is aborted. Weather is only set once per rule activation.
  46. /// </remarks>
  47. public void PickRandomWeather(EntityUid uid, RandomWeatherRuleComponent? component = null)
  48. {
  49. if (!Resolve(uid, ref component))
  50. return;
  51. if (component.AllowedWeathers.Count == 0)
  52. {
  53. _sawmill.Warning($"AllowedWeathers list is empty for RandomWeatherRule on {ToPrettyString(uid)}. Rule will not pick a new weather.");
  54. return;
  55. }
  56. component.CurrentWeather = _random.Pick(component.AllowedWeathers);
  57. // Get the MapId from the entity's transform
  58. _sawmill.Info($"Selected weather: {component.CurrentWeather}");
  59. var endTime = _gameTiming.CurTime + TimeSpan.FromSeconds(3600); // Weather duration of 1 hour
  60. WeatherPrototype? weather = null;
  61. if (component.CurrentWeather != "Clear")
  62. {
  63. if (!_protoManager.TryIndex(component.CurrentWeather, out weather))
  64. {
  65. _sawmill.Error($"Unknown weather {component.CurrentWeather}!");
  66. return;
  67. }
  68. }
  69. foreach (var mapId in _mapManager.GetAllMapIds())
  70. {
  71. if (component.WeatherInitialised == false)
  72. {
  73. _weather.SetWeather(mapId, weather, endTime);
  74. component.WeatherInitialised = true;
  75. }
  76. }
  77. }
  78. public void PickRandomDaytime(EntityUid uid, RandomWeatherRuleComponent? component = null)
  79. {
  80. if (!Resolve(uid, ref component))
  81. return;
  82. var chosenDaylight = _random.Pick(component.DayTimes);
  83. _sawmill.Info($"Selected daytime: {chosenDaylight}");
  84. var pickedLight = "#D8B059";
  85. if (chosenDaylight == "Day")
  86. {
  87. pickedLight = "#D8B059";
  88. }
  89. else if (chosenDaylight == "Dawn" || chosenDaylight == "Dusk")
  90. {
  91. pickedLight = "#cf7330";
  92. }
  93. else if (chosenDaylight == "Night")
  94. {
  95. pickedLight = "#2b3143";
  96. }
  97. foreach (var mapId in _mapManager.GetAllMapIds())
  98. {
  99. var mapEntityUid = _mapManager.GetMapEntityId(mapId);
  100. var lighting = _entManager.EnsureComponent<MapLightComponent>(mapEntityUid);
  101. var color = Color.TryFromHex(pickedLight);
  102. if (color.HasValue)
  103. {
  104. lighting.AmbientLightColor = color.Value;
  105. _entManager.Dirty(mapEntityUid, lighting);
  106. }
  107. }
  108. }
  109. }