SoundChaos

Excavate 2.0

Sep 6th, 2014
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.17 KB | None | 0 0
  1. local tArgs = { ... }
  2. if #tArgs ~= 1 and #tArgs ~= 2 then
  3. print( "Usage: excavate <diameter>" )
  4. return
  5. end
  6.  
  7. -- Variable to drop all cobblestone after every 8 blocks
  8. local l = 1
  9.  
  10. print("Would you like me to keep dirt, stone, and gravel? (type 'y' or 'n')")
  11. toKeep = io.read()
  12.  
  13. -- get depth argument
  14. local maxDepth = tonumber( tArgs[2] )
  15. if #tArgs == 1 then
  16. print( "No valid depth given, I will dig to bedrock" )
  17. end
  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 xPos,zPos = 0,0
  31. local xDir,zDir = 0,1
  32.  
  33. local goTo -- Filled in further down
  34. local refuel -- Filled in further down
  35.  
  36. -- List of items to throw out
  37. -- TODO: Implement argument to bypass this
  38. local dontKeep = {
  39. "minecraft:stone",
  40. "minecraft:cobblestone",
  41. "minecraft:dirt",
  42. "chisel:marble2",
  43. "chisel:limestone2",
  44. "minecraft:gravel"
  45. }
  46.  
  47. local function unload( _bKeepOneFuelStack )
  48. print( "Unloading items..." )
  49. for n=3,16 do
  50. local nCount = turtle.getItemCount(n)
  51. if nCount > 0 then
  52. turtle.select(n)
  53. local bDrop = true
  54. if _bKeepOneFuelStack and turtle.refuel(0) then
  55. bDrop = false
  56. _bKeepOneFuelStack = false
  57. end
  58. if bDrop then
  59. turtle.drop()
  60. unloaded = unloaded + nCount
  61. end
  62. end
  63. end
  64. collected = 0
  65. turtle.select(1)
  66. end
  67.  
  68. local function returnSupplies()
  69. local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  70. print( "Returning to surface..." )
  71. goTo( 0,0,0,0,-1 )
  72.  
  73. local fuelNeeded = 2*(x+y+z) + 1
  74. if not refuel( fuelNeeded ) then
  75. unload( true )
  76. print( "Waiting for fuel" )
  77. while not refuel( fuelNeeded ) do
  78. sleep(2)
  79. end
  80. else
  81. unload( true )
  82. end
  83.  
  84. print( "Resuming mining..." )
  85. goTo( x,y,z,xd,zd )
  86. end
  87.  
  88. -- Check of variable x exists within list
  89. function contains(list, x)
  90. for _, v in pairs(list) do
  91. if v == x then return true end
  92. end
  93. return false
  94. end
  95.  
  96. local function collect()
  97. local bFull = true
  98. local nTotalItems = 0
  99. for n=1,16 do
  100. local nCount = turtle.getItemCount(n)
  101. if nCount == 0 then
  102. bFull = false
  103. end
  104. nTotalItems = nTotalItems + nCount
  105. end
  106.  
  107.  
  108. --Drop items if they are in the dontKeep list
  109. -----------------------------------------------------------------------------------------
  110. if toKeep == "n" then
  111. l = l + 1
  112. if l == 31 then
  113. -- local u = 3 -- this is for the repeat loop to drop any block that is the same as slot 2
  114. -- turtle.select(2) -- select slot 2
  115. -- repeat
  116. -- if turtle.compareTo(u) then -- if slot 2 is equal to U
  117. -- turtle.select(u) -- Select slot U
  118. -- turtle.drop() -- drop slot U
  119. -- turtle.select(2) -- select slot two again
  120. -- end
  121. -- u = u + 1 -- change u to the next slot
  122. -- until u == 17
  123. -- turtle.select(1)
  124.  
  125. for i=2,16 do
  126. if turtle.getItemCount(i) > 0 then
  127. if contains(dontKeep,turtle.getItemDetail(i).name) then
  128. turtle.select(i)
  129. turtle.drop()
  130. turtle.select(1)
  131. end
  132. end
  133. end
  134. l = 1
  135. end
  136. end
  137. -----------------------------------------------------------------------------------------
  138.  
  139. if nTotalItems > collected then
  140. collected = nTotalItems
  141. if math.fmod(collected + unloaded, 50) == 0 then
  142. print( "Mined "..(collected + unloaded).." items." )
  143. end
  144. end
  145.  
  146. if bFull then
  147. print( "No empty slots left." )
  148. return false
  149. end
  150. return true
  151. end
  152.  
  153. function refuel( ammount )
  154. local fuelLevel = turtle.getFuelLevel()
  155. if fuelLevel == "unlimited" then
  156. return true
  157. end
  158.  
  159. local needed = ammount or (xPos + zPos + depth + 2)
  160. if turtle.getFuelLevel() < needed then
  161. local fueled = false
  162. for n=1,16 do
  163. if turtle.getItemCount(n) > 0 then
  164. turtle.select(n)
  165. if turtle.refuel(1) then
  166. while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  167. turtle.refuel(1)
  168. end
  169. if turtle.getFuelLevel() >= needed then
  170. turtle.select(1)
  171. return true
  172. end
  173. end
  174. end
  175. end
  176. turtle.select(1)
  177. return false
  178. end
  179.  
  180. return true
  181. end
  182.  
  183. local function tryForwards()
  184. if not refuel() then
  185. print( "Not enough Fuel" )
  186. returnSupplies()
  187. end
  188.  
  189. if turtle.detectUp() then
  190. if turtle.digUp() then
  191. if not collect() then
  192. returnSupplies()
  193. end
  194. end
  195. end
  196. if turtle.detectDown() then
  197. if turtle.digDown() then
  198. if not collect() then
  199. returnSupplies()
  200. end
  201. end
  202. end
  203.  
  204. while not turtle.forward() do
  205. if turtle.detect() then
  206. if turtle.dig() then
  207. if not collect() then
  208. returnSupplies()
  209. end
  210. else
  211. return false
  212. end
  213. elseif turtle.attack() or turtle.attackUp() then
  214. if not collect() then
  215. returnSupplies()
  216. end
  217. else
  218. sleep( 0.3 )
  219. end
  220. end
  221.  
  222. xPos = xPos + xDir
  223. zPos = zPos + zDir
  224. return true
  225. end
  226.  
  227. local function tryDown()
  228. if not refuel() then
  229. print( "Not enough Fuel" )
  230. returnSupplies()
  231. end
  232.  
  233. -- Custom change: Check if depth = maxDepth,
  234. if #tArgs == 2 then
  235. if depth + 1 == maxDepth then
  236. return false
  237. end
  238. end
  239.  
  240. if turtle.detectUp() then
  241. if turtle.digUp() then
  242. if not collect() then
  243. returnSupplies()
  244. end
  245. end
  246. end
  247.  
  248. for i = 1,3 do
  249. while not turtle.down() do
  250. if turtle.detectDown() then
  251. if turtle.digDown() then
  252. if not collect() then
  253. returnSupplies()
  254. end
  255. else
  256. return false
  257. end
  258. elseif turtle.attackDown() then
  259. if not collect() then
  260. returnSupplies()
  261. end
  262. else
  263. sleep( 0.3 )
  264. end
  265. end
  266. depth = depth + 1
  267. end
  268.  
  269. if math.fmod( depth, 10 ) == 0 then
  270. print( "Descended "..depth.." metres." )
  271. end
  272.  
  273. return true
  274. end
  275.  
  276. local function turnLeft()
  277. turtle.turnLeft()
  278. xDir, zDir = -zDir, xDir
  279. end
  280.  
  281. local function turnRight()
  282. turtle.turnRight()
  283. xDir, zDir = zDir, -xDir
  284. end
  285.  
  286. function goTo( x, y, z, xd, zd )
  287. while depth > y do
  288. if turtle.up() then
  289. depth = depth - 1
  290. elseif turtle.digUp() or turtle.attackUp() then
  291. collect()
  292. else
  293. sleep( 0.3 )
  294. end
  295. end
  296.  
  297. if xPos > x then
  298. while xDir ~= -1 do
  299. turnLeft()
  300. end
  301. while xPos > x do
  302. if turtle.forward() then
  303. xPos = xPos - 1
  304. elseif turtle.dig() or turtle.attack() or turtle.attackUp() then
  305. collect()
  306. else
  307. sleep( 0.3 )
  308. end
  309. end
  310. elseif xPos < x then
  311. while xDir ~= 1 do
  312. turnLeft()
  313. end
  314. while xPos < x do
  315. if turtle.forward() then
  316. xPos = xPos + 1
  317. elseif turtle.dig() or turtle.attack() or turtle.attackUp() then
  318. collect()
  319. else
  320. sleep( 0.3 )
  321. end
  322. end
  323. end
  324.  
  325. if zPos > z then
  326. while zDir ~= -1 do
  327. turnLeft()
  328. end
  329. while zPos > z do
  330. if turtle.forward() then
  331. zPos = zPos - 1
  332. elseif turtle.dig() or turtle.attack() or turtle.attackUp() then
  333. collect()
  334. else
  335. sleep( 0.3 )
  336. end
  337. end
  338. elseif zPos < z then
  339. while zDir ~= 1 do
  340. turnLeft()
  341. end
  342. while zPos < z do
  343. if turtle.forward() then
  344. zPos = zPos + 1
  345. elseif turtle.dig() or turtle.attack() or turtle.attackUp() then
  346. collect()
  347. else
  348. sleep( 0.3 )
  349. end
  350. end
  351. end
  352.  
  353. while depth < y do
  354. if turtle.down() then
  355. depth = depth + 1
  356. elseif turtle.digDown() or turtle.attackDown() then
  357. collect()
  358. else
  359. sleep( 0.3 )
  360. end
  361. end
  362.  
  363. while zDir ~= zd or xDir ~= xd do
  364. turnLeft()
  365. end
  366. end
  367.  
  368. if not refuel() then
  369. print( "Out of Fuel" )
  370. return
  371. end
  372.  
  373. print( "Excavating..." )
  374.  
  375. local reseal = false
  376. turtle.select(1)
  377. if turtle.digDown() then
  378. reseal = true
  379. end
  380.  
  381. local alternate = 0
  382. local done = false
  383. while not done do
  384. for n=1,size do
  385. for m=1,size-1 do
  386. if not tryForwards() then
  387. done = true
  388. break
  389. end
  390. end
  391. if done then
  392. break
  393. end
  394. if n<size then
  395. if math.fmod(n + alternate,2) == 0 then
  396. turnLeft()
  397. if not tryForwards() then
  398. done = true
  399. break
  400. end
  401. turnLeft()
  402. else
  403. turnRight()
  404. if not tryForwards() then
  405. done = true
  406. break
  407. end
  408. turnRight()
  409. end
  410. end
  411. end
  412. if done then
  413. break
  414. end
  415.  
  416. if size > 1 then
  417. if math.fmod(size,2) == 0 then
  418. turnRight()
  419. else
  420. if alternate == 0 then
  421. turnLeft()
  422. else
  423. turnRight()
  424. end
  425. alternate = 1 - alternate
  426. end
  427. end
  428.  
  429. if not tryDown() then
  430. done = true
  431. break
  432. end
  433. end
  434.  
  435. print( "Returning to surface..." )
  436.  
  437. -- Return to where we started
  438. goTo( 0,0,0,0,-1 )
  439. unload( false )
  440. goTo( 0,0,0,0,1 )
  441.  
  442. -- Seal the hole
  443. if reseal then
  444. turtle.placeDown()
  445. end
  446.  
  447. print( "Mined "..(collected + unloaded).." items total." )
Add Comment
Please, Sign In to add comment