theCountChuckula

enderdig

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