Files
OldThink/Content.Server/Stack/StackSystem.cs

191 lines
6.7 KiB
C#
Raw Normal View History

using System;
using Content.Server.Hands.Components;
using Content.Server.Popups;
2022-03-17 20:13:31 +13:00
using Content.Shared.Hands.EntitySystems;
2021-06-09 22:19:39 +02:00
using Content.Shared.Interaction;
get that crap outta here (completely rewrites inventorysystem) (#5807) * some work * equip: done unequip: todo * unequipping done & refactored events * workin * movin * reee namespaces * stun * mobstate * fixes * some work on events * removes serverside itemcomp & misc fixes * work * smol merge fix * ports template to prototype & finishes ui * moves relay & adds containerenumerator * actions & cuffs * my god what is actioncode * more fixes * im loosing my grasp on reality * more fixes * more work * explosions * yes * more work * more fixes * merge master & misc fixed because i forgot to commit before merging master * more fixes * fixes * moar * more work * moar fixes * suffixmap * more work on client * motivation low * no. no containers * mirroring client to server * fixes * move serverinvcomp * serverinventorycomponent is dead * gaming * only strippable & ai left... * only ai and richtext left * fixes ai * fixes * fixes sprite layers * more fixes * resolves optional * yes * stable:tm: * fixes * moar fixes * moar * fix some tests * lmao * no comment * good to merge:tm: * fixes build but for real * adresses some reviews * adresses some more reviews * nullables, yo * fixes lobbyscreen * timid refactor to differentiate actor & target * adresses more reviews * more * my god what a mess * removed the rest of duplicates * removed duplicate slotflags and renamed shoes to feet * removes another unused one * yes * fixes lobby & makes tryunequip return unequipped item * fixes * some funny renames * fixes * misc improvements to attemptevents * fixes * merge fixes Co-authored-by: Paul Ritter <ritter.paul1@gmail.com>
2021-12-30 22:56:10 +01:00
using Content.Shared.Item;
using Content.Shared.Stacks;
using Content.Shared.Verbs;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Map;
using Robust.Shared.Maths;
2021-10-04 01:29:50 +11:00
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
2021-06-09 22:19:39 +02:00
namespace Content.Server.Stack
{
/// <summary>
/// Entity system that handles everything relating to stacks.
/// This is a good example for learning how to code in an ECS manner.
/// </summary>
[UsedImplicitly]
public sealed class StackSystem : SharedStackSystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly PopupSystem _popupSystem = default!;
2022-03-17 20:13:31 +13:00
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
public static readonly int[] DefaultSplitAmounts = { 1, 5, 10, 20, 30, 50 };
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<StackComponent, InteractUsingEvent>(OnStackInteractUsing);
SubscribeLocalEvent<StackComponent, GetVerbsEvent<AlternativeVerb>>(OnStackAlternativeInteract);
}
/// <summary>
2021-12-03 16:08:30 +01:00
/// Try to split this stack into two. Returns a non-null <see cref="Robust.Shared.GameObjects.EntityUid"/> if successful.
/// </summary>
public EntityUid? Split(EntityUid uid, int amount, EntityCoordinates spawnPosition, SharedStackComponent? stack = null)
{
if (!Resolve(uid, ref stack))
2021-12-26 15:32:45 +13:00
return null;
// Get a prototype ID to spawn the new entity. Null is also valid, although it should rarely be picked...
var prototype = _prototypeManager.TryIndex<StackPrototype>(stack.StackTypeId, out var stackType)
? stackType.Spawn
: Prototype(stack.Owner)?.ID;
2021-11-10 18:23:12 +01:00
// Try to remove the amount of things we want to split from the original stack...
if (!Use(uid, amount, stack))
2021-12-26 15:32:45 +13:00
return null;
// Set the output parameter in the event instance to the newly split stack.
var entity = Spawn(prototype, spawnPosition);
if (TryComp(entity, out SharedStackComponent? stackComp))
{
// Set the split stack's count.
SetCount(entity, amount, stackComp);
// Don't let people dupe unlimited stacks
stackComp.Unlimited = false;
}
return entity;
}
/// <summary>
/// Spawns a stack of a certain stack type. See <see cref="StackPrototype"/>.
/// </summary>
public EntityUid Spawn(int amount, StackPrototype prototype, EntityCoordinates spawnPosition)
{
// Set the output result parameter to the new stack entity...
var entity = Spawn(prototype.Spawn, spawnPosition);
var stack = Comp<StackComponent>(entity);
// And finally, set the correct amount!
SetCount(entity, amount, stack);
return entity;
}
private void OnStackInteractUsing(EntityUid uid, StackComponent stack, InteractUsingEvent args)
{
2021-09-25 10:44:54 +02:00
if (args.Handled)
return;
if (!TryComp<StackComponent>(args.Used, out var otherStack))
return;
if (!otherStack.StackTypeId.Equals(stack.StackTypeId))
return;
var toTransfer = Math.Min(stack.Count, otherStack.AvailableSpace);
SetCount(uid, stack.Count - toTransfer, stack);
2021-12-03 15:53:09 +01:00
SetCount(args.Used, otherStack.Count + toTransfer, otherStack);
var popupPos = args.ClickLocation;
if (!popupPos.IsValid(EntityManager))
{
popupPos = Transform(args.User).Coordinates;
}
2021-12-03 15:53:09 +01:00
var filter = Filter.Entities(args.User);
switch (toTransfer)
{
case > 0:
_popupSystem.PopupCoordinates($"+{toTransfer}", popupPos, filter);
if (otherStack.AvailableSpace == 0)
{
_popupSystem.PopupCoordinates(Loc.GetString("comp-stack-becomes-full"),
popupPos.Offset(new Vector2(0, -0.5f)) , filter);
}
break;
case 0 when otherStack.AvailableSpace == 0:
_popupSystem.PopupCoordinates(Loc.GetString("comp-stack-already-full"), popupPos, filter);
break;
}
args.Handled = true;
}
private void OnStackAlternativeInteract(EntityUid uid, StackComponent stack, GetVerbsEvent<AlternativeVerb> args)
{
if (!args.CanAccess || !args.CanInteract)
return;
AlternativeVerb halve = new()
{
Text = Loc.GetString("comp-stack-split-halve"),
Category = VerbCategory.Split,
Act = () => UserSplit(uid, args.User, stack.Count / 2, stack),
Priority = 1
};
args.Verbs.Add(halve);
var priority = 0;
foreach (var amount in DefaultSplitAmounts)
{
if (amount >= stack.Count)
continue;
AlternativeVerb verb = new()
{
Text = amount.ToString(),
Category = VerbCategory.Split,
Act = () => UserSplit(uid, args.User, amount, stack),
// we want to sort by size, not alphabetically by the verb text.
Priority = priority
};
priority--;
args.Verbs.Add(verb);
}
}
private void UserSplit(EntityUid uid, EntityUid userUid, int amount,
StackComponent? stack = null,
TransformComponent? userTransform = null)
{
if (!Resolve(uid, ref stack))
return;
if (!Resolve(userUid, ref userTransform))
return;
if (amount <= 0)
{
_popupSystem.PopupCursor(Loc.GetString("comp-stack-split-too-small"), Filter.Entities(userUid));
return;
}
if (Split(uid, amount, userTransform.Coordinates, stack) is not {} split)
return;
2022-03-17 20:13:31 +13:00
_handsSystem.PickupOrDrop(userUid, split);
_popupSystem.PopupCursor(Loc.GetString("comp-stack-split"), Filter.Entities(userUid));
}
}
}