diff --git a/Content.Server/GameTicking/Rules/TraitorRuleSystem.cs b/Content.Server/GameTicking/Rules/TraitorRuleSystem.cs index 40bcc06259..d1941e9ffa 100644 --- a/Content.Server/GameTicking/Rules/TraitorRuleSystem.cs +++ b/Content.Server/GameTicking/Rules/TraitorRuleSystem.cs @@ -8,6 +8,7 @@ using Content.Server.Traitor.Uplink; using Content.Server.NPC.Systems; using Content.Shared.CCVar; using Content.Shared.Dataset; +using Content.Shared.Preferences; using Content.Shared.Mobs.Systems; using Content.Shared.Roles; using Robust.Server.Player; @@ -17,6 +18,7 @@ using Robust.Shared.Player; using Robust.Shared.Prototypes; using Robust.Shared.Random; using Robust.Shared.Utility; +using Robust.Shared.Timing; namespace Content.Server.GameTicking.Rules; @@ -27,10 +29,14 @@ public sealed class TraitorRuleSystem : GameRuleSystem [Dependency] private readonly IConfigurationManager _cfg = default!; [Dependency] private readonly IObjectivesManager _objectivesManager = default!; [Dependency] private readonly IChatManager _chatManager = default!; + [Dependency] private readonly IGameTiming _gameTiming = default!; + [Dependency] private readonly GameTicker _gameTicker = default!; [Dependency] private readonly FactionSystem _faction = default!; [Dependency] private readonly MobStateSystem _mobStateSystem = default!; [Dependency] private readonly UplinkSystem _uplink = default!; + [Dependency] private readonly SharedAudioSystem _audioSystem = default!; + private ISawmill _sawmill = default!; public override string Prototype => "Traitor"; @@ -46,21 +52,44 @@ public sealed class TraitorRuleSystem : GameRuleSystem private int _playersPerTraitor => _cfg.GetCVar(CCVars.TraitorPlayersPerTraitor); private int _maxTraitors => _cfg.GetCVar(CCVars.TraitorMaxTraitors); + public enum SelectionState + { + WaitingForSpawn = 0, + ReadyToSelect = 1, + SelectionMade = 2, + } + + public SelectionState SelectionStatus = SelectionState.WaitingForSpawn; + private TimeSpan _announceAt = TimeSpan.Zero; + private Dictionary _startCandidates = new(); + public override void Initialize() { base.Initialize(); + _sawmill = Logger.GetSawmill("preset"); + SubscribeLocalEvent(OnStartAttempt); SubscribeLocalEvent(OnPlayersSpawned); SubscribeLocalEvent(HandleLatejoin); SubscribeLocalEvent(OnRoundEndText); } + public override void Update(float frameTime) + { + base.Update(frameTime); + + if (SelectionStatus == SelectionState.ReadyToSelect && _gameTiming.CurTime >= _announceAt) + DoTraitorStart(); + } + public override void Started(){} public override void Ended() { Traitors.Clear(); + _startCandidates.Clear(); + SelectionStatus = SelectionState.WaitingForSpawn; } private void OnStartAttempt(RoundStartAttemptEvent ev) @@ -86,7 +115,6 @@ public sealed class TraitorRuleSystem : GameRuleSystem private void MakeCodewords() { - var codewordCount = _cfg.GetCVar(CCVars.TraitorCodewordCount); var adjectives = _prototypeManager.Index("adjectives").Values; var verbs = _prototypeManager.Index("verbs").Values; @@ -99,24 +127,51 @@ public sealed class TraitorRuleSystem : GameRuleSystem } } + private void DoTraitorStart() + { + if (!_startCandidates.Any()) + { + _sawmill.Error("Tried to start Traitor mode without any candidates."); + return; + } + + var numTraitors = MathHelper.Clamp(_startCandidates.Count / _playersPerTraitor, 1, _maxTraitors); + var codewordCount = _cfg.GetCVar(CCVars.TraitorCodewordCount); + + var traitorPool = FindPotentialTraitors(_startCandidates); + var selectedTraitors = PickTraitors(numTraitors, traitorPool); + + foreach (var traitor in selectedTraitors) + MakeTraitor(traitor); + + SelectionStatus = SelectionState.SelectionMade; + } + private void OnPlayersSpawned(RulePlayerJobsAssignedEvent ev) { if (!RuleAdded) return; - var numTraitors = MathHelper.Clamp(ev.Players.Length / _playersPerTraitor, 1, _maxTraitors); - var codewordCount = _cfg.GetCVar(CCVars.TraitorCodewordCount); + foreach (var player in ev.Players) + { + if (!ev.Profiles.ContainsKey(player.UserId)) + continue; - var traitorPool = FindPotentialTraitors(ev); - var selectedTraitors = PickTraitors(numTraitors, traitorPool); + _startCandidates[player] = ev.Profiles[player.UserId]; + } - foreach (var traitor in selectedTraitors) - MakeTraitor(traitor); + var delay = TimeSpan.FromSeconds( + _cfg.GetCVar(CCVars.TraitorStartDelay) + + _random.NextFloat(0f, _cfg.GetCVar(CCVars.TraitorStartDelayVariance))); + + _announceAt = _gameTiming.CurTime + delay; + + SelectionStatus = SelectionState.ReadyToSelect; } - public List FindPotentialTraitors(RulePlayerJobsAssignedEvent ev) + public List FindPotentialTraitors(in Dictionary candidates) { - var list = new List(ev.Players).Where(x => + var list = new List(candidates.Keys).Where(x => x.Data.ContentData()?.Mind?.AllRoles.All(role => role is not Job { CanBeAntag: false }) ?? false ).ToList(); @@ -124,11 +179,7 @@ public sealed class TraitorRuleSystem : GameRuleSystem foreach (var player in list) { - if (!ev.Profiles.ContainsKey(player.UserId)) - { - continue; - } - var profile = ev.Profiles[player.UserId]; + var profile = candidates[player]; if (profile.AntagPreferences.Contains(TraitorPrototypeID)) { prefList.Add(player); @@ -136,7 +187,7 @@ public sealed class TraitorRuleSystem : GameRuleSystem } if (prefList.Count == 0) { - Logger.InfoS("preset", "Insufficient preferred traitors, picking at random."); + _sawmill.Info("Insufficient preferred traitors, picking at random."); prefList = list; } return prefList; @@ -147,14 +198,14 @@ public sealed class TraitorRuleSystem : GameRuleSystem var results = new List(traitorCount); if (prefList.Count == 0) { - Logger.InfoS("preset", "Insufficient ready players to fill up with traitors, stopping the selection."); + _sawmill.Info("Insufficient ready players to fill up with traitors, stopping the selection."); return results; } for (var i = 0; i < traitorCount; i++) { results.Add(_random.PickAndTake(prefList)); - Logger.InfoS("preset", "Selected a preferred traitor."); + _sawmill.Info("Selected a preferred traitor."); } return results; } @@ -164,7 +215,7 @@ public sealed class TraitorRuleSystem : GameRuleSystem var mind = traitor.Data.ContentData()?.Mind; if (mind == null) { - Logger.ErrorS("preset", "Failed getting mind for picked traitor."); + _sawmill.Info("Failed getting mind for picked traitor."); return false; } @@ -211,7 +262,7 @@ public sealed class TraitorRuleSystem : GameRuleSystem //give traitors their codewords to keep in their character info menu traitorRole.Mind.Briefing = Loc.GetString("traitor-role-codewords", ("codewords", string.Join(", ", Codewords))); - SoundSystem.Play(_addedSound.GetSound(), Filter.Empty().AddPlayer(traitor), AudioParams.Default); + _audioSystem.PlayGlobal(_addedSound, Filter.Empty().AddPlayer(traitor), false, AudioParams.Default); return true; } @@ -233,6 +284,13 @@ public sealed class TraitorRuleSystem : GameRuleSystem if (!job.CanBeAntag) return; + // Before the announcement is made, late-joiners are considered the same as players who readied. + if (SelectionStatus < SelectionState.SelectionMade) + { + _startCandidates[ev.Player] = ev.Profile; + return; + } + // the nth player we adjust our probabilities around int target = ((_playersPerTraitor * TotalTraitors) + 1); diff --git a/Content.Shared/CCVar/CCVars.cs b/Content.Shared/CCVar/CCVars.cs index f857a08d4e..dd624cfb06 100644 --- a/Content.Shared/CCVar/CCVars.cs +++ b/Content.Shared/CCVar/CCVars.cs @@ -352,6 +352,12 @@ namespace Content.Shared.CCVar public static readonly CVarDef TraitorMaxPicks = CVarDef.Create("traitor.max_picks", 20); + public static readonly CVarDef TraitorStartDelay = + CVarDef.Create("traitor.start_delay", 4f * 60f); + + public static readonly CVarDef TraitorStartDelayVariance = + CVarDef.Create("traitor.start_delay_variance", 3f * 60f); + /* * TraitorDeathMatch */