feat: generalize recipe handling

This commit is contained in:
Emerson Rosen-Jones 2026-01-03 00:01:11 -05:00
parent 0f1ace9116
commit 83f2ec6012

View file

@ -40,20 +40,32 @@ local PRODUCTS = {
-- these will be crafted when needed -- these will be crafted when needed
local RECIPES = { local RECIPES = {
["minecraft:iron_nugget"] = { ["minecraft:iron_nugget"] = {
{"minecraft:iron_nugget", 60}, ingredients = {
{"minecraft:gravel", 24}, {"minecraft:iron_nugget", 60},
{"minecraft:gravel", 24},
},
addr = PRESS_ADDR,
}, },
["minecraft:gold_nugget"] = { ["minecraft:gold_nugget"] = {
{"minecraft:gold_nugget", 64}, ingredients = {
{"minecraft:gravel", 64}, {"minecraft:gold_nugget", 64},
{"minecraft:gravel", 64},
},
addr = PRESS_ADDR,
}, },
["create:zinc_nugget"] = { ["create:zinc_nugget"] = {
{"create:zinc_nugget", 64}, ingredients = {
{"minecraft:gravel", 32}, {"create:zinc_nugget", 64},
{"minecraft:gravel", 32},
},
addr = PRESS_ADDR,
}, },
["create:copper_nugget"] = { ["create:copper_nugget"] = {
{"create:copper_nugget", 63}, ingredients = {
{"minecraft:gravel", 18}, {"create:copper_nugget", 63},
{"minecraft:gravel", 18},
},
addr = PRESS_ADDR,
}, },
} }
@ -129,10 +141,10 @@ function getAmounts (storage_f)
return result return result
end end
function getNumCraftable (recipe, current_stock) function getNumCraftable (ingredients, current_stock)
local result = nil local result = nil
local item, count local item, count
for _, ingredient in ipairs(recipe) do for _, ingredient in ipairs(ingredients) do
item, count = ingredient[1], ingredient[2] item, count = ingredient[1], ingredient[2]
if current_stock[item] == nil then return 0 end if current_stock[item] == nil then return 0 end
local num = math.floor(current_stock[item] / count) local num = math.floor(current_stock[item] / count)
@ -151,7 +163,7 @@ function craftRecipe (ticker, recipe, count)
local createRequest local createRequest
createRequest = function (recipe, i) createRequest = function (recipe, i)
if i == nil then i = 1 end if i == nil then i = 1 end
local ingredient = recipe[i] local ingredient = recipe.ingredients[i]
if ingredient == nil then return end if ingredient == nil then return end
local item = { local item = {
name = ingredient[1], name = ingredient[1],
@ -160,7 +172,7 @@ function craftRecipe (ticker, recipe, count)
return item, createRequest(recipe, i + 1) return item, createRequest(recipe, i + 1)
end end
while count > 0 do while count > 0 do
ticker.requestFiltered(PRESS_ADDR, createRequest(recipe)) ticker.requestFiltered(recipe.addr, createRequest(recipe))
os.sleep(1) os.sleep(1)
count = count - 1 count = count - 1
end end
@ -189,7 +201,7 @@ function run (ticker, export_storage, current_modes)
if mode[1] == "recipe" then if mode[1] == "recipe" then
-- make as many recipes as you can with current stock -- make as many recipes as you can with current stock
local recipe = RECIPES[item] local recipe = RECIPES[item]
local count = getNumCraftable(recipe, stock_amounts) local count = getNumCraftable(recipe.ingredients, stock_amounts)
craftRecipe(ticker, recipe, count) craftRecipe(ticker, recipe, count)
elseif mode[1] == "export" then elseif mode[1] == "export" then
local export_amt = export_amounts[item] local export_amt = export_amounts[item]