Advertisement
jjprices

minecraft - computercraft - mining turtle - automine

Jan 1st, 2015
1,124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.23 KB | None | 0 0
  1. -----------------------------------
  2. --                               --
  3. --           automine            --
  4. --             v1.0              --
  5. -----------------------------------
  6. -- Authors: jjprices, ep9630 --
  7. -----------------------------------
  8. --
  9. -- What is this?
  10. --   Lua program for Minecraft with ComputerCraft mod.  This works only with mining turtles!
  11. --
  12. -- What does it do?
  13. --   It mines a tunnel 1 block wide, 2 high and places an item from inventory spot 1 (top left)
  14. --   15 blocks apart.  This is intended for torches, so place a stack of torches in the turtle's
  15. --   first inventory spot.  Place fuel in any other spot (coal works well, or lava bucket for epic).
  16. --   This isn't the most efficient way to mine, but it's fun and this was a good first exercise
  17. --   in trying out Lua and controlling a turtle.  You follow the turtle and use your
  18. --   pick axe to mine the items you want on the floor, walls, or ceiling of the tunnel that weren't in the
  19. --   direct path of your turtle.  You must stay relatively close to your turtle or it will go outside of
  20. --   loaded chunks and just shut down.
  21. --
  22. -- How do I get it?
  23. --   Easy!  Just enter the following command into your mining turtle:
  24. --     pastebin get HXVWzXDg autominer
  25. --
  26. -- Usage: automine <distance>
  27. --                  ^-- Number of blocks to mine forward before stopping
  28. --                      (Optional, mines until inventory full if left blank)
  29. --
  30. -- Example: automine 500 <- Mines for 500 blocks before stopping, but it'll
  31. --                          ask if you want to go more.  It's polite like that.
  32. --
  33. -- Notes:
  34. --   * If it stops, just right click on the turtle, it'll tell you why and what to do.
  35. --   * To pause the turtle, place ONE (1) item of your choosing in inventory spot 16 (lower right)
  36. --   * To stop the turtle, place TWO (2) or more items in inventory spot 16 (still the lower right)
  37.  
  38. local tArgs = { ... }
  39.  
  40. local lightdistance = 15
  41. local totalspaces = 0
  42. local lightspaces = 10
  43. local throwawayblocks = {"dirt", "cobblestone", "sand", "gravel"}
  44. local bridgeblock = "cobblestone"
  45.  
  46. local function throwAway()
  47.   turtle.turnLeft()
  48.   turtle.turnLeft()
  49.   for i = 1,15 do
  50.     turtle.select(i)
  51.     local blockInfo = turtle.getItemDetail(i)
  52.     if blockInfo then
  53.       local matchfound = false
  54.       for j, block in ipairs(throwawayblocks) do
  55.         if string.find(blockInfo.name, block) then
  56.           matchfound = true
  57.           turtle.drop()
  58.           break
  59.         end
  60.       end
  61.       if matchfound == false then
  62.         for j = 2, i-1 do
  63.           if turtle.getItemCount(j) == 0 then
  64.             turtle.transferTo(j)
  65.             break
  66.           else
  67.             if turtle.compareTo(j) then
  68.                turtle.transferTo(j)
  69.                if turtle.getItemCount(i) == 0 then
  70.                  break;
  71.                end
  72.             end
  73.           end
  74.         end
  75.       end      
  76.     end
  77.   end
  78.   turtle.turnLeft()
  79.   turtle.turnLeft()
  80.   turtle.select(2)
  81. end
  82.  
  83. local function digCorridor()
  84.   while turtle.forward() == false do
  85.     turtle.dig()
  86.   end
  87.   while turtle.detectUp() do
  88.     turtle.digUp()
  89.     --this sleep is necessary to allow time for sand or gravel to fall from above
  90.     sleep(0.5)
  91.   end
  92.   if turtle.detectDown() == false then
  93.     for i = 2,15 do
  94.       local blockInfo = turtle.getItemDetail(i)
  95.       if blockInfo then
  96.         if string.find(blockInfo.name, bridgeblock) then
  97.           turtle.select(i)
  98.           turtle.placeDown()
  99.           turtle.select(2)
  100.           break
  101.         end
  102.       end
  103.     end
  104.   end
  105. end
  106.  
  107. local function reFuel()
  108.   for i = 2,15 do
  109.     if turtle.getItemCount(i) > 0 then
  110.       turtle.select(i)
  111.       if turtle.refuel(1) then
  112.         break
  113.       end
  114.     end
  115.   end
  116.   turtle.select(2)
  117. end
  118.  
  119. local function checkFuel()
  120.   if turtle.getFuelLevel() < 3 then
  121.     reFuel()
  122.     if turtle.getFuelLevel() < 3 then
  123.       print "Out of fuel, please add fuel to inventory so I can continue..."
  124.       while turtle.getFuelLevel() < 3 do
  125.         sleep(1)
  126.         reFuel()
  127.       end
  128.       print "Ahhh... some fuel.  Let's continue!"
  129.     end
  130.   end
  131. end
  132.  
  133. local function returnHome()
  134.   local answer
  135.   repeat
  136.      print("Return to where")
  137.      io.write("I last started (y/n)? ")
  138.      io.flush()
  139.      answer=io.read()
  140.   until answer == "y" or answer == "n"
  141.   if answer == "y" then
  142.     print "Returning home"
  143.     turtle.turnRight()
  144.     turtle.turnRight()
  145.     for i=1,totalspaces do
  146.       while turtle.forward() == false do
  147.         turtle.dig()
  148.         checkFuel()
  149.       end
  150.     end
  151.   end
  152. end
  153.  
  154. local function placeLight()
  155.   turtle.select(1)
  156.   turtle.turnRight()
  157.   while turtle.detect() do
  158.     turtle.dig()
  159.   end
  160.   turtle.place()
  161.   turtle.select(2)
  162.   turtle.turnLeft()
  163.   lightspaces = 0
  164. end
  165.  
  166. local distanceLimit = 0
  167.  
  168. if #tArgs > 0 then
  169.   distanceLimit = tonumber(tArgs[1])
  170. end
  171.  
  172. --Main execution starts here
  173.  
  174. turtle.select(2)
  175. while turtle.getItemCount(16) <= 1 do
  176.   if turtle.getItemCount(15) > 0 then
  177.     print "Attempting to discard unwanted items..."
  178.     throwAway()
  179.     if turtle.getItemCount(14) > 0 then
  180.       print "Inventory full enough..."
  181.       break
  182.     end
  183.   end
  184.   checkFuel()
  185.   digCorridor()
  186.   totalspaces = totalspaces + 1
  187.   lightspaces = lightspaces + 1
  188.   if lightspaces >= lightdistance then
  189.     placeLight()
  190.   end
  191.   if turtle.getItemCount(16) == 1 then
  192.     print("1 item placed in slot 16.")
  193.     print("I'll pause until you remove it.")
  194.     while turtle.getItemCount(16) == 1 do
  195.       sleep(1)
  196.     end
  197.     if turtle.getItemCount(16) == 0 then
  198.       print("Ok!  Let's continue!")
  199.     end
  200.   end
  201.   if distanceLimit > 0 and totalspaces >= distanceLimit then
  202.     print("Reached distance of " .. distanceLimit .. "...")
  203.     local answer
  204.     repeat
  205.       print("Should we keep")
  206.       io.write("going forward (y/n)? ")
  207.       io.flush()
  208.       answer = io.read()
  209.     until answer == "y" or answer == "n"
  210.     if answer == "y" then
  211.       io.write("What additional distance? ")
  212.       io.flush()
  213.       local moreDistance
  214.       moreDistance = io.read()
  215.       distanceLimit = distanceLimit + tonumber(moreDistance)
  216.     else
  217.       break
  218.     end
  219.   end
  220. end
  221.  
  222. if turtle.getItemCount(16) > 0 then
  223.   print "Something is in last slot..."
  224. end
  225.  
  226. returnHome()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement