I am currently designing electricity for my game. If you've ever played or seen Factorio, this is pretty much the exact way I want it to be.

Okay, on to theory: What I came up with is some sort of power source produces a certain amount of Watts per frame. Most power sources cannot store any energy, so at the end of the frame, it is all lost.

Machines connected by wires to energy sources will perform a Dijkstra-style flood-fill from themselves through all connected wires, ignoring wires that have already been processed. The Watt cost will slightly increase for each wire it passes through (to simulate energy loss) and when it reaches a battery or power source, it will drain as much as it needs. If the source doesn't have enough, it continues to search for more power. If, by the end of the frame, it doesn't have enough Watts, it simply works slower or stops working entirely.

After all machines have done this, all batteries will perform a similar flood-fill, but ignoring other batteries, and will attempt to take ALL of the energy from sources (up to its storage capacity).

Then, all power sources discard all energy.

Is this how it actually works? I want to use numerical values, so just being connected to an energy source is not enough. Thanks.


Factorio handles all energy-relevant machines (generators, consumers and storage) by "power grids". A "power grid" is a network of all machines which are connected to each other by the same mesh of electricity poles.

That means when you place a power pole, you can perform one flood-fill to enumerate all objects which belong to that power grid and puts them into a list. Caching the machines which are on the same electric grid has the advantage that you don't need to perform an expensive flood-fill every frame.

(this is a simple solution, but not necessarily the most efficient one in every situation. You might want to check out this article on the Factorio Dev Blog. It gives more insight into the Factorio electric grid system)

Now the math isn't difficult anymore. You just sum up the consumption of all consumers and the production of all generators on each grid. If one outnumbers the other, you check if the difference can be fulfilled by storage. If it can't, you proportionally reduce the output of all generators (they run on reduced capacity) or proportionally reduce the efficiency of all machines (lack of energy).

  • \$\begingroup\$ If that's how it works, then what happens if two large power grids are suddenly connected to each other? How does Factorio handle that? I assume it simply merges the power grids, but I'm not sure how I would test something like that. Maybe flood filling for every pole that is placed and saving a temporary list of machines, then take all power grids containing those machines and deleting them, adding the new merged one? \$\endgroup\$
    – Anixias
    Mar 15 '17 at 12:51
  • \$\begingroup\$ @Anixias As I wrote, whenever the electric connections get changed, a new flood-fill is performed. You can just remove any machine which is found in that flood-fill from the grid it was on before and then add it to the new grid. In order to do that efficiently, each machine needs to know which grid it belongs to. \$\endgroup\$
    – Philipp
    Mar 15 '17 at 12:56
  • \$\begingroup\$ @Anixias although... merging two large grids or adding new polse to an existing large grid might be a special case which might be worth handling separately, because they can be handled much more efficiently when you detect them. But you don't know yet if that will be relevant to your game, so I would recommend you to go with a solution which works for you and think about optimizing when you actually recognize a perceivable performance problem during playtesting. \$\endgroup\$
    – Philipp
    Mar 15 '17 at 13:07
  • \$\begingroup\$ You don't need to flood fill when adding a new pole to the electric grid, just look at which networks it's connecting to via other poles and merge them if necessary. Removing nodes, on the other hand, might be a bit harder, as you will need to check if the N other poles connected to a pole are still connected and split them otherwise. Most of the case they will be in the same network, so optimizing for this case will be worth it. \$\endgroup\$
    – Darkhogg
    Mar 15 '17 at 13:30
  • \$\begingroup\$ So here's what I came up with: Example - 125W produced, 120W consumed, and there are 2 batteries. Each battery receives 2.5W. Next Example - 100W produced, 120W consumed. -20W distributed evenly across all batteries, add up all the power taken from batteries to the power produced by power sources. All machines work at produced/consumed speed, clamped at 0% - 100% \$\endgroup\$
    – Anixias
    Mar 15 '17 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.