]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
make suicide actions require confirming (#24609)
authordeltanedas <39013340+deltanedas@users.noreply.github.com>
Fri, 1 Mar 2024 02:48:43 +0000 (02:48 +0000)
committerGitHub <noreply@github.com>
Fri, 1 Mar 2024 02:48:43 +0000 (13:48 +1100)
* add ActionAttemptEvent

* add ConfirmableAction compsys

* make suicide actions confirmable

* use new trolling techniques

* better query and dirty them

* death

---------

Co-authored-by: deltanedas <@deltanedas:kde.org>
Content.Shared/Actions/ConfirmableActionComponent.cs [new file with mode: 0644]
Content.Shared/Actions/ConfirmableActionSystem.cs [new file with mode: 0644]
Content.Shared/Actions/Events/ActionAttemptEvent.cs [new file with mode: 0644]
Content.Shared/Actions/SharedActionsSystem.cs
Resources/Locale/en-US/actions/actions/suicide.ftl [new file with mode: 0644]
Resources/Prototypes/Actions/types.yml

diff --git a/Content.Shared/Actions/ConfirmableActionComponent.cs b/Content.Shared/Actions/ConfirmableActionComponent.cs
new file mode 100644 (file)
index 0000000..6c208f4
--- /dev/null
@@ -0,0 +1,48 @@
+using Robust.Shared.GameStates;
+using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
+
+namespace Content.Shared.Actions;
+
+/// <summary>
+/// An action that must be confirmed before using it.
+/// Using it for the first time primes it, after a delay you can then confirm it.
+/// Used for dangerous actions that cannot be undone (unlike screaming).
+/// </summary>
+[RegisterComponent, NetworkedComponent, Access(typeof(ConfirmableActionSystem))]
+[AutoGenerateComponentState, AutoGenerateComponentPause]
+public sealed partial class ConfirmableActionComponent : Component
+{
+    /// <summary>
+    /// Warning popup shown when priming the action.
+    /// </summary>
+    [DataField(required: true)]
+    public LocId Popup = string.Empty;
+
+    /// <summary>
+    /// If not null, this is when the action can be confirmed at.
+    /// This is the time of priming plus the delay.
+    /// </summary>
+    [DataField(customTypeSerializer: typeof(TimeOffsetSerializer))]
+    [AutoNetworkedField, AutoPausedField]
+    public TimeSpan? NextConfirm;
+
+    /// <summary>
+    /// If not null, this is when the action will unprime at.
+    /// This is <c>NextConfirm> plus <c>PrimeTime</c>
+    /// </summary>
+    [DataField(customTypeSerializer: typeof(TimeOffsetSerializer))]
+    [AutoNetworkedField, AutoPausedField]
+    public TimeSpan? NextUnprime;
+
+    /// <summary>
+    /// Forced delay between priming and confirming to prevent accidents.
+    /// </summary>
+    [DataField]
+    public TimeSpan ConfirmDelay = TimeSpan.FromSeconds(1);
+
+    /// <summary>
+    /// Once you prime the action it will unprime after this length of time.
+    /// </summary>
+    [DataField]
+    public TimeSpan PrimeTime = TimeSpan.FromSeconds(5);
+}
diff --git a/Content.Shared/Actions/ConfirmableActionSystem.cs b/Content.Shared/Actions/ConfirmableActionSystem.cs
new file mode 100644 (file)
index 0000000..26cc711
--- /dev/null
@@ -0,0 +1,80 @@
+using Content.Shared.Actions.Events;
+using Content.Shared.Popups;
+using Robust.Shared.Timing;
+
+namespace Content.Shared.Actions;
+
+/// <summary>
+/// Handles action priming, confirmation and automatic unpriming.
+/// </summary>
+public sealed class ConfirmableActionSystem : EntitySystem
+{
+    [Dependency] private readonly IGameTiming _timing = default!;
+    [Dependency] private readonly SharedPopupSystem _popup = default!;
+
+    public override void Initialize()
+    {
+        base.Initialize();
+
+        SubscribeLocalEvent<ConfirmableActionComponent, ActionAttemptEvent>(OnAttempt);
+    }
+
+    public override void Update(float frameTime)
+    {
+        base.Update(frameTime);
+
+        // handle automatic unpriming
+        var now = _timing.CurTime;
+        var query = EntityQueryEnumerator<ConfirmableActionComponent>();
+        while (query.MoveNext(out var uid, out var comp))
+        {
+            if (comp.NextUnprime is not {} time)
+                continue;
+
+            if (now >= time)
+                Unprime((uid, comp));
+        }
+    }
+
+    private void OnAttempt(Entity<ConfirmableActionComponent> ent, ref ActionAttemptEvent args)
+    {
+        if (args.Cancelled)
+            return;
+
+        // if not primed, prime it and cancel the action
+        if (ent.Comp.NextConfirm is not {} confirm)
+        {
+            Prime(ent, args.User);
+            args.Cancelled = true;
+            return;
+        }
+
+        // primed but the delay isnt over, cancel the action
+        if (_timing.CurTime < confirm)
+        {
+            args.Cancelled = true;
+            return;
+        }
+
+        // primed and delay has passed, let the action go through
+        Unprime(ent);
+    }
+
+    private void Prime(Entity<ConfirmableActionComponent> ent, EntityUid user)
+    {
+        var (uid, comp) = ent;
+        comp.NextConfirm = _timing.CurTime + comp.ConfirmDelay;
+        comp.NextUnprime = comp.NextConfirm + comp.PrimeTime;
+        Dirty(uid, comp);
+
+        _popup.PopupClient(Loc.GetString(comp.Popup), user, user, PopupType.LargeCaution);
+    }
+
+    private void Unprime(Entity<ConfirmableActionComponent> ent)
+    {
+        var (uid, comp) = ent;
+        comp.NextConfirm = null;
+        comp.NextUnprime = null;
+        Dirty(uid, comp);
+    }
+}
diff --git a/Content.Shared/Actions/Events/ActionAttemptEvent.cs b/Content.Shared/Actions/Events/ActionAttemptEvent.cs
new file mode 100644 (file)
index 0000000..26f23f9
--- /dev/null
@@ -0,0 +1,8 @@
+namespace Content.Shared.Actions.Events;
+
+/// <summary>
+/// Raised before an action is used and can be cancelled to prevent it.
+/// Allowed to have side effects like modifying the action component.
+/// </summary>
+[ByRefEvent]
+public record struct ActionAttemptEvent(EntityUid User, bool Cancelled = false);
index a6c40c7ae35b0f58328931ed8b3bbf970eb1db9d..a3bfa071308f70ad8fe0a83c7b2294952962a71c 100644 (file)
@@ -352,6 +352,13 @@ public abstract class SharedActionsSystem : EntitySystem
         if (!action.Enabled)
             return;
 
+        // check for action use prevention
+        // TODO: make code below use this event with a dedicated component
+        var attemptEv = new ActionAttemptEvent(user);
+        RaiseLocalEvent(actionEnt, ref attemptEv);
+        if (attemptEv.Cancelled)
+            return;
+
         var curTime = GameTiming.CurTime;
         // TODO: Check for charge recovery timer
         if (action.Cooldown.HasValue && action.Cooldown.Value.End > curTime)
diff --git a/Resources/Locale/en-US/actions/actions/suicide.ftl b/Resources/Locale/en-US/actions/actions/suicide.ftl
new file mode 100644 (file)
index 0000000..a271790
--- /dev/null
@@ -0,0 +1 @@
+suicide-action-popup = THIS ACTION WILL KILL YOU! Use it again to confirm.
index c63071551b93a85259075bcd01db799d473b409b..2d5ec9a6784c8d5f0a3abdbbe01cc5e8d62a2238 100644 (file)
@@ -1,3 +1,14 @@
+# base actions
+
+- type: entity
+  id: BaseSuicideAction
+  abstract: true
+  components:
+  - type: ConfirmableAction
+    popup: suicide-action-popup
+
+# actions
+
 - type: entity
   id: ActionScream
   name: Scream
@@ -47,6 +58,7 @@
     event: !type:OpenStorageImplantEvent
 
 - type: entity
+  parent: BaseSuicideAction
   id: ActionActivateMicroBomb
   name: Activate Microbomb
   description: Activates your internal microbomb, completely destroying you and your equipment
@@ -62,6 +74,7 @@
     event: !type:ActivateImplantEvent
 
 - type: entity
+  parent: BaseSuicideAction
   id: ActionActivateDeathAcidifier
   name: Activate Death-Acidifier
   description: Activates your death-acidifier, completely melting you and your equipment