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 <iaada@users.noreply.github.com>
This commit is contained in:
@@ -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;
|
||||||
|
}
|
||||||
@@ -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 Content.Shared.Verbs;
|
||||||
|
using Robust.Shared.Random;
|
||||||
|
|
||||||
namespace Content.Shared.Trigger.Systems;
|
namespace Content.Shared.Trigger.Systems;
|
||||||
|
|
||||||
@@ -13,6 +15,8 @@ public sealed partial class TriggerSystem
|
|||||||
|
|
||||||
SubscribeLocalEvent<ToggleTriggerConditionComponent, AttemptTriggerEvent>(OnToggleTriggerAttempt);
|
SubscribeLocalEvent<ToggleTriggerConditionComponent, AttemptTriggerEvent>(OnToggleTriggerAttempt);
|
||||||
SubscribeLocalEvent<ToggleTriggerConditionComponent, GetVerbsEvent<AlternativeVerb>>(OnToggleGetAltVerbs);
|
SubscribeLocalEvent<ToggleTriggerConditionComponent, GetVerbsEvent<AlternativeVerb>>(OnToggleGetAltVerbs);
|
||||||
|
|
||||||
|
SubscribeLocalEvent<RandomChanceTriggerConditionComponent, AttemptTriggerEvent>(OnRandomChanceTriggerAttempt);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnWhitelistTriggerAttempt(Entity<WhitelistTriggerConditionComponent> ent, ref AttemptTriggerEvent args)
|
private void OnWhitelistTriggerAttempt(Entity<WhitelistTriggerConditionComponent> ent, ref AttemptTriggerEvent args)
|
||||||
@@ -54,4 +58,23 @@ public sealed partial class TriggerSystem
|
|||||||
ent.Comp.Enabled = !ent.Comp.Enabled;
|
ent.Comp.Enabled = !ent.Comp.Enabled;
|
||||||
Dirty(ent);
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user