diff --git a/computer/13/garbage/evil.lua b/computer/13/garbage/evil.lua new file mode 100644 index 0000000..8f437c3 --- /dev/null +++ b/computer/13/garbage/evil.lua @@ -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) diff --git a/computer/13/make_belts.lua b/computer/13/garbage/make_belts.lua similarity index 100% rename from computer/13/make_belts.lua rename to computer/13/garbage/make_belts.lua diff --git a/computer/13/inventory.lua b/computer/13/inventory.lua index edfca6f..c0276fa 100644 --- a/computer/13/inventory.lua +++ b/computer/13/inventory.lua @@ -1,7 +1,16 @@ pp = require("cc.pretty") -pFront = peripheral.wrap("front") -pDown = peripheral.wrap("bottom") +function pFront(fn, ...) + 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) for i, v in pairs(item_list) do @@ -12,31 +21,8 @@ function findItems(item_list, target) return nil 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) - local item_list = pFront.list() + local item_list = pFront("list") local slot = findItems(item_list, type) if slot == nil then @@ -44,18 +30,31 @@ function takeItems(type, count) return false end - empty_slot = pFront.size() - -- todo error if not empty - pFront.pullItems("front", 1, 64, empty_slot) -- empty first slot - pFront.pullItems("front", slot, 64, 1) -- get target item to first slot - pFront.pullItems("front", empty_slot, 64, slot) -- empty last slot for next time + if slot ~= 1 then + local size = pFront("size") + local empty_slot = nil + for i = 1, size do + 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) end function selectItem(name, has_nbt) has_nbt = has_nbt or false if name == nil or name == "nil" then - for i = 1,16 do + for i = 1, 16 do if turtle.getItemCount(i) == 0 then turtle.select(i) return @@ -64,7 +63,7 @@ function selectItem(name, has_nbt) printError("no empty slot found") return end - for i = 1,16 do + for i = 1, 16 do detail = turtle.getItemDetail(i, true); if detail and stripModname(detail.name) == name then if has_nbt == (detail.nbt ~= nil) then @@ -78,10 +77,10 @@ end function getMissing(needed_items) missing = {} - for name,count in pairs(needed_items) do + for name, count in pairs(needed_items) do missing[name] = count end - target_contents = peripheral.call("front", "list") + target_contents = pFront("list") for _, target_item in pairs(target_contents) do for name, missing_count in pairs(missing) do @@ -98,3 +97,23 @@ function getMissing(needed_items) return missing 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 + diff --git a/computer/13/keep_stocked.lua b/computer/13/keep_stocked.lua deleted file mode 100644 index f0520ab..0000000 --- a/computer/13/keep_stocked.lua +++ /dev/null @@ -1,4 +0,0 @@ -keep_stocked = { - kelp = 10, - flint = 10 -} \ No newline at end of file diff --git a/computer/13/machines.lua b/computer/13/machines.lua index 4df90fa..478c137 100644 --- a/computer/13/machines.lua +++ b/computer/13/machines.lua @@ -14,7 +14,7 @@ function mill(extra_items) insertForward(1, item.count) end print(item_count,"items") - while peripheral.call("front", "getItemDetail", 1) do + while pFront("getItemDetail", 1) do sleep(0.1) end for i = 2, 10 do @@ -29,7 +29,7 @@ function deploy(extra_items, nbt) goUp() selectItem(extra_items[1].name, nbt) turtle.dropUp(1) - peripheral.call("front", "pullItem", "top") + pFront("pullItem", "top") goDown() goDown() @@ -39,7 +39,7 @@ function deploy_tool(extra_items) deploy(extra_items, true) goUp() goUp() - peripheral.call("front", "pushItem", "top") + pFront("pushItem", "top") turtle.suckUp() end function furnace(extra_items) @@ -55,7 +55,6 @@ function furnace(extra_items) insertForward(1, item.count) end wait_time = 10 * item_count - -- peripheral.call("front", "") -- TODO refuel sleep(wait_time) takeForward(3) @@ -83,14 +82,7 @@ function mix(extra_items) takeForward(10) end 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 - print(len(extra_items), "extra items") for _, item in pairs(extra_items) do slot = slot + 1 if slot == 4 then diff --git a/computer/13/main.lua b/computer/13/main.lua new file mode 100644 index 0000000..600a21b --- /dev/null +++ b/computer/13/main.lua @@ -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() \ No newline at end of file diff --git a/computer/13/pathfinding.lua b/computer/13/pathfinding.lua index 56cac54..d86096d 100644 --- a/computer/13/pathfinding.lua +++ b/computer/13/pathfinding.lua @@ -1,15 +1,7 @@ -pp = require("cc.pretty") +pp = require("cc.pretty").pretty_print --- if _G.pos == nil then _G.facing = "south" _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) @@ -34,17 +26,9 @@ local vecOf = { 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() if turtle.up() then _G.pos.y = _G.pos.y + 1 - savePos() else printError("failed to go up") printError(pos) @@ -54,7 +38,6 @@ end function goDown() if turtle.down() then _G.pos.y = _G.pos.y - 1 - savePos() else printError("failed to go down") printError(pos) @@ -64,19 +47,16 @@ end function goLeft() turtle.turnLeft() _G.facing = leftOf[_G.facing] - savePos() end function goRight() turtle.turnRight() _G.facing = rightOf[_G.facing] - savePos() end function goForward() if turtle.forward() then _G.pos = _G.pos + vecOf[_G.facing] - savePos() else printError("failed to go forward") printError(pos) @@ -86,7 +66,6 @@ end function goBack() if turtle.back() then _G.pos = _G.pos - vecOf[_G.facing] - savePos() else printError("failed to go backward") printError(pos) @@ -112,7 +91,6 @@ end function go_to(target, face) while target ~= _G.pos do stepTo(target) - -- print(_G.pos, _G.facing) end if face and face ~= _G.facing then if rightOf[_G.facing] == face then diff --git a/computer/13/pos.txt b/computer/13/pos.txt deleted file mode 100644 index e69de29..0000000 diff --git a/computer/13/recipes.lua b/computer/13/recipes.lua index b9faacf..2d64c0e 100644 --- a/computer/13/recipes.lua +++ b/computer/13/recipes.lua @@ -1,4 +1,4 @@ -pp = require("cc.pretty") +pp = require("cc.pretty").pretty_print require("stringshit") recipes = {} @@ -51,18 +51,17 @@ function read_recipe(file) end function load_recipes() - file = fs.open("recipes.txt", "r") + local file = fs.open("recipes.txt", "r") if not file then print("error: no recipes found") return end recipes = {} while 1 do - r = read_recipe(file) + local r = read_recipe(file) if r == nil then break end recipes[r.product] = r end - return recipes end function ingredientsOf(recipe) @@ -80,4 +79,6 @@ function ingredientsOf(recipe) items[recipe.base] = (items[recipe.base] or 0) + 1 end return items -end \ No newline at end of file +end + +load_recipes() diff --git a/computer/13/recipes.txt b/computer/13/recipes.txt index d9f1a45..7172e6a 100644 --- a/computer/13/recipes.txt +++ b/computer/13/recipes.txt @@ -160,3 +160,11 @@ craft birch_planks birch_slab birch_planks birch_planks nil birch_planks birch_p birch_slab steps: 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 \ No newline at end of file diff --git a/computer/13/stringshit.lua b/computer/13/stringshit.lua index 9b4cbbe..2c2f634 100644 --- a/computer/13/stringshit.lua +++ b/computer/13/stringshit.lua @@ -1,3 +1,4 @@ + function splitString(source, sep) sep = sep or " " elements = {} diff --git a/computer/13/test.lua b/computer/13/test.lua deleted file mode 100644 index 8d73ea6..0000000 --- a/computer/13/test.lua +++ /dev/null @@ -1,3 +0,0 @@ -s = require("sfx") -s.success() ---s.fail() diff --git a/computer/13/todo.txt b/computer/13/todo.txt new file mode 100644 index 0000000..9f3e4e0 --- /dev/null +++ b/computer/13/todo.txt @@ -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 diff --git a/computer/13/work.lua b/computer/13/work.lua index 437d8fd..46459bf 100644 --- a/computer/13/work.lua +++ b/computer/13/work.lua @@ -1,11 +1,15 @@ pp = require("cc.pretty") -require("keep_stocked") require("recipes") sfx = require("sfx") require("pathfinding") use_machine = require("machines") require("inventory") +keep_stocked = { + kelp = 10, + flint = 10 +} + go_to(vector.new(0,0,0), "south") recipes = load_recipes() @@ -89,15 +93,13 @@ function tryCreating(recipe) if len(missing_ingredients) == 0 then todo[#todo] = nil turtle.select(1) + -- todo exclude deploy_tool too and make it get its own tool if recipe.steps[1].machine ~= "craft" then for item, count in pairs(ingredients) do takeItems(item, count) end 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 @@ -113,18 +115,6 @@ while #todo > 0 do 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 @@ -141,15 +131,10 @@ while #todo > 0 do 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()