Advertisement
Nekrose483

stairs CC-Turtle

Feb 11th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. -- Stairwell program by Josh Swan
  2. -- Version 1.3
  3. -- Instructions: Copy file into 'ComputerCraft/lua/rom/programs/turtle' and rename to 'stairs' (no file suffix).
  4.  
  5. local tArgs = { ... }
  6.  
  7. turtle.refuel()
  8.  
  9. if #tArgs ~= 5 then
  10. print( "Usage: stairs <width> <height> <depth> <railingWidth> <centerColumnWidth>" )
  11. print( "Start turtle on the right side of the stairs on the floor. Railings of width 'railingWidth' will be added to both sides of the stairs. The stairs will wrap clockwise around a center column of width 'centerColumnWidth'. Turtle will not stop if its inventory is full." )
  12. return
  13. end
  14.  
  15. -- Mine a stairway to the specified depth and width
  16. local width = tonumber( tArgs[1] )
  17. local height = tonumber( tArgs[2] )
  18. local depth = tonumber( tArgs[3] )
  19. local railingWidth = tonumber(tArgs[4])
  20. local columnWidth = tonumber(tArgs[5])
  21. if columnWidth < 0 then
  22. columnWidth = 0
  23. end
  24.  
  25. if width < 1 then
  26. print( "Stair width must be at least 1" )
  27. return
  28. end
  29. if height < 2 then
  30. print( "Stair height must be at least 2" )
  31. return
  32. end
  33. if depth < 1 then
  34. print( "Stair depth must be at least 1" )
  35. return
  36. end
  37. if railingWidth < 0 then
  38. print( "Railing width must be a positive number or zero for no railings" )
  39. return
  40. end
  41. if columnWidth > 0 then
  42. if columnWidth < 3 and railingWidth > 0 then
  43. print( "Center column width must be zero for a straight flight of stairs or at least 3 for a stairwell with railings" )
  44. return
  45. elseif columnWidth < 2 and railingWidth == 0 then
  46. print( "Center column width must be zero for a straight flight of stairs or at least 2 for a stairwell without railings" )
  47. return
  48. end
  49. end
  50.  
  51. -- Digs stairs/stairwell
  52. function createStairs(width, height, depth, railingWidth, columnWidth)
  53. local originalDepth = depth
  54. local totalDepth = 0
  55.  
  56. while totalDepth < originalDepth do
  57. if columnWidth > 0 then
  58. depth = columnWidth-1
  59. end
  60.  
  61. local atDepth = false
  62. for d=1, depth+1, 1 do
  63. for h=1, height-1, 1 do turtle.up() end
  64. digForward()
  65.  
  66. if d == 1 then
  67. digStair(width, height - 1, railingWidth)
  68. elseif d == depth+1 and railingWidth > 0 then
  69. digStair(width, height - 1, railingWidth)
  70. else
  71. digStair(width, height, railingWidth)
  72. totalDepth = totalDepth + 1
  73. end
  74.  
  75. if totalDepth == originalDepth then
  76. atDepth = true
  77. break
  78. end
  79. end
  80.  
  81. -- Position at top
  82. for h=1, height-1, 1 do turtle.up() end
  83. digForward()
  84. railingOffset(railingWidth)
  85.  
  86. if atDepth then
  87. -- Make space at bottom
  88. digStair(width+(railingWidth*2), height-1, 0)
  89. else
  90. -- Dig landing
  91. digSquare(width+(railingWidth*2), height)
  92.  
  93. -- Reposition for next flight
  94. for w=1, width-1+(railingWidth), 1 do
  95. turtle.back()
  96. end
  97. turtle.turnRight()
  98. end
  99. end
  100. return true
  101. end
  102.  
  103. -- Helper function for digging a stair
  104. function digStair(width, height, railingWidth)
  105. railingOffset(railingWidth)
  106.  
  107. -- Dig stair too if there is no railing
  108. local digHeight = height + 1
  109. if railingWidth > 0 then
  110. digHeight = digHeight - 1
  111. end
  112.  
  113. for h=1, digHeight, 1 do
  114. -- Mine width
  115. turtle.turnLeft()
  116. digLine(width-1+(railingWidth*2))
  117. turtle.turnRight()
  118.  
  119. -- Dig down if we haven't reach the stair or railing level yet
  120. if h <= digHeight-1 then
  121. turtle.digDown()
  122. turtle.down()
  123. end
  124. end
  125. -- Cut stair out separately if there is a railing
  126. if railingWidth > 0 then
  127.  
  128. -- Reposition turtle
  129. turtle.turnLeft()
  130. for r=1, railingWidth, 1 do
  131. turtle.forward()
  132. end
  133. turtle.digDown()
  134. turtle.down()
  135.  
  136. -- Mine width
  137. for w=1, width-1, 1 do
  138. digForward()
  139. end
  140. -- Backup
  141. for w=1, width-1, 1 do
  142. turtle.back()
  143. end
  144. turtle.turnRight()
  145. end
  146. end
  147.  
  148. -- Handles digging into falling blocks
  149. function digForward()
  150. while not turtle.forward() do
  151. turtle.dig()
  152. sleep(0.3)
  153. end
  154. end
  155.  
  156. -- Dig the specified number of blocks in the direction the turtle is facing
  157. function digLine(length)
  158. for l=1, length, 1 do
  159. digForward()
  160. end
  161. -- Backup
  162. for l=1, length, 1 do
  163. turtle.back()
  164. end
  165. end
  166.  
  167. -- Offset for right railing
  168. function railingOffset(railingWidth)
  169. if railingWidth > 0 then
  170. turtle.turnRight()
  171. for r=1, railingWidth, 1 do
  172. digForward()
  173. end
  174. turtle.turnLeft()
  175. end
  176. end
  177.  
  178. -- Digs square with specified width and height
  179. function digSquare(width, height)
  180. for l=1, width, 1 do
  181. for h=1, height, 1 do
  182. -- Mine width
  183. turtle.turnLeft()
  184. digLine(width-1)
  185. turtle.turnRight()
  186.  
  187. -- Dig down if we haven't reach the floor level
  188. if h < height then
  189. turtle.digDown()
  190. turtle.down()
  191. end
  192. end
  193. if l < width then
  194. for h=1, height-1, 1 do turtle.up() end
  195. digForward()
  196. end
  197. end
  198. end
  199.  
  200. -- Execute stair creation --
  201. ----------------------------
  202. print ("Digging stairs...")
  203. createStairs(width, height, depth, railingWidth, columnWidth)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement