kreezxil

Hallway Builder - Turtle - Computer Craft

Sep 21st, 2024 (edited)
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.13 KB | Gaming | 0 0
  1. -- Hallway Builder Program (Updated with Slots 2 and 3 for Wall Blocks)
  2.  
  3. -- Ensure the turtle has enough fuel
  4. local function ensureFuel(needed)
  5.     if turtle.getFuelLevel() < needed then
  6.         print("Not enough fuel. Please refuel the turtle.")
  7.         error()
  8.     end
  9. end
  10.  
  11. -- Check if the turtle can move forward
  12. local function safeForward()
  13.     while not turtle.forward() do
  14.         if turtle.detect() then
  15.             turtle.dig()
  16.         else
  17.             sleep(0.5)
  18.         end
  19.     end
  20. end
  21.  
  22. -- Return to the starting position
  23. local function returnToStart(steps)
  24.     turtle.turnLeft()
  25.     turtle.turnLeft()
  26.     for i = 1, steps do
  27.         safeForward()
  28.     end
  29.     turtle.turnLeft()
  30.     turtle.turnLeft()
  31. end
  32.  
  33. -- Select a slot containing wall blocks (Slots 2 and 3)
  34. local function selectWallBlock()
  35.     if turtle.getItemCount(2) > 0 then
  36.         turtle.select(2)
  37.     elseif turtle.getItemCount(3) > 0 then
  38.         turtle.select(3)
  39.     else
  40.         print("Out of wall blocks!")
  41.         error()
  42.     end
  43. end
  44.  
  45. -- Build the floor and first layer of walls
  46. local function buildFirstLayer(length)
  47.     for i = 1, length do
  48.         -- Place floor block
  49.         turtle.select(1)  -- Floor blocks
  50.         turtle.placeDown()
  51.  
  52.         -- Place wall blocks to the left and right
  53.         selectWallBlock()
  54.  
  55.         turtle.turnLeft()
  56.         turtle.place()
  57.         turtle.turnRight()
  58.  
  59.         turtle.turnRight()
  60.         turtle.place()
  61.         turtle.turnLeft()
  62.  
  63.         -- Move forward
  64.         ensureFuel(1)
  65.         safeForward()
  66.     end
  67. end
  68.  
  69. -- Build wall layers
  70. local function buildWallLayer(length)
  71.     for i = 1, length do
  72.         selectWallBlock()
  73.  
  74.         turtle.turnLeft()
  75.         turtle.place()
  76.         turtle.turnRight()
  77.  
  78.         turtle.turnRight()
  79.         turtle.place()
  80.         turtle.turnLeft()
  81.  
  82.         -- Move forward
  83.         ensureFuel(1)
  84.         safeForward()
  85.     end
  86. end
  87.  
  88. -- Build the ceiling with half slabs
  89. local function buildCeiling(length)
  90.     for i = 1, length do
  91.         selectWallBlock()
  92.  
  93.         turtle.turnLeft()
  94.         turtle.place()
  95.         turtle.turnRight()
  96.  
  97.         turtle.turnRight()
  98.         turtle.place()
  99.         turtle.turnLeft()
  100.  
  101.         -- Place ceiling half slab
  102.         turtle.select(4)  -- Ceiling half slabs
  103.         turtle.placeUp()
  104.  
  105.         -- Move forward
  106.         ensureFuel(1)
  107.         safeForward()
  108.     end
  109. end
  110.  
  111. -- Main Program
  112. local hallwayLength = 20
  113.  
  114. -- Calculate required fuel
  115. local totalMoves = hallwayLength * 6  -- Approximate total movements
  116. ensureFuel(totalMoves)
  117.  
  118. -- Build the first layer (floor and bottom walls)
  119. buildFirstLayer(hallwayLength)
  120. returnToStart(hallwayLength)
  121.  
  122. -- Move up to the second layer
  123. ensureFuel(1)
  124. turtle.up()
  125.  
  126. -- Build the second layer of walls
  127. buildWallLayer(hallwayLength)
  128. returnToStart(hallwayLength)
  129.  
  130. -- Move up to the third layer
  131. ensureFuel(1)
  132. turtle.up()
  133.  
  134. -- Build the third layer of walls and the ceiling
  135. buildCeiling(hallwayLength)
  136. returnToStart(hallwayLength)
  137.  
  138. -- Move back down to the ground level
  139. ensureFuel(2)
  140. turtle.down()
  141. turtle.down()
  142.  
  143. print("Hallway construction complete!")
  144.  
Add Comment
Please, Sign In to add comment