Advertisement
nadkarnik

BUILD_HOUSE.lua

Jul 4th, 2025
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.97 KB | None | 0 0
  1. -- === Utility Functions ===
  2.  
  3. function selectNextSlot()
  4.     for i = 1, 16 do
  5.         local slot = (turtle.getSelectedSlot() % 16) + 1
  6.         turtle.select(slot)
  7.         if turtle.getItemCount() > 0 then return true end
  8.     end
  9.     return false
  10. end
  11.  
  12. function placeBlockDown()
  13.     while not turtle.placeDown() do
  14.         if turtle.getItemCount() == 0 and not selectNextSlot() then
  15.             error("Out of blocks!")
  16.         end
  17.         sleep(0.1)
  18.     end
  19. end
  20.  
  21. function placeBlockForward()
  22.     while not turtle.place() do
  23.         if turtle.getItemCount() == 0 and not selectNextSlot() then
  24.             error("Out of blocks!")
  25.         end
  26.         sleep(0.1)
  27.     end
  28. end
  29.  
  30. function forwardSafe()
  31.     while not turtle.forward() do
  32.         turtle.dig()
  33.         sleep(0.1)
  34.     end
  35. end
  36.  
  37. function upSafe()
  38.     while not turtle.up() do
  39.         turtle.digUp()
  40.         sleep(0.1)
  41.     end
  42. end
  43.  
  44. function downSafe()
  45.     while not turtle.down() do
  46.         turtle.digDown()
  47.         sleep(0.1)
  48.     end
  49. end
  50.  
  51. -- === Structure Building ===
  52.  
  53. function buildFloor(length, width)
  54.     for w = 1, width do
  55.         for l = 1, length do
  56.             placeBlockDown()
  57.             if l < length then forwardSafe() end
  58.         end
  59.         if w < width then
  60.             if w % 2 == 1 then
  61.                 turtle.turnRight()
  62.                 forwardSafe()
  63.                 turtle.turnRight()
  64.             else
  65.                 turtle.turnLeft()
  66.                 forwardSafe()
  67.                 turtle.turnLeft()
  68.             end
  69.         end
  70.     end
  71.  
  72.     -- Return to starting corner and face original direction
  73.     if width % 2 == 1 then
  74.         turtle.turnRight()
  75.         for i = 1, length - 1 do turtle.back() end
  76.         turtle.turnRight()
  77.     else
  78.         turtle.turnLeft()
  79.         for i = 1, length - 1 do turtle.back() end
  80.         turtle.turnLeft()
  81.     end
  82.     for i = 1, width - 1 do turtle.back() end
  83. end
  84.  
  85. function placeWallColumn(wallHeight)
  86.     for h = 1, wallHeight do
  87.         placeBlockForward()
  88.         if h < wallHeight then upSafe() end
  89.     end
  90.     for h = 1, wallHeight - 1 do downSafe() end
  91. end
  92.  
  93. function buildWallSide(distance, wallHeight)
  94.     for i = 1, distance do
  95.         placeWallColumn(wallHeight)
  96.         if i < distance then forwardSafe() end
  97.     end
  98. end
  99.  
  100. function buildWalls(length, width, height)
  101.     local wallHeight = height - 2
  102.     if wallHeight < 1 then return end
  103.     upSafe() -- Move to y = 2
  104.     buildWallSide(length, wallHeight)
  105.     turtle.turnRight()
  106.     buildWallSide(width, wallHeight)
  107.     turtle.turnRight()
  108.     buildWallSide(length, wallHeight)
  109.     turtle.turnRight()
  110.     buildWallSide(width, wallHeight)
  111.     turtle.turnRight()
  112.     downSafe() -- Return to y = 1
  113. end
  114.  
  115. function buildCeiling(length, width, height)
  116.     for i = 1, height - 1 do upSafe() end
  117.     for w = 1, width do
  118.         for l = 1, length do
  119.             placeBlockDown()
  120.             if l < length then
  121.                 forwardSafe()
  122.             end
  123.         end
  124.         if w < width then
  125.             if w % 2 == 1 then
  126.                 turtle.turnRight()
  127.                 forwardSafe()
  128.                 turtle.turnRight()
  129.             else
  130.                 turtle.turnLeft()
  131.                 forwardSafe()
  132.                 turtle.turnLeft()
  133.             end
  134.         end
  135.     end
  136.     -- Return to original position
  137.     if width % 2 == 1 then
  138.         turtle.turnRight()
  139.         for i = 1, length - 1 do turtle.back() end
  140.         turtle.turnRight()
  141.     else
  142.         turtle.turnLeft()
  143.         for i = 1, length - 1 do turtle.back() end
  144.         turtle.turnLeft()
  145.     end
  146.     for i = 1, width - 1 do turtle.back() end
  147.     for i = 1, height - 1 do downSafe() end
  148. end
  149.  
  150. function buildHollowBox(length, width, height)
  151.     if length < 2 or width < 2 or height < 3 then
  152.         error("❌ Minimum size is 2x2x3 (LxWxH)")
  153.     end
  154.  
  155.     if turtle.getFuelLevel() == 0 then
  156.         error("❌ Turtle needs fuel. Use turtle.refuel()")
  157.     end
  158.  
  159.     if turtle.getItemCount() == 0 and not selectNextSlot() then
  160.         error("❌ No blocks in inventory.")
  161.     end
  162.  
  163.     print("▶ Building floor...")
  164.     buildFloor(length, width)
  165.     print("▶ Building walls...")
  166.     buildWalls(length, width, height)
  167.     print("▶ Building ceiling...")
  168.     buildCeiling(length, width, height)
  169.     print("✅ Build complete!")
  170. end
  171.  
  172. -- === Menu Prompt ===
  173.  
  174. function promptNumber(prompt)
  175.     while true do
  176.         io.write(prompt)
  177.         local input = read()
  178.         local number = tonumber(input)
  179.         if number and number >= 2 then
  180.             return math.floor(number)
  181.         else
  182.             print("Please enter a number ≥ 2.")
  183.         end
  184.     end
  185. end
  186.  
  187. -- === Main Program ===
  188.  
  189. print("=== Build Hollow Structure ===")
  190. local len = promptNumber("Enter length (≥2): ")
  191. local wid = promptNumber("Enter width (≥2): ")
  192. local hei = promptNumber("Enter total height (≥3): ")
  193.  
  194. print("▶ Starting build: " .. len .. " x " .. wid .. " x " .. hei)
  195. buildHollowBox(len, wid, hei)
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement