Stack System Cleanup (#38872)

* eye on the prize

* OnStackInteractUsing, TryMergeStacks, TryMergeToHands, TryMergeToContacts

* namespace

* Use, get count, getMaxCount

* component access

* add regions, mark TODO

* obsolete TryAdd, public TryMergeStacks

* GetMaxCount

* event handlers

* event handlers

* SetCount

* client server event handlers

* move to shared

* Revert "move to shared"

This reverts commit 45540a2d6b8e1e6d2a8f83a584267776c7edcd73.

* misc changes to shared

* split

* spawn and SpawnNextToOrDrop

* SpawnMultipleAtPosition, SpawnMultipleNextToOrDrop, CalculateSpawns, general server cleanup

* Rename Use to TryUse.

* Small misc changes

* Remove obsolete functions

* Remove some SetCount calls

* Partialize

* small misc change

* don't nuke the git dif with the namespace block

* Comments and reordering

* touchup to UpdateLingering

* Summary comment for StackStatusControl

* Last pass

* Actual last pass (for now)

* I know myself too well

* fixup

* goodbye lingering

* fixes

* review

* fix test

* second look

* fix test

* forgot

* remove early comp getting

---------

Co-authored-by: iaada <iaada@users.noreply.github.com>
Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
This commit is contained in:
āda
2025-10-25 09:40:48 -05:00
committed by GitHub
parent 39aada2018
commit 8d8af1bab7
39 changed files with 935 additions and 753 deletions

View File

@@ -1,110 +1,118 @@
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Shared.Stacks
namespace Content.Shared.Stacks;
/// <summary>
/// Component on an entity that represents a stack of identical things, usually materials.
/// </summary>
[RegisterComponent, NetworkedComponent]
[Access(typeof(SharedStackSystem))]
public sealed partial class StackComponent : Component
{
[RegisterComponent, NetworkedComponent]
public sealed partial class StackComponent : Component
/// <summary>
/// What stack type we are.
/// </summary>
[DataField("stackType", required: true)]
public ProtoId<StackPrototype> StackTypeId = default!;
/// <summary>
/// Current stack count.
/// Do NOT set this directly, use the <see cref="SharedStackSystem.SetCount"/> method instead.
/// </summary>
[DataField]
public int Count = 30;
/// <summary>
/// Max amount of things that can be in the stack.
/// Overrides the max defined on the stack prototype.
/// </summary>
[DataField]
public int? MaxCountOverride;
/// <summary>
/// Set to true to not reduce the count when used.
/// </summary>
[DataField]
public bool Unlimited;
/// <summary>
/// When throwing this item, do we want to only throw one part of the stack or the whole stack at once?
/// </summary>
[DataField]
public bool ThrowIndividually;
/// <summary>
/// Used by StackStatusControl in client to update UI.
/// </summary>
[ViewVariables]
[Access(typeof(SharedStackSystem), Other = AccessPermissions.ReadWrite)] // Set by StackStatusControl
public bool UiUpdateNeeded { get; set; }
/// <summary>
/// Default IconLayer stack.
/// </summary>
[DataField]
public string BaseLayer = "";
/// <summary>
/// Determines if the visualizer uses composite or non-composite layers for icons. Defaults to false.
///
/// <list type="bullet">
/// <item>
/// <description>false: they are opaque and mutually exclusive (e.g. sprites in a cable coil). <b>Default value</b></description>
/// </item>
/// <item>
/// <description>true: they are transparent and thus layered one over another in ascending order first</description>
/// </item>
/// </list>
///
/// </summary>
[DataField("composite")]
public bool IsComposite;
/// <summary>
/// Sprite layers used in stack visualizer. Sprites first in layer correspond to lower stack states
/// e.g. <code>_spriteLayers[0]</code> is lower stack level than <code>_spriteLayers[1]</code>.
/// </summary>
[DataField]
public List<string> LayerStates = new();
/// <summary>
/// An optional function to convert the amounts used to adjust a stack's appearance.
/// Useful for different denominations of cash, for example.
/// </summary>
[DataField]
public StackLayerFunction LayerFunction = StackLayerFunction.None;
}
[Serializable, NetSerializable]
public sealed class StackComponentState : ComponentState
{
public int Count { get; }
public int? MaxCountOverride { get; }
public bool Unlimited { get; }
public StackComponentState(int count, int? maxCountOverride, bool unlimited)
{
[ViewVariables(VVAccess.ReadWrite)]
[DataField("stackType", required: true, customTypeSerializer: typeof(PrototypeIdSerializer<StackPrototype>))]
public string StackTypeId { get; private set; } = default!;
/// <summary>
/// Current stack count.
/// Do NOT set this directly, use the <see cref="SharedStackSystem.SetCount"/> method instead.
/// </summary>
[DataField("count")]
public int Count { get; set; } = 30;
/// <summary>
/// Max amount of things that can be in the stack.
/// Overrides the max defined on the stack prototype.
/// </summary>
[ViewVariables(VVAccess.ReadOnly)]
[DataField("maxCountOverride")]
public int? MaxCountOverride { get; set; }
/// <summary>
/// Set to true to not reduce the count when used.
/// Note that <see cref="Count"/> still limits the amount that can be used at any one time.
/// </summary>
[DataField("unlimited")]
[ViewVariables(VVAccess.ReadOnly)]
public bool Unlimited { get; set; }
[DataField("throwIndividually"), ViewVariables(VVAccess.ReadWrite)]
public bool ThrowIndividually { get; set; } = false;
[ViewVariables]
public bool UiUpdateNeeded { get; set; }
/// <summary>
/// Default IconLayer stack.
/// </summary>
[DataField("baseLayer")]
[ViewVariables(VVAccess.ReadWrite)]
public string BaseLayer = "";
/// <summary>
/// Determines if the visualizer uses composite or non-composite layers for icons. Defaults to false.
///
/// <list type="bullet">
/// <item>
/// <description>false: they are opaque and mutually exclusive (e.g. sprites in a cable coil). <b>Default value</b></description>
/// </item>
/// <item>
/// <description>true: they are transparent and thus layered one over another in ascending order first</description>
/// </item>
/// </list>
///
/// </summary>
[DataField("composite")]
[ViewVariables(VVAccess.ReadWrite)]
public bool IsComposite;
/// <summary>
/// Sprite layers used in stack visualizer. Sprites first in layer correspond to lower stack states
/// e.g. <code>_spriteLayers[0]</code> is lower stack level than <code>_spriteLayers[1]</code>.
/// </summary>
[DataField("layerStates")]
[ViewVariables(VVAccess.ReadWrite)]
public List<string> LayerStates = new();
/// <summary>
/// An optional function to convert the amounts used to adjust a stack's appearance.
/// Useful for different denominations of cash, for example.
/// </summary>
[DataField]
public StackLayerFunction LayerFunction = StackLayerFunction.None;
}
[Serializable, NetSerializable]
public sealed class StackComponentState : ComponentState
{
public int Count { get; }
public int? MaxCount { get; }
public StackComponentState(int count, int? maxCount)
{
Count = count;
MaxCount = maxCount;
}
}
[Serializable, NetSerializable]
public enum StackLayerFunction : byte
{
// <summary>
// No operation performed.
// </summary>
None,
// <summary>
// Arbitrarily thresholds the stack amount for each layer.
// Expects entity to have StackLayerThresholdComponent.
// </summary>
Threshold
Count = count;
MaxCountOverride = maxCountOverride;
Unlimited = unlimited;
}
}
[Serializable, NetSerializable]
public enum StackLayerFunction : byte
{
// <summary>
// No operation performed.
// </summary>
None,
// <summary>
// Arbitrarily thresholds the stack amount for each layer.
// Expects entity to have StackLayerThresholdComponent.
// </summary>
Threshold
}