Foundry Dev Blog #18 - New Energy System
Hope everybody enjoys the holidays and is having a good weekend in generally difficult times.
Energy System Overhaul
Since the 0.3 delay continued I decided to add in another big change that was not originally planned to be in 0.3, the energy system overhaul. I know most of you would prefer finally getting your hands on 0.3 instead of us always increasing scope, but I can tell you that we're actually getting closer. The art department still has a lot to do with the new underground mining, but we should finish the art of all the other things this week and that would mean that we could push an experimental build for public testing soon (with placeholder art for the underground mining).
So about the new energy system: I've spent the last three weeks almost exclusively on it rewriting it over and over again since I just wasn't happy with all the iterations. I'll start by listing the problems of the old energy system:
If your power is insufficient, some machines will work as planned and some won't work at all. This is pretty ugly and leads to people not instantly seeing that something isn't working anymore and primes an annoying surprise when something isn't produced anymore.
The power grid frame that shows the details simply wasn't accurate. I don't want to go too much into detail here, but basically everything got summed up and showed as one thing, which was mostly correct but for some cases it did not work at all and showed wrong data. The main reason for this were the transformer hierarchies and throughput constraints.
Internally transformers were just batteries that got charged by the High Voltage Grid (HVG) and drained by the Low Voltage Grid (LVG). A bad design choice that caused wrong-looking behaviors in some edge cases. For example generators would run and charge the transformer (until it was full) while there was no power consumer attached leaving people surprised where the energy goes (we tried to hide that transformers are actually charged, so that wasn't helping either).
Performance was not good enough to power what I have in mind for the endgame or high-complexity mods.
Let's see how I tackled all of those issues above, point by point:
I switched the system from a machine on/off state to a system where the machines work slower if satisfaction is below 100%. So if you only have half the required power your machines will run at half speed. This makes everything consistent. No more single machines not working because you have 99% satisfaction.
Removed the global summary of all values on the top and instead showing satisfaction and production statistics per grid as visual bars inside the frame:
Top bar shows the satisfaction inside a grid or for the transformers the throughput limit. While for power producers it shows how much of its maximum output is used. HVG is always prioritized over LVG as the biomass generator is only for the very early game
Transformers being batteries was one of the toughest changes and probably took me most of the time but eventually I came up with an algorithm that eliminates the internal batteries.
I was able to remove batteries and transformers completely from the lockstep simulation's tick update, making both have a O(1) complexity, meaning the amount of those machines has no impact on the per tick simulation performance. It still has an impact when the grid is modified as a one-time-cost but this is generally pretty fast. Additionally to this I was able to reduce the performance cost of the machines drawing power by 99.x%. Before the update every machine would basically ask the energy system for power which required lots of checks. Now the machines just use the satisfaction value of the previous tick and register what they would like to draw without caring if it's possible. At the end of the tick a new satisfaction value is calculated to be used in the upcoming tick. That means the energy system simulation is delayed by one tick (16 milliseconds) - which does not matter as it still is correct.
The overhaul also comes with a new limitation: A single LVG must not be supplied by two different HVG or otherwise they will short-circuit and you need to manually restart the transformers (after you fixed the issue, otherwise it will happen again). Connections itself with offline grids or disabled transformers are fine, so you shouldn't have many problems with re-architecting grid layouts. It only short-circuits when multiple transformers really draw power from different grids. The main reason for this is an algorithmic limitation but after doing some research I found out that this would also be the case in reality unless you have special synchronization transformer substations.
Gameplay-wise nothing really changed except for the new limitation above.
So long, - mrmcd