Advertisement
Guest User

Untitled

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