kophysty

Minecraft Turtule Home Builder

Feb 1st, 2025 (edited)
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.16 KB | Gaming | 0 0
  1. -- Constants definition
  2. local WIDTH = 15            -- horizontal dimension of the house floor
  3. local LENGTH = 20           -- vertical dimension of the house floor (use even number for better repositioning)
  4. local WALL_HEIGHT = 5       -- number of wall layers
  5. local OPENING_BOTTOM = 2    -- opening (window/door) starts at level 2
  6. local OPENING_TOP = 4       -- opening ends at level 4 (i.e. 3 blocks tall)
  7. local COBBLE_SLOT = 1       -- default inventory slot for cobblestone
  8. local PLANK_SLOT = 2        -- default inventory slot for roof material
  9.  
  10. -- Expected material names (adjust if needed)
  11. local COBBLE_NAME = "minecraft:cobblestone"
  12. local PLANK_NAME = "minecraft:planks"
  13.  
  14. -- Walls table in clockwise order.
  15. -- Assumes that after repositioning the turtle is at the southwest corner of the floor, facing east.
  16. -- Then its right side will be south, followed by west, north, and east.
  17. local walls = {
  18.   {length = WIDTH, openingStart = math.floor((WIDTH - 3) / 2) + 1, door = true},  -- south wall (door)
  19.   {length = LENGTH, openingStart = math.floor((LENGTH - 3) / 2) + 1, door = false}, -- west wall (window)
  20.   {length = WIDTH, openingStart = math.floor((WIDTH - 3) / 2) + 1, door = false},  -- north wall (window)
  21.   {length = LENGTH, openingStart = math.floor((LENGTH - 3) / 2) + 1, door = false}  -- east wall (window)
  22. }
  23.  
  24. -- Function to ensure that the active slot for a given material is not empty.
  25. -- If the default slot is empty, search all slots for an item matching materialName.
  26. function ensureMaterial(materialName, defaultSlot)
  27.   if turtle.getItemCount(defaultSlot) > 0 then
  28.     turtle.select(defaultSlot)
  29.     return
  30.   else
  31.     for i = 1, 16 do
  32.       local detail = turtle.getItemDetail(i)
  33.       if detail and detail.name == materialName and detail.count > 0 then
  34.         turtle.select(i)
  35.         return
  36.       end
  37.     end
  38.   end
  39.   error("Out of material: " .. materialName)
  40. end
  41.  
  42. -- New function to check if there is a hole (air) below the turtle.
  43. function isHoleBelow()
  44.   if not turtle.detectDown() then
  45.     return true
  46.   end
  47.   local success, data = turtle.inspectDown()
  48.   if not success then
  49.     return true
  50.   else
  51.     return data.name == "minecraft:air"
  52.   end
  53. end
  54.  
  55. -- Safe movement functions with obstacle removal
  56. function safeForward()
  57.   while not turtle.forward() do
  58.     if turtle.detect() then
  59.       turtle.dig()         -- dig block in front if present
  60.     else
  61.       turtle.attack()      -- attack mob if present
  62.     end
  63.     os.sleep(0.5)
  64.   end
  65. end
  66.  
  67. function safeUp()
  68.   while not turtle.up() do
  69.     if turtle.detectUp() then
  70.       turtle.digUp()       -- clear block above
  71.     else
  72.       turtle.attackUp()    -- attack entity above if needed
  73.     end
  74.     os.sleep(0.5)
  75.   end
  76. end
  77.  
  78. function safeDown()
  79.   while not turtle.down() do
  80.     if turtle.detectDown() then
  81.       turtle.digDown()     -- clear block below if present
  82.     else
  83.       os.sleep(0.5)
  84.     end
  85.   end
  86. end
  87.  
  88. -- Movement function used during ground leveling and roof building that does not remove obstacles.
  89. function forwardNoDig()
  90.   while not turtle.forward() do
  91.     os.sleep(0.5)
  92.   end
  93. end
  94.  
  95. -- Level the ground by traversing a WIDTH x LENGTH area in a snake pattern.
  96. -- Assumes the turtle starts at the southwest corner of the target area, facing east.
  97. function levelGround(width, length)
  98.   for row = 1, length do
  99.     for col = 1, width do
  100.       if isHoleBelow() then
  101.         ensureMaterial(COBBLE_NAME, COBBLE_SLOT)
  102.         turtle.placeDown()
  103.         os.sleep(0.2)  -- delay to allow block placement
  104.       end
  105.       if col < width then
  106.         safeForward()
  107.       end
  108.     end
  109.     if row < length then
  110.       if row % 2 == 1 then
  111.         turtle.turnLeft()    -- from east, left -> north
  112.         safeForward()
  113.         turtle.turnLeft()    -- now facing west
  114.       else
  115.         turtle.turnRight()   -- from west, right -> north
  116.         safeForward()
  117.         turtle.turnRight()   -- now facing east
  118.       end
  119.     end
  120.   end
  121.   -- Reposition the turtle to the starting corner (southwest) with correct orientation.
  122.   repositionToStart(width, length)
  123. end
  124.  
  125. -- Reposition the turtle to the southwest corner facing east.
  126. -- Based on the snake fill pattern, final position depends on whether length is odd or even.
  127. function repositionToStart(width, length)
  128.   if length % 2 == 1 then
  129.     -- For odd number of rows, final position is at (width, length) facing east (northeast corner).
  130.     turtle.turnRight()
  131.     turtle.turnRight()  -- now facing west
  132.     for i = 1, (width - 1) do
  133.       safeForward()
  134.     end
  135.     turtle.turnLeft()   -- now facing south
  136.     for i = 1, (length - 1) do
  137.       safeForward()
  138.     end
  139.     turtle.turnLeft()   -- now facing east at (1,1)
  140.   else
  141.     -- For even number of rows, final position is at (1, length) facing west (northwest corner).
  142.     turtle.turnLeft()   -- from west, left -> south
  143.     for i = 1, (length - 1) do
  144.       safeForward()
  145.     end
  146.     turtle.turnLeft()   -- from south, left -> east
  147.   end
  148. end
  149.  
  150. -- Function to build one wall segment along the current edge.
  151. -- The turtle places a block on its right if the current position is not within the opening.
  152. function buildWallSegmentRight(wallLength, openingStart, currentLevel)
  153.   for pos = 1, wallLength do
  154.     if not (currentLevel >= OPENING_BOTTOM and currentLevel <= OPENING_TOP and pos >= openingStart and pos < openingStart + 3) then
  155.       turtle.turnRight()  -- face right (exterior side)
  156.       ensureMaterial(COBBLE_NAME, COBBLE_SLOT)
  157.       if turtle.detect() then
  158.         turtle.dig()      -- clear obstacle if needed
  159.       end
  160.       turtle.place()      -- place block on exterior side
  161.       turtle.turnLeft()   -- return to original direction
  162.       os.sleep(0.1)       -- short delay after placing
  163.     end
  164.     if pos < wallLength then
  165.       safeForward()
  166.     end
  167.   end
  168. end
  169.  
  170. -- Build all wall layers by traversing the interior perimeter.
  171. -- Assumes the turtle starts at the southwest interior corner facing east.
  172. function buildWalls()
  173.   for level = 1, WALL_HEIGHT do
  174.     for i = 1, 4 do
  175.       local wall = walls[i]
  176.       buildWallSegmentRight(wall.length, wall.openingStart, level)
  177.       turtle.turnRight()  -- prepare for next wall segment
  178.     end
  179.     if level < WALL_HEIGHT then
  180.       safeUp()  -- move up to next layer
  181.       -- After ascending, the turtle is assumed to be in the correct corner.
  182.     end
  183.   end
  184. end
  185.  
  186. -- New roof building function.
  187. -- Instead of placing blocks above the turtle (which blocks its movement),
  188. -- we ascend one level and build the roof by placing blocks below the turtle.
  189. function buildRoof()
  190.   print("Ascending to roof build level...")
  191.   safeUp()  -- move one block up so that the roof is built beneath the turtle
  192.   for row = 1, LENGTH do
  193.     for col = 1, WIDTH do
  194.       ensureMaterial(PLANK_NAME, PLANK_SLOT)
  195.       if turtle.detectDown() then
  196.         -- Do not dig if the block below is already correct; assume it's our roof block.
  197.         local success, data = turtle.inspectDown()
  198.         if not (success and data.name == PLANK_NAME) then
  199.           turtle.digDown()
  200.         end
  201.       end
  202.       turtle.placeDown()  -- place roof block below
  203.       os.sleep(0.1)       -- short delay after placing
  204.       if col < WIDTH then
  205.         safeForward()
  206.       end
  207.     end
  208.     if row < LENGTH then
  209.       if row % 2 == 1 then
  210.         turtle.turnRight()
  211.         safeForward()
  212.         turtle.turnRight()
  213.       else
  214.         turtle.turnLeft()
  215.         safeForward()
  216.         turtle.turnLeft()
  217.       end
  218.     end
  219.   end
  220.   print("Roof build complete. Descending back to roof level...")
  221.   safeDown()  -- optionally, return to roof level
  222. end
  223.  
  224. -- Main function executing the building process; accepts varargs for command-line parameters.
  225. function main(...)
  226.   local args = {...}
  227.   local quickMode = false
  228.   if args[1] and args[1] == "quick" then
  229.     quickMode = true
  230.   end
  231.  
  232.   if not quickMode then
  233.     print("Starting ground leveling...")
  234.     levelGround(WIDTH, LENGTH)
  235.   else
  236.     print("Quick mode activated: skipping ground leveling.")
  237.   end
  238.  
  239.   print("Starting wall building...")
  240.   buildWalls()
  241.   print("Building roof...")
  242.   buildRoof()
  243.   print("Construction complete.")
  244. end
  245.  
  246. -- Execute main program with command-line arguments
  247. main(...)
  248.  
Add Comment
Please, Sign In to add comment