r/howdidtheycodeit • u/Nil4u • Aug 08 '23
Question How did they code the production statistics screen in Factorio?
I'm talking about this if you are not familiar with the game, which I doubt.
I can imagine it must be some database being created in the background. The graphs are then generated over the game ticks.
Is it a SQL database, or do they store it as a json file? What's your idea on how one could build something similar?
11
Upvotes
11
u/kernalphage Mod - Generalist Aug 08 '23
I forget if I read this in an elastisearch blog or a Factorio Friday, but the key to efficient historical data is aggregation.
Iirc each chunk can be (roughly) updated in parallel, and then each chunk's production statistics are aggregated into a single tick's worth of production.
Those tick totals are then placed as data points on the 10s production screen.
Every time a new tick comes in, the old one is evicted (maybe with a Ring Buffer) and added to a "one second production data" point - and once you've got 60 ticks in that point, it's added to the bucket for the Minute tag.
Follow the same algorithm, and adding a new timescale is just one more (maybe 1k data points) buffer; it's not saving the data for every frame that's ever happened.
Kind of like mechanical clocks rolling over to the next digit, if you've ever seen that.
8
u/kaszy Aug 08 '23
I don't really understand why you would want to use database on disk if the running game requires all needed entities in memory to work anyway. Every inserter, every power source, every producer and consumer must be alive in RAM so the game can access them every tick. It's just a matter of grouping and filtering this data from the list of entities, no need for extra source.
7
u/Mr_Noe_ Aug 08 '23
I will add that the Factorio subreddit is a very welcoming place and frequented by the game devs. If you ask over there, then you might get an answer directly from the source or get pointed to any Factorio Friday Facts that might have talked about it.
2
u/stevep98 Aug 08 '23
First of all, not all SQL databases have to use files for storage. They could use memory. For example, https://www.sqlite.org/inmemorydb.html
Secondly, data like this is called 'Time series data', and there are databases which are optimized for that type of data. https://en.wikipedia.org/wiki/Time_series_database
For example, what would happen if you wanted to zoom in or out on a particular part of the data? Time series database can support many millions of data points, but automatically give you a sampling of points if you've zoomed out, and don't need those millions of data points. Also can support rolling averages, automatic deletion of old data, efficient storage.
2
u/MyPunsSuck Aug 09 '23
Were it me, I'd have a Stats singleton/global with a bunch of LogStat(enum stat, int amount) functions that get called each time something does something. It wouldn't be too hard to parallelize. These values increment a value in an array; one array for each stat being tracked.
Each tick, the Stats system points to the oldest index in the array, and nulls it to start counting afresh. To get the stat over the last n seconds, sum the array