Kasama

excavate

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