Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.47 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. turtle.select(n)
  70. local data = turtle.getItemDetail(n)
  71.  
  72. if data ~= nil then
  73. if data.name == "minecraft:cobblestone" or
  74. data.name == "minecraft:sand" or
  75. data.name == "minecraft:gravel" then
  76. turtle.drop()
  77. end
  78. end
  79.  
  80. local nCount = turtle.getItemCount(n)
  81. if nCount == 0 then
  82. bFull = false
  83. end
  84. nTotalItems = nTotalItems + nCount
  85. end
  86.  
  87. if nTotalItems > collected then
  88. collected = nTotalItems
  89. if math.fmod(collected + unloaded, 50) == 0 then
  90. print( "Mined "..(collected + unloaded).." items." )
  91. end
  92. end
  93.  
  94. if bFull then
  95. print( "No empty slots left." )
  96. return false
  97. end
  98. return true
  99. end
  100.  
  101. function refuel( ammount )
  102. local fuelLevel = turtle.getFuelLevel()
  103. if fuelLevel == "unlimited" then
  104. return true
  105. end
  106.  
  107. local needed = ammount or (xPos + zPos + depth + 2)
  108. if turtle.getFuelLevel() < needed then
  109. local fueled = false
  110. for n=1,16 do
  111. if turtle.getItemCount(n) > 0 then
  112. turtle.select(n)
  113. if turtle.refuel(1) then
  114. while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  115. turtle.refuel(1)
  116. end
  117. if turtle.getFuelLevel() >= needed then
  118. turtle.select(1)
  119. return true
  120. end
  121. end
  122. end
  123. end
  124. turtle.select(1)
  125. return false
  126. end
  127.  
  128. return true
  129. end
  130.  
  131. local function tryForwards()
  132. if not refuel() then
  133. print( "Not enough Fuel" )
  134. returnSupplies()
  135. end
  136.  
  137. while not turtle.forward() do
  138. if turtle.detect() then
  139. if turtle.dig() then
  140. if not collect() then
  141. returnSupplies()
  142. end
  143. else
  144. return false
  145. end
  146. end
  147. end
  148.  
  149. xPos = xPos + xDir
  150. zPos = zPos + zDir
  151. return true
  152. end
  153.  
  154. local function tryUp()
  155. if turtle.detectUp() then
  156. if turtle.digUp() then
  157. if not collect() then
  158. returnSupplies()
  159. end
  160. end
  161. end
  162. end
  163.  
  164. local function tryDown()
  165. if turtle.detectDown() then
  166. if turtle.digDown() then
  167. if not collect() then
  168. returnSupplies()
  169. end
  170. end
  171. end
  172. end
  173.  
  174. local function goDown()
  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. else
  190. sleep( 0.5 )
  191. end
  192. end
  193.  
  194. depth = depth + 1
  195. if math.fmod( depth, 10 ) == 0 then
  196. print( "Descended "..depth.." metres." )
  197. end
  198.  
  199. return true
  200. end
  201.  
  202. local function turnLeft()
  203. turtle.turnLeft()
  204. xDir, zDir = -zDir, xDir
  205. end
  206.  
  207. local function turnRight()
  208. turtle.turnRight()
  209. xDir, zDir = zDir, -xDir
  210. end
  211.  
  212. function goTo( x, y, z, xd, zd )
  213. while depth > y do
  214. if turtle.up() then
  215. depth = depth - 1
  216. elseif turtle.digUp() then
  217. collect()
  218. else
  219. sleep( 0.5 )
  220. end
  221. end
  222.  
  223. if xPos > x then
  224. while xDir ~= -1 do
  225. turnLeft()
  226. end
  227. while xPos > x do
  228. if turtle.forward() then
  229. xPos = xPos - 1
  230. elseif turtle.dig() then
  231. collect()
  232. else
  233. sleep( 0.5 )
  234. end
  235. end
  236. elseif xPos < x then
  237. while xDir ~= 1 do
  238. turnLeft()
  239. end
  240. while xPos < x do
  241. if turtle.forward() then
  242. xPos = xPos + 1
  243. elseif turtle.dig() then
  244. collect()
  245. else
  246. sleep( 0.5 )
  247. end
  248. end
  249. end
  250.  
  251. if zPos > z then
  252. while zDir ~= -1 do
  253. turnLeft()
  254. end
  255. while zPos > z do
  256. if turtle.forward() then
  257. zPos = zPos - 1
  258. elseif turtle.dig() then
  259. collect()
  260. else
  261. sleep( 0.5 )
  262. end
  263. end
  264. elseif zPos < z then
  265. while zDir ~= 1 do
  266. turnLeft()
  267. end
  268. while zPos < z do
  269. if turtle.forward() then
  270. zPos = zPos + 1
  271. elseif turtle.dig() then
  272. collect()
  273. else
  274. sleep( 0.5 )
  275. end
  276. end
  277. end
  278.  
  279. while depth < y do
  280. if turtle.down() then
  281. depth = depth + 1
  282. elseif turtle.digDown() then
  283. collect()
  284. else
  285. sleep( 0.5 )
  286. end
  287. end
  288.  
  289. while zDir ~= zd or xDir ~= xd do
  290. turnLeft()
  291. end
  292. end
  293.  
  294. if not refuel() then
  295. print( "Out of Fuel" )
  296. return
  297. end
  298.  
  299. print( "Excavating..." )
  300.  
  301. local reseal = false
  302. turtle.select(1)
  303. if turtle.digDown() then
  304. reseal = true
  305. end
  306.  
  307. local alternate = 0
  308. local done = false
  309. while not done do
  310. for n=1,size do
  311. for m=1,size-1 do
  312. if not tryForwards() then
  313. done = true
  314. break
  315. end
  316.  
  317. tryUp()
  318.  
  319. tryDown()
  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.  
  332. tryUp()
  333.  
  334. tryDown()
  335.  
  336. turnLeft()
  337. else
  338. turnRight()
  339. if not tryForwards() then
  340. done = true
  341. break
  342. end
  343.  
  344. tryUp()
  345.  
  346. tryDown()
  347.  
  348. turnRight()
  349. end
  350. end
  351. end
  352. if done then
  353. break
  354. end
  355.  
  356. if size > 1 then
  357. if math.fmod(size,2) == 0 then
  358. turnRight()
  359. else
  360. if alternate == 0 then
  361. turnLeft()
  362. else
  363. turnRight()
  364. end
  365. alternate = 1 - alternate
  366. end
  367. end
  368.  
  369. for i=1,3 do
  370. tryUp()
  371.  
  372. if not goDown() then
  373. done = true
  374. break
  375. end
  376.  
  377. tryDown()
  378. end
  379. end
  380.  
  381. print( "Returning to surface..." )
  382.  
  383. -- Return to where we started
  384. goTo( 0,0,0,0,-1 )
  385. unload( false )
  386. goTo( 0,0,0,0,1 )
  387.  
  388. -- Seal the hole
  389. if reseal then
  390. turtle.placeDown()
  391. end
  392.  
  393. print( "Mined "..(collected + unloaded).." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement