Advertisement
amumia

build_room

Nov 19th, 2012
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.67 KB | None | 0 0
  1. -- Room builder
  2. -- Can build any box shaped rooms of any size (WxDXH)
  3. -- Will auto select slot for fuel and build materials
  4. -- If it runs out of fuel or material mid build it will pause and wait for more
  5. --
  6. -- Made by Amumia amumia.pyramid at gmail dot com
  7. -- Use at your own risk
  8.  
  9. function buildPlatform(width, depth)
  10.     realWidth = width + 1
  11.     realDepth = depth + 1
  12.     for i = 1, realWidth do
  13.         for j = 1, realDepth do
  14.             checkCanContinue()
  15.             turtle.placeDown()
  16.             if not (realDepth == j) then
  17.                 turtle.forward()
  18.             end
  19.         end
  20.        
  21.         if not (realWidth == i) then
  22.             -- Change row
  23.             if (i % 2) == 0 then
  24.                 turtle.turnLeft()
  25.             else
  26.                 turtle.turnRight()
  27.             end
  28.             turtle.forward()
  29.             if (i % 2) == 0 then
  30.                 turtle.turnLeft()
  31.             else
  32.                 turtle.turnRight()
  33.             end
  34.         end
  35.     end
  36. end
  37.  
  38. function build4Walls(width, depth, height)
  39.     for i = 1, height do
  40.         turtle.up();
  41.         for j = 1, 4 do
  42.             if j % 2 == 0 then
  43.                 size = depth
  44.             else
  45.                 size = width
  46.             end
  47.  
  48.             checkCanContinue()
  49.             turtle.placeDown()
  50.             for k = 1, size do
  51.                 turtle.forward()
  52.                 checkCanContinue()
  53.                 turtle.placeDown()
  54.             end
  55.             turtle.turnRight()
  56.         end
  57.     end
  58. end
  59.  
  60. function selectMaterialSlot()
  61.     hasMaterials = false
  62.     while not hasMaterials do
  63.         for i = 1, 16 do
  64.             if turtle.getItemCount(i) > 0 then
  65.                 turtle.select(i)
  66.                 currentSlot = i
  67.                 print("Selecting slot " .. i .. " with " .. turtle.getItemCount(i) .. "units")
  68.                 hasMaterials = true
  69.                 break
  70.             end
  71.         end
  72.         if not hasMaterials then
  73.             print("Need more materials. Press key to continue")
  74.             io.read()
  75.         end
  76.     end
  77. end
  78.  
  79. function checkSlotHasMaterial(slot)
  80.     if turtle.getItemCount(slot) <= 0 then
  81.         selectMaterialSlot()
  82.     end
  83. end
  84.  
  85. function checkFuelLevel()
  86.     if turtle.getFuelLevel() <= 10 then
  87.         shell.run("refuel all")
  88.         while turtle.getFuelLevel() <= 0 do
  89.             print("Need more fuel. Press key to continue")
  90.             io.read()
  91.             shell.run("refuel all")
  92.         end
  93.     end
  94. end
  95.  
  96. function checkCanContinue()
  97.     checkFuelLevel()
  98.     checkSlotHasMaterial(currentSlot)
  99. end
  100.  
  101. currentSlot = 1
  102.  
  103. print("Room builder")
  104. print("Width:")
  105. width = io.read()
  106. width = tonumber(width) + 1
  107. print("Depth:")
  108. depth = io.read()
  109. depth = tonumber(depth) + 1
  110. print("Height:")
  111. height = io.read()
  112. height = tonumber(height)
  113.  
  114. -- Prepare for building (fuel and materials)
  115. checkCanContinue()
  116.  
  117. -- Position to start floor
  118. if turtle.detectDown() then
  119.     turtle.up();
  120. end
  121.  
  122. -- Build floor
  123. buildPlatform(width, depth)
  124.  
  125. -- Position to build walls
  126. if depth > width then
  127.     turtle.turnLeft()
  128.     for i = 1, width do
  129.         turtle.forward()
  130.     end
  131.     turtle.turnLeft()
  132.     sleep(1)
  133.     turtle.turnLeft()
  134. elseif width > depth then
  135.     turtle.turnLeft()
  136.     turtle.turnLeft()
  137.     for i = 1, depth do
  138.         turtle.forward()
  139.     end
  140.     turtle.turnRight()
  141. else
  142.     if not ((width + 1) % 2 == 0) then
  143.         turtle.turnRight()
  144.     end
  145.     turtle.turnRight()
  146. end
  147.  
  148. -- Build walls
  149. build4Walls(width, depth, height)
  150.  
  151. -- Position to build ceiling
  152. turtle.up()
  153. for i = 1, width do
  154.     turtle.forward()
  155. end
  156. turtle.turnRight()
  157.  
  158. -- Build ceiling
  159. buildPlatform(width, depth)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement