MigasRocha

Teste

Dec 9th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.46 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,15 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.                 turtle.select(16)
  35.                 turtle.place()
  36.                 turtle.drop()
  37.                 unloaded = uloaded + nCount
  38.             end        
  39.             if bDrop then
  40.                 turtle.select(16)
  41.                 turtle.place()
  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 function tryForwards()
  127.     if not refuel() then
  128.         print( "Not enough Fuel" )
  129.         returnSupplies()
  130.     end
  131.    
  132.     while not turtle.forward() do
  133.         if turtle.detect() then
  134.             if turtle.dig() then
  135.                 if not collect() then
  136.                     returnSupplies()
  137.                 end
  138.             else
  139.                 return false
  140.             end
  141.         elseif turtle.attack() then
  142.             if not collect() then
  143.                 returnSupplies()
  144.             end
  145.         else
  146.             sleep( 0.5 )
  147.         end
  148.     end
  149.    
  150.     xPos = xPos + xDir
  151.     zPos = zPos + zDir
  152.     return true
  153. end
  154.  
  155. local function tryDown()
  156.     if not refuel() then
  157.         print( "Not enough Fuel" )
  158.         returnSupplies()
  159.     end
  160.    
  161.     while not turtle.down() do
  162.         if turtle.detectDown() then
  163.             if turtle.digDown() then
  164.                 if not collect() then
  165.                     returnSupplies()
  166.                 end
  167.             else
  168.                 return false
  169.             end
  170.         elseif turtle.attackDown() then
  171.             if not collect() then
  172.                 returnSupplies()
  173.             end
  174.         else
  175.             sleep( 0.5 )
  176.         end
  177.     end
  178.  
  179.     depth = depth + 1
  180.     if math.fmod( depth, 10 ) == 0 then
  181.         print( "Descended "..depth.." metres." )
  182.     end
  183.  
  184.     return true
  185. end
  186.  
  187. local function turnLeft()
  188.     turtle.turnLeft()
  189.     xDir, zDir = -zDir, xDir
  190. end
  191.  
  192. local function turnRight()
  193.     turtle.turnRight()
  194.     xDir, zDir = zDir, -xDir
  195. end
  196.  
  197. function goTo( x, y, z, xd, zd )
  198.     while depth > y do
  199.         if turtle.up() then
  200.             depth = depth - 1
  201.         elseif turtle.digUp() or turtle.attackUp() then
  202.             collect()
  203.         else
  204.             sleep( 0.5 )
  205.         end
  206.     end
  207.  
  208.     if xPos > x then
  209.         while xDir ~= -1 do
  210.             turnLeft()
  211.         end
  212.         while xPos > x do
  213.             if turtle.forward() then
  214.                 xPos = xPos - 1
  215.             elseif turtle.dig() or turtle.attack() then
  216.                 collect()
  217.             else
  218.                 sleep( 0.5 )
  219.             end
  220.         end
  221.     elseif xPos < x then
  222.         while xDir ~= 1 do
  223.             turnLeft()
  224.         end
  225.         while xPos < x do
  226.             if turtle.forward() then
  227.                 xPos = xPos + 1
  228.             elseif turtle.dig() or turtle.attack() then
  229.                 collect()
  230.             else
  231.                 sleep( 0.5 )
  232.             end
  233.         end
  234.     end
  235.    
  236.     if zPos > z then
  237.         while zDir ~= -1 do
  238.             turnLeft()
  239.         end
  240.         while zPos > z do
  241.             if turtle.forward() then
  242.                 zPos = zPos - 1
  243.             elseif turtle.dig() or turtle.attack() then
  244.                 collect()
  245.             else
  246.                 sleep( 0.5 )
  247.             end
  248.         end
  249.     elseif zPos < z then
  250.         while zDir ~= 1 do
  251.             turnLeft()
  252.         end
  253.         while zPos < z do
  254.             if turtle.forward() then
  255.                 zPos = zPos + 1
  256.             elseif turtle.dig() or turtle.attack() then
  257.                 collect()
  258.             else
  259.                 sleep( 0.5 )
  260.             end
  261.         end
  262.     end
  263.    
  264.     while depth < y do
  265.         if turtle.down() then
  266.             depth = depth + 1
  267.         elseif turtle.digDown() or turtle.attackDown() then
  268.             collect()
  269.         else
  270.             sleep( 0.5 )
  271.         end
  272.     end
  273.    
  274.     while zDir ~= zd or xDir ~= xd do
  275.         turnLeft()
  276.     end
  277. end
  278.  
  279. if not refuel() then
  280.     print( "Out of Fuel" )
  281.     return
  282. end
  283.  
  284. print( "Excavating..." )
  285.  
  286. local reseal = false
  287. turtle.select(1)
  288. if turtle.digDown() then
  289.     reseal = true
  290. end
  291.  
  292. local alternate = 0
  293. local done = false
  294. while not done do
  295.     for n=1,size do
  296.         for m=1,size-1 do
  297.             if not tryForwards() then
  298.                 done = true
  299.                 break
  300.             end
  301.         end
  302.         if done then
  303.             break
  304.         end
  305.         if n<size then
  306.             if math.fmod(n + alternate,2) == 0 then
  307.                 turnLeft()
  308.                 if not tryForwards() then
  309.                     done = true
  310.                     break
  311.                 end
  312.                 turnLeft()
  313.             else
  314.                 turnRight()
  315.                 if not tryForwards() then
  316.                     done = true
  317.                     break
  318.                 end
  319.                 turnRight()
  320.             end
  321.         end
  322.     end
  323.     if done then
  324.         break
  325.     end
  326.    
  327.     if size > 1 then
  328.         if math.fmod(size,2) == 0 then
  329.             turnRight()
  330.         else
  331.             if alternate == 0 then
  332.                 turnLeft()
  333.             else
  334.                 turnRight()
  335.             end
  336.             alternate = 1 - alternate
  337.         end
  338.     end
  339.    
  340.     if not tryDown() then
  341.         done = true
  342.         break
  343.     end
  344. end
  345.  
  346. print( "Returning to surface..." )
  347.  
  348. -- Return to where we started
  349. goTo( 0,0,0,0,-1 )
  350. unload( false )
  351. goTo( 0,0,0,0,1 )
  352.  
  353. -- Seal the hole
  354. if reseal then
  355.     turtle.placeDown()
  356. end
  357.  
  358. print( "Mined "..(collected + unloaded).." items total." )
Add Comment
Please, Sign In to add comment