home about the studio about the team luxe game engine

You've successfully subscribed to Studio Any Percent!

Subscribe to Studio Any Percent

Stay up to date! Get all the latest & greatest posts delivered straight to your inbox

mossfield origins - dev #7

power network

The power grid is green

And sometimes red if something lacks power... Note I won't just be using color to distinguish states, this is just debug visualization for myself at the moment!

The power network

It took me a while to reason about how things will work. The actual power system has simple rules: A powered building needs to be adjacent to a building providing power. In the game terms I call that the "direct network", the adjacent squares of a power source.

The above image shows a power source that provides 4 units of power, and there are 5 buildings in the direct network. What happens if you build a power source adjacent to another one? Or build a power line on either side of a building, there's quite a few cases where a decision has to be made about who is the power source and why.

Power sources
To decide what the power network looks like I came up with the following which works well:
- For each power source in the game, add up it's direct network until it runs out of capacity.
- Keep track of the consumers that were assigned, and which are on the direct network but weren't (over load)

Power lines

A power line extends the range of any sources in range by some radius.
The next step is to iterate all the power lines. A power line can have several potential power sources extended via the same node. A power line also has a direct network, as mentioned above, buildings must be adjacent to receive power from it.

Which power source do we give a consumer? My intuition here says that the one with the most capacity, e.g we spread the load onto the sources that have the most available power. So here's the steps for each power line:
- sort the producers in range by available power
- iterate the direct network of the power line, and try to assign it to any producer in range

This will assign them in order of most available power to least, and then each power line will behave differently, as the network has changed by the time it checks.

Consumers without power

Once that loop ends, any consumers that didn't get assigned have no power source assigned (we reset the list before we update the network). Now we know which consumers are offline, and can easily display that in game.

Above in the code there's a _consumer_power_source map. This stores a link between a power consumer and which building is powering it. This is the value that the Power.on(entity) will return elsewhere, true if a source is found and false otherwise. This will block producers when they lack power etc.

The result

I quickly drew some connecting lines and here you can see the effect. A power line = power symbol. A wind symbol = a wind turbine. A wind turbine can only support 4 cells, so the first power source takes the first 4 on it's direct network, and leaves one without. The power line is adjacent to the one that lacks power, and is in range of another generator, so presto, it connects the two.

In motion

I tidied the visuals a little and showed the system at work in the video below, the same setup as described above.


Since I don't want to get stuck on little details just yet, this is working perfectly well. I'll clearly mark buildings that lack power at all times, selecting a power source will show which things are connected, selecting a power line will show which generators it's connected to.

What's important I think initially is knowing that a building lacks power, and being able to poke the grid to find out why. The buildings details will expand on that too when selected (e.g in range of source but over capacity, or no power source in range).

Next up I'm going to divide the day as intended. There's a work phase, a recreation phase, and a night time. I'll send them off to sleep and let them take a break, and if nothing goes too wrong maybe the screenshots will start looking a bit more varied.

Till then!