Advertisement
McBaron

Untitled

Jun 29th, 2025 (edited)
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.64 KB | Gaming | 0 0
  1. -- Turtle Smart Fill with True Robust Layer Return and Chest Dump
  2.  
  3. local function askNumber(prompt)
  4.     print(prompt)
  5.     local val = tonumber(read())
  6.     while not val do
  7.         print("Please enter a valid number:")
  8.         val = tonumber(read())
  9.     end
  10.     return math.floor(val)
  11. end
  12.  
  13. local function askDirection()
  14.     print("Build direction? Type 'up' for bottom-to-top or 'down' for top-to-bottom:")
  15.     local dir = read()
  16.     while dir ~= "up" and dir ~= "down" do
  17.         print("Please type 'up' or 'down':")
  18.         dir = read()
  19.     end
  20.     return dir
  21. end
  22.  
  23. local moves = {}
  24. local heading = 0 -- 0: forward, 1: right, 2: back, 3: left
  25.  
  26. local function forward()
  27.     while not turtle.forward() do
  28.         turtle.dig()
  29.         sleep(0.2)
  30.     end
  31.     table.insert(moves, {action="fwd"})
  32. end
  33.  
  34. local function moveUp()
  35.     while not turtle.up() do
  36.         turtle.digUp()
  37.         sleep(0.2)
  38.     end
  39. end
  40.  
  41. local function moveDown()
  42.     while not turtle.down() do
  43.         turtle.digDown()
  44.         sleep(0.2)
  45.     end
  46. end
  47.  
  48. local function turnRight()
  49.     turtle.turnRight()
  50.     heading = (heading + 1) % 4
  51.     table.insert(moves, {action="tr"})
  52. end
  53.  
  54. local function turnLeft()
  55.     turtle.turnLeft()
  56.     heading = (heading + 3) % 4
  57.     table.insert(moves, {action="tl"})
  58. end
  59.  
  60. local function turnRightRaw() turtle.turnRight(); heading = (heading + 1) % 4 end
  61. local function turnLeftRaw() turtle.turnLeft(); heading = (heading + 3) % 4 end
  62.  
  63. local function refuelIfNeeded(needed)
  64.     turtle.select(1)
  65.     if turtle.getFuelLevel() == "unlimited" then return true end
  66.     if turtle.getFuelLevel() < needed then
  67.         print("Refueling from slot 1...")
  68.         if not turtle.refuel(64) then
  69.             print("⚠️ Could not refuel from slot 1.")
  70.         else
  71.             print("Fuel level: "..turtle.getFuelLevel())
  72.         end
  73.     end
  74.     if turtle.getFuelLevel() < needed then
  75.         print("❌ Not enough fuel. Need at least "..needed..".")
  76.         return false
  77.     end
  78.     return true
  79. end
  80.  
  81. local function dumpToChest()
  82.     print("Dumping items to chest...")
  83.     for i = 2, 16 do
  84.         turtle.select(i)
  85.         local item = turtle.getItemDetail()
  86.         if item then
  87.             if not (item.name == targetBlockName) then
  88.                 turtle.drop()
  89.             end
  90.         end
  91.     end
  92. end
  93.  
  94. local function isInventoryFull()
  95.     for i = 3, 16 do
  96.         if turtle.getItemCount(i) == 0 then return false end
  97.     end
  98.     return true
  99. end
  100.  
  101. local function placeSlot2Block()
  102.     turtle.digDown()
  103.     if turtle.detectDown() then return true end
  104.     for i = 2, 16 do
  105.         turtle.select(i)
  106.         local item = turtle.getItemDetail()
  107.         if item and item.name == targetBlockName then
  108.             return turtle.placeDown()
  109.         end
  110.     end
  111.     print("⚠️ No matching block left to place.")
  112.     return false
  113. end
  114.  
  115. -- Ask user
  116. local length = askNumber("Enter length:")
  117. local width = askNumber("Enter width:")
  118. local height = askNumber("Enter height:")
  119. local direction = askDirection()
  120.  
  121. -- Fuel estimate/check
  122. local estimatedMoves = length * width * height * 3 + (height - 1) * (length + width)
  123. print("Estimated fuel needed: "..estimatedMoves)
  124. if not refuelIfNeeded(estimatedMoves) then return end
  125.  
  126. -- Block type from slot 2
  127. turtle.select(2)
  128. local slot2Block = turtle.getItemDetail()
  129. if not slot2Block then
  130.     print("❌ No block found in slot 2. Please load desired block.")
  131.     return
  132. end
  133. targetBlockName = slot2Block.name
  134. print("📦 Using only block type: "..targetBlockName)
  135. turtle.select(2)
  136.  
  137. print("Place a chest in front of turtle's starting position for dumps.")
  138.  
  139. -- Move forward to first block (starting reference)
  140. forward()
  141.  
  142. -- True robust: return to origin of the layer and clear moves
  143. local function returnToOrigin()
  144.     -- Face forward
  145.     while heading ~= 0 do
  146.         turnRightRaw()
  147.     end
  148.     -- Undo all moves in reverse order
  149.     for i = #moves, 1, -1 do
  150.         local move = moves[i]
  151.         if move.action == "fwd" then
  152.             turtle.back()
  153.         elseif move.action == "up" then
  154.             turtle.down()
  155.         elseif move.action == "down" then
  156.             turtle.up()
  157.         elseif move.action == "tr" then
  158.             turtle.turnLeft()
  159.             heading = (heading + 3) % 4
  160.         elseif move.action == "tl" then
  161.             turtle.turnRight()
  162.             heading = (heading + 1) % 4
  163.         end
  164.     end
  165.     moves = {}
  166. end
  167.  
  168. -- Build one layer, robust return at end
  169. local function buildLayer()
  170.     for w = 1, width do
  171.         for l = 1, length do
  172.             turtle.dig()
  173.             if not placeSlot2Block() then return false end
  174.             if isInventoryFull() then
  175.                 print("Inventory full! Returning to dump in chest.")
  176.                 return "dump"
  177.             end
  178.             if l < length then forward() end
  179.         end
  180.         if w < width then
  181.             if w % 2 == 1 then
  182.                 turnRight()
  183.                 forward()
  184.                 turnRight()
  185.             else
  186.                 turnLeft()
  187.                 forward()
  188.                 turnLeft()
  189.             end
  190.         end
  191.     end
  192.     -- Always return to start of layer (lower/upper left corner, facing original)
  193.     returnToOrigin()
  194.     return true
  195. end
  196.  
  197. -- Main build logic, moving up/down *after* return, clearing moves each time
  198. local function build()
  199.     if direction == "up" then
  200.         for h = 1, height do
  201.             print("Building layer "..h.." / "..height)
  202.             local layerResult = buildLayer()
  203.             if layerResult == "dump" then
  204.                 dumpToChest()
  205.                 print("Please clear inventory and re-run script to finish the area!")
  206.                 return
  207.             elseif not layerResult then return end
  208.             if h < height then
  209.                 moveUp()
  210.                 moves = {} -- CLEAR move log at start of new layer
  211.             end
  212.         end
  213.     else
  214.         for i = 1, height - 1 do moveUp() end
  215.         for h = height, 1, -1 do
  216.             print("Building layer "..h.." / "..height)
  217.             local layerResult = buildLayer()
  218.             if layerResult == "dump" then
  219.                 dumpToChest()
  220.                 print("Please clear inventory and re-run script to finish the area!")
  221.                 return
  222.             elseif not layerResult then return end
  223.             if h > 1 then
  224.                 moveDown()
  225.                 moves = {} -- CLEAR move log at start of new layer
  226.             end
  227.         end
  228.     end
  229.     dumpToChest()
  230.     -- At very end, turtle is back at original start, facing forward.
  231. end
  232.  
  233. build()
  234. print("✅ Build complete! Returned to starting point and dumped inventory.")
  235.  
Tags: fill
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement