r/howdidtheycodeit • u/Deive_Ex • Jul 24 '22
Question How did they code Oxygen Not Included pipe system?
I'm trying to make base-building game and I'd like to implement a similar pipe system like the one used in Oxygen Not Included.
Here's a reference video of how it works: https://youtu.be/fH8av1lCPxc?t=1323
Things can get pretty complex: link link
Now, I've been frying my brain for some days already trying to make some prototypes but I can't really figure out a way to have the same quirks that this system has. Here's some important points I was able to observe:
- One pipe tile can move 1 "packet" at a time;
- Pipes get their packets from "output" tiles and gives it to either the next pipe or to an "input" tile
- A pipe needs to be connected to an "input" tile for packets to start moving. If not pipe in the chain is connected to an input tile, packets will stay still.
- Pipes don't have a set direction. Depending on what they're connected to, liquids packets can move from A to B or B to A. In other words, packets always moves in the direction of an input tile, and if you change where this tile is in the chain, packets can change direction.
- Pipes can have multiple connections. In the case of ONI, it can have up to 4 connections (up, down, lef, right). Pipes will alternate which connection the packet will go for every connection it has. Again, this works for both sides, so it can GIVE packets to one or more other pipes or it can RECEIVE packets from one or more other pipes.
- UPDATE: I've noticed a new rule thanks to the comments here: packets tries to move along ALL valid paths, not just the shortest one. So if I make a grid of pipes and have one input and one output, the packets will be split in a way that after a while, the entire grid will be travelled. by multiple packets. So, basically, each packet will take a different route to the end.
I was able to implement a very simple "conveyor belt" system that works transfering objects to a single direction, but it's not nearly close to what ONI does.
20
u/the_Demongod Jul 24 '22
It's hard to say exactly how they did it without investigating the fluid behaviors in-depth (I've never played the game). The main thing your conveyor belt system is probably lacking is a notion of pressure, which decides which way fluid should flow. Whenever the pipe network changes you'll need to traverse the whole graph and identify the sources of input and output, and find the "flow field" through the graph that connects each inlet to the outlet. Those flow lines will be what your pipe update algorithm uses to decide which way a given packet should flow.
2
u/TheSkiGeek Jul 25 '22
Important to note that while "unconnected" pipes don't have a set direction, connected pipes in ONI effectively do. They generally won't work unless there is an unambiguous way for liquid to flow from building outputs to building inputs. I suspect that they do something when the pipe connections are changed to mark each pipe (or perhaps each unbroken section of connected pipes) with a flow direction. And then each game tick you're just pushing liquid along in that direction (and splitting/merging it at intersections).
I'm pretty sure their "pipes" (liquids/gases) and "conveyors" (solids) use the exact same system internally, it's just locking the "pipe" directions based on what inputs and outputs are connected.
2
u/qoning Jul 25 '22
If you ever took an intro graph course in a CS degree, it should be fairly trivial to see how that works. Every input/output is a node in a graph and each connection between two of them creates an edge. Directionality is established from input to output. When looking where the "packet" should go, you iterate through all the connected consumers, or the producers do that, that's implementation dependent.
Think of the entire thing as an abstract graph. It's no longer pipes or whatever, it's just a set of nodes and edges and a "master" entity deciding what happens at every point.
1
u/Slime0 Jul 25 '22
I have an idea, but I have some questions about how it works. What happens if you have a square of pipes (like 5x5 or something) and connect two outputs and one input to different points on it? Does the section of pipe between the outputs get used at all? Same thing with one output and two inputs. Don't connect the inputs and outputs directly to the square, put them on an offshoot of a few tiles.
If you connect an output to an input with a long pipe, and then add another pipe that comes off that long one and then goes back to it, like a mug handle, does the longer path get used at all?
What happens if you make a large grid of tiles, with an output at one corner and an input at the opposite corner? What if the input is at an adjacent corner? What if the input and output are near each other in the middle of a large grid? Does it actually use all the pipes or does it just ignore the ones it doesn't need?
3
u/KaosuRyoko Jul 25 '22
Since it's a unity game, you can actually inspect the source code directly using a tool like ILSpy
1
u/Dannington Jul 25 '22
I should think the pipes in particular are more of a ‘fill all the gaps’ system with the pipe contents being drawn towards all exit nodes. The pumps can only add water if there’s no water already in the next section of pipe so the water needs to be continually drawn towards egress nodes - I guess even if those nodes aren’t being used at that time - like a shower or whatever.
1
u/mikeful Jul 25 '22 edited Jul 25 '22
First idea that came to mind is to calculate Dijkstra map from all inputs traveling only on pipe tiles. Use the map as flow map to move liquid packets towards exits. Recalculate maps if new pipe tiles are added touching existing pipe system.
https://www.youtube.com/watch?v=2ExLEY32RgM