Wyvern67

Untitled

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