Escape inventory refactor (#10203)
This commit is contained in:
@@ -23,7 +23,7 @@ namespace Content.Server.Contests
|
||||
/// </summary>
|
||||
public float MassContest(EntityUid roller, EntityUid target, PhysicsComponent? rollerPhysics = null, PhysicsComponent? targetPhysics = null)
|
||||
{
|
||||
if (!Resolve(roller, ref rollerPhysics) || !Resolve(target, ref targetPhysics))
|
||||
if (!Resolve(roller, ref rollerPhysics, false) || !Resolve(target, ref targetPhysics, false))
|
||||
return 1f;
|
||||
|
||||
if (rollerPhysics == null || targetPhysics == null)
|
||||
@@ -43,7 +43,7 @@ namespace Content.Server.Contests
|
||||
/// <summary>
|
||||
public float DamageContest(EntityUid roller, EntityUid target, DamageableComponent? rollerDamage = null, DamageableComponent? targetDamage = null)
|
||||
{
|
||||
if (!Resolve(roller, ref rollerDamage) || !Resolve(target, ref targetDamage))
|
||||
if (!Resolve(roller, ref rollerDamage, false) || !Resolve(target, ref targetDamage, false))
|
||||
return 1f;
|
||||
|
||||
if (rollerDamage == null || targetDamage == null)
|
||||
|
||||
@@ -6,17 +6,11 @@ namespace Content.Server.Resist;
|
||||
public sealed class CanEscapeInventoryComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// How long it takes to break out of storage. Default at 5 seconds.
|
||||
/// Base doafter length for uncontested breakouts.
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
[DataField("resistTime")]
|
||||
public float ResistTime = 5f;
|
||||
|
||||
/// <summary>
|
||||
/// For quick exit if the player attempts to move while already resisting
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
public bool IsResisting = false;
|
||||
[DataField("baseResistTime")]
|
||||
public float BaseResistTime = 5f;
|
||||
|
||||
/// <summary>
|
||||
/// Cancellation token used to cancel the DoAfter if the mob is removed before it's complete
|
||||
|
||||
@@ -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 { }
|
||||
|
||||
Reference in New Issue
Block a user