From e8b3d7d83e1a5366c51dd440c16afc3e275a5296 Mon Sep 17 00:00:00 2001 From: Olav Bakke Svendsen Date: Tue, 14 May 2024 11:56:37 +0200 Subject: [PATCH] vines/turtle.lua: command strings testing --- vines/turtle.lua | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/vines/turtle.lua b/vines/turtle.lua index 1c3293f..5bc5ef8 100644 --- a/vines/turtle.lua +++ b/vines/turtle.lua @@ -10,7 +10,7 @@ -- index -- repeat -local automata = periperal.wrap("left") +local automata = peripheral.wrap("left") local shears = { "alloyed:steel_shears" @@ -47,7 +47,7 @@ end -- returns the first empty slot -- if no empty slot is found, returns nil local findEmpty = function() - for i = 1, 16 do if not turtle.getItemDetail() then return i end end + for i = 1, 16 do if not turtle.getItemDetail(i) then return i end end return false, "No empty slot" end @@ -76,7 +76,7 @@ end -- select first slot containing an item from list t if not already selected. -- returns bool. local select = function(t) - local item = getItemDetail() + local item = turtle.getItemDetail() if item and oneOf(t, item.name) then return true else @@ -92,7 +92,7 @@ end -- (this function exists because weak automata cannot digBlock with tools in slots >9) local transferAndSelect = function(i, t) local msg = "Could not transfer and select: " - if i < 1 or i > 16 then return false, msg .. "Index out of range" + if i < 1 or i > 16 then return false, msg .. "Index out of range" end local i_tmp, e = findEmpty() if not i_tmp then return false, msg .. e end local i_from, e = find(t) @@ -106,8 +106,8 @@ local transferAndSelect = function(i, t) turtle.transferTo(i_tmp) turtle.select(i_from) turtle.transferTo(i) - turtle.select(i_tmp) - turtle.transferTo(i_from) +-- turtle.select(i_tmp) +-- turtle.transferTo(i_from) end turtle.select(i) return true @@ -128,7 +128,34 @@ local harvest = function() end +-- commands as strings +-- chars corresponds to actions, when immediatel followed by a number, repeats the action that many times +-- "udfblrh" = up -> down -> forward -> backward -> turn left -> turn right -> harvest +-- "u15r" = 15x up -> turn right +-- "u15 r" = 15x up -> turn right +-- "u1 5r" = invalid + +-- runCommand : string -> true | false message +local runCommand = function(str) + local cmd, n, rest = string.match(string.gsub(str, "^%s+", ""), "^(%w)(%d*)(.+)") + if n == "" then n = 1 else n = tonumber(n) end + for i = 1, n do + print(cmd, tostring(i).."/"..tostring(n)) + end + runCommand(rest) +end + -- for testing purposes + +local sequence = function() + print("pretending to unload stray items") + print("pretending to refuel") + print("pretending to stock up on shears") + local cmd = "rf4h" + print(cmd) + runCommand(cmd) +end + while true do local _, key = os.pullEvent("key") if key == 72 then -- h @@ -155,5 +182,9 @@ while true do turtle.up() elseif key == 340 then -- Shift, down turtle.down() + elseif key == 257 then -- Enter, run sequence + print("Is the turtle indexed properly?\n(press y to confirm)") + local _, char = os.pullEvent("char") + if char = "y" then sequence() else print("Aborted") end end end -- 2.30.2