]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
RandomChance trigger condition (#39543)
authorāda <ss.adasts@gmail.com>
Tue, 12 Aug 2025 23:04:29 +0000 (18:04 -0500)
committerGitHub <noreply@github.com>
Tue, 12 Aug 2025 23:04:29 +0000 (01:04 +0200)
* branch names don't matter anyway

* commits are a window to the soul

* requested change

* also requested

* ship it

* remove key

---------

Co-authored-by: iaada <iaada@users.noreply.github.com>
Content.Shared/Trigger/Components/Conditions/RandomChanceTriggerConditionComponent.cs [new file with mode: 0644]
Content.Shared/Trigger/Systems/TriggerSystem.Condition.cs

diff --git a/Content.Shared/Trigger/Components/Conditions/RandomChanceTriggerConditionComponent.cs b/Content.Shared/Trigger/Components/Conditions/RandomChanceTriggerConditionComponent.cs
new file mode 100644 (file)
index 0000000..2612f16
--- /dev/null
@@ -0,0 +1,16 @@
+using Robust.Shared.GameStates;
+
+namespace Content.Shared.Trigger.Components.Conditions;
+
+/// <summary>
+/// This condition will cancel triggers based on random chance.
+/// </summary>
+[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
+public sealed partial class RandomChanceTriggerConditionComponent : BaseTriggerConditionComponent
+{
+    /// <summary>
+    /// Chance for the trigger to succeed.
+    /// </summary>
+    [DataField, AutoNetworkedField]
+    public float SuccessChance = .9f;
+}
index a917f1ad48c1431f92265f55cfd576b4f313dc89..2d0756556ad9bec2035bd69069ce76a4ec63a6c1 100644 (file)
@@ -1,5 +1,7 @@
-using Content.Shared.Trigger.Components.Conditions;
+using Content.Shared.Random.Helpers;
+using Content.Shared.Trigger.Components.Conditions;
 using Content.Shared.Verbs;
+using Robust.Shared.Random;
 
 namespace Content.Shared.Trigger.Systems;
 
@@ -13,6 +15,8 @@ public sealed partial class TriggerSystem
 
         SubscribeLocalEvent<ToggleTriggerConditionComponent, AttemptTriggerEvent>(OnToggleTriggerAttempt);
         SubscribeLocalEvent<ToggleTriggerConditionComponent, GetVerbsEvent<AlternativeVerb>>(OnToggleGetAltVerbs);
+
+        SubscribeLocalEvent<RandomChanceTriggerConditionComponent, AttemptTriggerEvent>(OnRandomChanceTriggerAttempt);
     }
 
     private void OnWhitelistTriggerAttempt(Entity<WhitelistTriggerConditionComponent> ent, ref AttemptTriggerEvent args)
@@ -54,4 +58,23 @@ public sealed partial class TriggerSystem
         ent.Comp.Enabled = !ent.Comp.Enabled;
         Dirty(ent);
     }
+
+    private void OnRandomChanceTriggerAttempt(Entity<RandomChanceTriggerConditionComponent> ent,
+        ref AttemptTriggerEvent args)
+    {
+        if (args.Key == null || ent.Comp.Keys.Contains(args.Key))
+        {
+            // TODO: Replace with RandomPredicted once the engine PR is merged
+            var hash = new List<int>
+            {
+                (int)_timing.CurTick.Value,
+                GetNetEntity(ent).Id,
+                args.User == null ? 0 : GetNetEntity(args.User.Value).Id,
+            };
+            var seed = SharedRandomExtensions.HashCodeCombine(hash);
+            var rand = new System.Random(seed);
+
+            args.Cancelled |= !rand.Prob(ent.Comp.SuccessChance); // When not successful, Cancelled = true
+        }
+    }
 }