wbennet997

Untitled

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