SoundChaos

Modified Excavate

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