Advertisement
antisocialian

Excavate+ by Yggdresil

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