jakimfett

ComputerCraft Turtle Mining Program - v1.2

Jan 31st, 2013
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.29 KB | None | 0 0
  1. --Created by jakimfett
  2. --todo: multi level excavations
  3. --place torches under development
  4. --Please note that the slots for item tests (gravel, cobble, dirt) have shifted to allow torch placement
  5.  
  6. os.setComputerLabel("yourusername")
  7. turtle.select(1)
  8. local direction = "forward"
  9. local targetFuelLevel = 10
  10. local digDelay = 1 --Wait time to allow gravel/sand to fall
  11.  
  12. local fuelSlot = 16
  13. local chestSlot = 15
  14.  
  15. local torchSlot = 14
  16. local cobbleSlot = 13
  17. local gravelSlot = 12
  18. local dirtSlot = 11
  19. local maxSlot = 10 --must be one less than the garbage slot
  20.  
  21. local placeChests = 1
  22.  
  23. local discardDirt = 1
  24. local discardCobble = 1
  25. local discardGravel = 1
  26.  
  27. --Defining the size of hole to dig
  28. --Using the Cartesian coordinate system
  29. --The "Y" axis is up and down, multiplied by 3 (the turtle digs 3 high tunnels)
  30. --The "X" axis is left and right
  31. --The "Z" axis is forward and back
  32. local sizeY = 0
  33. local sizeX = 0
  34. local sizeZ = 0
  35. local sizeDirection = "left"
  36.  
  37. local torchCountX = 1
  38. local torchCountZ = 1
  39. local placeTorchEvery = 6
  40.  
  41. function verifyReady()
  42.   if discardDirt == 1 then
  43.     if turtle.getItemCount(dirtSlot) == 0 then
  44.       print("Please put some dirt in slot:")
  45.       print(dirtSlot)
  46.       os.sleep(5)
  47.       os.reboot()
  48.     end
  49.   end
  50.  
  51.   if discardCobble == 1 then
  52.     if turtle.getItemCount(cobbleSlot) == 0 then
  53.       print("Please put some cobble in slot:")
  54.       print(cobbleSlot)
  55.       os.sleep(5)
  56.       os.reboot()
  57.     end
  58.   end
  59.  
  60.   if discardGravel == 1 then
  61.     if turtle.getItemCount(gravelSlot) == 0 then
  62.       print("Please put some gravel in slot:")
  63.       print(gravelSlot)
  64.       os.sleep(5)
  65.       os.reboot()
  66.     end
  67.   end
  68.   turtle.select(1)
  69. end
  70.  
  71. function userInput()
  72.  term.clear()
  73.  term.setCursorPos(1, 1)
  74.  print("Please input a length:")
  75.  sizeZ = read()
  76.  print("Please input a width:")
  77.  sizeX = read()
  78.  print("Direction to excavate (left/right):")
  79.  sizeDirection = read()
  80. end
  81.  
  82. function refuel()
  83.   local currentFuelLevel = turtle.getFuelLevel()
  84.   if currentFuelLevel <= targetFuelLevel then
  85.     turtle.select(fuelSlot)
  86.     turtle.refuel()
  87.   end
  88.   turtle.select(1)
  89. end
  90.  
  91. function digForward(distance)
  92.   local count = 0
  93.   repeat
  94.     local cleared = 0
  95.     repeat --Loop dig and detect because of gravel and sand
  96.       turtle.dig()
  97.       os.sleep(digDelay)
  98.     until turtle.detect() == false
  99.     turtle.forward()
  100.     turtle.digDown()
  101.     turtle.digUp()
  102.     if turtle.detectUp() then --If digging up released gravel or sand
  103.       repeat
  104.         turtle.digUp()
  105.         os.sleep(digDelay)
  106.       until turtle.detectUp() == false
  107.     end
  108.     if turtle.getItemCount(10) ~= 0 then
  109.       garbageCollect()
  110.       turtle.select(1)
  111.     end
  112.     count = count + 1
  113.     torchCountZ = torchCountZ + 1
  114.     if torchCountZ > placeTorchEvery then
  115.       if torchCountX > placeTorchEvery then
  116.         placeTorchHere()
  117.         torchCountX = 1
  118.       end
  119.       torchCountZ = 1
  120.     end
  121.   until count == distance
  122.   turtle.select(1)
  123. end
  124.  
  125. function placeTorchHere()
  126.   turtle.select(torchSlot)
  127.   turtle.placeDown()
  128.   turtle.select(1)
  129. end
  130.  
  131. function turn(direction)
  132.   if direction == "right" then
  133.     turtle.turnRight()
  134.   elseif direction == "left" then
  135.     turtle.turnLeft()
  136.   end
  137. end
  138.  
  139. function chestDump()
  140.   if placeChests == 1 then
  141.     --garbageCollect()
  142.     turtle.select(chestSlot)
  143.     turtle.placeDown()
  144.     local count = 1
  145.     repeat
  146.       turtle.select(count)
  147.       turtle.dropDown()
  148.       count = count + 1
  149.     until count == maxSlot + 1
  150.     turtle.select(1)
  151.   end
  152. end
  153.  
  154. function garbageCollect()
  155.   if discardDirt == 1 then
  156.     local dirtCount = 1
  157.     repeat
  158.       turtle.select(dirtCount)
  159.       if turtle.compareTo(dirtSlot) then
  160.         turtle.drop()
  161.       end
  162.       dirtCount = dirtCount + 1
  163.     until dirtCount == maxSlot + 1
  164.   end
  165.  
  166.   if discardCobble == 1 then
  167.     local cobbleCount = 1
  168.     repeat
  169.       turtle.select(cobbleCount)
  170.       if turtle.compareTo(cobbleSlot) then
  171.         turtle.drop()
  172.       end
  173.       cobbleCount = cobbleCount + 1
  174.     until cobbleCount == maxSlot + 1
  175.   end
  176.  
  177.   if discardGravel == 1 then
  178.     local gravelCount = 1
  179.     repeat
  180.       turtle.select(gravelCount)
  181.       if turtle.compareTo(gravelSlot) then
  182.         turtle.drop()
  183.       end
  184.       gravelCount = gravelCount + 1
  185.     until gravelCount == maxSlot + 1
  186.   end
  187.  
  188.   compactInventory()
  189.   compactInventory()
  190.   if turtle.getItemCount(maxSlot - 1) ~= 0 then
  191.     chestDump()
  192.   end
  193.   turtle.select(1)
  194. end
  195.  
  196. function compactInventory()
  197.   local compaceCount = 1
  198.   repeat
  199.     local countMove = 2
  200.     repeat
  201.       turtle.select(countMove)
  202.       turtle.transferTo(countMove - 1)
  203.       countMove = countMove + 1
  204.     until countMove == maxSlot + 1
  205.     compaceCount = compaceCount + 1
  206.   until compaceCount > 1
  207. end
  208.  
  209. function excavateLevel()
  210.   local nextTurn = sizeDirection
  211.   local count = 1
  212.   repeat
  213.     refuel()
  214.     sizeZ = sizeZ + 0
  215.     digForward(sizeZ)
  216.     turtle.select(1)
  217.     turn(nextTurn)
  218.     digForward(1)
  219.     torchCountX = torchCountX + 1
  220.     turn(nextTurn)
  221.     if nextTurn == "left" then
  222.       nextTurn = "right"
  223.     elseif nextTurn == "right" then
  224.       nextTurn = "left"
  225.     end
  226.     count = count + 1
  227.   until count >= sizeX + 0 --sizeX + 0 ensures that sizeX evaluated as number
  228. end
  229.  
  230. verifyReady()
  231. userInput()
  232. excavateLevel()
Advertisement
Add Comment
Please, Sign In to add comment