Xenia at a computer
Skip to Content

FoxSylv.dev:$ cd _

FoxSylv.dev

~/coding

~/trackmania

~/contact

cd _

~/coding

~/trackmania

~/contact

Pipebelts

https://mods.factorio.com/mod/Pipebelts

Add pipes to your Factorio belts

Goals

Originally, my goal was to make a steam-powered transport belt in factorio. Scope creep eventually tranformed that idea into a more general 'belt' which could be powered by any fluid or by electricity.

Production Details

A quick-and-dirty implementation isn't actually too difficult. The problem is that the naive approach is very very laggy. From testing, the naive approach was only able to handle about 2000 pipebelts before noticeable frame drops started occurring. That's still early-game Factorio, and optimization was neccessary to make the mod usuable.

The first major optimization I implemented was grouping. The logic behind grouping is this: By far the most common use case for transport belts is to have long lines of the same belt, so we can group them and process it as, for example, 1 group of 50 instead of 50 individual belts. With some extra handling, grouping can also take into account underground belts and splitters. Then, we can store the group as a rooted graph, using the root to handle group operations and only traversing the full graph as needed.

The second major optimization was warning sprites. A source of lag is the rendering and derendering of the warning sprites, so by placing a cap on how many can be rendered at a time and creating a different, less-laggy but less-clear solution when that cap is reached, lag can be reduced. This is especially important for when an entire base shuts down all at once due to an insufficient requirement and all the belts stop.

The third major optimization had to do with the electric handling. Pipes are connected, which meant that whatever fluid is in the root's pipe is the exact fluid that is within every pipe of that group. For electricity, this is not the case, as every belt is independent. Checking every belt individually was the source of lag in the naive-approach, and continued to be a problem here. The solution is two-fold. First, I treat electricity like pipes, so that only one belt in a group needs power to sustain the entire group. This is a nice QOL feature, and also lets us specifically choose the root so as to ensure it is powered if possible. To find this root, instead of checking every belt every update frame, I instead only check when an electric pole is placed or broken. The electric grid can only change (in regular gameplay) when a pole is placed or broken, so we can simply check the pipebelts that pole reaches and update only those pipebelts for far less work.

The final optimization is to spread out the work across all frames. Although each individual pipebelt group only updates once every 45 ticks by default, we can stagger the updates to not have them all process on the same frame to reduce stuttering.

Conclusion

The final code is frankly a mess. It was my first time using lua, and my first time making a factorio mod, so a lot of moment-to-moment decisions don't really make sense when taken altogether. While I'm quite proud of some of the fluid-related optimizations I made (my code runs faster than the official hyper-optimized transport belt code itself, so there's no need to optimize my own code further), the electricity-related code is jank. There are a few edge-case bugs and a lot of behind-the-scenes laggy nonsense going on that still make it pretty terrible despite my optimizations.

Ideally, I would rewrite a lot of the code to make it more sturdy and readable (particularly the electricity handling code), but I'm still satisfied with what I have. My original goal was just to make a steam-powered transport belt, and I accomplished that smaller goal with ruthless efficiency. Maybe some day I'll come back and fix things, or even add some default mod integrations, but that's a long-term maybe.

  • Favicon by drakonicguy
  • Background Xenia art by Neotheta
  • Footer Xenia art by cathodegaytube
  • Blobfoxes by Volpeon
Xenia the fox linux mascot confidently holding a keyboard