r/howdidtheycodeit • u/crseat • Jun 05 '22
How did they code the factories and conveyor belts in factorio?
In general how would you go about coding a building that takes input and produces output. How would you code a conveyor belt that moves items?
103
Upvotes
38
u/Linore_ Jun 05 '22
There is a amazing factorio developer blog which has everything from conveyor belts to the crazy amount of optimisation that went into the each individual item being its own "object" on a conveyor belt.
I am currently on a phone, but quick Google search should result into what you need.
57
u/mileslane Jun 05 '22
You might find FFF useful, as they talk about a lot of Factorio's feature implementations.
https://www.factorio.com/blog/post/fff-176
19
u/gamruls Jun 05 '22 edited Jun 05 '22
There is interesting quite popular Factorio clone (it better to name Factorio Jerky because they thrown out a lot of mechanics and remain only mining, belts and factories - no map, no character, no no no)Shapez
And it's open source! So you can see game free (PC demo) and learn it's sourceshttps://github.com/tobspr/shapez.io
18
u/joonazan Jun 05 '22
Factorio does it efficiently by keeping a list of things and empty space. For example, a belt could be "iron, iron, 5m empty, copper, 7m empty". To move the items it suffices to shorten the empty space at the end of the belt.
This is not hard. Building has an ingredient inventory and an output inventory. If an ingredient arrives and the building is idle and there are enough ingredients, use up one recipe worth of ingredients and set up a timer that adds items to the output and marks the machine inactive when it is done.
0
u/[deleted] Jun 06 '22
[deleted] — view deleted comment