Mindrole trigger condition (#40323)
* mind role trigger condition * fix * nits --------- Co-authored-by: ArtisticRoomba <145879011+ArtisticRoomba@users.noreply.github.com>
This commit is contained in:
@@ -23,7 +23,6 @@ public abstract class SharedRoleSystem : EntitySystem
|
|||||||
[Dependency] private readonly SharedAudioSystem _audio = default!;
|
[Dependency] private readonly SharedAudioSystem _audio = default!;
|
||||||
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
||||||
[Dependency] protected readonly ISharedPlayerManager Player = default!;
|
[Dependency] protected readonly ISharedPlayerManager Player = default!;
|
||||||
[Dependency] private readonly IEntityManager _entityManager = default!;
|
|
||||||
[Dependency] private readonly EntityWhitelistSystem _whitelist = default!;
|
[Dependency] private readonly EntityWhitelistSystem _whitelist = default!;
|
||||||
[Dependency] private readonly SharedMindSystem _minds = default!;
|
[Dependency] private readonly SharedMindSystem _minds = default!;
|
||||||
[Dependency] private readonly IPrototypeManager _prototypes = default!;
|
[Dependency] private readonly IPrototypeManager _prototypes = default!;
|
||||||
@@ -400,7 +399,7 @@ public abstract class SharedRoleSystem : EntitySystem
|
|||||||
|
|
||||||
foreach (var role in delete)
|
foreach (var role in delete)
|
||||||
{
|
{
|
||||||
_entityManager.DeleteEntity(role);
|
PredictedDel(role);
|
||||||
}
|
}
|
||||||
|
|
||||||
var update = MindRolesUpdate(mind);
|
var update = MindRolesUpdate(mind);
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
using Content.Shared.Whitelist;
|
||||||
|
using Robust.Shared.GameStates;
|
||||||
|
|
||||||
|
namespace Content.Shared.Trigger.Components.Conditions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Checks if a triggered entity or the user of a trigger has a certain mindrole.
|
||||||
|
/// Cancels the trigger otherwise.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Mind roles are only networked to their owner! So if you use this on any other entity than yourself it won't be predicted.
|
||||||
|
/// </remarks>
|
||||||
|
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
|
||||||
|
public sealed partial class MindRoleTriggerConditionComponent : BaseTriggerConditionComponent
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Whitelist for what mind role components on the owning entity allow this trigger.
|
||||||
|
/// </summary>
|
||||||
|
[DataField, AutoNetworkedField]
|
||||||
|
public EntityWhitelist? EntityWhitelist;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Whitelist for what mind role components on the User allow this trigger.
|
||||||
|
/// </summary>
|
||||||
|
[DataField, AutoNetworkedField]
|
||||||
|
public EntityWhitelist? UserWhitelist;
|
||||||
|
}
|
||||||
@@ -4,20 +4,20 @@ using Robust.Shared.GameStates;
|
|||||||
namespace Content.Shared.Trigger.Components.Conditions;
|
namespace Content.Shared.Trigger.Components.Conditions;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Checks if the user of a trigger satisfies a whitelist and blacklist condition for the triggered entity or the one triggering it.
|
/// Checks if the user of a trigger satisfies a whitelist and blacklist condition.
|
||||||
/// Cancels the trigger otherwise.
|
/// Cancels the trigger otherwise.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
|
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
|
||||||
public sealed partial class WhitelistTriggerConditionComponent : BaseTriggerConditionComponent
|
public sealed partial class WhitelistTriggerConditionComponent : BaseTriggerConditionComponent
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Whitelist for what entites can cause this trigger.
|
/// Whitelist for what entities can cause this trigger.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[DataField, AutoNetworkedField]
|
[DataField, AutoNetworkedField]
|
||||||
public EntityWhitelist? UserWhitelist;
|
public EntityWhitelist? UserWhitelist;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Blacklist for what entites can cause this trigger.
|
/// Blacklist for what entities can cause this trigger.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[DataField, AutoNetworkedField]
|
[DataField, AutoNetworkedField]
|
||||||
public EntityWhitelist? UserBlacklist;
|
public EntityWhitelist? UserBlacklist;
|
||||||
|
|||||||
@@ -10,31 +10,36 @@ public sealed partial class TriggerSystem
|
|||||||
private void InitializeCondition()
|
private void InitializeCondition()
|
||||||
{
|
{
|
||||||
SubscribeLocalEvent<WhitelistTriggerConditionComponent, AttemptTriggerEvent>(OnWhitelistTriggerAttempt);
|
SubscribeLocalEvent<WhitelistTriggerConditionComponent, AttemptTriggerEvent>(OnWhitelistTriggerAttempt);
|
||||||
|
|
||||||
SubscribeLocalEvent<UseDelayTriggerConditionComponent, AttemptTriggerEvent>(OnUseDelayTriggerAttempt);
|
SubscribeLocalEvent<UseDelayTriggerConditionComponent, AttemptTriggerEvent>(OnUseDelayTriggerAttempt);
|
||||||
|
|
||||||
SubscribeLocalEvent<ToggleTriggerConditionComponent, AttemptTriggerEvent>(OnToggleTriggerAttempt);
|
SubscribeLocalEvent<ToggleTriggerConditionComponent, AttemptTriggerEvent>(OnToggleTriggerAttempt);
|
||||||
SubscribeLocalEvent<ToggleTriggerConditionComponent, GetVerbsEvent<AlternativeVerb>>(OnToggleGetAltVerbs);
|
|
||||||
|
|
||||||
SubscribeLocalEvent<RandomChanceTriggerConditionComponent, AttemptTriggerEvent>(OnRandomChanceTriggerAttempt);
|
SubscribeLocalEvent<RandomChanceTriggerConditionComponent, AttemptTriggerEvent>(OnRandomChanceTriggerAttempt);
|
||||||
|
SubscribeLocalEvent<MindRoleTriggerConditionComponent, AttemptTriggerEvent>(OnMindRoleTriggerAttempt);
|
||||||
|
|
||||||
|
SubscribeLocalEvent<ToggleTriggerConditionComponent, GetVerbsEvent<AlternativeVerb>>(OnToggleGetAltVerbs);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnWhitelistTriggerAttempt(Entity<WhitelistTriggerConditionComponent> ent, ref AttemptTriggerEvent args)
|
private void OnWhitelistTriggerAttempt(Entity<WhitelistTriggerConditionComponent> ent, ref AttemptTriggerEvent args)
|
||||||
{
|
{
|
||||||
if (args.Key == null || ent.Comp.Keys.Contains(args.Key))
|
if (args.Key != null && !ent.Comp.Keys.Contains(args.Key))
|
||||||
args.Cancelled |= !_whitelist.CheckBoth(args.User, ent.Comp.UserBlacklist, ent.Comp.UserWhitelist);
|
return;
|
||||||
|
|
||||||
|
args.Cancelled |= !_whitelist.CheckBoth(args.User, ent.Comp.UserBlacklist, ent.Comp.UserWhitelist);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnUseDelayTriggerAttempt(Entity<UseDelayTriggerConditionComponent> ent, ref AttemptTriggerEvent args)
|
private void OnUseDelayTriggerAttempt(Entity<UseDelayTriggerConditionComponent> ent, ref AttemptTriggerEvent args)
|
||||||
{
|
{
|
||||||
if (args.Key == null || ent.Comp.Keys.Contains(args.Key))
|
if (args.Key != null && !ent.Comp.Keys.Contains(args.Key))
|
||||||
args.Cancelled |= _useDelay.IsDelayed(ent.Owner, ent.Comp.UseDelayId);
|
return;
|
||||||
|
|
||||||
|
args.Cancelled |= _useDelay.IsDelayed(ent.Owner, ent.Comp.UseDelayId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnToggleTriggerAttempt(Entity<ToggleTriggerConditionComponent> ent, ref AttemptTriggerEvent args)
|
private void OnToggleTriggerAttempt(Entity<ToggleTriggerConditionComponent> ent, ref AttemptTriggerEvent args)
|
||||||
{
|
{
|
||||||
if (args.Key == null || ent.Comp.Keys.Contains(args.Key))
|
if (args.Key != null && !ent.Comp.Keys.Contains(args.Key))
|
||||||
args.Cancelled |= !ent.Comp.Enabled;
|
return;
|
||||||
|
|
||||||
|
args.Cancelled |= !ent.Comp.Enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnToggleGetAltVerbs(Entity<ToggleTriggerConditionComponent> ent, ref GetVerbsEvent<AlternativeVerb> args)
|
private void OnToggleGetAltVerbs(Entity<ToggleTriggerConditionComponent> ent, ref GetVerbsEvent<AlternativeVerb> args)
|
||||||
@@ -62,19 +67,51 @@ public sealed partial class TriggerSystem
|
|||||||
private void OnRandomChanceTriggerAttempt(Entity<RandomChanceTriggerConditionComponent> ent,
|
private void OnRandomChanceTriggerAttempt(Entity<RandomChanceTriggerConditionComponent> ent,
|
||||||
ref AttemptTriggerEvent args)
|
ref AttemptTriggerEvent args)
|
||||||
{
|
{
|
||||||
if (args.Key == null || ent.Comp.Keys.Contains(args.Key))
|
if (args.Key != null && !ent.Comp.Keys.Contains(args.Key))
|
||||||
{
|
return;
|
||||||
// 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
|
// 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
|
||||||
|
}
|
||||||
|
private void OnMindRoleTriggerAttempt(Entity<MindRoleTriggerConditionComponent> ent, ref AttemptTriggerEvent args)
|
||||||
|
{
|
||||||
|
if (args.Key != null && !ent.Comp.Keys.Contains(args.Key))
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (ent.Comp.EntityWhitelist != null)
|
||||||
|
{
|
||||||
|
if (!_mind.TryGetMind(ent.Owner, out var entMindId, out var entMindComp))
|
||||||
|
{
|
||||||
|
args.Cancelled = true; // the entity has no mind
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!_role.MindHasRole((entMindId, entMindComp), ent.Comp.EntityWhitelist))
|
||||||
|
{
|
||||||
|
args.Cancelled = true; // the entity does not have the required role
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ent.Comp.UserWhitelist != null)
|
||||||
|
{
|
||||||
|
if (args.User == null || !_mind.TryGetMind(args.User.Value, out var userMindId, out var userMindComp))
|
||||||
|
{
|
||||||
|
args.Cancelled = true; // no user or the user has no mind
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!_role.MindHasRole((userMindId, userMindComp), ent.Comp.UserWhitelist))
|
||||||
|
{
|
||||||
|
args.Cancelled = true; // the user does not have the required role
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ using Content.Shared.Database;
|
|||||||
using Content.Shared.DeviceLinking;
|
using Content.Shared.DeviceLinking;
|
||||||
using Content.Shared.EntityTable;
|
using Content.Shared.EntityTable;
|
||||||
using Content.Shared.Item.ItemToggle;
|
using Content.Shared.Item.ItemToggle;
|
||||||
|
using Content.Shared.Mind;
|
||||||
using Content.Shared.Popups;
|
using Content.Shared.Popups;
|
||||||
|
using Content.Shared.Roles;
|
||||||
using Content.Shared.Timing;
|
using Content.Shared.Timing;
|
||||||
using Content.Shared.Trigger.Components;
|
using Content.Shared.Trigger.Components;
|
||||||
using Content.Shared.Whitelist;
|
using Content.Shared.Whitelist;
|
||||||
@@ -39,6 +41,8 @@ public sealed partial class TriggerSystem : EntitySystem
|
|||||||
[Dependency] private readonly EntityWhitelistSystem _whitelist = default!;
|
[Dependency] private readonly EntityWhitelistSystem _whitelist = default!;
|
||||||
[Dependency] private readonly ItemToggleSystem _itemToggle = default!;
|
[Dependency] private readonly ItemToggleSystem _itemToggle = default!;
|
||||||
[Dependency] private readonly SharedDeviceLinkSystem _deviceLink = default!;
|
[Dependency] private readonly SharedDeviceLinkSystem _deviceLink = default!;
|
||||||
|
[Dependency] private readonly SharedRoleSystem _role = default!;
|
||||||
|
[Dependency] private readonly SharedMindSystem _mind = default!;
|
||||||
[Dependency] private readonly EntityTableSystem _entityTable = default!;
|
[Dependency] private readonly EntityTableSystem _entityTable = default!;
|
||||||
|
|
||||||
public const string DefaultTriggerKey = "trigger";
|
public const string DefaultTriggerKey = "trigger";
|
||||||
|
|||||||
Reference in New Issue
Block a user