This commit is contained in:
Crispy 2025-05-23 15:45:14 +02:00
parent c78597a8fb
commit 760729ee4b
14 changed files with 126 additions and 102 deletions

View file

@ -0,0 +1,21 @@
sfx = require("sfx")
return
parallel.waitForAll(
function()
while true do
sleep(math.random(10, 100))
if math.random(10) > 5 then
sfx.fail()
else
sfx.success()
end
event = { os.pullEventRaw() }
if event[1] == "terminate" then
shell.exit()
os.shutdown()
end
end
end, function()
term.setCursorPos(1, 1)
shell.run("shell")
end)

View file

@ -1,7 +1,16 @@
pp = require("cc.pretty") pp = require("cc.pretty")
pFront = peripheral.wrap("front") function pFront(fn, ...)
pDown = peripheral.wrap("bottom") return peripheral.call("front", fn, unpack(arg))
end
function pBottom(fn, ...)
return peripheral.call("bottom", fn, unpack(arg))
end
function pTop(fn, ...)
return peripheral.call("top", fn, unpack(arg))
end
function findItems(item_list, target) function findItems(item_list, target)
for i, v in pairs(item_list) do for i, v in pairs(item_list) do
@ -12,31 +21,8 @@ function findItems(item_list, target)
return nil return nil
end end
-- uses selected item
function insertForward(slot, count)
turtle.dropDown(count)
pFront.pullItems("bottom", 1, 64, slot)
end
function takeForward(slot)
-- pDown.pullItems("front", slot, 64, 1)
peripheral.call("bottom", "pullItems", "front", slot or 1)
turtle.suckDown()
end
function insertDepot(count)
turtle.dropDown(count)
peripheral.call("front", "pullItem", "bottom")
end
function takeDepot()
peripheral.call("front", "pushItem", "bottom")
turtle.suckDown()
end
function takeItems(type, count) function takeItems(type, count)
local item_list = pFront.list() local item_list = pFront("list")
local slot = findItems(item_list, type) local slot = findItems(item_list, type)
if slot == nil then if slot == nil then
@ -44,11 +30,24 @@ function takeItems(type, count)
return false return false
end end
empty_slot = pFront.size() if slot ~= 1 then
-- todo error if not empty local size = pFront("size")
pFront.pullItems("front", 1, 64, empty_slot) -- empty first slot local empty_slot = nil
pFront.pullItems("front", slot, 64, 1) -- get target item to first slot for i = 1, size do
pFront.pullItems("front", empty_slot, 64, slot) -- empty last slot for next time if pFront("getItemDetail", i) == nil then
empty_slot = i
break
end
end
if empty_slot == nil then
printError("no empty slot in chest, pls fix")
exit()
end
if empty_slot ~= 1 then
pFront("pullItems", "front", 1, 64, empty_slot) -- empty first slot
end
pFront("pullItems", "front", slot, 64, 1) -- get target item to first slot
end
return turtle.suck(count) return turtle.suck(count)
end end
@ -81,7 +80,7 @@ function getMissing(needed_items)
for name, count in pairs(needed_items) do for name, count in pairs(needed_items) do
missing[name] = count missing[name] = count
end end
target_contents = peripheral.call("front", "list") target_contents = pFront("list")
for _, target_item in pairs(target_contents) do for _, target_item in pairs(target_contents) do
for name, missing_count in pairs(missing) do for name, missing_count in pairs(missing) do
@ -98,3 +97,23 @@ function getMissing(needed_items)
return missing return missing
end end
function insertForward(slot, count)
turtle.dropDown(count)
pFront("pullItems", "bottom", 1, 64, slot)
end
function takeForward(slot)
pBottom("pullItems", "front", slot or 1)
turtle.suckDown()
end
function insertDepot(count)
turtle.dropDown(count)
pFront("pullItem", "bottom")
end
function takeDepot()
pFront("pushItem", "bottom")
turtle.suckDown()
end

View file

@ -1,4 +0,0 @@
keep_stocked = {
kelp = 10,
flint = 10
}

View file

