Advertisement
Frayna

excavate_and_stop.lua

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