diff --git a/Content.Server/Stack/StackSystem.cs b/Content.Server/Stack/StackSystem.cs index 728ccefb1f..f3e7cda230 100644 --- a/Content.Server/Stack/StackSystem.cs +++ b/Content.Server/Stack/StackSystem.cs @@ -1,7 +1,12 @@ using System; +using System.Collections.Generic; +using Content.Server.Hands.Components; +using Content.Server.Items; using Content.Server.Popups; using Content.Shared.Interaction; +using Content.Shared.Popups; using Content.Shared.Stacks; +using Content.Shared.Verbs; using JetBrains.Annotations; using Robust.Shared.GameObjects; using Robust.Shared.IoC; @@ -23,11 +28,14 @@ namespace Content.Server.Stack [Dependency] private readonly IPrototypeManager _prototypeManager = default!; [Dependency] private readonly PopupSystem _popupSystem = default!; + public static readonly List DefaultSplitAmounts = new() { 1, 5, 10, 20, 30, 50 }; + public override void Initialize() { base.Initialize(); SubscribeLocalEvent(OnStackInteractUsing); + SubscribeLocalEvent(OnStackAlternativeInteract); } /// @@ -117,5 +125,64 @@ namespace Content.Server.Stack args.Handled = true; } + + private void OnStackAlternativeInteract(EntityUid uid, StackComponent stack, GetAlternativeVerbsEvent args) + { + if (!args.CanAccess || !args.CanInteract) + return; + + Verb halve = new(); + halve.Text = Loc.GetString("comp-stack-split-halve"); + halve.Category = VerbCategory.Split; + halve.Act = () => UserSplit(args.User, stack, stack.Count / 2); + halve.Priority = 1; + args.Verbs.Add(halve); + + var priority = 0; + foreach (var amount in DefaultSplitAmounts) + { + if (amount >= stack.Count) + continue; + + Verb verb = new(); + verb.Text = amount.ToString(); + verb.Category = VerbCategory.Split; + verb.Act = () => UserSplit(args.User, stack, amount); + + // we want to sort by size, not alphabetically by the verb text. + verb.Priority = priority; + priority--; + + args.Verbs.Add(verb); + } + } + + private void UserSplit(IEntity user, StackComponent stack, int amount) + { + if (amount <= 0) + { + user.PopupMessage(Loc.GetString("comp-stack-split-too-small")); + return; + } + + if (user.TryGetComponent(out var hands)) + { + if (hands.TryGetActiveHeldEntity(out var heldItem) && heldItem != stack.Owner) + { + return; + } + } + else + { + return; + } + + var secondStack = Split(stack.Owner.Uid, amount, user.Transform.Coordinates, stack); + user.PopupMessage(Loc.GetString("comp-stack-split")); + if (secondStack is not null && secondStack.TryGetComponent(out var itemComponent)) + { + hands.PutInHandOrDrop(itemComponent); + } + } } } diff --git a/Content.Shared/Verbs/VerbCategory.cs b/Content.Shared/Verbs/VerbCategory.cs index 2de3eb145f..f06b84f5e9 100644 --- a/Content.Shared/Verbs/VerbCategory.cs +++ b/Content.Shared/Verbs/VerbCategory.cs @@ -50,5 +50,8 @@ namespace Content.Shared.Verbs public static readonly VerbCategory SetTransferAmount = new("verb-categories-transfer", "/Textures/Interface/VerbIcons/spill.svg.192dpi.png"); + + public static readonly VerbCategory Split = + new("verb-categories-split", null); } } diff --git a/Resources/Locale/en-US/stack/stack-component.ftl b/Resources/Locale/en-US/stack/stack-component.ftl index cce1fea47e..e8cbaa69df 100644 --- a/Resources/Locale/en-US/stack/stack-component.ftl +++ b/Resources/Locale/en-US/stack/stack-component.ftl @@ -16,3 +16,8 @@ comp-stack-already-full = Stack is already full. # Shown when a stack becomes full comp-stack-becomes-full = Stack is now full. + +# Text related to splitting a stack +comp-stack-split = You split the stack. +comp-stack-split-halve = Halve +comp-stack-split-too-small = Stack is too small to split. diff --git a/Resources/Locale/en-US/verbs/verb-system.ftl b/Resources/Locale/en-US/verbs/verb-system.ftl index 809b9a60e7..5bdb607ffa 100644 --- a/Resources/Locale/en-US/verbs/verb-system.ftl +++ b/Resources/Locale/en-US/verbs/verb-system.ftl @@ -1,4 +1,4 @@ -verb-system-waiting-on-server-text = Waiting on Server... +verb-system-waiting-on-server-text = Waiting on Server... verb-system-no-verbs-text = No verbs! verb-system-null-server-response = Entity not in view. You should not see this. @@ -21,3 +21,4 @@ verb-categories-close = Close verb-categories-open = Open verb-categories-rotate = Rotate verb-categories-transfer = Set Transfer Amount +verb-categories-split = Split