vines/turtle.lua: command strings testing
authorOlav Bakke Svendsen <mail@olavbs.no>
Tue, 14 May 2024 09:56:37 +0000 (11:56 +0200)
committerOlav Bakke Svendsen <mail@olavbs.no>
Tue, 14 May 2024 09:56:37 +0000 (11:56 +0200)
vines/turtle.lua

index 1c3293fd5b517a9dc743dcd88e57dae1652bbdf1..5bc5ef8032c1c3689f0091c43d082b16a5039035 100644 (file)
@@ -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