@ -14,7 +14,7 @@ function mill(extra_items)
insertForward(1, item.count) insertForward(1, item.count)
end end
print(item_count,"items") print(item_count,"items")
while peripheral.call("front", "getItemDetail", 1) do while pFront("getItemDetail", 1) do
sleep(0.1) sleep(0.1)
end end
for i = 2, 10 do for i = 2, 10 do
@ -29,7 +29,7 @@ function deploy(extra_items, nbt)
goUp() goUp()
selectItem(extra_items[1].name, nbt) selectItem(extra_items[1].name, nbt)
turtle.dropUp(1) turtle.dropUp(1)
peripheral.call("front", "pullItem", "top") pFront("pullItem", "top")
goDown() goDown()
goDown() goDown()
@ -39,7 +39,7 @@ function deploy_tool(extra_items)
deploy(extra_items, true) deploy(extra_items, true)
goUp() goUp()
goUp() goUp()
peripheral.call("front", "pushItem", "top") pFront("pushItem", "top")
turtle.suckUp() turtle.suckUp()
end end
function furnace(extra_items) function furnace(extra_items)
@ -55,7 +55,6 @@ function furnace(extra_items)
insertForward(1, item.count) insertForward(1, item.count)
end end
wait_time = 10 * item_count wait_time = 10 * item_count
-- peripheral.call("front", "")
-- TODO refuel -- TODO refuel
sleep(wait_time) sleep(wait_time)
takeForward(3) takeForward(3)
@ -83,14 +82,7 @@ function mix(extra_items)
takeForward(10) takeForward(10)
end end
function craft(extra_items) function craft(extra_items)
-- for i = 1, 16 do
-- if turtle.getItemCount(i) ~= 0 then
-- turtle.select(i)
-- turtle.drop()
-- end
-- end
local slot = 0 local slot = 0
print(len(extra_items), "extra items")
for _, item in pairs(extra_items) do for _, item in pairs(extra_items) do
slot = slot + 1 slot = slot + 1
if slot == 4 then if slot == 4 then

14
computer/13/main.lua Normal file
View file

@ -0,0 +1,14 @@
pretty = require("cc.pretty").pretty_print
require("recipes")
sfx = require("sfx")
require("pathfinding")
require("inventory")
keep_stocked = {
kelp = 10,
flint = 10
}
go_to(vector.new(0,0,0), "south")
recipes = load_recipes()

View file

@ -1,15 +1,7 @@
pp = require("cc.pretty") pp = require("cc.pretty").pretty_print
-- if _G.pos == nil then
_G.facing = "south" _G.facing = "south"
_G.pos = vector.new(0,0,0) _G.pos = vector.new(0,0,0)
-- file = fs.open("pos.txt", "r")
-- data = splitString(file.readAll())
-- _G.pos.x = tonumber(data[1])
-- _G.pos.y = tonumber(data[2])
-- _G.pos.z = tonumber(data[3])
-- _G.facing = data[4]
-- end
local up = vector.new(0,1,0) local up = vector.new(0,1,0)
@ -34,17 +26,9 @@ local vecOf = {
west = vector.new(-1,0,0), west = vector.new(-1,0,0),
} }
function savePos()
-- fs.delete("pos.txt")
-- file = fs.open("pos.txt", "w")
-- file.write(_G.pos.x .. " " .. _G.pos.y .. " " .. _G.pos.z .. " " .. _G.facing)
end
function goUp() function goUp()
if turtle.up() then if turtle.up() then
_G.pos.y = _G.pos.y + 1 _G.pos.y = _G.pos.y + 1
savePos()
else else
printError("failed to go up") printError("failed to go up")
printError(pos) printError(pos)
@ -54,7 +38,6 @@ end
function goDown() function goDown()
if turtle.down() then if turtle.down() then
_G.pos.y = _G.pos.y - 1 _G.pos.y = _G.pos.y - 1
savePos()
else else
printError("failed to go down") printError("failed to go down")
printError(pos) printError(pos)
@ -64,19 +47,16 @@ end
function goLeft() function goLeft()
turtle.turnLeft() turtle.turnLeft()
_G.facing = leftOf[_G.facing] _G.facing = leftOf[_G.facing]
savePos()
end end
function goRight() function goRight()
turtle.turnRight() turtle.turnRight()
_G.facing = rightOf[_G.facing] _G.facing = rightOf[_G.facing]
savePos()
end end
function goForward() function goForward()
if turtle.forward() then if turtle.forward() then
_G.pos = _G.pos + vecOf[_G.facing] _G.pos = _G.pos + vecOf[_G.facing]
savePos()
else else
printError("failed to go forward") printError("failed to go forward")
printError(pos) printError(pos)
@ -86,7 +66,6 @@ end
function goBack() function goBack()
if turtle.back() then if turtle.back() then
_G.pos = _G.pos - vecOf[_G.facing] _G.pos = _G.pos - vecOf[_G.facing]
savePos()
else else
printError("failed to go backward") printError("failed to go backward")
printError(pos) printError(pos)
@ -112,7 +91,6 @@ end
function go_to(target, face) function go_to(target, face)
while target ~= _G.pos do while target ~= _G.pos do
stepTo(target) stepTo(target)
-- print(_G.pos, _G.facing)
end end
if face and face ~= _G.facing then if face and face ~= _G.facing then
if rightOf[_G.facing] == face then if rightOf[_G.facing] == face then

