Lanzr

custom dig

Nov 10th, 2023 (edited)
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.44 KB | None | 0 0
  1. if not turtle then
  2. printError("Requires a Turtle")
  3. return
  4. end
  5.  
  6. local tArgs = { ... }
  7. -- if #tArgs ~= 1 then
  8. -- local programName = arg[0] or fs.getName(shell.getRunningProgram())
  9. -- print("Usage: " .. programName .. " <diameter>")
  10. -- return
  11. -- end
  12. local length = tonumber(arg[1])
  13. local height = tonumber(arg[2])
  14. local depth = tonumber(arg[3])
  15. -- Mine in a quarry pattern until we hit something we can't dig
  16. local size = tonumber(tArgs[1])
  17. if size < 1 then
  18. print("Excavate diameter must be positive")
  19. return
  20. end
  21.  
  22. local depth = 0
  23. local unloaded = 0
  24. local collected = 0
  25.  
  26. local xPos, zPos = 0, 0
  27. local xDir, zDir = 0, 1
  28.  
  29. local goTo -- Filled in further down
  30. local refuel -- Filled in further down
  31.  
  32. local function unload(_bKeepOneFuelStack)
  33. print("Unloading items...")
  34. for n = 1, 16 do
  35. local nCount = turtle.getItemCount(n)
  36. if nCount > 0 then
  37. turtle.select(n)
  38. local bDrop = true
  39. if _bKeepOneFuelStack and turtle.refuel(0) then
  40. bDrop = false
  41. _bKeepOneFuelStack = false
  42. end
  43. if bDrop then
  44. turtle.drop()
  45. unloaded = unloaded + nCount
  46. end
  47. end
  48. end
  49. collected = 0
  50. turtle.select(1)
  51. end
  52.  
  53. local function returnSupplies()
  54. local x, y, z, xd, zd = xPos, depth, zPos, xDir, zDir
  55. print("Returning to surface...")
  56. goTo(0, 0, 0, 0, -1)
  57.  
  58. local fuelNeeded = 2 * (x + y + z) + 1
  59. if not refuel(fuelNeeded) then
  60. unload(true)
  61. print("Waiting for fuel")
  62. while not refuel(fuelNeeded) do
  63. os.pullEvent("turtle_inventory")
  64. end
  65. else
  66. unload(true)
  67. end
  68.  
  69. print("Resuming mining...")
  70. goTo(x, y, z, xd, zd)
  71. end
  72.  
  73. local function collect()
  74. local bFull = true
  75. local nTotalItems = 0
  76. for n = 1, 16 do
  77. local nCount = turtle.getItemCount(n)
  78. if nCount == 0 then
  79. bFull = false
  80. end
  81. nTotalItems = nTotalItems + nCount
  82. end
  83.  
  84. if nTotalItems > collected then
  85. collected = nTotalItems
  86. if math.fmod(collected + unloaded, 50) == 0 then
  87. print("Mined " .. collected + unloaded .. " items.")
  88. end
  89. end
  90.  
  91. if bFull then
  92. print("No empty slots left.")
  93. return false
  94. end
  95. return true
  96. end
  97.  
  98. function refuel(amount)
  99. local fuelLevel = turtle.getFuelLevel()
  100. if fuelLevel == "unlimited" then
  101. return true
  102. end
  103.  
  104. local needed = amount or xPos + zPos + depth + 2
  105. if turtle.getFuelLevel() < needed then
  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. local turnFlag = true
  293. local alternate = 0
  294. local done = false
  295. while not done do
  296. for n = 1, length do
  297. for _ = 1, height - 1 do
  298. if not tryForwards() then
  299. done = true
  300. -- cant forward
  301. break
  302. end
  303. end
  304. if done then
  305. break
  306. end
  307. if n < length then
  308. if turnFlag then
  309. turnLeft()
  310. if not tryForwards() then
  311. done = true
  312. break
  313. end
  314. turnLeft()
  315. else
  316. turnRight()
  317. if not tryForwards() then
  318. done = true
  319. break
  320. end
  321. turnRight()
  322. end
  323. turnFlag = not turnFlag
  324. end
  325. end
  326. if done then
  327. break
  328. end
  329. -- width loop
  330. -- if height > 1 then
  331. -- if turnFlag == 0 then
  332. -- turnRight()
  333. -- else
  334. -- if alternate == 0 then
  335. -- turnLeft()
  336. -- else
  337. -- turnRight()
  338. -- end
  339. -- alternate = 1 - alternate
  340. -- end
  341. -- turnFlag = not turnFlag
  342. -- end
  343.  
  344. if not tryDown() then
  345. done = true
  346. break
  347. end
  348. turnFlag = not turnFlag
  349. end
  350.  
  351. print("Returning to surface...")
  352.  
  353. -- Return to where we started
  354. goTo(0, 0, 0, 0, -1)
  355. unload(false)
  356. goTo(0, 0, 0, 0, 1)
  357.  
  358. -- Seal the hole
  359. if reseal then
  360. turtle.placeDown()
  361. end
  362.  
  363. print("Mined " .. collected + unloaded .. " items total.")
Add Comment
Please, Sign In to add comment