1. Subtract resources consumed by population (food, etc). If insufficient resources, then they starve.
2. Calculate the maintenance costs from all ships and buildings
3. Subtract the maintenance costs from available resources
4. If insufficient resources for maintenance, then disable buildings that require that resource, if that is still not enough, place the active squadrons that is idling adjacent to friendly system into reserves (different maintenance costs between active and reserve), if that still is not sufficient, scrap buildings that used that resource in its development, and add the recycled resources, if that is still not sufficient, scrap the ships in reserve that yields that resource, if that is still not enough, scrap the equipment from active squadrons that are not adjacent to friendly systems that yields that resource. At this point, there won't be any buildings or ships with equipment that consume that resource, so the economy is now stabilized.
5. Calculate the optimal consumption from all planets that are not blockaded. - This step is mostly done, don't have check for blockade yet. It was a bit tricky handling multiple races with different region space usage and sliders for each region type, but I think this was the hardest step.
6. Compare the total consumption to available resources, and assign percentage to the resources that are not sufficient to cover the consumption. For example, if your empire needs 20 Ores, but only have 10 Ores, then set the Ores to be 50% for all of your empire's consumption.
7. Finally produce outputs, using the penalty. For each region that consumes one or more resource, check to see if any have a penalty (i.e. 50%), and take the lowest percentage, both consumption and output are multiplied by that penalty. So Industry that consumes 10 Ores and generates 10 Industry will now only generate 5 industry, and consumes 5 ores. If a bioorganic factory have 75% penalty in Ores, and 50% penalty in food, it will produce and consume with 50% penalty. The outputs are then added to the empire's resources for next turn, it is not used in current turn's consumption.
8. Any remaining resources that are not consumed this turn (and is from current turn, not the new resources just generated) are then added to the empire and planet projects. For example, Research Points are not consumed in step 7, so you have 100 research points and 25 radioactives left, it is then assigned to the Empire Projects where research are done. Let's say that you have two research projects in Projects, first one costs 50 research points and second costs 75 research points, but also 50 radioactives. It assigns the resources to the first project, which then deducts 50 RP, completing the project. Then it assigns the remaining resources to the next project, which uses up all of research points and radioactives, leaving it with 25 RP and 25 radioactives to complete the project. Another example is if first costs 200 RP, and second costs 100 RP and 50 radioactives, and you have 100 RP and 25 radioactives, the first project consumes all of the RP, leaving 100 RP before completion, and the second project consumes the 25 radioactives, leaving 25 before completion, but didn't get any RP, so it still have 100 RP before completion.
This is a cascading system, and you can change the order of projects at any time to prioritize one above others if needed. Same will be of system and planet development. System development builds ships and other system-wide structures, while planet development builds buildings and terraforms.
9. Any remaining resources will be finally stored or converted if it is not storable for next turn.
Whew! The hardest step is done, with calculations for optimal consumption. I can now copy the algorithm for step 7. The rest of the steps are easy to do, except for 8 which will change the research system. Then I will implement the UI for regions in planets. At this point, I will finally upload the source code.