JerryWester

[ComputerCraft] [WIP] Turtle Control (Local)

Jun 9th, 2020
2,608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.56 KB | None | 0 0
  1. --[[
  2. ~ List of Keys ~
  3.  
  4. W - Move Forward / Move Up (holding left shift) / Move Down (holding left control)
  5. S - Move Back / Move Down (holding left shift) / Move Up (holding left control)
  6. A - Turn Left
  7. D - Turn Right
  8. N - Break In Front / Above (holding left shift) / Below (holding left control)
  9. M - Place In Front / Above (holding left shift) / Below (holding left control)
  10. Arrow Keys - Move Inventory Cursor
  11. R - Refuel
  12. F - Check Fuel Level
  13. E - Exit
  14. ]]
  15. local forwardKey = keys.w
  16. local backKey = keys.s
  17. local turnLeftKey = keys.a
  18. local turnRightKey = keys.d
  19. local upModKey = keys.leftShift
  20. local downModKey = keys.leftCtrl
  21. local breakKey = keys.n
  22. local placeKey = keys.m
  23. local inventoryKey = keys.i
  24. local exitKey = keys.e
  25. local refuelKey = keys.r
  26. local checkFuelKey = keys.f
  27. local selectUpKey = keys.up
  28. local selectDownKey = keys.down
  29. local selectLeftKey = keys.left
  30. local selectRightKey = keys.right
  31. local dropKey = keys.q
  32. local suckKey = keys.z
  33.  
  34. local upMod = false
  35. local downMod = false
  36. local blocks =
  37. {
  38.   top = {
  39.     name = "Empty",
  40.     completeName = "Empty",
  41.     modName = "minecraft",
  42.     metadata = 0,
  43.     state = {}
  44.   },
  45.   front = {
  46.     name = "Empty",
  47.     completeName = "Empty",
  48.     modName = "minecraft",
  49.     metadata = 0,
  50.     state = {}
  51.   },
  52.   bottom = {
  53.     name = "Empty",
  54.     completeName = "Empty",
  55.     modName = "minecraft",
  56.     metadata = 0,
  57.     state = {}
  58.   }
  59. }
  60.  
  61. local function updateBlocks()
  62.   if turtle.detectUp() then
  63.     local _, inspect = turtle.inspectUp()
  64.     inspect.modName = inspect.name:match(".-:"):sub(1, -2)
  65.  
  66.     inspect.name = inspect.name:match(":.+"):sub(2, -1):gsub("_", " "):gsub("(%a)(%a+)", function(a, b) return string.upper(a) .. string.lower(b) end)
  67.  
  68.     inspect.completeName = inspect.name
  69.  
  70.     if inspect.state.variant then
  71.       inspect.completeName = inspect.completeName.." ("..inspect.state.variant:gsub("_", " "):gsub("(%a)(%a+)", function(a, b) return string.upper(a) .. string.lower(b) end)..")"
  72.     end
  73.    
  74.     if inspect.state.color then
  75.       inspect.completeName = inspect.completeName.." ("..inspect.state.color:gsub("_", " "):gsub("(%a)(%a+)", function(a, b) return string.upper(a) .. string.lower(b) end)..")"
  76.     end
  77.     blocks.top = inspect
  78.   else
  79.     blocks.top = {name = "Empty", completeName = "Empty", modName = "minecraft", metadata = 0, state = {}}
  80.   end
  81.   if turtle.detect() then
  82.     local _, inspect = turtle.inspect()
  83.     inspect.modName = inspect.name:match(".-:"):sub(1, -2)
  84.  
  85.     inspect.name = inspect.name:match(":.+"):sub(2, -1):gsub("_", " "):gsub("(%a)(%a+)", function(a, b) return string.upper(a) .. string.lower(b) end)
  86.  
  87.     inspect.completeName = inspect.name
  88.    
  89.     if inspect.state.variant then
  90.       inspect.completeName = inspect.completeName.." ("..inspect.state.variant:gsub("_", " "):gsub("(%a)(%a+)", function(a, b) return string.upper(a) .. string.lower(b) end)..")"
  91.     end
  92.    
  93.     if inspect.state.color then
  94.       inspect.completeName = inspect.completeName.." ("..inspect.state.color:gsub("_", " "):gsub("(%a)(%a+)", function(a, b) return string.upper(a) .. string.lower(b) end)..")"
  95.     end
  96.     blocks.front = inspect
  97.   else
  98.     blocks.front = {name = "Empty", completeName = "Empty", modName = "minecraft", metadata = 0, state = {}}
  99.   end
  100.   if turtle.detectDown() then
  101.     local _, inspect = turtle.inspectDown()
  102.     inspect.modName = inspect.name:match(".-:"):sub(1, -2)
  103.  
  104.     inspect.name = inspect.name:match(":.+"):sub(2, -1):gsub("_", " "):gsub("(%a)(%a+)", function(a, b) return string.upper(a) .. string.lower(b) end)
  105.  
  106.     inspect.completeName = inspect.name
  107.  
  108.     if inspect.state.variant then
  109.       inspect.completeName = inspect.completeName.." ("..inspect.state.variant:gsub("_", " "):gsub("(%a)(%a+)", function(a, b) return string.upper(a) .. string.lower(b) end)..")"
  110.     end
  111.    
  112.     if inspect.state.color then
  113.       inspect.completeName = inspect.completeName.." ("..inspect.state.color:gsub("_", " "):gsub("(%a)(%a+)", function(a, b) return string.upper(a) .. string.lower(b) end)..")"
  114.     end
  115.     blocks.bottom = inspect
  116.   else
  117.     blocks.bottom = {name = "Empty", completeName = "Empty", modName = "minecraft", metadata = 0, state = {}}
  118.   end
  119. end
  120.  
  121. local function getInv()
  122.  
  123. end
  124.  
  125. local function checkFuel()
  126.   print( (turtle.getFuelLevel() / turtle.getFuelLimit()) * 100 .. "% (" .. turtle.getFuelLevel() .. "/" .. turtle.getFuelLimit() .. ")" )
  127. end
  128.  
  129. local function refuel()
  130.   if turtle.refuel(0) then
  131.     turtle.refuel(1)
  132.   end
  133.   checkFuel()
  134. end
  135.  
  136. local function hasValue (tab, val)
  137.     for index, value in ipairs(tab) do
  138.         if value == val then
  139.             return true
  140.         end
  141.     end
  142.  
  143.     return false
  144. end
  145.  
  146. local function init()
  147.   term.clear()
  148.   term.setCursorPos(1,1)
  149.   updateBlocks()
  150. end
  151.  
  152.  
  153.  
  154. local run = true
  155. init()
  156. while run do
  157.   e, p1, p2, p3, p4, p5 = os.pullEvent()
  158.   if e == "key_up" then
  159.  
  160.     if p1 == upModKey then
  161.       upMod = false
  162.     end
  163.     if p1 == downModKey then
  164.       downMod = false
  165.     end
  166.  
  167.     if p1 == forwardKey then
  168.       if turtle.getFuelLevel() <= 0 then
  169.         print("out of fuel")
  170.       else
  171.         if upMod and not downMod then
  172.           if turtle.up() then
  173.             print("up")
  174.           else
  175.             print("block in the way")
  176.           end
  177.         elseif downMod and not upMod then
  178.           if turtle.down() then
  179.             print("down")
  180.           else
  181.             print("block in the way")
  182.           end
  183.         else
  184.           if turtle.forward() then
  185.             print("forward")
  186.           else
  187.             print("block in the way")
  188.           end
  189.         end
  190.       end
  191.     end
  192.  
  193.     if p1 == backKey then
  194.       if turtle.getFuelLevel() <= 0 then
  195.         print("out of fuel")
  196.       else
  197.         if upMod and not downMod then
  198.           if turtle.down() then
  199.             print("down")
  200.           else
  201.             print("block in the way")
  202.           end
  203.         elseif downMod and not upMod then
  204.           if turtle.up() then
  205.             print("up")
  206.           else
  207.             print("block in the way")
  208.           end
  209.         else
  210.           if turtle.back() then
  211.             print("back")
  212.           else
  213.             print("block in the way")
  214.           end
  215.         end
  216.       end
  217.     end
  218.  
  219.     if p1 == breakKey then
  220.       if upMod and not downMod then
  221.         if turtle.digUp() then
  222.           print("Broke Above Block")
  223.         else
  224.           print("Unable To Break")
  225.         end
  226.       elseif downMod and not upMod then
  227.         if turtle.digDown() then
  228.           print("Broke Below Block")
  229.         else
  230.           print("Unable To Break")
  231.         end
  232.       else
  233.         if turtle.dig() then
  234.           print("Broke Front Block")
  235.         else
  236.           print("Unable To Break")
  237.         end
  238.       end
  239.     end
  240.  
  241.     if p1 == placeKey then
  242.       if upMod and not downMod then
  243.         if turtle.placeUp() then
  244.           print("Placed Above Block")
  245.         else
  246.           print("Unable To Place")
  247.         end
  248.       elseif downMod and not upMod then
  249.         if turtle.placeDown() then
  250.           print("Placed Below Block")
  251.         else
  252.           print("Unable To Place")
  253.         end
  254.       else
  255.         if turtle.place() then
  256.           print("Placed Front Block")
  257.         else
  258.           print("Unable To Place")
  259.         end
  260.       end
  261.     end
  262.  
  263.     if p1 == turnLeftKey then
  264.       if turtle.turnLeft() then
  265.         print("Turn left")
  266.       end
  267.     end
  268.  
  269.     if p1 == turnRightKey then
  270.       if turtle.turnRight() then
  271.         print("Turn right")
  272.       end
  273.     end
  274.  
  275.     if p1 == selectUpKey then
  276.       turtle.select( (((turtle.getSelectedSlot()-1) - 4) % 16) + 1 )
  277.     end
  278.  
  279.     if p1 == selectDownKey then
  280.       turtle.select( (((turtle.getSelectedSlot()-1) + 4) % 16) + 1 )
  281.     end
  282.  
  283.     if p1 == selectLeftKey then
  284.       turtle.select( (((turtle.getSelectedSlot()-1) - 1) % 16) + 1 )
  285.     end
  286.  
  287.     if p1 == selectRightKey then
  288.       turtle.select( (((turtle.getSelectedSlot()-1) + 1) % 16) + 1 )
  289.     end
  290.  
  291.     if p1 == checkFuelKey then
  292.       checkFuel()
  293.     end
  294.  
  295.     if p1 == refuelKey then
  296.       refuel()
  297.     end
  298.  
  299.     if p1 == exitKey then
  300.       run = false
  301.       term.clear()
  302.       term.setCursorPos(1,1)
  303.     end
  304.  
  305.     if hasValue({forwardKey, backKey, turnLeftKey, turnRightKey, placeKey, breakKey}, p1) then
  306.       updateBlocks()
  307.       print(blocks.top.completeName)
  308.       print(blocks.front.completeName)
  309.       print(blocks.bottom.completeName)
  310.     end
  311.  
  312.   elseif e == "key" then
  313.     if p1 == upModKey then
  314.       upMod = true
  315.     end
  316.     if p1 == downModKey then
  317.       downMod = true
  318.     end
  319.   end
  320. end
Advertisement
Add Comment
Please, Sign In to add comment