Advertisement
Alexr360

Turtle Tunnles

Apr 25th, 2025 (edited)
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.56 KB | None | 0 0
  1. -- grid_mine.lua
  2. -- Mines a grid of parallel tunnels at y=0 until out of fuel.
  3. -- Configure the tunnel length and spacing below:
  4.  
  5. local tunnelLength = 16     -- how long each tunnel runs before shifting over
  6. local spacing      = 4      -- how many blocks between adjacent tunnels
  7.  
  8. --------------------------------------------------------------------------------
  9. -- Helper: try to refuel if out of fuel; returns true if there is fuel available
  10. local function refuelIfNeeded()
  11.   if turtle.getFuelLevel() > 0 then
  12.     return true
  13.   end
  14.   -- try each slot for 1 item
  15.   for slot = 1,16 do
  16.     turtle.select(slot)
  17.     if turtle.refuel(1) then
  18.       print(("Refueled from slot %d → fuel=%d"):format(slot, turtle.getFuelLevel()))
  19.       turtle.select(1)
  20.       return true
  21.     end
  22.   end
  23.   -- no fuel found
  24.   print("❌ Out of fuel, stopping.")
  25.   return false
  26. end
  27.  
  28. -- ensure we start with slot 1 selected
  29. turtle.select(1)
  30.  
  31. --------------------------------------------------------------------------------
  32. -- 1) Get starting GPS position
  33. local x,y,z = gps.locate()
  34. -- round to integer
  35. x = math.floor(x + 0.5)
  36. y = math.floor(y + 0.5)
  37. z = math.floor(z + 0.5)
  38. print(("Starting at GPS x=%d, y=%d, z=%d"):format(x,y,z))
  39.  
  40. --------------------------------------------------------------------------------
  41. -- 2) Move down (digging) to y=0 if above it
  42. if y > 0 then
  43.   print("Moving down to y=0 level...")
  44.   while y > 0 do
  45.     if not refuelIfNeeded() then return end
  46.     if not turtle.down() then
  47.       turtle.digDown()
  48.       if not turtle.down() then
  49.         error("Blocked below, cannot reach y=0")
  50.       end
  51.     end
  52.     y = y - 1
  53.     print("  now at y="..y)
  54.   end
  55. elseif y < 0 then
  56.   print("⚠️  Already below y=0 (y="..y.."), unexpected.")
  57. end
  58.  
  59. --------------------------------------------------------------------------------
  60. -- 3) Mine an endless grid of tunnels until fuel runs out
  61. local direction = 1  -- 1 = moving “forward” along +X, -1 = moving backward
  62.  
  63. while true do
  64.   -- each tunnel segment
  65.   for i = 1, tunnelLength do
  66.     if not refuelIfNeeded() then return end
  67.     if turtle.detect() then turtle.dig() end
  68.     turtle.forward()
  69.   end
  70.  
  71.   -- shift over by `spacing` blocks to start the next tunnel
  72.   if direction == 1 then
  73.     turtle.turnRight()
  74.   else
  75.     turtle.turnLeft()
  76.   end
  77.  
  78.   for i = 1, spacing do
  79.     if not refuelIfNeeded() then return end
  80.     if turtle.detect() then turtle.dig() end
  81.     turtle.forward()
  82.   end
  83.  
  84.   if direction == 1 then
  85.     turtle.turnRight()
  86.   else
  87.     turtle.turnLeft()
  88.   end
  89.  
  90.   direction = -direction
  91. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement