Files
wwdpublic/Content.Server/StationEvents/BasicStationEventSchedulerSystem.cs
DEATHB4DEFEAT 1132b058ff Fewer Mid-Round Events (#486)
# Description

Ports and improves
https://github.com/Simple-Station/Parkstation-Friendly-Chainsaw/pull/59

Slows rounds down so the station doesn't always have to evacuate in a
blaze and can instead leave complete.
Also makes Survival not stupidly destructive and gives a chance for the
shift to last a decent time.
Allows servers to configure the times themselves if they want to, so
defaults don't matter too much.

---

# Changelog

🆑
- tweak: Mid-round events will occur much less often
2024-06-25 16:47:09 -04:00

118 lines
4.0 KiB
C#

using System.Linq;
using Content.Server.Administration;
using Content.Server.GameTicking.Rules;
using Content.Server.GameTicking.Rules.Components;
using Content.Server.StationEvents.Components;
using Content.Shared.Administration;
using Content.Shared.CCVar;
using JetBrains.Annotations;
using Robust.Shared.Configuration;
using Robust.Shared.Random;
using Robust.Shared.Toolshed;
using Robust.Shared.Utility;
namespace Content.Server.StationEvents
{
/// <summary>
/// The basic event scheduler rule, loosely based off of /tg/ events, which most
/// game presets use.
/// </summary>
[UsedImplicitly]
public sealed class BasicStationEventSchedulerSystem : GameRuleSystem<BasicStationEventSchedulerComponent>
{
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly EventManagerSystem _event = default!;
[Dependency] private readonly IConfigurationManager _config = default!;
protected override void Ended(EntityUid uid, BasicStationEventSchedulerComponent component, GameRuleComponent gameRule,
GameRuleEndedEvent args)
{
component.TimeUntilNextEvent = BasicStationEventSchedulerComponent.MinimumTimeUntilFirstEvent;
}
public override void Update(float frameTime)
{
base.Update(frameTime);
if (!_event.EventsEnabled)
return;
var query = EntityQueryEnumerator<BasicStationEventSchedulerComponent, GameRuleComponent>();
while (query.MoveNext(out var uid, out var eventScheduler, out var gameRule))
{
if (!GameTicker.IsGameRuleActive(uid, gameRule))
continue;
if (eventScheduler.TimeUntilNextEvent > 0)
{
eventScheduler.TimeUntilNextEvent -= frameTime;
return;
}
_event.RunRandomEvent();
ResetTimer(eventScheduler);
}
}
/// <summary>
/// Reset the event timer once the event is done.
/// </summary>
private void ResetTimer(BasicStationEventSchedulerComponent component)
{
component.TimeUntilNextEvent = _random.Next(_config.GetCVar(CCVars.GameEventsBasicMinimumTime),
_config.GetCVar(CCVars.GameEventsBasicMaximumTime));
}
}
[ToolshedCommand, AdminCommand(AdminFlags.Debug)]
public sealed class StationEventCommand : ToolshedCommand
{
private EventManagerSystem? _stationEvent;
[CommandImplementation("lsprob")]
public IEnumerable<(string, float)> LsProb()
{
_stationEvent ??= GetSys<EventManagerSystem>();
var events = _stationEvent.AllEvents();
var totalWeight = events.Sum(x => x.Value.Weight);
foreach (var (proto, comp) in events)
{
yield return (proto.ID, comp.Weight / totalWeight);
}
}
[CommandImplementation("lsprobtime")]
public IEnumerable<(string, float)> LsProbTime([CommandArgument] float time)
{
_stationEvent ??= GetSys<EventManagerSystem>();
var events = _stationEvent.AllEvents().Where(pair => pair.Value.EarliestStart <= time).ToList();
var totalWeight = events.Sum(x => x.Value.Weight);
foreach (var (proto, comp) in events)
{
yield return (proto.ID, comp.Weight / totalWeight);
}
}
[CommandImplementation("prob")]
public float Prob([CommandArgument] string eventId)
{
_stationEvent ??= GetSys<EventManagerSystem>();
var events = _stationEvent.AllEvents();
var totalWeight = events.Sum(x => x.Value.Weight);
var weight = 0f;
if (events.TryFirstOrNull(p => p.Key.ID == eventId, out var pair))
{
weight = pair.Value.Value.Weight;
}
return weight / totalWeight;
}
}
}