Counting item stacks with circuits?

Don't know how to use a machine? Looking for efficient setups? Stuck in a mission?
User avatar
Drury
Filter Inserter
Filter Inserter
Posts: 794
Joined: Tue Mar 25, 2014 8:01 pm
Contact:

Counting item stacks with circuits?

Post by Drury »

Say, you have a chest holding some random junk, and you want to know exactly how much... In terms of inventory slots, not just raw numbers. Say, for the purposes of knowing how much a mixed storage of arbitrary size could hold.

Basically, you want something like this:

Image

The idea is that you somehow divide each item's signal by the corresponding stack size, but I can't think of a way to make that work.

An alternative would be to have a bunch of arithmetics each with a different inbuilt stack size, problem is the amount of them (would need 7 since there are 8 different stack sizes - 1, 5, 10, 20, 50, 100, 200, 2000) and some kind of signal filter to get each item to a corresponding stack divider arithmetic. No idea how I'd do that either.
User avatar
Drury
Filter Inserter
Filter Inserter
Posts: 794
Joined: Tue Mar 25, 2014 8:01 pm
Contact:

Re: Counting item stacks with circuits?

Post by Drury »

I came up with a solution, it's not pretty or compact and it delays the signal by 3 ticks buuut I guess it works. I dare anyone to come up with something better (because honestly this is kinda crap even by my standards).

Image
Image


Went with the filtering approach. The thing I'm standing next to is the wood chest with the items. The three lines of combinators represent 3 different stack sizes - 50, 100, 200. Items get filtered based on stack sizes via this method (you select which belongs where in the constant combinator manually), then an arithmetic combinator divides them by the stack size, rounding down. This generally gives us an output 1 less than reality, so we calculate the remainder from division as well, and if that's more than 1, then that 1 is added to the final result. In normal operation you'd have 8 lines, one for each stack size.

A more concise explanation of how a line of this works, going from left to right:

The constant combinators hold 1 of each item type for the given stack size. There's about 220 unique items in the game and one combinator can hold up to 18 unique items, meaning 8 rows of 2 is enough for every type (you'd probably need more for mods). I suspect you'd need more for items with stacks of 50 and 100 and less for 1 and 5, but whatever.

The first arithmetic multiplies Each item type from the constant combinators by -2147483648. This is technically unnecessary as you can set each item type to this value in the constant combinator, but whatever.

Next comes the decider. This checks if Each < 0, outputs Each. The wiring here is a little tricky - the chest is connected to the red input of the decider, where the previous arithmetic, the one with -2147483648 as its output, is connected to the green input of the decider. It's crucial that these don't mix, because the chest output goes across all lines. Now, the output of that same -2147483648 arithmetic is also connected to the output of the decider via red wire, where they get added together, filtering items for us via integer underflow magic.

This filtered signal is then passed to two arithmetic combinators. One divides it by the stack size of our line, for the 50 stack line that's i.e. Each/50, output Each. Now if we're dealing with 170 filter inserters, then the result will be 3 due to division always rounding down. This is no good, 170 filter inserters take up 4 slots. Hence the second arithmetic, also connected to the output of the filter. This one divides Each by 50 as well, except it only outputs the remainder as Each, that's the % sign thing (AKA modulo). In the case of filter inserters, that's going to be 20.

The remainder/modulo is then put through a decider that outputs 1 if Each > 0. In practical terms, this serves to count even partial stacks as stacks, because, as I said, the original divider doesn't count those. This is then added to the final result on the power pole. Tada.

To make a properly timed system you'd have to add an extra dummy combinator per line to delay the original signal by a tick due to the modulo shenanigans, so that'd be even less space/anything-efficient.
Drury wrote:but whatever.
blueprint
mrvn
Smart Inserter
Smart Inserter
Posts: 5851
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: Counting item stacks with circuits?

Post by mrvn »

I was really hoping for something better than filtering. I want to divide by arbitrary numbers and not just stack sizes and for all of Bobs+Angels items. Each item would need their own arithmetic combinator and things would get HUGE.

There really should be an inverse combinator or 1/each=each.
The Eriksonn
Fast Inserter
Fast Inserter
Posts: 230
Joined: Wed Jun 08, 2016 6:16 pm
Contact:

Re: Counting item stacks with circuits?

Post by The Eriksonn »

i Think you can write 1/each in the combinator now, but it wont make any sense becauce it is Always integer so you can only get 1 or 0

unless you do 1000/each and then do something with that
mrvn
Smart Inserter
Smart Inserter
Posts: 5851
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: Counting item stacks with circuits?

Post by mrvn »

Yeah, obviously you would need 1000/each to get a useful result.

I really would love to have combinators that can act on wire color so you could do:

red wire / green wire = each

Include wireless as well:

logistics network / each = each
User avatar
Impatient
Filter Inserter
Filter Inserter
Posts: 883
Joined: Sun Mar 20, 2016 2:51 am
Contact:

Re: Counting item stacks with circuits?

Post by Impatient »

mrvn wrote: ...
I really would love to have combinators that can act on wire color ...
Plz support: viewtopic.php?f=6&t=56890

:)
leitk
Fast Inserter
Fast Inserter
Posts: 115
Joined: Wed Jun 21, 2017 7:20 pm
Contact:

Re: Counting item stacks with circuits?

Post by leitk »

Here's something a bit better, still needs a filter.
StackCounter.PNG
StackCounter.PNG (409.35 KiB) Viewed 5921 times
The CCs have -1 billion for everything not of the right stack size and 1 less than the stack size for everything of the right stack size.
(in this example for 50 stack size it has 49 for coal and filter inserters, and -1 billion for everything else)
This can be limited if you're OK with only counting a few things.
The decider is: each > 0 output each. Again be sure not to cross the wires.
The division is: each / stack size output each. Since we've added one less than the stack size, this will be the right amount

If you have many things to count and don't want to set up so many constant combinators (and don't mind an extra tick), you can instead have one that subtracts 1 billion from everything, and then your CC will have ( billion + stack size - 1 for each stack size of the right kind, and nothing for other stack sizes (so 1000000049 for coal in the 50 stack size CC).

I would also like combinators to act on wire color.
Post Reply

Return to “Gameplay Help”