trainworld_computercraft/computer/13/recipes.lua
2025-06-22 00:08:46 +02:00

134 lines
3 KiB
Lua

pp = require("cc.pretty").pretty_print
require("stringshit")
require("fluids")
recipes = {}
local knownAmounts = {}
knownAmounts["111"] = 9000
knownAmounts["222"] = 18000
knownAmounts["333"] = 27000
knownAmounts["444"] = 36000
knownAmounts["41.6"] = 3375 -- 500mB / 12
knownAmounts["41"] = 3375 -- 500mB / 12
knownAmounts["12"] = 1000
function mb2droplet(amount)
return math.floor(amount * 81 + 0.5)
end
function droplet2mb(amount)
return amount / 81
end
function droplet2string(droplets)
local mb = droplet2mb(droplets)
local out = tostring(math.floor(mb))
-- local frac = math.fmod(mb, 1)
-- if frac > 0 then
-- out = out .. ""
-- end
out = out .. " mB"
return out
end
function parseFluidAmount(text)
-- local parts = splitString(text, "*")
if knownAmounts[text] then
return knownAmounts[text]
end
return mb2droplet(tonumber(text))
end
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 not line then return nil end
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))
if isFluid[product] then
yield = parseFluidAmount(yield)
end
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], ":")
if isFluid[itemdata[1]] and itemdata[2] then
amount = parseFluidAmount(itemdata[2])
else
amount = tonumber(itemdata[2]) or 1
end
table.insert(extra_items, {
name = itemdata[1],
count = amount
})
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()