robathome

MC xpost

Nov 12th, 2025 (edited)
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. -- xpost.lua
  2. -- Usage: xpost <height>
  3. -- Builds a hollow cross-shaped post, leaves a doorway at ground level (forward),
  4. -- returns down the hollow center, exits, and seals the doorway from outside.
  5.  
  6. local args = {...}
  7. local height = tonumber(args[1] or "")
  8. if not height or height < 1 then
  9. print("Usage: xpost <height>")
  10. return
  11. end
  12.  
  13. -- === Helpers ===
  14. local function selectAnyBlock()
  15. for s = 1, 16 do
  16. if turtle.getItemCount(s) > 0 then
  17. turtle.select(s)
  18. return true
  19. end
  20. end
  21. return false
  22. end
  23.  
  24. -- Ensure the space in front contains a block (place if air)
  25. local function ensureFrontFilled()
  26. if turtle.detect() then return true end
  27. if not selectAnyBlock() then return false end
  28. return turtle.place()
  29. end
  30.  
  31. local function turnAround() turtle.turnLeft(); turtle.turnLeft() end
  32.  
  33. local function safeForward(maxTries)
  34. maxTries = maxTries or 50
  35. local tries = 0
  36. while not turtle.forward() do
  37. if turtle.detect() then turtle.dig() else turtle.attack() end
  38. tries = tries + 1
  39. if tries > maxTries then return false end
  40. end
  41. return true
  42. end
  43.  
  44. local function safeUp(maxTries)
  45. maxTries = maxTries or 50
  46. local tries = 0
  47. while not turtle.up() do
  48. if turtle.detectUp() then turtle.digUp() else turtle.attackUp() end
  49. tries = tries + 1
  50. if tries > maxTries then return false end
  51. end
  52. return true
  53. end
  54.  
  55. local function safeDown(maxTries)
  56. maxTries = maxTries or 50
  57. local tries = 0
  58. while not turtle.down() do
  59. if turtle.detectDown() then turtle.digDown() else turtle.attackDown() end
  60. tries = tries + 1
  61. if tries > maxTries then return false end
  62. end
  63. return true
  64. end
  65.  
  66. print(("Building hollow X-post %d high…"):format(height))
  67.  
  68. -- === Build loop ===
  69. -- We stand at the center. Each layer: ensure blocks in 4 directions.
  70. -- On layer 1, skip the forward block to leave the doorway.
  71. for layer = 1, height do
  72. for side = 1, 4 do
  73. local skipDoorway = (layer == 1 and side == 1) -- side==1 is current facing direction
  74. if not skipDoorway then
  75. if not ensureFrontFilled() then
  76. print(("Out of blocks while placing at layer %d, side %d"):format(layer, side))
  77. return
  78. end
  79. end
  80. turtle.turnRight()
  81. end
  82.  
  83. if layer < height then
  84. if not safeUp() then
  85. print(("Failed to move up at layer %d"):format(layer))
  86. return
  87. end
  88. end
  89. end
  90.  
  91. -- === Return to ground through the hollow core ===
  92. for _ = 1, height - 1 do
  93. if not safeDown() then
  94. print("Failed to move down through the center")
  95. return
  96. end
  97. end
  98.  
  99. -- === Exit the doorway and seal it ===
  100. if not safeForward() then
  101. print("Failed to exit through the doorway (path blocked?)")
  102. return
  103. end
  104. turnAround()
  105. if not ensureFrontFilled() then
  106. print("Out of blocks when sealing the doorway")
  107. return
  108. end
  109. turnAround()
  110.  
  111. print("Done. Hollow X-post complete, doorway sealed behind the turtle.")
  112.  
Advertisement
Add Comment
Please, Sign In to add comment