krom

Excav2

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