Dev Diary 1 - Simulation


One of the biggest struggles I faced with my previous projects is not managing my physics processes properly. Leading to huge hangs and lag-spikes. This time around, I set out to avoid that. (I am aware of some hiccupping if you have lots of different productions going, fixing soon.)

To achieve this, there are only two (non-movement) physics processes running.

-Input_Manager (for player input processing)

-World_Tick_Manager - which is the literal heartbeat of the game.

So much so, that I don't use the built-in pausing that Godot has. When you pause the game, it is simply the world_tick_manager that stops running.

So my goal was to take the 60 physics ticks that Godot uses, and divide the various simulation needs amongst them. Lucky for me, I only need 15 (and possibly less). If this doesn't make any sense, allow me to explain.
Every second, world_tick_manager runs its physics_process function 60 times. So I have a number that counts to 15, and resets. If that number = 1, then do these things, if that number = 2, then do other things. The ticks are as follows:

Tick 1:
    -Check the day of the month, if it's the 1st, calculate the company value, private share values, purge public share values that aren't relevant to the player, and start a new entry in the finances/info menu.

    -Update the current month finances in the finances/info menu.

    -Calculate the date.

    -Update the date in the ui.

Tick 2:

    -Calculate all country's resource generation. (This is the heaviest tick, working on making this lighter)

Tick 3:

    -Management sell overstock.

    -Management take wages.

Tick 4:
    -Simulate all markets. (2nd heaviest tick)

Tick 5:
    -Random specific market flux. (Crashes and spikes)

Tick 6:
    -Refresh the market screen if it's showing.

    -Refresh the library if it's showing. (specifically the market price)

    -Check if there are any info-popups visible, if there are, refresh them. (market prices again)

Tick 7:

    -Calculate expenses for Employee Wages, Employee Benefits, Electricity Usage, Water Usage, and property taxes.

Tick 8:

    -Purge the news panel of entries older than the newest 100.

    -If a country quick-info panel is showing, refresh it. (This is the panel that shows up when you right click on a country)

Tick 9:

    -Go through the export dictionaries, and tick their days down 1.

Tick 10:

    -Go through the export dictionaries, and deliver any that have 0 days left.

Tick 11:

    -Go through the export dictionaries, and load any deliveries that have to go out today.

Tick 12:

    -Generate research for all Engineers and Scientists employed.

Tick 13:

    -Calculate and subtract loan interest.

Tick 14:

    -If public shares are enabled, adjust value based on current sentiment, add to the historical data, refresh charts if they are visible, simulate potential investors buying or selling stock.

Tick 15:

    -Take all money that came in and out and show it to the player +/-

    -Actually add or subtract that amount from the total, and display it to the player.

~

Obviously some of these could be condensed, but I have a new more things to add in there as the project develops.

So, at 16x speed, the world_tick_manager can run through those 15 ticks - 4 times per second. Which is 4 days per second.

Any speed slower than that, it does the 15 ticks right away, and then waits until it has to run again to keep up with the speed of the game. (So at 4x speed, it does the 15 ticks, then counts to 60 - doing nothing, then resets to 0)

So 1x and 16x run at the same speed in the world_tick_manager, and just waits for the next day if it needs to. This way, if I do find that I am overloading one of these ticks, it will be apparent through gameplay no matter what speed we are playing at.

Some movement based physics that happen in the game:

While it appears that the globe is rotating, it is in-fact just the background stars that are rotating.
While in the main menu when first starting up the game, both the background AND the globe are spinning.

When exports are traveling, they are simply just a model that is given it's route, and the model moves along it. The actual calculations for exports have nothing to do with how long it takes for the transports to reach their destination, they are just calculated before hand to be the same. (The movement speed is also changed when the game speed changes so that they move faster or slower and will reach their destination at the same time as the calculations say they will.)

Hoping that's not too confusing. Trying to ride the line between people who know nothing about this stuff, and people who know waaay more than me.

Upcoming:

DD2: Countries

DD3: Fertilities

DD4: Building

DD5: Inventory & Items

DD6: ???

Get Factory Default: Build, Trade, Repeat

Download NowName your own price

Comments

Log in with itch.io to leave a comment.

(1 edit)

Interesting read again, thanks!

If you run into issues in the future, godot also supports multithreading, so you could move some of these calculations to their own threads.

I totally forgot that was a thing. 😄  I'll look into that.