RampingStationEventSchedulerSystem.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using Content.Server.GameTicking;
  2. using Content.Server.GameTicking.Rules;
  3. using Content.Server.StationEvents.Components;
  4. using Content.Shared.GameTicking.Components;
  5. using Robust.Shared.Random;
  6. namespace Content.Server.StationEvents;
  7. public sealed class RampingStationEventSchedulerSystem : GameRuleSystem<RampingStationEventSchedulerComponent>
  8. {
  9. [Dependency] private readonly IRobustRandom _random = default!;
  10. [Dependency] private readonly EventManagerSystem _event = default!;
  11. [Dependency] private readonly GameTicker _gameTicker = default!;
  12. /// <summary>
  13. /// Returns the ChaosModifier which increases as round time increases to a point.
  14. /// </summary>
  15. public float GetChaosModifier(EntityUid uid, RampingStationEventSchedulerComponent component)
  16. {
  17. var roundTime = (float) _gameTicker.RoundDuration().TotalSeconds;
  18. if (roundTime > component.EndTime)
  19. return component.MaxChaos;
  20. return component.MaxChaos / component.EndTime * roundTime + component.StartingChaos;
  21. }
  22. protected override void Started(EntityUid uid, RampingStationEventSchedulerComponent component, GameRuleComponent gameRule, GameRuleStartedEvent args)
  23. {
  24. base.Started(uid, component, gameRule, args);
  25. // Worlds shittiest probability distribution
  26. // Got a complaint? Send them to
  27. component.MaxChaos = _random.NextFloat(component.AverageChaos - component.AverageChaos / 4, component.AverageChaos + component.AverageChaos / 4);
  28. // This is in minutes, so *60 for seconds (for the chaos calc)
  29. component.EndTime = _random.NextFloat(component.AverageEndTime - component.AverageEndTime / 4, component.AverageEndTime + component.AverageEndTime / 4) * 60f;
  30. component.StartingChaos = component.MaxChaos / 10;
  31. PickNextEventTime(uid, component);
  32. }
  33. public override void Update(float frameTime)
  34. {
  35. base.Update(frameTime);
  36. if (!_event.EventsEnabled)
  37. return;
  38. var query = EntityQueryEnumerator<RampingStationEventSchedulerComponent, GameRuleComponent>();
  39. while (query.MoveNext(out var uid, out var scheduler, out var gameRule))
  40. {
  41. if (!GameTicker.IsGameRuleActive(uid, gameRule))
  42. continue;
  43. if (scheduler.TimeUntilNextEvent > 0f)
  44. {
  45. scheduler.TimeUntilNextEvent -= frameTime;
  46. continue;
  47. }
  48. PickNextEventTime(uid, scheduler);
  49. _event.RunRandomEvent(scheduler.ScheduledGameRules);
  50. }
  51. }
  52. /// <summary>
  53. /// Sets the timing of the next event addition.
  54. /// </summary>
  55. private void PickNextEventTime(EntityUid uid, RampingStationEventSchedulerComponent component)
  56. {
  57. var mod = GetChaosModifier(uid, component);
  58. // 4-12 minutes baseline. Will get faster over time as the chaos mod increases.
  59. component.TimeUntilNextEvent = _random.NextFloat(240f / mod, 720f / mod);
  60. }
  61. }