Combinator Help

Don't know how to use a machine? Looking for efficient setups? Stuck in a mission?
Post Reply
Kelderek
Filter Inserter
Filter Inserter
Posts: 250
Joined: Tue Nov 11, 2014 6:04 pm
Contact:

Combinator Help

Post by Kelderek »

I have a situation with using both red and green color wires to create two similar networks. Each network is connected to a bunch of chests at a train station so that I can read the number of items inside, such as iron plates. So on the red wire I have an iron plate count and on the green wire I have a different iron plate count.

If I feed those red wire and green wires as inputs to a a single decider combinator, I cannot access the red iron plate count separate from the green iron plate count to do a simple comparison. I think it just looks at it as a single generic iron plate value (maybe it adds the two inputs?).

My solution to this problem was to create two new distinct signals using two more arithmetic combinators, one for the red wire where iron plates * 1, output value as "A" on red wire, and the other for green wire where iron plates * 1, output value as "B" on green wire. Now I can feed those into my decider to do the comparison.

This feels highly convoluted to me, is there a simpler way to achieve this result? I only have a minimal amount of experience with combinators. I am a programmer by trade IRL, but this type of low-level programming always gave me trouble.

It seems to me that it would be useful for a combinator to accept the same signal type from both the red and green wires and be able to distinguish between them so you can use arithmetic or do a comparison on them. For example, red "A" + green "A", or red iron plates < green iron plates, etc.

quyxkh
Smart Inserter
Smart Inserter
Posts: 1028
Joined: Sun May 08, 2016 9:01 am
Contact:

Re: Combinator Help

Post by quyxkh »

You can make it lots simpler, each-to-each combinators are your friends. I prefer green for "have" and red for "want" or "take" when I can get it, put one E2E×-1 combinator on your red signal, its output is now isolated from its input circuit so you can use any color you want, but keep it red so you can wire the "want" and "have" values to separate inputs on a < or > decider, compare-with-zero is the usual but you'll find others.

Don't forget that circuit wires are actually busses, they carry all signals simultaneously. The "have less than want" calculation for an entire complex doesn't have to take more than a few combinators total. It's part of the fun, how few combinators do you really need? The answer's often enough startlingly small.

I'm right there with you on how weird circuits are, "thinking in circuits" is a very different mindset. Addition and subtraction you do by connecting wires or pairing inputs, comparison is quite often of a have and (negated)want input sum against a benchmark, accumulation you do with feedback loops.

Kelderek
Filter Inserter
Filter Inserter
Posts: 250
Joined: Tue Nov 11, 2014 6:04 pm
Contact:

Re: Combinator Help

Post by Kelderek »

quyxkh wrote:You can make it lots simpler, each-to-each combinators are your friends. I prefer green for "have" and red for "want" or "take" when I can get it, put one E2E×-1 combinator on your red signal, its output is now isolated from its input circuit so you can use any color you want, but keep it red so you can wire the "want" and "have" values to separate inputs on a < or > decider, compare-with-zero is the usual but you'll find others.

Don't forget that circuit wires are actually busses, they carry all signals simultaneously. The "have less than want" calculation for an entire complex doesn't have to take more than a few combinators total. It's part of the fun, how few combinators do you really need? The answer's often enough startlingly small.

I'm right there with you on how weird circuits are, "thinking in circuits" is a very different mindset. Addition and subtraction you do by connecting wires or pairing inputs, comparison is quite often of a have and (negated)want input sum against a benchmark, accumulation you do with feedback loops.
I'm sorry, but I find this very confusing and I don't really understand. What does "have", "want" and "take" mean in this context? What does E2Ex-1 mean? Are you saying to take each * 1 for the left side and each * -1 for the right side and adding it together so that I would know left > right if the number > 0 and right > left if the number < 0? I'm not sure how that is any simpler than what I did.

quyxkh
Smart Inserter
Smart Inserter
Posts: 1028
Joined: Sun May 08, 2016 9:01 am
Contact:

Re: Combinator Help

Post by quyxkh »

