Advertisement
Inksaver

go (direct turtle commands) 2020/11/25

Sep 5th, 2020 (edited)
3,069
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.97 KB | None | 0 0
  1. version = 20201125.1115
  2. --[[
  3.     -- pastebin get xQqK3VcK go.lua
  4.     last updated 05/09/2020
  5.     Will auto-download clsTurtle.lua
  6.     Used to move turtle in multiple directions
  7.     Use: go in command prompt
  8.     keys:
  9.     q = quit
  10.     e = slot
  11.     1 - 0 select slot
  12.     w = forward
  13.     a = turnLeft
  14.     d = turnRight
  15.     s = back
  16.     some versions of MC or CC use different key constants:
  17.     1.12.2 keys.one=2, keys.two=3...  keys.zero=11
  18.     1.16.2 keys.one=49 keys.two=50..  keys.zero=48
  19. ]]
  20.  
  21. args = {...} -- eg go r1f5u2
  22.  
  23. local kv = {}
  24. kv[keys.zero]   = "0"
  25. kv[keys.one]    = "1"
  26. kv[keys.two]    = "2"
  27. kv[keys.three]  = "3"
  28. kv[keys.four]   = "4"
  29. kv[keys.five]   = "5"
  30. kv[keys.six]    = "6"
  31. kv[keys.seven]  = "7"
  32. kv[keys.eight]  = "8"
  33. kv[keys.nine]   = "9"
  34.  
  35. function checkArgs()
  36.     local path = ""
  37.     if args[1] ~= nil then
  38.         path = args[1]
  39.     end
  40.     return path
  41. end
  42.  
  43. function checkLibs(libDir, filename)
  44.     local fileExists = false
  45.     if fs.exists(libDir) then
  46.         if not fs.isDir(libDir) then
  47.             fs.move(libDir, libDir.."Renamed")
  48.             fs.makeDir(libDir)
  49.         end
  50.     else
  51.         fs.makeDir(libDir)
  52.     end
  53.     if fs.exists(fs.combine(libDir, filename)) or fs.exists(fs.combine(libDir, filename..".lua")) then
  54.         fileExists = true
  55.     end
  56.     return fileExists
  57. end
  58.  
  59. function clear(setCP)
  60.     term.clear()
  61.     term.setCursorPos(1, 1)
  62.     if setCP then
  63.         term.write("use: f u d b r l s(lot) p(lace) x(dig)")
  64.         term.setCursorPos(1, 2)
  65.     end
  66. end
  67.  
  68. function getCommand()
  69.     local cmd = ""
  70.     local minNum = 0
  71.     local maxNum = 9
  72.     local col, row = term.getCursorPos()
  73.     if keys.one == 49 then  --newer versions
  74.         minNum = 48
  75.         maxNum = 57
  76.     end
  77.     term.write("cmd?_")
  78.     while true do
  79.         local event, key = os.pullEvent("key")
  80.         if key == keys.q then -- quit
  81.             cmd = ""
  82.             break
  83.         elseif key == keys.s then -- user wants to select slot no
  84.             cmd = "s"
  85.             term.setCursorPos(1, row)
  86.             term.write("slot no? "..cmd.."_")
  87.         elseif key == keys.p or key == keys.x then -- user wants to place or excavate
  88.             cmd = keys.getName(key)
  89.             term.setCursorPos(1, row)
  90.             term.write("direction u=0 f=1 d=2? "..cmd.."_")
  91.         elseif key == keys.f or key == keys.b or key == keys.u or key == keys.d or key == keys.l or key == keys.r then
  92.             cmd = keys.getName(key) -- eg f
  93.             term.setCursorPos(1, row)
  94.             term.write("no? "..cmd.."_")
  95.         elseif key >= minNum and key <= maxNum then -- 1 to 0 chosen = 49 to 58
  96.             --check if cmd is empty
  97.             if string.len(cmd) > 0 then -- not empty so build command
  98.                 local c = string.sub(cmd, 1, 1)
  99.                 local num = kv[key]
  100.                 if c == "s" then -- select slot
  101.                     if cmd == "s" then
  102.                         cmd = "s"..num
  103.                         term.setCursorPos(1, row)
  104.                         term.write("0 to 6 or enter? "..cmd.."_")
  105.                     else
  106.                         if key + offset - 1 <= 6 then -- allow 0 to 6 only
  107.                             cmd = cmd..num
  108.                             break
  109.                         end
  110.                     end
  111.                 elseif c == "p" or c == "x" then
  112.                     if key == keys.zero then
  113.                         cmd = c.."0"
  114.                         break
  115.                     elseif key == keys.one or key == keys.two then
  116.                         cmd = c..num
  117.                         break
  118.                     end
  119.                 elseif c == "r" or c == "l" then
  120.                     cmd = cmd..num
  121.                     break
  122.                 elseif  c == "f" or c == "b" or c == "u" or c == "d" or c == "r" or c == "l" then
  123.                     cmd = cmd..num
  124.                     term.setCursorPos(1, row)
  125.                     term.write("0-9 or Enter? "..cmd.."_")
  126.                 end
  127.             end
  128.         elseif key == keys.enter then -- command finished
  129.             break
  130.         end
  131.     end
  132.     clear()
  133.     return cmd
  134. end
  135.  
  136. function main()
  137.     clear(false)
  138.     local doContinue = true
  139.     if not checkLibs("lib", "clsTurtle") then
  140.         -- use pastebin get to download clsTurtle to libs folder
  141.         print("Missing clsTurtle.lua in libs directory")
  142.         print("Attempting to obtain from Pastebin...")
  143.         if shell.run("pastebin","get","tvfj90gK","lib/clsTurtle.lua") then
  144.             print("clsTurtle.lua installed from Pastebin")
  145.             sleep(2)
  146.         else
  147.             print("failed to install clsTurtle.lua from Pastebin")
  148.             doContinue = false
  149.         end
  150.     end
  151.     if doContinue then
  152.         print("Current fuel: "..turtle.getFuelLevel().." / "..turtle.getFuelLimit())
  153.         T = require("lib.clsTurtle"):new()
  154.         local action,  modifier
  155.         local cmd = checkArgs() -- empty string or cmd eg go r1f5u2
  156.         local direction = {"up", "forward", "down"}
  157.         if cmd ~= "" then
  158.             T:go(cmd)
  159.         else
  160.             sleep(2)
  161.             clear(true)
  162.             while true do
  163.                 cmd = string.upper(getCommand())
  164.                 action = string.sub(cmd, 1, 1)
  165.                 modifier = string.sub(cmd, 2)
  166.                 if cmd == "" then
  167.                     break
  168.                 else
  169.                     if action == "S" then --select slot
  170.                         turtle.select(tonumber(modifier))
  171.                     elseif action == "X" then --dig
  172.                         T:dig(direction[modifier + 1])
  173.                     elseif action == "P" then --place
  174.                         --place current selected slot contents
  175.                         --T:place(blockType, damageNo, direction, leaveExisting)
  176.                         local slotContains, slotCount, slotDamage = T:getSlotContains(turtle.getSelectedSlot)
  177.                        
  178.                         print("Place "..slotContains.." count: "..slotCount.." damage: "..tostring(slotDamage).." Enter")
  179.                         read()
  180.                         T:place(slotContains, slotDamage, direction[modifier + 1], false)
  181.                     else
  182.                         T:go(cmd)
  183.                     end
  184.                 end
  185.             end
  186.         end
  187.     end
  188. end
  189.  
  190. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement