Advertisement
thaile4ever

mine 2.0

Mar 6th, 2013
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.68 KB | None | 0 0
  1. local tArgs = { ... }
  2.  
  3. -- Mine in a quarry pattern until we hit something we can't dig
  4. local size = tonumber( tArgs[1] )
  5. if size < 1 then
  6. print( "Excavate diameter must be positive" )
  7. return
  8. end
  9.  
  10. local depth = 0
  11.  
  12. if #tArgs == 2 then
  13. depth = tonumber(tArgs[2])
  14. end
  15.  
  16. local unloaded = 0
  17. local collected = 0
  18.  
  19. local xPos,zPos = 0,0
  20. local xDir,zDir = 0,1
  21.  
  22. local goTo -- Filled in further down
  23. local refuel -- Filled in further down
  24.  
  25. local function unload( _bKeepOneFuelStack )
  26. print( "Unloading items..." )
  27. for n=1,16 do
  28. local nCount = turtle.getItemCount(n)
  29. if nCount > 0 then
  30. turtle.select(n)
  31. local bDrop = true
  32. if _bKeepOneFuelStack and turtle.refuel(0) then
  33. bDrop = false
  34. _bKeepOneFuelStack = false
  35. end
  36. if bDrop then
  37. turtle.drop()
  38. unloaded = unloaded + nCount
  39. end
  40. end
  41. end
  42. collected = 0
  43. turtle.select(1)
  44. end
  45.  
  46. local function returnSupplies()
  47. local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  48. print( "Returning to surface..." )
  49. goTo( 0,0,0,0,-1 )
  50.  
  51. local fuelNeeded = 2*(x+y+z) + 1
  52. if not refuel( fuelNeeded ) then
  53. unload( true )
  54. print( "Waiting for fuel" )
  55. while not refuel( fuelNeeded ) do
  56. sleep(1)
  57. end
  58. else
  59. unload( true )
  60. end
  61.  
  62. print( "Resuming mining..." )
  63. goTo( x,y,z,xd,zd )
  64. end
  65.  
  66. local function collect()
  67. local bFull = true
  68. local nTotalItems = 0
  69. for n=1,16 do
  70. local nCount = turtle.getItemCount(n)
  71. if nCount == 0 then
  72. bFull = false
  73. end
  74. nTotalItems = nTotalItems + nCount
  75. end
  76.  
  77. if nTotalItems > collected then
  78. collected = nTotalItems
  79. if math.fmod(collected + unloaded, 50) == 0 then
  80. print( "Mined "..(collected + unloaded).." items." )
  81. end
  82. end
  83.  
  84. if bFull then
  85. print( "No empty slots left." )
  86. return false
  87. end
  88. return true
  89. end
  90.  
  91. function refuel( ammount )
  92. local fuelLevel = turtle.getFuelLevel()
  93. if fuelLevel == "unlimited" then
  94. return true
  95. end
  96.  
  97. local needed = ammount or (xPos + zPos + depth + 1)
  98. if turtle.getFuelLevel() < needed then
  99. local fueled = false
  100. for n=1,16 do
  101. if turtle.getItemCount(n) > 0 then
  102. turtle.select(n)
  103. if turtle.refuel(1) then
  104. while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  105. turtle.refuel(1)
  106. end
  107. if turtle.getFuelLevel() >= needed then
  108. turtle.select(1)
  109. return true
  110. end
  111. end
  112. end
  113. end
  114. turtle.select(1)
  115. return false
  116. end
  117.  
  118. return true
  119. end
  120.  
  121. local function tryDig()
  122. if turtle.detectUp() then
  123. if turtle.digUp() then
  124. if not collect() then
  125. returnSupplies()
  126. end
  127. else
  128. sleep(1)
  129. end
  130. end
  131.  
  132. if turtle.detectDown() then
  133. if turtle.digDown() then
  134. if not collect() then
  135. returnSupplies()
  136. end
  137. else
  138. sleep(1)
  139. end
  140. end
  141.  
  142.  
  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.  
  314. if depth > 0 then
  315. for n=0, depth do
  316. tryDown()
  317. end
  318. end
  319.  
  320.  
  321. while not done do
  322. for n=1,size do
  323. for m=1,size-1 do
  324. tryDig()
  325. if not tryForwards() then
  326. done = true
  327. break
  328. end
  329. end
  330. if done then
  331. break
  332. end
  333. if n<size then
  334. if math.fmod(n + alternate,2) == 0 then
  335. turnLeft()
  336. tryDig()
  337. if not tryForwards() then
  338. done = true
  339. break
  340. end
  341. tryDig()
  342. turnLeft()
  343. else
  344. turnRight()
  345. tryDig()
  346. if not tryForwards() then
  347. done = true
  348. break
  349. end
  350. tryDig()
  351. turnRight()
  352. end
  353. end
  354. end
  355. if done then
  356. break
  357. end
  358.  
  359. if size > 1 then
  360. if math.fmod(size,2) == 0 then
  361. turnRight()
  362. else
  363. if alternate == 0 then
  364. turnLeft()
  365. else
  366. turnRight()
  367. end
  368. alternate = 1 - alternate
  369. end
  370. end
  371.  
  372. tryDig()
  373. tryDown()
  374. tryDown()
  375.  
  376. if not tryDown() then
  377. done = true
  378. break
  379. end
  380. end
  381.  
  382. print( "Returning to surface..." )
  383.  
  384. -- Return to where we started
  385. goTo( 0,0,0,0,-1 )
  386. unload( false )
  387. goTo( 0,0,0,0,1 )
  388.  
  389. -- Seal the hole
  390. if reseal then
  391. turtle.placeDown()
  392. end
  393.  
  394. print( "Mined "..(collected + unloaded).." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement