2023-09-11 21:20:46 +10:00
|
|
|
using System.Linq;
|
2023-07-08 14:08:32 +10:00
|
|
|
using System.Numerics;
|
2022-02-26 18:24:08 +13:00
|
|
|
using Content.Server.Popups;
|
2022-05-12 22:30:30 -07:00
|
|
|
using Content.Server.Pulling;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Stack;
|
2022-04-28 06:11:15 -06:00
|
|
|
using Content.Server.Storage.EntitySystems;
|
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;
|
2021-06-21 02:21:20 -07:00
|
|
|
using Content.Shared.Hands;
|
|
|
|
|
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;
|
2021-12-30 22:56:10 +01:00
|
|
|
using Content.Shared.Inventory;
|
2021-07-31 03:14:00 +02:00
|
|
|
using Content.Shared.Physics.Pull;
|
2022-05-12 22:30:30 -07:00
|
|
|
using Content.Shared.Pulling.Components;
|
2022-12-24 23:28:21 -05:00
|
|
|
using Content.Shared.Stacks;
|
2023-09-11 21:20:46 +10:00
|
|
|
using Content.Shared.Storage;
|
2022-05-12 22:30:30 -07:00
|
|
|
using Content.Shared.Throwing;
|
2021-12-05 21:02:04 +01:00
|
|
|
using Robust.Shared.Containers;
|
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;
|
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
|
|
|
{
|
2021-12-30 22:56:10 +01:00
|
|
|
[Dependency] private readonly InventorySystem _inventorySystem = default!;
|
2021-07-26 12:58:17 +02:00
|
|
|
[Dependency] private readonly StackSystem _stackSystem = default!;
|
2021-09-17 07:16:11 -07:00
|
|
|
[Dependency] private readonly HandVirtualItemSystem _virtualItemSystem = default!;
|
2021-11-29 02:34:44 +13:00
|
|
|
[Dependency] private readonly ActionBlockerSystem _actionBlockerSystem = default!;
|
2022-01-10 17:22:56 +13:00
|
|
|
[Dependency] private readonly SharedHandVirtualItemSystem _virtualSystem = default!;
|
2022-02-26 18:24:08 +13:00
|
|
|
[Dependency] private readonly PopupSystem _popupSystem = default!;
|
2022-03-17 20:13:31 +13:00
|
|
|
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
|
|
|
|
|
[Dependency] private readonly PullingSystem _pullingSystem = default!;
|
2022-03-24 02:33:01 +13:00
|
|
|
[Dependency] private readonly ThrowingSystem _throwingSystem = default!;
|
2022-04-28 06:11:15 -06:00
|
|
|
[Dependency] private readonly StorageSystem _storageSystem = default!;
|
2022-03-24 02:33:01 +13:00
|
|
|
|
2018-08-18 15:28:02 -07:00
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
2022-02-26 18:24:08 +13:00
|
|
|
SubscribeLocalEvent<HandsComponent, DisarmedEvent>(OnDisarmed, before: new[] { typeof(StunSystem) });
|
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);
|
|
|
|
|
|
2020-05-31 14:32:05 -07:00
|
|
|
CommandBinds.Builder
|
|
|
|
|
.Bind(ContentKeyFunctions.ThrowItemInHand, new PointerInputCmdHandler(HandleThrowItem))
|
|
|
|
|
.Bind(ContentKeyFunctions.SmartEquipBackpack, InputCmdHandler.FromDelegate(HandleSmartEquipBackpack))
|
2020-07-27 00:54:32 +02:00
|
|
|
.Bind(ContentKeyFunctions.SmartEquipBelt, InputCmdHandler.FromDelegate(HandleSmartEquipBelt))
|
|
|
|
|
.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
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
if (TryComp(uid, out SharedPullerComponent? puller) && puller.Pulling is EntityUid pulled && TryComp(pulled, out SharedPullableComponent? pullable))
|
|
|
|
|
_pullingSystem.TryStopPull(pullable);
|
|
|
|
|
|
2022-04-06 17:41:12 +12:00
|
|
|
if (!_handsSystem.TryDrop(uid, component.ActiveHand!, null, checkActionBlocker: false))
|
2022-02-26 18:24:08 +13:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
args.Handled = true; // no shove/stun.
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-23 21:38:52 +02:00
|
|
|
protected override void HandleEntityRemoved(EntityUid uid, HandsComponent hands, EntRemovedFromContainerMessage args)
|
2022-01-10 17:22:56 +13:00
|
|
|
{
|
2023-04-23 21:38:52 +02:00
|
|
|
base.HandleEntityRemoved(uid, hands, args);
|
|
|
|
|
|
2022-01-10 17:22:56 +13:00
|
|
|
if (!Deleted(args.Entity) && TryComp(args.Entity, out HandVirtualItemComponent? @virtual))
|
2023-10-19 12:34:31 -07:00
|
|
|
_virtualSystem.Delete((args.Entity, @virtual), uid);
|
2022-01-10 17:22:56 +13:00
|
|
|
}
|
2022-11-13 22:34:26 +01:00
|
|
|
|
|
|
|
|
private void HandleBodyPartAdded(EntityUid uid, HandsComponent component, ref BodyPartAddedEvent args)
|
|
|
|
|
{
|
|
|
|
|
if (args.Part.PartType != BodyPartType.Hand)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// If this annoys you, which it should.
|
|
|
|
|
// Ping Smugleaf.
|
|
|
|
|
var location = args.Part.Symmetry switch
|
|
|
|
|
{
|
|
|
|
|
BodyPartSymmetry.None => HandLocation.Middle,
|
|
|
|
|
BodyPartSymmetry.Left => HandLocation.Left,
|
|
|
|
|
BodyPartSymmetry.Right => HandLocation.Right,
|
|
|
|
|
_ => throw new ArgumentOutOfRangeException(nameof(args.Part.Symmetry))
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
AddHand(uid, args.Slot, location);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HandleBodyPartRemoved(EntityUid uid, HandsComponent component, ref BodyPartRemovedEvent args)
|
|
|
|
|
{
|
|
|
|
|
if (args.Part.PartType != BodyPartType.Hand)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
RemoveHand(uid, args.Slot);
|
|
|
|
|
}
|
2022-01-05 17:53:08 +13:00
|
|
|
|
|
|
|
|
#region pulling
|
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
|
|
|
{
|
2021-12-13 18:46:09 +13:00
|
|
|
if (args.Puller.Owner != uid)
|
|
|
|
|
return;
|
|
|
|
|
|
2022-06-30 14:40:32 -04:00
|
|
|
if (TryComp<SharedPullerComponent>(args.Puller.Owner, out var pullerComp) && !pullerComp.NeedsHands)
|
2021-12-13 18:46:09 +13:00
|
|
|
return;
|
|
|
|
|
|
2021-12-03 15:53:09 +01:00
|
|
|
if (!_virtualItemSystem.TrySpawnVirtualItemInHand(args.Pulled.Owner, 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)
|
|
|
|
|
{
|
2021-12-13 18:46:09 +13:00
|
|
|
if (args.Puller.Owner != uid)
|
|
|
|
|
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
|
2022-01-05 17:53:08 +13:00
|
|
|
|| !TryComp(hand.HeldEntity, out HandVirtualItemComponent? virtualItem)
|
2021-12-03 15:53:09 +01:00
|
|
|
|| virtualItem.BlockingEntity != args.Pulled.Owner)
|
2021-07-31 03:14:00 +02:00
|
|
|
continue;
|
|
|
|
|
|
2022-01-05 17:53:08 +13:00
|
|
|
QueueDel(hand.HeldEntity.Value);
|
2021-07-31 03:14:00 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
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-10-28 09:59:53 +11:00
|
|
|
private bool HandleThrowItem(ICommonSession? playerSession, EntityCoordinates coordinates, EntityUid entity)
|
2018-08-22 01:19:47 -07:00
|
|
|
{
|
2023-10-28 09:59:53 +11:00
|
|
|
if (playerSession == null)
|
2019-09-17 16:08:45 -07:00
|
|
|
return false;
|
2018-08-22 01:19:47 -07:00
|
|
|
|
2021-12-06 00:52:58 +01:00
|
|
|
if (playerSession.AttachedEntity is not {Valid: true} player ||
|
2022-01-05 17:53:08 +13:00
|
|
|
!Exists(player) ||
|
2023-10-11 02:18:49 -07:00
|
|
|
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
|
|
|
|
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
|
|
|
|
2023-09-11 09:42:41 +10:00
|
|
|
var direction = coordinates.ToMapPos(EntityManager) - Transform(player).WorldPosition;
|
2021-06-21 02:21:20 -07:00
|
|
|
if (direction == Vector2.Zero)
|
|
|
|
|
return true;
|
2021-03-08 04:09:59 +11:00
|
|
|
|
2023-07-08 14:08:32 +10:00
|
|
|
direction = direction.Normalized() * Math.Min(direction.Length(), hands.ThrowRange);
|
2021-07-25 16:58:02 +10:00
|
|
|
|
|
|
|
|
var throwStrength = hands.ThrowForceMultiplier;
|
2022-07-27 19:28:23 -04:00
|
|
|
|
|
|
|
|
// Let other systems change the thrown entity (useful for virtual items)
|
|
|
|
|
// or the throw strength.
|
|
|
|
|
var ev = new BeforeThrowEvent(throwEnt, direction, throwStrength, player);
|
|
|
|
|
RaiseLocalEvent(player, ev, false);
|
|
|
|
|
|
|
|
|
|
if (ev.Handled)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
// This can grief the above event so we raise it afterwards
|
|
|
|
|
if (!TryDrop(player, throwEnt, handsComp: hands))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
_throwingSystem.TryThrow(ev.ItemUid, ev.Direction, ev.ThrowStrength, ev.PlayerUid);
|
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
|
|
|
}
|
2021-03-16 15:50:20 +01:00
|
|
|
private void HandleSmartEquipBackpack(ICommonSession? session)
|
2020-05-05 00:39:15 +02:00
|
|
|
{
|
2021-12-30 22:56:10 +01:00
|
|
|
HandleSmartEquip(session, "back");
|
2020-05-05 00:39:15 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-16 15:50:20 +01:00
|
|
|
private void HandleSmartEquipBelt(ICommonSession? session)
|
2020-05-05 00:39:15 +02:00
|
|
|
{
|
2021-12-30 22:56:10 +01:00
|
|
|
HandleSmartEquip(session, "belt");
|
2020-05-05 00:39:15 +02:00
|
|
|
}
|
|
|
|
|
|
2022-09-03 11:38:46 +12:00
|
|
|
// why tf is this even in hands system.
|
|
|
|
|
// TODO: move to storage or inventory
|
2021-12-30 22:56:10 +01:00
|
|
|
private void HandleSmartEquip(ICommonSession? session, string equipmentSlot)
|
2020-05-05 00:39:15 +02:00
|
|
|
{
|
2023-10-28 09:59:53 +11:00
|
|
|
if (session is not { } playerSession)
|
2021-06-21 02:21:20 -07:00
|
|
|
return;
|
|
|
|
|
|
2022-01-05 17:53:08 +13:00
|
|
|
if (playerSession.AttachedEntity is not {Valid: true} plyEnt || !Exists(plyEnt))
|
2020-05-05 00:39:15 +02:00
|
|
|
return;
|
|
|
|
|
|
2022-09-03 11:38:46 +12:00
|
|
|
if (!_actionBlockerSystem.CanInteract(plyEnt, null))
|
2022-02-19 14:16:15 -05:00
|
|
|
return;
|
|
|
|
|
|
2023-04-07 11:21:12 -07:00
|
|
|
if (!TryComp<HandsComponent>(plyEnt, out var hands) || hands.ActiveHand == null)
|
2020-05-05 00:39:15 +02:00
|
|
|
return;
|
|
|
|
|
|
2021-12-30 22:56:10 +01:00
|
|
|
if (!_inventorySystem.TryGetSlotEntity(plyEnt, equipmentSlot, out var slotEntity) ||
|
2023-09-11 21:20:46 +10:00
|
|
|
!TryComp(slotEntity, out StorageComponent? storageComponent))
|
2020-05-05 00:39:15 +02:00
|
|
|
{
|
2023-03-27 04:01:09 +05:00
|
|
|
if (_inventorySystem.HasSlot(plyEnt, equipmentSlot))
|
|
|
|
|
{
|
|
|
|
|
if (hands.ActiveHand.HeldEntity == null && slotEntity != null)
|
|
|
|
|
{
|
|
|
|
|
_inventorySystem.TryUnequip(plyEnt, equipmentSlot);
|
|
|
|
|
PickupOrDrop(plyEnt, slotEntity.Value);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (hands.ActiveHand.HeldEntity == null)
|
|
|
|
|
return;
|
|
|
|
|
if (!_inventorySystem.CanEquip(plyEnt, hands.ActiveHand.HeldEntity.Value, equipmentSlot, out var reason))
|
|
|
|
|
{
|
|
|
|
|
_popupSystem.PopupEntity(Loc.GetString(reason), plyEnt, session);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (slotEntity == null)
|
|
|
|
|
{
|
|
|
|
|
_inventorySystem.TryEquip(plyEnt, hands.ActiveHand.HeldEntity.Value, equipmentSlot);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_inventorySystem.TryUnequip(plyEnt, equipmentSlot);
|
|
|
|
|
_inventorySystem.TryEquip(plyEnt, hands.ActiveHand.HeldEntity.Value, equipmentSlot);
|
|
|
|
|
PickupOrDrop(plyEnt, slotEntity.Value);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-12-19 10:41:47 +13:00
|
|
|
_popupSystem.PopupEntity(Loc.GetString("hands-system-missing-equipment-slot", ("slotName", equipmentSlot)), plyEnt, session);
|
2020-05-05 00:39:15 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-03 11:38:46 +12:00
|
|
|
if (hands.ActiveHand.HeldEntity != null)
|
2020-05-05 00:39:15 +02:00
|
|
|
{
|
2022-04-28 06:11:15 -06:00
|
|
|
_storageSystem.PlayerInsertHeldEntity(slotEntity.Value, plyEnt, storageComponent);
|
2020-05-05 00:39:15 +02:00
|
|
|
}
|
2023-09-11 21:20:46 +10:00
|
|
|
else
|
2020-05-05 00:39:15 +02:00
|
|
|
{
|
2023-09-11 21:20:46 +10:00
|
|
|
if (!storageComponent.Container.ContainedEntities.Any())
|
2020-05-05 00:39:15 +02:00
|
|
|
{
|
2022-12-19 10:41:47 +13:00
|
|
|
_popupSystem.PopupEntity(Loc.GetString("hands-system-empty-equipment-slot", ("slotName", equipmentSlot)), plyEnt, session);
|
2020-05-05 00:39:15 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-09-11 21:20:46 +10:00
|
|
|
var lastStoredEntity = storageComponent.Container.ContainedEntities[^1];
|
|
|
|
|
|
|
|
|
|
if (storageComponent.Container.Remove(lastStoredEntity))
|
2021-06-21 02:21:20 -07:00
|
|
|
{
|
2022-03-17 20:13:31 +13:00
|
|
|
PickupOrDrop(plyEnt, lastStoredEntity, animateUser: true, handsComp: hands);
|
2021-06-21 02:21:20 -07:00
|
|
|
}
|
2020-05-05 00:39:15 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-05 17:53:08 +13:00
|
|
|
#endregion
|
2018-08-18 15:28:02 -07:00
|
|
|
}
|
|
|
|
|
}
|