Advertisement
dracoix

Turtle Excavate 1.6

Feb 27th, 2019
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.45 KB | None | 0 0
  1. local tArgs = { ... }
  2. if #tArgs < 1 then
  3.     print( "Usage: diggit <diameter> <0 or 1 for blacklist enabled>" )
  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 black = false
  15.  
  16. if (#tArgs >= 2) then
  17.     black = (tArgs[2] == "1")
  18. end
  19.    
  20. local depth = 0
  21. local unloaded = 0
  22. local collected = 0
  23.  
  24. local xPos,zPos = 0,0
  25. local xDir,zDir = 0,1
  26.  
  27. local goTo -- Filled in further down
  28. local refuel -- Filled in further down
  29.  
  30. local function unload( _bKeepOneFuelStack )
  31.     print( "Unloading items..." )
  32.     for n=1,16 do
  33.         local nCount = turtle.getItemCount(n)
  34.         if nCount > 0 then
  35.             turtle.select(n)           
  36.             local bDrop = true
  37.             if _bKeepOneFuelStack and turtle.refuel(0) then
  38.                 bDrop = false
  39.                 _bKeepOneFuelStack = false
  40.             end        
  41.             if bDrop then
  42.                 turtle.drop()
  43.                 unloaded = unloaded + nCount
  44.             end
  45.         end
  46.     end
  47.     collected = 0
  48.     turtle.select(1)
  49. end
  50.  
  51. local function returnSupplies()
  52.     local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  53.     print( "Returning to surface..." )
  54.     goTo( 0,0,0,0,-1 )
  55.    
  56.     local fuelNeeded = 2*(x+y+z) + 1
  57.     if not refuel( fuelNeeded ) then
  58.         unload( true )
  59.         print( "Waiting for fuel" )
  60.         while not refuel( fuelNeeded ) do
  61.             os.pullEvent( "turtle_inventory" )
  62.         end
  63.     else
  64.         unload( true ) 
  65.     end
  66.    
  67.     print( "Resuming mining..." )
  68.     goTo( x,y,z,xd,zd )
  69. end
  70.  
  71. local function collect()   
  72.     local bFull = true
  73.     local nTotalItems = 0
  74.     for n=1,16 do
  75.         local nCount = turtle.getItemCount(n)
  76.         if nCount == 0 then
  77.             bFull = false
  78.         end
  79.         nTotalItems = nTotalItems + nCount
  80.     end
  81.    
  82.     if nTotalItems > collected then
  83.         collected = nTotalItems
  84.         if math.fmod(collected + unloaded, 50) == 0 then
  85.             print( "Mined "..(collected + unloaded).." items." )
  86.         end
  87.     end
  88.    
  89.     if bFull then
  90.         print( "No empty slots left." )
  91.         return false
  92.     end
  93.     return true
  94. end
  95.  
  96. function refuel( ammount )
  97.     local fuelLevel = turtle.getFuelLevel()
  98.     if fuelLevel == "unlimited" then
  99.         return true
  100.     end
  101.    
  102.     local needed = ammount or (xPos + zPos + depth + 2)
  103.     if turtle.getFuelLevel() < needed then
  104.         local fueled = false
  105.         for n=1,16 do
  106.             if turtle.getItemCount(n) > 0 then
  107.                 turtle.select(n)
  108.                 if turtle.refuel(1) then
  109.                     while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  110.                         turtle.refuel(1)
  111.                     end
  112.                     if turtle.getFuelLevel() >= needed then
  113.                         turtle.select(1)
  114.                         return true
  115.                     end
  116.                 end
  117.             end
  118.         end
  119.         turtle.select(1)
  120.         return false
  121.     end
  122.    
  123.     return true
  124. end
  125.  
  126. local iup
  127. local idown
  128. local dup
  129. local ddown
  130.  
  131. local function isBlacklist(name)
  132.     if not black then
  133.         return false
  134.     end
  135.     if (name == "minecraft:stone") then
  136.         return true
  137.     end
  138.     if (name == "minecraft:dirt") then
  139.         return true
  140.     end
  141.     if (name == "minecraft:water") then
  142.         return true
  143.     end
  144.     if (name == "minecraft:lava") then
  145.         return true
  146.     end
  147.     if (name == "minecraft:flowing_water") then
  148.         return true
  149.     end
  150.     print ("Ooo piece of candy: " .. name)
  151.     return false
  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.    
  179.     xPos = xPos + xDir
  180.     zPos = zPos + zDir
  181.  
  182.     iup, dup = turtle.inspectUp()
  183.     idown, ddown = turtle.inspectDown()
  184.  
  185.     if iup then
  186.         if not (isBlacklist(dup.name)) then
  187.             if turtle.digUp() then
  188.                 if not collect() then
  189.                     returnSupplies()
  190.                 end
  191.             end
  192.         end
  193.     end
  194.  
  195.     if idown then
  196.         if not (isBlacklist(ddown.name)) then
  197.             if turtle.digDown() then
  198.                 if not collect() then
  199.                     returnSupplies()
  200.                 end
  201.             end
  202.         end
  203.     end
  204.  
  205.     return true
  206. end
  207.  
  208. local function tryDown()
  209.     if not refuel() then
  210.         print( "Not enough Fuel" )
  211.         returnSupplies()
  212.     end
  213.    
  214.     while not turtle.down() do
  215.         if turtle.detectDown() then
  216.             if turtle.digDown() then
  217.                 if not collect() then
  218.                     returnSupplies()
  219.                 end
  220.             else
  221.                 return false
  222.             end
  223.         elseif turtle.attackDown() then
  224.             if not collect() then
  225.                 returnSupplies()
  226.             end
  227.         else
  228.             sleep( 0.5 )
  229.         end
  230.     end
  231.  
  232.     depth = depth + 1
  233.     if math.fmod( depth, 10 ) == 0 then
  234.         print( "Descended "..depth.." metres." )
  235.     end
  236.  
  237.     return true
  238. end
  239.  
  240. local function turnLeft()
  241.     turtle.turnLeft()
  242.     xDir, zDir = -zDir, xDir
  243. end
  244.  
  245. local function turnRight()
  246.     turtle.turnRight()
  247.     xDir, zDir = zDir, -xDir
  248. end
  249.  
  250. function goTo( x, y, z, xd, zd )
  251.     while depth > y do
  252.         if turtle.up() then
  253.             depth = depth - 1
  254.         elseif turtle.digUp() or turtle.attackUp() then
  255.             collect()
  256.         else
  257.             sleep( 0.5 )
  258.         end
  259.     end
  260.  
  261.     if xPos > x then
  262.         while xDir ~= -1 do
  263.             turnLeft()
  264.         end
  265.         while xPos > x do
  266.             if turtle.forward() then
  267.                 xPos = xPos - 1
  268.             elseif turtle.dig() or turtle.attack() then
  269.                 collect()
  270.             else
  271.                 sleep( 0.5 )
  272.             end
  273.         end
  274.     elseif xPos < x then
  275.         while xDir ~= 1 do
  276.             turnLeft()
  277.         end
  278.         while xPos < x do
  279.             if turtle.forward() then
  280.                 xPos = xPos + 1
  281.             elseif turtle.dig() or turtle.attack() then
  282.                 collect()
  283.             else
  284.                 sleep( 0.5 )
  285.             end
  286.         end
  287.     end
  288.    
  289.     if zPos > z then
  290.         while zDir ~= -1 do
  291.             turnLeft()
  292.         end
  293.         while zPos > z do
  294.             if turtle.forward() then
  295.                 zPos = zPos - 1
  296.             elseif turtle.dig() or turtle.attack() then
  297.                 collect()
  298.             else
  299.                 sleep( 0.5 )
  300.             end
  301.         end
  302.     elseif zPos < z then
  303.         while zDir ~= 1 do
  304.             turnLeft()
  305.         end
  306.         while zPos < z do
  307.             if turtle.forward() then
  308.                 zPos = zPos + 1
  309.             elseif turtle.dig() or turtle.attack() then
  310.                 collect()
  311.             else
  312.                 sleep( 0.5 )
  313.             end
  314.         end
  315.     end
  316.    
  317.     while depth < y do
  318.         if turtle.down() then
  319.             depth = depth + 1
  320.         elseif turtle.digDown() or turtle.attackDown() then
  321.             collect()
  322.         else
  323.             sleep( 0.5 )
  324.         end
  325.     end
  326.    
  327.     while zDir ~= zd or xDir ~= xd do
  328.         turnLeft()
  329.     end
  330. end
  331.  
  332. if not refuel() then
  333.     print( "Out of Fuel" )
  334.     return
  335. end
  336.  
  337. print("Finding Excavation Floor...")
  338. while not turtle.detectDown() do
  339.     tryDown()
  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.     tryDown()
  403.     tryDown()
  404.     if turtle.detectDown() then
  405.         if turtle.digDown() then
  406.             if not collect() then
  407.                 returnSupplies()
  408.             end
  409.         end
  410.     end
  411. end
  412.  
  413. print( "Returning to surface..." )
  414.  
  415. -- Return to where we started
  416. goTo( 0,0,0,0,-1 )
  417. unload( false )
  418. goTo( 0,0,0,0,1 )
  419.  
  420. -- Seal the hole
  421. if reseal then
  422.     turtle.placeDown()
  423. end
  424.  
  425. print( "Mined "..(collected + unloaded).." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement