So I created a inventory system that allows you to have split items and the issue I am having is getting the max amount of ammo for the mag between different stacks. Example there is 3 stacks the first one has 3 the second one has 2 and the last one has 18 for the total of 23 when the mag holds 15 bullets and ive tried a couple ideas but I cant get the remaining value of the last stack. Any help would be amazing and if you need more information let me know! Thank you!
I am not sure if I understand the problem. You want to reload your weapon and take 15 bullets from the inventory? Why not iterating over all ammo stacks and check if the stack has less than 15 bullets and if so, take 3 and note that you now only need 15-3 bullets. Then you look at the next stack and take 2. Calculate you now need 10 bullets. Now at the last stack you see you have 23 bullets but need 10. Then you subtract 10 from the 23 which leaves 13 at the stack and then you break from your loop over the stack. Finally you delete all stacks with 0 bullets from the inventory.
Idk why but I was over complicating it, I was clamping between 2 stacks for a max of 15, and I was getting the range between them, while removing items using recursion, but this is much more simple and will work thank you ![]()
If you wanted to fill up a mag from split stacks 3, 2, 18, just make a greedy accumulator that wants to suck up to 15 bullets in that order. Sucks 3 (expects 12 more; depletes stack), then 2 (expects 10 more; depletes stack), then 10 (expects no more; leaves 8 in the stack).
For even better experience, pre-sort the stacks in the ascending order, so 2, 3, 18. It should always suck up the smaller stacks first.
I believe this is the same thing Olipool said, but Iām hoping that my description is more tidy.