Files
crystall-punk-14/Content.Shared/ActionBlocker/ActionBlockerSystem.cs

244 lines
7.9 KiB
C#
Raw Permalink Normal View History

2022-04-10 16:48:11 +12:00
using Content.Shared.Body.Events;
2021-06-09 22:19:39 +02:00
using Content.Shared.Emoting;
using Content.Shared.Hands;
using Content.Shared.Interaction;
using Content.Shared.Interaction.Components;
using Content.Shared.Interaction.Events;
using Content.Shared.Item;
2022-04-10 16:48:11 +12:00
using Content.Shared.Movement.Components;
using Content.Shared.Movement.Events;
2021-06-09 22:19:39 +02:00
using Content.Shared.Speech;
using Content.Shared.Throwing;
using Content.Shared.Weapons.Melee;
2020-12-20 04:31:04 +01:00
using JetBrains.Annotations;
using Robust.Shared.Containers;
2020-12-20 04:26:21 +01:00
2021-06-09 22:19:39 +02:00
namespace Content.Shared.ActionBlocker
2020-12-20 04:26:21 +01:00
{
/// <summary>
/// Utility methods to check if a specific entity is allowed to perform an action.
/// </summary>
[UsedImplicitly]
public sealed class ActionBlockerSystem : EntitySystem
2020-12-20 04:26:21 +01:00
{
[Dependency] private readonly SharedContainerSystem _container = default!;
private EntityQuery<ComplexInteractionComponent> _complexInteractionQuery;
2022-04-10 16:48:11 +12:00
public override void Initialize()
2020-12-20 04:26:21 +01:00
{
2022-04-10 16:48:11 +12:00
base.Initialize();
_complexInteractionQuery = GetEntityQuery<ComplexInteractionComponent>();
SubscribeLocalEvent<InputMoverComponent, ComponentStartup>(OnMoverStartup);
2022-04-10 16:48:11 +12:00
}
private void OnMoverStartup(EntityUid uid, InputMoverComponent component, ComponentStartup args)
2022-04-10 16:48:11 +12:00
{
UpdateCanMove(uid, component);
}
public bool CanMove(EntityUid uid, InputMoverComponent? component = null)
2022-04-10 16:48:11 +12:00
{
return Resolve(uid, ref component, false) && component.CanMove;
}
public bool UpdateCanMove(EntityUid uid, InputMoverComponent? component = null)
2022-04-10 16:48:11 +12:00
{
if (!Resolve(uid, ref component, false))
return false;
var ev = new UpdateCanMoveEvent(uid);
RaiseLocalEvent(uid, ev);
if (component.CanMove == ev.Cancelled)
2023-10-11 10:41:11 +11:00
Dirty(uid, component);
2022-04-10 16:48:11 +12:00
component.CanMove = !ev.Cancelled;
return !ev.Cancelled;
2020-12-20 04:26:21 +01:00
}
/// <summary>
/// Checks if a given entity is able to do specific complex interactions.
/// This is used to gate manipulation to general humanoids. If a mouse shouldn't be able to do something, then it's complex.
/// </summary>
public bool CanComplexInteract(EntityUid user)
{
return _complexInteractionQuery.HasComp(user);
}
/// <summary>
/// Raises an event directed at both the user and the target entity to check whether a user is capable of
/// interacting with this entity.
/// </summary>
/// <remarks>
/// If this is a generic interaction without a target (e.g., stop-drop-and-roll when burning), the target
/// may be null. Note that this is checked by <see cref="SharedInteractionSystem"/>. In the majority of
/// cases, systems that provide interactions will not need to check this themselves, though they may need to
/// check other blockers like <see cref="CanPickup(EntityUid)"/>
/// </remarks>
/// <returns></returns>
public bool CanInteract(EntityUid user, EntityUid? target)
2020-12-20 04:26:21 +01:00
{
if (!CanConsciouslyPerformAction(user))
return false;
var ev = new InteractionAttemptEvent(user, target);
RaiseLocalEvent(user, ref ev);
2020-12-20 04:26:21 +01:00
if (ev.Cancelled)
return false;
if (target == null || target == user)
return true;
var targetEv = new GettingInteractedWithAttemptEvent(user, target);
RaiseLocalEvent(target.Value, ref targetEv);
return !targetEv.Cancelled;
2020-12-20 04:26:21 +01:00
}
/// <summary>
/// Can a user utilize the entity that they are currently holding in their hands.
/// </summary>>
/// <remarks>
/// This event is automatically checked by <see cref="SharedInteractionSystem"/> for any interactions that
/// involve using a held entity. In the majority of cases, systems that provide interactions will not need
/// to check this themselves.
/// </remarks>
public bool CanUseHeldEntity(EntityUid user, EntityUid used)
2020-12-20 04:26:21 +01:00
{
var ev = new UseAttemptEvent(user, used);
RaiseLocalEvent(user, ev);
2020-12-20 04:26:21 +01:00
return !ev.Cancelled;
2020-12-20 04:26:21 +01:00
}
/// <summary>
/// Whether a user conscious to perform an action.
/// </summary>
/// <remarks>
/// This should be used when you want a much more permissive check than <see cref="CanInteract"/>
/// </remarks>
public bool CanConsciouslyPerformAction(EntityUid user)
{
var ev = new ConsciousAttemptEvent(user);
RaiseLocalEvent(user, ref ev);
return !ev.Cancelled;
}
2022-07-27 19:28:23 -04:00
public bool CanThrow(EntityUid user, EntityUid itemUid)
2020-12-20 04:26:21 +01:00
{
2022-07-27 19:28:23 -04:00
var ev = new ThrowAttemptEvent(user, itemUid);
RaiseLocalEvent(user, ev);
if (ev.Cancelled)
return false;
var itemEv = new ThrowItemAttemptEvent(user);
RaiseLocalEvent(itemUid, ref itemEv);
return !itemEv.Cancelled;
2020-12-20 04:26:21 +01:00
}
public bool CanSpeak(EntityUid uid)
2020-12-20 04:26:21 +01:00
{
// This one is used as broadcast
var ev = new SpeakAttemptEvent(uid);
RaiseLocalEvent(uid, ev, true);
2020-12-20 04:26:21 +01:00
return !ev.Cancelled;
2020-12-20 04:26:21 +01:00
}
public bool CanDrop(EntityUid uid)
2020-12-20 04:26:21 +01:00
{
var ev = new DropAttemptEvent();
RaiseLocalEvent(uid, ev);
return !ev.Cancelled;
2020-12-20 04:26:21 +01:00
}
2022-03-17 20:13:31 +13:00
public bool CanPickup(EntityUid user, EntityUid item)
2020-12-20 04:26:21 +01:00
{
2022-03-17 20:13:31 +13:00
var userEv = new PickupAttemptEvent(user, item);
RaiseLocalEvent(user, userEv);
2022-03-17 20:13:31 +13:00
if (userEv.Cancelled)
return false;
var itemEv = new GettingPickedUpAttemptEvent(user, item);
RaiseLocalEvent(item, itemEv);
2022-03-17 20:13:31 +13:00
return !itemEv.Cancelled;
2020-12-20 04:26:21 +01:00
}
public bool CanEmote(EntityUid uid)
2020-12-20 04:26:21 +01:00
{
// This one is used as broadcast
var ev = new EmoteAttemptEvent(uid);
RaiseLocalEvent(uid, ev, true);
2020-12-20 04:26:21 +01:00
return !ev.Cancelled;
2020-12-20 04:26:21 +01:00
}
public bool CanAttack(EntityUid uid, EntityUid? target = null, Entity<MeleeWeaponComponent>? weapon = null, bool disarm = false)
2020-12-20 04:26:21 +01:00
{
// If target is in a container can we attack
if (target != null && _container.IsEntityInContainer(target.Value))
{
return false;
}
_container.TryGetOuterContainer(uid, Transform(uid), out var outerContainer);
// If we're in a container can we attack the target.
if (target != null && target != outerContainer?.Owner && _container.IsEntityInContainer(uid))
{
var containerEv = new CanAttackFromContainerEvent(uid, target);
RaiseLocalEvent(uid, containerEv);
return containerEv.CanAttack;
}
var ev = new AttackAttemptEvent(uid, target, weapon, disarm);
RaiseLocalEvent(uid, ev);
if (ev.Cancelled)
return false;
if (target == null)
return true;
var tev = new GettingAttackedAttemptEvent(uid, weapon, disarm);
RaiseLocalEvent(target.Value, ref tev);
return !tev.Cancelled;
2020-12-20 04:26:21 +01:00
}
public bool CanChangeDirection(EntityUid uid)
2020-12-20 04:26:21 +01:00
{
var ev = new ChangeDirectionAttemptEvent(uid);
RaiseLocalEvent(uid, ev);
return !ev.Cancelled;
2020-12-20 04:26:21 +01:00
}
public bool CanShiver(EntityUid uid)
2020-12-20 04:26:21 +01:00
{
var ev = new ShiverAttemptEvent(uid);
RaiseLocalEvent(uid, ref ev);
return !ev.Cancelled;
2020-12-20 04:26:21 +01:00
}
public bool CanSweat(EntityUid uid)
2020-12-20 04:26:21 +01:00
{
var ev = new SweatAttemptEvent(uid);
RaiseLocalEvent(uid, ref ev);
return !ev.Cancelled;
2020-12-20 04:26:21 +01:00
}
}
}