-- "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
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()
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