Advertisement
SuchyYT

Tunnel V2

Mar 23rd, 2024 (edited)
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.32 KB | None | 0 0
  1. -- Function to place a torch behind the turtle
  2. local function placeTorch()
  3.     turtle.select(2) -- Select torch slot
  4.     turtle.place()  -- Place torch
  5. end
  6.  
  7. -- Function to drop items behind the turtle when inventory is full
  8. local function dropItems()
  9.     for slot = 3, 16 do
  10.         turtle.select(slot)
  11.         turtle.drop()
  12.     end
  13. end
  14.  
  15. -- Main function to dig the tunnel
  16. local function digTunnel(length, resumeIfNoTorches)
  17.     local torchInterval = 10
  18.     local torchesAvailable = true
  19.     local fuelLevel = turtle.getFuelLevel()
  20.  
  21.     for i = 1, length do
  22.         -- Check fuel level and refuel if needed
  23.         if fuelLevel == nil or fuelLevel <= 0 then
  24.             print("Out of fuel!")
  25.             -- Attempt to refuel
  26.             if not turtle.refuel() then
  27.                 print("Unable to refuel!")
  28.                 return
  29.             end
  30.             fuelLevel = turtle.getFuelLevel() -- Update fuel level
  31.         end
  32.  
  33.         -- Dig forward
  34.         if not turtle.dig() then
  35.             -- If unable to dig, try to move forward and dig again
  36.             turtle.forward()
  37.             turtle.dig()
  38.         end
  39.  
  40.         -- Drop items behind the turtle if inventory is full
  41.         if turtle.getItemCount(16) > 0 then
  42.             dropItems()
  43.         end
  44.  
  45.         -- Place torch every torchInterval blocks behind the turtle
  46.         if torchesAvailable and i % torchInterval == 0 then
  47.             if turtle.getItemCount(2) > 0 then
  48.                 placeTorch()
  49.             else
  50.                 print("Out of torches!")
  51.                 if not resumeIfNoTorches then
  52.                     return
  53.                 end
  54.                 torchesAvailable = false
  55.             end
  56.         end
  57.  
  58.         -- Move up and clear any obstacles
  59.         if not turtle.digUp() then
  60.             turtle.up()
  61.         end
  62.     end
  63. end
  64.  
  65. -- Main program
  66. local args = {...}
  67. local tunnelLength = tonumber(args[1])
  68. local resumeIfNoTorches = args[2] == "true"
  69.  
  70. if tunnelLength == nil or tunnelLength <= 0 then
  71.     print("Invalid tunnel length. Please provide a positive number as argument.")
  72.     return
  73. end
  74.  
  75. if tunnelLength == math.huge then
  76.     tunnelLength = 999999999 -- Set a large finite number for infinite tunneling
  77. end
  78.  
  79. print("Digging tunnel...")
  80. digTunnel(tunnelLength, resumeIfNoTorches)
  81. print("Tunneling complete.")
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement