Advertisement
skypop

CC-Excavate

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