Advertisement
Guest User

Excavate+

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