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

index 5bc5ef8032c1c3689f0091c43d082b16a5039035..0cbe3905c721c64d3ab52c3b00bcbe327be89241 100644 (file)
@@ -135,25 +135,37 @@ end
 -- "u15 r" = 15x up -> turn right
 -- "u1 5r" = invalid
 
+-- commands : t f
+local commands =
+  { ["u"] = turtle.up
+  , ["d"] = turtle.down
+  , ["f"] = turtle.forward
+  , ["b"] = turtle.back
+  , ["l"] = turtle.turnLeft
+  , ["r"] = turtle.turnRight
+  , ["h"] = harvest
+  }
+
 -- runCommand : string -> true | false message
-local runCommand = function(str)
-  local cmd, n, rest = string.match(string.gsub(str, "^%s+", ""), "^(%w)(%d*)(.+)")
+local runCommand
+runCommand = function(str)
+  local c, n, rest = string.match(string.gsub(str, "^%s+", ""), "^(%w)(%d*)(.*)")
+  if not c then return true end
   if n == "" then n = 1 else n = tonumber(n) end
-  for i = 1, n do
-    print(cmd, tostring(i).."/"..tostring(n))
-  end
-  runCommand(rest)
+  for i = 1, n do commands[c]() end
+--     print(cmd, tostring(i).."/"..tostring(n))
+--   end
+  return 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"
+  local cmd = "rf3hfhflfr2"
   print(cmd)
-  runCommand(cmd)
+  return runCommand(cmd)
 end
 
 while true do
@@ -174,9 +186,9 @@ while true do
     turtle.forward()
   elseif key == 83 then -- s, back
     turtle.back()
-  elseif key == 87 then -- a, turn left
+  elseif key == 65 then -- a, turn left
     turtle.turnLeft()
-  elseif key == 87 then -- d, turn right
+  elseif key == 68 then -- d, turn right
     turtle.turnRight()
   elseif key == 32 then -- Space, up
     turtle.up()
@@ -185,6 +197,6 @@ while true do
   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
+    if char == "y" then sequence() else print("Aborted") end
   end
 end