dracoix

Turtle Bore 1.1

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