Advertisement
Guest User

Untitled

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