Niranufoti
Plantera
Since there has seemingly been some confusion about when logic gates emit smoke, and more generally, about how they interact and when they interact the way they're supposed to, I want to clarify some things. Some of this has been said by other people in other places (shoutout to @Statue of Libroty), I was just the first to fact-check and compile all of it into a big post. This is meant essentially as a translation of Terraria's code into human language that anyone can refer to as needed, as well as a guide to solve the most common problems.
Now, by "order of operations", I'm not talking about "Please email my dad a shark" mathematical operations, but about the growing complexity of Terraria's wiring. In the beginning, most devices were rather simple - dart traps, doors, etc. - with pumps being the most complex for relying on two devices working together. Then, more wire colors as well as teleporters were added, increasing the complexity. With 1.3.1, we now have logic gates - devices that themselves emit signals the tick they're activated, leading to an arbitrarily large number of steps within a single game tick.
In this post, I'll occasionally be using terminology from Terraria's code - I'll underline those words to make modders' lives easier.
Here, the teleporters are activated in pairs from left to right each tick, carrying the player all the way to the right on the first pulse and one teleporter to the left on subsequent pulses.
The only case where this problem can occur is if a single switch (or other signal source) activates multiple chains of logic gates that lead together again somewhere down the road. Because of the way the gates are processed, it is possible that different signals reach a single gate at different times - in the same tick, but after a different number of steps.
In order to solve this problem, one has to make sure that all possible signals reach a specific gate at the same time. To achieve this, signals can be delayed by any number of calculation steps using any gate with just a single input (a diode). Toggling this input will toggle the whole gate, so the signal will pass through unscathed, except it's delayed by one step of calculation. As an additional bonus, the wire color can be changed as needed.
That's all for now; I hope it's helpful. I plan on updating and expanding this with more information as time goes on and new features are added, as well as possibly going into more detail with full information for engineers and modders alike (unless the team complains about me using identifiers from their code). If you have any feedback, questions or feel like there's something I should add to this or word differently or whatever, just tell me.


Order of Operations
Before we jump into things, I want to issue a huge "Thank you!"-scream for @Yoraiz0r, who was kind enough to answer my questions and confirm my findings. Without him, pretty much every sentence in this post would contain an "apparently", "it seems" or "as far as I can tell". You're the best!Now, by "order of operations", I'm not talking about "Please email my dad a shark" mathematical operations, but about the growing complexity of Terraria's wiring. In the beginning, most devices were rather simple - dart traps, doors, etc. - with pumps being the most complex for relying on two devices working together. Then, more wire colors as well as teleporters were added, increasing the complexity. With 1.3.1, we now have logic gates - devices that themselves emit signals the tick they're activated, leading to an arbitrarily large number of steps within a single game tick.
In this post, I'll occasionally be using terminology from Terraria's code - I'll underline those words to make modders' lives easier.


Tripping Wires
When a signal is emitted from a tile - a switch was hit, a pressure plate stepped on, a timer ticked; or for short: the wire is tripped - a number of things happen in succession:- The four wire colors - red, blue, green and yellow, in that order - are hit one after the other.
- For each color, the signal travels through wires of that color, activating devices along the way - "simple" devices (traps, doors, lights, actuators) are triggered directly; pumps, teleporters and logic gate lamps are marked down to be handled seperately (non-faulty lamps are also toggled though).
- Afterwards, pumps connected by that wire color are handled in pairs - the first inlet pump that was hit pumps to the first outlet pump, the second inlet to the second outlet etc.
- When all four wire colors have finished, teleporters are handled, again in order of wire colors (red, blue, green, yellow). The activated teleporters are always the one that was hit first and the one that was hit last.
- Lastly, the (unobtainable) pixel boxes and any logic gates are handled.
Here, the teleporters are activated in pairs from left to right each tick, carrying the player all the way to the right on the first pulse and one teleporter to the left on subsequent pulses.


Logic Gates
Simply speaking, if there are logic gates involved, the game alternates between two steps: Hitting wires and checking logic gates.- For each lamp that was hit, the corresponding logic gate is evaluated (if there are faulty lamps involved, that's handled differently).
If the gate is supposed to pulse, but already has, a puff of smoke is emitted. - Then, for each gate that changed state (or faulty gate that's supposed to pulse), if that gate hasn't pulsed during this iteration, it does so now (tripping its output wires, cf. above).
Checking logic gates will be skipped in that case to keep things more orderly. - This is repeated as long as the tripped wires hit more logic gate lamps.


Smoke and missing pulses
As stated above, smoke occurs when a gate is supposed to pulse multiple times during a single iteration. This is to prevent freezing the game with an infinite loop by hooking a wire up to one of its inputs. While this limitation can be used for simple reset mechanisms, it causes collateral damage in the form of robbing us of pulses we would have needed, especially when working with devices such as lights, doors or actuators that have two states - if the gate switches on and off again, but sends only one pulse, the door is opened, but not closed again - if a logic gate lamp is used instead of a door, this can possibly cause quite a mess.The only case where this problem can occur is if a single switch (or other signal source) activates multiple chains of logic gates that lead together again somewhere down the road. Because of the way the gates are processed, it is possible that different signals reach a single gate at different times - in the same tick, but after a different number of steps.
In order to solve this problem, one has to make sure that all possible signals reach a specific gate at the same time. To achieve this, signals can be delayed by any number of calculation steps using any gate with just a single input (a diode). Toggling this input will toggle the whole gate, so the signal will pass through unscathed, except it's delayed by one step of calculation. As an additional bonus, the wire color can be changed as needed.


Last edited: