Adds stack splitting (#4981)

* Adds stack splitting

* Apply suggestions from code review

Co-authored-by: ike709 <ike709@github.com>
Co-authored-by: Vera Aguilera Puerto <6766154+Zumorica@users.noreply.github.com>
This commit is contained in:
ike709
2021-10-25 09:36:04 -05:00
committed by GitHub
parent debd22e19c
commit ec92f4eef2
4 changed files with 77 additions and 1 deletions

View File

@@ -1,7 +1,12 @@
using System; using System;
using System.Collections.Generic;
using Content.Server.Hands.Components;
using Content.Server.Items;
using Content.Server.Popups; using Content.Server.Popups;
using Content.Shared.Interaction; using Content.Shared.Interaction;
using Content.Shared.Popups;
using Content.Shared.Stacks; using Content.Shared.Stacks;
using Content.Shared.Verbs;
using JetBrains.Annotations; using JetBrains.Annotations;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.IoC; using Robust.Shared.IoC;
@@ -23,11 +28,14 @@ namespace Content.Server.Stack
[Dependency] private readonly IPrototypeManager _prototypeManager = default!; [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly PopupSystem _popupSystem = default!; [Dependency] private readonly PopupSystem _popupSystem = default!;
public static readonly List<int> DefaultSplitAmounts = new() { 1, 5, 10, 20, 30, 50 };
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<StackComponent, InteractUsingEvent>(OnStackInteractUsing); SubscribeLocalEvent<StackComponent, InteractUsingEvent>(OnStackInteractUsing);
SubscribeLocalEvent<StackComponent, GetAlternativeVerbsEvent>(OnStackAlternativeInteract);
} }
/// <summary> /// <summary>
@@ -117,5 +125,64 @@ namespace Content.Server.Stack
args.Handled = true; 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<HandsComponent>(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<ItemComponent>(out var itemComponent))
{
hands.PutInHandOrDrop(itemComponent);
}
}
} }
} }

View File

@@ -50,5 +50,8 @@ namespace Content.Shared.Verbs
public static readonly VerbCategory SetTransferAmount = public static readonly VerbCategory SetTransferAmount =
new("verb-categories-transfer", "/Textures/Interface/VerbIcons/spill.svg.192dpi.png"); new("verb-categories-transfer", "/Textures/Interface/VerbIcons/spill.svg.192dpi.png");
public static readonly VerbCategory Split =
new("verb-categories-split", null);
} }
} }

View File

@@ -16,3 +16,8 @@ comp-stack-already-full = Stack is already full.
# Shown when a stack becomes full # Shown when a stack becomes full
comp-stack-becomes-full = Stack is now 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.

View File

@@ -21,3 +21,4 @@ verb-categories-close = Close
verb-categories-open = Open verb-categories-open = Open
verb-categories-rotate = Rotate verb-categories-rotate = Rotate
verb-categories-transfer = Set Transfer Amount verb-categories-transfer = Set Transfer Amount
verb-categories-split = Split