refactor event schedulers to use explicit game rules (#29320)

* works, still has testing values, im sure I did stupid shit.

* shitvent crapfactor

* snap extra word out of existence

* shit I died of old

* remove useless inaccurate design comments

* Oopsie, handle requirement params in RandomRuleSystem too

* I'm a slash slinging hasher

* Address reviews, add admin alerts I forgor

* EntityMan saves the day

* address reviews 1

* eh, I actually don't care about the cargo gifts thing.

* started

* Do reviews

* you actually meant 1.2 lmao

* dependency inheritance is a fickle bitch

* I have no idea.

* Threads are for sheets not computers.

* fix traitor rule test

* fix round type tattling

* break things

* It worky

* Toolshed makes we want to drink depresso.

* Finished?

* remove debug values

* timings

* use defaults

* alphabetize

* bobby drop tables

* Float required fr fr

* continue

* more continence

* uno mas

* obsolution

* cleanup and documentations

* Yell at self

* use the right value defaults

* housekeeping
This commit is contained in:
IProduceWidgets
2024-08-14 01:21:01 -04:00
committed by GitHub
parent 58a33b2593
commit 07ec00ed05
21 changed files with 600 additions and 241 deletions

View File

@@ -9,6 +9,7 @@ using JetBrains.Annotations;
using Robust.Shared.Console;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;
using Robust.Shared.Localization;
namespace Content.Server.GameTicking;
@@ -71,6 +72,16 @@ public sealed partial class GameTicker
var ruleEntity = Spawn(ruleId, MapCoordinates.Nullspace);
_sawmill.Info($"Added game rule {ToPrettyString(ruleEntity)}");
_adminLogger.Add(LogType.EventStarted, $"Added game rule {ToPrettyString(ruleEntity)}");
var str = Loc.GetString("station-event-system-run-event", ("eventName", ToPrettyString(ruleEntity)));
#if DEBUG
_chatManager.SendAdminAlert(str);
#else
if (RunLevel == GameRunLevel.InRound) // avoids telling admins the round type before it starts so that can be handled elsewhere.
{
_chatManager.SendAdminAlert(str);
}
#endif
Log.Info(str);
var ev = new GameRuleAddedEvent(ruleEntity, ruleId);
RaiseLocalEvent(ruleEntity, ref ev, true);

View File

@@ -2,11 +2,12 @@ using System.Linq;
using Content.Server.Administration;
using Content.Server.GameTicking;
using Content.Server.GameTicking.Rules;
using Content.Server.GameTicking.Rules.Components;
using Content.Server.StationEvents.Components;
using Content.Shared.Administration;
using Content.Shared.EntityTable;
using Content.Shared.GameTicking.Components;
using JetBrains.Annotations;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Toolshed;
using Robust.Shared.Utility;
@@ -23,13 +24,17 @@ namespace Content.Server.StationEvents
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly EventManagerSystem _event = default!;
public const float MinEventTime = 60 * 3;
public const float MaxEventTime = 60 * 10;
protected override void Started(EntityUid uid, BasicStationEventSchedulerComponent component, GameRuleComponent gameRule,
GameRuleStartedEvent args)
{
// A little starting variance so schedulers dont all proc at once.
component.TimeUntilNextEvent = RobustRandom.NextFloat(component.MinimumTimeUntilFirstEvent, component.MinimumTimeUntilFirstEvent + 120);
}
protected override void Ended(EntityUid uid, BasicStationEventSchedulerComponent component, GameRuleComponent gameRule,
GameRuleEndedEvent args)
{
component.TimeUntilNextEvent = BasicStationEventSchedulerComponent.MinimumTimeUntilFirstEvent;
component.TimeUntilNextEvent = component.MinimumTimeUntilFirstEvent;
}
@@ -49,10 +54,10 @@ namespace Content.Server.StationEvents
if (eventScheduler.TimeUntilNextEvent > 0)
{
eventScheduler.TimeUntilNextEvent -= frameTime;
return;
continue;
}
_event.RunRandomEvent();
_event.RunRandomEvent(eventScheduler.ScheduledGameRules);
ResetTimer(eventScheduler);
}
}
@@ -62,7 +67,7 @@ namespace Content.Server.StationEvents
/// </summary>
private void ResetTimer(BasicStationEventSchedulerComponent component)
{
component.TimeUntilNextEvent = _random.NextFloat(MinEventTime, MaxEventTime);
component.TimeUntilNextEvent = component.MinMaxEventTiming.Next(_random);
}
}
@@ -70,7 +75,8 @@ namespace Content.Server.StationEvents
public sealed class StationEventCommand : ToolshedCommand
{
private EventManagerSystem? _stationEvent;
private BasicStationEventSchedulerSystem? _basicScheduler;
private EntityTableSystem? _entityTable;
private IComponentFactory? _compFac;
private IRobustRandom? _random;
/// <summary>
@@ -88,10 +94,11 @@ namespace Content.Server.StationEvents
/// to even exist) so I think it's fine.
/// </remarks>
[CommandImplementation("simulate")]
public IEnumerable<(string, float)> Simulate([CommandArgument] int rounds, [CommandArgument] int playerCount, [CommandArgument] float roundEndMean, [CommandArgument] float roundEndStdDev)
public IEnumerable<(string, float)> Simulate([CommandArgument] EntityPrototype eventScheduler, [CommandArgument] int rounds, [CommandArgument] int playerCount, [CommandArgument] float roundEndMean, [CommandArgument] float roundEndStdDev)
{
_stationEvent ??= GetSys<EventManagerSystem>();
_basicScheduler ??= GetSys<BasicStationEventSchedulerSystem>();
_entityTable ??= GetSys<EntityTableSystem>();
_compFac ??= IoCManager.Resolve<IComponentFactory>();
_random ??= IoCManager.Resolve<IRobustRandom>();
var occurrences = new Dictionary<string, int>();
@@ -101,6 +108,13 @@ namespace Content.Server.StationEvents
occurrences.Add(ev.Key.ID, 0);
}
if (!eventScheduler.TryGetComponent<BasicStationEventSchedulerComponent>(out var basicScheduler, _compFac))
{
return occurrences.Select(p => (p.Key, (float)p.Value)).OrderByDescending(p => p.Item2);
}
var compMinMax = basicScheduler.MinMaxEventTiming; // we gotta do this since we cant execute on comp w/o an ent.
for (var i = 0; i < rounds; i++)
{
var curTime = TimeSpan.Zero;
@@ -111,9 +125,16 @@ namespace Content.Server.StationEvents
while (curTime.TotalSeconds < randomEndTime)
{
// sim an event
curTime += TimeSpan.FromSeconds(_random.NextFloat(BasicStationEventSchedulerSystem.MinEventTime, BasicStationEventSchedulerSystem.MaxEventTime));
curTime += TimeSpan.FromSeconds(compMinMax.Next(_random));
if (!_stationEvent.TryBuildLimitedEvents(basicScheduler.ScheduledGameRules, out var selectedEvents))
{
continue; // doesnt break because maybe the time is preventing events being available.
}
var available = _stationEvent.AvailableEvents(false, playerCount, curTime);
var ev = _stationEvent.FindEvent(available);
var plausibleEvents = new Dictionary<EntityPrototype, StationEventComponent>(available.Intersect(selectedEvents)); // C# makes me sad
var ev = _stationEvent.FindEvent(plausibleEvents);
if (ev == null)
continue;
@@ -125,26 +146,40 @@ namespace Content.Server.StationEvents
}
[CommandImplementation("lsprob")]
public IEnumerable<(string, float)> LsProb()
public IEnumerable<(string, float)> LsProb([CommandArgument] EntityPrototype eventScheduler)
{
_compFac ??= IoCManager.Resolve<IComponentFactory>();
_stationEvent ??= GetSys<EventManagerSystem>();
var events = _stationEvent.AllEvents();
var totalWeight = events.Sum(x => x.Value.Weight);
if (!eventScheduler.TryGetComponent<BasicStationEventSchedulerComponent>(out var basicScheduler, _compFac))
yield break;
foreach (var (proto, comp) in events)
if (!_stationEvent.TryBuildLimitedEvents(basicScheduler.ScheduledGameRules, out var events))
yield break;
var totalWeight = events.Sum(x => x.Value.Weight); // Well this shit definitely isnt correct now, and I see no way to make it correct.
// Its probably *fine* but it wont be accurate if the EntityTableSelector does any subsetting.
foreach (var (proto, comp) in events) // The only solution I see is to do a simulation, and we already have that, so...!
{
yield return (proto.ID, comp.Weight / totalWeight);
}
}
[CommandImplementation("lsprobtime")]
public IEnumerable<(string, float)> LsProbTime([CommandArgument] float time)
public IEnumerable<(string, float)> LsProbTime([CommandArgument] EntityPrototype eventScheduler, [CommandArgument] float time)
{
_compFac ??= IoCManager.Resolve<IComponentFactory>();
_stationEvent ??= GetSys<EventManagerSystem>();
var events = _stationEvent.AllEvents().Where(pair => pair.Value.EarliestStart <= time).ToList();
var totalWeight = events.Sum(x => x.Value.Weight);
if (!eventScheduler.TryGetComponent<BasicStationEventSchedulerComponent>(out var basicScheduler, _compFac))
yield break;
if (!_stationEvent.TryBuildLimitedEvents(basicScheduler.ScheduledGameRules, out var untimedEvents))
yield break;
var events = untimedEvents.Where(pair => pair.Value.EarliestStart <= time).ToList();
var totalWeight = events.Sum(x => x.Value.Weight); // same subsetting issue as lsprob.
foreach (var (proto, comp) in events)
{
@@ -153,12 +188,18 @@ namespace Content.Server.StationEvents
}
[CommandImplementation("prob")]
public float Prob([CommandArgument] string eventId)
public float Prob([CommandArgument] EntityPrototype eventScheduler, [CommandArgument] string eventId)
{
_compFac ??= IoCManager.Resolve<IComponentFactory>();
_stationEvent ??= GetSys<EventManagerSystem>();
var events = _stationEvent.AllEvents();
var totalWeight = events.Sum(x => x.Value.Weight);
if (!eventScheduler.TryGetComponent<BasicStationEventSchedulerComponent>(out var basicScheduler, _compFac))
return 0f;
if (!_stationEvent.TryBuildLimitedEvents(basicScheduler.ScheduledGameRules, out var events))
return 0f;
var totalWeight = events.Sum(x => x.Value.Weight); // same subsetting issue as lsprob.
var weight = 0f;
if (events.TryFirstOrNull(p => p.Key.ID == eventId, out var pair))
{

View File

@@ -1,14 +1,35 @@
namespace Content.Server.StationEvents.Components;
using Content.Shared.Destructible.Thresholds;
using Content.Shared.EntityTable.EntitySelectors;
namespace Content.Server.StationEvents.Components;
[RegisterComponent, Access(typeof(BasicStationEventSchedulerSystem))]
public sealed partial class BasicStationEventSchedulerComponent : Component
{
public const float MinimumTimeUntilFirstEvent = 300;
/// <summary>
/// How long the the scheduler waits to begin starting rules.
/// </summary>
[DataField]
public float MinimumTimeUntilFirstEvent = 200;
/// <summary>
/// How long until the next check for an event runs
/// The minimum and maximum time between rule starts in seconds.
/// </summary>
/// Default value is how long until first event is allowed
[ViewVariables(VVAccess.ReadWrite)]
public float TimeUntilNextEvent = MinimumTimeUntilFirstEvent;
[DataField]
public MinMax MinMaxEventTiming = new(3 * 60, 10 * 60);
/// <summary>
/// How long until the next check for an event runs, is initially set based on MinimumTimeUntilFirstEvent & MinMaxEventTiming.
/// </summary>
[DataField]
public float TimeUntilNextEvent;
/// <summary>
/// The gamerules that the scheduler can choose from
/// </summary>
/// Reminder that though we could do all selection via the EntityTableSelector, we also need to consider various <see cref="StationEventComponent"/> restrictions.
/// As such, we want to pass a list of acceptable game rules, which are then parsed for restrictions by the <see cref="EventManagerSystem"/>.
[DataField(required: true)]
public EntityTableSelector ScheduledGameRules = default!;
}

View File

@@ -1,24 +0,0 @@
using Content.Shared.Random;
using Robust.Shared.Prototypes;
namespace Content.Server.StationEvents.Components;
/// <summary>
/// This is used for running meteor swarm events at regular intervals.
/// </summary>
[RegisterComponent, Access(typeof(MeteorSchedulerSystem)), AutoGenerateComponentPause]
public sealed partial class MeteorSchedulerComponent : Component
{
/// <summary>
/// The weights for which swarms will be selected.
/// </summary>
[DataField]
public ProtoId<WeightedRandomEntityPrototype> Config = "DefaultConfig";
/// <summary>
/// The time at which the next swarm occurs.
/// </summary>
[DataField, AutoPausedField]
public TimeSpan NextSwarmTime = TimeSpan.Zero;
}

View File

@@ -1,17 +1,41 @@
namespace Content.Server.StationEvents.Components;
using Content.Shared.EntityTable.EntitySelectors;
namespace Content.Server.StationEvents.Components;
[RegisterComponent, Access(typeof(RampingStationEventSchedulerSystem))]
public sealed partial class RampingStationEventSchedulerComponent : Component
{
[DataField("endTime"), ViewVariables(VVAccess.ReadWrite)]
/// <summary>
/// Average ending chaos modifier for the ramping event scheduler. Higher means faster.
/// Max chaos chosen for a round will deviate from this
/// </summary>
[DataField]
public float AverageChaos = 6f;
/// <summary>
/// Average time (in minutes) for when the ramping event scheduler should stop increasing the chaos modifier.
/// Close to how long you expect a round to last, so you'll probably have to tweak this on downstreams.
/// </summary>
[DataField]
public float AverageEndTime = 40f;
[DataField]
public float EndTime;
[DataField("maxChaos"), ViewVariables(VVAccess.ReadWrite)]
[DataField]
public float MaxChaos;
[DataField("startingChaos"), ViewVariables(VVAccess.ReadWrite)]
[DataField]
public float StartingChaos;
[DataField("timeUntilNextEvent"), ViewVariables(VVAccess.ReadWrite)]
[DataField]
public float TimeUntilNextEvent;
/// <summary>
/// The gamerules that the scheduler can choose from
/// </summary>
/// Reminder that though we could do all selection via the EntityTableSelector, we also need to consider various <see cref="StationEventComponent"/> restrictions.
/// As such, we want to pass a list of acceptable game rules, which are then parsed for restrictions by the <see cref="EventManagerSystem"/>.
[DataField(required: true)]
public EntityTableSelector ScheduledGameRules = default!;
}

View File

@@ -1,5 +1,4 @@
using System.Linq;
using Content.Server.Chat.Managers;
using Content.Server.GameTicking;
using Content.Server.RoundEnd;
using Content.Server.StationEvents.Components;
@@ -8,6 +7,8 @@ using Robust.Server.Player;
using Robust.Shared.Configuration;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Content.Shared.EntityTable.EntitySelectors;
using Content.Shared.EntityTable;
namespace Content.Server.StationEvents;
@@ -17,7 +18,7 @@ public sealed class EventManagerSystem : EntitySystem
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly IPrototypeManager _prototype = default!;
[Dependency] private readonly IChatManager _chat = default!;
[Dependency] private readonly EntityTableSystem _entityTable = default!;
[Dependency] public readonly GameTicker GameTicker = default!;
[Dependency] private readonly RoundEndSystem _roundEnd = default!;
@@ -34,7 +35,8 @@ public sealed class EventManagerSystem : EntitySystem
/// <summary>
/// Randomly runs a valid event.
/// </summary>
public string RunRandomEvent()
[Obsolete("use overload taking EnityTableSelector instead or risk unexpected results")]
public void RunRandomEvent()
{
var randomEvent = PickRandomEvent();
@@ -42,14 +44,86 @@ public sealed class EventManagerSystem : EntitySystem
{
var errStr = Loc.GetString("station-event-system-run-random-event-no-valid-events");
Log.Error(errStr);
return errStr;
return;
}
var ent = GameTicker.AddGameRule(randomEvent);
var str = Loc.GetString("station-event-system-run-event",("eventName", ToPrettyString(ent)));
_chat.SendAdminAlert(str);
Log.Info(str);
return str;
GameTicker.AddGameRule(randomEvent);
}
/// <summary>
/// Randomly runs an event from provided EntityTableSelector.
/// </summary>
public void RunRandomEvent(EntityTableSelector limitedEventsTable)
{
if (!TryBuildLimitedEvents(limitedEventsTable, out var limitedEvents))
{
Log.Warning("Provided event table could not build dict!");
return;
}
var randomLimitedEvent = FindEvent(limitedEvents); // this picks the event, It might be better to use the GetSpawns to do it, but that will be a major rebalancing fuck.
if (randomLimitedEvent == null)
{
Log.Warning("The selected random event is null!");
return;
}
if (!_prototype.TryIndex(randomLimitedEvent, out _))
{
Log.Warning("A requested event is not available!");
return;
}
GameTicker.AddGameRule(randomLimitedEvent);
}
/// <summary>
/// Returns true if the provided EntityTableSelector gives at least one prototype with a StationEvent comp.
/// </summary>
public bool TryBuildLimitedEvents(EntityTableSelector limitedEventsTable, out Dictionary<EntityPrototype, StationEventComponent> limitedEvents)
{
limitedEvents = new Dictionary<EntityPrototype, StationEventComponent>();
var availableEvents = AvailableEvents(); // handles the player counts and individual event restrictions
if (availableEvents.Count == 0)
{
Log.Warning("No events were available to run!");
return false;
}
var selectedEvents = _entityTable.GetSpawns(limitedEventsTable);
if (selectedEvents.Any() != true) // This is here so if you fuck up the table it wont die.
return false;
foreach (var eventid in selectedEvents)
{
if (!_prototype.TryIndex(eventid, out var eventproto))
{
Log.Warning("An event ID has no prototype index!");
continue;
}
if (limitedEvents.ContainsKey(eventproto)) // This stops it from dying if you add duplicate entries in a fucked table
continue;
if (eventproto.Abstract)
continue;
if (!eventproto.TryGetComponent<StationEventComponent>(out var stationEvent, EntityManager.ComponentFactory))
continue;
if (!availableEvents.ContainsKey(eventproto))
continue;
limitedEvents.Add(eventproto, stationEvent);
}
if (!limitedEvents.Any())
return false;
return true;
}
/// <summary>

View File

@@ -1,54 +0,0 @@
using Content.Server.GameTicking.Rules;
using Content.Server.StationEvents.Components;
using Content.Shared.CCVar;
using Content.Shared.GameTicking.Components;
using Content.Shared.Random.Helpers;
using Robust.Shared.Configuration;
using Robust.Shared.Prototypes;
namespace Content.Server.StationEvents;
/// <summary>
/// This handles scheduling and launching meteors at a station at regular intervals.
/// TODO: there is 100% a world in which this is genericized and can be used for lots of basic event scheduling
/// </summary>
public sealed class MeteorSchedulerSystem : GameRuleSystem<MeteorSchedulerComponent>
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
private TimeSpan _meteorMinDelay;
private TimeSpan _meteorMaxDelay;
public override void Initialize()
{
base.Initialize();
_cfg.OnValueChanged(CCVars.MeteorSwarmMinTime, f => { _meteorMinDelay = TimeSpan.FromMinutes(f); }, true);
_cfg.OnValueChanged(CCVars.MeteorSwarmMaxTime, f => { _meteorMaxDelay = TimeSpan.FromMinutes(f); }, true);
}
protected override void Started(EntityUid uid, MeteorSchedulerComponent component, GameRuleComponent gameRule, GameRuleStartedEvent args)
{
base.Started(uid, component, gameRule, args);
component.NextSwarmTime = Timing.CurTime + RobustRandom.Next(_meteorMinDelay, _meteorMaxDelay);
}
protected override void ActiveTick(EntityUid uid, MeteorSchedulerComponent component, GameRuleComponent gameRule, float frameTime)
{
base.ActiveTick(uid, component, gameRule, frameTime);
if (Timing.CurTime < component.NextSwarmTime)
return;
RunSwarm((uid, component));
component.NextSwarmTime += RobustRandom.Next(_meteorMinDelay, _meteorMaxDelay);
}
private void RunSwarm(Entity<MeteorSchedulerComponent> ent)
{
var swarmWeights = _prototypeManager.Index(ent.Comp.Config);
GameTicker.StartGameRule(swarmWeights.Pick(RobustRandom));
}
}

View File

@@ -1,22 +1,20 @@
using Content.Server.GameTicking;
using Content.Server.GameTicking;
using Content.Server.GameTicking.Rules;
using Content.Server.GameTicking.Rules.Components;
using Content.Server.StationEvents.Components;
using Content.Server.StationEvents.Events;
using Content.Shared.CCVar;
using Content.Shared.GameTicking.Components;
using Robust.Shared.Configuration;
using Robust.Shared.Random;
namespace Content.Server.StationEvents;
public sealed class RampingStationEventSchedulerSystem : GameRuleSystem<RampingStationEventSchedulerComponent>
{
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly EventManagerSystem _event = default!;
[Dependency] private readonly GameTicker _gameTicker = default!;
/// <summary>
/// Returns the ChaosModifier which increases as round time increases to a point.
/// </summary>
public float GetChaosModifier(EntityUid uid, RampingStationEventSchedulerComponent component)
{
var roundTime = (float) _gameTicker.RoundDuration().TotalSeconds;
@@ -30,14 +28,11 @@ public sealed class RampingStationEventSchedulerSystem : GameRuleSystem<RampingS
{
base.Started(uid, component, gameRule, args);
var avgChaos = _cfg.GetCVar(CCVars.EventsRampingAverageChaos);
var avgTime = _cfg.GetCVar(CCVars.EventsRampingAverageEndTime);
// Worlds shittiest probability distribution
// Got a complaint? Send them to
component.MaxChaos = _random.NextFloat(avgChaos - avgChaos / 4, avgChaos + avgChaos / 4);
component.MaxChaos = _random.NextFloat(component.AverageChaos - component.AverageChaos / 4, component.AverageChaos + component.AverageChaos / 4);
// This is in minutes, so *60 for seconds (for the chaos calc)
component.EndTime = _random.NextFloat(avgTime - avgTime / 4, avgTime + avgTime / 4) * 60f;
component.EndTime = _random.NextFloat(component.AverageEndTime - component.AverageEndTime / 4, component.AverageEndTime + component.AverageEndTime / 4) * 60f;
component.StartingChaos = component.MaxChaos / 10;
PickNextEventTime(uid, component);
@@ -54,19 +49,22 @@ public sealed class RampingStationEventSchedulerSystem : GameRuleSystem<RampingS
while (query.MoveNext(out var uid, out var scheduler, out var gameRule))
{
if (!GameTicker.IsGameRuleActive(uid, gameRule))
return;
continue;
if (scheduler.TimeUntilNextEvent > 0f)
{
scheduler.TimeUntilNextEvent -= frameTime;
return;
continue;
}
PickNextEventTime(uid, scheduler);
_event.RunRandomEvent();
_event.RunRandomEvent(scheduler.ScheduledGameRules);
}
}
/// <summary>
/// Sets the timing of the next event addition.
/// </summary>
private void PickNextEventTime(EntityUid uid, RampingStationEventSchedulerComponent component)
{
var mod = GetChaosModifier(uid, component);