STATEDLIGHT

clear room

Mar 21st, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. local tArgs = { ... }
  2. if #tArgs == 0 then
  3. print( "Usage: clearRoomLarge <radius> <(optional, if not go until bedrock)depth>" )
  4. return
  5. end
  6.  
  7. -- Mine in a quarry pattern until we hit something we can't
  8. local size = tonumber( tArgs[1] )
  9. if size < 1 then
  10. print( "Excavate radius must be positive" )
  11. return
  12. end
  13.  
  14. targetDepth = 256
  15. if #tArgs > 1 then
  16. targetDepth = tonumber( tArgs[2] )
  17. if targetDepth < 1 then
  18. print( "Depth provided too shallow" )
  19. return
  20. end
  21. targetDepth = targetDepth - 1
  22. end
  23. collected = 0
  24. depth = 0
  25. tripDepth = 0
  26. stopCause = 0
  27.  
  28. local xPos,zPos = 0,0
  29. local xDir,zDir = 0,1
  30.  
  31. local function collect()
  32. collected = collected + 1
  33. if math.fmod(collected, 25) == 0 then
  34. print( "Mined "..collected.." blocks." )
  35. end
  36.  
  37. for n=1,9 do
  38. if turtle.getItemCount(n) == 0 then
  39. return true
  40. end
  41. end
  42.  
  43. print( "No empty slots left." )
  44. return false
  45. end
  46.  
  47. local function tryForwards()
  48. while not turtle.forward() do
  49. if turtle.dig() then
  50. if not collect() then
  51. stopCause = 2
  52. return false
  53. end
  54. else
  55. -- give sand a chance to fall
  56. sleep(0.8)
  57. if turtle.dig() then
  58. if not collect() then
  59. stopCause = 2
  60. return false
  61. end
  62. else
  63. stopCause = 1
  64. return false
  65. end
  66. end
  67. end
  68. xPos = xPos + xDir
  69. zPos = zPos + zDir
  70. return true
  71. end
  72.  
  73. local function tryDown()
  74. if depth == targetDepth then
  75. return false
  76. end
  77. if not turtle.down() then
  78. if turtle.digDown() then
  79. if not collect() then
  80. return false
  81. end
  82. end
  83. if not turtle.down() then
  84. return false
  85. end
  86. end
  87. depth = depth + 1
  88. if math.fmod( depth, 10 ) == 0 then
  89. print( "Descended "..depth.." metres." )
  90. end
  91. return true
  92. end
  93.  
  94. local function turnLeft()
  95. turtle.turnLeft()
  96. xDir, zDir = -zDir, xDir
  97. end
  98.  
  99. local function turnRight()
  100. turtle.turnRight()
  101. xDir, zDir = zDir, -xDir
  102. end
  103.  
  104. local function main() -- ===================START MAIN LOOP FUNCTION
  105.  
  106. print( "Excavating..." )
  107.  
  108. local reseal = false
  109. if turtle.digDown() then
  110. reseal = true
  111. end
  112.  
  113. local alternate = 0
  114. local done = false
  115. while not done do
  116. for n=1,size do
  117. for m=1,size-1 do
  118. if not tryForwards() then
  119. done = true
  120. break
  121. end
  122. end
  123. if done then
  124. break
  125. end
  126. if n<size then
  127. if math.fmod(n + alternate,2) == 0 then
  128. turnLeft()
  129. if not tryForwards() then
  130. done = true
  131. break
  132. end
  133. turnLeft()
  134. else
  135. turnRight()
  136. if not tryForwards() then
  137. done = true
  138. break
  139. end
  140. turnRight()
  141. end
  142. end
  143. end
  144. if done then
  145. break
  146. end
  147.  
  148. if size > 1 then
  149. if math.fmod(size,2) == 0 then
  150. turnRight()
  151. else
  152. if alternate == 0 then
  153. turnLeft()
  154. else
  155. turnRight()
  156. end
  157. alternate = 1 - alternate
  158. end
  159. end
  160.  
  161. if not tryDown() then
  162. done = true
  163. break
  164. end
  165. end
  166.  
  167. tripDepth = depth
  168.  
  169. print( "Returning to surface..." )
  170. -- Return to where we started
  171. while depth > 0 do
  172. if turtle.up() then
  173. depth = depth - 1
  174. elseif turtle.digUp() then
  175. collect()
  176. else
  177. sleep( 0.5 )
  178. end
  179. end
  180.  
  181. if xPos > 0 then
  182. while xDir ~= -1 do
  183. turnLeft()
  184. end
  185. while xPos > 0 do
  186. if turtle.forward() then
  187. xPos = xPos - 1
  188. elseif turtle.dig() then
  189. collect()
  190. else
  191. sleep( 0.5 )
  192. end
  193. end
  194. end
  195.  
  196. if zPos > 0 then
  197. while zDir ~= -1 do
  198. turnLeft()
  199. end
  200. while zPos > 0 do
  201. if turtle.forward() then
  202. zPos = zPos - 1
  203. elseif turtle.dig() then
  204. collected = collected + 1
  205. else
  206. sleep( 0.5 )
  207. end
  208. end
  209. end
  210. while zDir ~= 1 do
  211. turnLeft()
  212. end
  213.  
  214. -- Seal the hole
  215. if reseal then
  216. turtle.placeDown()
  217. turtle.forward()
  218. turtle.placeDown()
  219. turtle.back()
  220. end
  221.  
  222. turtle.turnLeft()
  223. turtle.forward()
  224. for n=1,9 do
  225. turtle.drop()
  226. end
  227. turtle.back()
  228. turtle.turnRight()
  229. end -- ===========================================END MAIN LOOP FUNCTION
  230.  
  231. local done = false
  232. while not done do
  233. if (tripDepth == targetDepth) or (stopCause == 1) then
  234. done = true
  235. else
  236. stopCause = 0
  237. xPos,zPos = 0,0
  238. xDir,zDir = 0,1
  239. turtle.digDown()
  240. for n=1,tripDepth do
  241. if turtle.down() then
  242. depth = depth + 1
  243. end
  244. end
  245. main()
  246. end
  247. end
  248.  
  249. print( "Mined "..collected.." blocks total." )
Add Comment
Please, Sign In to add comment