Advertisement
McBaron

Untitled

Jun 14th, 2025
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.35 KB | Gaming | 0 0
  1. -- Turtle Dirt Filler with Debug Feedback
  2.  
  3. -- Function to prompt for numeric input
  4. local function askForNumber(prompt)
  5.     print(prompt)
  6.     local input = read()
  7.     local number = tonumber(input)
  8.     while not number do
  9.         print("Please enter a valid number.")
  10.         input = read()
  11.         number = tonumber(input)
  12.     end
  13.     return number
  14. end
  15.  
  16. -- Ask for area dimensions
  17. local length = askForNumber("Enter length:")
  18. local width = askForNumber("Enter width:")
  19. local height = askForNumber("Enter height:")
  20.  
  21. print("Preparing to build area of size "..length.." x "..width.." x "..height)
  22.  
  23. -- Function to select dirt in inventory
  24. local function selectDirt()
  25.     for i = 1, 16 do
  26.         turtle.select(i)
  27.         local item = turtle.getItemDetail()
  28.         if item and item.name:find("dirt") then
  29.             print("Selected dirt in slot "..i)
  30.             return true
  31.         end
  32.     end
  33.     print("No dirt found in inventory!")
  34.     return false
  35. end
  36.  
  37. -- Place dirt below turtle
  38. local function placeDown()
  39.     if turtle.detectDown() then
  40.         print("Block already exists below.")
  41.         return true
  42.     end
  43.  
  44.     if not turtle.placeDown() then
  45.         print("Trying to select dirt to place...")
  46.         if not selectDirt() then
  47.             print("ERROR: Out of dirt. Cannot continue.")
  48.             return false
  49.         end
  50.         if not turtle.placeDown() then
  51.             print("ERROR: Failed to place dirt even after selecting.")
  52.             return false
  53.         end
  54.     end
  55.  
  56.     print("Placed dirt below.")
  57.     return true
  58. end
  59.  
  60. -- Move forward with digging if needed
  61. local function forward()
  62.     while not turtle.forward() do
  63.         print("Blocked. Digging forward...")
  64.         turtle.dig()
  65.         sleep(0.5)
  66.     end
  67. end
  68.  
  69. -- Move up with digging if needed
  70. local function moveUp()
  71.     while not turtle.up() do
  72.         print("Blocked above. Digging up...")
  73.         turtle.digUp()
  74.         sleep(0.5)
  75.     end
  76. end
  77.  
  78. -- Build one horizontal layer
  79. local function buildLayer(layerNum)
  80.     print("Starting layer "..layerNum.."...")
  81.     for w = 1, width do
  82.         for l = 1, length do
  83.             if not placeDown() then
  84.                 print("Stopping layer due to placement failure.")
  85.                 return false
  86.             end
  87.             if l < length then
  88.                 forward()
  89.             end
  90.         end
  91.  
  92.         -- Move to next row
  93.         if w < width then
  94.             if w % 2 == 1 then
  95.                 turtle.turnRight()
  96.                 forward()
  97.                 turtle.turnRight()
  98.             else
  99.                 turtle.turnLeft()
  100.                 forward()
  101.                 turtle.turnLeft()
  102.             end
  103.         end
  104.     end
  105.  
  106.     -- Return to starting row direction
  107.     if width % 2 == 1 then
  108.         turtle.turnRight()
  109.         for i = 1, length - 1 do forward() end
  110.         turtle.turnRight()
  111.     else
  112.         turtle.turnLeft()
  113.         for i = 1, length - 1 do forward() end
  114.         turtle.turnLeft()
  115.     end
  116.  
  117.     print("Finished layer "..layerNum..".")
  118.     return true
  119. end
  120.  
  121. -- Build all layers
  122. for h = 1, height do
  123.     if not buildLayer(h) then
  124.         print("Build aborted on layer "..h)
  125.         return
  126.     end
  127.     if h < height then
  128.         print("Moving up to layer "..(h + 1))
  129.         moveUp()
  130.     end
  131. end
  132.  
  133. print("✅ Build complete! All "..height.." layer(s) placed.")
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement