Advertisement
nadkarnik

BUILD_HOUSE.lua

Jul 4th, 2025
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.21 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
  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. function buildWalls(length, width, height)
  95.     local function wallSegment()
  96.         for h = 1, height - 2 do
  97.             upSafe()
  98.             placeBlockForward()
  99.         end
  100.         for h = 1, height - 2 do downSafe() end
  101.     end
  102.  
  103.     local function moveAndWall(dist)
  104.         for i = 1, dist - 1 do
  105.             forwardSafe()
  106.             wallSegment()
  107.         end
  108.     end
  109.  
  110.     wallSegment()
  111.     moveAndWall(length)
  112.     turtle.turnRight()
  113.     wallSegment()
  114.     moveAndWall(width)
  115.     turtle.turnRight()
  116.     wallSegment()
  117.     moveAndWall(length)
  118.     turtle.turnRight()
  119.     wallSegment()
  120.     moveAndWall(width)
  121.     turtle.turnRight()
  122. end
  123.  
  124. function buildCeiling(length, width, height)
  125.     for i = 1, height - 1 do upSafe() end
  126.     buildFloor(length, width)
  127.     for i = 1, height - 1 do downSafe() end
  128. end
  129.  
  130. function buildHollowBox(length, width, height)
  131.     if length < 2 or width < 2 or height < 2 then
  132.         error("❌ Dimensions must be at least 2x2x2.")
  133.     end
  134.  
  135.     if turtle.getFuelLevel() == 0 then
  136.         error("❌ Turtle needs fuel! Use turtle.refuel().")
  137.     end
  138.  
  139.     if turtle.getItemCount() == 0 and not selectNextSlot() then
  140.         error("❌ No blocks in inventory.")
  141.     end
  142.  
  143.     print("▶ Building floor...")
  144.     buildFloor(length, width)
  145.     print("▶ Building walls...")
  146.     buildWalls(length, width, height)
  147.     print("▶ Building ceiling...")
  148.     buildCeiling(length, width, height)
  149.     print("✅ Done!")
  150. end
  151.  
  152. -- === Menu Prompt ===
  153.  
  154. function promptNumber(prompt)
  155.     while true do
  156.         io.write(prompt)
  157.         local input = read()
  158.         local number = tonumber(input)
  159.         if number and number >= 2 then
  160.             return math.floor(number)
  161.         else
  162.             print("Please enter a number ≥ 2.")
  163.         end
  164.     end
  165. end
  166.  
  167. -- === Main Program ===
  168.  
  169. print("=== Build Hollow Structure ===")
  170. local len = promptNumber("Enter length (≥2): ")
  171. local wid = promptNumber("Enter width (≥2): ")
  172. local hei = promptNumber("Enter height (≥2): ")
  173.  
  174. print("▶ Starting build: " .. len .. " x " .. wid .. " x " .. hei)
  175. buildHollowBox(len, wid, hei)
  176.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement