robathome icon

MC xpost

robathome | PRO | 11/12/25 07:14:52 PM UTC (Edited) | 0 ⭐ | 269 👁️ | Never ⏰ | []
text |

2.89 KB

|

None

|

0 👍

/

0 👎

-- xpost.lua
-- Usage: xpost <height>
-- Builds a hollow cross-shaped post, leaves a doorway at ground level (forward),
-- returns down the hollow center, exits, and seals the doorway from outside.
 local args = {...}
local height = tonumber(args[1] or "")
if not height or height < 1 then
  print("Usage: xpost <height>")
  return
end
 -- === Helpers ===
local function selectAnyBlock()
  for s = 1, 16 do
    if turtle.getItemCount(s) > 0 then
      turtle.select(s)
      return true
    end
  end
  return false
end
 -- Ensure the space in front contains a block (place if air)
local function ensureFrontFilled()
  if turtle.detect() then return true end
  if not selectAnyBlock() then return false end
  return turtle.place()
end
 local function turnAround() turtle.turnLeft(); turtle.turnLeft() end
 local function safeForward(maxTries)
  maxTries = maxTries or 50
  local tries = 0
  while not turtle.forward() do
    if turtle.detect() then turtle.dig() else turtle.attack() end
    tries = tries + 1
    if tries > maxTries then return false end
  end
  return true
end
 local function safeUp(maxTries)
  maxTries = maxTries or 50
  local tries = 0
  while not turtle.up() do
    if turtle.detectUp() then turtle.digUp() else turtle.attackUp() end
    tries = tries + 1
    if tries > maxTries then return false end
  end
  return true
end
 local function safeDown(maxTries)
  maxTries = maxTries or 50
  local tries = 0
  while not turtle.down() do
    if turtle.detectDown() then turtle.digDown() else turtle.attackDown() end
    tries = tries + 1
    if tries > maxTries then return false end
  end
  return true
end
 print(("Building hollow X-post %d high…"):format(height))
 -- === Build loop ===
-- We stand at the center. Each layer: ensure blocks in 4 directions.
-- On layer 1, skip the forward block to leave the doorway.
for layer = 1, height do
  for side = 1, 4 do
    local skipDoorway = (layer == 1 and side == 1) -- side==1 is current facing direction
    if not skipDoorway then
      if not ensureFrontFilled() then
        print(("Out of blocks while placing at layer %d, side %d"):format(layer, side))
        return
      end
    end
    turtle.turnRight()
  end
   if layer < height then
    if not safeUp() then
      print(("Failed to move up at layer %d"):format(layer))
      return
    end
  end
end
 -- === Return to ground through the hollow core ===
for _ = 1, height - 1 do
  if not safeDown() then
    print("Failed to move down through the center")
    return
  end
end
 -- === Exit the doorway and seal it ===
if not safeForward() then
  print("Failed to exit through the doorway (path blocked?)")
  return
end
turnAround()
if not ensureFrontFilled() then
  print("Out of blocks when sealing the doorway")
  return
end
turnAround()
 print("Done. Hollow X-post complete, doorway sealed behind the turtle.")
 

Comments