Skip to content

Commit

Permalink
Mirror: StrippableSystem doafter overhaul (#205)
Browse files Browse the repository at this point in the history
## Mirror of PR #25994: [StrippableSystem doafter
overhaul](space-wizards/space-station-14#25994)
from <img src="https://avatars.githubusercontent.com/u/10567778?v=4"
alt="space-wizards" width="22"/>
[space-wizards](https://github.com/space-wizards)/[space-station-14](https://github.com/space-wizards/space-station-14)

###### `41ca8f3dfcb986432e1e509247bf239cac137836`

PR opened by <img
src="https://avatars.githubusercontent.com/u/42424291?v=4"
width="16"/><a href="https://github.com/Krunklehorn"> Krunklehorn</a> at
2024-03-11 12:36:28 UTC

---

PR changed 7 files with 465 additions and 305 deletions.

The PR had the following labels:
- Status: Needs Review


---

<details open="true"><summary><h1>Original Body</h1></summary>

> ## About the PR
> 
> Refactors Strippable DoAfter events to make them synchronous and
organized.
> 
> 
> ## Technical details
> 
> ### Strippable System & Component
> - Synchronous DoAfters
> - Made use of `TimeSpan`, `GetStripTimeModifiers()` and `ByRefEvent`
> - Reorganized checks, removed some redundant ones
> - Resolve pattern where useful
> - Added more asserts
> - Lots of cleanup
> 
> The DoAfters were grouped under one event to avoid copy-pasting eight
separate cancel checks, asserts and function signatures.
> 
> Let me know if this is bad for performance and I'll roll them out
instead.
> 
> 
> ## Media
> 
> - [x] I have added screenshots/videos to this PR showcasing its
changes ingame, **or** this PR does not require an ingame showcase
> 
> 
> ## Breaking changes
> 
> ### TimeSpans
> `ThievingComponent`, `InventoryTemplatePrototype` and
`ToggleableClothingSystem` use `TimeSpan` in places where they intersect
with `StrippableComponent`.
> 
> 
> **Changelog**
> 
> N/A
> 


</details>

Signed-off-by: VMSolidus <[email protected]>
Co-authored-by: SimpleStation14 <Unknown>
Co-authored-by: VMSolidus <[email protected]>
  • Loading branch information
SimpleStation14 and VMSolidus committed Jul 1, 2024
1 parent 7c83c8b commit 89a6bb3
Show file tree
Hide file tree
Showing 7 changed files with 465 additions and 305 deletions.
676 changes: 417 additions & 259 deletions Content.Server/Strip/StrippableSystem.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private void StartDoAfter(EntityUid user, EntityUid item, EntityUid wearer, Togg
if (component.StripDelay == null)
return;

var (time, stealth) = _strippable.GetStripTimeModifiers(user, wearer, (float) component.StripDelay.Value.TotalSeconds);
var (time, stealth) = _strippable.GetStripTimeModifiers(user, wearer, component.StripDelay.Value);

var args = new DoAfterArgs(EntityManager, user, time, new ToggleClothingDoAfterEvent(), item, wearer, item)
{
Expand Down
2 changes: 1 addition & 1 deletion Content.Shared/Inventory/InventoryTemplatePrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public sealed partial class SlotDefinition
[DataField("slotFlags")] public SlotFlags SlotFlags { get; private set; } = SlotFlags.PREVENTEQUIP;
[DataField("showInWindow")] public bool ShowInWindow { get; private set; } = true;
[DataField("slotGroup")] public string SlotGroup { get; private set; } = "Default";
[DataField("stripTime")] public float StripTime { get; private set; } = 4f;
[DataField("stripTime")] public TimeSpan StripTime { get; private set; } = TimeSpan.FromSeconds(4f);

[DataField("uiWindowPos", required: true)]
public Vector2i UIWindowPosition { get; private set; }
Expand Down
81 changes: 41 additions & 40 deletions Content.Shared/Strip/Components/StrippableComponent.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Content.Shared.DoAfter;
using Content.Shared.Inventory;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
Expand All @@ -8,10 +9,10 @@ namespace Content.Shared.Strip.Components
public sealed partial class StrippableComponent : Component
{
/// <summary>
/// The strip delay for hands.
/// The strip delay for hands.
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("handDelay")]
public float HandStripDelay = 4f;
public TimeSpan HandStripDelay = TimeSpan.FromSeconds(4f);
}

[NetSerializable, Serializable]
Expand All @@ -21,63 +22,63 @@ public enum StrippingUiKey : byte
}

[NetSerializable, Serializable]
public sealed class StrippingSlotButtonPressed : BoundUserInterfaceMessage
public sealed class StrippingSlotButtonPressed(string slot, bool isHand) : BoundUserInterfaceMessage
{
public readonly string Slot;

public readonly bool IsHand;

public StrippingSlotButtonPressed(string slot, bool isHand)
{
Slot = slot;
IsHand = isHand;
}
public readonly string Slot = slot;
public readonly bool IsHand = isHand;
}

[NetSerializable, Serializable]
public sealed class StrippingEnsnareButtonPressed : BoundUserInterfaceMessage
{
public StrippingEnsnareButtonPressed()
{
}
}
public sealed class StrippingEnsnareButtonPressed : BoundUserInterfaceMessage;

public abstract class BaseBeforeStripEvent : EntityEventArgs, IInventoryRelayEvent
[ByRefEvent]
public abstract class BaseBeforeStripEvent(TimeSpan initialTime, bool stealth = false) : EntityEventArgs, IInventoryRelayEvent
{
public readonly float InitialTime;
public float Time => MathF.Max(InitialTime * Multiplier + Additive, 0f);
public float Additive = 0;
public float Multiplier = 1f;
public bool Stealth;
public readonly TimeSpan InitialTime = initialTime;
public TimeSpan Multiplier = TimeSpan.FromSeconds(1f);
public TimeSpan Additive = TimeSpan.Zero;
public bool Stealth = stealth;

public SlotFlags TargetSlots { get; } = SlotFlags.GLOVES;
public TimeSpan Time => TimeSpan.FromSeconds(MathF.Max(InitialTime.Seconds * Multiplier.Seconds + Additive.Seconds, 0f));

public BaseBeforeStripEvent(float initialTime, bool stealth = false)
{
InitialTime = initialTime;
Stealth = stealth;
}
public SlotFlags TargetSlots { get; } = SlotFlags.GLOVES;
}

/// <summary>
/// Used to modify strip times. Raised directed at the user.
/// Used to modify strip times. Raised directed at the user.
/// </summary>
/// <remarks>
/// This is also used by some stripping related interactions, i.e., interactions with items that are currently equipped by another player.
/// This is also used by some stripping related interactions, i.e., interactions with items that are currently equipped by another player.
/// </remarks>
public sealed class BeforeStripEvent : BaseBeforeStripEvent
{
public BeforeStripEvent(float initialTime, bool stealth = false) : base(initialTime, stealth) { }
}
[ByRefEvent]
public sealed class BeforeStripEvent(TimeSpan initialTime, bool stealth = false) : BaseBeforeStripEvent(initialTime, stealth);

/// <summary>
/// Used to modify strip times. Raised directed at the target.
/// Used to modify strip times. Raised directed at the target.
/// </summary>
/// <remarks>
/// This is also used by some stripping related interactions, i.e., interactions with items that are currently equipped by another player.
/// This is also used by some stripping related interactions, i.e., interactions with items that are currently equipped by another player.
/// </remarks>
public sealed class BeforeGettingStrippedEvent : BaseBeforeStripEvent
[ByRefEvent]
public sealed class BeforeGettingStrippedEvent(TimeSpan initialTime, bool stealth = false) : BaseBeforeStripEvent(initialTime, stealth);

/// <summary>
/// Organizes the behavior of DoAfters for <see cref="StrippableSystem">.
/// </summary>
[Serializable, NetSerializable]
public sealed partial class StrippableDoAfterEvent : DoAfterEvent
{
public BeforeGettingStrippedEvent(float initialTime, bool stealth = false) : base(initialTime, stealth) { }
public readonly bool InsertOrRemove;
public readonly bool InventoryOrHand;
public readonly string SlotOrHandName;

public StrippableDoAfterEvent(bool insertOrRemove, bool inventoryOrHand, string slotOrHandName)
{
InsertOrRemove = insertOrRemove;
InventoryOrHand = inventoryOrHand;
SlotOrHandName = slotOrHandName;
}

public override DoAfterEvent Clone() => this;
}
}
2 changes: 1 addition & 1 deletion Content.Shared/Strip/Components/ThievingComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public sealed partial class ThievingComponent : Component
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("stripTimeReduction")]
public float StripTimeReduction = 0.5f;
public TimeSpan StripTimeReduction = TimeSpan.FromSeconds(0.5f);

/// <summary>
/// Should it notify the user if they're stripping a pocket?
Expand Down
6 changes: 3 additions & 3 deletions Content.Shared/Strip/SharedStrippableSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public override void Initialize()
SubscribeLocalEvent<StrippableComponent, DragDropDraggedEvent>(OnDragDrop);
}

public (float Time, bool Stealth) GetStripTimeModifiers(EntityUid user, EntityUid target, float initialTime)
public (TimeSpan Time, bool Stealth) GetStripTimeModifiers(EntityUid user, EntityUid target, TimeSpan initialTime)
{
var userEv = new BeforeStripEvent(initialTime);
RaiseLocalEvent(user, userEv);
RaiseLocalEvent(user, ref userEv);
var ev = new BeforeGettingStrippedEvent(userEv.Time, userEv.Stealth);
RaiseLocalEvent(target, ev);
RaiseLocalEvent(target, ref ev);
return (ev.Time, ev.Stealth);
}

Expand Down
1 change: 1 addition & 0 deletions Content.Shared/Strip/ThievingSystem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Content.Shared.Inventory;
using Content.Shared.Strip;
using Content.Shared.Strip.Components;

namespace Content.Shared.Strip;
Expand Down

0 comments on commit 89a6bb3

Please sign in to comment.