trainworld_computercraft/computer/13/work.lua
2025-05-22 20:51:25 +02:00

154 lines
3.4 KiB
Lua

pp = require("cc.pretty")
require("keep_stocked")
require("recipes")
sfx = require("sfx")
require("pathfinding")
use_machine = require("machines")
require("inventory")
go_to(vector.new(0,0,0), "south")
recipes = load_recipes()
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)
-- read()
end
end
go_to(vector.new(0,0,0), "south")
for i = 1, 16 do
if turtle.getItemCount(i) ~= 0 then
turtle.select(i)
turtle.drop()
end
end
turtle.select(1)
end
todo = {}
for i = 1,copies do
table.insert(todo, 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
todo[#todo] = nil
turtle.select(1)
for item, count in pairs(ingredients) do
takeItems(item, count)
end
doRecipe(current_recipe)
-- for item, min_count in pairs(getMissing(keep_stocked)) do
-- table.insert(todo, recipes[item])
-- end
return nil
end
return missing_ingredients
end
while #todo > 0 do
-- for item, _count in pairs(getMissing(keep_stocked)) do
-- print("creating", item, "to keep stock up")
-- tryCreating(recipes[item])
-- end
current_recipe = todo[#todo]
-- ingredients = ingredientsOf(current_recipe)
pp.pretty_print(ingredients)
missing_ingredients = tryCreating(current_recipe)
if missing_ingredients then
-- missing_ingredients = getMissing(ingredients)
-- if len(missing_ingredients) == 0 then
-- todo[#todo] = nil
-- turtle.select(1)
-- for item, count in pairs(ingredients) do
-- takeItems(item, count)
-- end
-- doRecipe(current_recipe)
-- for item, min_count in pairs(getMissing(keep_stocked)) do
-- table.insert(todo, recipes[item])
-- end
-- else
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)
-- for i = 1,count do
table.insert(todo, recipes[name])
-- end
end
end
end
-- print("aaa")
-- read()
end
print("done!")
sfx.success()