robathome

MC lay blocks

Nov 6th, 2025 (edited)
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. -- lay_perp_line.lua
  2. -- Place blocks perpendicular to travel while moving forward.
  3. -- Usage:
  4. -- lay_perp_line <down|left|right> [steps]
  5. -- Examples:
  6. -- lay_perp_line down -- place below each step until blocked
  7. -- lay_perp_line left 25 -- 25 steps, place to the left each step
  8.  
  9. local args = {...}
  10. local DIR = args[1]
  11. local STEPS = tonumber(args[2] or "0") -- 0 = run until blocked
  12.  
  13. local function die(msg) print(msg) error(msg, 0) end
  14. if not DIR or not ({down=true,left=true,right=true})[DIR] then
  15. die("Usage: lay_perp_line <down|left|right> [steps]")
  16. end
  17.  
  18. -- Select a slot with blocks; keep current slot if it still has items
  19. local function ensureBlockSelected()
  20. if turtle.getItemCount() > 0 then return true end
  21. for i=1,16 do
  22. if turtle.getItemCount(i) > 0 then turtle.select(i); return true end
  23. end
  24. return false
  25. end
  26.  
  27. local function placeDownIfAir()
  28. if turtle.detectDown() then return true end
  29. if not ensureBlockSelected() then return false end
  30. return turtle.placeDown()
  31. end
  32.  
  33. local function placeLeftIfAir()
  34. turtle.turnLeft()
  35. local ok = true
  36. if not turtle.detect() then
  37. ok = ensureBlockSelected() and turtle.place() or false
  38. end
  39. turtle.turnRight()
  40. return ok
  41. end
  42.  
  43. local function placeRightIfAir()
  44. turtle.turnRight()
  45. local ok = true
  46. if not turtle.detect() then
  47. ok = ensureBlockSelected() and turtle.place() or false
  48. end
  49. turtle.turnLeft()
  50. return ok
  51. end
  52.  
  53. local function placePerp()
  54. if DIR == "down" then return placeDownIfAir()
  55. elseif DIR == "left" then return placeLeftIfAir()
  56. else return placeRightIfAir() end
  57. end
  58.  
  59. local stepsDone = 0
  60. while true do
  61. -- Place in the requested perpendicular direction if there is air there.
  62. if not placePerp() then
  63. if not ensureBlockSelected() then print("Out of blocks."); break end
  64. -- If placement failed for another reason, continue. It might already be filled.
  65. end
  66.  
  67. -- Stop if something is immediately ahead.
  68. if turtle.detect() then
  69. print("Blocked ahead. Stopping.")
  70. break
  71. end
  72.  
  73. -- Move forward one block.
  74. if not turtle.forward() then
  75. print("Movement failed. Stopping.")
  76. break
  77. end
  78.  
  79. stepsDone = stepsDone + 1
  80. if STEPS > 0 and stepsDone >= STEPS then break end
  81. end
  82.  
  83. print(("Done. Steps: %d"):format(stepsDone))
  84.  
Advertisement
Add Comment
Please, Sign In to add comment