88 lines
2 KiB
Lua
88 lines
2 KiB
Lua
pp = require("cc.pretty").pretty_print
|
|
require("stringshit")
|
|
recipes = {}
|
|
|
|
function read_recipe(file)
|
|
product = file.readLine()
|
|
if product == nil then return nil end
|
|
base = nil
|
|
intermediate = nil
|
|
repeats = 1
|
|
yield = 1
|
|
while 1 do
|
|
line = file.readLine()
|
|
if string.sub(line, 1, 5) == "base " then
|
|
base = string.sub(line, 6)
|
|
elseif string.sub(line, 1, 13) == "intermediate " then
|
|
intermediate = string.sub(line, 14)
|
|
elseif string.sub(line, 1, 7) == "repeat " then
|
|
repeats = tonumber(string.sub(line, 8))
|
|
elseif string.sub(line, 1, 6) == "yield " then
|
|
yield = tonumber(string.sub(line, 7))
|
|
elseif line == "steps:" then
|
|
break
|
|
end
|
|
end
|
|
steps = {}
|
|
while 1 do
|
|
line = file.readLine()
|
|
if line == "" or line == nil then
|
|
break
|
|
end
|
|
|
|
words = splitString(line)
|
|
extra_items = {}
|
|
for i = 2, #words do
|
|
itemdata = splitString(words[i], ":")
|
|
table.insert(extra_items, {
|
|
name = itemdata[1],
|
|
count = tonumber(itemdata[2] or 1)
|
|
})
|
|
end
|
|
table.insert(steps, {
|
|
machine = words[1],
|
|
extra_items = extra_items
|
|
})
|
|
end
|
|
return {
|
|
product = product,
|
|
base = base,
|
|
yield = yield,
|
|
intermediate = intermediate or base,
|
|
repeats = repeats,
|
|
steps = steps
|
|
}
|
|
end
|
|
|
|
function load_recipes()
|
|
local file = fs.open("recipes.txt", "r")
|
|
if not file then
|
|
print("error: no recipes found")
|
|
return
|
|
end
|
|
recipes = {}
|
|
while 1 do
|
|
local r = read_recipe(file)
|
|
if r == nil then break end
|
|
recipes[r.product] = r
|
|
end
|
|
end
|
|
|
|
function ingredientsOf(recipe)
|
|
items = {}
|
|
for step_index = 1,#recipe.steps do
|
|
step = recipe.steps[step_index]
|
|
for _,item in pairs(step.extra_items) do
|
|
if item.name ~= "nil" then
|
|
old_sum = items[item.name] or 0
|
|
items[item.name] = old_sum + item.count * recipe.repeats
|
|
end
|
|
end
|
|
end
|
|
if recipe.base then
|
|
items[recipe.base] = (items[recipe.base] or 0) + 1
|
|
end
|
|
return items
|
|
end
|
|
|
|
load_recipes()
|