Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.91 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. if not collect() then
  130. returnSupplies()
  131. end
  132. else
  133. return false
  134. end
  135. end
  136. end
  137.  
  138. xPos = xPos + xDir
  139. zPos = zPos + zDir
  140. return true
  141. end
  142.  
  143. local function tryDown()
  144. if not refuel() then
  145. print( "Not enough Fuel" )
  146. returnSupplies()
  147. end
  148.  
  149. while not turtle.down() do
  150. if turtle.detectDown() then
  151. if turtle.digDown() then
  152. if not collect() then
  153. returnSupplies()
  154. end
  155. else
  156. return false
  157. end
  158. else
  159. sleep( 0.5 )
  160. end
  161. end
  162.  
  163. depth = depth + 1
  164. if math.fmod( depth, 10 ) == 0 then
  165. print( "Descended "..depth.." metres." )
  166. end
  167.  
  168. return true
  169. end
  170.  
  171. local function turnLeft()
  172. turtle.turnLeft()
  173. xDir, zDir = -zDir, xDir
  174. end
  175.  
  176. local function turnRight()
  177. turtle.turnRight()
  178. xDir, zDir = zDir, -xDir
  179. end
  180.  
  181. function goTo( x, y, z, xd, zd )
  182. while depth > y do
  183. if turtle.up() then
  184. depth = depth - 1
  185. elseif turtle.digUp() then
  186. collect()
  187. else
  188. sleep( 0.5 )
  189. end
  190. end
  191.  
  192. if xPos > x then
  193. while xDir ~= -1 do
  194. turnLeft()
  195. end
  196. while xPos > x do
  197. if turtle.forward() then
  198. xPos = xPos - 1
  199. elseif turtle.dig() then
  200. collect()
  201. else
  202. sleep( 0.5 )
  203. end
  204. end
  205. elseif xPos < x then
  206. while xDir ~= 1 do
  207. turnLeft()
  208. end
  209. while xPos < x do
  210. if turtle.forward() then
  211. xPos = xPos + 1
  212. elseif turtle.dig() then
  213. collect()
  214. else
  215. sleep( 0.5 )
  216. end
  217. end
  218. end
  219.  
  220. if zPos > z then
  221. while zDir ~= -1 do
  222. turnLeft()
  223. end
  224. while zPos > z do
  225. if turtle.forward() then
  226. zPos = zPos - 1
  227. elseif turtle.dig() then
  228. collect()
  229. else
  230. sleep( 0.5 )
  231. end
  232. end
  233. elseif zPos < z then
  234. while zDir ~= 1 do
  235. turnLeft()
  236. end
  237. while zPos < z do
  238. if turtle.forward() then
  239. zPos = zPos + 1
  240. elseif turtle.dig() then
  241. collect()
  242. else
  243. sleep( 0.5 )
  244. end
  245. end
  246. end
  247.  
  248. while depth < y do
  249. if turtle.down() then
  250. depth = depth + 1
  251. elseif turtle.digDown() then
  252. collect()
  253. else
  254. sleep( 0.5 )
  255. end
  256. end
  257.  
  258. while zDir ~= zd or xDir ~= xd do
  259. turnLeft()
  260. end
  261. end
  262.  
  263. if not refuel() then
  264. print( "Out of Fuel" )
  265. return
  266. end
  267.  
  268. print( "Excavating..." )
  269.  
  270. local reseal = false
  271. turtle.select(1)
  272. if turtle.digDown() then
  273. reseal = true
  274. end
  275.  
  276. local alternate = 0
  277. local done = false
  278. while not done do
  279. for n=1,size do
  280. for m=1,size-1 do
  281. if not tryForwards() then
  282. done = true
  283. break
  284. end
  285.  
  286. if turtle.detectUp() then
  287. if turtle.digUp() then
  288. if not collect() then
  289. returnSupplies()
  290. end
  291. end
  292. end
  293.  
  294. if turtle.detectDown() then
  295. if turtle.digDown() then
  296. if not collect() then
  297. returnSupplies()
  298. end
  299. end
  300. end
  301. end
  302. if done then
  303. break
  304. end
  305. if n<size then
  306. if math.fmod(n + alternate,2) == 0 then
  307. turnLeft()
  308. if not tryForwards() then
  309. done = true
  310. break
  311. end
  312.  
  313. if turtle.detectUp() then
  314. if turtle.digUp() then
  315. if not collect() then
  316. returnSupplies()
  317. end
  318. end
  319. end
  320.  
  321. if turtle.detectDown() then
  322. if turtle.digDown() then
  323. if not collect() then
  324. returnSupplies()
  325. end
  326. end
  327. end
  328.  
  329. turnLeft()
  330. else
  331. turnRight()
  332. if not tryForwards() then
  333. done = true
  334. break
  335. end
  336.  
  337. if turtle.detectUp() then
  338. if turtle.digUp() then
  339. if not collect() then
  340. returnSupplies()
  341. end
  342. end
  343. end
  344.  
  345. if turtle.detectDown() then
  346. if turtle.digDown() then
  347. if not collect() then
  348. returnSupplies()
  349. end
  350. end
  351. end
  352.  
  353. turnRight()
  354. end
  355. end
  356. end
  357. if done then
  358. break
  359. end
  360.  
  361. if size > 1 then
  362. if math.fmod(size,2) == 0 then
  363. turnRight()
  364. else
  365. if alternate == 0 then
  366. turnLeft()
  367. else
  368. turnRight()
  369. end
  370. alternate = 1 - alternate
  371. end
  372. end
  373.  
  374. for i=1,3 do
  375. if turtle.detectUp() then
  376. if turtle.digUp() then
  377. if not collect() then
  378. returnSupplies()
  379. end
  380. end
  381. end
  382.  
  383. if not tryDown() then
  384. done = true
  385. break
  386. end
  387.  
  388. if turtle.detectDown() then
  389. if turtle.digDown() then
  390. if not collect() then
  391. returnSupplies()
  392. end
  393. end
  394. end
  395. end
  396. end
  397.  
  398. print( "Returning to surface..." )
  399.  
  400. -- Return to where we started
  401. goTo( 0,0,0,0,-1 )
  402. unload( false )
  403. goTo( 0,0,0,0,1 )
  404.  
  405. -- Seal the hole
  406. if reseal then
  407. turtle.placeDown()
  408. end
  409.  
  410. print( "Mined "..(collected + unloaded).." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement