Refactor antag rule code (#23445)
* Initial Pass, Rev, Thief * Zombie initial pass * Rebase, Traitor * Nukeops, More overloads * Revert RevolutionaryRuleComponent * Use TryRoundStartAttempt, Rewrite nukie spawning * Comments, Add task scheduler to GameRuleSystem * Zombie initial testing done * Sort methods, rework GameRuleTask * Add CCVar, Initial testing continues * Might as well get rid of the obsolete logging * Oops, i dont know how to log apparently * Suggested formatting fixes * Suggested changes * Fix merge issues * Minor optimisation * Allowed thief to choose other antags * Review changes * Spawn items on floor first, then inserting * minor tweaks * Shift as much as possible to ProtoId<> * Remove unneeded * Add exclusive antag attribute * Fix merge issues * Minor formatting fix * Convert to struct * Cleanup * Review cleanup (need to test a lot) * Some fixes, (mostly) tested * oop * Pass tests (for real) --------- Co-authored-by: Rainfall <rainfey0+git@gmail.com> Co-authored-by: AJCM <AJCM@tutanota.com>
This commit is contained in:
@@ -1,13 +1,10 @@
|
||||
using System.Linq;
|
||||
using Content.Server.Antag;
|
||||
using Content.Server.Chat.Managers;
|
||||
using Content.Server.GameTicking.Rules.Components;
|
||||
using Content.Server.Mind;
|
||||
using Content.Server.NPC.Systems;
|
||||
using Content.Server.Objectives;
|
||||
using Content.Server.PDA.Ringer;
|
||||
using Content.Server.Roles;
|
||||
using Content.Server.Shuttles.Components;
|
||||
using Content.Server.Traitor.Uplink;
|
||||
using Content.Shared.CCVar;
|
||||
using Content.Shared.Dataset;
|
||||
@@ -15,17 +12,15 @@ using Content.Shared.Mind;
|
||||
using Content.Shared.Mobs.Systems;
|
||||
using Content.Shared.Objectives.Components;
|
||||
using Content.Shared.PDA;
|
||||
using Content.Shared.Preferences;
|
||||
using Content.Shared.Roles;
|
||||
using Content.Shared.Roles.Jobs;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Audio.Systems;
|
||||
using Robust.Shared.Configuration;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Timing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Content.Server.GameTicking.Rules;
|
||||
|
||||
@@ -35,16 +30,15 @@ public sealed class TraitorRuleSystem : GameRuleSystem<TraitorRuleComponent>
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
||||
[Dependency] private readonly IChatManager _chatManager = default!;
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
||||
[Dependency] private readonly NpcFactionSystem _npcFaction = default!;
|
||||
[Dependency] private readonly MobStateSystem _mobStateSystem = default!;
|
||||
[Dependency] private readonly UplinkSystem _uplink = default!;
|
||||
[Dependency] private readonly SharedAudioSystem _audioSystem = default!;
|
||||
[Dependency] private readonly MindSystem _mindSystem = default!;
|
||||
[Dependency] private readonly SharedRoleSystem _roleSystem = default!;
|
||||
[Dependency] private readonly SharedJobSystem _jobs = default!;
|
||||
[Dependency] private readonly ObjectivesSystem _objectives = default!;
|
||||
[Dependency] private readonly IGameTiming _timing = default!;
|
||||
|
||||
private int PlayersPerTraitor => _cfg.GetCVar(CCVars.TraitorPlayersPerTraitor);
|
||||
private int MaxTraitors => _cfg.GetCVar(CCVars.TraitorMaxTraitors);
|
||||
@@ -61,46 +55,45 @@ public sealed class TraitorRuleSystem : GameRuleSystem<TraitorRuleComponent>
|
||||
SubscribeLocalEvent<TraitorRuleComponent, ObjectivesTextPrependEvent>(OnObjectivesTextPrepend);
|
||||
}
|
||||
|
||||
//Set min players on game rule
|
||||
protected override void Added(EntityUid uid, TraitorRuleComponent component, GameRuleComponent gameRule, GameRuleAddedEvent args)
|
||||
{
|
||||
base.Added(uid, component, gameRule, args);
|
||||
|
||||
gameRule.MinPlayers = _cfg.GetCVar(CCVars.TraitorMinPlayers);
|
||||
}
|
||||
|
||||
protected override void Started(EntityUid uid, TraitorRuleComponent component, GameRuleComponent gameRule, GameRuleStartedEvent args)
|
||||
{
|
||||
base.Started(uid, component, gameRule, args);
|
||||
MakeCodewords(component);
|
||||
}
|
||||
|
||||
protected override void ActiveTick(EntityUid uid, TraitorRuleComponent component, GameRuleComponent gameRule, float frameTime)
|
||||
{
|
||||
base.ActiveTick(uid, component, gameRule, frameTime);
|
||||
|
||||
if (component.SelectionStatus == TraitorRuleComponent.SelectionState.ReadyToSelect && _gameTiming.CurTime > component.AnnounceAt)
|
||||
if (component.SelectionStatus < TraitorRuleComponent.SelectionState.Started && component.AnnounceAt < _timing.CurTime)
|
||||
{
|
||||
DoTraitorStart(component);
|
||||
component.SelectionStatus = TraitorRuleComponent.SelectionState.Started;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check for enough players
|
||||
/// </summary>
|
||||
/// <param name="ev"></param>
|
||||
private void OnStartAttempt(RoundStartAttemptEvent ev)
|
||||
{
|
||||
var query = EntityQueryEnumerator<TraitorRuleComponent, GameRuleComponent>();
|
||||
while (query.MoveNext(out var uid, out var traitor, out var gameRule))
|
||||
{
|
||||
if (!GameTicker.IsGameRuleAdded(uid, gameRule))
|
||||
continue;
|
||||
|
||||
MakeCodewords(traitor);
|
||||
|
||||
var minPlayers = _cfg.GetCVar(CCVars.TraitorMinPlayers);
|
||||
if (!ev.Forced && ev.Players.Length < minPlayers)
|
||||
{
|
||||
_chatManager.SendAdminAnnouncement(Loc.GetString("traitor-not-enough-ready-players",
|
||||
("readyPlayersCount", ev.Players.Length), ("minimumPlayers", minPlayers)));
|
||||
ev.Cancel();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ev.Players.Length == 0)
|
||||
{
|
||||
_chatManager.DispatchServerAnnouncement(Loc.GetString("traitor-no-one-ready"));
|
||||
ev.Cancel();
|
||||
}
|
||||
}
|
||||
TryRoundStartAttempt(ev, Loc.GetString("traitor-title"));
|
||||
}
|
||||
|
||||
private void MakeCodewords(TraitorRuleComponent component)
|
||||
{
|
||||
var codewordCount = _cfg.GetCVar(CCVars.TraitorCodewordCount);
|
||||
var adjectives = _prototypeManager.Index<DatasetPrototype>("adjectives").Values;
|
||||
var verbs = _prototypeManager.Index<DatasetPrototype>("verbs").Values;
|
||||
var adjectives = _prototypeManager.Index<DatasetPrototype>(component.CodewordAdjectives).Values;
|
||||
var verbs = _prototypeManager.Index<DatasetPrototype>(component.CodewordVerbs).Values;
|
||||
var codewordPool = adjectives.Concat(verbs).ToList();
|
||||
var finalCodewordCount = Math.Min(codewordCount, codewordPool.Count);
|
||||
component.Codewords = new string[finalCodewordCount];
|
||||
@@ -112,125 +105,99 @@ public sealed class TraitorRuleSystem : GameRuleSystem<TraitorRuleComponent>
|
||||
|
||||
private void DoTraitorStart(TraitorRuleComponent component)
|
||||
{
|
||||
if (!component.StartCandidates.Any())
|
||||
{
|
||||
Log.Error("Tried to start Traitor mode without any candidates.");
|
||||
var eligiblePlayers = _antagSelection.GetEligiblePlayers(_playerManager.Sessions, component.TraitorPrototypeId);
|
||||
|
||||
if (eligiblePlayers.Count == 0)
|
||||
return;
|
||||
}
|
||||
|
||||
var numTraitors = MathHelper.Clamp(component.StartCandidates.Count / PlayersPerTraitor, 1, MaxTraitors);
|
||||
var traitorPool = _antagSelection.FindPotentialAntags(component.StartCandidates, component.TraitorPrototypeId);
|
||||
var selectedTraitors = _antagSelection.PickAntag(numTraitors, traitorPool);
|
||||
var traitorsToSelect = _antagSelection.CalculateAntagCount(_playerManager.PlayerCount, PlayersPerTraitor, MaxTraitors);
|
||||
|
||||
foreach (var traitor in selectedTraitors)
|
||||
{
|
||||
MakeTraitor(traitor);
|
||||
}
|
||||
var selectedTraitors = _antagSelection.ChooseAntags(traitorsToSelect, eligiblePlayers);
|
||||
|
||||
component.SelectionStatus = TraitorRuleComponent.SelectionState.SelectionMade;
|
||||
MakeTraitor(selectedTraitors, component);
|
||||
}
|
||||
|
||||
private void OnPlayersSpawned(RulePlayerJobsAssignedEvent ev)
|
||||
{
|
||||
var query = EntityQueryEnumerator<TraitorRuleComponent, GameRuleComponent>();
|
||||
while (query.MoveNext(out var uid, out var traitor, out var gameRule))
|
||||
//Start the timer
|
||||
var query = QueryActiveRules();
|
||||
while (query.MoveNext(out _, out var comp, out var gameRuleComponent))
|
||||
{
|
||||
if (!GameTicker.IsGameRuleAdded(uid, gameRule))
|
||||
continue;
|
||||
foreach (var player in ev.Players)
|
||||
{
|
||||
if (!ev.Profiles.ContainsKey(player.UserId))
|
||||
continue;
|
||||
|
||||
traitor.StartCandidates[player] = ev.Profiles[player.UserId];
|
||||
}
|
||||
|
||||
var delay = TimeSpan.FromSeconds(
|
||||
_cfg.GetCVar(CCVars.TraitorStartDelay) +
|
||||
_random.NextFloat(0f, _cfg.GetCVar(CCVars.TraitorStartDelayVariance)));
|
||||
|
||||
traitor.AnnounceAt = _gameTiming.CurTime + delay;
|
||||
//Set the delay for choosing traitors
|
||||
comp.AnnounceAt = _timing.CurTime + delay;
|
||||
|
||||
traitor.SelectionStatus = TraitorRuleComponent.SelectionState.ReadyToSelect;
|
||||
comp.SelectionStatus = TraitorRuleComponent.SelectionState.ReadyToStart;
|
||||
}
|
||||
}
|
||||
|
||||
public bool MakeTraitor(ICommonSession traitor, bool giveUplink = true, bool giveObjectives = true)
|
||||
public bool MakeTraitor(List<EntityUid> traitors, TraitorRuleComponent component, bool giveUplink = true, bool giveObjectives = true)
|
||||
{
|
||||
var traitorRule = EntityQuery<TraitorRuleComponent>().FirstOrDefault();
|
||||
if (traitorRule == null)
|
||||
foreach (var traitor in traitors)
|
||||
{
|
||||
//todo fuck me this shit is awful
|
||||
//no i wont fuck you, erp is against rules
|
||||
GameTicker.StartGameRule("Traitor", out var ruleEntity);
|
||||
traitorRule = Comp<TraitorRuleComponent>(ruleEntity);
|
||||
MakeCodewords(traitorRule);
|
||||
MakeTraitor(traitor, component, giveUplink, giveObjectives);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool MakeTraitor(EntityUid traitor, TraitorRuleComponent component, bool giveUplink = true, bool giveObjectives = true)
|
||||
{
|
||||
//Grab the mind if it wasnt provided
|
||||
if (!_mindSystem.TryGetMind(traitor, out var mindId, out var mind))
|
||||
{
|
||||
Log.Info("Failed getting mind for picked traitor.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (HasComp<TraitorRoleComponent>(mindId))
|
||||
{
|
||||
Log.Error($"Player {traitor.Name} is already a traitor.");
|
||||
Log.Error($"Player {mind.CharacterName} is already a traitor.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mind.OwnedEntity is not { } entity)
|
||||
{
|
||||
Log.Error("Mind picked for traitor did not have an attached entity.");
|
||||
return false;
|
||||
}
|
||||
var briefing = Loc.GetString("traitor-role-codewords-short", ("codewords", string.Join(", ", component.Codewords)));
|
||||
|
||||
// Calculate the amount of currency on the uplink.
|
||||
var startingBalance = _cfg.GetCVar(CCVars.TraitorStartingBalance);
|
||||
if (_jobs.MindTryGetJob(mindId, out _, out var prototype))
|
||||
startingBalance = Math.Max(startingBalance - prototype.AntagAdvantage, 0);
|
||||
|
||||
// Give traitors their codewords and uplink code to keep in their character info menu
|
||||
var briefing = Loc.GetString("traitor-role-codewords-short", ("codewords", string.Join(", ", traitorRule.Codewords)));
|
||||
Note[]? code = null;
|
||||
if (giveUplink)
|
||||
{
|
||||
// Calculate the amount of currency on the uplink.
|
||||
var startingBalance = _cfg.GetCVar(CCVars.TraitorStartingBalance);
|
||||
if (_jobs.MindTryGetJob(mindId, out _, out var prototype))
|
||||
startingBalance = Math.Max(startingBalance - prototype.AntagAdvantage, 0);
|
||||
|
||||
// creadth: we need to create uplink for the antag.
|
||||
// PDA should be in place already
|
||||
var pda = _uplink.FindUplinkTarget(mind.OwnedEntity!.Value);
|
||||
if (pda == null || !_uplink.AddUplink(mind.OwnedEntity.Value, startingBalance))
|
||||
var pda = _uplink.FindUplinkTarget(traitor);
|
||||
if (pda == null || !_uplink.AddUplink(traitor, startingBalance))
|
||||
return false;
|
||||
|
||||
// Give traitors their codewords and uplink code to keep in their character info menu
|
||||
code = EnsureComp<RingerUplinkComponent>(pda.Value).Code;
|
||||
|
||||
// If giveUplink is false the uplink code part is omitted
|
||||
briefing = string.Format("{0}\n{1}", briefing,
|
||||
Loc.GetString("traitor-role-uplink-code-short", ("code", string.Join("-", code).Replace("sharp","#"))));
|
||||
Loc.GetString("traitor-role-uplink-code-short", ("code", string.Join("-", code).Replace("sharp", "#"))));
|
||||
}
|
||||
|
||||
// Prepare traitor role
|
||||
var traitorRole = new TraitorRoleComponent
|
||||
{
|
||||
PrototypeId = traitorRule.TraitorPrototypeId,
|
||||
};
|
||||
_antagSelection.SendBriefing(traitor, GenerateBriefing(component.Codewords, code), null, component.GreetSoundNotification);
|
||||
|
||||
component.TraitorMinds.Add(mindId);
|
||||
|
||||
// Assign traitor roles
|
||||
_roleSystem.MindAddRole(mindId, new TraitorRoleComponent
|
||||
{
|
||||
PrototypeId = traitorRule.TraitorPrototypeId
|
||||
}, mind);
|
||||
// Assign briefing and greeting sound
|
||||
PrototypeId = component.TraitorPrototypeId
|
||||
}, mind, true);
|
||||
// Assign briefing
|
||||
_roleSystem.MindAddRole(mindId, new RoleBriefingComponent
|
||||
{
|
||||
Briefing = briefing
|
||||
}, mind);
|
||||
_roleSystem.MindPlaySound(mindId, traitorRule.GreetSoundNotification, mind);
|
||||
SendTraitorBriefing(mindId, traitorRule.Codewords, code);
|
||||
traitorRule.TraitorMinds.Add(mindId);
|
||||
Briefing = briefing.ToString()
|
||||
}, mind, true);
|
||||
|
||||
// Change the faction
|
||||
_npcFaction.RemoveFaction(entity, "NanoTrasen", false);
|
||||
_npcFaction.AddFaction(entity, "Syndicate");
|
||||
_npcFaction.RemoveFaction(traitor, component.NanoTrasenFaction, false);
|
||||
_npcFaction.AddFaction(traitor, component.SyndicateFaction);
|
||||
|
||||
// Give traitors their objectives
|
||||
if (giveObjectives)
|
||||
@@ -241,7 +208,7 @@ public sealed class TraitorRuleSystem : GameRuleSystem<TraitorRuleComponent>
|
||||
Log.Debug($"Attempting {maxPicks} objective picks with {maxDifficulty} difficulty");
|
||||
for (var pick = 0; pick < maxPicks && maxDifficulty > difficulty; pick++)
|
||||
{
|
||||
var objective = _objectives.GetRandomObjective(mindId, mind, "TraitorObjectiveGroups");
|
||||
var objective = _objectives.GetRandomObjective(mindId, mind, component.ObjectiveGroup);
|
||||
if (objective == null)
|
||||
continue;
|
||||
|
||||
@@ -255,54 +222,26 @@ public sealed class TraitorRuleSystem : GameRuleSystem<TraitorRuleComponent>
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send a codewords and uplink codes to traitor chat.
|
||||
/// </summary>
|
||||
/// <param name="mind">A mind (player)</param>
|
||||
/// <param name="codewords">Codewords</param>
|
||||
/// <param name="code">Uplink codes</param>
|
||||
private void SendTraitorBriefing(EntityUid mind, string[] codewords, Note[]? code)
|
||||
{
|
||||
if (!_mindSystem.TryGetSession(mind, out var session))
|
||||
return;
|
||||
|
||||
_chatManager.DispatchServerMessage(session, Loc.GetString("traitor-role-greeting"));
|
||||
_chatManager.DispatchServerMessage(session, Loc.GetString("traitor-role-codewords", ("codewords", string.Join(", ", codewords))));
|
||||
if (code != null)
|
||||
_chatManager.DispatchServerMessage(session, Loc.GetString("traitor-role-uplink-code", ("code", string.Join("-", code).Replace("sharp","#"))));
|
||||
}
|
||||
|
||||
private void HandleLatejoin(PlayerSpawnCompleteEvent ev)
|
||||
{
|
||||
var query = EntityQueryEnumerator<TraitorRuleComponent, GameRuleComponent>();
|
||||
while (query.MoveNext(out var uid, out var traitor, out var gameRule))
|
||||
var query = QueryActiveRules();
|
||||
while (query.MoveNext(out _, out var comp, out _))
|
||||
{
|
||||
if (!GameTicker.IsGameRuleAdded(uid, gameRule))
|
||||
if (comp.TotalTraitors >= MaxTraitors)
|
||||
continue;
|
||||
|
||||
if (traitor.TotalTraitors >= MaxTraitors)
|
||||
continue;
|
||||
if (!ev.LateJoin)
|
||||
continue;
|
||||
if (!ev.Profile.AntagPreferences.Contains(traitor.TraitorPrototypeId))
|
||||
|
||||
if (!_antagSelection.IsPlayerEligible(ev.Player, comp.TraitorPrototypeId))
|
||||
continue;
|
||||
|
||||
if (ev.JobId == null || !_prototypeManager.TryIndex<JobPrototype>(ev.JobId, out var job))
|
||||
//If its before we have selected traitors, continue
|
||||
if (comp.SelectionStatus < TraitorRuleComponent.SelectionState.Started)
|
||||
continue;
|
||||
|
||||
if (!job.CanBeAntag)
|
||||
continue;
|
||||
|
||||
// Before the announcement is made, late-joiners are considered the same as players who readied.
|
||||
if (traitor.SelectionStatus < TraitorRuleComponent.SelectionState.SelectionMade)
|
||||
{
|
||||
traitor.StartCandidates[ev.Player] = ev.Profile;
|
||||
continue;
|
||||
}
|
||||
|
||||
// the nth player we adjust our probabilities around
|
||||
var target = PlayersPerTraitor * traitor.TotalTraitors + 1;
|
||||
|
||||
var target = PlayersPerTraitor * comp.TotalTraitors + 1;
|
||||
var chance = 1f / PlayersPerTraitor;
|
||||
|
||||
// If we have too many traitors, divide by how many players below target for next traitor we are.
|
||||
@@ -322,7 +261,7 @@ public sealed class TraitorRuleSystem : GameRuleSystem<TraitorRuleComponent>
|
||||
// You get one shot.
|
||||
if (_random.Prob(chance))
|
||||
{
|
||||
MakeTraitor(ev.Player);
|
||||
MakeTraitor(ev.Mob, comp);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -338,6 +277,38 @@ public sealed class TraitorRuleSystem : GameRuleSystem<TraitorRuleComponent>
|
||||
args.Text += "\n" + Loc.GetString("traitor-round-end-codewords", ("codewords", string.Join(", ", comp.Codewords)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start this game rule manually
|
||||
/// </summary>
|
||||
public TraitorRuleComponent StartGameRule()
|
||||
{
|
||||
var comp = EntityQuery<TraitorRuleComponent>().FirstOrDefault();
|
||||
if (comp == null)
|
||||
{
|
||||
GameTicker.StartGameRule("Traitor", out var ruleEntity);
|
||||
comp = Comp<TraitorRuleComponent>(ruleEntity);
|
||||
}
|
||||
|
||||
return comp;
|
||||
}
|
||||
|
||||
public void MakeTraitorAdmin(EntityUid entity, bool giveUplink, bool giveObjectives)
|
||||
{
|
||||
var traitorRule = StartGameRule();
|
||||
MakeTraitor(entity, traitorRule, giveUplink, giveObjectives);
|
||||
}
|
||||
|
||||
private string GenerateBriefing(string[] codewords, Note[]? uplinkCode)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
sb.AppendLine(Loc.GetString("traitor-role-greeting"));
|
||||
sb.AppendLine(Loc.GetString("traitor-role-codewords-short", ("codewords", string.Join(", ", codewords))));
|
||||
if (uplinkCode != null)
|
||||
sb.AppendLine(Loc.GetString("traitor-role-uplink-code-short", ("code", string.Join("-", uplinkCode).Replace("sharp", "#"))));
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public List<(EntityUid Id, MindComponent Mind)> GetOtherTraitorMindsAliveAndConnected(MindComponent ourMind)
|
||||
{
|
||||
List<(EntityUid Id, MindComponent Mind)> allTraitors = new();
|
||||
|
||||
Reference in New Issue
Block a user