Pre-Order the Game!

Desura Digital Distribution

Thursday, August 16, 2012

Consumption code done!

The algorithm for calculating the amount of consumption for each planet is done!  It's about 100 lines of code, and this is just for getting how much a planet can consume if the resources are available.  This economy system is turning out to be more complicated than I thought, but I've broken it down into parts, and I believe that when it's all done, it will be very powerful and flexible.  This is the steps involved in processing a turn for economy:

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.


  1. Man that's a hell of a procedure. Do you keep specs like that in a some sort of design document?

    Can you explain population starvation step? Do all starving people die in a single turn or something less severe like penalty to population growth in MoO 2.

  2. This blog is basically my design document :) that's why I post both ideas and progress here.

    For population starvation, it will sort the planets from most populated to least populated. It then subtracts resources for each planet. When a planet tries to subtract but there are not sufficient, then it uses a reverse growth formula, with more people dying if a planet is full than if it's sparsely populated. Next turn it will report all planets that has starvation in the sitrep.

    This may look complicated, but that's for under the hood, it actually makes things simple for the player to understand. No fancy economy formulaes that the player struggle to grasp, like MoO 3, but the calculations are a bit involved due to the amount of variables. But the concept should be easy to understand.

  3. One minor problem I see. If you have many industries that require multiple resources with multiple penalties, then resources won't be consumed optimally (Step 7). I can come up with a full scenario with multiple planets if you want me to, but I'll summarize for now.

    Say resource A and B are both in demand and have percentages (from Step 6) of 40% and 60% respectively. Also imagine that resource B is also being used in another industry. The percentages in Step 6 are calculated on total demand. But in this short demonstration, B is not being used effectively. All B using industries are capped at 60%, but some run below that level. Since some run below that level (40% in this example) other industries could use this slack.

    With that said, I acknowledge that this isn't a big issue and is something that could be tagged as an acceptable bug and fixed later. In a large empire, I don't imagine this becoming a significant problem unless someone customizes the game with a funky economy. If a funky economy is used, you'd need to do some iterative and/or matrix based solution.
    A way to make this a bit better might be to add a Step 7a that basically redoes Step 7 with the remaining resources. In a single use economy, Step 7a would just verify that Step 7 is complete. In a complicated economy, Step 7a would use remaining resources - I imagine the %age (which would need to be recalculated, so I suppose 7a would need to call the function used for Step 6) for each resource would be much smaller, e.g. ~70% for Step 7, ~5% for Step 7a. This will also depend on how well the player balances his/her economy. If one resource is stuck at ~10% and others ~90%, but both resources are needed for ship building, for example, then Step 7a might need to be repeated several times in order to use all the resources. (An idea would be to make 7a a WHILE type step, i.e. While (Step_6_% > 5 && Step_6_% != Step_6_%_prev) { ...

    Sorry for the extra long post. I probably could have communicated that in less words. Hope this is useful.

  4. Erik, yes, I'm aware of that problem. You can detect it when you increase the slider for a region, and the production/consumption don't go up because it's now capped. It's your responsibility to make sure you're not wasting labors. In a small empire, it will be easy to manage. In a large empire, you can either use the planets screen to manage multiple planets at once, and suffer some wasted labor, or micromanage every planet for optimal output.

    The problem with looping is that it's a lot of CPU work just to get that 1% increase. In this game, there won't be multiple consumption for a region, however the ability is there for modders. When you modify a slider, all the planets are updated with their consumptions/productions, because your change may affect other planets. Looping would cause that to be slow, making dragging slider slow in a huge empire. I will try to optimize the formula, but it will still require that every planet be updated.