-
How to extract data from game? https://wiki.factorio.com/Console#Write_all_enabled_recipes_to_file
-
What to extract? What is the API? https://lua-api.factorio.com/latest/LuaRecipe.html
-
First: scraper, saves file to
%AppData%\Factorio\script-output
(on Windows):
/c
local recipes = {}
function getItems (list)
local items = {}
for _, item in ipairs(list) do
items[item.name] = {
amount = item.amount,
type = item.type,
probabilistic = item.probability
}
end
return items
end
function getCatalysts (catalysts)
local items = {}
for _, catalyst in ipairs(catalysts) do
items[catalyst.item.name] = catalyst.amount
end
return items
end
for _, recipe in pairs(game.player.force.recipes) do
recipes[recipe.name] = {
category = recipe.category,
subgroup = recipe.subgroup.name,
ingredients = getItems(recipe.ingredients),
group = recipe.group.name,
products = getItems(recipe.products),
catalysts = getCatalysts(recipe.catalysts)
}
end
game.write_file("recipes.lua", serpent.block(recipes) .. "\n", true)
- Weird format? Yes, but I want it in JSON! I know and like JavaScript.
const fs = require('fs')
const lua = fs.readFileSync('recipes.lua', 'utf8')
const json = lua.replace(/ = /g, ': ')
const recipes = (new Function(`return ${json}`))(); // Ugly hack here. Look away
fs.writeFileSync('recipes.json', JSON.stringify(recipes, null, 2))
- Visualization on https://beta.observablehq.com/d/190cf51be8f93b13