Advertisement
Guest User

test1.lua

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