Advertisement
McBaron

asgtyre

Jun 14th, 2025
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.43 KB | Gaming | 0 0
  1. -- Fill Area with Dirt using a Turtle
  2.  
  3. -- Function to ask for a number
  4. local function askForNumber(prompt)
  5.     print(prompt)
  6.     local input = read()
  7.     return tonumber(input)
  8. end
  9.  
  10. -- Prompt user for dimensions
  11. local length = askForNumber("Enter length:")
  12. local width = askForNumber("Enter width:")
  13. local height = askForNumber("Enter height:")
  14.  
  15. -- Select dirt in inventory (first found slot)
  16. local function selectDirt()
  17.     for i = 1, 16 do
  18.         turtle.select(i)
  19.         local item = turtle.getItemDetail()
  20.         if item and item.name:find("dirt") then
  21.             return true
  22.         end
  23.     end
  24.     return false
  25. end
  26.  
  27. -- Place a block below the turtle
  28. local function placeDown()
  29.     if not turtle.detectDown() then
  30.         if not turtle.placeDown() then
  31.             if not selectDirt() then
  32.                 print("Out of dirt!")
  33.                 return false
  34.             end
  35.             if not turtle.placeDown() then
  36.                 print("Failed to place dirt.")
  37.                 return false
  38.             end
  39.         end
  40.     end
  41.     return true
  42. end
  43.  
  44. -- Move forward with obstacle handling
  45. local function forward()
  46.     while not turtle.forward() do
  47.         turtle.dig()
  48.         sleep(0.5)
  49.     end
  50. end
  51.  
  52. -- Build one horizontal layer
  53. local function buildLayer()
  54.     for w = 1, width do
  55.         for l = 1, length do
  56.             placeDown()
  57.             if l < length then
  58.                 forward()
  59.             end
  60.         end
  61.         -- Move to next row
  62.         if w < width then
  63.             if w % 2 == 1 then
  64.                 turtle.turnRight()
  65.                 forward()
  66.                 turtle.turnRight()
  67.             else
  68.                 turtle.turnLeft()
  69.                 forward()
  70.                 turtle.turnLeft()
  71.             end
  72.         end
  73.     end
  74.  
  75.     -- Return to starting position of the layer
  76.     if width % 2 == 1 then
  77.         turtle.turnRight()
  78.         for i = 1, width - 1 do forward() end
  79.         turtle.turnRight()
  80.     else
  81.         if width > 1 then
  82.             if width % 2 == 0 then
  83.                 turtle.turnLeft()
  84.                 for i = 1, width - 1 do forward() end
  85.                 turtle.turnLeft()
  86.             end
  87.         end
  88.     end
  89. end
  90.  
  91. -- Main loop: build each layer
  92. for h = 1, height do
  93.     buildLayer()
  94.     if h < height then
  95.         while not turtle.up() do
  96.             turtle.digUp()
  97.             sleep(0.5)
  98.         end
  99.     end
  100. end
  101.  
  102. print("Done building!")
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement