Latejoin traitors (#9783)

This commit is contained in:
Rane
2022-07-28 20:59:45 -04:00
committed by GitHub
parent 3433ecee77
commit b8be23004b
5 changed files with 67 additions and 11 deletions

View File

@@ -1,7 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using Content.Server.Chat.Managers;
using Content.Server.GameTicking.Rules.Configurations;
using Content.Server.Objectives.Interfaces;
using Content.Server.Players;
using Content.Server.Roles;
@@ -42,12 +40,16 @@ public sealed class TraitorRuleSystem : GameRuleSystem
public int TotalTraitors => Traitors.Count;
public string[] Codewords = new string[3];
private int _playersPerTraitor => _cfg.GetCVar(CCVars.TraitorPlayersPerTraitor);
private int _maxTraitors => _cfg.GetCVar(CCVars.TraitorMaxTraitors);
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<RoundStartAttemptEvent>(OnStartAttempt);
SubscribeLocalEvent<RulePlayerJobsAssignedEvent>(OnPlayersSpawned);
SubscribeLocalEvent<PlayerSpawnCompleteEvent>(HandleLatejoin);
SubscribeLocalEvent<RoundEndTextAppendEvent>(OnRoundEndText);
}
@@ -107,9 +109,8 @@ public sealed class TraitorRuleSystem : GameRuleSystem
if (!RuleAdded)
return;
var playersPerTraitor = _cfg.GetCVar(CCVars.TraitorPlayersPerTraitor);
var maxTraitors = _cfg.GetCVar(CCVars.TraitorMaxTraitors);
var numTraitors = MathHelper.Clamp(ev.Players.Length / playersPerTraitor, 1, maxTraitors);
var numTraitors = MathHelper.Clamp(ev.Players.Length / _playersPerTraitor, 1, _maxTraitors);
var codewordCount = _cfg.GetCVar(CCVars.TraitorCodewordCount);
var traitorPool = FindPotentialTraitors(ev);
var selectedTraitors = PickTraitors(numTraitors, traitorPool);
@@ -154,7 +155,7 @@ public sealed class TraitorRuleSystem : GameRuleSystem
Logger.InfoS("preset", "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));
@@ -211,6 +212,48 @@ public sealed class TraitorRuleSystem : GameRuleSystem
return true;
}
private void HandleLatejoin(PlayerSpawnCompleteEvent ev)
{
if (!RuleAdded)
return;
if (TotalTraitors >= _maxTraitors)
return;
if (!ev.LateJoin)
return;
if (!ev.Profile.AntagPreferences.Contains(TraitorPrototypeID))
return;
if (ev.JobId == null || !_prototypeManager.TryIndex<JobPrototype>(ev.JobId, out var job))
return;
if (!job.CanBeAntag)
return;
// the nth player we adjust our probabilities around
int target = ((_playersPerTraitor * TotalTraitors) + 1);
float chance = (1f / _playersPerTraitor);
/// If we have too many traitors, divide by how many players below target for next traitor we are.
if (ev.JoinOrder < target)
{
chance /= (target - ev.JoinOrder);
} else // Tick up towards 100% chance.
{
chance *= ((ev.JoinOrder + 1) - target);
}
if (chance > 1)
chance = 1;
// Now that we've calculated our chance, roll and make them a traitor if we roll under.
// You get one shot.
if (_random.Prob((float) chance))
{
MakeTraitor(ev.Player);
}
}
private void OnRoundEndText(RoundEndTextAppendEvent ev)
{
if (!RuleAdded)