Have and want are ways of assigning semantics to the values on the wires. Generally if I'm comparing two values, it's to trigger some response if I have less than I want. Why are you comparing your two plate counts? What are you going to do with the result? If you make positive values mean "have" and negative values mean "want", and try to keep "have" values on the green wire and "want" values on the red wire, you'll be able to read your circuits much more easily, a `<` decider reads as "if I have less than I want", with a larger want magnitude than have magnitude translating to a <0 combined signal on the input port -- for each of the many dozens of signals on that wire.

E2E is just "each to each", it's a very common pair of port specs, I don't know if there's a more accepted term for them. An E2E×-1 converts its input from have to want or vice-versa, if you've got some less-usual semantics for your operands it's not so hard to remember what it is once you're used to tracking have/want semantics in the usual case.

So you don't need virtual signals, and you only need one E2E×-1 combinator to give whichever count makes more sense to you to "want" semantics, with negative values, for that E2E>0 "have more than I want" or E2E<0 "have less than I want" combinator that outputs only, but all, the items that need attention.

Kelderek
Filter Inserter
Filter Inserter
Posts: 250
Joined: Tue Nov 11, 2014 6:04 pm
Contact:

Re: Combinator Help

Post by Kelderek »

I guess it may help to explain my ultimate goal which is to control rail signals (and soon to be train stops in 0.15) of a pair of same-name train stations. For simplicity I'll refer to them as left and right stations. I want the rail signal closed condition to be like this:

Left rail signal closed if ((both left AND right stations are empty blocks with no trains) AND (left station iron plates > right station iron plates))

Right rail signal closed if ((both left AND right stations are empty blocks with no trains) AND (right station iron plates > left station iron plates))

The result is if both stations are empty of trains, the circuit will force one of them to be closed, preferentially opening the one with the most space available to take new iron plates. Once a train arrives at the open signal, the other one turns back on too so that a second train could come in if necessary.

My setup works fine so far, but I use something like 8 combinators to do it (4 math and 4 decider) and a bunch of virtual signals, A, B, C, D, etc. My initial question here is just about the part of this that compares the iron plates. There is a second half of this that pertains to testing whether or not any trains are currently present based on the state of rail signals.

quyxkh
Smart Inserter
Smart Inserter
Posts: 1028
Joined: Sun May 08, 2016 9:01 am
Contact:

Re: Combinator Help

Post by quyxkh »

Random side note: when I tested this a week or so ago I concluded that trains would path to a station regardless of signal conditions en route, so you couldn't really choose which stations a train would pick with the entry signal. _Thank you_ for getting me to retest this, I had it oh so wrong. Oh boy.

Okay.

Since "have" and "want" don't really apply, how about "red on the right", and red still negative to line up with that? Green-wire your buffer chests, E2E×-1 the right one so right's red and negative, then the green signal and red signal go to an E2E?•1 decider outputting 1 for true, <0 outputs left station < right station, >0 is left > right, you're interested in iron.

With just two stations you don't even need any double-signalling tricks, because one of them's going to be the one trains prefer if they're both green, you only need force that one closed if the other's open and has more iron. Three combinators, with the red-negative-right-want convention it's even easy to read:
Close right station if left open and has more iron
Close right station if left open and has more iron
Screenshot from 2017-04-23 12-56-27.png (3.7 MiB) Viewed 1521 times
Because of the red-negative convention you can immediately see that the * combinator's going to be an E2E×-1, always assuming there's nothing funky going on, but point at the red wire and you can see it goes to a > decider with a green wire from the other set of chests, that's just _got_ to be an E2E comparator, "is there more on the left?" Its output goes to the *, which gets its input from the comparison and the green-wired left-station signal and sends its output to the red-wired right-station signal. You can _read_ this: left-station has more iron * signal's green → right station red, and that's exactly what it's set to: iron * green → red, sent on a nice congruent red wire to the right-station signal, which is set to close if red > 0. And that's all it takes. In this screenshot I dropped an iron into a left-station chest, the right-station signal went red. Train arrives, right-station signal goes green.

When you need to check circuit-controlled stations for train presence you can double-signal them, control the outer signal but check the inner one.

Post Reply

Return to “Gameplay Help”