Bmorr

btunnel.lua

Jun 2nd, 2021 (edited)
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.23 KB | None | 0 0
  1. --[[
  2.     pastebin get FJS8kYTw btunnel.lua
  3. --]]
  4.  
  5.  
  6. local tArgs = { ... }
  7. if not (#tArgs >= 1) then
  8.     local programName = arg[0] or fs.getName(shell.getRunningProgram())
  9.     print("Usage: " .. programName .. " <length>")
  10.     return
  11. end
  12.  
  13. local length = tonumber(tArgs[1])
  14. if length < 1 then
  15.     print("Tunnel length must be positive")
  16.     return
  17. end
  18.  
  19. -- Mining Functions
  20. function tryDig()
  21.     while turtle.detect() do
  22.         if turtle.dig() then
  23.             sleep(0.5)
  24.         else
  25.             return false
  26.         end
  27.     end
  28.     return true
  29. end
  30.  
  31. function tryDigUp()
  32.     while turtle.detectUp() do
  33.         if turtle.digUp() then
  34.             sleep(0.5)
  35.         else
  36.             return false
  37.         end
  38.     end
  39.     return true
  40. end
  41.  
  42. function tryDigDown()
  43.     while turtle.detectDown() do
  44.         if turtle.digDown() then
  45.             sleep(0.5)
  46.         else
  47.             return false
  48.         end
  49.     end
  50.     return true
  51. end
  52.  
  53.  
  54. -- Refueling
  55. function refuel()
  56.     local fuelLevel = turtle.getFuelLevel()
  57.     if fuelLevel == "unlimited" or fuelLevel > 0 then
  58.         return
  59.     end
  60.  
  61.     function tryRefuel()
  62.         for n = 1, 16 do
  63.             if turtle.getItemCount(n) > 0 then
  64.                 turtle.select(n)
  65.                 if turtle.refuel(1) then
  66.                     turtle.select(1)
  67.                     return true
  68.                 end
  69.             end
  70.         end
  71.         turtle.select(1)
  72.         return false
  73.     end
  74.  
  75.     if not tryRefuel() then
  76.         print("Add more fuel to continue.")
  77.         while not tryRefuel() do
  78.             os.pullEvent("turtle_inventory")
  79.         end
  80.         print("Resuming Tunnel.")
  81.     end
  82. end
  83.  
  84. -- Movement Functions
  85. local pos, dir = vector.new(0, 0, 0), 0
  86.  
  87. local cardinal_directions = {}
  88. cardinal_directions["N"] = 0
  89. cardinal_directions["E"] = 1
  90. cardinal_directions["S"] = 2
  91. cardinal_directions["W"] = 3
  92. cardinal_directions[0] = vector.new( 0,  0, -1)
  93. cardinal_directions[1] = vector.new(1,  0,  0)
  94. cardinal_directions[2] = vector.new( 0,  0, 1)
  95. cardinal_directions[3] = vector.new(-1,  0,  0)
  96.  
  97. local up, down = vector.new(0, 1, 0), vector.new(0, -1, 0)
  98.  
  99. local function iDir(num) -- Increment dir
  100.     local ldir = dir + num  -- local dir
  101.     if ldir < 0 then
  102.         ldir = ldir + 4
  103.     elseif ldir > 3 then
  104.         ldir = ldir - 4
  105.     end
  106.     return ldir
  107. end
  108.  
  109. function tryUp()
  110.     refuel()
  111.     while not turtle.up() do
  112.         if turtle.detectUp() then
  113.             if not tryDigUp() then
  114.                 return false
  115.             end
  116.         else
  117.             sleep(0.5)
  118.         end
  119.     end
  120.     pos = pos + up
  121.     print("Moved to", pos)
  122.     return true
  123. end
  124.  
  125. function tryDown()
  126.     refuel()
  127.     while not turtle.down() do
  128.         if turtle.detectDown() then
  129.             if not tryDigDown() then
  130.                 return false
  131.             end
  132.         else
  133.             sleep(0.5)
  134.         end
  135.     end
  136.     pos = pos + down
  137.     print("Moved to", pos)
  138.     return true
  139. end
  140.  
  141. function tryForward(doprint)
  142.     doprint = doprint or true
  143.     refuel()
  144.     while not turtle.forward() do
  145.         if turtle.detect() then
  146.             if not tryDig() then
  147.                 return false
  148.             end
  149.         else
  150.             sleep(0.5)
  151.         end
  152.     end
  153.     pos = pos + cardinal_directions[dir]
  154.     if doprint then
  155.         print("Moved to", pos)
  156.     end
  157.     return true
  158. end
  159.  
  160. function tryBack()
  161.     refuel()
  162.     if not turtle.back() then
  163.         turnLeft(2)
  164.         local ret = tryForward()
  165.         turnLeft(2)
  166.         return ret
  167.     else
  168.         pos = pos - cardinal_directions[dir]
  169.         print("Moved to", pos)
  170.     end
  171.     return true
  172. end
  173.  
  174. function turnRight(count)
  175.     count = count or 1
  176.     turtle.turnRight()
  177.     dir = iDir(1)
  178.     if count > 1 then
  179.         turnRight(count - 1)
  180.     end
  181. end
  182.  
  183. function turnLeft(count)
  184.     count = count or 1
  185.     turtle.turnLeft()
  186.     dir = iDir(-1)
  187.     if count > 1 then
  188.         turnLeft(count - 1)
  189.     end
  190. end
  191.  
  192. for i = 1, length do
  193.     tryForward()
  194.     turnLeft()
  195.     tryDig()
  196.     tryUp()
  197.     tryDig()
  198.     tryUp()
  199.     tryDig()
  200.     turnRight(2)
  201.     tryDig()
  202.     tryDown()
  203.     tryDig()
  204.     tryDown()
  205.     tryDig()
  206.     turnLeft()
  207. end
  208.  
  209. for i = 1, length do
  210.     tryBack()
  211. end
  212.  
Add Comment
Please, Sign In to add comment