Advertisement
Guest User

Turtle excavate plus

a guest
May 29th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.51 KB | None | 0 0
  1.  
  2.  
  3.  
  4. local tArgs = { ... }
  5. if #tArgs ~= 1 then
  6.     print( "Usage: excavate <starting layer> <diameter>" )
  7.     return
  8. end
  9.  
  10. -- First go to y layer
  11. local gpsx, gpsy, gpsz = gps.locate(5)
  12. if not gpsx then
  13.     print( "The turtle needs a gps signal" )
  14.     return
  15. end
  16. local startingLayer = gpsy
  17. local diggingLayer = tonumber( tArgs[1] )
  18. local depthFromHome = 0
  19.  
  20. -- Mine in a quarry pattern until we hit something we can't dig
  21. local size = tonumber( tArgs[2] )
  22. if size < 1 then
  23.     print( "Excavate diameter must be positive" )
  24.     return
  25. end
  26.    
  27. local depth = 0
  28. local unloaded = 0
  29. local collected = 0
  30.  
  31. local xPos,zPos = 0,0
  32. local xDir,zDir = 0,1
  33.  
  34. local goTo -- Filled in further down
  35. local refuel -- Filled in further down
  36.  
  37.  
  38. -- Block black list
  39.     local blackist = {
  40.       "minecraft:cobblestone"
  41.     }
  42.     local _blist = {}
  43.  
  44.     -- Inverse blacklist for fast hashmap lookup
  45.     for i = 1, #blacklist do
  46.       _blist[blacklist[i]] = true
  47.     end
  48.    
  49.  
  50. local function unload( _bKeepOneFuelStack )
  51.     print( "Unloading items..." )
  52.     for n=1,16 do
  53.         local nCount = turtle.getItemCount(n)
  54.         if nCount > 0 then
  55.             turtle.select(n)           
  56.             local bDrop = true
  57.             if _bKeepOneFuelStack and turtle.refuel(0) then
  58.                 bDrop = false
  59.                 _bKeepOneFuelStack = false
  60.             end        
  61.             if bDrop then
  62.                 turtle.drop()
  63.                 unloaded = unloaded + nCount
  64.             end
  65.         end
  66.     end
  67.     collected = 0
  68.     turtle.select(1)
  69. end
  70.  
  71. local function returnSupplies()
  72.     local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  73.     print( "Returning to surface..." )
  74.     goTo( 0,0,0,0,-1 )
  75.    
  76.     local fuelNeeded = 2*(x+y+z) + 1 +  2*(depthFromHome)
  77.     if not refuel( fuelNeeded ) then
  78.         unload( true )
  79.         print( "Waiting for fuel" )
  80.         while not refuel( fuelNeeded ) do
  81.             os.pullEvent( "turtle_inventory" )
  82.         end
  83.     else
  84.         unload( true ) 
  85.     end
  86.    
  87.     print( "Resuming mining..." )
  88.     goTo( x,y,z,xd,zd )
  89. end
  90.  
  91. local function collect()   
  92.     local bFull = true
  93.     local nTotalItems = 0
  94.     for n=1,16 do
  95.         local temp, pickedUpItem = turtle.getItemDetail(n)
  96.         if applyBlackList and not _blist[pickedUpItem["name"]] then
  97.           -- Drop the item
  98.           local currentSelected = turtle.getSelectedSlot()
  99.           turtle.select(n)
  100.           turtle.drop()
  101.           turtle.select(currentSelected)
  102.         end
  103.         local nCount = turtle.getItemCount(n)
  104.         if nCount == 0 then
  105.             bFull = false
  106.         end
  107.         nTotalItems = nTotalItems + nCount
  108.     end
  109.    
  110.     if nTotalItems > collected then
  111.         collected = nTotalItems
  112.         if math.fmod(collected + unloaded, 50) == 0 then
  113.             print( "Mined "..(collected + unloaded).." items." )
  114.         end
  115.     end
  116.    
  117.     if bFull then
  118.         print( "No empty slots left." )
  119.         return false
  120.     end
  121.     return true
  122. end
  123.  
  124. function refuel( ammount )
  125.     local fuelLevel = turtle.getFuelLevel()
  126.     if fuelLevel == "unlimited" then
  127.         return true
  128.     end
  129.    
  130.     local needed = ammount or (xPos + zPos + depth + 2 +  2*(depthFromHome))
  131.     if turtle.getFuelLevel() < needed then
  132.         local fueled = false
  133.         for n=1,16 do
  134.             if turtle.getItemCount(n) > 0 then
  135.                 turtle.select(n)
  136.                 if turtle.refuel(1) then
  137.                     while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  138.                         turtle.refuel(1)
  139.                     end
  140.                     if turtle.getFuelLevel() >= needed then
  141.                         turtle.select(1)
  142.                         return true
  143.                     end
  144.                 end
  145.             end
  146.         end
  147.         turtle.select(1)
  148.         return false
  149.     end
  150.    
  151.     return true
  152. end
  153.  
  154. local function tryForwards()
  155.     if not refuel() then
  156.         print( "Not enough Fuel" )
  157.         returnSupplies()
  158.     end
  159.    
  160.     while not turtle.forward() do
  161.         if turtle.detect() then
  162.             if turtle.dig() then
  163.                 if not collect() then
  164.                     returnSupplies()
  165.                 end
  166.             else
  167.                 return false
  168.             end
  169.         elseif turtle.attack() then
  170.             if not collect() then
  171.                 returnSupplies()
  172.             end
  173.         else
  174.             sleep( 0.5 )
  175.         end
  176.     end
  177.    
  178.     xPos = xPos + xDir
  179.     zPos = zPos + zDir
  180.     return true
  181. end
  182.  
  183. local function tryDown()
  184.     if not refuel() then
  185.         print( "Not enough Fuel" )
  186.         returnSupplies()
  187.     end
  188.    
  189.     while not turtle.down() do
  190.         if turtle.detectDown() then
  191.             if turtle.digDown() then
  192.                 if not collect() then
  193.                     returnSupplies()
  194.                 end
  195.             else
  196.                 return false
  197.             end
  198.         elseif turtle.attackDown() then
  199.             if not collect() then
  200.                 returnSupplies()
  201.             end
  202.         else
  203.             sleep( 0.5 )
  204.         end
  205.     end
  206.  
  207.     depth = depth + 1
  208.     if math.fmod( depth, 10 ) == 0 then
  209.         print( "Descended "..depth.." metres." )
  210.     end
  211.  
  212.     return true
  213. end
  214.  
  215. local function turnLeft()
  216.     turtle.turnLeft()
  217.     xDir, zDir = -zDir, xDir
  218. end
  219.  
  220. local function turnRight()
  221.     turtle.turnRight()
  222.     xDir, zDir = zDir, -xDir
  223. end
  224.  
  225. function goToDiggingLayer(){
  226.    
  227.     while startingLayer - depthFromHome < diggingLayer do
  228.         if turtle.down() then
  229.             depthFromHome = depthFromHome + 1
  230.         elseif turtle.digDown() or turtle.attackDown() then
  231.             collect()
  232.         else
  233.             sleep( 0.5 )
  234.         end
  235.     end
  236.    
  237. }
  238.  
  239. function goToStartingLayer(){
  240.    
  241.     for i = 1, depthFromHome do
  242.         if not turtle.up() then
  243.             if turtle.digUp() or turtle.attackUp() then
  244.                 collect()
  245.             else
  246.                 sleep( 0.5 )
  247.             end
  248.         end
  249.     end
  250.    
  251. }
  252.  
  253. function goTo( x, y, z, xd, zd )
  254.     while depth > y do
  255.         if turtle.up() then
  256.             depth = depth - 1
  257.         elseif turtle.digUp() or turtle.attackUp() then
  258.             collect()
  259.         else
  260.             sleep( 0.5 )
  261.         end
  262.     end
  263.  
  264.     if xPos > x then
  265.         while xDir ~= -1 do
  266.             turnLeft()
  267.         end
  268.         while xPos > x do
  269.             if turtle.forward() then
  270.                 xPos = xPos - 1
  271.             elseif turtle.dig() or turtle.attack() then
  272.                 collect()
  273.             else
  274.                 sleep( 0.5 )
  275.             end
  276.         end
  277.     elseif xPos < x then
  278.         while xDir ~= 1 do
  279.             turnLeft()
  280.         end
  281.         while xPos < x do
  282.             if turtle.forward() then
  283.                 xPos = xPos + 1
  284.             elseif turtle.dig() or turtle.attack() then
  285.                 collect()
  286.             else
  287.                 sleep( 0.5 )
  288.             end
  289.         end
  290.     end
  291.    
  292.     if zPos > z then
  293.         while zDir ~= -1 do
  294.             turnLeft()
  295.         end
  296.         while zPos > z do
  297.             if turtle.forward() then
  298.                 zPos = zPos - 1
  299.             elseif turtle.dig() or turtle.attack() then
  300.                 collect()
  301.             else
  302.                 sleep( 0.5 )
  303.             end
  304.         end
  305.     elseif zPos < z then
  306.         while zDir ~= 1 do
  307.             turnLeft()
  308.         end
  309.         while zPos < z do
  310.             if turtle.forward() then
  311.                 zPos = zPos + 1
  312.             elseif turtle.dig() or turtle.attack() then
  313.                 collect()
  314.             else
  315.                 sleep( 0.5 )
  316.             end
  317.         end
  318.     end
  319.    
  320.     while depth < y do
  321.         if turtle.down() then
  322.             depth = depth + 1
  323.         elseif turtle.digDown() or turtle.attackDown() then
  324.             collect()
  325.         else
  326.             sleep( 0.5 )
  327.         end
  328.     end
  329.    
  330.     while zDir ~= zd or xDir ~= xd do
  331.         turnLeft()
  332.     end
  333. end
  334.  
  335. if not refuel() then
  336.     print( "Out of Fuel" )
  337.     return
  338. end
  339.  
  340. print( "Excavating..." )
  341.  
  342. goToDiggingLayer()
  343.  
  344. local reseal = false
  345. turtle.select(1)
  346. if turtle.digDown() then
  347.     reseal = true
  348. end
  349.  
  350. local alternate = 0
  351. local done = false
  352. while not done do
  353.     for n=1,size do
  354.         for m=1,size-1 do
  355.             if not tryForwards() then
  356.                 done = true
  357.                 break
  358.             end
  359.         end
  360.         if done then
  361.             break
  362.         end
  363.         if n<size then
  364.             if math.fmod(n + alternate,2) == 0 then
  365.                 turnLeft()
  366.                 if not tryForwards() then
  367.                     done = true
  368.                     break
  369.                 end
  370.                 turnLeft()
  371.             else
  372.                 turnRight()
  373.                 if not tryForwards() then
  374.                     done = true
  375.                     break
  376.                 end
  377.                 turnRight()
  378.             end
  379.         end
  380.     end
  381.     if done then
  382.         break
  383.     end
  384.    
  385.     if size > 1 then
  386.         if math.fmod(size,2) == 0 then
  387.             turnRight()
  388.         else
  389.             if alternate == 0 then
  390.                 turnLeft()
  391.             else
  392.                 turnRight()
  393.             end
  394.             alternate = 1 - alternate
  395.         end
  396.     end
  397.    
  398.     if not tryDown() then
  399.         done = true
  400.         break
  401.     end
  402. end
  403.  
  404. print( "Returning to surface..." )
  405.  
  406. -- Return to where we started
  407. goTo( 0,0,0,0,-1 )
  408. goToStartingLayer()
  409. unload( false )
  410. turnLeft()
  411. turnLeft()
  412.  
  413. -- Seal the hole
  414. if reseal then
  415.     turtle.placeDown()
  416. end
  417.  
  418. print( "Mined "..(collected + unloaded).." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement