Friday Facts #416 - Fluids 2.0

Posted by raiguard on 2024-06-21

Hello,
Grab your best lube, because it's time to talk about fluids!


Reaching the breaking point

It is no secret that Factorio's fluid system is unpredictable, unintuitive, and at times, frustrating. Pipe throughput decreases over distance at an inconsistent rate, so the only way to reliably know how much fluid you can push through a pipe is to reference a table on the wiki. Furthermore, the throughput can vary depending on the order that the pipes were built. It is not a fun system to play with.

There have been many attempts to rewrite the fluid system, but all ended in failure for one reason or another. The existing algorithm is pretty fast and works well enough for standard vanilla gameplay, and we have tried to leave it alone as much as possible. However, during our playtesting of Space Age, it became clear that the existing system would no longer do the job. The production scaling afforded by the new machines and quality brought the flow algorithm to its knees.

Why isn't this working!?

In an attempt to avoid a total rewrite, we first tried increasing throughput by increasing pipe volume. This "solved" the primary issue of low throughput with the trade-off of massively increasing buffer sizes. Storage tanks became effectively useless, and defensive walls utilizing flamethrower turrets were buffering tens of thousands of units of fluid, sucking bases dry. Ultimately, we were unable to come to an agreement on how to proceed; a rewrite was deemed too complex, but nobody had any better ideas. The issue was put aside to be "discussed again later".

I was extremely dissatisfied with this conclusion. The volume hack was a band-aid to hide a problem rather than a well-formed fix. Sometimes it is easier to ask for forgiveness than to ask for permission, so I took a risk and began to rewrite the fluid system.


The new algorithm

During one of the fluids discussions, Rseding proposed an algorithm very close to one he had played with several times, an algorithm from the Minecraft mod Thermal Expansion by team CoFH:

  • Pipes, underground pipes, and storage tanks are merged into fluid "segments".
  • Each segment inherits its volume from the fluid boxes that comprise it and can hold one fluid.
  • Machines can push fluid into a segment at an unlimited rate, and can pull from a segment at a rate proportional to how full the segment is. In other words, if a segment is half full, then the pulling rate is half of the maximum.
  • As a special case, pumps can pull at a faster rate if they are connected directly to a storage tank.

There is no longer a realistic fluid "flow" through pipes; fluid pushed to a segment will be immediately available at any point along a segment. This is the "nuclear electric-network type solution" that was discussed in previous FFFs. The result is that pipes "just work" and you almost never have to worry about throughput.

It just works™.


Demonstrations

Note the debug visualisations:

  • Green represents the amount of fluid in the entity.
  • Blue represents the amount of flow through the entity on that tick.

Fluid flow animations are not final.

Long pipelines

In the old system, throughput would get worse as pipes got longer. The issue was not necessarily that this behavior existed, but that it was unpredictable.

In the new system, segments are a single unit, so fluid no longer propagates across a pipeline. Longer pipelines have higher throughput, but take longer to fully empty. The exact throughput numbers are subject to change based on further playtesting.

Junctions

The old system functioned relatively okay with straight pipelines, but as soon as a junction was formed everything would fall apart. The amount of fluid flowing to each side of the junction would be different based on the order that the entities were built.

In the new system, junctions are entirely unimportant; a segment only cares about the machines connected to it. When a machine is updated, its max pulling rate is limited based on the filled ratio of the connected segment. This results in much more even splitting, which while still not perfect, is pretty damn close.

Input starvation

In the old system, junctions would get particularly bad when machines were starved of input fluid. The fuel plant is getting a much larger share of the water than the oxidizer plant.

In the new system, both plants get a much more equal share of the fluid.

Pumps

In the old system, pumps were required in order to maintain higher throughput. However, it almost never worked as you would expect:

In the new system, there is no longer a need to use pumps for this purpose. Pumps are still useful to force directional flow, act as conditional valves with a circuit network, and to load/unload trains, but you won't be placing nearly as many of them as before.

Endgame setups

At last, we come back to our favorite chemical plant setup. This setup doesn't work because the petroleum is getting tied up in the output pipe.

In the new system, the output segment has sufficient capacity such that all of the chemical plants have an opportunity to push their petroleum.


Fun over realism

The new system is a fairly large step back in terms of the "realism" of the fluid simulation in Factorio. But as a game designer, you always have to make trade-offs between what would make sense in the real world and what is fun for a game. Assembling machines abstract away an enormous amount of work that goes into real-life automation and puts it in a neat little box. Similarly, the new fluid system gives you the behavior you would expect from a pipeline while abstracting away all of the details of real fluid flow.

It is a very fun system to play with. There are many interesting possibilities with this system that we did not predict, and we will share some of our stories in future FFFs. I may also go into detail about the architecture of this new system and how I approached the refactor at another time.

There are a few details that remain work-in-progress, but the new system as a whole works very well. We can't wait for you to get your hands on it!


As always, flow your thoughts to us at the usual places.