View file

View file

@ -1,4 +1,4 @@
pp = require("cc.pretty") pp = require("cc.pretty").pretty_print
require("stringshit") require("stringshit")
recipes = {} recipes = {}
@ -51,18 +51,17 @@ function read_recipe(file)
end end
function load_recipes() function load_recipes()
file = fs.open("recipes.txt", "r") local file = fs.open("recipes.txt", "r")
if not file then if not file then
print("error: no recipes found") print("error: no recipes found")
return return
end end
recipes = {} recipes = {}
while 1 do while 1 do
r = read_recipe(file) local r = read_recipe(file)
if r == nil then break end if r == nil then break end
recipes[r.product] = r recipes[r.product] = r
end end
return recipes
end end
function ingredientsOf(recipe) function ingredientsOf(recipe)
@ -81,3 +80,5 @@ function ingredientsOf(recipe)
end end
return items return items
end end
load_recipes()

View file

@ -160,3 +160,11 @@ craft birch_planks birch_slab birch_planks birch_planks nil birch_planks birch_p
birch_slab birch_slab
steps: steps:
craft birch_planks birch_planks birch_planks craft birch_planks birch_planks birch_planks
computer_normal
steps:
craft andesite_alloy andesite_alloy andesite_alloy andesite_alloy polished_rose_quartz andesite_alloy andesite_alloy glass_pane andesite_alloy
glass_pane
steps:
craft glass glass glass glass glass glass

View file

@ -1,3 +1,4 @@
function splitString(source, sep) function splitString(source, sep)
sep = sep or " " sep = sep or " "
elements = {} elements = {}

View file

@ -1,3 +0,0 @@
s = require("sfx")
s.success()
--s.fail()

12
computer/13/todo.txt Normal file
View file

@ -0,0 +1,12 @@
sw: fluid basin
hw: spout
sw: spout
casting
stock keeping
multi-item crafting
push items into existing stacks in chest
pull items from multiple stacks if necessary
refuel self
refuel furnace
delivery turtle

View file

@ -1,11 +1,15 @@
pp = require("cc.pretty") pp = require("cc.pretty")
require("keep_stocked")
require("recipes") require("recipes")
sfx = require("sfx") sfx = require("sfx")
require("pathfinding") require("pathfinding")
use_machine = require("machines") use_machine = require("machines")
require("inventory") require("inventory")
keep_stocked = {
kelp = 10,
flint = 10
}
go_to(vector.new(0,0,0), "south") go_to(vector.new(0,0,0), "south")
recipes = load_recipes() recipes = load_recipes()
@ -89,15 +93,13 @@ function tryCreating(recipe)
if len(missing_ingredients) == 0 then if len(missing_ingredients) == 0 then
todo[#todo] = nil todo[#todo] = nil
turtle.select(1) turtle.select(1)
-- todo exclude deploy_tool too and make it get its own tool
if recipe.steps[1].machine ~= "craft" then if recipe.steps[1].machine ~= "craft" then
for item, count in pairs(ingredients) do for item, count in pairs(ingredients) do
takeItems(item, count) takeItems(item, count)
end end
end end
doRecipe(current_recipe) doRecipe(current_recipe)
-- for item, min_count in pairs(getMissing(keep_stocked)) do
-- table.insert(todo, recipes[item])
-- end
return nil return nil
end end
return missing_ingredients return missing_ingredients
@ -113,18 +115,6 @@ while #todo > 0 do
pp.pretty_print(ingredients) pp.pretty_print(ingredients)
missing_ingredients = tryCreating(current_recipe) missing_ingredients = tryCreating(current_recipe)
if missing_ingredients then 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) wait_for = listUncraftable(missing_ingredients)
while len(wait_for) > 0 do while len(wait_for) > 0 do
for name,count in pairs(wait_for) do for name,count in pairs(wait_for) do
@ -141,15 +131,10 @@ while #todo > 0 do
for name, count in pairs(missing_ingredients) do for name, count in pairs(missing_ingredients) do
if recipes[name] then if recipes[name] then
print("first making", count, name) print("first making", count, name)
-- for i = 1,count do
table.insert(todo, recipes[name]) table.insert(todo, recipes[name])
-- end
end end
end end
end end
-- print("aaa")
-- read()
end end
print("done!") print("done!")
sfx.success() sfx.success()