Advertisement
Dynamic225

resumeQuarry

Aug 18th, 2020
1,765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.32 KB | None | 0 0
  1. -- Usage (program name) <Width> <Depth> <Length> <Direction> <Start Width> <Start Depth> <Start Length>
  2. -- Place a chest with enough space for what you are digging behind the turtle before beginning
  3. -- Put any fuel in the first slot, if you don't want to have to refuel, use refuel<amount> before running this
  4. -- Make sure you keep the chunk(s) the turtle is digging in loaded, otherwise it will break
  5.  
  6.  
  7. local tArgs = { ... }
  8. if #tArgs ~= 7 then
  9. print( "Usage: excavate <Width> <Depth> <Length> <Direction> <Start Width> <Start Depth> <Start Length>" )
  10. print( "Direction - 0 for down, 1 for up" )
  11. return
  12. end
  13.  
  14.  
  15. local size1 = tonumber( tArgs[1] )
  16. local size2 = tonumber( tArgs[2] )
  17. local size3 = tonumber( tArgs[3] )
  18. local upordown = tonumber( tArgs[4] )
  19.  
  20. local startWidth = tonumber(tArgs[5])-1
  21. local startDepth = tonumber(tArgs[6])-1
  22. local startLength = tonumber(tArgs[7])-1
  23. if size1 < 1 then
  24. print( "X size must be positive" )
  25. return
  26. end
  27. if size2 < 1 then
  28. print( "Depth must be positive" )
  29. return
  30. end
  31. if size3 < 1 then
  32. print( "Z size must be positive" )
  33. return
  34. end
  35. if upordown~=0 and upordown~=1 then
  36. print( "Invalid direction" )
  37. return
  38. end
  39. if startWidth < 0 then
  40. print( "Start Width must be positive" )
  41. return
  42. end
  43. if startDepth < 0 then
  44. print( "Start Depth must be positive" )
  45. return
  46. end
  47. if startLength < 0 then
  48. print( "Start Length must be positive" )
  49. return
  50. end
  51. if startWidth >= size1 then
  52. print( "Start Width must not be greater than quarry width" )
  53. return
  54. end
  55. if startDepth >= size2 then
  56. print( "Start Depth must not be greater than quarry depth" )
  57. return
  58. end
  59. if startLength >= size3 then
  60. print( "Start Length must not be greater than quarry length" )
  61. return
  62. end
  63.  
  64. local depth = 0
  65. local unloaded = 0
  66. local collected = 0
  67.  
  68. local xPos,zPos = 0,0
  69. local xDir,zDir = 0,1
  70.  
  71. local goTo -- Filled in further down
  72. local refuel -- Filled in further down
  73.  
  74. local function unload()
  75. print( "Unloading items..." )
  76. for n=2,16 do
  77. unloaded = unloaded + turtle.getItemCount(n)
  78. turtle.select(n)
  79. turtle.drop()
  80. end
  81. collected = 0
  82. turtle.select(1)
  83. end
  84.  
  85. local function returnSupplies()
  86. local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  87. print( "Returning to surface..." )
  88. goTo( 0,0,0,0,-1 )
  89.  
  90. local fuelNeeded = x+y+z + x+y+z + 1
  91. if not refuel( fuelNeeded ) then
  92. unload()
  93. print( "Waiting for fuel" )
  94. while not refuel( fuelNeeded ) do
  95. sleep(1)
  96. end
  97. else
  98. unload()
  99. end
  100.  
  101. print( "Resuming mining..." )
  102. goTo( x,y,z,xd,zd )
  103. end
  104.  
  105. local function collect()
  106. local bFull = true
  107. local nTotalItems = 0
  108. for n=1,16 do
  109. local nCount = turtle.getItemCount(n)
  110. if nCount == 0 then
  111. bFull = false
  112. end
  113. nTotalItems = nTotalItems + nCount
  114. end
  115.  
  116. if nTotalItems > collected then
  117. collected = nTotalItems
  118. if math.fmod(collected + unloaded, 50) == 0 then
  119. print( "Mined "..(collected + unloaded).." items." )
  120. end
  121. end
  122.  
  123. if bFull then
  124. print( "No empty slots left." )
  125. return false
  126. end
  127. return true
  128. end
  129.  
  130. function refuel( ammount )
  131. local fuelLevel = turtle.getFuelLevel()
  132. if fuelLevel == "unlimited" then
  133. return true
  134. end
  135.  
  136. local needed = ammount or (xPos + zPos + depth + 1)
  137. if turtle.getFuelLevel() < needed then
  138. local fueled = false
  139. for n=1,16 do
  140. if turtle.getItemCount(n) > 0 then
  141. turtle.select(n)
  142. if turtle.refuel(1) then
  143. while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  144. turtle.refuel(1)
  145. end
  146. if turtle.getFuelLevel() >= needed then
  147. turtle.select(1)
  148. return true
  149. end
  150. end
  151. end
  152. end
  153. turtle.select(1)
  154. return false
  155. end
  156.  
  157. return true
  158. end
  159.  
  160. local function tryForwards()
  161. if not refuel() then
  162. print( "Not enough Fuel" )
  163. returnSupplies()
  164. end
  165.  
  166. while not turtle.forward() do
  167. if turtle.detect() then
  168. if turtle.dig() then
  169. if not collect() then
  170. returnSupplies()
  171. end
  172. else
  173. return false
  174. end
  175. elseif turtle.attack() then
  176. if not collect() then
  177. returnSupplies()
  178. end
  179. else
  180. sleep( 0.5 )
  181. end
  182. end
  183.  
  184. xPos = xPos + xDir
  185. zPos = zPos + zDir
  186. return true
  187. end
  188.  
  189. local function tryDown()
  190. if not refuel() then
  191. print( "Not enough Fuel" )
  192. returnSupplies()
  193. end
  194.  
  195. while not turtle.down() do
  196. if turtle.detectDown() then
  197. if turtle.digDown() then
  198. if not collect() then
  199. returnSupplies()
  200. end
  201. else
  202. return false
  203. end
  204. elseif turtle.attackDown() then
  205. if not collect() then
  206. returnSupplies()
  207. end
  208. else
  209. sleep( 0.5 )
  210. end
  211. end
  212.  
  213. depth = depth + 1
  214.  
  215. return true
  216. end
  217.  
  218. local function tryUp()
  219. if not refuel() then
  220. print( "Not enough Fuel" )
  221. returnSupplies()
  222. end
  223.  
  224. while not turtle.up() do
  225. if turtle.detectUp() then
  226. if turtle.digUp() then
  227. if not collect() then
  228. returnSupplies()
  229. end
  230. else
  231. return false
  232. end
  233. elseif turtle.attackUp() then
  234. if not collect() then
  235. returnSupplies()
  236. end
  237. else
  238. sleep( 0.5 )
  239. end
  240. end
  241.  
  242. depth = depth - 1
  243.  
  244. return true
  245. end
  246.  
  247. local function turnLeft()
  248. turtle.turnLeft()
  249. xDir, zDir = -zDir, xDir
  250. end
  251.  
  252. local function turnRight()
  253. turtle.turnRight()
  254. xDir, zDir = zDir, -xDir
  255. end
  256.  
  257. function goTo( x, y, z, xd, zd )
  258. while depth > y do
  259. if turtle.up() then
  260. depth = depth - 1
  261. elseif turtle.digUp() or turtle.attackUp() then
  262. collect()
  263. else
  264. sleep( 0.5 )
  265. end
  266. end
  267.  
  268. if xPos > x then
  269. while xDir ~= -1 do
  270. turnLeft()
  271. end
  272. while xPos > x do
  273. if turtle.forward() then
  274. xPos = xPos - 1
  275. elseif turtle.dig() or turtle.attack() then
  276. collect()
  277. else
  278. sleep( 0.5 )
  279. end
  280. end
  281. elseif xPos < x then
  282. while xDir ~= 1 do
  283. turnLeft()
  284. end
  285. while xPos < x do
  286. if turtle.forward() then
  287. xPos = xPos + 1
  288. elseif turtle.dig() or turtle.attack() then
  289. collect()
  290. else
  291. sleep( 0.5 )
  292. end
  293. end
  294. end
  295.  
  296. if zPos > z then
  297. while zDir ~= -1 do
  298. turnLeft()
  299. end
  300. while zPos > z do
  301. if turtle.forward() then
  302. zPos = zPos - 1
  303. elseif turtle.dig() or turtle.attack() then
  304. collect()
  305. else
  306. sleep( 0.5 )
  307. end
  308. end
  309. elseif zPos < z then
  310. while zDir ~= 1 do
  311. turnLeft()
  312. end
  313. while zPos < z do
  314. if turtle.forward() then
  315. zPos = zPos + 1
  316. elseif turtle.dig() or turtle.attack() then
  317. collect()
  318. else
  319. sleep( 0.5 )
  320. end
  321. end
  322. end
  323.  
  324. while depth < y do
  325. if turtle.down() then
  326. depth = depth + 1
  327. elseif turtle.digDown() or turtle.attackDown() then
  328. collect()
  329. else
  330. sleep( 0.5 )
  331. end
  332. end
  333.  
  334. while zDir ~= zd or xDir ~= xd do
  335. turnLeft()
  336. end
  337. end
  338.  
  339. if not refuel() then
  340. print( "Out of Fuel" )
  341. return
  342. end
  343.  
  344. print( "Excavating..." )
  345.  
  346. local reseal = false
  347. turtle.select(1)
  348. if upordown == 0 then
  349. if turtle.digDown() then
  350. end
  351. elseif upordown == 1 then
  352. if turtle.digUp() then
  353. end
  354. end
  355.  
  356. local startDir = (-1)^(startWidth%2)
  357.  
  358. goTo(startWidth, startDepth, startLength, 0, startDir)
  359.  
  360. startLength = (startDir == -1) and math.abs((size3-1) - startLength) or startLength
  361. local xResumed = false
  362. local zResumed = false
  363.  
  364. local alternate = 0
  365. local done = startDepth
  366. while done~=size2 do
  367. for varx=1,size1 do
  368. if xResumed or varx-1 == startWidth then
  369. xResumed = true
  370. for varz=1,size3-1 do
  371. if zResumed or varz-1 == startLength then
  372. zResumed = true
  373. if not tryForwards() then
  374. done = size2
  375. break
  376. end
  377. end
  378. end
  379. zResumed = true
  380. if done==size2 then
  381. break
  382. end
  383. if varx<size1 then
  384. if math.fmod(varx + alternate,2) == 0 then
  385. turnLeft()
  386. if not tryForwards() then
  387. done = size2
  388. break
  389. end
  390. turnLeft()
  391. else
  392. turnRight()
  393. if not tryForwards() then
  394. done = size2
  395. break
  396. end
  397. turnRight()
  398. end
  399. end
  400. end
  401. end
  402. if done==size2 then
  403. break
  404. end
  405.  
  406. if size1 > 1 then
  407. goTo( 0,depth,0,0,1 )
  408. end
  409. if done ~= size2-1 then
  410. if upordown == 0 then
  411. if not tryDown() then
  412. done = size2
  413. break
  414. end
  415. elseif upordown == 1 then
  416. if not tryUp() then
  417. done = size2
  418. break
  419. end
  420. end
  421. end
  422. done = done + 1
  423. end
  424.  
  425. print( "Returning to surface..." )
  426.  
  427. -- Return to where we started
  428. goTo( 0,0,0,0,-1 )
  429. unload()
  430. goTo( 0,0,0,0,1 )
  431.  
  432. -- Seal the hole
  433. if reseal then
  434. turtle.placeDown()
  435. end
  436.  
  437. print( "Mined "..(collected + unloaded).." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement