Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Carrying/escape-inventory tweaks #834

Merged
108 changes: 99 additions & 9 deletions Content.Server/Nyanotrasen/Carrying/CarryingSystem.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System.Numerics;
using System.Threading;
using Content.Server.DoAfter;
using Content.Server.Body.Systems;
using Content.Server.Hands.Systems;
using Content.Server.Resist;
using Content.Server.Popups;
using Content.Server.Contests;
using Content.Server.Item.PseudoItem;
using Content.Server.Storage.EntitySystems;
using Content.Shared.Climbing; // Shared instead of Server
using Content.Shared.Mobs;
using Content.Shared.DoAfter;
Expand All @@ -22,9 +25,14 @@
using Content.Shared.Pulling.Components;
using Content.Shared.Standing;
using Content.Shared.ActionBlocker;
using Content.Shared.Item;
using Content.Shared.Item.PseudoItem;
using Content.Shared.Mind.Components;
using Content.Shared.Throwing;
using Content.Shared.Physics.Pull;
using Content.Shared.Mobs.Systems;
using Content.Shared.Popups;
using Content.Shared.Storage;
using Robust.Shared.Map.Components;

namespace Content.Server.Carrying
Expand All @@ -42,7 +50,7 @@ public sealed class CarryingSystem : EntitySystem
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly ContestsSystem _contests = default!;
[Dependency] private readonly MovementSpeedModifierSystem _movementSpeed = default!;
[Dependency] private readonly RespiratorSystem _respirator = default!;
[Dependency] private readonly PseudoItemSystem _pseudoItem = default!;

public override void Initialize()
{
Expand All @@ -61,6 +69,7 @@ public override void Initialize()
SubscribeLocalEvent<BeingCarriedComponent, StartClimbEvent>(OnStartClimb);
SubscribeLocalEvent<BeingCarriedComponent, BuckleChangeEvent>(OnBuckleChange);
SubscribeLocalEvent<CarriableComponent, CarryDoAfterEvent>(OnDoAfter);
SubscribeLocalEvent<CarryingComponent, GetVerbsEvent<InnateVerb>>(AddInsertCarriedVerb); // Frontier
}


