Event refactor (#9589)

* Station event refactor

* Remove clientside `IStationEventManager`

we can just use prototypes

* Basic API idea

* Cruft

* first attempt at epicness

* okay yeah this shit is really clean

* sort out minor stuff

* Convert `BreakerFlip`

* `BureaucraticError` + general cleanup

* `DiseaseOutbreak`

* `FalseAlarm`

* `GasLeak`

* `KudzuGrowth`

* `MeteorSwarm`

* `MouseMigration`

* misc errors

* `PowerGridCheck`

* `RandomSentience`

* `VentClog`

* `VentCritters`

* `ZombieOutbreak`

* Rewrite basic event scheduler

* Minor fixes and logging

* ooooops

* errors + fix

* linter

* completions, `RuleStarted` property, update loop fixes

* Tweaks

* Fix #9462

* Basic scheduler update fix, and fixes #8174

* Add test

* UI cleanup

* really this was just for testing
This commit is contained in:
Kara
2022-07-10 18:48:41 -07:00
committed by GitHub
parent f28cdaaa7c
commit b9a0894d7c
55 changed files with 1095 additions and 1582 deletions

View File

@@ -0,0 +1,76 @@
using Content.Shared.Sound;
using JetBrains.Annotations;
namespace Content.Server.GameTicking.Rules.Configurations;
/// <summary>
/// Defines a configuration for a given station event game rule, since all station events are just
/// game rules.
/// </summary>
[UsedImplicitly]
public sealed class StationEventRuleConfiguration : GameRuleConfiguration
{
[DataField("id", required: true)]
private string _id = default!;
public override string Id => _id;
public const float WeightVeryLow = 0.0f;
public const float WeightLow = 5.0f;
public const float WeightNormal = 10.0f;
public const float WeightHigh = 15.0f;
public const float WeightVeryHigh = 20.0f;
[DataField("weight")]
public float Weight = WeightNormal;
[DataField("startAnnouncement")]
public string? StartAnnouncement;
[DataField("endAnnouncement")]
public string? EndAnnouncement;
[DataField("startAudio")]
public SoundSpecifier? StartAudio;
[DataField("endAudio")]
public SoundSpecifier? EndAudio;
/// <summary>
/// In minutes, when is the first round time this event can start
/// </summary>
[DataField("earliestStart")]
public int EarliestStart = 5;
/// <summary>
/// In minutes, the amount of time before the same event can occur again
/// </summary>
[DataField("reoccurrenceDelay")]
public int ReoccurrenceDelay = 30;
/// <summary>
/// When in the lifetime to start the event.
/// </summary>
[DataField("startAfter")]
public float StartAfter;
/// <summary>
/// When in the lifetime to end the event..
/// </summary>
[DataField("endAfter")]
public float EndAfter = float.MaxValue;
/// <summary>
/// How many players need to be present on station for the event to run
/// </summary>
/// <remarks>
/// To avoid running deadly events with low-pop
/// </remarks>
[DataField("minimumPlayers")]
public int MinimumPlayers;
/// <summary>
/// How many times this even can occur in a single round
/// </summary>
[DataField("maxOccurrences")]
public int? MaxOccurrences;
}

View File

@@ -34,14 +34,14 @@ public sealed class DeathMatchRuleSystem : GameRuleSystem
SubscribeLocalEvent<DamageChangedEvent>(OnHealthChanged);
}
public override void Started(GameRuleConfiguration _)
public override void Started()
{
_chatManager.DispatchServerAnnouncement(Loc.GetString("rule-death-match-added-announcement"));
_playerManager.PlayerStatusChanged += OnPlayerStatusChanged;
}
public override void Ended(GameRuleConfiguration _)
public override void Ended()
{
_deadCheckTimer = null;
_restartTimer = null;
@@ -64,7 +64,7 @@ public sealed class DeathMatchRuleSystem : GameRuleSystem
private void RunDelayedCheck()
{
if (!Enabled || _deadCheckTimer != null)
if (!RuleAdded || _deadCheckTimer != null)
return;
_deadCheckTimer = DeadCheckDelay;
@@ -72,7 +72,7 @@ public sealed class DeathMatchRuleSystem : GameRuleSystem
public override void Update(float frameTime)
{
if (!Enabled)
if (!RuleAdded)
return;
// If the restart timer is active, that means the round is ending soon, no need to check for winners.

View File

@@ -9,10 +9,16 @@ public abstract class GameRuleSystem : EntitySystem
[Dependency] protected GameTicker GameTicker = default!;
/// <summary>
/// Whether this GameRule is currently enabled or not.
/// Whether this GameRule is currently added or not.
/// Be sure to check this before doing anything rule-specific.
/// </summary>
public bool Enabled { get; protected set; } = false;
public bool RuleAdded { get; protected set; }
/// <summary>
/// Whether this game rule has been started after being added.
/// You probably want to check this before doing any update loop stuff.
/// </summary>
public bool RuleStarted { get; protected set; }
/// <summary>
/// When the GameRule prototype with this ID is added, this system will be enabled.
@@ -20,6 +26,12 @@ public abstract class GameRuleSystem : EntitySystem
/// </summary>
public new abstract string Prototype { get; }
/// <summary>
/// Holds the current configuration after the event has been added.
/// This should not be getting accessed before the event is enabled, as usual.
/// </summary>
public GameRuleConfiguration Configuration = default!;
public override void Initialize()
{
base.Initialize();
@@ -35,7 +47,10 @@ public abstract class GameRuleSystem : EntitySystem
if (ev.Rule.Configuration.Id != Prototype)
return;
Enabled = true;
Configuration = ev.Rule.Configuration;
RuleAdded = true;
Added();
}
private void OnGameRuleStarted(GameRuleStartedEvent ev)
@@ -43,7 +58,9 @@ public abstract class GameRuleSystem : EntitySystem
if (ev.Rule.Configuration.Id != Prototype)
return;
Started(ev.Rule.Configuration);
RuleStarted = true;
Started();
}
private void OnGameRuleEnded(GameRuleEndedEvent ev)
@@ -51,17 +68,27 @@ public abstract class GameRuleSystem : EntitySystem
if (ev.Rule.Configuration.Id != Prototype)
return;
Enabled = false;
Ended(ev.Rule.Configuration);
RuleAdded = false;
RuleStarted = false;
Ended();
}
/// <summary>
/// Called when the game rule has been started..
/// Called when the game rule has been added.
/// You should avoid using this in favor of started--they are not the same thing.
/// </summary>
public abstract void Started(GameRuleConfiguration configuration);
/// <remarks>
/// This is virtual because it doesn't actually have to be used, and most of the time shouldn't be.
/// </remarks>
public virtual void Added() { }
/// <summary>
/// Called when the game rule has ended..
/// Called when the game rule has been started.
/// </summary>
public abstract void Ended(GameRuleConfiguration configuration);
public abstract void Started();
/// <summary>
/// Called when the game rule has ended.
/// </summary>
public abstract void Ended();
}

