Delay the selection of traitors at round start. (#13161)

Co-authored-by: Kara <lunarautomaton6@gmail.com>
This commit is contained in:
Vordenburg
2023-01-20 11:18:47 -05:00
committed by GitHub
parent 6f58f5a36c
commit 7b1d93b281
2 changed files with 83 additions and 19 deletions

View File

@@ -8,6 +8,7 @@ using Content.Server.Traitor.Uplink;
using Content.Server.NPC.Systems; using Content.Server.NPC.Systems;
using Content.Shared.CCVar; using Content.Shared.CCVar;
using Content.Shared.Dataset; using Content.Shared.Dataset;
using Content.Shared.Preferences;
using Content.Shared.Mobs.Systems; using Content.Shared.Mobs.Systems;
using Content.Shared.Roles; using Content.Shared.Roles;
using Robust.Server.Player; using Robust.Server.Player;
@@ -17,6 +18,7 @@ using Robust.Shared.Player;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
using Robust.Shared.Random; using Robust.Shared.Random;
using Robust.Shared.Utility; using Robust.Shared.Utility;
using Robust.Shared.Timing;
namespace Content.Server.GameTicking.Rules; namespace Content.Server.GameTicking.Rules;
@@ -27,10 +29,14 @@ public sealed class TraitorRuleSystem : GameRuleSystem
[Dependency] private readonly IConfigurationManager _cfg = default!; [Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly IObjectivesManager _objectivesManager = default!; [Dependency] private readonly IObjectivesManager _objectivesManager = default!;
[Dependency] private readonly IChatManager _chatManager = 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 FactionSystem _faction = default!;
[Dependency] private readonly MobStateSystem _mobStateSystem = default!; [Dependency] private readonly MobStateSystem _mobStateSystem = default!;
[Dependency] private readonly UplinkSystem _uplink = default!; [Dependency] private readonly UplinkSystem _uplink = default!;
[Dependency] private readonly SharedAudioSystem _audioSystem = default!;
private ISawmill _sawmill = default!;
public override string Prototype => "Traitor"; public override string Prototype => "Traitor";
@@ -46,21 +52,44 @@ public sealed class TraitorRuleSystem : GameRuleSystem
private int _playersPerTraitor => _cfg.GetCVar(CCVars.TraitorPlayersPerTraitor); private int _playersPerTraitor => _cfg.GetCVar(CCVars.TraitorPlayersPerTraitor);
private int _maxTraitors => _cfg.GetCVar(CCVars.TraitorMaxTraitors); 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<IPlayerSession, HumanoidCharacterProfile> _startCandidates = new();
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
_sawmill = Logger.GetSawmill("preset");
SubscribeLocalEvent<RoundStartAttemptEvent>(OnStartAttempt); SubscribeLocalEvent<RoundStartAttemptEvent>(OnStartAttempt);
SubscribeLocalEvent<RulePlayerJobsAssignedEvent>(OnPlayersSpawned); SubscribeLocalEvent<RulePlayerJobsAssignedEvent>(OnPlayersSpawned);
SubscribeLocalEvent<PlayerSpawnCompleteEvent>(HandleLatejoin); SubscribeLocalEvent<PlayerSpawnCompleteEvent>(HandleLatejoin);
SubscribeLocalEvent<RoundEndTextAppendEvent>(OnRoundEndText); SubscribeLocalEvent<RoundEndTextAppendEvent>(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 Started(){}
public override void Ended() public override void Ended()
{ {
Traitors.Clear(); Traitors.Clear();
_startCandidates.Clear();
SelectionStatus = SelectionState.WaitingForSpawn;
} }
private void OnStartAttempt(RoundStartAttemptEvent ev) private void OnStartAttempt(RoundStartAttemptEvent ev)
@@ -86,7 +115,6 @@ public sealed class TraitorRuleSystem : GameRuleSystem
private void MakeCodewords() private void MakeCodewords()
{ {
var codewordCount = _cfg.GetCVar(CCVars.TraitorCodewordCount); var codewordCount = _cfg.GetCVar(CCVars.TraitorCodewordCount);
var adjectives = _prototypeManager.Index<DatasetPrototype>("adjectives").Values; var adjectives = _prototypeManager.Index<DatasetPrototype>("adjectives").Values;
var verbs = _prototypeManager.Index<DatasetPrototype>("verbs").Values; var verbs = _prototypeManager.Index<DatasetPrototype>("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) private void OnPlayersSpawned(RulePlayerJobsAssignedEvent ev)
{ {
if (!RuleAdded) if (!RuleAdded)
return; return;
var numTraitors = MathHelper.Clamp(ev.Players.Length / _playersPerTraitor, 1, _maxTraitors); foreach (var player in ev.Players)
var codewordCount = _cfg.GetCVar(CCVars.TraitorCodewordCount); {
if (!ev.Profiles.ContainsKey(player.UserId))
continue;
var traitorPool = FindPotentialTraitors(ev); _startCandidates[player] = ev.Profiles[player.UserId];
var selectedTraitors = PickTraitors(numTraitors, traitorPool); }
foreach (var traitor in selectedTraitors) var delay = TimeSpan.FromSeconds(
MakeTraitor(traitor); _cfg.GetCVar(CCVars.TraitorStartDelay) +
_random.NextFloat(0f, _cfg.GetCVar(CCVars.TraitorStartDelayVariance)));
_announceAt = _gameTiming.CurTime + delay;
SelectionStatus = SelectionState.ReadyToSelect;
} }
public List<IPlayerSession> FindPotentialTraitors(RulePlayerJobsAssignedEvent ev) public List<IPlayerSession> FindPotentialTraitors(in Dictionary<IPlayerSession, HumanoidCharacterProfile> candidates)
{ {
var list = new List<IPlayerSession>(ev.Players).Where(x => var list = new List<IPlayerSession>(candidates.Keys).Where(x =>
x.Data.ContentData()?.Mind?.AllRoles.All(role => role is not Job { CanBeAntag: false }) ?? false x.Data.ContentData()?.Mind?.AllRoles.All(role => role is not Job { CanBeAntag: false }) ?? false
).ToList(); ).ToList();
@@ -124,11 +179,7 @@ public sealed class TraitorRuleSystem : GameRuleSystem
foreach (var player in list) foreach (var player in list)
{ {
if (!ev.Profiles.ContainsKey(player.UserId)) var profile = candidates[player];
{
continue;
}
var profile = ev.Profiles[player.UserId];
if (profile.AntagPreferences.Contains(TraitorPrototypeID)) if (profile.AntagPreferences.Contains(TraitorPrototypeID))
{ {
prefList.Add(player); prefList.Add(player);
@@ -136,7 +187,7 @@ public sealed class TraitorRuleSystem : GameRuleSystem
} }
if (prefList.Count == 0) if (prefList.Count == 0)
{ {
Logger.InfoS("preset", "Insufficient preferred traitors, picking at random."); _sawmill.Info("Insufficient preferred traitors, picking at random.");
prefList = list; prefList = list;
} }
return prefList; return prefList;
@@ -147,14 +198,14 @@ public sealed class TraitorRuleSystem : GameRuleSystem
var results = new List<IPlayerSession>(traitorCount); var results = new List<IPlayerSession>(traitorCount);
if (prefList.Count == 0) 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; return results;
} }
for (var i = 0; i < traitorCount; i++) for (var i = 0; i < traitorCount; i++)
{ {
results.Add(_random.PickAndTake(prefList)); results.Add(_random.PickAndTake(prefList));
Logger.InfoS("preset", "Selected a preferred traitor."); _sawmill.Info("Selected a preferred traitor.");
} }
return results; return results;
} }
@@ -164,7 +215,7 @@ public sealed class TraitorRuleSystem : GameRuleSystem
var mind = traitor.Data.ContentData()?.Mind; var mind = traitor.Data.ContentData()?.Mind;
if (mind == null) if (mind == null)
{ {
Logger.ErrorS("preset", "Failed getting mind for picked traitor."); _sawmill.Info("Failed getting mind for picked traitor.");
return false; return false;
} }
@@ -211,7 +262,7 @@ public sealed class TraitorRuleSystem : GameRuleSystem
//give traitors their codewords to keep in their character info menu //give traitors their codewords to keep in their character info menu
traitorRole.Mind.Briefing = Loc.GetString("traitor-role-codewords", ("codewords", string.Join(", ", Codewords))); 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; return true;
} }
@@ -233,6 +284,13 @@ public sealed class TraitorRuleSystem : GameRuleSystem
if (!job.CanBeAntag) if (!job.CanBeAntag)
return; 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 // the nth player we adjust our probabilities around
int target = ((_playersPerTraitor * TotalTraitors) + 1); int target = ((_playersPerTraitor * TotalTraitors) + 1);

View File

@@ -352,6 +352,12 @@ namespace Content.Shared.CCVar
public static readonly CVarDef<int> TraitorMaxPicks = public static readonly CVarDef<int> TraitorMaxPicks =
CVarDef.Create("traitor.max_picks", 20); CVarDef.Create("traitor.max_picks", 20);
public static readonly CVarDef<float> TraitorStartDelay =
CVarDef.Create("traitor.start_delay", 4f * 60f);
public static readonly CVarDef<float> TraitorStartDelayVariance =
CVarDef.Create("traitor.start_delay_variance", 3f * 60f);
/* /*
* TraitorDeathMatch * TraitorDeathMatch
*/ */