SoundChaos

FastMine

May 8th, 2020 (edited)
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.71 KB | None | 0 0
  1. local tArgs = { ... }
  2. if #tArgs ~= 1 and #tArgs ~= 2 then
  3.     print( "Usage: excavate <diameter>" )
  4.     return
  5. end
  6.  
  7. -- JUSTIN CODE: Variable to drop all cobblestone after every 8 blocks
  8. local l = 1
  9.  
  10. -- get depth argument
  11. local maxDepth = tonumber( tArgs[2] )
  12. if #tArgs == 1 then
  13.     print( "No valid depth given, I will dig to bedrock" )
  14. end
  15.  
  16. -- Mine in a quarry pattern until we hit something we can't dig
  17. local size = tonumber( tArgs[1] )
  18. if size < 1 then
  19.     print( "Excavate diameter must be positive" )
  20.     return
  21. end
  22.    
  23. local depth = 0
  24. local unloaded = 0
  25. local collected = 0
  26.  
  27. local xPos,zPos = 0,0
  28. local xDir,zDir = 0,1
  29.  
  30. local goTo -- Filled in further down
  31. local refuel -- Filled in further down
  32.    
  33.  
  34. local function unload( _bKeepOneFuelStack )
  35.     print( "Unloading items..." )
  36.     for n=3,16 do
  37.         local nCount = turtle.getItemCount(n)
  38.         if nCount > 0 then
  39.             turtle.select(n)          
  40.             local bDrop = true
  41.             if _bKeepOneFuelStack and turtle.refuel(0) then
  42.                 bDrop = false
  43.                 _bKeepOneFuelStack = false
  44.             end        
  45.             if bDrop then
  46.                 turtle.drop()
  47.                 unloaded = unloaded + nCount
  48.             end
  49.         end
  50.     end
  51.     collected = 0
  52.     turtle.select(1)
  53. end
  54.  
  55. local function returnSupplies()
  56.     local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  57.     print( "Returning to surface..." )
  58.     goTo( 0,0,0,0,-1 )
  59.    
  60.     local fuelNeeded = 2*(x+y+z) + 1
  61.     if not refuel( fuelNeeded ) then
  62.         unload( true )
  63.         print( "Waiting for fuel" )
  64.         while not refuel( fuelNeeded ) do
  65.             sleep(2)
  66.         end
  67.     else
  68.         unload( true )
  69.     end
  70.    
  71.     print( "Resuming mining..." )
  72.     goTo( x,y,z,xd,zd )
  73. end
  74.  
  75.  
  76.  
  77. local function collect()  
  78.     local bFull = true
  79.     local nTotalItems = 0
  80.     for n=1,16 do
  81.         local nCount = turtle.getItemCount(n)
  82.         if nCount == 0 then
  83.             bFull = false
  84.         end
  85.         nTotalItems = nTotalItems + nCount
  86.     end
  87.    
  88.     --JUSTIN CODE SECTION FOR VARIABLE ABOVE
  89.     -----------------------------------------------------------------------------------------
  90.     l = l + 1
  91.     if l == 31 then
  92.         local u = 3 -- this is for the repeat loop to drop any block that is the same as slot 2
  93.         turtle.select(2) -- select slot 2
  94.         repeat
  95.             if turtle.compareTo(u) then -- if slot 2 is equal to U
  96.                 turtle.select(u) -- Select slot U
  97.                 turtle.drop() -- drop slot U
  98.                 turtle.select(2) -- select slot two again  
  99.             end
  100.             u = u + 1 -- change u to the next slot
  101.         until u == 17
  102.         turtle.select(1)
  103.         l = 1
  104.     end
  105.     -----------------------------------------------------------------------------------------
  106.    
  107.     if nTotalItems > collected then
  108.         collected = nTotalItems
  109.         if math.fmod(collected + unloaded, 50) == 0 then
  110.             print( "Mined "..(collected + unloaded).." items." )
  111.         end
  112.     end
  113.    
  114.     if bFull then
  115.         print( "No empty slots left." )
  116.         return false
  117.     end
  118.     return true
  119. end
  120.  
  121. function refuel( ammount )
  122.     local fuelLevel = turtle.getFuelLevel()
  123.     if fuelLevel == "unlimited" then
  124.         return true
  125.     end
  126.    
  127.     local needed = ammount or (xPos + zPos + depth + 2)
  128.     if turtle.getFuelLevel() < needed then
  129.         local fueled = false
  130.         for n=1,16 do
  131.             if turtle.getItemCount(n) > 0 then
  132.                 turtle.select(n)
  133.                 if turtle.refuel(1) then
  134.                     while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  135.                         turtle.refuel(1)
  136.                     end
  137.                     if turtle.getFuelLevel() >= needed then
  138.                         turtle.select(1)
  139.                         return true
  140.                     end
  141.                 end
  142.             end
  143.         end
  144.         turtle.select(1)
  145.         return false
  146.     end
  147.    
  148.     return true
  149. end
  150.  
  151. local function tryForwards()
  152.     if not refuel() then
  153.         print( "Not enough Fuel" )
  154.         returnSupplies()
  155.     end
  156.    
  157.     if turtle.detectUp() then
  158.         if turtle.digUp() then
  159.             if not collect() then
  160.                 returnSupplies()
  161.             end
  162.         end
  163.     end
  164.     if turtle.detectDown() then
  165.         if turtle.digDown() then
  166.             if not collect() then
  167.                 returnSupplies()
  168.             end
  169.         end
  170.     end
  171.        
  172.     while not turtle.forward() do
  173.         if turtle.detect() then
  174.             if turtle.dig() then
  175.                 if not collect() then
  176.                     returnSupplies()
  177.                 end
  178.             else
  179.                 return false
  180.             end
  181.         elseif turtle.attack() or turtle.attackUp() then
  182.             if not collect() then
  183.                 returnSupplies()
  184.             end
  185.         else
  186.             sleep( 0.3 )
  187.         end
  188.     end
  189.    
  190.     xPos = xPos + xDir
  191.     zPos = zPos + zDir
  192.     return true
  193. end
  194.  
  195. local function tryDown()
  196.     if not refuel() then
  197.         print( "Not enough Fuel" )
  198.         returnSupplies()
  199.     end
  200.    
  201.     -- Custom change: Check if depth = maxDepth,
  202.     if #tArgs == 2 then
  203.         if depth + 1 >= maxDepth then
  204.             print( "dug "..depth.." metres.")
  205.             return false
  206.         end
  207.     end
  208.    
  209.     if turtle.detectUp() then
  210.         if turtle.digUp() then
  211.             if not collect() then
  212.                 returnSupplies()
  213.             end
  214.         end
  215.     end
  216.    
  217.     for i = 1,3 do
  218.         while not turtle.down() do
  219.             if turtle.detectDown() then
  220.                 if turtle.digDown() then
  221.                     if not collect() then
  222.                         returnSupplies()
  223.                     end
  224.                 else
  225.                     return false
  226.                 end
  227.             elseif turtle.attackDown() then
  228.                 if not collect() then
  229.                     returnSupplies()
  230.                 end
  231.             else
  232.                 sleep( 0.3 )
  233.             end
  234.         end
  235.         depth = depth + 1
  236.     end
  237.  
  238.     if math.fmod( depth, 10 ) == 0 then
  239.         print( "Descended "..depth.." metres." )
  240.     end
  241.  
  242.     return true
  243. end
  244.  
  245. local function turnLeft()
  246.     turtle.turnLeft()
  247.     xDir, zDir = -zDir, xDir
  248. end
  249.  
  250. local function turnRight()
  251.     turtle.turnRight()
  252.     xDir, zDir = zDir, -xDir
  253. end
  254.  
  255. function goTo( x, y, z, xd, zd )
  256.     while depth > y do
  257.         if turtle.up() then
  258.             depth = depth - 1
  259.         elseif turtle.digUp() or turtle.attackUp() then
  260.             collect()
  261.         else
  262.             sleep( 0.3 )
  263.         end
  264.     end
  265.  
  266.     if xPos > x then
  267.         while xDir ~= -1 do
  268.             turnLeft()
  269.         end
  270.         while xPos > x do
  271.             if turtle.forward() then
  272.                 xPos = xPos - 1
  273.             elseif turtle.dig() or turtle.attack() or turtle.attackUp() then
  274.                 collect()
  275.             else
  276.                 sleep( 0.3 )
  277.             end
  278.         end
  279.     elseif xPos < x then
  280.         while xDir ~= 1 do
  281.             turnLeft()
  282.         end
  283.         while xPos < x do
  284.             if turtle.forward() then
  285.                 xPos = xPos + 1
  286.             elseif turtle.dig() or turtle.attack() or turtle.attackUp() then
  287.                 collect()
  288.             else
  289.                 sleep( 0.3 )
  290.             end
  291.         end
  292.     end
  293.    
  294.     if zPos > z then
  295.         while zDir ~= -1 do
  296.             turnLeft()
  297.         end
  298.         while zPos > z do
  299.             if turtle.forward() then
  300.                 zPos = zPos - 1
  301.             elseif turtle.dig() or turtle.attack() or turtle.attackUp() then
  302.                 collect()
  303.             else
  304.                 sleep( 0.3 )
  305.             end
  306.         end
  307.     elseif zPos < z then
  308.         while zDir ~= 1 do
  309.             turnLeft()
  310.         end
  311.         while zPos < z do
  312.             if turtle.forward() then
  313.                 zPos = zPos + 1
  314.             elseif turtle.dig() or turtle.attack() or turtle.attackUp() then
  315.                 collect()
  316.             else
  317.                 sleep( 0.3 )
  318.             end
  319.         end
  320.     end
  321.    
  322.     while depth < y do
  323.         if turtle.down() then
  324.             depth = depth + 1
  325.         elseif turtle.digDown() or turtle.attackDown() then
  326.             collect()
  327.         else
  328.             sleep( 0.3 )
  329.         end
  330.     end
  331.    
  332.     while zDir ~= zd or xDir ~= xd do
  333.         turnLeft()
  334.     end
  335. end
  336.  
  337. if not refuel() then
  338.     print( "Out of Fuel" )
  339.     return
  340. end
  341.  
  342. print( "Excavating..." )
  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. unload( false )
  409. goTo( 0,0,0,0,1 )
  410.  
  411. -- Seal the hole
  412. if reseal then
  413.     turtle.placeDown()
  414. end
  415.  
  416. print( "Mined "..(collected + unloaded).." items total." )
Add Comment
Please, Sign In to add comment