2023-07-08 14:08:32 +10:00
|
|
|
using System.Numerics;
|
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-03-19 14:30:56 +11:00
|
|
|
using Content.Shared.Movement.Pulling.Components;
|
|
|
|
|
using Content.Shared.Movement.Pulling.Systems;
|
2022-12-24 23:28:21 -05:00
|
|
|
using Content.Shared.Stacks;
|
2025-03-22 06:00:21 +01:00
|
|
|
using Content.Shared.Standing;
|
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;
|
2025-03-22 06:00:21 +01:00
|
|
|
using Robust.Shared.Physics.Components;
|
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;
|
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!;
|
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!;
|
|
|
|
|
|
2025-03-31 18:00:04 -04:00
|
|
|
private EntityQuery<PhysicsComponent> _physicsQuery;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Items dropped when the holder falls down will be launched in
|
|
|
|
|
/// a direction offset by up to this many degrees from the holder's
|
|
|
|
|
/// movement direction.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private const float DropHeldItemsSpread = 45;
|
|
|
|
|
|
2018-08-18 15:28:02 -07:00
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
2025-05-06 09:39:05 -07:00
|
|
|
SubscribeLocalEvent<HandsComponent, DisarmedEvent>(OnDisarmed, before: new[] {typeof(StunSystem), typeof(SharedStaminaSystem)});
|
2021-07-31 03:14:00 +02: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);
|
|
|
|
|
|
2025-03-22 06:00:21 +01:00
|
|
|
SubscribeLocalEvent<HandsComponent, DropHandItemsEvent>(OnDropHandItems);
|
|
|
|
|
|
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>();
|
2025-03-31 18:00:04 -04:00
|
|
|
|
|
|
|
|
_physicsQuery = GetEntityQuery<PhysicsComponent>();
|
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;
|
|
|
|
|
|
2025-06-25 09:13:03 -04:00
|
|
|
foreach (var held in EnumerateHeld(ent.AsNullable()))
|
2023-11-19 17:44:42 +00:00
|
|
|
{
|
2025-06-25 09:13:03 -04:00
|
|
|
args.Contents.Add(held);
|
2023-11-19 17:44:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-19 11:38:22 +10:00
|
|
|
private void OnDisarmed(EntityUid uid, HandsComponent component, ref DisarmedEvent args)
|
2022-02-26 18:24:08 +13:00
|
|
|
{
|
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.
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-25 09:13:03 -04:00
|
|
|
private void HandleBodyPartAdded(Entity<HandsComponent> ent, ref BodyPartAddedEvent args)
|
2022-11-13 22:34:26 +01:00
|
|
|
{
|
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
|
|
|
};
|
|
|
|
|
|
2025-06-25 09:13:03 -04:00
|
|
|
AddHand(ent.AsNullable(), args.Slot, location);
|
2022-11-13 22:34:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 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) ||
|
2025-06-25 09:13:03 -04:00
|
|
|
!TryGetActiveItem((player, hands), out var throwEnt) ||
|
|
|
|
|
!_actionBlockerSystem.CanThrow(player, throwEnt.Value))
|
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;
|
|
|
|
|
|
2025-06-26 19:50:49 -04:00
|
|
|
if (TryComp(throwEnt, out StackComponent? stack) && stack.Count > 1 && stack.ThrowIndividually)
|
2018-10-30 01:13:10 -07:00
|
|
|
{
|
2025-06-26 19:50:49 -04:00
|
|
|
var splitStack = _stackSystem.Split(throwEnt.Value, 1, Comp<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.
|
2025-06-25 09:13:03 -04:00
|
|
|
var ev = new BeforeThrowEvent(throwEnt.Value, 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
|
2025-06-25 09:13:03 -04:00
|
|
|
if (IsHolding((player, hands), throwEnt, out _) && !TryDrop(player, throwEnt.Value))
|
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
|
|
|
|
2025-03-22 06:00:21 +01:00
|
|
|
private void OnDropHandItems(Entity<HandsComponent> entity, ref DropHandItemsEvent args)
|
|
|
|
|
{
|
2025-03-31 18:00:04 -04:00
|
|
|
// If the holder doesn't have a physics component, they ain't moving
|
|
|
|
|
var holderVelocity = _physicsQuery.TryComp(entity, out var physics) ? physics.LinearVelocity : Vector2.Zero;
|
|
|
|
|
var spreadMaxAngle = Angle.FromDegrees(DropHeldItemsSpread);
|
2025-03-22 06:00:21 +01:00
|
|
|
|
2025-06-25 09:13:03 -04:00
|
|
|
foreach (var hand in entity.Comp.Hands.Keys)
|
2025-03-22 06:00:21 +01:00
|
|
|
{
|
2025-06-25 09:13:03 -04:00
|
|
|
if (!TryGetHeldItem(entity.AsNullable(), hand, out var heldEntity))
|
2025-03-22 06:00:21 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var throwAttempt = new FellDownThrowAttemptEvent(entity);
|
2025-06-25 09:13:03 -04:00
|
|
|
RaiseLocalEvent(heldEntity.Value, ref throwAttempt);
|
2025-03-22 06:00:21 +01:00
|
|
|
|
|
|
|
|
if (throwAttempt.Cancelled)
|
|
|
|
|
continue;
|
|
|
|
|
|
2025-06-25 09:13:03 -04:00
|
|
|
if (!TryDrop(entity.AsNullable(), hand, checkActionBlocker: false))
|
2025-03-22 06:00:21 +01:00
|
|
|
continue;
|
|
|
|
|
|
2025-03-31 18:00:04 -04:00
|
|
|
// Rotate the item's throw vector a bit for each item
|
|
|
|
|
var angleOffset = _random.NextAngle(-spreadMaxAngle, spreadMaxAngle);
|
|
|
|
|
// Rotate the holder's velocity vector by the angle offset to get the item's velocity vector
|
|
|
|
|
var itemVelocity = angleOffset.RotateVec(holderVelocity);
|
|
|
|
|
// Decrease the distance of the throw by a random amount
|
|
|
|
|
itemVelocity *= _random.NextFloat(1f);
|
|
|
|
|
// Heavier objects don't get thrown as far
|
|
|
|
|
// If the item doesn't have a physics component, it isn't going to get thrown anyway, but we'll assume infinite mass
|
2025-06-25 09:13:03 -04:00
|
|
|
itemVelocity *= _physicsQuery.TryComp(heldEntity, out var heldPhysics) ? heldPhysics.InvMass : 0;
|
2025-03-31 18:00:04 -04:00
|
|
|
// Throw at half the holder's intentional throw speed and
|
|
|
|
|
// vary the speed a little to make it look more interesting
|
|
|
|
|
var throwSpeed = entity.Comp.BaseThrowspeed * _random.NextFloat(0.45f, 0.55f);
|
|
|
|
|
|
2025-06-25 09:13:03 -04:00
|
|
|
_throwingSystem.TryThrow(heldEntity.Value,
|
2025-03-31 18:00:04 -04:00
|
|
|
itemVelocity,
|
|
|
|
|
throwSpeed,
|
|
|
|
|
entity,
|
|
|
|
|
pushbackRatio: 0,
|
|
|
|
|
compensateFriction: false
|
|
|
|
|
);
|
2025-03-22 06:00:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 17:53:08 +13:00
|
|
|
#endregion
|
2018-08-18 15:28:02 -07:00
|
|
|
}
|
|
|
|
|
}
|