EvaKnievel

Excavate

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