Advertisement
EvilBMO

3x3 refuel test 1

Apr 28th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.11 KB | None | 0 0
  1. local tArgs = { ... }
  2. if #tArgs ~= 1 then
  3.     print( "Usage: tunnel <length>" )
  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. if length < 1 then
  10.     print( "Tunnel length must be positive" )
  11.     return
  12. end
  13.    
  14. local total = 0
  15.         -- Total is the furthest it has mined so far
  16. local pos = 0
  17.         -- Pos is its current position relative to its starting position
  18. local b = 10
  19.         -- b is the total distance it must mine before returning to the chest
  20. local depth = 0
  21. local collected = 0
  22.  
  23. local function collect()
  24.     collected = collected + 1
  25.     if math.fmod(collected, 25) == 0 then
  26.         print( "Mined "..collected.." items." )
  27.     end
  28. end
  29.  
  30. local function tryDig()
  31.     while turtle.detect() do
  32.         if turtle.dig() then
  33.             collect()
  34.             sleep(0.5)
  35.         else
  36.             return false
  37.         end
  38.     end
  39.     return true
  40. end
  41.  
  42. local function tryDigUp()
  43.     while turtle.detectUp() do
  44.         if turtle.digUp() then
  45.             collect()
  46.             sleep(0.5)
  47.         else
  48.             return false
  49.         end
  50.     end
  51.     return true
  52. end
  53.  
  54. local function tryDigDown()
  55.     while turtle.detectDown() do
  56.         if turtle.digDown() then
  57.             collect()
  58.             sleep(0.5)
  59.         else
  60.             return false
  61.         end
  62.     end
  63.     return true
  64. end
  65.  
  66. local function refuel()
  67.     local fuelLevel = turtle.getFuelLevel()
  68.     if fuelLevel == "unlimited" or fuelLevel > 0 then
  69.         return
  70.     end
  71.    
  72.     local function tryRefuel()
  73.         for n=1,16 do
  74.             if turtle.getItemCount(n) > 0 then
  75.                 turtle.select(n)
  76.                 if turtle.refuel(1) then
  77.                     turtle.select(1)
  78.                     return true
  79.                 end
  80.             end
  81.         end
  82.         turtle.select(1)
  83.         return false
  84.     end
  85.    
  86.     if not tryRefuel() then
  87.         print( "Add more fuel to continue." )
  88.         while not tryRefuel() do
  89.             sleep(1)
  90.         end
  91.         print( "Resuming Tunnel." )
  92.     end
  93. end
  94.  
  95. local function tryUp()
  96.     refuel()
  97.     while not turtle.up() do
  98.         if turtle.detectUp() then
  99.             if not tryDigUp() then
  100.                 return false
  101.             end
  102.         elseif turtle.attackUp() then
  103.             collect()
  104.         else
  105.             sleep( 0.5 )
  106.         end
  107.     end
  108.     return true
  109. end
  110.  
  111. local function tryDown()
  112.     refuel()
  113.     while not turtle.down() do
  114.         if turtle.detectDown() then
  115.             if not tryDigDown() then
  116.                 return false
  117.             end
  118.         elseif turtle.attackDown() then
  119.             collect()
  120.         else
  121.             sleep( 0.5 )
  122.         end
  123.     end
  124.     return true
  125. end
  126.  
  127. local function unload( _bKeepOneFuelStack )
  128.     print( "Unloading items..." )
  129.     for n=1,16 do
  130.         local nCount = turtle.getItemCount(n)
  131.         if nCount > 0 then
  132.             turtle.select(n)           
  133.             local bDrop = true
  134.             if _bKeepOneFuelStack and turtle.refuel(0) then
  135.                 bDrop = false
  136.                 _bKeepOneFuelStack = false
  137.             end        
  138.             if bDrop then
  139.                 turtle.drop()
  140.                 unloaded = unloaded + nCount
  141.             end
  142.         end
  143.     end
  144.     collected = 0
  145.     turtle.select(1)
  146. end
  147.  
  148. local function gohome()
  149.     if pos>0 then
  150.         turtle.forward()
  151.         pos = pos - 1
  152.     else
  153.         turtle.turnleft()
  154.         turtle.turnleft()
  155.         unload( _bKeepOneFuelStack )
  156.         resume()
  157.     end
  158. end
  159.    
  160.  
  161.  
  162.    
  163. local function resume()
  164.     if pos<total then
  165.         turtle.forward()
  166.         pos = pos + 1
  167.     else
  168.         sleep( 0.5 )
  169.     end
  170. end
  171.  
  172. local function tryForward()
  173.     refuel()
  174.     while not turtle.forward() do
  175.         if turtle.detect() then
  176.             if not tryDig() then
  177.                 return false
  178.             end
  179.         elseif turtle.attack() then
  180.             collect()
  181.             pos = pos + 1
  182.             total = total + 1
  183.         else
  184.             sleep( 0.5 )
  185.         end
  186.     end
  187.     return true
  188. end
  189.  
  190. print( "Tunnelling..." )
  191.  
  192. for n=1,length do
  193.     turtle.placeDown()
  194.     tryDigUp()
  195.     turtle.turnLeft()
  196.     tryDig()
  197.     tryUp()
  198.     tryDig()
  199.     tryUp()
  200.     tryDig()
  201.     turtle.turnRight()
  202.     turtle.turnRight()
  203.     tryDig()
  204.     tryDown()
  205.     tryDig()
  206.     tryDown()
  207.     tryDig()
  208.     turtle.turnLeft()
  209.    
  210.    
  211.     if pos=total then
  212.         turtle.turnleft()
  213.         turtle.turnleft()
  214.         gohome()
  215.     else
  216.         sleep( 0.5 )
  217.     end
  218.  
  219.        
  220.        
  221.    
  222.     if n<length then
  223.         tryDig()
  224.         if not tryForward() then
  225.             print( "Aborting Tunnel." )
  226.             break
  227.         end
  228.     else
  229.         print( "Tunnel complete." )
  230.     end
  231.  
  232. end
  233.  
  234. --[[
  235. print( "Returning to start..." )
  236.  
  237. -- Return to where we started
  238. turtle.turnLeft()
  239. turtle.turnLeft()
  240. while depth > 0 do
  241.     if turtle.forward() then
  242.         depth = depth - 1
  243.     else
  244.         turtle.dig()
  245.     end
  246. end
  247. turtle.turnRight()
  248. turtle.turnRight()
  249. ]]
  250.  
  251. print( "Tunnel complete." )
  252. print( "Mined "..collected.." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement