Advertisement
peregrin5

excavateUp

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