Expand Down Expand Up @@ -124,7 +133,12 @@ private void OnThrow(EntityUid uid, CarryingComponent component, BeforeThrowEven

private void OnParentChanged(EntityUid uid, CarryingComponent component, ref EntParentChangedMessage args)
{
if (Transform(uid).MapID != args.OldMapId)
var xform = Transform(uid);
if (xform.MapID != args.OldMapId)
return;

// Do not drop the carried entity if the new parent is a grid
if (xform.ParentUid == xform.GridUid)
return;

DropCarried(uid, component.Carried);
Expand Down Expand Up @@ -157,6 +171,9 @@ private void OnMoveInput(EntityUid uid, BeingCarriedComponent component, ref Mov
if (!TryComp<CanEscapeInventoryComponent>(uid, out var escape))
return;

if (args.OldMovement == MoveButtons.None || args.OldMovement == MoveButtons.Walk)
return; // Don't try to escape if not moving *cries*

if (_actionBlockerSystem.CanInteract(uid, component.Carrier))
{
_escapeInventorySystem.AttemptEscape(uid, component.Carrier, escape, _contests.MassContest(uid, component.Carrier));
Expand Down Expand Up @@ -206,15 +223,10 @@ private void OnDoAfter(EntityUid uid, CarriableComponent component, CarryDoAfter
Carry(args.Args.User, uid);
args.Handled = true;
}

private void StartCarryDoAfter(EntityUid carrier, EntityUid carried, CarriableComponent component)
{
TimeSpan length = TimeSpan.FromSeconds(3);

var mod = _contests.MassContest(carrier, carried);

if (mod != 0)
length /= mod;

var length = GetPickupDuration(carrier, carried); // Frontier: instead of in-line calculation, use a separate function
if (length >= TimeSpan.FromSeconds(9))
{
_popupSystem.PopupEntity(Loc.GetString("carry-too-heavy"), carried, carrier, Shared.Popups.PopupType.SmallCaution);
Expand All @@ -234,13 +246,19 @@ private void StartCarryDoAfter(EntityUid carrier, EntityUid carried, CarriableCo
NeedHand = true
};
_doAfterSystem.TryStartDoAfter(args);

_popupSystem.PopupEntity(Loc.GetString("carry-started", ("carrier", carrier)), carried, carried);
}

private void Carry(EntityUid carrier, EntityUid carried)
{
if (TryComp<SharedPullableComponent>(carried, out var pullable))
_pullingSystem.TryStopPull(pullable);

// Don't allow people to stack upon each other. They're too weak for that!
if (TryComp<CarryingComponent>(carried, out var carryComp))
DropCarried(carried, carryComp.Carried);

Transform(carrier).AttachToGridOrMap();
Transform(carried).AttachToGridOrMap();
Transform(carried).Coordinates = Transform(carrier).Coordinates;
Expand Down Expand Up @@ -310,5 +328,77 @@ public bool CanCarry(EntityUid carrier, EntityUid carried, CarriableComponent? c

return true;
}

public override void Update(float frameTime)
{
var query = EntityQueryEnumerator<BeingCarriedComponent>();
while (query.MoveNext(out var carried, out var comp))
{
var carrier = comp.Carrier;
if (carrier is not { Valid: true } || carried is not { Valid: true })
continue;

// Make sure the carried entity is always centered relative to the carrier, as gravity pulls can offset it otherwise
var xform = Transform(carried);
if (!xform.LocalPosition.EqualsApprox(Vector2.Zero))
{
xform.LocalPosition = Vector2.Zero;
}
}
query.Dispose();
}

public TimeSpan GetPickupDuration(EntityUid carrier, EntityUid carried)
{
TimeSpan length = TimeSpan.FromSeconds(3);

var mod = _contests.MassContest(carrier, carried);
if (mod != 0)
length /= mod;

return length;
}

public bool TryCarry(EntityUid carrier, EntityUid toCarry, CarriableComponent? carriedComp = null)
{
if (!Resolve(toCarry, ref carriedComp, false))
return false;

if (!CanCarry(carrier, toCarry, carriedComp))
return false;

// The second one means that carrier *is also* inside a bag.
if (HasComp<BeingCarriedComponent>(carrier) || HasComp<ItemComponent>(carrier))
return false;

if (GetPickupDuration(carrier, toCarry) > TimeSpan.FromSeconds(9))
return false;

Carry(carrier, toCarry);

return true;
}

private void AddInsertCarriedVerb(EntityUid uid, CarryingComponent component, GetVerbsEvent<InnateVerb> args)
{
var toInsert = args.Using;
if (toInsert is not { Valid: true } || !args.CanAccess || !TryComp<PseudoItemComponent>(toInsert, out var pseudoItem))
return;

if (!HasComp<StorageComponent>(args.Target))
return; // Can't check if the person would actually fit here

InnateVerb verb = new()
{
Act = () =>
{
DropCarried(uid, toInsert.Value);
_pseudoItem.TryInsert(args.Target, toInsert.Value, args.User, pseudoItem);
},
Text = Loc.GetString("action-name-insert-other", ("target", toInsert)),
Priority = 2
};
args.Verbs.Add(verb);
}
}
}
16 changes: 16 additions & 0 deletions Content.Server/Nyanotrasen/Item/PseudoItem/PseudoItemSystem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Content.Server.Actions;
using Content.Server.Bed.Sleep;
using Content.Server.Carrying;
using Content.Server.DoAfter;
using Content.Server.Popups;
using Content.Server.Storage.EntitySystems;
Expand All @@ -22,6 +23,7 @@ public sealed class PseudoItemSystem : EntitySystem
[Dependency] private readonly ItemSystem _itemSystem = default!;
[Dependency] private readonly DoAfterSystem _doAfter = default!;
[Dependency] private readonly TagSystem _tagSystem = default!;
[Dependency] private readonly CarryingSystem _carrying = default!; // Frontier
[Dependency] private readonly ActionsSystem _actions = default!; // Frontier
[Dependency] private readonly PopupSystem _popup = default!; // Frontier

Expand Down Expand Up @@ -115,6 +117,20 @@ private void OnGettingPickedUpAttempt(EntityUid uid, PseudoItemComponent compone
if (args.User == args.Item)
return;

// Frontier: prevent people from pushing each other from a bag
if (HasComp<ItemComponent>(args.User))
{
args.Cancel();
return;
}

// Frontier: try to carry the person when taking them out of a bag.
if (_carrying.TryCarry(args.User, uid))
{
args.Cancel();
return;
}

Transform(uid).AttachToGridOrMap();
args.Cancel();
}
Expand Down
4 changes: 4 additions & 0 deletions Content.Server/Resist/CanEscapeInventoryComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ public sealed partial class CanEscapeInventoryComponent : Component

[DataField("doAfter")]
public DoAfterId? DoAfter;

// Frontier
[DataField]
public EntityUid? EscapeCancelAction;
}
42 changes: 41 additions & 1 deletion Content.Server/Resist/EscapeInventorySystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
using Robust.Shared.Containers;
using Content.Server.Storage.Components;
using Content.Server.Carrying;
using Content.Shared.Actions;
using Content.Shared.Movement.Systems;
using Robust.Shared.Prototypes;

namespace Content.Server.Resist;

Expand All @@ -28,6 +31,12 @@ public sealed class EscapeInventorySystem : EntitySystem
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
[Dependency] private readonly ContestsSystem _contests = default!;
[Dependency] private readonly CarryingSystem _carryingSystem = default!; // Carrying system from Nyanotrasen.
[Dependency] private readonly SharedActionsSystem _actions = default!;
[Dependency] private readonly EntityManager _entityManager = default!;

// Frontier - cancel inventory escape
[ValidatePrototypeId<EntityPrototype>]
private readonly string _escapeCancelAction = "ActionCancelEscape";

/// <summary>
/// You can't escape the hands of an entity this many times more massive than you.
Expand All @@ -41,13 +50,17 @@ public override void Initialize()
SubscribeLocalEvent<CanEscapeInventoryComponent, MoveInputEvent>(OnRelayMovement);
SubscribeLocalEvent<CanEscapeInventoryComponent, EscapeInventoryEvent>(OnEscape);
SubscribeLocalEvent<CanEscapeInventoryComponent, DroppedEvent>(OnDropped);
SubscribeLocalEvent<CanEscapeInventoryComponent, EscapeInventoryCancelActionEvent>(OnCancelEscape); // Frontier
}

private void OnRelayMovement(EntityUid uid, CanEscapeInventoryComponent component, ref MoveInputEvent args)
{
if (!_containerSystem.TryGetContainingContainer(uid, out var container) || !_actionBlockerSystem.CanInteract(uid, container.Owner))
return;

if (args.OldMovement == MoveButtons.None || args.OldMovement == MoveButtons.Walk)
return; // This event gets fired when the user holds down shift, which makes no sense

// Contested
if (_handsSystem.IsHolding(container.Owner, uid, out var inHand))
{
Expand Down Expand Up @@ -87,9 +100,13 @@ private void OnRelayMovement(EntityUid uid, CanEscapeInventoryComponent componen
if (!_doAfterSystem.TryStartDoAfter(doAfterEventArgs, out component.DoAfter))
return;

Dirty(user, component);
//Dirty(user, component);
_popupSystem.PopupEntity(Loc.GetString("escape-inventory-component-start-resisting"), user, user);
_popupSystem.PopupEntity(Loc.GetString("escape-inventory-component-start-resisting-target"), container, container);

// Frontier - escape cancel action
if (component.EscapeCancelAction is not { Valid: true })
_actions.AddAction(user, ref component.EscapeCancelAction, _escapeCancelAction);
}

private void OnEscape(EntityUid uid, CanEscapeInventoryComponent component, EscapeInventoryEvent args)
Expand All @@ -100,6 +117,8 @@ private void OnEscape(EntityUid uid, CanEscapeInventoryComponent component, Esca
if (args.Handled || args.Cancelled)
return;

RemoveCancelAction(uid, component); // Frontier

if (TryComp<BeingCarriedComponent>(uid, out var carried)) // Start of carrying system of nyanotrasen.
{
_carryingSystem.DropCarried(carried.Carrier, uid);
Expand All @@ -114,5 +133,26 @@ private void OnDropped(EntityUid uid, CanEscapeInventoryComponent component, Dro
{
if (component.DoAfter != null)
_doAfterSystem.Cancel(component.DoAfter);

RemoveCancelAction(uid, component); // Frontier
}

// Frontier
private void RemoveCancelAction(EntityUid uid, CanEscapeInventoryComponent component)
{
if (component.EscapeCancelAction is not { Valid: true })
return;

_actions.RemoveAction(uid, component.EscapeCancelAction);
component.EscapeCancelAction = null;
}

// Frontier
private void OnCancelEscape(EntityUid uid, CanEscapeInventoryComponent component, EscapeInventoryCancelActionEvent args)
{
if (component.DoAfter != null)
_doAfterSystem.Cancel(component.DoAfter);

RemoveCancelAction(uid, component);
}
}
5 changes: 5 additions & 0 deletions Content.Shared/Resist/EscapeInventoryCancelEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Content.Shared.Actions;

public sealed partial class EscapeInventoryCancelActionEvent : InstantActionEvent
{
}
1 change: 1 addition & 0 deletions Resources/Locale/en-US/nyanotrasen/carrying/carry.ftl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
carry-verb = Carry

carry-too-heavy = You're not strong enough.
carry-started = {THE($carrier)} is trying to pick you up!
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- type: entity
id: ActionCancelEscape
name: Stop escaping
description: Calm down and sit peacefuly in your carrier's inventory
noSpawn: true
components:
- type: InstantAction
icon: _NF/Actions/escapeinventory.rsi/cancel-escape.png
event: !type:EscapeInventoryCancelActionEvent
useDelay: 2
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions Resources/Textures/_NF/Actions/escapeinventory.rsi/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": 1,
"license": "CC-BY-SA-3.0",
"copyright": "Duffelbag icon taken from tgstation at commit https://github.com/tgstation/tgstation/commit/547852588166c8e091b441e4e67169e156bb09c1 | Modified by Mnemotechnician (github)",
"size": {
"x": 32,
"y": 32
},
"states": [
{
"name": "cancel-escape"
}
]
}
Loading