Guest User

mine

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