trainworld_computercraft/computer/13/work.lua
2025-05-28 02:37:55 +02:00

143 lines
3.1 KiB
Lua

pp = require("cc.pretty").pretty_print
require("recipes")
sfx = require("sfx")
require("pathfinding")
use_machine = require("machines")
require("inventory")
keep_stocked = {
kelp = 10,
flint = 10
}
resetAllFluidDevices()
goHome()
print("known recipes:\n")
for k, _ in pairs(recipes) do
write(k)
write(", ")
end
write("\nchoose one: ")
function completion(partial)
list = {}
for k, _ in pairs(recipes) do
if string.sub(k, 1, #partial) == partial then
table.insert(list, string.sub(k, #partial + 1))
end
end
return list
end
input = splitString(read(nil, nil, completion))
target_product = input[1]
copies = input[2] or 1
if recipes[target_product] == nil then
print("I don't know how to make that, sorry")
return
end
-- pp.pretty_print(ingredientsOf(recipes[target_product]))
-- pp.pretty_print(recipes)
function doRecipe(recipe)
inited = false
for i = 1, recipe.repeats do
for _, step in pairs(recipe.steps) do
if inited then
selectItem(recipe.intermediate, true)
else
inited = true
selectItem(recipe.base, false)
end
use_machine[step.machine](step.extra_items, recipe.product, recipe.yield)
-- read()
end
end
goHome()
for i = 1, 16 do
if turtle.getItemCount(i) ~= 0 then
turtle.select(i)
turtle.drop()
end
end
turtle.select(1)
end
work_queue = {}
for i = 1, copies do
table.insert(work_queue, recipes[target_product])
end
function listUncraftable(ingredients)
wait_for = {}
if len(ingredients) == 0 then
return wait_for
end
for name, count in pairs(ingredients) do
print("missing", count, name)
if recipes[name] == nil then
wait_for[name] = count
end
end
return wait_for
end
function tryCreating(recipe)
ingredients = ingredientsOf(recipe)
missing_ingredients = getMissing(ingredients)
if len(missing_ingredients) == 0 then
work_queue[#work_queue] = nil
turtle.select(1)
--peripheral.call("left","playSound","entity.cat.ambient")
-- todo exclude deploy_tool too and make it get its own tool
if recipe.steps[1].machine ~= "craft" and
recipe.steps[1].machine ~= "alloy"
then
for item, count in pairs(ingredients) do
takeItems(item, count)
end
end
doRecipe(current_recipe)
return nil
end
return missing_ingredients
end
while #work_queue > 0 do
-- for item, _count in pairs(getMissing(keep_stocked)) do
-- print("creating", item, "to keep stock up")
-- tryCreating(recipes[item])
-- end
current_recipe = work_queue[#work_queue]
-- ingredients = ingredientsOf(current_recipe)
pp(ingredients)
missing_ingredients = tryCreating(current_recipe)
if missing_ingredients then
wait_for = listUncraftable(missing_ingredients)
while len(wait_for) > 0 do
for name, count in pairs(wait_for) do
print("please supply", count, name, "manually")
end
print("press return to continue, q to quit")
sfx.fail()
if read() == "q" then
return
end
missing_ingredients = getMissing(ingredients)
wait_for = listUncraftable(missing_ingredients)
end
for name, count in pairs(missing_ingredients) do
if recipes[name] then
print("first making", count, name)
table.insert(work_queue, recipes[name])
end
end
end
end
print("done!")
sfx.success()