192 lines
6.7 KiB
C#
192 lines
6.7 KiB
C#
using System;
|
|
using Content.Server.Hands.Components;
|
|
using Content.Server.Popups;
|
|
using Content.Shared.Interaction;
|
|
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;
|
|
using Robust.Shared.Player;
|
|
using Robust.Shared.Prototypes;
|
|
|
|
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 class StackSystem : SharedStackSystem
|
|
{
|
|
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
|
[Dependency] private readonly PopupSystem _popupSystem = 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>
|
|
/// 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))
|
|
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;
|
|
|
|
// Try to remove the amount of things we want to split from the original stack...
|
|
if (!Use(uid, amount, stack))
|
|
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)
|
|
{
|
|
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);
|
|
SetCount(args.Used, otherStack.Count + toTransfer, otherStack);
|
|
|
|
var popupPos = args.ClickLocation;
|
|
|
|
if (!popupPos.IsValid(EntityManager))
|
|
{
|
|
popupPos = Transform(args.User).Coordinates;
|
|
}
|
|
|
|
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;
|
|
|
|
if (TryComp<HandsComponent>(userUid, out var hands) && TryComp<SharedItemComponent>(split, out var item))
|
|
{
|
|
hands.PutInHandOrDrop(item);
|
|
}
|
|
|
|
_popupSystem.PopupCursor(Loc.GetString("comp-stack-split"), Filter.Entities(userUid));
|
|
}
|
|
}
|
|
}
|