Guest User

Untitled

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