koriar

ex5

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