Advertisement
ZGAMMAA

Branch

Apr 11th, 2025
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.09 KB | None | 0 0
  1. -- Branch Mining Turtle Program
  2. -- Digs 2-high main tunnel with branches every 2 blocks
  3. -- Ignores cobblestone, returns to refuel/unload at origin
  4.  
  5. -- === CONFIG ===
  6. local MAIN_TUNNEL_LENGTH = 50      -- Length of the main tunnel
  7. local BRANCH_LENGTH = 6            -- How far each side branch goes
  8. local BRANCH_SPACING = 2           -- How often to branch
  9. local TORCH_INTERVAL = 10
  10. local TORCH_SLOT = 16
  11. local FUEL_BUFFER = 100
  12.  
  13. -- === STATE ===
  14. local step = 0
  15. local x, y, z, dir = 0, 0, 0, 1 -- Assume facing East
  16. local startX, startY, startZ, startDir = 0, 0, 0, 1
  17.  
  18. local cobbleNames = {
  19.   ["minecraft:cobblestone"] = true,
  20.   ["minecraft:stone"] = true,
  21.   ["minecraft:gravel"] = true
  22. }
  23.  
  24. -- === DIRECTION ===
  25. local function update(dx, dy, dz) x, y, z = x + dx, y + dy, z + dz end
  26. local function turnRight() turtle.turnRight(); dir = (dir + 1) % 4 end
  27. local function turnLeft() turtle.turnLeft(); dir = (dir - 1) % 4 end
  28. local function face(d) while dir ~= d do turnRight() end end
  29.  
  30. -- === MOVEMENT ===
  31. local function tryForward()
  32.   while not turtle.forward() do turtle.dig(); sleep(0.2) end
  33.   if dir == 0 then update(0, 0, -1)
  34.   elseif dir == 1 then update(1, 0, 0)
  35.   elseif dir == 2 then update(0, 0, 1)
  36.   elseif dir == 3 then update(-1, 0, 0) end
  37. end
  38.  
  39. local function tryUp()
  40.   while not turtle.up() do turtle.digUp(); sleep(0.2) end
  41.   update(0, 1, 0)
  42. end
  43.  
  44. local function tryDown()
  45.   while not turtle.down() do turtle.digDown(); sleep(0.2) end
  46.   update(0, -1, 0)
  47. end
  48.  
  49. -- === POSITIONING ===
  50. local function goTo(tx, ty, tz)
  51.   while y > ty do tryUp() end
  52.   while y < ty do tryDown() end
  53.   if x < tx then face(1) while x < tx do tryForward() end end
  54.   if x > tx then face(3) while x > tx do tryForward() end end
  55.   if z < tz then face(2) while z < tz do tryForward() end end
  56.   if z > tz then face(0) while z > tz do tryForward() end end
  57. end
  58.  
  59. local function returnToOrigin()
  60.   goTo(startX, startY, startZ)
  61.   face(startDir)
  62. end
  63.  
  64. -- === INVENTORY ===
  65. local function isFull()
  66.   for i = 1, 15 do
  67.     if turtle.getItemCount(i) == 0 then return false end
  68.   end
  69.   return true
  70. end
  71.  
  72. local function dropCobble()
  73.   for i = 1, 15 do
  74.     local detail = turtle.getItemDetail(i)
  75.     if detail and cobbleNames[detail.name] then
  76.       turtle.select(i)
  77.       turtle.drop()
  78.     end
  79.   end
  80. end
  81.  
  82. local function unloadInventory()
  83.   print("📦 Unloading...")
  84.   local ox, oy, oz, od = x, y, z, dir
  85.   returnToOrigin()
  86.   face((startDir + 2) % 4)
  87.   for i = 1, 15 do turtle.select(i); turtle.drop() end
  88.   face(startDir)
  89.   goTo(ox, oy, oz); face(od)
  90. end
  91.  
  92. -- === FUEL ===
  93. local function refuelIfNeeded()
  94.   if turtle.getFuelLevel() == "unlimited" or turtle.getFuelLevel() > FUEL_BUFFER then return end
  95.   print("🔋 Refueling...")
  96.   local ox, oy, oz, od = x, y, z, dir
  97.   returnToOrigin()
  98.   face((startDir - 1) % 4)
  99.   for i = 1, 16 do
  100.     turtle.select(i)
  101.     if turtle.suck() and turtle.refuel() then break end
  102.   end
  103.   face(startDir)
  104.   goTo(ox, oy, oz); face(od)
  105. end
  106.  
  107. -- === DIGGING ===
  108. local function digTunnelSection()
  109.   turtle.dig()
  110.   tryForward()
  111.   turtle.digUp()
  112.   tryUp()
  113.   placeTorch()
  114.   tryDown()
  115. end
  116.  
  117. -- === TORCH ===
  118. local function placeTorch()
  119.   if step % TORCH_INTERVAL == 0 then
  120.     turtle.select(TORCH_SLOT)
  121.     turtle.placeUp()
  122.     print("🕯️ Torch placed")
  123.   end
  124. end
  125.  
  126. local function digBranch(dirOffset)
  127.   local originalDir = dir
  128.   if dirOffset == "left" then turnLeft() else turnRight() end
  129.   for i = 1, BRANCH_LENGTH do
  130.     turtle.dig()
  131.     tryForward()
  132.     turtle.digUp()
  133.     tryUp()
  134.     tryDown()
  135.   end
  136.   -- Turn around and return
  137.   turnRight(); turnRight()
  138.   for i = 1, BRANCH_LENGTH do tryForward() end
  139.   face(originalDir)
  140. end
  141.  
  142. -- === MAIN ===
  143. startX, startY, startZ, startDir = x, y, z, dir
  144. print("🚜 Starting branch mining...")
  145.  
  146. for i = 1, MAIN_TUNNEL_LENGTH do
  147.   digTunnelSection()
  148.   step = step + 1
  149.  
  150.   if step % BRANCH_SPACING == 0 then
  151.     digBranch("left")
  152.     digBranch("right")
  153.   end
  154.  
  155.   dropCobble()
  156.   if isFull() then unloadInventory() end
  157.   refuelIfNeeded()
  158. end
  159.  
  160. returnToOrigin()
  161. print("✅ Mining complete. Returned to start.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement