AndreSoYeah

Untitled

Jan 12th, 2021
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. local tArgs = { ... }
  2. if #tArgs ~= 1 then
  3. print( "Usage: excavate <diameter>" )
  4. return
  5. end
  6.  
  7. -- Mine in a quarry pattern until we hit something we can't dig
  8. local size = tonumber( tArgs[1] )
  9. if size < 1 then
  10. print( "Excavate diameter must be positive" )
  11. return
  12. end
  13.  
  14. local depth = 0
  15. local unloaded = 0
  16. local collected = 0
  17.  
  18. local xPos,zPos = 0,0
  19. local xDir,zDir = 0,1
  20.  
  21. local goTo -- Filled in further down
  22. local refuel -- Filled in further down
  23.  
  24. local function unload( useAllFuel )
  25. print( "Unloading items..." )
  26. for n=1,16 do
  27. local nCount = turtle.getItemCount(n)
  28. if nCount > 0 then
  29. turtle.select(n)
  30. local bDrop = true
  31. if useAllFuel and turtle.refuel(50000) then
  32. bDrop = false
  33. end
  34. if bDrop then
  35. turtle.drop()
  36. unloaded = unloaded + nCount
  37. end
  38. end
  39. end
  40. collected = 0
  41. turtle.select(1)
  42. end
  43.  
  44. local function returnSupplies()
  45. local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  46. print( "Returning to surface..." )
  47. goTo( 0,0,0,0,-1 )
  48.  
  49. local fuelNeeded = 2*(x+y+z) + 1
  50. if not refuel( fuelNeeded ) then
  51. unload( true )
  52. print( "Waiting for fuel" )
  53. while not refuel( fuelNeeded ) do
  54. os.pullEvent( "turtle_inventory" )
  55. end
  56. else
  57. unload( true )
  58. end
  59.  
  60. print( "Resuming mining..." )
  61. goTo( x,y,z,xd,zd )
  62. end
  63.  
  64. local function collect()
  65. local bFull = true
  66. local nTotalItems = 0
  67. for n=1,16 do
  68. local nCount = turtle.getItemCount(n)
  69. if nCount == 0 then
  70. bFull = false
  71. end
  72. nTotalItems = nTotalItems + nCount
  73. end
  74.  
  75. if nTotalItems > collected then
  76. collected = nTotalItems
  77. if math.fmod(collected + unloaded, 50) == 0 then
  78. print( "Mined "..(collected + unloaded).." items." )
  79. end
  80. end
  81.  
  82. if bFull then
  83. print( "No empty slots left." )
  84. return false
  85. end
  86. return true
  87. end
  88.  
  89. function refuel( ammount )
  90. local fuelLevel = turtle.getFuelLevel()
  91. if fuelLevel == "unlimited" then
  92. return true
  93. end
  94.  
  95. local needed = ammount or (xPos + zPos + depth + 2)
  96. if turtle.getFuelLevel() < needed then
  97. local fueled = false
  98. for n=1,16 do
  99. if turtle.getItemCount(n) > 0 then
  100. turtle.select(n)
  101. if turtle.refuel(1) then
  102. while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  103. turtle.refuel(1)
  104. end
  105. if turtle.getFuelLevel() >= needed then
  106. turtle.select(1)
  107. return true
  108. end
  109. end
  110. end
  111. end
  112. turtle.select(1)
  113. return false
  114. end
  115.  
  116. return true
  117. end
  118.  
  119. local function tryForwards()
  120. if not refuel() then
  121. print( "Not enough Fuel" )
  122. returnSupplies()
  123. end
  124.  
  125. while not turtle.forward() do
  126. if turtle.detect() then
  127. if turtle.dig() then
  128. if not collect() then
  129. returnSupplies()
  130. end
  131. else
  132. return false
  133. end
  134. elseif turtle.attack() then
  135. if not collect() then
  136. returnSupplies()
  137. end
  138. else
  139. sleep( 0.5 )
  140. end
  141. end
  142.  
  143. xPos = xPos + xDir
  144. zPos = zPos + zDir
  145. return true
  146. end
  147.  
  148. local function tryDown()
  149. if not refuel() then
  150. print( "Not enough Fuel" )
  151. returnSupplies()
  152. end
  153.  
  154. while not turtle.down() do
  155. if turtle.detectDown() then
  156. if turtle.digDown() then
  157. if not collect() then
  158. returnSupplies()
  159. end
  160. else
  161. return false
  162. end
  163. elseif turtle.attackDown() then
  164. if not collect() then
  165. returnSupplies()
  166. end
  167. else
  168. sleep( 0.5 )
  169. end
  170. end
  171.  
  172. depth = depth + 1
  173. if math.fmod( depth, 10 ) == 0 then
  174. print( "Descended "..depth.." metres." )
  175. end
  176.  
  177. return true
  178. end
  179.  
  180. local function turnLeft()
  181. turtle.turnLeft()
  182. xDir, zDir = -zDir, xDir
  183. end
  184.  
  185. local function turnRight()
  186. turtle.turnRight()
  187. xDir, zDir = zDir, -xDir
  188. end
  189.  
  190. function goTo( x, y, z, xd, zd )
  191. while depth > y do
  192. if turtle.up() then
  193. depth = depth - 1
  194. elseif turtle.digUp() or turtle.attackUp() then
  195. collect()
  196. else
  197. sleep( 0.5 )
  198. end
  199. end
  200.  
  201. if xPos > x then
  202. while xDir ~= -1 do
  203. turnLeft()
  204. end
  205. while xPos > x do
  206. if turtle.forward() then
  207. xPos = xPos - 1
  208. elseif turtle.dig() or turtle.attack() then
  209. collect()
  210. else
  211. sleep( 0.5 )
  212. end
  213. end
  214. elseif xPos < x then
  215. while xDir ~= 1 do
  216. turnLeft()
  217. end
  218. while xPos < x do
  219. if turtle.forward() then
  220. xPos = xPos + 1
  221. elseif turtle.dig() or turtle.attack() then
  222. collect()
  223. else
  224. sleep( 0.5 )
  225. end
  226. end
  227. end
  228.  
  229. if zPos > z then
  230. while zDir ~= -1 do
  231. turnLeft()
  232. end
  233. while zPos > z do
  234. if turtle.forward() then
  235. zPos = zPos - 1
  236. elseif turtle.dig() or turtle.attack() then
  237. collect()
  238. else
  239. sleep( 0.5 )
  240. end
  241. end
  242. elseif zPos < z then
  243. while zDir ~= 1 do
  244. turnLeft()
  245. end
  246. while zPos < z do
  247. if turtle.forward() then
  248. zPos = zPos + 1
  249. elseif turtle.dig() or turtle.attack() then
  250. collect()
  251. else
  252. sleep( 0.5 )
  253. end
  254. end
  255. end
  256.  
  257. while depth < y do
  258. if turtle.down() then
  259. depth = depth + 1
  260. elseif turtle.digDown() or turtle.attackDown() then
  261. collect()
  262. else
  263. sleep( 0.5 )
  264. end
  265. end
  266.  
  267. while zDir ~= zd or xDir ~= xd do
  268. turnLeft()
  269. end
  270. end
  271.  
  272. if not refuel() then
  273. print( "Out of Fuel" )
  274. return
  275. end
  276.  
  277. print( "Excavating..." )
  278.  
  279. local reseal = false
  280. turtle.select(1)
  281. if turtle.digDown() then
  282. reseal = true
  283. end
  284.  
  285. local alternate = 0
  286. local done = false
  287. while not done do
  288. for n=1,size do
  289. for m=1,size-1 do
  290. if not tryForwards() then
  291. done = true
  292. break
  293. end
  294. end
  295. if done then
  296. break
  297. end
  298. if n<size then
  299. if math.fmod(n + alternate,2) == 0 then
  300. turnLeft()
  301. if not tryForwards() then
  302. done = true
  303. break
  304. end
  305. turnLeft()
  306. else
  307. turnRight()
  308. if not tryForwards() then
  309. done = true
  310. break
  311. end
  312. turnRight()
  313. end
  314. end
  315. end
  316. if done then
  317. break
  318. end
  319.  
  320. if size > 1 then
  321. if math.fmod(size,2) == 0 then
  322. turnRight()
  323. else
  324. if alternate == 0 then
  325. turnLeft()
  326. else
  327. turnRight()
  328. end
  329. alternate = 1 - alternate
  330. end
  331. end
  332.  
  333. if not tryDown() then
  334. done = true
  335. break
  336. end
  337. end
  338.  
  339. print( "Returning to surface..." )
  340.  
  341. -- Return to where we started
  342. goTo( 0,0,0,0,-1 )
  343. unload( false )
  344. goTo( 0,0,0,0,1 )
  345.  
  346. -- Seal the hole
  347. if reseal then
  348. turtle.placeDown()
  349. end
  350.  
  351. print( "Mined "..(collected + unloaded).." items total." )
Add Comment
Please, Sign In to add comment