Advertisement
nadkarnik

BUILD_HOUSE.lua

Jul 4th, 2025
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.27 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 placeBlockUp()
  22.     while not turtle.placeUp() 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 placeBlockForward()
  31.     while not turtle.place() do
  32.         if turtle.getItemCount() == 0 and not selectNextSlot() then
  33.             error("Out of blocks!")
  34.         end
  35.         sleep(0.1)
  36.     end
  37. end
  38.  
  39. function forwardSafe()
  40.     while not turtle.forward() do
  41.         turtle.dig()
  42.         sleep(0.1)
  43.     end
  44. end
  45.  
  46. function upSafe()
  47.     while not turtle.up() do
  48.         turtle.digUp()
  49.         sleep(0.1)
  50.     end
  51. end
  52.  
  53. function downSafe()
  54.     while not turtle.down() do
  55.         turtle.digDown()
  56.         sleep(0.1)
  57.     end
  58. end
  59.  
  60. -- === Structure Building Functions ===
  61.  
  62. function buildFloor(length, width)
  63.     for w = 1, width do
  64.         for l = 1, length do
  65.             placeBlockDown()
  66.             if l < length then forwardSafe() end
  67.         end
  68.         if w < width then
  69.             if w % 2 == 1 then
  70.                 turtle.turnRight()
  71.                 forwardSafe()
  72.                 turtle.turnRight()
  73.             else
  74.                 turtle.turnLeft()
  75.                 forwardSafe()
  76.                 turtle.turnLeft()
  77.             end
  78.         end
  79.     end
  80.  
  81.     -- Return to starting corner and original facing
  82.     if width % 2 == 1 then
  83.         turtle.turnRight()
  84.         for i = 1, length - 1 do turtle.back() end
  85.         turtle.turnRight()
  86.     else
  87.         turtle.turnLeft()
  88.         for i = 1, length - 1 do turtle.back() end
  89.         turtle.turnLeft()
  90.     end
  91.     for i = 1, width - 1 do turtle.back() end
  92. end
  93.  
  94. local function placeWallColumn(height)
  95.     upSafe() -- Move above floor level (y=2)
  96.     for h = 2, height - 1 do
  97.         placeBlockForward()
  98.         if h < height - 1 then upSafe() end
  99.     end
  100.     for h = 2, height - 1 do downSafe() end -- Return down to floor
  101. end
  102.  
  103. local function buildWallSide(distance, height)
  104.     for i = 1, distance do
  105.         placeWallColumn(height)
  106.         if i < distance then forwardSafe() end
  107.     end
  108. end
  109.  
  110. function buildWalls(length, width, height)
  111.     -- Start at corner facing length side
  112.     buildWallSide(length, height)
  113.     turtle.turnRight()
  114.     buildWallSide(width, height)
  115.     turtle.turnRight()
  116.     buildWallSide(length, height)
  117.     turtle.turnRight()
  118.     buildWallSide(width, height)
  119.     turtle.turnRight()
  120.     -- Finished walls, back at start facing original direction
  121. end
  122.  
  123. function buildCeiling(length, width, height)
  124.     -- Move up to ceiling level
  125.     for i = 1, height - 1 do upSafe() end
  126.  
  127.     -- Place ceiling (like floor but above)
  128.     for w = 1, width do
  129.         for l = 1, length do
  130.             placeBlockDown()
  131.             if l < length then forwardSafe() end
  132.         end
  133.         if w < width then
  134.             if w % 2 == 1 then
  135.                 turtle.turnRight()
  136.                 forwardSafe()
  137.                 turtle.turnRight()
  138.             else
  139.                 turtle.turnLeft()
  140.                 forwardSafe()
  141.                 turtle.turnLeft()
  142.             end
  143.         end
  144.     end
  145.  
  146.     -- Return to starting corner and original facing below ceiling
  147.     if width % 2 == 1 then
  148.         turtle.turnRight()
  149.         for i = 1, length - 1 do turtle.back() end
  150.         turtle.turnRight()
  151.     else
  152.         turtle.turnLeft()
  153.         for i = 1, length - 1 do turtle.back() end
  154.         turtle.turnLeft()
  155.     end
  156.     for i = 1, width - 1 do turtle.back() end
  157.     for i = 1, height - 1 do downSafe() end
  158. end
  159.  
  160. function buildHollowBox(length, width, height)
  161.     if length < 2 or width < 2 or height < 2 then
  162.         error("❌ Dimensions must be at least 2x2x2.")
  163.     end
  164.  
  165.     if turtle.getFuelLevel() == 0 then
  166.         error("❌ Turtle needs fuel! Use turtle.refuel().")
  167.     end
  168.  
  169.     if turtle.getItemCount() == 0 and not selectNextSlot() then
  170.         error("❌ No blocks in inventory.")
  171.     end
  172.  
  173.     print("▶ Building floor...")
  174.     buildFloor(length, width)
  175.     print("▶ Building walls...")
  176.     buildWalls(length, width, height)
  177.     print("▶ Building ceiling...")
  178.     buildCeiling(length, width, height)
  179.     print("✅ Done!")
  180. end
  181.  
  182. -- === Menu Prompt ===
  183.  
  184. function promptNumber(prompt)
  185.     while true do
  186.         io.write(prompt)
  187.         local input = read()
  188.         local number = tonumber(input)
  189.         if number and number >= 2 then
  190.             return math.floor(number)
  191.         else
  192.             print("Please enter a number ≥ 2.")
  193.         end
  194.     end
  195. end
  196.  
  197. -- === Main Program ===
  198.  
  199. print("=== Build Hollow Structure ===")
  200. local len = promptNumber("Enter length (≥2): ")
  201. local wid = promptNumber("Enter width (≥2): ")
  202. local hei = promptNumber("Enter height (≥2): ")
  203.  
  204. print("▶ Starting build: " .. len .. " x " .. wid .. " x " .. hei)
  205. buildHollowBox(len, wid, hei)
  206.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement