Escape inventory refactor (#10203)

This commit is contained in:
Rane
2022-08-10 04:37:20 -04:00
committed by GitHub
parent 49986231ca
commit e81510bf3e
3 changed files with 48 additions and 24 deletions

View File

@@ -1,13 +1,14 @@
using Content.Shared.Movement;
using Content.Server.DoAfter;
using Content.Server.Contests;
using Robust.Shared.Containers;
using Content.Server.Popups;
using Robust.Shared.Player;
using Content.Shared.Storage;
using Content.Shared.Inventory;
using Content.Shared.Hands.Components;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.ActionBlocker;
using Content.Shared.Movement.Events;
using Content.Shared.Interaction.Events;
namespace Content.Server.Resist;
@@ -17,6 +18,13 @@ public sealed class EscapeInventorySystem : EntitySystem
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly SharedContainerSystem _containerSystem = default!;
[Dependency] private readonly ActionBlockerSystem _actionBlockerSystem = default!;
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
[Dependency] private readonly ContestsSystem _contests = default!;
/// <summary>
/// You can't escape the hands of an entity this many times more massive than you.
/// </summary>
public const float MaximumMassDisadvantage = 6f;
public override void Initialize()
{
@@ -26,20 +34,38 @@ public sealed class EscapeInventorySystem : EntitySystem
SubscribeLocalEvent<CanEscapeInventoryComponent, UpdateCanMoveEvent>(OnMoveAttempt);
SubscribeLocalEvent<CanEscapeInventoryComponent, EscapeDoAfterComplete>(OnEscapeComplete);
SubscribeLocalEvent<CanEscapeInventoryComponent, EscapeDoAfterCancel>(OnEscapeFail);
SubscribeLocalEvent<CanEscapeInventoryComponent, DroppedEvent>(OnDropped);
}
private void OnRelayMovement(EntityUid uid, CanEscapeInventoryComponent component, ref MoveInputEvent args)
{
//Prevents the user from creating multiple DoAfters if they're already resisting.
if (component.IsResisting == true)
if (component.CancelToken != null)
return;
if (_containerSystem.TryGetContainingContainer(uid, out var container)
&& (HasComp<SharedStorageComponent>(container.Owner) || HasComp<InventoryComponent>(container.Owner) || HasComp<SharedHandsComponent>(container.Owner)))
if (!_containerSystem.TryGetContainingContainer(uid, out var container) || !_actionBlockerSystem.CanInteract(uid, container.Owner))
return;
// Contested
if (_handsSystem.IsHolding(container.Owner, uid, out var inHand))
{
if (_actionBlockerSystem.CanInteract(uid, container.Owner))
AttemptEscape(uid, container.Owner, component);
var contestResults = _contests.MassContest(uid, container.Owner);
// Inverse if we aren't going to divide by 0, otherwise just use a default multiplier of 1.
if (contestResults != 0)
contestResults = 1 / contestResults;
else
contestResults = 1;
if (contestResults >= MaximumMassDisadvantage)
return;
AttemptEscape(uid, container.Owner, component, contestResults);
return;
}
// Uncontested
if (HasComp<SharedStorageComponent>(container.Owner) || HasComp<InventoryComponent>(container.Owner))
AttemptEscape(uid, container.Owner, component);
}
private void OnMoveAttempt(EntityUid uid, CanEscapeInventoryComponent component, UpdateCanMoveEvent args)
@@ -48,10 +74,10 @@ public sealed class EscapeInventorySystem : EntitySystem
args.Cancel();
}
private void AttemptEscape(EntityUid user, EntityUid container, CanEscapeInventoryComponent component)
private void AttemptEscape(EntityUid user, EntityUid container, CanEscapeInventoryComponent component, float multiplier = 1f)
{
component.CancelToken = new();
var doAfterEventArgs = new DoAfterEventArgs(user, component.ResistTime, component.CancelToken.Token, container)
var doAfterEventArgs = new DoAfterEventArgs(user, component.BaseResistTime * multiplier, component.CancelToken.Token, container)
{
BreakOnTargetMove = false,
BreakOnUserMove = false,
@@ -62,7 +88,6 @@ public sealed class EscapeInventorySystem : EntitySystem
UserCancelledEvent = new EscapeDoAfterCancel(),
};
component.IsResisting = true;
_popupSystem.PopupEntity(Loc.GetString("escape-inventory-component-start-resisting"), user, Filter.Entities(user));
_popupSystem.PopupEntity(Loc.GetString("escape-inventory-component-start-resisting-target"), container, Filter.Entities(container));
_doAfterSystem.DoAfter(doAfterEventArgs);
@@ -72,12 +97,17 @@ public sealed class EscapeInventorySystem : EntitySystem
{
//Drops the mob on the tile below the container
Transform(uid).AttachParentToContainerOrGrid(EntityManager);
component.IsResisting = false;
component.CancelToken = null;
}
private void OnEscapeFail(EntityUid uid, CanEscapeInventoryComponent component, EscapeDoAfterCancel ev)
{
component.IsResisting = false;
component.CancelToken = null;
}
private void OnDropped(EntityUid uid, CanEscapeInventoryComponent component, DroppedEvent args)
{
component.CancelToken?.Cancel();
}
private sealed class EscapeDoAfterComplete : EntityEventArgs { }