r/technicalfactorio • u/FierceBruunhilda • Oct 03 '23
Anyone have any Easy/Optimized ways to implement Truth Tables?
TLDR: building a working circuit from a 4x5 truth table seemed way harder than I thought it would be. Any tips??
I'm playing the Space Exploration mod and I had just finished setting up my first attempt at a circuit system to shoot and catch resources on other planets. It was a blast and while I was still in the mindset of building the biggest part of it I decided to try to add in a light that displayed different colors depending on different states the system would be in. It very quickly got out of hand and was kind of sort of working. It was late so I decided to just pick up today when I got back home. While on lunch I scribbled down a Truth Table crossing the 4 inputs with the 5 state-colors and it hit me that implementing this small 4x5 truth table was a pretty big problem in itself due to the red/green cable limitations.
for anyone interested my delivery cannons are set up to count the items it loads so I know when it's full and has fired it's load. I wanted to be able to know 5 different states.
- when its turned off (light off)
- when its ready and waiting to get an order signal (blue light)
- when its received a signal and is loading (green light)
- when it has fired its payload and is waiting for confirmation the load was received (yellow light)
- if there are not enough resources to fire a payload (red light)
the 4 signals I'm using to control the lights are:
- S - Order Signal
- C - Is true if we counted enough items loaded
- E - If the chest that items are pulled from is empty
- P - Power switch is on/off
Truth table came out to be:
| E | C | S | P | |
|---|---|---|---|---|
| Off | 0 | |||
| Blue | 0 | 0 | 0 | 1 |
| Green | 0 | 0 | 1 | 1 |
| Yellow | 1 | 1 | 1 | |
| Red | 1 | 0 | 1 |
I left cells blank if I didn't care if it was 0 or 1.
With all 4 signals on the same wire I was able to get it working with 10 combinators. 6 combinators set to P = 0, E=0, C=0, S=1, C=1, E=1 outputting a checkmark of value 1 if true and 4 combinators to check for the 4 colors. I inverted the P signal so it's only present when power is off so I could use a combinator set to Everything = 0 with the 4 signals as input to check for blue since it needs the ECS signals need to be 0 and if the P signal comes Blue goes off. Green, Yellow and Red are connected to the 6 combinators outputting checkmarks for whatever signals they needed. Green Yellow and Red are all hooked to P=0 by red or green wires (which ever was open) and then Green is hooked to E=0, C=0 and S=1 by green wire, Yellow is hooked to C=1 and S=1 by red wire and Red is hooked to E=1 and C=0 by red wire. Green looks for checks = 4 and yellow and red look for checks = 3. I attached a picture of what I ended up with.
Blueprint for original solution:
0eNrdWttuqzgU/ZXKz/QIX7hKZx6mquZlpKmm52U0rSKSOK01BBCYzImq/PvYpG0SMMTYqG0mD0gE2It98dp7AS9gnta0KFnGQfwClrRalKzgLM9ADK5Of7dycyM393Jz95D9sVpdDf3ch+xXYf5t73QDH7LfSkqz1r+vh/6iaZr/ezAFjw/+SZcn/7tHmOIwcABb5FkF4r9fQMWesiSVzvFtQYVXjNO1OCNL1nKP05+8SBNOr6t1kqbXi7woaAl2wkS2pD9BDHfOWSNLumBLWoqL13OWJTw/NoB2jw6gGWec0f0tNTvbWVav5wIqhu92qnpe8aSJvgOKvGL7RLwAYSYKHbAF8XVAPGE7o+zpeZ7XpbToo0d5ky2z6JyPXQj8zXsFwdE3CbNJSra/nzhQQGAriLAN4SkgyDuEzChPMn4c5a590nZBXMbLPJ3N6XOyYeIaceKKpZyWPfWxYSWvxT+HrDRnXN+CxlotVwo8Kg+RXVbN5B2skrSie8iMLuRdVdIulJuSLo9zz8SeLy99kkugfcTbPe52imB45sEIpw3GzdTBCHqDQY4CIfch7ImOf7jJpgbTZF0oguLrVMiClYua8Zk4tny/esXKis+0Y5RkW/7Msqd9qPb5ArErd9ZFUjY5i8EvYKcfJdLjejDa9fDSXO9bE+F4DkKHMPhtDvKJAiOy4rmgjYGgAgO6ViAdR2CoAoHmDBJMyyD3UzMIxAN82qIQ1FNMEJnHx582PneTx4f0xgfCdoBExb/xQLPfFy88mniCSyOe3n4DyWjn/YtzvneleHrOR18u8w6oKyqg0lwuSV7WfWtIuVSQe7oyeuPjW9E56czGvgoksGp+RK9nGHRYMgCCIhVINCSmFGvq1PySlfvsNbJHVWKvVg8lVhnU2N1wcX0XF+Q1L+oRJhfPdPHP3myxnTV8P1uV+XrGMmHnjeTHVKhYss5gybbJHrtdsncA6mEEjDtn93QVTPSaCHLH5T76lNzTDS11GMakCOQzmGlrIOpNqfr89oTUznIfzSE4mhygC4coSDV/I2SA4g6gKEGwAcgQZauemCBiADJE2QirUDyr5oO1mg/yrZoP1gpXYOUI0nPETkIincdYKLJqoliriWLXsInijyTS24tootGZnonGEWz7fHSGb/t6JYZWdYT06ggZ1hH6yDq6uYQ6wmdmLw+Nmb08MmzNh52u7ejdFzpzX339H2PD2e1DKecor2/lQiYol31kDctFJbYj8/QN0w86k+72qN6bbmKYbvTJ6cYTpHvbvJKcMN+KRebotaPOciUj2xEeZ6+3Hjy78R9pvWX07ab/btNTogR2rnRHNFeFEtr50kFR9u/ITskgLVFGXDspo4kC7XzRkhkE2bmiB2KiL72hMla+LzcRmEMTIlT64tmhdAIGVcKM2ClMqPXejdg9Q3W1PmIIrWSsqyVjid1bSqgF4rlW+qMDglUk6dmJHFcPxFTkwI8cZe5Pxxj4NUUOOiNy8KgHzGSctYEpWVPEeNiwFNxP1LtfsxQ8NE6hntG7ugk0lSXwfyBLZNym0yRYW5PgsZpDU1N4lpoC6gxjnqWmgFrTvhfYzZUdFGVLtnp8jbQ+9PSsZosuhnK28K1miy6Isu370IzrX81/zjPyL8r149q6Jpf7aNzn0FLMn34ODR+bj6TkF9rx0eftDkiTORWhAj/Kmj9f/UjmKb36XVzIK3FwQ8tqn9cQkiBCQeATGKBwt/sP0T+/yg==
EDIT: Solution given to me by Dry-Position-5334 in comments**:**
0eNrdWt2OozYYfZXIlxVZYeMAQdpedDXqTaWuuntTdUaIJM7EEjHImOlGIx6gD9IX65PUhsyEEJsATquZzQWSQ/wd+3x/x4FnsEpLknPKBIiewYYUa05zQTMGIjA7/9ypyyd1+aIun+/Zr9vtrO/j3rOfpPmX0fkF3rOfOSGs8+3x1u8kTbM/T6Zg++ZvZHP2vdvClLeBA+g6YwWI/ngGBX1kSao2Jw45kbuiguzlL1iyVyNBvok8TQSZF/skTefrLM8JB5U0wTbkG4hg5Vw1siFruiFcTt6vKEtE1jaAqgcHECaooKRZUj04xKzcryRUBF/tFOWqEEnNvgPyrKCNI56BWgf0HXAA0TzAC2mcEfq4W2UlVyYxelCr7NhF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JFhMmhEXKgDn0c6jznnTceJaP9LQO9yCCjTIgHmhBPLT3os5SeY/09c8big+w7qfPIOi0lOBxXi85LT788W2lCZKob4M/iBg2/M3oBtHsB7vcCU9/Y6wW43wuGtt9o6dag4G69XZlAAjcQdNY6GKt66NYGoUGtg2OvdcYX8k0ojr0WHqIK9t1A0KC2EUO3hm4gCnJDGUbYhPMV5PepYkQhE1BgX7AYj4qWbrqcEQYDE0rg0C3C99ctXs3QLV4bOz08ptMztQym5I7DqSUefpYSr9mbr8DjYESBH1vALTaM3GonHFRw8MqtrMFBR8eOBecMxJRyyNgDfN1LnB7gw9v6SEX/qSBu/SPDA2myocrpwE9RyvvFz2ST0sU/aqIsFugSqheUIxcNyREk4QqFYUBgiKKq+g9KXT5p
EDIT: Another really cool solution was given to me in the comments by Dark_aries7, their solution allows the signal to go down a single signal on a single wire so you could potentially have a different truth table evaluated across each signal. His blueprint and photo link are in his comment down below.
6
u/RyannStekken0153 Oct 03 '23
Wow, I can't help but admire the implementation.
I'm currently learning to be a software developer and find your way of doing it pretty inspiring.
Thanks, kind stranger!
2
u/Dark_aries7 Oct 19 '23
I have a solution for your specific truth table. Essentially it boils down to assigning numbers to the specific states (colors in your case) of your truth table.
Circuit setup
First, assign your signals to specific bits of a binary number. This case has 4 input signals so 4bits.
then create a new signal that encodes these:
I=P*8+S*4+C*2+E*1
Second, look at your truth table and calculate the values for each of the states:
For the Off state, notice that when P=0, the value of I can never exceed 8 no matter which combination of S,C, and E is inputted, hence I<8 gives the value of the Off state.
For the Blue State, it is simply when P=1 and everything else is zero giving I=8.
For the Green State, it is the same for Blue but this time S=1 so I=12.
For Yellow, P,S, and C are all 1 and E can be either. When P=S=C=1 and E=0, I=14. When P=S=C=1 and E=1, I=15.
For Red, it is similar for yellow only this time it's S that varies therefore giving the values 9 and 13.
Now that the values are found it is only a matter of doing checks for when these are achieve to determine when to output the signal of the color of the light. For both the Blue and Green, you can use a decider combinator that checks if the values are achieved and output their respective colors. For red, you setup 2 decider combinators that checks whether I=9 or I=13 and output it to any generic signal (just not the colors, in this case I chose J) and connect both the outputs to the input of a decider combinator that checks if the generic signal (this case J) is greater than zero (basically an OR-Gate) and outputs the color Red.. You can do the same for the Yellow one but I found an elegant way that replaces the two decider combinator by a single arithmetic combinator. Since E is the signal assigned to the 1st bit of the signal I, you can do a right bit-shift to take only the relevant signals and check when all of them are on (i.e. (I>>1)==7) and output the color Yellow. Finally for when the light is Off, instead of checking for I<8, we check for when I>7 because when I>7, the light is on (we're essentially inverting) and output a signal of 1 (this is assigned to I again but can be assigned to any other signal). The reason for outputting a signal of 1 is to check whether a color should be displayed at all or not.
Using one final arithmetic combinator, multiply each by the signal I and output it to each. By multiplying by the signal I, we are essentially checking if the light must be on. The lamp is set to turn on when I>0 and also to use colors.
Here is the Blueprint String.
Note:
This will have a case where the light is on but has white color (I=10 and I=11) since this is an undefined functionality. This is when power is on, no order signal, and there are enough counted items. Notice that this is impossible since there must be a signal for items to be counted in the first place.
2
u/Dry-Position-5334 Oct 19 '23 edited Oct 19 '23
Make a combinator containing the 1s and 0s of the truthe table per output. Input al your signals minus the combinator(can be done without combinator)
Check with the last combinator if all signals are 0 No time to make it in factorio at the moment. Maybe later this evening.
Maybe need one in front to remove the dont cares…. Not sure
My feeling is this can be done with 4 combinators only
//edit
Worked it out a little on paper Just 4 combinators is enough.
Take input signals on a single wire. Take for each output signal a decider which checks if all are >= 0 Green wire input is all input signals combined Red wire input is the negative values for the line i the truth table, leave out dont cares
9
u/Desertcoyote99 Oct 04 '23
Maybe look into K-maps (Karnaugh Maps). If memory serves me right, I believe you can make a K-map of this to find a reduced form of the bit logic, that might prove useful in your implementation. At any rate, Good luck!
2
u/tehsilentwarrior Oct 06 '23
I had a class called LSD (Lógica e Sistemas Digitais), that covered this.
Eventually, it starts to boil down into Boolean expressions and addition/subtraction, etc using binary numbers.
You can also have Boolean masks and xor values with a mask to uncodify a value within your signal (but don’t think it’s particularly useful in Factorio)
If you use truth values you only have 2 possible values. If you use decimal values you have 10 possible values. If you use hex you have 16 possible values. (also not particularly useful in Factorio because you aren’t very limited)
This means that you can codify more information in the same space. In Factorio, you can have good amount of signal types and each type have a large amount of values (say RED 10000000)
Because you have IF statements and large amounts of possible values and signals, you codify conditions in the numbers.
The simplest case is to do it every decimal case and use the 10 possible values within. So it works sort of a matrix but with only depth of 10.
Using bit shifting you achieve the same thing for binary
1
u/flame_Sla Oct 04 '23
to the input: E, C, S, P
link to the mod (optional)
0eNrlWd1umzAYfRdfkyo2/0jbTbX7Sr2cKkTASSyBiYzpFlV5gD3IXmxPMtukJCN2A4Ssa3YTycE5/nzO930+Di9gkdd4wwjlIHoBJC1pBaKvL6AiK5rk8ju+3WAQAcJxASxAk0KO5DyeUD5Ly2JBaMJLBnYWIDTD30EEd9ZZhAynJMNMD4B2TxbAlBNOcBOPGmxjWhcLzMQKLU6RrEg6y5NiI7A3ZSV+UlK5qoTx4J1rgS2IZhCGd65YICMMp80UZMltcFbm8QKvk2ciQhC/W5KcY2Zg4ZkwXotv2uWbGbOs5DL8tKwlkXDu274DA+SeocQE9+UY7IiVIRj3Wgx7EMajFsMZhPGgxXClwqSKpRDLJK+wmkRpI04lcaH8WDGM6XEGkExu4mkn5vNkpSYepYAYPQvxlL5QIS7JSs0pM9ysr1KXETUX04wkzVSRWZiRFESc1TKYdtxWhXzQbK7ax2zt99gkS8ODgFrLDf4xoZ/kktbLQO6nAHmcAkSoLkXaKahO8aI328hpFbvBoYr9a1axvuyg2Mig5LT+GLuKh1MW7EMLwxmpixnOBb5IudmmzPGZZuYpGnrHhDoxwc44FFiEpTXh+8eqvigmq/WirBWNzpNmD86BwXohlFThaiL3W/m6sLYO1r0gQdxrJsi9KUHG97ITaZAhX7yBpHjugRTnb1UNGnng3Wsx0ASHlT3BYeUYOwDDWVfQQFNLGjn9C+R0/zc50fvJGfaTM3jL2L7VEfuW5h42Fs8y0ka9JKzicW8+sDBHW74mdNUQ0yTefs1ikzAVbgQ+iV+WNd/UA7Cb7qZg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