Advertisement
nadkarnik

BUILD_HOUSE.lua

Jul 4th, 2025
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.95 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 placeDown()
  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 placeForward()
  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. -- === BUILD FUNCTIONS ===
  52.  
  53. function buildFloor(length, width)
  54.     for z = 1, width do
  55.         for x = 1, length do
  56.             placeDown()
  57.             if x < length then forwardSafe() end
  58.         end
  59.         if z < width then
  60.             if z % 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.     -- Return to origin
  72.     if width % 2 == 1 then
  73.         turtle.turnRight()
  74.         for i = 1, length - 1 do turtle.back() end
  75.         turtle.turnRight()
  76.     else
  77.         turtle.turnLeft()
  78.         for i = 1, length - 1 do turtle.back() end
  79.         turtle.turnLeft()
  80.     end
  81.     for i = 1, width - 1 do turtle.back() end
  82. end
  83.  
  84. function buildWalls(length, width, height)
  85.     local wallHeight = height - 2
  86.     if wallHeight < 1 then return end
  87.  
  88.     -- Move up to wall base level (Y=2)
  89.     upSafe()
  90.  
  91.     -- Build wall perimeter
  92.     for h = 1, wallHeight do
  93.         for side = 1, 4 do
  94.             local distance = (side % 2 == 1) and length or width
  95.             for i = 1, distance do
  96.                 if i == 1 or i == distance then
  97.                     placeForward()
  98.                 elseif side == 1 or side == 3 then
  99.                     if h == 1 then placeForward() end
  100.                 end
  101.                 if i < distance then forwardSafe() end
  102.             end
  103.             turtle.turnRight()
  104.         end
  105.         -- Go up for next wall layer
  106.         if h < wallHeight then upSafe() end
  107.     end
  108.  
  109.     -- Return down to floor
  110.     for i = 1, wallHeight do downSafe() end
  111. end
  112.  
  113. function buildCeiling(length, width, height)
  114.     for i = 1, height - 1 do upSafe() end
  115.  
  116.     for z = 1, width do
  117.         for x = 1, length do
  118.             placeDown()
  119.             if x < length then forwardSafe() end
  120.         end
  121.         if z < width then
  122.             if z % 2 == 1 then
  123.                 turtle.turnRight()
  124.                 forwardSafe()
  125.                 turtle.turnRight()
  126.             else
  127.                 turtle.turnLeft()
  128.                 forwardSafe()
  129.                 turtle.turnLeft()
  130.             end
  131.         end
  132.     end
  133.  
  134.     -- Return to floor level and origin
  135.     if width % 2 == 1 then
  136.         turtle.turnRight()
  137.         for i = 1, length - 1 do turtle.back() end
  138.         turtle.turnRight()
  139.     else
  140.         turtle.turnLeft()
  141.         for i = 1, length - 1 do turtle.back() end
  142.         turtle.turnLeft()
  143.     end
  144.     for i = 1, width - 1 do turtle.back() end
  145.  
  146.     for i = 1, height - 1 do downSafe() end
  147. end
  148.  
  149. -- === MAIN BUILD FUNCTION ===
  150.  
  151. function buildHollowBox(length, width, height)
  152.     if length < 2 or width < 2 or height < 3 then
  153.         error("❌ Dimensions must be at least 2x2x3")
  154.     end
  155.  
  156.     if turtle.getFuelLevel() == 0 then
  157.         error("❌ Turtle needs fuel! Use turtle.refuel()")
  158.     end
  159.  
  160.     if turtle.getItemCount() == 0 and not selectNextSlot() then
  161.         error("❌ No blocks available")
  162.     end
  163.  
  164.     print("▶ Building floor...")
  165.     buildFloor(length, width)
  166.  
  167.     print("▶ Building walls...")
  168.     buildWalls(length, width, height)
  169.  
  170.     print("▶ Building ceiling...")
  171.     buildCeiling(length, width, height)
  172.  
  173.     print("✅ Done.")
  174. end
  175.  
  176. -- === MENU ===
  177.  
  178. function promptNumber(prompt)
  179.     while true do
  180.         io.write(prompt)
  181.         local input = read()
  182.         local number = tonumber(input)
  183.         if number and number >= 2 then
  184.             return math.floor(number)
  185.         else
  186.             print("Please enter a number ≥ 2.")
  187.         end
  188.     end
  189. end
  190.  
  191. -- === START ===
  192.  
  193. print("=== Build Hollow Structure ===")
  194. local len = promptNumber("Enter length (≥2): ")
  195. local wid = promptNumber("Enter width (≥2): ")
  196. local hei = promptNumber("Enter height (≥3): ")
  197.  
  198. print("▶ Starting build: " .. len .. " x " .. wid .. " x " .. hei)
  199. buildHollowBox(len, wid, hei)
  200.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement