Page 1 of 1

Factorio Simulation

Posted: Fri Sep 18, 2020 4:35 pm
by honzik_hrom
Hello there!
I wanted to ask, whether someone knows a good lua to json transformer of factorio's data.

Why I am asking: We have a programming lecture. Until last year the project was to simulate and then optimize StarCraft2. Students had to do a forward simulation of the game. Basically the simulation answers the question: How long does it take to build, for example, SCV, Barrack, SupplyDepot, Marine, Marine, Marine.
So given things to build the simulator should tell you how long does it take.
The optimization would answer a different question: What is the best buildorder to create 3 Marines. Or in 5 minutes of simulation time, produce as many marines as possible.

The students really like this project. However the techtree of starcraft2 is not deep enough, thus the optimization algorithm is not very hard to create.

Factorio's techtree is more complicated, thus we had the idea to switch to Factorio instead. Besides, it is a better topic for CS students anyway.

For the simulation, I need certain data. What does the creation of a thing cost, and how long does it take. Or in case of the raw materials (stone, iron, copper, coal,...) how often are they produced by the miners. I have peaked into the folders of factorio, and found under "Factorio/data/base/prototypes/recipe" most of the information.
It is all lua code, I'd like to be it in json. I have found online some parsers, however, they seem to be quite old and out of date?

Furthermore I'd like to hear some inside of what makes sense to simulate.
In the case of StarCraft we don't consider a lot of things. For example geometry (we don't care where we build the buildings, we just build them).
In the case of factorio we will have to do simplifications as well.
Geometry is the first candidate which we will ignore, i.e. in the beginning we don't need belts at all, things will just go from one place to another instantly, we don't care about water, or where the actuall resources would be.
The player itself will be also probably heavily abstracted.
There will be no aliens.

Yes, there are lots of questions regarding this project. First and foremost I need to look into the data and find out, what am I missing.

Re: Factorio Simulation

Posted: Fri Sep 18, 2020 6:02 pm
by eradicator
Grab any table2json library such as this one.
Then make a mod and in data-final-fixes do something like

Code: Select all

local json = require('json')
print(json.encode(data.raw.recipe))
Then start factorio and redirect stdout to a file. Scratch off the unnessecary logging bits and you should be done.

Or so i thought...but it seems json as a format can't represent "mixed or invalid key types".

You can print it as a lua table:

Code: Select all

print(serpent.block(data.raw.recipe))
Which will give you lots of this:

Code: Select all

  sulfur = {
    category = "chemistry",
    crafting_machine_tint = {
      primary = {a = 1, b = 0.088999999999999986, g = 0.99499999999999993, r = 1},
      quaternary = {a = 1, b = 0.35, g = 1, r = 0.95399999999999991},
      secondary = {a = 1, b = 0.69099999999999993, g = 0.97400000000000002, r = 1},
      tertiary = {a = 1, b = 0.71399999999999997, g = 0.63799999999999999, r = 0.72299999999999995}
    },
    enabled = false,
    energy_required = 1,
    ingredients = {
      {amount = 30, name = "water", type = "fluid"},
      {amount = 30, name = "petroleum-gas", type = "fluid"}
    },
    name = "sulfur",
    results = {
      {amount = 2, name = "sulfur", type = "item"}
    },
    type = "recipe"
  },

Re: Factorio Simulation

Posted: Fri Sep 18, 2020 7:12 pm
by honzik_hrom
Ah, so this is a valid approach: https://mods.factorio.com/mod/recipelister
I'll try that out.

Re: Factorio Simulation

Posted: Fri Sep 18, 2020 7:39 pm
by eradicator
By the looks of it that mod uses the more complicated approach of using the pre-parsed data that the engine offers after startup. It's a bit more complicated to extract but the format is nicely normalized (data.raw can be a bit chaotic). If you just want the recipe tree and somebody already made the code then lucky you ;). In case you care: Recipe diffculty (normal and expensive) don't exist simultaneously at runtime - you can only get one or the other.

Re: Factorio Simulation

Posted: Mon Sep 21, 2020 10:51 am
by honzik_hrom
Thanks for the information.

The mod worked well. Now I need to analyse the data.

Re: Factorio Simulation

Posted: Thu Sep 24, 2020 12:36 pm
by honzik_hrom
One rather important question.
I cannot find the crafting time information anywhere.
I thought that the mod does a bad job and just doesn't print it out in the JSON file.
However browsing threw the Lua code of the game, I could not find any time relevant information.

Take the assembling-machine-1 : https://wiki.factorio.com/Assembling_ma ... mal%20mode
The crafting time is `0.5`s.
However in the recipe `.local/share/Steam/steamapps/common/Factorio/data/base/prototypes/recipe/demo-recipe.lue` such information is nowhere to be found.

Can anybody tell me where the crafting time information is hidden, or how it is computed?

Re: Factorio Simulation

Posted: Thu Sep 24, 2020 12:43 pm
by Bilka
In the linked recipe lister mod it's most likely output via https://lua-api.factorio.com/latest/Lua ... ype.energy. In the file you linked it's https://wiki.factorio.com/Prototype/Rec ... y_required with the default of 0.5 as per the linked docs.

Re: Factorio Simulation

Posted: Thu Sep 24, 2020 1:07 pm
by eradicator
Just one of the things that is "a bit chaotic" about data stage -> Anything that has a implicit default value isn't visible. Result_count, energy, probably some other things that might not be too relevant for recipes though.

Re: Factorio Simulation

Posted: Fri Sep 25, 2020 7:55 am
by honzik_hrom
Ah, got it, inside a recipe time is energy (if assembled with 1 speed, i.e. player's crafting speed, oil-refinery or chemical-plant (without modules)) and the default value is 0.5.

This is a huge help, thanks!