using Content.Server.Administration.Logs; using Content.Server.Antag; using Content.Server.EUI; using Content.Server.Flash; using Content.Server.GameTicking.Rules.Components; using Content.Server.Mind; using Content.Server.Popups; using Content.Server.Revolutionary; using Content.Server.Revolutionary.Components; using Content.Server.Roles; using Content.Server.RoundEnd; using Content.Server.Shuttles.Systems; using Content.Server.Station.Systems; using Content.Shared.Database; using Content.Shared.Humanoid; using Content.Shared.IdentityManagement; using Content.Shared.Inventory; using Content.Shared.Mind; using Content.Shared.Mind.Components; using Content.Shared.Mindshield.Components; using Content.Shared.Mobs; using Content.Shared.Mobs.Components; using Content.Shared.Mobs.Systems; using Content.Shared.NPC.Prototypes; using Content.Shared.NPC.Systems; using Content.Shared.Revolutionary.Components; using Content.Shared.Roles; using Content.Shared.Stunnable; using Content.Shared.Zombies; using Robust.Shared.Prototypes; using Robust.Shared.Timing; using System.Linq; namespace Content.Server.GameTicking.Rules; /// /// Where all the main stuff for Revolutionaries happens (Assigning Head Revs, Command on station, and checking for the game to end.) /// public sealed class RevolutionaryRuleSystem : GameRuleSystem { [Dependency] private readonly IAdminLogManager _adminLogManager = default!; [Dependency] private readonly IGameTiming _timing = default!; [Dependency] private readonly AntagSelectionSystem _antagSelection = default!; [Dependency] private readonly EuiManager _euiMan = default!; [Dependency] private readonly MindSystem _mind = default!; [Dependency] private readonly MobStateSystem _mobState = default!; [Dependency] private readonly NpcFactionSystem _npcFaction = default!; [Dependency] private readonly PopupSystem _popup = default!; [Dependency] private readonly RoleSystem _role = default!; [Dependency] private readonly SharedStunSystem _stun = default!; [Dependency] private readonly RoundEndSystem _roundEnd = default!; [Dependency] private readonly StationSystem _stationSystem = default!; [Dependency] private readonly EmergencyShuttleSystem _emergencyShuttle = default!; [Dependency] private readonly InventorySystem _inventory = default!; //Used in OnPostFlash, no reference to the rule component is available public readonly ProtoId RevolutionaryNpcFaction = "Revolutionary"; public readonly ProtoId RevPrototypeId = "Rev"; public override void Initialize() { base.Initialize(); SubscribeLocalEvent(OnStartAttempt); SubscribeLocalEvent(OnPlayerJobAssigned); SubscribeLocalEvent(OnCommandMobStateChanged); SubscribeLocalEvent(OnHeadRevMobStateChanged); SubscribeLocalEvent(OnRoundEndText); SubscribeLocalEvent(OnGetBriefing); SubscribeLocalEvent(OnPostFlash); } //Set miniumum players protected override void Added(EntityUid uid, RevolutionaryRuleComponent component, GameRuleComponent gameRule, GameRuleAddedEvent args) { base.Added(uid, component, gameRule, args); gameRule.MinPlayers = component.MinPlayers; } protected override void Started(EntityUid uid, RevolutionaryRuleComponent component, GameRuleComponent gameRule, GameRuleStartedEvent args) { base.Started(uid, component, gameRule, args); component.CommandCheck = _timing.CurTime + component.TimerWait; } protected override void ActiveTick(EntityUid uid, RevolutionaryRuleComponent component, GameRuleComponent gameRule, float frameTime) { base.ActiveTick(uid, component, gameRule, frameTime); if (component.CommandCheck <= _timing.CurTime) { component.CommandCheck = _timing.CurTime + component.TimerWait; if (CheckCommandLose()) { _roundEnd.DoRoundEndBehavior(RoundEndBehavior.ShuttleCall, component.ShuttleCallTime); GameTicker.EndGameRule(uid, gameRule); } } } private void OnRoundEndText(RoundEndTextAppendEvent ev) { var revsLost = CheckRevsLose(); var commandLost = CheckCommandLose(); var query = AllEntityQuery(); while (query.MoveNext(out var headrev)) { // This is (revsLost, commandsLost) concatted together // (moony wrote this comment idk what it means) var index = (commandLost ? 1 : 0) | (revsLost ? 2 : 0); ev.AddLine(Loc.GetString(Outcomes[index])); ev.AddLine(Loc.GetString("rev-headrev-count", ("initialCount", headrev.HeadRevs.Count))); foreach (var player in headrev.HeadRevs) { // TODO: when role entities are a thing this has to change var count = CompOrNull(player.Value)?.ConvertedCount ?? 0; _mind.TryGetSession(player.Value, out var session); var username = session?.Name; if (username != null) { ev.AddLine(Loc.GetString("rev-headrev-name-user", ("name", player.Key), ("username", username), ("count", count))); } else { ev.AddLine(Loc.GetString("rev-headrev-name", ("name", player.Key), ("count", count))); } // TODO: someone suggested listing all alive? revs maybe implement at some point } } } private void OnGetBriefing(EntityUid uid, RevolutionaryRoleComponent comp, ref GetBriefingEvent args) { if (!TryComp(uid, out var mind) || mind.OwnedEntity == null) return; var head = HasComp(mind.OwnedEntity); args.Append(Loc.GetString(head ? "head-rev-briefing" : "rev-briefing")); } //Check for enough players to start rule private void OnStartAttempt(RoundStartAttemptEvent ev) { TryRoundStartAttempt(ev, Loc.GetString("roles-antag-rev-name")); } private void OnPlayerJobAssigned(RulePlayerJobsAssignedEvent ev) { var query = QueryActiveRules(); while (query.MoveNext(out var uid, out var activeGameRule, out var comp, out var gameRule)) { var eligiblePlayers = _antagSelection.GetEligiblePlayers(ev.Players, comp.HeadRevPrototypeId); if (eligiblePlayers.Count == 0) continue; var headRevCount = _antagSelection.CalculateAntagCount(ev.Players.Length, comp.PlayersPerHeadRev, comp.MaxHeadRevs); var headRevs = _antagSelection.ChooseAntags(headRevCount, eligiblePlayers); GiveHeadRev(headRevs, comp.HeadRevPrototypeId, comp); } } private void GiveHeadRev(IEnumerable chosen, ProtoId antagProto, RevolutionaryRuleComponent comp) { foreach (var headRev in chosen) GiveHeadRev(headRev, antagProto, comp); } private void GiveHeadRev(EntityUid chosen, ProtoId antagProto, RevolutionaryRuleComponent comp) { RemComp(chosen); var inCharacterName = MetaData(chosen).EntityName; if (!_mind.TryGetMind(chosen, out var mind, out _)) return; if (!_role.MindHasRole(mind)) { _role.MindAddRole(mind, new RevolutionaryRoleComponent { PrototypeId = antagProto }, silent: true); } comp.HeadRevs.Add(inCharacterName, mind); _inventory.SpawnItemsOnEntity(chosen, comp.StartingGear); var revComp = EnsureComp(chosen); EnsureComp(chosen); _antagSelection.SendBriefing(chosen, Loc.GetString("head-rev-role-greeting"), Color.CornflowerBlue, revComp.RevStartSound); } /// /// Called when a Head Rev uses a flash in melee to convert somebody else. /// private void OnPostFlash(EntityUid uid, HeadRevolutionaryComponent comp, ref AfterFlashedEvent ev) { var alwaysConvertible = HasComp(ev.Target); if (!_mind.TryGetMind(ev.Target, out var mindId, out var mind) && !alwaysConvertible) return; if (HasComp(ev.Target) || HasComp(ev.Target) || !HasComp(ev.Target) && !alwaysConvertible || !_mobState.IsAlive(ev.Target) || HasComp(ev.Target)) { return; } _npcFaction.AddFaction(ev.Target, RevolutionaryNpcFaction); var revComp = EnsureComp(ev.Target); if (ev.User != null) { _adminLogManager.Add(LogType.Mind, LogImpact.Medium, $"{ToPrettyString(ev.User.Value)} converted {ToPrettyString(ev.Target)} into a Revolutionary"); if (_mind.TryGetRole(ev.User.Value, out var headrev)) headrev.ConvertedCount++; } if (mindId == default || !_role.MindHasRole(mindId)) { _role.MindAddRole(mindId, new RevolutionaryRoleComponent { PrototypeId = RevPrototypeId }); } if (mind?.Session != null) _antagSelection.SendBriefing(mind.Session, Loc.GetString("rev-role-greeting"), Color.Red, revComp.RevStartSound); } public void OnHeadRevAdmin(EntityUid entity) { if (HasComp(entity)) return; var revRule = EntityQuery().FirstOrDefault(); if (revRule == null) { GameTicker.StartGameRule("Revolutionary", out var ruleEnt); revRule = Comp(ruleEnt); } GiveHeadRev(entity, revRule.HeadRevPrototypeId, revRule); } //TODO: Enemies of the revolution private void OnCommandMobStateChanged(EntityUid uid, CommandStaffComponent comp, MobStateChangedEvent ev) { if (ev.NewMobState == MobState.Dead || ev.NewMobState == MobState.Invalid) CheckCommandLose(); } /// /// Checks if all of command is dead and if so will remove all sec and command jobs if there were any left. /// private bool CheckCommandLose() { var commandList = new List(); var heads = AllEntityQuery(); while (heads.MoveNext(out var id, out _)) { commandList.Add(id); } return IsGroupDead(commandList, true); } private void OnHeadRevMobStateChanged(EntityUid uid, HeadRevolutionaryComponent comp, MobStateChangedEvent ev) { if (ev.NewMobState == MobState.Dead || ev.NewMobState == MobState.Invalid) CheckRevsLose(); } /// /// Checks if all the Head Revs are dead and if so will deconvert all regular revs. /// private bool CheckRevsLose() { var stunTime = TimeSpan.FromSeconds(4); var headRevList = new List(); var headRevs = AllEntityQuery(); while (headRevs.MoveNext(out var uid, out _, out _)) { headRevList.Add(uid); } // If no Head Revs are alive all normal Revs will lose their Rev status and rejoin Nanotrasen if (IsGroupDead(headRevList, false)) { var rev = AllEntityQuery(); while (rev.MoveNext(out var uid, out _, out var mc)) { if (HasComp(uid)) continue; _npcFaction.RemoveFaction(uid, RevolutionaryNpcFaction); _stun.TryParalyze(uid, stunTime, true); RemCompDeferred(uid); _popup.PopupEntity(Loc.GetString("rev-break-control", ("name", Identity.Entity(uid, EntityManager))), uid); _adminLogManager.Add(LogType.Mind, LogImpact.Medium, $"{ToPrettyString(uid)} was deconverted due to all Head Revolutionaries dying."); if (!_mind.TryGetMind(uid, out var mindId, out var mind, mc)) continue; // remove their antag role _role.MindTryRemoveRole(mindId); // make it very obvious to the rev they've been deconverted since // they may not see the popup due to antag and/or new player tunnel vision if (_mind.TryGetSession(mindId, out var session)) _euiMan.OpenEui(new DeconvertedEui(), session); } return true; } return false; } /// /// Will take a group of entities and check if they are all alive or dead /// /// The list of the entities /// Bool for if you want to check if someone is in space and consider them dead. (Won't check when emergency shuttle arrives just in case) /// private bool IsGroupDead(List list, bool checkOffStation) { var dead = 0; foreach (var entity in list) { if (TryComp(entity, out var state)) { if (state.CurrentState == MobState.Dead || state.CurrentState == MobState.Invalid) { dead++; } else if (checkOffStation && _stationSystem.GetOwningStation(entity) == null && !_emergencyShuttle.EmergencyShuttleArrived) { dead++; } } //If they don't have the MobStateComponent they might as well be dead. else { dead++; } } return dead == list.Count || list.Count == 0; } private static readonly string[] Outcomes = { // revs survived and heads survived... how "rev-reverse-stalemate", // revs won and heads died "rev-won", // revs lost and heads survived "rev-lost", // revs lost and heads died "rev-stalemate" }; }