using Content.Server.GameTicking.Rules; using Content.Server.Traitor.Components; using Content.Shared.Mind; using Content.Shared.Mind.Components; namespace Content.Server.Traitor.Systems; /// /// Makes entities with a traitor either immediately if they have a mind or when a mind is added. /// public sealed class AutoTraitorSystem : EntitySystem { [Dependency] private readonly TraitorRuleSystem _traitorRule = default!; public override void Initialize() { base.Initialize(); SubscribeLocalEvent(OnMapInit); SubscribeLocalEvent(OnMindAdded); } private void OnMapInit(EntityUid uid, AutoTraitorComponent comp, MapInitEvent args) { TryMakeTraitor(uid, comp); } private void OnMindAdded(EntityUid uid, AutoTraitorComponent comp, MindAddedMessage args) { TryMakeTraitor(uid, comp); } /// /// Sets the GiveUplink field. /// public void SetGiveUplink(EntityUid uid, bool giveUplink, AutoTraitorComponent? comp = null) { if (!Resolve(uid, ref comp)) return; comp.GiveUplink = giveUplink; } /// /// Sets the GiveObjectives field. /// public void SetGiveObjectives(EntityUid uid, bool giveObjectives, AutoTraitorComponent? comp = null) { if (!Resolve(uid, ref comp)) return; comp.GiveObjectives = giveObjectives; } /// /// Checks if there is a mind, then makes it a traitor using the options. /// public bool TryMakeTraitor(EntityUid uid, AutoTraitorComponent? comp = null) { if (!Resolve(uid, ref comp)) return false; if (!TryComp(uid, out var mindContainer) || mindContainer.Mind == null) return false; var mindId = mindContainer.Mind.Value; if (!TryComp(mindId, out var mind) || mind.Session == null) return false; var session = mind.Session; _traitorRule.MakeTraitor(session, giveUplink: comp.GiveUplink, giveObjectives: comp.GiveObjectives); // prevent spamming anything if it fails RemComp(uid); return true; } }