Advertisement
nadkarnik

BUILD_HOUSE.lua

Jul 4th, 2025
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.92 KB | None | 0 0
  1. -- Helper: Select next non-empty slot
  2. function selectNextSlot()
  3.     for i = 1, 16 do
  4.         local slot = (turtle.getSelectedSlot() % 16) + 1
  5.         turtle.select(slot)
  6.         if turtle.getItemCount() > 0 then
  7.             return true
  8.         end
  9.     end
  10.     return false
  11. end
  12.  
  13. -- Helper: Try placing a block in a direction, handling slot changes
  14. function placeBlockDown()
  15.     while not turtle.placeDown() do
  16.         if turtle.getItemCount() == 0 then
  17.             if not selectNextSlot() then error("Out of blocks!") end
  18.         else
  19.             sleep(0.2)
  20.         end
  21.     end
  22. end
  23.  
  24. function placeBlockUp()
  25.     while not turtle.placeUp() do
  26.         if turtle.getItemCount() == 0 then
  27.             if not selectNextSlot() then error("Out of blocks!") end
  28.         else
  29.             sleep(0.2)
  30.         end
  31.     end
  32. end
  33.  
  34. function placeBlockForward()
  35.     while not turtle.place() do
  36.         if turtle.getItemCount() == 0 then
  37.             if not selectNextSlot() then error("Out of blocks!") end
  38.         else
  39.             sleep(0.2)
  40.         end
  41.     end
  42. end
  43.  
  44. -- Helpers: Movement (with digging if needed)
  45. function forwardSafe()
  46.     while not turtle.forward() do
  47.         turtle.dig()
  48.         sleep(0.2)
  49.     end
  50. end
  51.  
  52. function upSafe()
  53.     while not turtle.up() do
  54.         turtle.digUp()
  55.         sleep(0.2)
  56.     end
  57. end
  58.  
  59. function downSafe()
  60.     while not turtle.down() do
  61.         turtle.digDown()
  62.         sleep(0.2)
  63.     end
  64. end
  65.  
  66. -- Build the floor at the current level
  67. function buildFloor(length, width)
  68.     for w = 1, width do
  69.         for l = 1, length do
  70.             placeBlockDown()
  71.             if l < length then forwardSafe() end
  72.         end
  73.         if w < width then
  74.             if w % 2 == 1 then
  75.                 turtle.turnRight()
  76.                 forwardSafe()
  77.                 turtle.turnRight()
  78.             else
  79.                 turtle.turnLeft()
  80.                 forwardSafe()
  81.                 turtle.turnLeft()
  82.             end
  83.         end
  84.     end
  85.  
  86.     -- Return to bottom-left corner facing original direction
  87.     if width % 2 == 1 then
  88.         turtle.turnRight()
  89.         for i = 1, length - 1 do turtle.back() end
  90.         turtle.turnRight()
  91.     else
  92.         turtle.turnLeft()
  93.         for i = 1, length - 1 do turtle.back() end
  94.         turtle.turnLeft()
  95.     end
  96.     for i = 1, width - 1 do turtle.back() end
  97. end
  98.  
  99. -- Build walls around the structure
  100. function buildWalls(length, width, height)
  101.     local function buildWallSegment()
  102.         for h = 1, height - 2 do
  103.             upSafe()
  104.             placeBlockForward()
  105.         end
  106.         for i = 1, height - 2 do downSafe() end
  107.     end
  108.  
  109.     local function moveAndWall(dist)
  110.         for i = 1, dist - 1 do
  111.             forwardSafe()
  112.             buildWallSegment()
  113.         end
  114.     end
  115.  
  116.     buildWallSegment()
  117.     moveAndWall(length)
  118.     turtle.turnRight()
  119.     buildWallSegment()
  120.     moveAndWall(width)
  121.     turtle.turnRight()
  122.     buildWallSegment()
  123.     moveAndWall(length)
  124.     turtle.turnRight()
  125.     buildWallSegment()
  126.     moveAndWall(width)
  127.     turtle.turnRight()
  128. end
  129.  
  130. -- Build ceiling (go to height, then build floor again at top)
  131. function buildCeiling(length, width, height)
  132.     for i = 1, height - 1 do upSafe() end
  133.     buildFloor(length, width)
  134.     for i = 1, height - 1 do downSafe() end
  135. end
  136.  
  137. -- Main function
  138. function buildHollowBox(length, width, height)
  139.     if length < 2 or width < 2 or height < 2 then
  140.         error("Dimensions must be at least 2x2x2.")
  141.     end
  142.  
  143.     -- Ensure a valid slot is selected
  144.     if turtle.getItemCount() == 0 then
  145.         if not selectNextSlot() then
  146.             error("No blocks in inventory!")
  147.         end
  148.     end
  149.  
  150.     buildFloor(length, width)
  151.     buildWalls(length, width, height)
  152.     buildCeiling(length, width, height)
  153.  
  154.     print("✅ Hollow structure complete.")
  155. end
  156.  
  157. -- Example call:
  158. -- Uncomment and customize the below line to run directly:
  159. -- buildHollowBox(5, 4, 3)
  160.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement