From 024301e69846711c031033f4e4486140350b4a79 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C4=81da?= Date: Tue, 12 Aug 2025 18:04:29 -0500 Subject: [PATCH] RandomChance trigger condition (#39543) * 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 --- .../RandomChanceTriggerConditionComponent.cs | 16 ++++++++++++ .../Systems/TriggerSystem.Condition.cs | 25 ++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 Content.Shared/Trigger/Components/Conditions/RandomChanceTriggerConditionComponent.cs diff --git a/Content.Shared/Trigger/Components/Conditions/RandomChanceTriggerConditionComponent.cs b/Content.Shared/Trigger/Components/Conditions/RandomChanceTriggerConditionComponent.cs new file mode 100644 index 0000000000..2612f16007 --- /dev/null +++ b/Content.Shared/Trigger/Components/Conditions/RandomChanceTriggerConditionComponent.cs @@ -0,0 +1,16 @@ +using Robust.Shared.GameStates; + +namespace Content.Shared.Trigger.Components.Conditions; + +/// +/// This condition will cancel triggers based on random chance. +/// +[RegisterComponent, NetworkedComponent, AutoGenerateComponentState] +public sealed partial class RandomChanceTriggerConditionComponent : BaseTriggerConditionComponent +{ + /// + /// Chance for the trigger to succeed. + /// + [DataField, AutoNetworkedField] + public float SuccessChance = .9f; +} diff --git a/Content.Shared/Trigger/Systems/TriggerSystem.Condition.cs b/Content.Shared/Trigger/Systems/TriggerSystem.Condition.cs index a917f1ad48..2d0756556a 100644 --- a/Content.Shared/Trigger/Systems/TriggerSystem.Condition.cs +++ b/Content.Shared/Trigger/Systems/TriggerSystem.Condition.cs @@ -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(OnToggleTriggerAttempt); SubscribeLocalEvent>(OnToggleGetAltVerbs); + + SubscribeLocalEvent(OnRandomChanceTriggerAttempt); } private void OnWhitelistTriggerAttempt(Entity 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 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)_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 + } + } } -- 2.51.2