2022-07-09 02:09:52 -07:00
|
|
|
using Content.Shared.Popups;
|
2021-05-26 10:20:57 +02:00
|
|
|
using Content.Shared.Stacks;
|
2021-10-25 09:36:04 -05:00
|
|
|
using Content.Shared.Verbs;
|
2021-05-26 10:20:57 +02:00
|
|
|
using JetBrains.Annotations;
|
|
|
|
|
using Robust.Shared.Map;
|
2021-10-04 01:29:50 +11:00
|
|
|
using Robust.Shared.Player;
|
2021-05-26 10:20:57 +02:00
|
|
|
using Robust.Shared.Prototypes;
|
|
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.Stack
|
2021-05-26 10:20:57 +02:00
|
|
|
{
|
|
|
|
|
/// <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]
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class StackSystem : SharedStackSystem
|
2021-05-26 10:20:57 +02:00
|
|
|
{
|
|
|
|
|
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
|
|
|
|
|
2021-11-09 15:34:40 +01:00
|
|
|
public static readonly int[] DefaultSplitAmounts = { 1, 5, 10, 20, 30, 50 };
|
2021-10-25 09:36:04 -05:00
|
|
|
|
2021-05-26 10:20:57 +02:00
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
2022-02-10 15:30:59 +13:00
|
|
|
SubscribeLocalEvent<StackComponent, GetVerbsEvent<AlternativeVerb>>(OnStackAlternativeInteract);
|
2021-05-26 10:20:57 +02:00
|
|
|
}
|
|
|
|
|
|
2022-03-28 17:03:14 +13:00
|
|
|
public override void SetCount(EntityUid uid, int amount, SharedStackComponent? component = null)
|
|
|
|
|
{
|
|
|
|
|
if (!Resolve(uid, ref component))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
base.SetCount(uid, amount, component);
|
|
|
|
|
|
|
|
|
|
// Queue delete stack if count reaches zero.
|
|
|
|
|
if (component.Count <= 0)
|
|
|
|
|
QueueDel(uid);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 10:20:57 +02:00
|
|
|
/// <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.
|
2021-05-26 10:20:57 +02:00
|
|
|
/// </summary>
|
2021-11-09 15:34:40 +01:00
|
|
|
public EntityUid? Split(EntityUid uid, int amount, EntityCoordinates spawnPosition, SharedStackComponent? stack = null)
|
2021-05-26 10:20:57 +02:00
|
|
|
{
|
2021-09-20 13:39:05 +02:00
|
|
|
if (!Resolve(uid, ref stack))
|
2021-12-26 15:32:45 +13:00
|
|
|
return null;
|
2021-09-20 13:39:05 +02:00
|
|
|
|
2021-05-26 10:20:57 +02:00
|
|
|
// 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
|
2021-12-14 18:11:26 +01:00
|
|
|
: Prototype(stack.Owner)?.ID;
|
2021-11-10 18:23:12 +01:00
|
|
|
|
2021-06-12 11:24:34 +02:00
|
|
|
// Try to remove the amount of things we want to split from the original stack...
|
2021-09-20 13:39:05 +02:00
|
|
|
if (!Use(uid, amount, stack))
|
2021-12-26 15:32:45 +13:00
|
|
|
return null;
|
2021-05-26 10:20:57 +02:00
|
|
|
|
|
|
|
|
// Set the output parameter in the event instance to the newly split stack.
|
2021-12-14 18:11:26 +01:00
|
|
|
var entity = Spawn(prototype, spawnPosition);
|
2021-05-26 10:20:57 +02:00
|
|
|
|
2021-12-14 18:11:26 +01:00
|
|
|
if (TryComp(entity, out SharedStackComponent? stackComp))
|
2021-05-26 10:20:57 +02:00
|
|
|
{
|
|
|
|
|
// Set the split stack's count.
|
2021-11-09 15:34:40 +01:00
|
|
|
SetCount(entity, amount, stackComp);
|
2021-10-31 08:27:11 -05:00
|
|
|
// Don't let people dupe unlimited stacks
|
|
|
|
|
stackComp.Unlimited = false;
|
2021-05-26 10:20:57 +02:00
|
|
|
}
|
2021-06-12 11:24:34 +02:00
|
|
|
|
|
|
|
|
return entity;
|
2021-05-26 10:20:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-06-12 11:24:34 +02:00
|
|
|
/// Spawns a stack of a certain stack type. See <see cref="StackPrototype"/>.
|
2021-05-26 10:20:57 +02:00
|
|
|
/// </summary>
|
2021-11-09 15:34:40 +01:00
|
|
|
public EntityUid Spawn(int amount, StackPrototype prototype, EntityCoordinates spawnPosition)
|
2021-05-26 10:20:57 +02:00
|
|
|
{
|
|
|
|
|
// Set the output result parameter to the new stack entity...
|
2021-12-14 18:11:26 +01:00
|
|
|
var entity = Spawn(prototype.Spawn, spawnPosition);
|
|
|
|
|
var stack = Comp<StackComponent>(entity);
|
2021-05-26 10:20:57 +02:00
|
|
|
|
|
|
|
|
// And finally, set the correct amount!
|
2021-11-09 15:34:40 +01:00
|
|
|
SetCount(entity, amount, stack);
|
2021-06-12 11:24:34 +02:00
|
|
|
return entity;
|
2021-05-26 10:20:57 +02:00
|
|
|
}
|
|
|
|
|
|
2022-08-29 22:31:27 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Say you want to spawn 97 stacks of something that has a max stack count of 30.
|
|
|
|
|
/// This would spawn 3 stacks of 30 and 1 stack of 7.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SpawnMultiple(int amount, int maxCountPerStack, StackPrototype prototype, EntityCoordinates spawnPosition)
|
|
|
|
|
{
|
|
|
|
|
while (amount > 0)
|
|
|
|
|
{
|
|
|
|
|
if (amount > maxCountPerStack)
|
|
|
|
|
{
|
|
|
|
|
var entity = Spawn("MaterialBiomass", spawnPosition);
|
|
|
|
|
var stack = Comp<StackComponent>(entity);
|
|
|
|
|
|
|
|
|
|
SetCount(entity, maxCountPerStack, stack);
|
|
|
|
|
amount -= maxCountPerStack;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var entity = Spawn("MaterialBiomass", spawnPosition);
|
|
|
|
|
var stack = Comp<StackComponent>(entity);
|
|
|
|
|
|
|
|
|
|
SetCount(entity, amount, stack);
|
|
|
|
|
amount = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SpawnMultiple(int amount, int maxCountPerStack, string prototype, EntityCoordinates spawnPosition)
|
|
|
|
|
{
|
|
|
|
|
if (!_prototypeManager.TryIndex<StackPrototype>(prototype, out var stackType))
|
|
|
|
|
{
|
|
|
|
|
Logger.Error("Failed to index stack prototype " + prototype);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SpawnMultiple(amount, maxCountPerStack, stackType, spawnPosition);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 15:30:59 +13:00
|
|
|
private void OnStackAlternativeInteract(EntityUid uid, StackComponent stack, GetVerbsEvent<AlternativeVerb> args)
|
2021-10-25 09:36:04 -05:00
|
|
|
{
|
2022-06-26 21:07:07 -07:00
|
|
|
if (!args.CanAccess || !args.CanInteract || args.Hands == null)
|
2021-10-25 09:36:04 -05:00
|
|
|
return;
|
|
|
|
|
|
2022-02-10 15:30:59 +13:00
|
|
|
AlternativeVerb halve = new()
|
2021-12-14 18:11:26 +01:00
|
|
|
{
|
|
|
|
|
Text = Loc.GetString("comp-stack-split-halve"),
|
|
|
|
|
Category = VerbCategory.Split,
|
|
|
|
|
Act = () => UserSplit(uid, args.User, stack.Count / 2, stack),
|
|
|
|
|
Priority = 1
|
|
|
|
|
};
|
2021-10-25 09:36:04 -05:00
|
|
|
args.Verbs.Add(halve);
|
|
|
|
|
|
|
|
|
|
var priority = 0;
|
|
|
|
|
foreach (var amount in DefaultSplitAmounts)
|
|
|
|
|
{
|
|
|
|
|
if (amount >= stack.Count)
|
|
|
|
|
continue;
|
|
|
|
|
|
2022-02-10 15:30:59 +13:00
|
|
|
AlternativeVerb verb = new()
|
2021-12-14 18:11:26 +01:00
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
};
|
2021-10-25 09:36:04 -05:00
|
|
|
|
|
|
|
|
priority--;
|
|
|
|
|
|
|
|
|
|
args.Verbs.Add(verb);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-09 15:34:40 +01:00
|
|
|
private void UserSplit(EntityUid uid, EntityUid userUid, int amount,
|
|
|
|
|
StackComponent? stack = null,
|
|
|
|
|
TransformComponent? userTransform = null)
|
2021-10-25 09:36:04 -05:00
|
|
|
{
|
2021-11-09 15:34:40 +01:00
|
|
|
if (!Resolve(uid, ref stack))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!Resolve(userUid, ref userTransform))
|
|
|
|
|
return;
|
|
|
|
|
|
2021-10-25 09:36:04 -05:00
|
|
|
if (amount <= 0)
|
|
|
|
|
{
|
2022-07-09 02:09:52 -07:00
|
|
|
PopupSystem.PopupCursor(Loc.GetString("comp-stack-split-too-small"), Filter.Entities(userUid), PopupType.Medium);
|
2021-10-25 09:36:04 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 18:11:26 +01:00
|
|
|
if (Split(uid, amount, userTransform.Coordinates, stack) is not {} split)
|
2021-11-09 15:34:40 +01:00
|
|
|
return;
|
|
|
|
|
|
2022-03-28 17:03:14 +13:00
|
|
|
HandsSystem.PickupOrDrop(userUid, split);
|
2021-11-09 15:34:40 +01:00
|
|
|
|
2022-03-28 17:03:14 +13:00
|
|
|
PopupSystem.PopupCursor(Loc.GetString("comp-stack-split"), Filter.Entities(userUid));
|
2021-10-25 09:36:04 -05:00
|
|
|
}
|
2021-05-26 10:20:57 +02:00
|
|
|
}
|
|
|
|
|
}
|