Advertisement
denvys5

Excavate Advanced

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