return !_tagSystem.HasTag(user, "BypassInteractionRangeChecks");
}
+ /// <summary>
+ /// Returns true if the specified entity should hand interact with the target instead of attacking
+ /// </summary>
+ /// <param name="user">The user interacting in combat mode</param>
+ /// <param name="target">The target of the interaction</param>
+ /// <returns></returns>
+ public bool CombatModeCanHandInteract(EntityUid user, EntityUid? target)
+ {
+ // Always allow attack in these cases
+ if (target == null || !TryComp<HandsComponent>(user, out var hands) || hands.ActiveHand?.HeldEntity is not null)
+ return false;
+
+ // Only eat input if:
+ // - Target isn't an item
+ // - Target doesn't cancel should-interact event
+ // This is intended to allow items to be picked up in combat mode,
+ // but to also allow items to force attacks anyway (like mobs which are items, e.g. mice)
+ if (!HasComp<ItemComponent>(target))
+ return false;
+
+ var combatEv = new CombatModeShouldHandInteractEvent();
+ RaiseLocalEvent(target.Value, ref combatEv);
+
+ if (combatEv.Cancelled)
+ return false;
+
+ return true;
+ }
+
/// <summary>
/// Resolves user interactions with objects.
/// </summary>
// TODO this needs to be handled better. This probably bypasses many complex can-interact checks in weird roundabout ways.
if (_actionBlockerSystem.CanInteract(user, target))
{
- UserInteraction(relay.RelayEntity.Value, coordinates, target, altInteract, checkCanInteract, checkAccess, checkCanUse);
+ UserInteraction(relay.RelayEntity.Value, coordinates, target, altInteract, checkCanInteract,
+ checkAccess, checkCanUse);
return;
}
}
if (target != null && Deleted(target.Value))
return;
- if (!altInteract && TryComp(user, out CombatModeComponent? combatMode) && combatMode.IsInCombatMode)
+ if (!altInteract && TryComp<CombatModeComponent>(user, out var combatMode) && combatMode.IsInCombatMode)
{
- // Eat the input
- return;
+ if (!CombatModeCanHandInteract(user, target))
+ return;
}
if (!ValidateInteractAndFace(user, coordinates))
: !checkAccess || InRangeUnobstructed(user, target.Value); // permits interactions with wall mounted entities
// Does the user have hands?
- if (!TryComp(user, out HandsComponent? hands) || hands.ActiveHand == null)
+ if (!TryComp<HandsComponent>(user, out var hands) || hands.ActiveHand == null)
{
var ev = new InteractNoHandEvent(user, target, coordinates);
RaiseLocalEvent(user, ev);
}
// empty-hand interactions
+ // combat mode hand interactions will always be true here -- since
+ // they check this earlier before returning in
if (hands.ActiveHandEntity is not { } held)
{
if (inRangeUnobstructed && target != null)
AltInteract = altInteract;
}
}
+
+ /// <summary>
+ /// Raised directed by-ref on an item to determine if hand interactions should go through.
+ /// Defaults to allowing hand interactions to go through. Cancel to force the item to be attacked instead.
+ /// </summary>
+ /// <param name="Cancelled">Whether the hand interaction should be cancelled.</param>
+ [ByRefEvent]
+ public record struct CombatModeShouldHandInteractEvent(bool Cancelled = false);
}
using Content.Shared.Bed.Sleep;
using Content.Shared.Emoting;
using Content.Shared.Hands;
+using Content.Shared.Interaction;
using Content.Shared.Interaction.Events;
using Content.Shared.Inventory.Events;
using Content.Shared.Item;
SubscribeLocalEvent<MobStateComponent, UpdateCanMoveEvent>(CheckAct);
SubscribeLocalEvent<MobStateComponent, StandAttemptEvent>(CheckAct);
SubscribeLocalEvent<MobStateComponent, TryingToSleepEvent>(OnSleepAttempt);
+ SubscribeLocalEvent<MobStateComponent, CombatModeShouldHandInteractEvent>(OnCombatModeShouldHandInteract);
}
private void OnStateExitSubscribers(EntityUid target, MobStateComponent component, MobState state)
CheckAct(target, component, args);
}
+ private void OnCombatModeShouldHandInteract(EntityUid uid, MobStateComponent component, ref CombatModeShouldHandInteractEvent args)
+ {
+ // Disallow empty-hand-interacting in combat mode
+ // for non-dead mobs
+ if (!IsDead(uid, component))
+ args.Cancelled = true;
+ }
+
#endregion
}