Pre-Order the Game!

Desura Digital Distribution

Thursday, August 9, 2012

Resources and Consumption

Here's an update on the resource system:

As you may know, I've added resources in a XML file, so players can create their own resources, whether or not it's preservable (food can't be stored since they only last a year for example, so excess are converted into some money).  Also regions are defined in XML file, so players can create different regions, what restrictions they have (for example, Radioactive Mining regions are restricted to only the planets that have Radioactives special), and what resources they consume or produce.

While this may seem a simple system, I realized that the math behind this is very complicated.  Each planet can have different regions, and different races.  Planet specials and race bonuses may affect the output/consumption.  The more you produce something from a planet, the more that region need to consume.  Resources are interchangeable between planets, so if one planet is a mining powerhouse, it can supply other planets with ores/minerals.

The problem is now this:  How do the game know how much you're producing, and how much you're consuming?  Let's say that there's three regions defined:

Mining - Produces Raw Minerals
Refining - Produces Alloys, Consumes Raw Minerals
Industry - Produces "Production", Consumes Alloys

To correctly calculate the output and consumption in the same turn, it must loop through every planet, checking its output and consumption.  So first loop will discover that Mining produces Raw Minerals, but since there's no raw minerals beforehand, Refining wouldn't be producing anything, and same for Industry.  Then on second loop, it will say "we now have Raw Minerals, which regions use it?" and go through the same process again.  Now Refining produces Alloys.  Third loop will finally determine that Industry can produce Production.

Now, what happens if the player changes slider so that the mining no longer produces Raw Minerals?  It'd have to go through the loops again.  Due to the regions/resources being able to be defined by the player, it could become a complicated economy system with 5 or so iterations before the goods are usable by projects.  Some region may consume two or more resources (for example, bioorganic factory consumes Ores and Food), some region may produce two or more resources (Radioactive Mining will produce radioactives and ores).  Not to consider racial/regional/planet bonuses that affects the whole thing as well.  How many loops do the game need to perform?  How do it know when to stop?

So the system that worked in MoO 2/3 can't work here because in MoO 2/3, the resources are pre-defined (MoO 2 has command points for ships, and BC, MoO 3 has only Mining and Industry, a simple system).  So I'm proposing a different system that will make things easier under the hood, and makes things interesting for the player:  Turn-Based Processing.

What is Turn-Based Processing?  Let's use the above example of Mining, Refining, and Industry.  First turn, you don't have any Raw Minerals, nor Alloys.  So Refining and Industry won't be producing anything.  However, Mining will produce 10 Raw Minerals this turn.  You then hit end turn, and proceed to second turn.  Now you have 10 Raw Minerals, and the game tells the regions that "we have 10 Raw Minerals, tell me what you're making".  It will report that the Refining can produce 10 Alloys, and consumes 10 Raw Minerals, and Mining will produce 10 Raw Minerals.  You hit end turn, the game consumes all the resources by regions, then produces the goods from regions.  You're now on third turn.  You now have 10 Raw Minerals (from mining last turn), and 10 Alloys.  The Industry region now finally can produce "Production" because you now have Alloys.

One interesting side effect from this is that if your mining planet was attacked and destroyed, you won't feel the effects until a few turns later due to the chain of production.

If the resources are not preservable, they last only one turn, long enough to be consumed.  They usually have another resource that they change into (for example, each unit of food left over will be converted to half a BC) on end of second turn.


  1. Though turn based approach is fine, there is not so complex way to process resources in your initial idea. Something like breadth first search algorithm but with all "open nodes" processed in a single iteration. Algorithm revolves around three sets:
    - R, set of all resources (all "nodes")
    - C, set of processed/consumed resources (closed "nodes")
    - A, set of available resources (open "nodes")

    Pseudocode is following (ignore underscores, I'm not sure if multiple whitespaces are shown in comments):

    while C different from R {
    __A = empty
    __for each r in R without C
    ____if C contains all requirements for r, add r to A
    __if A is empty there is circular dependency

    __check what the empire can do with A
    __add elements of A to C

    Note that algorithm fails (infinite loop) if there is circular dependency among the resources. To simplify the code, whole thing can be used before starting the game or while loading a mod to generate list of passes used during the game.

  2. While the node tree might work, there's a couple of issues. First, as you've already pointed out, is the circular dependency. I could add a check for that, but how to gracefully handle that?

    Second, it's not as flexible as the turn-based economy due to everything being done in one turn. The advantages with turn-based economy is that spies can wreck havoc on another empire's economy by either destroying or stealing resources, and that empire can then respond to the change the next turn. If you lost 20 raw minerals to sabotage, then instead of having it processed automatically on the same turn, you now need to handle that this turn. It don't hurt you for just one turn, the effect is felt as it goes down the production chain. Less raw minerals this turn means less alloys next turn, resulting in less industry on the third turn.

    Another thing is that there could be natural disasters, such as famine across planets in the same system, or other events, and instead of having them affect instantly, it affects your production chain. Maybe a star went supernova, and all of your mining planets were in that system, so you immediately commence to build mining regions on other planets before you run out of industry.

    It will feel more like a real economy, instead of just numbers. "Oh, I lost a planet, that means I have x less industry and y less reseaerch immediately". I don't want that, I want "Oh, I lost a planet, what can I do to reduce the impact on my production chain in advance?". I want the economy to be a living thing that requires attention, instead of just "build all buildings and forget". Each building will require upkeep, not just in money, but in other resources as well. Farming buildings will consume raw minerals (fertilizers, etc), too much farming will leave little for refining, for example.

  3. This sounds very cool, That's all I've got to say.

  4. Ou yeah, I like those implications of turn based processing.