brickviking

job.lua

May 14th, 2019 (edited)
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.08 KB | None | 0 0
  1. -- Version v0.1 Not the initial cut, but borrowed from somewhere else
  2. -- Tinkered with until I got what I wanted. Pair this up with job.lua
  3. -- Version v0.3 Corrected tunnel code
  4.  
  5. local tArgs = { ... }
  6. if #tArgs ~= 1 then
  7.     print( "Usage: tunnel <length>" )
  8.     return
  9. end
  10.  
  11. -- Mine in a quarry pattern until we hit something we can't dig
  12. local length = tonumber( tArgs[1] )
  13. if length < 1 then
  14.     print( "Tunnel length must be positive" )
  15.     return
  16. end
  17.  
  18. local depth = 0
  19. local collected = 0
  20.  
  21. local function collect()
  22.     collected = collected + 1
  23.     if math.fmod(collected, 25) == 0 then
  24.         print( "Mined "..collected.." items." )
  25.     end
  26. end
  27.  
  28. local function tryDig()
  29.     while turtle.detect() do
  30.         if turtle.dig() then
  31.             collect()
  32.             sleep(0.2)
  33.         else
  34.             return false
  35.         end
  36.     end
  37.     return true
  38. end
  39.  
  40. local function tryDigUp()
  41.     while turtle.detectUp() do
  42.         if turtle.digUp() then
  43.             collect()
  44.             sleep(0.2)
  45.         else
  46.             return false
  47.         end
  48.     end
  49.     return true
  50. end
  51.  
  52. local function tryDigDown()
  53.     while turtle.detectDown() do
  54.         if turtle.digDown() then
  55.             collect()
  56.             sleep(0.2)
  57.         else
  58.             return false
  59.         end
  60.     end
  61.     return true
  62. end
  63.  
  64. local function refuel()
  65.     local fuelLevel = turtle.getFuelLevel()
  66.     if fuelLevel == "unlimited" or fuelLevel > 0 then
  67.         return
  68.     end
  69.  
  70.     local function tryRefuel()
  71.         for n=1,16 do
  72.             if turtle.getItemCount(n) > 0 then
  73.                 turtle.select(n)
  74.                 if turtle.refuel(1) then
  75.                     turtle.select(1)
  76.                     return true
  77.                 end
  78.             end
  79.         end
  80.         turtle.select(1)
  81.         return false
  82.     end
  83.  
  84.     if not tryRefuel() then
  85.         print( "Add more fuel to continue." )
  86.         while not tryRefuel() do
  87.             os.pullEvent( "turtle_inventory" )
  88.         end
  89.         print( "Resuming Tunnel." )
  90.     end
  91. end
  92.  
  93. local function tryUp()
  94.     refuel()
  95.     while not turtle.up() do
  96.         if turtle.detectUp() then
  97.             if not tryDigUp() then
  98.                 return false
  99.             end
  100.         elseif turtle.attackUp() then
  101.             collect()
  102.         else
  103.             sleep( 0.2 )
  104.         end
  105.     end
  106.     return true
  107. end
  108.  
  109. local function tryDown()
  110.     refuel()
  111.     while not turtle.down() do
  112.         if turtle.detectDown() then
  113.             if not tryDigDown() then
  114.                 return false
  115.             end
  116.         elseif turtle.attackDown() then
  117.             collect()
  118.         else
  119.             sleep( 0.2 )
  120.         end
  121.     end
  122.     return true
  123. end
  124.  
  125. local function tryForward()
  126.     refuel()
  127.     while not turtle.forward() do
  128.         if turtle.detect() then
  129.             if not tryDig() then
  130.                 return false
  131.             end
  132.         elseif turtle.attack() then
  133.             collect()
  134.         else
  135.             sleep( 0.2 )
  136.         end
  137.     end
  138.     return true
  139. end
  140.  
  141. print( "Tunnelling..." )
  142. tryDig()
  143. tryForward()
  144.  
  145. for n=1,length do
  146. --    turtle.placeDown()
  147.     -- Front block
  148.     -- Left side Level1/2 (and above)
  149.     turtle.turnLeft()
  150.     tryDig() -- 1
  151.     tryDigUp() -- 2
  152.     tryUp()
  153.     tryDig()
  154.     -- Level 3
  155. --    tryDigUp()
  156. --    tryUp()
  157. --    tryDig()
  158.     -- Now the right side
  159.     turtle.turnRight()
  160.     turtle.turnRight()
  161. --    tryDig()  --3
  162. --    tryDown()
  163.     tryDig()  --2
  164.     tryDown()
  165.     tryDig()  --1
  166.     turtle.turnLeft()
  167.  
  168.     if n<length then
  169.         tryDig()
  170.         if not tryForward() then
  171.             print( "Aborting Tunnel." )
  172.             break
  173.         end
  174.     else
  175.         print( "Tunnel complete." )
  176.     end
  177.  
  178. end
  179.  
  180. --[[
  181. print( "Returning to start..." )
  182.  
  183. -- Return to where we started
  184. turtle.turnLeft()
  185. turtle.turnLeft()
  186. while depth > 0 do
  187.     if turtle.forward() then
  188.         depth = depth - 1
  189.     else
  190.         turtle.dig()
  191.     end
  192. end
  193. turtle.turnRight()
  194. turtle.turnRight()
  195. ]]
  196.  
  197. print( "Tunnel complete." )
  198. print( "Mined "..collected.." items total." )
  199.  
Add Comment
Please, Sign In to add comment