Lanzr

Untitled

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