RandomWeatherRuleSystem.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. public void PickRandomWeather(EntityUid uid, RandomWeatherRuleComponent? component = null)
  43. {
  44. if (!Resolve(uid, ref component))
  45. return;
  46. if (component.AllowedWeathers.Count == 0)
  47. {
  48. _sawmill.Warning($"AllowedWeathers list is empty for RandomWeatherRule on {ToPrettyString(uid)}. Rule will not pick a new weather.");
  49. return;
  50. }
  51. component.CurrentWeather = _random.Pick(component.AllowedWeathers);
  52. // Get the MapId from the entity's transform
  53. _sawmill.Info($"Selected weather: {component.CurrentWeather}");
  54. var endTime = _gameTiming.CurTime + TimeSpan.FromSeconds(3600); // Weather duration of 1 hour
  55. WeatherPrototype? weather = null;
  56. if (component.CurrentWeather != "Clear")
  57. {
  58. if (!_protoManager.TryIndex(component.CurrentWeather, out weather))
  59. {
  60. _sawmill.Error($"Unknown weather {component.CurrentWeather}!");
  61. return;
  62. }
  63. }
  64. foreach (var mapId in _mapManager.GetAllMapIds())
  65. {
  66. _weather.SetWeather(mapId, weather, endTime);
  67. }
  68. }
  69. public void PickRandomDaytime(EntityUid uid, RandomWeatherRuleComponent? component = null)
  70. {
  71. if (!Resolve(uid, ref component))
  72. return;
  73. var chosenDaylight = _random.Pick(component.DayTimes);
  74. _sawmill.Info($"Selected daytime: {chosenDaylight}");
  75. var pickedLight = "#D8B059";
  76. if (chosenDaylight == "Day")
  77. {
  78. pickedLight = "#D8B059";
  79. }
  80. else if (chosenDaylight == "Dawn" || chosenDaylight == "Dusk")
  81. {
  82. pickedLight = "#cf7330";
  83. }
  84. else if (chosenDaylight == "Night")
  85. {
  86. pickedLight = "#2b3143";
  87. }
  88. foreach (var mapId in _mapManager.GetAllMapIds())
  89. {
  90. var mapEntityUid = _mapManager.GetMapEntityId(mapId);
  91. var lighting = _entManager.EnsureComponent<MapLightComponent>(mapEntityUid);
  92. var color = Color.TryFromHex(pickedLight);
  93. if (color.HasValue)
  94. {
  95. lighting.AmbientLightColor = color.Value;
  96. _entManager.Dirty(mapEntityUid, lighting);
  97. }
  98. }
  99. }
  100. }