Guest User

EXCAVATE+

a guest
Oct 21st, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.75 KB | None | 0 0
  1. local tArgs = { ... }
  2. local excludeItems = {}
  3. if #tArgs < 1 then
  4. print("Usage: excavate <diameter> [<excluded items>]")
  5. return
  6. elseif #tArgs > 1 then
  7. if tArgs[2] == "None" then
  8. excludeItems = nil
  9. else
  10. for i = 2, #tArgs do
  11. excludeItems[tArgs[i]] = true
  12. end
  13. end
  14. else
  15. print('Using default exclude list (Cobblestone, Dirt, Granite, Gravel)...')
  16. excludeItems = {
  17. ["minecraft:cobblestone"] = true,
  18. ["minecraft:dirt"] = true,
  19. ["chisel:granite"] = true,
  20. ["minecraft:gravel"] = true
  21. }
  22. end
  23.  
  24. -- Mine in a quarry pattern until we hit something we can't dig
  25. local size = tonumber(tArgs[1])
  26. if size < 1 then
  27. print("Excavate diameter must be positive")
  28. return
  29. end
  30.  
  31. local depth = 0
  32. local unloaded = 0
  33. local collected = 0
  34.  
  35. local xPos, zPos = 0, 0
  36. local xDir, zDir = 0, 1
  37.  
  38. local goTo -- Filled in further down
  39. local refuel -- Filled in further down
  40.  
  41. -- Empty inventory and update count of total items
  42. local function unload(_bKeepOneFuelStack)
  43. print("Unloading items...")
  44. for n = 1, 16 do
  45. local nCount = turtle.getItemCount(n)
  46. if nCount > 0 then
  47. turtle.select(n)
  48. if _bKeepOneFuelStack and turtle.refuel(0) then
  49. _bKeepOneFuelStack = false
  50. else
  51. turtle.drop()
  52. unloaded = unloaded + nCount
  53. end
  54. end
  55. end
  56. collected = 0
  57. turtle.select(1)
  58. end
  59.  
  60. -- Return to surface, call unload and refuel if necessary before returning to previous position
  61. local function returnSupplies()
  62. local x, y, z, xd, zd = xPos, depth, zPos, xDir, zDir
  63. print("Returning to surface...")
  64. goTo(0, 0, 0, 0, -1)
  65.  
  66. local fuelNeeded = 2 * (x + y + z + 1)
  67. if not refuel(fuelNeeded) then
  68. unload(true)
  69. print("Waiting for fuel")
  70. while not refuel(fuelNeeded) do
  71. os.pullEvent("turtle_inventory")
  72. end
  73. else
  74. unload(true)
  75. end
  76.  
  77. print("Resuming mining...")
  78. goTo(x, y, z, xd, zd)
  79. end
  80.  
  81. -- Check if inventory full, update count of collected items
  82. local function collect()
  83. local bFull = true
  84. local nTotalItems = 0
  85. for n = 1, 16 do
  86. local nCount = turtle.getItemCount(n)
  87. if nCount == 0 then
  88. bFull = false
  89. elseif excludeItems[turtle.getItemDetail(n).name] then
  90. local nPreviousSlot = turtle.getSelectedSlot()
  91. turtle.select(n)
  92. turtle.drop()
  93. turtle.select(nPreviousSlot)
  94. bFull = false
  95. nCount = 0
  96. end
  97.  
  98. nTotalItems = nTotalItems + nCount
  99. end
  100.  
  101. if nTotalItems > collected then
  102. collected = nTotalItems
  103. if math.fmod(collected + unloaded, 50) == 0 then
  104. print("Mined " .. (collected + unloaded) .. " items.")
  105. end
  106. end
  107.  
  108. if bFull then
  109. print("No empty slots left.")
  110. return false
  111. end
  112. return true
  113. end
  114.  
  115. -- Check fuel level, refill if necessary and possible
  116. function refuel(amount)
  117. local fuelLevel = turtle.getFuelLevel()
  118. if fuelLevel == "unlimited" then
  119. return true
  120. end
  121.  
  122. local needed = amount or (xPos + zPos + depth + 2) -- +2 = move 1 and back
  123. if fuelLevel < needed then
  124. for n = 1, 16 do
  125. if turtle.getItemCount(n) > 0 then
  126. turtle.select(n)
  127. if turtle.refuel(1) then
  128. while turtle.getItemCount() > 0 and turtle.getFuelLevel() < needed do
  129. turtle.refuel(1)
  130. end
  131. if turtle.getFuelLevel() >= needed then
  132. turtle.select(1)
  133. return true
  134. end
  135. end
  136. end
  137. end
  138. turtle.select(1)
  139. return false
  140. end
  141.  
  142. return true
  143. end
  144.  
  145. -- Attempt to dig/attack in front of the turtle
  146. local function dig()
  147. if turtle.detect() then
  148. if turtle.dig() then
  149. if not collect() then
  150. returnSupplies()
  151. end
  152. else
  153. return false
  154. end
  155. elseif turtle.attack() then
  156. if not collect() then
  157. returnSupplies()
  158. end
  159. else
  160. sleep(0.5)
  161. end
  162.  
  163. return true
  164. end
  165.  
  166. -- Attempt to dig/attack below the turtle
  167. local function digDown()
  168. if turtle.detectDown() then
  169. if turtle.digDown() then
  170. if not collect() then
  171. returnSupplies()
  172. end
  173. else
  174. return false
  175. end
  176. elseif turtle.attackDown() then
  177. if not collect() then
  178. returnSupplies()
  179. end
  180. else
  181. sleep(0.5)
  182. end
  183. return true
  184. end
  185.  
  186. -- Attempt to dig/attack above the turtle
  187. local function digUp()
  188. if turtle.detectUp() then
  189. if turtle.digUp() then
  190. if not collect() then
  191. returnSupplies()
  192. end
  193. else
  194. return false
  195. end
  196. elseif turtle.attackUp() then
  197. if not collect() then
  198. returnSupplies()
  199. end
  200. else
  201. sleep(0.5)
  202. end
  203.  
  204. return true
  205. end
  206.  
  207. -- Try moving the turtle forwards, digging above and below the turtle prior to moving
  208. local function tryForwards()
  209. if not refuel() then
  210. print("Not enough Fuel")
  211. returnSupplies()
  212. end
  213.  
  214. while not turtle.forward() do
  215. if not dig() then
  216. return false
  217. end
  218. end
  219.  
  220. if depth ~= 0 then
  221. digUp()
  222. end
  223. digDown()
  224.  
  225. xPos = xPos + xDir
  226. zPos = zPos + zDir
  227. return true
  228. end
  229.  
  230. -- Self explanatory
  231. local function tryDown()
  232. if not refuel() then
  233. print("Not enough Fuel")
  234. returnSupplies()
  235. end
  236.  
  237. while not turtle.down() do
  238. if not digDown() then
  239. return false
  240. end
  241. end
  242.  
  243. depth = depth + 1
  244. if math.fmod(depth, 10) == 0 then
  245. print("Descended " .. depth .. " metres.")
  246. end
  247.  
  248. return true
  249. end
  250.  
  251. -- Self explanatory
  252. local function turnLeft()
  253. turtle.turnLeft()
  254. xDir, zDir = -zDir, xDir
  255. end
  256.  
  257. -- Self explanatory
  258. local function turnRight()
  259. turtle.turnRight()
  260. xDir, zDir = zDir, -xDir
  261. end
  262.  
  263. -- Go to coordinates x,y,z facing the direction indicated by xd or xz
  264. -- Coordinates are in a Cartesian coordinate system where the turtle's starting point is the origin
  265. function goTo(x, y, z, xd, zd)
  266. while depth > y do
  267. if turtle.up() then
  268. depth = depth - 1
  269. else
  270. digUp()
  271. end
  272. end
  273.  
  274. if xPos > x then
  275. while xDir ~= -1 do
  276. if zDir == -1 then
  277. turnRight()
  278. else
  279. turnLeft()
  280. end
  281. end
  282. while xPos > x do
  283. tryForwards()
  284. end
  285. elseif xPos < x then
  286. while xDir ~= 1 do
  287. if zDir == 1 then
  288. turnRight()
  289. else
  290. turnLeft()
  291. end
  292. end
  293. while xPos < x do
  294. tryForwards()
  295. end
  296. end
  297.  
  298. if zPos > z then
  299. while zDir ~= -1 do
  300. turnLeft()
  301. end
  302. while zPos > z do
  303. tryForwards()
  304. end
  305. elseif zPos < z then
  306. while zDir ~= 1 do
  307. turnLeft()
  308. end
  309. while zPos < z do
  310. tryForwards()
  311. end
  312. end
  313.  
  314. while depth < y do
  315. if turtle.down() then
  316. depth = depth + 1
  317. else
  318. digDown()
  319. end
  320. end
  321.  
  322. -- TODO: Optimize to avoid useless turns
  323. while zDir ~= zd or xDir ~= xd do
  324. turnLeft()
  325. end
  326. end
  327.  
  328. if not refuel() then
  329. print("Out of Fuel")
  330. while not refuel() do
  331. os.pullEvent("turtle_inventory")
  332. end
  333. end
  334.  
  335. print("Excavating...")
  336.  
  337. local reseal = false
  338. turtle.select(1)
  339. if turtle.digDown() then
  340. reseal = true
  341. end
  342.  
  343. local alternate = 0
  344. local done = false
  345.  
  346. -- Blocks above the turtle will also be dug out so begin one block lower
  347. if not tryDown() then
  348. done = true
  349. end
  350. -- Additional blocks are dug when calling tryForwards so for this instance, dig the bottom block manually
  351. digDown()
  352. while not done do
  353. for n = 1, size do
  354. for m = 1, size - 1 do
  355. if not tryForwards() then
  356. done = true
  357. break
  358. end
  359. end
  360. if done then
  361. break
  362. end
  363. if n < size then
  364. if math.fmod(n + alternate, 2) == 0 then
  365. turnLeft()
  366. if not tryForwards() then
  367. done = true
  368. break
  369. end
  370. turnLeft()
  371. else
  372. turnRight()
  373. if not tryForwards() then
  374. done = true
  375. break
  376. end
  377. turnRight()
  378. end
  379. end
  380. end
  381. if done then
  382. break
  383. end
  384.  
  385. if size > 1 then
  386. if math.fmod(size, 2) == 0 then
  387. turnRight()
  388. else
  389. if alternate == 0 then
  390. turnLeft()
  391. else
  392. turnRight()
  393. end
  394. alternate = 1 - alternate
  395. end
  396. end
  397.  
  398. for n = 1, 3 do
  399. if not tryDown() then
  400. done = true
  401. break
  402. end
  403. end
  404. -- Additional blocks are dug when calling tryForwards so for this instance, dig the bottom block manually
  405. digDown()
  406. end
  407.  
  408. print("Returning to surface...")
  409.  
  410. -- Return to where we started
  411. goTo(0, 0, 0, 0, -1)
  412. unload(false)
  413. goTo(0, 0, 0, 0, 1)
  414.  
  415. -- Seal the hole
  416. if reseal then
  417. turtle.placeDown()
  418. end
  419.  
  420. print("Mined " .. (collected + unloaded) .. " items total.")
Add Comment
Please, Sign In to add comment