Advertisement
whenov

Untitled

Apr 25th, 2013
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.44 KB | None | 0 0
  1.  
  2. local tArgs = { ... }
  3. if #tArgs ~= 1 then
  4. print( "Usage: excavate <diameter>" )
  5. return
  6. end
  7.  
  8. -- Mine in a quarry pattern until we hit something we can't dig
  9. local size = tonumber( tArgs[1] )
  10. if size < 1 then
  11. print( "Excavate diameter must be positive" )
  12. return
  13. end
  14.  
  15. local depth = 0
  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. turtle.select(15) -- cobblestone
  70. for n=1,13 do
  71. local nCount = turtle.getItemCount(n)
  72. if nCount == 0 then
  73. bFull = false
  74. else -- drop useless items
  75. turtle.compareTo(14) and turtle.drop() -- cobblestone
  76. turtle.compareTo(15) and turtle.drop() -- gravel
  77. turtle.compareTo(16) and turtle.drop() -- dirt
  78. end
  79. nTotalItems = nTotalItems + nCount
  80. end
  81. turtle.select(1)
  82.  
  83. if nTotalItems > collected then
  84. collected = nTotalItems
  85. if math.fmod(collected + unloaded, 50) == 0 then
  86. print( "Mined "..(collected + unloaded).." items." )
  87. end
  88. end
  89.  
  90. if bFull then
  91. print( "No empty slots left." )
  92. return false
  93. end
  94. return true
  95. end
  96.  
  97. function refuel( ammount )
  98. local fuelLevel = turtle.getFuelLevel()
  99. if fuelLevel == "unlimited" then
  100. return true
  101. end
  102.  
  103. local needed = ammount or (xPos + zPos + depth + 1)
  104. if turtle.getFuelLevel() < needed then
  105. local fueled = false
  106. for n=1,16 do
  107. if turtle.getItemCount(n) > 0 then
  108. turtle.select(n)
  109. if turtle.refuel(1) then
  110. while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  111. turtle.refuel(1)
  112. end
  113. if turtle.getFuelLevel() >= needed then
  114. turtle.select(1)
  115. return true
  116. end
  117. end
  118. end
  119. end
  120. turtle.select(1)
  121. return false
  122. end
  123.  
  124. return true
  125. end
  126.  
  127. local function tryForwards()
  128. if not refuel() then
  129. print( "Not enough Fuel" )
  130. returnSupplies()
  131. end
  132.  
  133. while not turtle.forward() do
  134. if turtle.detect() then
  135. if turtle.dig() then
  136. if not collect() then
  137. returnSupplies()
  138. end
  139. else
  140. return false
  141. end
  142. elseif turtle.attack() then
  143. if not collect() then
  144. returnSupplies()
  145. end
  146. else
  147. sleep( 0.5 )
  148. end
  149. end
  150.  
  151. xPos = xPos + xDir
  152. zPos = zPos + zDir
  153. return true
  154. end
  155.  
  156. local function tryDown()
  157. if not refuel() then
  158. print( "Not enough Fuel" )
  159. returnSupplies()
  160. end
  161.  
  162. while not turtle.down() do
  163. if turtle.detectDown() then
  164. if turtle.digDown() then
  165. if not collect() then
  166. returnSupplies()
  167. end
  168. else
  169. return false
  170. end
  171. elseif turtle.attackDown() then
  172. if not collect() then
  173. returnSupplies()
  174. end
  175. else
  176. sleep( 0.5 )
  177. end
  178. end
  179.  
  180. depth = depth + 1
  181. if math.fmod( depth, 10 ) == 0 then
  182. print( "Descended "..depth.." metres." )
  183. end
  184.  
  185. return true
  186. end
  187.  
  188. local function turnLeft()
  189. turtle.turnLeft()
  190. xDir, zDir = -zDir, xDir
  191. end
  192.  
  193. local function turnRight()
  194. turtle.turnRight()
  195. xDir, zDir = zDir, -xDir
  196. end
  197.  
  198. function goTo( x, y, z, xd, zd )
  199. while depth > y do
  200. if turtle.up() then
  201. depth = depth - 1
  202. elseif turtle.digUp() or turtle.attackUp() then
  203. collect()
  204. else
  205. sleep( 0.5 )
  206. end
  207. end
  208.  
  209. if xPos > x then
  210. while xDir ~= -1 do
  211. turnLeft()
  212. end
  213. while xPos > x do
  214. if turtle.forward() then
  215. xPos = xPos - 1
  216. elseif turtle.dig() or turtle.attack() then
  217. collect()
  218. else
  219. sleep( 0.5 )
  220. end
  221. end
  222. elseif xPos < x then
  223. while xDir ~= 1 do
  224. turnLeft()
  225. end
  226. while xPos < x do
  227. if turtle.forward() then
  228. xPos = xPos + 1
  229. elseif turtle.dig() or turtle.attack() then
  230. collect()
  231. else
  232. sleep( 0.5 )
  233. end
  234. end
  235. end
  236.  
  237. if zPos > z then
  238. while zDir ~= -1 do
  239. turnLeft()
  240. end
  241. while zPos > z do
  242. if turtle.forward() then
  243. zPos = zPos - 1
  244. elseif turtle.dig() or turtle.attack() then
  245. collect()
  246. else
  247. sleep( 0.5 )
  248. end
  249. end
  250. elseif zPos < z then
  251. while zDir ~= 1 do
  252. turnLeft()
  253. end
  254. while zPos < z do
  255. if turtle.forward() then
  256. zPos = zPos + 1
  257. elseif turtle.dig() or turtle.attack() then
  258. collect()
  259. else
  260. sleep( 0.5 )
  261. end
  262. end
  263. end
  264.  
  265. while depth < y do
  266. if turtle.down() then
  267. depth = depth + 1
  268. elseif turtle.digDown() or turtle.attackDown() then
  269. collect()
  270. else
  271. sleep( 0.5 )
  272. end
  273. end
  274.  
  275. while zDir ~= zd or xDir ~= xd do
  276. turnLeft()
  277. end
  278. end
  279.  
  280. if not refuel() then
  281. print( "Out of Fuel" )
  282. return
  283. end
  284.  
  285. print( "Excavating..." )
  286.  
  287. local reseal = false
  288. turtle.select(1)
  289. if turtle.digDown() then
  290. reseal = true
  291. end
  292.  
  293. local alternate = 0
  294. local done = false
  295. while not done do
  296. for n=1,size do
  297. for m=1,size-1 do
  298. if not tryForwards() then
  299. done = true
  300. break
  301. end
  302. end
  303. if done then
  304. break
  305. end
  306. if n<size then
  307. if math.fmod(n + alternate,2) == 0 then
  308. turnLeft()
  309. if not tryForwards() then
  310. done = true
  311. break
  312. end
  313. turnLeft()
  314. else
  315. turnRight()
  316. if not tryForwards() then
  317. done = true
  318. break
  319. end
  320. turnRight()
  321. end
  322. end
  323. end
  324. if done then
  325. break
  326. end
  327.  
  328. if size > 1 then
  329. if math.fmod(size,2) == 0 then
  330. turnRight()
  331. else
  332. if alternate == 0 then
  333. turnLeft()
  334. else
  335. turnRight()
  336. end
  337. alternate = 1 - alternate
  338. end
  339. end
  340.  
  341. if not tryDown() then
  342. done = true
  343. break
  344. end
  345. end
  346.  
  347. print( "Returning to surface..." )
  348.  
  349. -- Return to where we started
  350. goTo( 0,0,0,0,-1 )
  351. unload( false )
  352. goTo( 0,0,0,0,1 )
  353.  
  354. -- Seal the hole
  355. if reseal then
  356. turtle.placeDown()
  357. end
  358.  
  359. print( "Mined "..(collected + unloaded).." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement