stack cleanup and mild refactoring (#11717)

* stack cleanup

* fix tests and ulong

* somehow did half a commit

* ulong got usmall. (it's ints now)

* sussy baka cleanup

* mirror's review

* make da tests pass again

* shadowcommander review

* sloth por favor

* Update StoreSystem.Ui.cs
This commit is contained in:
Nemanja
2022-11-08 21:24:23 -05:00
committed by GitHub
parent eebb31493c
commit 9428d4b341
31 changed files with 252 additions and 115 deletions

View File

@@ -10,9 +10,7 @@ using Content.Shared.Database;
using Robust.Server.GameObjects;
using System.Linq;
using Content.Server.Stack;
using Content.Shared.Prototypes;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
namespace Content.Server.Store.Systems;
@@ -133,7 +131,9 @@ public sealed partial class StoreSystem : EntitySystem
}
//subtract the cash
foreach (var currency in listing.Cost)
{
component.Balance[currency.Key] -= currency.Value;
}
//spawn entity
if (listing.ProductEntity != null)
@@ -191,7 +191,7 @@ public sealed partial class StoreSystem : EntitySystem
if (msg.Session.AttachedEntity is not { Valid: true} buyer)
return;
FixedPoint2 amountRemaining = msg.Amount;
var coordinates = Transform(buyer).Coordinates;
@@ -199,35 +199,9 @@ public sealed partial class StoreSystem : EntitySystem
foreach (var value in sortedCashValues)
{
var cashId = proto.Cash[value];
if (!_proto.TryIndex<EntityPrototype>(cashId, out var cashProto))
continue;
//how many times this subdivision fits in the amount remaining
var amountToSpawn = (int) Math.Floor((double) (amountRemaining / value));
if (cashProto.HasComponent<StackComponent>())
{
var amountToRemove = amountToSpawn; //we don't want to modify amountToSpawn, as we use it for calculations
while (amountToRemove > 0)
{
var ent = Spawn(cashId, coordinates);
if (!TryComp<StackComponent>(ent, out var stack))
return; //you really fucked up if you got here
var maxAmount = Math.Min(amountToRemove, stack.MaxCount); //limit it based on max stack amount
_stack.SetCount(ent, maxAmount, stack);
_hands.PickupOrDrop(buyer, ent);
amountToRemove -= maxAmount;
}
}
else //please for the love of christ give your currency stack component
{
for (var i = 0; i < amountToSpawn; i++)
{
var ent = Spawn(cashId, coordinates);
_hands.PickupOrDrop(buyer, ent);
}
}
var amountToSpawn = (int) MathF.Floor((float) (amountRemaining / value));
var ents = _stack.SpawnMultiple(cashId, amountToSpawn, coordinates);
_hands.PickupOrDrop(buyer, ents.First());
amountRemaining -= value * amountToSpawn;
}