2023-07-08 14:08:32 +10:00
|
|
|
using System.Numerics;
|
2024-01-14 06:18:47 -04:00
|
|
|
using Content.Server.Inventory;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Stack;
|
2022-02-26 18:24:08 +13:00
|
|
|
using Content.Server.Stunnable;
|
2021-11-29 02:34:44 +13:00
|
|
|
using Content.Shared.ActionBlocker;
|
2022-11-13 22:34:26 +01:00
|
|
|
using Content.Shared.Body.Part;
|
2022-11-09 07:34:07 +11:00
|
|
|
using Content.Shared.CombatMode;
|
2024-02-21 04:01:45 +00:00
|
|
|
using Content.Shared.Damage.Systems;
|
2023-11-19 17:44:42 +00:00
|
|
|
using Content.Shared.Explosion;
|
2021-06-21 02:21:20 -07:00
|
|
|
using Content.Shared.Hands.Components;
|
2022-05-12 22:30:30 -07:00
|
|
|
using Content.Shared.Hands.EntitySystems;
|
2018-08-22 01:19:47 -07:00
|
|
|
using Content.Shared.Input;
|
2024-01-14 06:18:47 -04:00
|
|
|
using Content.Shared.Inventory.VirtualItem;
|
2024-03-19 14:30:56 +11:00
|
|
|
using Content.Shared.Movement.Pulling.Components;
|
|
|
|
|
using Content.Shared.Movement.Pulling.Events;
|
|
|
|
|
using Content.Shared.Movement.Pulling.Systems;
|
2022-12-24 23:28:21 -05:00
|
|
|
using Content.Shared.Stacks;
|
2022-05-12 22:30:30 -07:00
|
|
|
using Content.Shared.Throwing;
|
2022-01-05 17:53:08 +13:00
|
|
|
using Robust.Shared.GameStates;
|
2020-05-31 14:32:05 -07:00
|
|
|
using Robust.Shared.Input.Binding;
|
2019-04-15 21:11:38 -06:00
|
|
|
using Robust.Shared.Map;
|
2023-10-28 09:59:53 +11:00
|
|
|
using Robust.Shared.Player;
|
2024-05-02 05:32:47 -07:00
|
|
|
using Robust.Shared.Random;
|
2023-12-31 22:24:37 -05:00
|
|
|
using Robust.Shared.Timing;
|
2021-07-31 03:14:00 +02:00
|
|
|
using Robust.Shared.Utility;
|
2018-08-18 15:28:02 -07:00
|
|
|
|
2021-09-17 07:16:11 -07:00
|
|
|
namespace Content.Server.Hands.Systems
|
2018-08-18 15:28:02 -07:00
|
|
|
{
|
2023-09-11 21:20:46 +10:00
|
|
|
public sealed class HandsSystem : SharedHandsSystem
|
2018-08-18 15:28:02 -07:00
|
|
|
{
|
2023-12-31 22:24:37 -05:00
|
|
|
[Dependency] private readonly IGameTiming _timing = default!;
|
2024-05-02 05:32:47 -07:00
|
|
|
[Dependency] private readonly IRobustRandom _random = default!;
|
2021-07-26 12:58:17 +02:00
|
|
|
[Dependency] private readonly StackSystem _stackSystem = default!;
|
2024-01-14 06:18:47 -04:00
|
|
|
[Dependency] private readonly VirtualItemSystem _virtualItemSystem = default!;
|
2021-11-29 02:34:44 +13:00
|
|
|
[Dependency] private readonly ActionBlockerSystem _actionBlockerSystem = default!;
|
2024-03-20 21:59:56 -04:00
|
|
|
[Dependency] private readonly SharedTransformSystem _transformSystem = default!;
|
2022-03-17 20:13:31 +13:00
|
|
|
[Dependency] private readonly PullingSystem _pullingSystem = default!;
|
2022-03-24 02:33:01 +13:00
|
|
|
[Dependency] private readonly ThrowingSystem _throwingSystem = default!;
|
|
|
|
|
|
2018-08-18 15:28:02 -07:00
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
2024-02-21 04:01:45 +00:00
|
|
|
SubscribeLocalEvent<HandsComponent, DisarmedEvent>(OnDisarmed, before: new[] {typeof(StunSystem), typeof(StaminaSystem)});
|
2021-07-31 03:14:00 +02:00
|
|
|
|
|
|
|
|
SubscribeLocalEvent<HandsComponent, PullStartedMessage>(HandlePullStarted);
|
|
|
|
|
SubscribeLocalEvent<HandsComponent, PullStoppedMessage>(HandlePullStopped);
|
2020-02-18 19:43:54 -08:00
|
|
|
|
2022-11-13 22:34:26 +01:00
|
|
|
SubscribeLocalEvent<HandsComponent, BodyPartAddedEvent>(HandleBodyPartAdded);
|
|
|
|
|
SubscribeLocalEvent<HandsComponent, BodyPartRemovedEvent>(HandleBodyPartRemoved);
|
|
|
|
|
|
2022-01-05 17:53:08 +13:00
|
|
|
SubscribeLocalEvent<HandsComponent, ComponentGetState>(GetComponentState);
|
|
|
|
|
|
2023-11-19 17:44:42 +00:00
|
|
|
SubscribeLocalEvent<HandsComponent, BeforeExplodeEvent>(OnExploded);
|
|
|
|
|
|
2020-05-31 14:32:05 -07:00
|
|
|
CommandBinds.Builder
|
|
|
|
|
.Bind(ContentKeyFunctions.ThrowItemInHand, new PointerInputCmdHandler(HandleThrowItem))
|
2020-07-27 00:54:32 +02:00
|
|
|
.Register<HandsSystem>();
|
2018-08-18 15:28:02 -07:00
|
|
|
}
|
2018-11-21 20:58:11 +01:00
|
|
|
|
2022-01-05 17:53:08 +13:00
|
|
|
public override void Shutdown()
|
|
|
|
|
{
|
|
|
|
|
base.Shutdown();
|
|
|
|
|
|
|
|
|
|
CommandBinds.Unregister<HandsSystem>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void GetComponentState(EntityUid uid, HandsComponent hands, ref ComponentGetState args)
|
|
|
|
|
{
|
2022-03-17 20:13:31 +13:00
|
|
|
args.State = new HandsComponentState(hands);
|
2022-01-05 17:53:08 +13:00
|
|
|
}
|
|
|
|
|
|
2023-12-31 22:24:37 -05:00
|
|
|
|
2023-11-19 17:44:42 +00:00
|
|
|
private void OnExploded(Entity<HandsComponent> ent, ref BeforeExplodeEvent args)
|
|
|
|
|
{
|
2024-04-02 07:18:31 +02:00
|
|
|
if (ent.Comp.DisableExplosionRecursion)
|
|
|
|
|
return;
|
|
|
|
|
|
2023-11-19 17:44:42 +00:00
|
|
|
foreach (var hand in ent.Comp.Hands.Values)
|
|
|
|
|
{
|
2023-12-27 18:05:20 -05:00
|
|
|
if (hand.HeldEntity is { } uid)
|
2023-11-19 17:44:42 +00:00
|
|
|
args.Contents.Add(uid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-26 18:24:08 +13:00
|
|
|
private void OnDisarmed(EntityUid uid, HandsComponent component, DisarmedEvent args)
|
|
|
|
|
{
|
2022-03-17 20:13:31 +13:00
|
|
|
if (args.Handled)
|
2022-02-26 18:24:08 +13:00
|
|
|
return;
|
|
|
|
|
|
2022-03-17 20:13:31 +13:00
|
|
|
// Break any pulls
|
2024-03-19 14:30:56 +11:00
|
|
|
if (TryComp(uid, out PullerComponent? puller) && TryComp(puller.Pulling, out PullableComponent? pullable))
|
|
|
|
|
_pullingSystem.TryStopPull(puller.Pulling.Value, pullable);
|
2022-03-17 20:13:31 +13:00
|
|
|
|
2024-05-02 05:32:47 -07:00
|
|
|
var offsetRandomCoordinates = _transformSystem.GetMoverCoordinates(args.Target).Offset(_random.NextVector2(1f, 1.5f));
|
|
|
|
|
if (!ThrowHeldItem(args.Target, offsetRandomCoordinates))
|
2022-02-26 18:24:08 +13:00
|
|
|
return;
|
|
|
|
|
|
2024-02-21 04:01:45 +00:00
|
|
|
args.PopupPrefix = "disarm-action-";
|
|
|
|
|
|
2022-02-26 18:24:08 +13:00
|
|
|
args.Handled = true; // no shove/stun.
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-13 22:34:26 +01:00
|
|
|
private void HandleBodyPartAdded(EntityUid uid, HandsComponent component, ref BodyPartAddedEvent args)
|
|
|
|
|
{
|
2024-03-28 01:48:37 +01:00
|
|
|
if (args.Part.Comp.PartType != BodyPartType.Hand)
|
2022-11-13 22:34:26 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// If this annoys you, which it should.
|
|
|
|
|
// Ping Smugleaf.
|
2024-03-28 01:48:37 +01:00
|
|
|
var location = args.Part.Comp.Symmetry switch
|
2022-11-13 22:34:26 +01:00
|
|
|
{
|
|
|
|
|
BodyPartSymmetry.None => HandLocation.Middle,
|
|
|
|
|
BodyPartSymmetry.Left => HandLocation.Left,
|
|
|
|
|
BodyPartSymmetry.Right => HandLocation.Right,
|
2024-03-28 01:48:37 +01:00
|
|
|
_ => throw new ArgumentOutOfRangeException(nameof(args.Part.Comp.Symmetry))
|
2022-11-13 22:34:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
AddHand(uid, args.Slot, location);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandleBodyPartRemoved(EntityUid uid, HandsComponent component, ref BodyPartRemovedEvent args)
|
|
|
|
|
{
|
2024-03-28 01:48:37 +01:00
|
|
|
if (args.Part.Comp.PartType != BodyPartType.Hand)
|
2022-11-13 22:34:26 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
RemoveHand(uid, args.Slot);
|
|
|
|
|
}
|
2022-01-05 17:53:08 +13:00
|
|
|
|
|
|
|
|
#region pulling
|
2023-12-27 18:05:20 -05:00
|
|
|
|
2022-06-30 14:40:32 -04:00
|
|
|
private void HandlePullStarted(EntityUid uid, HandsComponent component, PullStartedMessage args)
|
2021-07-31 03:14:00 +02:00
|
|
|
{
|
2024-03-19 14:30:56 +11:00
|
|
|
if (args.PullerUid != uid)
|
2021-12-13 18:46:09 +13:00
|
|
|
return;
|
|
|
|
|
|
2024-03-19 14:30:56 +11:00
|
|
|
if (TryComp<PullerComponent>(args.PullerUid, out var pullerComp) && !pullerComp.NeedsHands)
|
2021-12-13 18:46:09 +13:00
|
|
|
return;
|
|
|
|
|
|
2024-03-19 14:30:56 +11:00
|
|
|
if (!_virtualItemSystem.TrySpawnVirtualItemInHand(args.PulledUid, uid))
|
2021-07-31 03:14:00 +02:00
|
|
|
{
|
2021-09-17 07:16:11 -07:00
|
|
|
DebugTools.Assert("Unable to find available hand when starting pulling??");
|
2021-07-31 03:14:00 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandlePullStopped(EntityUid uid, HandsComponent component, PullStoppedMessage args)
|
|
|
|
|
{
|
2024-03-19 14:30:56 +11:00
|
|
|
if (args.PullerUid != uid)
|
2021-12-13 18:46:09 +13:00
|
|
|
return;
|
|
|
|
|
|
2021-07-31 03:14:00 +02:00
|
|
|
// Try find hand that is doing this pull.
|
|
|
|
|
// and clear it.
|
2022-03-17 20:13:31 +13:00
|
|
|
foreach (var hand in component.Hands.Values)
|
2021-07-31 03:14:00 +02:00
|
|
|
{
|
2021-12-30 18:27:15 -08:00
|
|
|
if (hand.HeldEntity == null
|
2024-01-14 06:18:47 -04:00
|
|
|
|| !TryComp(hand.HeldEntity, out VirtualItemComponent? virtualItem)
|
2024-03-19 14:30:56 +11:00
|
|
|
|| virtualItem.BlockingEntity != args.PulledUid)
|
|
|
|
|
{
|
2021-07-31 03:14:00 +02:00
|
|
|
continue;
|
2024-03-19 14:30:56 +11:00
|
|
|
}
|
2021-07-31 03:14:00 +02:00
|
|
|
|
2024-04-13 08:36:05 -07:00
|
|
|
TryDrop(args.PullerUid, hand, handsComp: component);
|
2021-07-31 03:14:00 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-27 18:05:20 -05:00
|
|
|
|
2022-01-05 17:53:08 +13:00
|
|
|
#endregion
|
2021-07-31 03:14:00 +02:00
|
|
|
|
2022-01-05 17:53:08 +13:00
|
|
|
#region interactions
|
2023-12-27 18:05:20 -05:00
|
|
|
|
2023-10-28 09:59:53 +11:00
|
|
|
private bool HandleThrowItem(ICommonSession? playerSession, EntityCoordinates coordinates, EntityUid entity)
|
2018-08-22 01:19:47 -07:00
|
|
|
{
|
2024-07-21 16:13:28 +10:00
|
|
|
if (playerSession?.AttachedEntity is not {Valid: true} player || !Exists(player) || !coordinates.IsValid(EntityManager))
|
2019-09-17 16:08:45 -07:00
|
|
|
return false;
|
2018-08-22 01:19:47 -07:00
|
|
|
|
2023-12-27 18:05:20 -05:00
|
|
|
return ThrowHeldItem(player, coordinates);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Throw the player's currently held item.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool ThrowHeldItem(EntityUid player, EntityCoordinates coordinates, float minDistance = 0.1f)
|
|
|
|
|
{
|
|
|
|
|
if (ContainerSystem.IsEntityInContainer(player) ||
|
2023-04-07 11:21:12 -07:00
|
|
|
!TryComp(player, out HandsComponent? hands) ||
|
2023-10-11 02:18:49 -07:00
|
|
|
hands.ActiveHandEntity is not { } throwEnt ||
|
2022-07-27 19:28:23 -04:00
|
|
|
!_actionBlockerSystem.CanThrow(player, throwEnt))
|
2019-09-17 16:08:45 -07:00
|
|
|
return false;
|
2019-07-18 23:33:02 +02:00
|
|
|
|
2023-12-31 22:24:37 -05:00
|
|
|
if (_timing.CurTime < hands.NextThrowTime)
|
|
|
|
|
return false;
|
|
|
|
|
hands.NextThrowTime = _timing.CurTime + hands.ThrowCooldown;
|
|
|
|
|
|
2022-03-17 20:13:31 +13:00
|
|
|
if (EntityManager.TryGetComponent(throwEnt, out StackComponent? stack) && stack.Count > 1 && stack.ThrowIndividually)
|
2018-10-30 01:13:10 -07:00
|
|
|
{
|
2022-03-17 20:13:31 +13:00
|
|
|
var splitStack = _stackSystem.Split(throwEnt, 1, EntityManager.GetComponent<TransformComponent>(player).Coordinates, stack);
|
2019-09-01 16:23:30 -07:00
|
|
|
|
2021-12-05 21:02:04 +01:00
|
|
|
if (splitStack is not {Valid: true})
|
2021-05-26 10:20:57 +02:00
|
|
|
return false;
|
|
|
|
|
|
2021-12-05 21:02:04 +01:00
|
|
|
throwEnt = splitStack.Value;
|
2018-10-30 01:13:10 -07:00
|
|
|
}
|
2018-11-21 20:58:11 +01:00
|
|
|
|
2024-08-06 04:02:01 -07:00
|
|
|
var direction = _transformSystem.ToMapCoordinates(coordinates).Position - _transformSystem.GetWorldPosition(player);
|
2021-06-21 02:21:20 -07:00
|
|
|
if (direction == Vector2.Zero)
|
|
|
|
|
return true;
|
2021-03-08 04:09:59 +11:00
|
|
|
|
2023-12-27 18:05:20 -05:00
|
|
|
var length = direction.Length();
|
|
|
|
|
var distance = Math.Clamp(length, minDistance, hands.ThrowRange);
|
2024-07-08 11:03:53 +02:00
|
|
|
direction *= distance / length;
|
2021-07-25 16:58:02 +10:00
|
|
|
|
2024-07-08 11:03:53 +02:00
|
|
|
var throwSpeed = hands.BaseThrowspeed;
|
2022-07-27 19:28:23 -04:00
|
|
|
|
|
|
|
|
// Let other systems change the thrown entity (useful for virtual items)
|
|
|
|
|
// or the throw strength.
|
2024-07-08 11:03:53 +02:00
|
|
|
var ev = new BeforeThrowEvent(throwEnt, direction, throwSpeed, player);
|
2023-12-11 15:40:22 -08:00
|
|
|
RaiseLocalEvent(player, ref ev);
|
2022-07-27 19:28:23 -04:00
|
|
|
|
2023-12-11 15:40:22 -08:00
|
|
|
if (ev.Cancelled)
|
2022-07-27 19:28:23 -04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
// This can grief the above event so we raise it afterwards
|
2023-12-31 22:24:37 -05:00
|
|
|
if (IsHolding(player, throwEnt, out _, hands) && !TryDrop(player, throwEnt, handsComp: hands))
|
2022-07-27 19:28:23 -04:00
|
|
|
return false;
|
|
|
|
|
|
2024-07-08 11:03:53 +02:00
|
|
|
_throwingSystem.TryThrow(ev.ItemUid, ev.Direction, ev.ThrowSpeed, ev.PlayerUid, compensateFriction: !HasComp<LandAtCursorComponent>(ev.ItemUid));
|
2019-07-18 23:33:02 +02:00
|
|
|
|
2019-09-17 16:08:45 -07:00
|
|
|
return true;
|
2018-08-22 01:19:47 -07:00
|
|
|
}
|
2022-02-19 14:16:15 -05:00
|
|
|
|
2022-01-05 17:53:08 +13:00
|
|
|
#endregion
|
2018-08-18 15:28:02 -07:00
|
|
|
}
|
|
|
|
|
}
|