Advertisement
Friendsincode

Untitled

Apr 13th, 2025 (edited)
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.67 KB | None | 0 0
  1. -- 🐢 Smart Strip Miner: Mines 3-high, Snakes, Tracks Fuel + Inventory
  2. -- Chest behind turtle. Coal in slot 16.
  3.  
  4. -- === Interactive Setup ===
  5. print("📐 Set up your mining area:")
  6. write("Enter width (X axis): ") width = tonumber(read())
  7. write("Enter length (Z axis): ") length = tonumber(read())
  8. write("Enter total depth to mine downward (in blocks): ") totalDepth = tonumber(read())
  9.  
  10. if not width or not length or not totalDepth then
  11.   error("❌ Invalid input. Please enter numbers only.")
  12. end
  13.  
  14. local layers = math.floor(totalDepth / 3)
  15.  
  16. -- === State ===
  17. local x, y, z = 0, 0, 0
  18. local dir = 0 -- 0=N, 1=E, 2=S, 3=W
  19.  
  20. -- === Fuel Logic ===
  21. function fillFuelTank()
  22.   local maxFuel = turtle.getFuelLimit()
  23.   local currentFuel = turtle.getFuelLevel()
  24.   local needed = maxFuel - currentFuel
  25.  
  26.   turtle.select(16)
  27.   local coalCount = turtle.getItemCount(16)
  28.   local perCoal = 80
  29.   local coalToUse = math.min(math.ceil(needed / perCoal), coalCount)
  30.  
  31.   if coalToUse > 0 then
  32.     print("⛽ Filling fuel with "..coalToUse.." coal...")
  33.     turtle.refuel(coalToUse)
  34.     print("✅ Fuel: "..turtle.getFuelLevel().."/"..maxFuel)
  35.   else
  36.     error("❌ No coal in slot 16.")
  37.   end
  38. end
  39.  
  40. function calculateFuelTo(xT, yT, zT)
  41.   return math.abs(x - xT) + math.abs(y - yT) + math.abs(z - zT)
  42. end
  43.  
  44. function ensureFuel(required)
  45.   if turtle.getFuelLevel() < required then
  46.     turtle.select(16)
  47.     if not turtle.refuel(1) then
  48.       print("⛽ Insert coal in slot 16 to continue...")
  49.       while not turtle.refuel(1) do sleep(5) end
  50.     end
  51.     print("✅ Refueled to "..turtle.getFuelLevel())
  52.   end
  53. end
  54.  
  55. -- === Position & Direction ===
  56. function updatePosition(move)
  57.   if move == "forward" then
  58.     if dir == 0 then z = z - 1 elseif dir == 1 then x = x + 1
  59.     elseif dir == 2 then z = z + 1 elseif dir == 3 then x = x - 1 end
  60.   elseif move == "back" then
  61.     if dir == 0 then z = z + 1 elseif dir == 1 then x = x - 1
  62.     elseif dir == 2 then z = z - 1 elseif dir == 3 then x = x + 1 end
  63.   elseif move == "down" then y = y - 1
  64.   elseif move == "up" then y = y + 1 end
  65. end
  66.  
  67. function turnLeft() dir = (dir - 1) % 4; turtle.turnLeft() end
  68. function turnRight() dir = (dir + 1) % 4; turtle.turnRight() end
  69. function face(targetDir) while dir ~= targetDir do turnRight() end end
  70.  
  71. -- === Movement ===
  72. function smartForward()
  73.   ensureFuel(1)
  74.   while not turtle.forward() do
  75.     turtle.dig()
  76.     sleep(0.3)
  77.   end
  78.   updatePosition("forward")
  79. end
  80.  
  81. function smartDown()
  82.   ensureFuel(1)
  83.   while not turtle.down() do
  84.     turtle.digDown()
  85.     sleep(0.3)
  86.   end
  87.   updatePosition("down")
  88. end
  89.  
  90. function smartUp()
  91.   ensureFuel(1)
  92.   while not turtle.up() do
  93.     turtle.digUp()
  94.     sleep(0.3)
  95.   end
  96.   updatePosition("up")
  97. end
  98.  
  99. function descendThree()
  100.   for i = 1, 3 do smartDown() end
  101. end
  102.  
  103. -- === Dig Helper ===
  104. function dig3Column()
  105.   turtle.digUp()
  106.   turtle.dig()
  107.   turtle.digDown()
  108. end
  109.  
  110. -- === Inventory ===
  111. function isInventoryFull()
  112.   for i = 1, 15 do if turtle.getItemCount(i) == 0 then return false end end
  113.   return true
  114. end
  115.  
  116. function dumpInventory()
  117.   for i = 1, 15 do
  118.     turtle.select(i)
  119.     turtle.drop()
  120.   end
  121. end
  122.  
  123. -- === Navigation ===
  124. function goTo(xT, yT, zT)
  125.   while y > yT do smartDown() end
  126.   while y < yT do smartUp() end
  127.   if x > xT then face(3) elseif x < xT then face(1) end
  128.   while x ~= xT do smartForward() end
  129.   if z > zT then face(0) elseif z < zT then face(2) end
  130.   while z ~= zT do smartForward() end
  131. end
  132.  
  133. -- === MAIN ===
  134. fillFuelTank()
  135.  
  136. for layer = 1, layers do
  137.   print("⛏️ Mining layer "..layer.." at Y="..y)
  138.  
  139.   for row = 1, width do
  140.     for col = 1, length - 1 do
  141.       dig3Column()
  142.  
  143.       if turtle.getFuelLevel() < calculateFuelTo(0, 0, 0) + 10 then
  144.         print("⚠️ Returning to refuel.")
  145.         local sx, sy, sz, sd = x, y, z, dir
  146.         goTo(0, 0, 0)
  147.         face(2)
  148.         dumpInventory()
  149.         ensureFuel(calculateFuelTo(sx, sy, sz))
  150.         goTo(sx, sy, sz)
  151.         face(sd)
  152.       end
  153.  
  154.       if isInventoryFull() then
  155.         print("📦 Inventory full. Returning.")
  156.         local sx, sy, sz, sd = x, y, z, dir
  157.         goTo(0, 0, 0)
  158.         face(2)
  159.         dumpInventory()
  160.         goTo(sx, sy, sz)
  161.         face(sd)
  162.       end
  163.  
  164.       smartForward()
  165.     end
  166.  
  167.     -- Snake turn
  168.     if row < width then
  169.       local turnFunc = row % 2 == 1 and turnRight or turnLeft
  170.       turnFunc(); smartForward(); dig3Column(); turnFunc()
  171.     end
  172.   end
  173.  
  174.   -- End-of-layer dump and descend
  175.   goTo(0, 0, 0)
  176.   local returnDir = dir
  177.   face(2) -- face South to chest
  178.   dumpInventory()
  179.   face(returnDir)
  180.  
  181.   if layer < layers then descendThree() end
  182. end
  183.  
  184. print("✅ Done mining all layers.")
  185. goTo(0, 0, 0)
  186. face(2)
  187.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement