Tech devlog: Scaling the MegaFactory


My goal with MegaFactory is to allow the player to build a factory on a truly massive scale.

Factory simulation games are typically limited by CPU, since that is where the simulation code runs. Being able to use multiple CPU cores is therefore critical to achieving large scale.

I took some clues from the game Dyson Sphere Program. In this game, the world is split into planets and solar systems. Of course I do not know how this game is implemented, but I assume this is done so that each planet can run its simulation on a separate thread.

Following the same principle, I have divided my factory into separate floors. Each floor is currently a grid of 300x100 tiles. With machines being 1-4 tiles, this allows you to build at least 7500 machines in one floor.
To further increase the factory, you can buy additional floors, only one of which is visible at a time. All the floors still run the same detailed simulation though, with each machine being individually simulated. However, with the separate floors, each floor can run its simulation on a separate thread, so that it scales across multiple CPU cores.

Just as DSP has logistic spaceships that can transport items between planets, MegaFactory also has cross-floor transportation.
In MegaFactory this will be Quantum-Mechanical-Sci-Fi-Magic teleporters that send items between floors. The transporter building can only send and the receiver building can only receive.
This of course adds one synchronization point, which has to be handled on a single coordinator thread. However, this will be only at the start of each simulation cycle, the rest of the simulation runs on separate worker threads.

I have tried to illustrate the system on the drawing below. At the end of each cycle, the floor worker threads write to shared storage what their transporters offer and what their receivers request. When all simulation workers report done, the coordinator thread will process the requests, divide the available resources, and update the worker threads with the result.


Leave a comment

Log in with itch.io to leave a comment.