Revert "Gamerule Entities" (#15724)

This commit is contained in:
metalgearsloth
2023-04-24 16:21:05 +10:00
committed by GitHub
parent 39cc02b8f9
commit d3552dae00
124 changed files with 4328 additions and 3083 deletions

View File

@@ -1,6 +1,5 @@
using System.Linq;
using Content.Server.Chat.Managers;
using Content.Server.GameTicking.Rules.Components;
using Content.Server.NPC.Systems;
using Content.Server.Objectives.Interfaces;
using Content.Server.PDA.Ringer;
@@ -25,7 +24,7 @@ using Robust.Shared.Utility;
namespace Content.Server.GameTicking.Rules;
public sealed class TraitorRuleSystem : GameRuleSystem<TraitorRuleComponent>
public sealed class TraitorRuleSystem : GameRuleSystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
@@ -33,6 +32,7 @@ public sealed class TraitorRuleSystem : GameRuleSystem<TraitorRuleComponent>
[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!;
@@ -40,8 +40,30 @@ public sealed class TraitorRuleSystem : GameRuleSystem<TraitorRuleComponent>
private ISawmill _sawmill = default!;
private int PlayersPerTraitor => _cfg.GetCVar(CCVars.TraitorPlayersPerTraitor);
private int MaxTraitors => _cfg.GetCVar(CCVars.TraitorMaxTraitors);
public override string Prototype => "Traitor";
private readonly SoundSpecifier _addedSound = new SoundPathSpecifier("/Audio/Misc/tatoralert.ogg");
public List<TraitorRole> Traitors = new();
private const string TraitorPrototypeID = "Traitor";
private const string TraitorUplinkPresetId = "StorePresetUplink";
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 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()
{
@@ -55,101 +77,101 @@ public sealed class TraitorRuleSystem : GameRuleSystem<TraitorRuleComponent>
SubscribeLocalEvent<RoundEndTextAppendEvent>(OnRoundEndText);
}
protected override void ActiveTick(EntityUid uid, TraitorRuleComponent component, GameRuleComponent gameRule, float frameTime)
public override void Update(float frameTime)
{
base.ActiveTick(uid, component, gameRule, frameTime);
base.Update(frameTime);
if (component.SelectionStatus == TraitorRuleComponent.SelectionState.ReadyToSelect && _gameTiming.CurTime > component.AnnounceAt)
DoTraitorStart(component);
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)
{
var query = EntityQueryEnumerator<TraitorRuleComponent, GameRuleComponent>();
while (query.MoveNext(out var uid, out var traitor, out var gameRule))
MakeCodewords();
if (!RuleAdded)
return;
var minPlayers = _cfg.GetCVar(CCVars.TraitorMinPlayers);
if (!ev.Forced && ev.Players.Length < minPlayers)
{
if (!GameTicker.IsGameRuleAdded(uid, gameRule))
continue;
_chatManager.DispatchServerAnnouncement(Loc.GetString("traitor-not-enough-ready-players", ("readyPlayersCount", ev.Players.Length), ("minimumPlayers", minPlayers)));
ev.Cancel();
return;
}
MakeCodewords(traitor);
var minPlayers = _cfg.GetCVar(CCVars.TraitorMinPlayers);
if (!ev.Forced && ev.Players.Length < minPlayers)
{
_chatManager.DispatchServerAnnouncement(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();
}
if (ev.Players.Length == 0)
{
_chatManager.DispatchServerAnnouncement(Loc.GetString("traitor-no-one-ready"));
ev.Cancel();
}
}
private void MakeCodewords(TraitorRuleComponent component)
private void MakeCodewords()
{
var codewordCount = _cfg.GetCVar(CCVars.TraitorCodewordCount);
var adjectives = _prototypeManager.Index<DatasetPrototype>("adjectives").Values;
var verbs = _prototypeManager.Index<DatasetPrototype>("verbs").Values;
var codewordPool = adjectives.Concat(verbs).ToList();
var finalCodewordCount = Math.Min(codewordCount, codewordPool.Count);
component.Codewords = new string[finalCodewordCount];
Codewords = new string[finalCodewordCount];
for (var i = 0; i < finalCodewordCount; i++)
{
component.Codewords[i] = _random.PickAndTake(codewordPool);
Codewords[i] = _random.PickAndTake(codewordPool);
}
}
private void DoTraitorStart(TraitorRuleComponent component)
private void DoTraitorStart()
{
if (!component.StartCandidates.Any())
if (!_startCandidates.Any())
{
_sawmill.Error("Tried to start Traitor mode without any candidates.");
return;
}
var numTraitors = MathHelper.Clamp(component.StartCandidates.Count / PlayersPerTraitor, 1, MaxTraitors);
var traitorPool = FindPotentialTraitors(component.StartCandidates, component);
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);
}
component.SelectionStatus = TraitorRuleComponent.SelectionState.SelectionMade;
SelectionStatus = SelectionState.SelectionMade;
}
private void OnPlayersSpawned(RulePlayerJobsAssignedEvent ev)
{
var query = EntityQueryEnumerator<TraitorRuleComponent, GameRuleComponent>();
while (query.MoveNext(out var uid, out var traitor, out var gameRule))
if (!RuleAdded)
return;
foreach (var player in ev.Players)
{
if (!GameTicker.IsGameRuleAdded(uid, gameRule))
if (!ev.Profiles.ContainsKey(player.UserId))
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;
traitor.SelectionStatus = TraitorRuleComponent.SelectionState.ReadyToSelect;
_startCandidates[player] = ev.Profiles[player.UserId];
}
var delay = TimeSpan.FromSeconds(
_cfg.GetCVar(CCVars.TraitorStartDelay) +
_random.NextFloat(0f, _cfg.GetCVar(CCVars.TraitorStartDelayVariance)));
_announceAt = _gameTiming.CurTime + delay;
SelectionStatus = SelectionState.ReadyToSelect;
}
public List<IPlayerSession> FindPotentialTraitors(in Dictionary<IPlayerSession, HumanoidCharacterProfile> candidates, TraitorRuleComponent component)
public List<IPlayerSession> FindPotentialTraitors(in Dictionary<IPlayerSession, HumanoidCharacterProfile> candidates)
{
var list = new List<IPlayerSession>();
var pendingQuery = GetEntityQuery<PendingClockInComponent>();
@@ -174,7 +196,7 @@ public sealed class TraitorRuleSystem : GameRuleSystem<TraitorRuleComponent>
foreach (var player in list)
{
var profile = candidates[player];
if (profile.AntagPreferences.Contains(component.TraitorPrototypeId))
if (profile.AntagPreferences.Contains(TraitorPrototypeID))
{
prefList.Add(player);
}
@@ -206,14 +228,6 @@ public sealed class TraitorRuleSystem : GameRuleSystem<TraitorRuleComponent>
public bool MakeTraitor(IPlayerSession traitor)
{
var traitorRule = EntityQuery<TraitorRuleComponent>().FirstOrDefault();
if (traitorRule == null)
{
//todo fuck me this shit is awful
GameTicker.StartGameRule("traitor", out var ruleEntity);
traitorRule = EntityManager.GetComponent<TraitorRuleComponent>(ruleEntity);
}
var mind = traitor.Data.ContentData()?.Mind;
if (mind == null)
{
@@ -240,15 +254,14 @@ public sealed class TraitorRuleSystem : GameRuleSystem<TraitorRuleComponent>
if (pda == null || !_uplink.AddUplink(mind.OwnedEntity.Value, startingBalance))
return false;
// add the ringtone uplink and get its code for greeting
var code = AddComp<RingerUplinkComponent>(pda.Value).Code;
var antagPrototype = _prototypeManager.Index<AntagPrototype>(traitorRule.TraitorPrototypeId);
var antagPrototype = _prototypeManager.Index<AntagPrototype>(TraitorPrototypeID);
var traitorRole = new TraitorRole(mind, antagPrototype);
mind.AddRole(traitorRole);
traitorRule.Traitors.Add(traitorRole);
traitorRole.GreetTraitor(traitorRule.Codewords, code);
Traitors.Add(traitorRole);
traitorRole.GreetTraitor(Codewords, code);
_faction.RemoveFaction(entity, "NanoTrasen", false);
_faction.AddFaction(entity, "Syndicate");
@@ -267,173 +280,147 @@ public sealed class TraitorRuleSystem : GameRuleSystem<TraitorRuleComponent>
}
//give traitors their codewords and uplink code to keep in their character info menu
traitorRole.Mind.Briefing = Loc.GetString("traitor-role-codewords-short", ("codewords", string.Join(", ", traitorRule.Codewords)))
traitorRole.Mind.Briefing = Loc.GetString("traitor-role-codewords-short", ("codewords", string.Join(", ", Codewords)))
+ "\n" + Loc.GetString("traitor-role-uplink-code-short", ("code", string.Join("", code)));
_audioSystem.PlayGlobal(traitorRule.AddedSound, Filter.Empty().AddPlayer(traitor), false, AudioParams.Default);
_audioSystem.PlayGlobal(_addedSound, Filter.Empty().AddPlayer(traitor), false, AudioParams.Default);
return true;
}
private void HandleLatejoin(PlayerSpawnCompleteEvent ev)
{
var query = EntityQueryEnumerator<TraitorRuleComponent, GameRuleComponent>();
while (query.MoveNext(out var uid, out var traitor, out var gameRule))
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;
// Before the announcement is made, late-joiners are considered the same as players who readied.
if (SelectionStatus < SelectionState.SelectionMade)
{
if (!GameTicker.IsGameRuleAdded(uid, gameRule))
continue;
_startCandidates[ev.Player] = ev.Profile;
return;
}
if (traitor.TotalTraitors >= MaxTraitors)
continue;
if (!ev.LateJoin)
continue;
if (!ev.Profile.AntagPreferences.Contains(traitor.TraitorPrototypeId))
continue;
// the nth player we adjust our probabilities around
int target = ((_playersPerTraitor * TotalTraitors) + 1);
if (ev.JobId == null || !_prototypeManager.TryIndex<JobPrototype>(ev.JobId, out var job))
continue;
float chance = (1f / _playersPerTraitor);
if (!job.CanBeAntag)
continue;
// 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;
// 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 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(chance))
{
MakeTraitor(ev.Player);
}
// Now that we've calculated our chance, roll and make them a traitor if we roll under.
// You get one shot.
if (_random.Prob(chance))
{
MakeTraitor(ev.Player);
}
}
private void OnRoundEndText(RoundEndTextAppendEvent ev)
{
var query = EntityQueryEnumerator<TraitorRuleComponent, GameRuleComponent>();
while (query.MoveNext(out var uid, out var traitor, out var gameRule))
if (!RuleAdded)
return;
var result = Loc.GetString("traitor-round-end-result", ("traitorCount", Traitors.Count));
result += "\n" + Loc.GetString("traitor-round-end-codewords", ("codewords", string.Join(", ", Codewords))) + "\n";
foreach (var traitor in Traitors)
{
if (!GameTicker.IsGameRuleAdded(uid, gameRule))
continue;
var name = traitor.Mind.CharacterName;
traitor.Mind.TryGetSession(out var session);
var username = session?.Name;
var result = Loc.GetString("traitor-round-end-result", ("traitorCount", traitor.Traitors.Count));
result += "\n" + Loc.GetString("traitor-round-end-codewords", ("codewords", string.Join(", ", traitor.Codewords))) +
"\n";
foreach (var t in traitor.Traitors)
var objectives = traitor.Mind.AllObjectives.ToArray();
if (objectives.Length == 0)
{
var name = t.Mind.CharacterName;
t.Mind.TryGetSession(out var session);
var username = session?.Name;
var objectives = t.Mind.AllObjectives.ToArray();
if (objectives.Length == 0)
{
if (username != null)
{
if (name == null)
result += "\n" + Loc.GetString("traitor-user-was-a-traitor", ("user", username));
else
result += "\n" + Loc.GetString("traitor-user-was-a-traitor-named", ("user", username),
("name", name));
}
else if (name != null)
result += "\n" + Loc.GetString("traitor-was-a-traitor-named", ("name", name));
continue;
}
if (username != null)
{
if (name == null)
result += "\n" + Loc.GetString("traitor-user-was-a-traitor-with-objectives",
("user", username));
result += "\n" + Loc.GetString("traitor-user-was-a-traitor", ("user", username));
else
result += "\n" + Loc.GetString("traitor-user-was-a-traitor-with-objectives-named",
("user", username), ("name", name));
result += "\n" + Loc.GetString("traitor-user-was-a-traitor-named", ("user", username), ("name", name));
}
else if (name != null)
result += "\n" + Loc.GetString("traitor-was-a-traitor-with-objectives-named", ("name", name));
result += "\n" + Loc.GetString("traitor-was-a-traitor-named", ("name", name));
foreach (var objectiveGroup in objectives.GroupBy(o => o.Prototype.Issuer))
continue;
}
if (username != null)
{
if (name == null)
result += "\n" + Loc.GetString("traitor-user-was-a-traitor-with-objectives", ("user", username));
else
result += "\n" + Loc.GetString("traitor-user-was-a-traitor-with-objectives-named", ("user", username), ("name", name));
}
else if (name != null)
result += "\n" + Loc.GetString("traitor-was-a-traitor-with-objectives-named", ("name", name));
foreach (var objectiveGroup in objectives.GroupBy(o => o.Prototype.Issuer))
{
result += "\n" + Loc.GetString($"preset-traitor-objective-issuer-{objectiveGroup.Key}");
foreach (var objective in objectiveGroup)
{
result += "\n" + Loc.GetString($"preset-traitor-objective-issuer-{objectiveGroup.Key}");
foreach (var objective in objectiveGroup)
foreach (var condition in objective.Conditions)
{
foreach (var condition in objective.Conditions)
var progress = condition.Progress;
if (progress > 0.99f)
{
var progress = condition.Progress;
if (progress > 0.99f)
{
result += "\n- " + Loc.GetString(
"traitor-objective-condition-success",
("condition", condition.Title),
("markupColor", "green")
);
}
else
{
result += "\n- " + Loc.GetString(
"traitor-objective-condition-fail",
("condition", condition.Title),
("progress", (int) (progress * 100)),
("markupColor", "red")
);
}
result += "\n- " + Loc.GetString(
"traitor-objective-condition-success",
("condition", condition.Title),
("markupColor", "green")
);
}
else
{
result += "\n- " + Loc.GetString(
"traitor-objective-condition-fail",
("condition", condition.Title),
("progress", (int) (progress * 100)),
("markupColor", "red")
);
}
}
}
}
ev.AddLine(result);
}
ev.AddLine(result);
}
public List<TraitorRole> GetOtherTraitorsAliveAndConnected(Mind.Mind ourMind)
public IEnumerable<TraitorRole> GetOtherTraitorsAliveAndConnected(Mind.Mind ourMind)
{
List<TraitorRole> allTraitors = new();
foreach (var traitor in EntityQuery<TraitorRuleComponent>())
{
foreach (var role in GetOtherTraitorsAliveAndConnected(ourMind, traitor))
{
if (!allTraitors.Contains(role))
allTraitors.Add(role);
}
}
var traitors = Traitors;
List<TraitorRole> removeList = new();
return allTraitors;
}
public List<TraitorRole> GetOtherTraitorsAliveAndConnected(Mind.Mind ourMind, TraitorRuleComponent component)
{
return component.Traitors // don't want
return Traitors // don't want
.Where(t => t.Mind is not null) // no mind
.Where(t => t.Mind.OwnedEntity is not null) // no entity
.Where(t => t.Mind.Session is not null) // player disconnected
.Where(t => t.Mind != ourMind) // ourselves
.Where(t => _mobStateSystem.IsAlive((EntityUid) t.Mind.OwnedEntity!)) // dead
.Where(t => t.Mind.CurrentEntity == t.Mind.OwnedEntity).ToList(); // not in original body
.Where(t => t.Mind.CurrentEntity == t.Mind.OwnedEntity); // not in original body
}
}