View File

@@ -25,16 +25,16 @@ public sealed class InactivityTimeRestartRuleSystem : GameRuleSystem
SubscribeLocalEvent<GameRunLevelChangedEvent>(RunLevelChanged);
}
public override void Started(GameRuleConfiguration config)
public override void Started()
{
if (config is not InactivityGameRuleConfiguration inactivityConfig)
if (Configuration is not InactivityGameRuleConfiguration inactivityConfig)
return;
InactivityMaxTime = inactivityConfig.InactivityMaxTime;
RoundEndDelay = inactivityConfig.RoundEndDelay;
_playerManager.PlayerStatusChanged += PlayerStatusChanged;
}
public override void Ended(GameRuleConfiguration _)
public override void Ended()
{
_playerManager.PlayerStatusChanged -= PlayerStatusChanged;
@@ -64,7 +64,7 @@ public sealed class InactivityTimeRestartRuleSystem : GameRuleSystem
private void RunLevelChanged(GameRunLevelChangedEvent args)
{
if (!Enabled)
if (!RuleAdded)
return;
switch (args.New)

View File

@@ -23,10 +23,11 @@ public sealed class MaxTimeRestartRuleSystem : GameRuleSystem
SubscribeLocalEvent<GameRunLevelChangedEvent>(RunLevelChanged);
}
public override void Started(GameRuleConfiguration config)
public override void Started()
{
if (config is not MaxTimeRestartRuleConfiguration maxTimeRestartConfig)
if (Configuration is not MaxTimeRestartRuleConfiguration maxTimeRestartConfig)
return;
RoundMaxTime = maxTimeRestartConfig.RoundMaxTime;
RoundEndDelay = maxTimeRestartConfig.RoundEndDelay;
@@ -34,7 +35,7 @@ public sealed class MaxTimeRestartRuleSystem : GameRuleSystem
RestartTimer();
}
public override void Ended(GameRuleConfiguration _)
public override void Ended()
{
StopTimer();
}
@@ -62,7 +63,7 @@ public sealed class MaxTimeRestartRuleSystem : GameRuleSystem
private void RunLevelChanged(GameRunLevelChangedEvent args)
{
if (!Enabled)
if (!RuleAdded)
return;
switch (args.New)

View File

@@ -56,7 +56,7 @@ public sealed class NukeopsRuleSystem : GameRuleSystem
private void OnNukeExploded(NukeExplodedEvent ev)
{
if (!Enabled)
if (!RuleAdded)
return;
_opsWon = true;
@@ -65,7 +65,7 @@ public sealed class NukeopsRuleSystem : GameRuleSystem
private void OnRoundEndText(RoundEndTextAppendEvent ev)
{
if (!Enabled)
if (!RuleAdded)
return;
ev.AddLine(_opsWon ? Loc.GetString("nukeops-ops-won") : Loc.GetString("nukeops-crew-won"));
@@ -78,7 +78,7 @@ public sealed class NukeopsRuleSystem : GameRuleSystem
private void OnMobStateChanged(MobStateChangedEvent ev)
{
if (!Enabled)
if (!RuleAdded)
return;
if (!_aliveNukeops.TryFirstOrNull(x => x.Key.OwnedEntity == ev.Entity, out var op)) return;
@@ -93,7 +93,7 @@ public sealed class NukeopsRuleSystem : GameRuleSystem
private void OnPlayersSpawning(RulePlayerSpawningEvent ev)
{
if (!Enabled)
if (!RuleAdded)
return;
_aliveNukeops.Clear();
@@ -292,7 +292,7 @@ public sealed class NukeopsRuleSystem : GameRuleSystem
private void OnStartAttempt(RoundStartAttemptEvent ev)
{
if (!Enabled)
if (!RuleAdded)
return;
var minPlayers = _cfg.GetCVar(CCVars.NukeopsMinPlayers);
@@ -311,11 +311,10 @@ public sealed class NukeopsRuleSystem : GameRuleSystem
}
}
public override void Started(GameRuleConfiguration _)
public override void Started()
{
_opsWon = false;
}
public override void Ended(GameRuleConfiguration _) { }
public override void Ended() { }
}

View File

@@ -57,7 +57,7 @@ public sealed class PiratesRuleSystem : GameRuleSystem
private void OnRoundEndTextEvent(RoundEndTextAppendEvent ev)
{
if (!Enabled)
if (!RuleAdded)
return;
if (Deleted(_pirateShip))
@@ -120,14 +120,14 @@ public sealed class PiratesRuleSystem : GameRuleSystem
}
}
public override void Started(GameRuleConfiguration _) { }
public override void Started() { }
public override void Ended(GameRuleConfiguration _) { }
public override void Ended() { }
private void OnPlayerSpawningEvent(RulePlayerSpawningEvent ev)
{
// Forgive me for copy-pasting nukies.
if (!Enabled)
if (!RuleAdded)
{
return;
}
@@ -225,7 +225,7 @@ public sealed class PiratesRuleSystem : GameRuleSystem
private void OnStartAttempt(RoundStartAttemptEvent ev)
{
if (!Enabled)
if (!RuleAdded)
return;
var minPlayers = _cfg.GetCVar(CCVars.PiratesMinPlayers);

View File

@@ -9,12 +9,12 @@ public sealed class SandboxRuleSystem : GameRuleSystem
public override string Prototype => "Sandbox";
public override void Started(GameRuleConfiguration _)
public override void Started()
{
_sandbox.IsSandboxEnabled = true;
}
public override void Ended(GameRuleConfiguration _)
public override void Ended()
{
_sandbox.IsSandboxEnabled = false;
}

View File

@@ -16,12 +16,12 @@ public sealed class SecretRuleSystem : GameRuleSystem
public override string Prototype => "Secret";
public override void Started(GameRuleConfiguration _)
public override void Started()
{
PickRule();
}
public override void Ended(GameRuleConfiguration _)
public override void Ended()
{
// noop
// Preset should already handle it.

View File

@@ -97,7 +97,7 @@ public sealed class SuspicionRuleSystem : GameRuleSystem
private void OnRoundStartAttempt(RoundStartAttemptEvent ev)
{
if (!Enabled)
if (!RuleAdded)
return;
var minPlayers = _cfg.GetCVar(CCVars.SuspicionMinPlayers);
@@ -119,7 +119,7 @@ public sealed class SuspicionRuleSystem : GameRuleSystem
private void OnPlayersAssigned(RulePlayerJobsAssignedEvent ev)
{
if (!Enabled)
if (!RuleAdded)
return;
var minTraitors = _cfg.GetCVar(CCVars.SuspicionMinTraitors);
@@ -203,7 +203,7 @@ public sealed class SuspicionRuleSystem : GameRuleSystem
}
}
public override void Started(GameRuleConfiguration _)
public override void Started()
{
_playerManager.PlayerStatusChanged += PlayerManagerOnPlayerStatusChanged;
@@ -269,7 +269,7 @@ public sealed class SuspicionRuleSystem : GameRuleSystem
Timer.SpawnRepeating(DeadCheckDelay, CheckWinConditions, _checkTimerCancel.Token);
}
public override void Ended(GameRuleConfiguration _)
public override void Ended()
{
_doorSystem.AccessType = SharedDoorSystem.AccessTypes.Id;
EndTime = null;
@@ -288,7 +288,7 @@ public sealed class SuspicionRuleSystem : GameRuleSystem
private void CheckWinConditions()
{
if (!Enabled || !_cfg.GetCVar(CCVars.GameLobbyEnableWin))
if (!RuleAdded || !_cfg.GetCVar(CCVars.GameLobbyEnableWin))
return;
var traitorsAlive = 0;
@@ -457,7 +457,7 @@ public sealed class SuspicionRuleSystem : GameRuleSystem
private void OnLateJoinRefresh(RefreshLateJoinAllowedEvent ev)
{
if (!Enabled)
if (!RuleAdded)
return;
ev.Disallow();

View File

@@ -63,7 +63,7 @@ public sealed class TraitorDeathMatchRuleSystem : GameRuleSystem
private void OnPlayerSpawned(PlayerSpawnCompleteEvent ev)
{
if (!Enabled)
if (!RuleAdded)
return;
var session = ev.Player;
@@ -144,7 +144,7 @@ public sealed class TraitorDeathMatchRuleSystem : GameRuleSystem
private void OnGhostAttempt(GhostAttemptHandleEvent ev)
{
if (!Enabled || ev.Handled)
if (!RuleAdded || ev.Handled)
return;
ev.Handled = true;
@@ -181,7 +181,7 @@ public sealed class TraitorDeathMatchRuleSystem : GameRuleSystem
private void OnRoundEndText(RoundEndTextAppendEvent ev)
{
if (!Enabled)
if (!RuleAdded)
return;
var lines = new List<string>();
@@ -200,14 +200,14 @@ public sealed class TraitorDeathMatchRuleSystem : GameRuleSystem
ev.AddLine(string.Join('\n', lines));
}
public override void Started(GameRuleConfiguration _)
public override void Started()
{
_restarter.RoundMaxTime = TimeSpan.FromMinutes(30);
_restarter.RestartTimer();
_safeToEndRound = true;
}
public override void Ended(GameRuleConfiguration _)
public override void Ended()
{
}

View File

@@ -49,16 +49,16 @@ public sealed class TraitorRuleSystem : GameRuleSystem
SubscribeLocalEvent<RoundEndTextAppendEvent>(OnRoundEndText);
}
public override void Started(GameRuleConfiguration _) {}
public override void Started() {}
public override void Ended(GameRuleConfiguration _)
public override void Ended()
{
_traitors.Clear();
}
private void OnStartAttempt(RoundStartAttemptEvent ev)
{
if (!Enabled)
if (!RuleAdded)
return;
// If the current preset doesn't explicitly contain the traitor game rule, just carry on and remove self.
@@ -86,7 +86,7 @@ public sealed class TraitorRuleSystem : GameRuleSystem
private void OnPlayersSpawned(RulePlayerJobsAssignedEvent ev)
{
if (!Enabled)
if (!RuleAdded)
return;
var playersPerTraitor = _cfg.GetCVar(CCVars.TraitorPlayersPerTraitor);
@@ -197,7 +197,7 @@ public sealed class TraitorRuleSystem : GameRuleSystem
private void OnRoundEndText(RoundEndTextAppendEvent ev)
{
if (!Enabled)
if (!RuleAdded)
return;
var result = Loc.GetString("traitor-round-end-result", ("traitorCount", _traitors.Count));

View File

@@ -65,7 +65,7 @@ public sealed class ZombieRuleSystem : GameRuleSystem
private void OnRoundEndText(RoundEndTextAppendEvent ev)
{
if (!Enabled)
if (!RuleAdded)
return;
//this is just the general condition thing used for determining the win/lose text
@@ -113,7 +113,7 @@ public sealed class ZombieRuleSystem : GameRuleSystem
private void OnJobAssigned(RulePlayerJobsAssignedEvent ev)
{
if (!Enabled)
if (!RuleAdded)
return;
_initialInfectedNames = new();
@@ -127,14 +127,14 @@ public sealed class ZombieRuleSystem : GameRuleSystem
/// </remarks>
private void OnMobStateChanged(MobStateChangedEvent ev)
{
if (!Enabled)
if (!RuleAdded)
return;
CheckRoundEnd(ev.Entity);
}
private void OnEntityZombified(EntityZombifiedEvent ev)
{
if (!Enabled)
if (!RuleAdded)
return;
CheckRoundEnd(ev.Target);
}
@@ -158,7 +158,7 @@ public sealed class ZombieRuleSystem : GameRuleSystem
private void OnStartAttempt(RoundStartAttemptEvent ev)
{
if (!Enabled)
if (!RuleAdded)
return;
var minPlayers = _cfg.GetCVar(CCVars.ZombieMinPlayers);
@@ -177,13 +177,13 @@ public sealed class ZombieRuleSystem : GameRuleSystem
}
}
public override void Started(GameRuleConfiguration configuration)
public override void Started()
{
//this technically will run twice with zombies on roundstart, but it doesn't matter because it fails instantly
InfectInitialPlayers();
}
public override void Ended(GameRuleConfiguration configuration) { }
public override void Ended() { }
private void OnZombifySelf(EntityUid uid, ZombifyOnDeathComponent component, ZombifySelfActionEvent args)
{