Advertisement
HarvDad

pit

Jun 5th, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.29 KB | None | 0 0
  1. -- pit
  2. -- Mines pit to specified depth or bedrock
  3. -- Turtle returns to its starting point when mission is completed or fuel runs low
  4. -- Written by HarvDad, June 2014
  5.  
  6. args = {...}
  7. nArgs = #args
  8.  
  9. version = "pit: Rev 2.4"
  10. mission = "Mines pit to specified length, width and depth or bedrock."
  11.  
  12. usage = "walls <length> <width> <depth>"
  13.  
  14. x = 0
  15. y = 0
  16. z = 0
  17. orgX = 0
  18. orgY = 0
  19. orgZ = 0
  20. orgLength = 0
  21. orgWidth = 0
  22. face = 0
  23. minimumFuel = 100
  24. missionMessage = "Mission complete."
  25. abort = false
  26. startLevel = 0
  27. local currentFuelLevel = turtle.getFuelLevel()
  28. targetArea = 0
  29. areaMined = 0
  30. bedrockReached = false
  31. lowestLevelReached = false
  32. lowestLevelMined = 0
  33. finalApproach = false
  34. liquidCaution = true
  35.  
  36. areaCovered = 0
  37.  
  38. if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
  39. print(version)
  40. print(mission)
  41. print(usage)
  42. return
  43. end
  44.  
  45. if nArgs ~= 3 then
  46. print(usage)
  47. return
  48. end
  49.  
  50. length = tonumber(args[1])
  51. if length == nil then
  52. print("\"", args[1], "\" is not a valid length")
  53. return
  54. end
  55. if length < 1 then
  56. print("length must be a positive number greater than zero")
  57. end
  58. orgLength = length
  59.  
  60. width = tonumber(args[2])
  61. if width == nil then
  62. print("\"", args[2], "\" is not a valid width")
  63. return
  64. end
  65. if width < 1 then
  66. print("width must be a positive number greater than zero")
  67. end
  68. orgWidth = width
  69.  
  70. targetArea = length * width
  71.  
  72. depth = tonumber(args[3])
  73. if startLevel == nil then
  74. print("\"", args[3], "\" is not a valid depth")
  75. return
  76. end
  77.  
  78. currentSlot = 2
  79. materialSlot = 1
  80. maxSlot = 16
  81. fillerSlot = 2
  82.  
  83. nextTurn = "left"
  84.  
  85. local clock = os.clock
  86. function sleep(n) -- seconds
  87. local t0 = clock()
  88. while clock() - t0 <= n do end
  89. end
  90.  
  91. function left()
  92. if face == 0 then face = 1 turtle.turnLeft() return end
  93. if face == 1 then face = 2 turtle.turnLeft() return end
  94. if face == 2 then face = 3 turtle.turnLeft() return end
  95. if face == 3 then face = 0 turtle.turnLeft() return end
  96. print("function left\(\): Bad face value: ", face)
  97. end
  98.  
  99. function right()
  100. if face == 0 then face = 3 turtle.turnRight() return end
  101. if face == 1 then face = 0 turtle.turnRight() return end
  102. if face == 2 then face = 1 turtle.turnRight() return end
  103. if face == 3 then face = 2 turtle.turnRight() return end
  104. print("function right\(\): Bad face value: ", face)
  105. end
  106.  
  107. function up()
  108. while turtle.detectUp() do -- This loop added in case of falling sand or whatever
  109. turtle.digUp()
  110. end
  111.  
  112. turtle.up()
  113. y = y+1
  114. end
  115.  
  116. function rise(nBlocks)
  117. local i
  118. for i=1,nBlocks do
  119. up()
  120. end
  121. end
  122.  
  123. function down()
  124. if turtle.detectDown() then
  125. if not turtle.digDown() then
  126. return -- must be at bedrock
  127. end
  128. end
  129. turtle.down()
  130. y = y-1
  131. -- print("down: y is now ", y)
  132. end
  133.  
  134. function turn()
  135. if nextTurn == "left" then
  136. left()
  137. else
  138. right()
  139. end
  140. chomp()
  141. forward()
  142. if nextTurn == "left" then
  143. left()
  144. nextTurn = "right"
  145. else
  146. right()
  147. nextTurn = "left"
  148. end
  149. areaCovered = areaCovered + 1
  150. end
  151.  
  152. function forward()
  153. if liquidCaution and (not turtle.detect()) then
  154. turtle.select(fillerSlot)
  155. turtle.place()
  156. end
  157. while turtle.detect() do -- This loop added in case of falling sand or whatever
  158. turtle.dig()
  159. end
  160. for i=1,10 do -- This loop trys to handle pests (mob) that might be in the way
  161. if turtle.forward() then
  162. break
  163. end
  164. turtle.attack()
  165. sleep(2)
  166. end
  167. areaCovered = areaCovered + 1
  168.  
  169. if face == 0 then z = z+1 return end
  170. if face == 1 then x = x-1 return end
  171. if face == 2 then z = z-1 return end
  172. if face == 3 then x = x+1 return end
  173. end
  174.  
  175. function setFace(f)
  176. if f == 0 then
  177. if face == 0 then return end
  178. if face == 1 then right() return end
  179. if face == 2 then right() right() return end
  180. if face == 3 then left() return end
  181. end
  182.  
  183. if f == 1 then
  184. if face == 0 then left() return end
  185. if face == 1 then return end
  186. if face == 2 then right() return end
  187. if face == 3 then right() right() return end
  188. end
  189.  
  190. if f == 2 then
  191. if face == 0 then left() left() return end
  192. if face == 1 then left() return end
  193. if face == 2 then return end
  194. if face == 3 then right() return end
  195. end
  196.  
  197. if f == 3 then
  198. if face == 0 then right() return end
  199. if face == 1 then left() left() return end
  200. if face == 2 then left() return end
  201. if face == 3 then return end
  202. end
  203. end
  204.  
  205. function home(targetY)
  206. -- print("home:face ", face, ", x = ", x, ", z = ", z)
  207. if finalApproach then print("Final approach to home...") end
  208.  
  209. liquidCaution = false -- No need to watch for liquid blocks
  210. if x < 0 then
  211. setFace(3)
  212. while x < 0 do
  213. forward()
  214. end
  215. else
  216. if x > 0 then
  217. setFace(1)
  218. while x > 0 do
  219. forward()
  220. end
  221. end
  222. end
  223.  
  224. if z < 0 then
  225. setFace(0)
  226. while z < 0 do
  227. forward()
  228. end
  229. else
  230. if z > 0 then
  231. setFace(2)
  232. while z > 0 do
  233. forward()
  234. end
  235. end
  236. end
  237.  
  238. if finalApproach then print(" y = ", y, ", lowestLevel = ", lowestLevel) end
  239. if targetY == lowestLevel then
  240. setFace(0)
  241. liquidCaution = true
  242. return
  243. end
  244.  
  245. if finalApproach then print(" y = ", y, ", targetY = ", targetY) end
  246. if y < targetY then
  247. if finalApproach then print(" Heading up..." ) end
  248. while y < targetY do
  249. if finalApproach then print(" up..." ) end
  250. up()
  251. end
  252. end
  253.  
  254. setFace(0)
  255. liquidCaution = true
  256. end
  257.  
  258. function isKeeper()
  259. for i=1,4 do
  260. if turtle.compareTo(i) then
  261. return true
  262. end
  263. end
  264. return false
  265. end
  266.  
  267. function isDisposable()
  268. for i=1,4 do
  269. if turtle.compareTo(i) then
  270. return true
  271. end
  272. end
  273. return false
  274. end
  275.  
  276. function purge()
  277. --[[
  278. for i=1,4 do
  279. turtle.select(i)
  280. if isDisposable() then
  281. count = turtle.getItemCount(i)
  282. turtle.dropUp(count - 1)
  283. end
  284. end
  285. --]]
  286. for i=5,16 do
  287. turtle.select(i)
  288. if isDisposable() then
  289. turtle.dropUp()
  290. end
  291. end
  292. turtle.select(1)
  293. end
  294.  
  295. function isFull()
  296. for i=5,16 do
  297. if turtle.getItemCount(i) == 0 then
  298. return false
  299. end
  300. end
  301. turtle.select(1)
  302. return true
  303. end
  304.  
  305. function emptySlots()
  306. empties = 0
  307.  
  308. for i=5,16 do
  309. if turtle.getItemCount(i) == 0 then
  310. empties = empties + 1
  311. end
  312. end
  313. return empties
  314. end
  315.  
  316. function ensureMaterial()
  317. if turtle.getItemCount(materialSlot) < 3 then
  318. organizeMaterial()
  319. end
  320. if turtle.getItemCount(materialSlot) < 3 then
  321. print("No more material")
  322. return false
  323. end
  324. return true
  325. end
  326.  
  327. function organizeMaterial()
  328. local i
  329. materialCount = turtle.getItemCount(materialSlot)
  330.  
  331. if materialCount < 3 then
  332. -- print("Attempting to refill slot ", materialSlot)
  333. for i=2,16 do
  334. turtle.select(i)
  335. if turtle.compareTo(materialSlot) then
  336. turtle.transferTo(materialSlot)
  337. end
  338. end
  339. end
  340. turtle.select(materialSlot)
  341. end
  342.  
  343. -- Group all materials into minimum slots
  344.  
  345. function consolidate()
  346. for dst = 5,16 do
  347. for src=(dst+1),16 do
  348. turtle.select(src)
  349. if turtle.getItemCount(src) > 0 then
  350. if turtle.compareTo(dst) then
  351. turtle.transferTo(dst)
  352. end
  353. end
  354. end
  355. end
  356. turtle.select(1)
  357. end
  358.  
  359.  
  360. function layBlock()
  361. turtle.select(materialSlot)
  362.  
  363. if startLevel > 2 then
  364. if turtle.detectUp() then
  365. if turtle.compareUp() then
  366. return
  367. else
  368. turtle.digUp()
  369. end
  370. end
  371. if ensureMaterial() then
  372. turtle.placeUp()
  373. end
  374. else
  375. if turtle.detectDown() then
  376. if turtle.compareDown() then
  377. return
  378. else
  379. turtle.digDown()
  380. end
  381. end
  382. if ensureMaterial() then
  383. turtle.placeDown()
  384. end
  385. end
  386. end
  387.  
  388. function checkFuel()
  389. if currentFuelLevel == "unlimited" then
  390. return true
  391. end
  392.  
  393. if currentFuelLevel < minimumFuel then
  394. if not turtle.refuel() then
  395. areaCovered = targetArea
  396. missionMessage = "Mission aborted due to low fuel."
  397. abort = true
  398. return false
  399. end
  400. end
  401. return true
  402. end
  403.  
  404. currentSlot = 1
  405.  
  406.  
  407. function tryToGoDown()
  408. success = false
  409.  
  410. if bedrockReached then
  411. -- print("tryToGoDown: No can do. Already at bedrock.")
  412. return success
  413. end
  414. if lowestLevelReached then
  415. -- print("tryToGoDown: Already at lowest level. Duh.")
  416. return success
  417. end
  418.  
  419. -- print("Trying to go down a level...")
  420. if y == lowestLevel then
  421. lowestLevelReached = true
  422. print(" but y is ", y, " and lowestLevel is ", lowestLevel)
  423. print("tryToGoDown: lowest level reached at ", y, "/", lowestLevel)
  424. return success
  425. end
  426.  
  427. if turtle.detectDown() then
  428. if not turtle.digDown() then
  429. bedrockReached = true
  430. print("tryToGoDown: bedrock reached at y = ", y)
  431. else
  432. turtle.down()
  433. success = true
  434. y = y - 1
  435. end
  436. else
  437. turtle.down()
  438. success = true
  439. y = y - 1
  440. end
  441.  
  442. return success
  443. end
  444.  
  445.  
  446. -- Try for 3 levels down, but 2 will be considered successful
  447.  
  448. function goToNextLevel()
  449. success = false
  450.  
  451. if tryToGoDown() == success then -- We need to go down at least 2 blocks
  452. if tryToGoDown() == success then -- 2nd level down counts as success
  453. success = true
  454. tryToGoDown() -- 3rd level down is ideal, but not mandatory
  455. end
  456. end
  457.  
  458. return success
  459. end
  460.  
  461. function dig()
  462. if not turtle.detect() then
  463. turtle.select(fillerSlot)
  464. turtle.place()
  465. end
  466. turtle.dig()
  467. turtle.select(1)
  468. end
  469.  
  470. function digUp()
  471. if not turtle.detectUp() then
  472. turtle.select(fillerSlot)
  473. turtle.placeUp()
  474. end
  475. turtle.digUp()
  476. turtle.select(1)
  477. end
  478.  
  479. function digDown()
  480. if not turtle.detectDown() then
  481. turtle.select(fillerSlot)
  482. turtle.placeDown()
  483. end
  484. turtle.select(1)
  485. return turtle.digDown()
  486. end
  487.  
  488. function digRow(nBlocks)
  489. -- print("digRow nBlocks: ", nBlocks)
  490.  
  491. for i=1,nBlocks do
  492. digUp()
  493. if not digDown() then
  494. if not bedrockReached then
  495. bedrockReached = true
  496. -- print("digRow: bedrock reached at y = ", y)
  497. end
  498. end
  499. areaMined = areaMined + 1
  500. -- print(" areaMined = ", areaMined)
  501. if i < nBlocks then
  502. forward()
  503. end
  504. end
  505. if isFull() then
  506. purge()
  507. consolidate()
  508. end
  509. end
  510.  
  511. function layRow(nBlocks)
  512. turtle.select(materialSlot)
  513. for i=1,nBlocks do
  514. if ensureMaterial() then
  515. turtle.placeDown()
  516. turtle.forward()
  517. else
  518. if ensureMaterial() then
  519. turtle.placeDown()
  520. turtle.forward()
  521. else
  522. print("Can't find more material")
  523. return
  524. end
  525. end
  526. end
  527. end
  528.  
  529. function mineArea()
  530. areaMined = 0
  531. length = orgLength
  532. width = orgWidth
  533. turtle.select(1)
  534.  
  535. -- while areaMined < targetArea do
  536. while length >= 1 and width >= 1 do
  537. setFace(0)
  538. digRow(length)
  539.  
  540. setFace(1)
  541. forward()
  542. digRow(width - 1)
  543.  
  544. setFace(2)
  545. forward()
  546. digRow(length - 1)
  547.  
  548. setFace(3)
  549. digRow(width - 1)
  550.  
  551. left()
  552. forward()
  553. -- print("Finished area?")
  554. turtle.digUp()
  555. turtle.digDown()
  556. length = length - 2
  557. width = width - 2
  558. end
  559. lowestLevelMined = y
  560. -- print("AREA IS MINED!")
  561. end
  562.  
  563. function dump()
  564. --[[
  565. for i = 1,4 do
  566. turtle.select(i)
  567. count = turtle.getItemCount(i)
  568. if count > 1 then
  569. turtle.drop(count - 1)
  570. end
  571. end
  572. --]]
  573.  
  574. for i=5,16 do
  575. turtle.select(i)
  576. if turtle.getItemCount(i) > 0 then
  577. turtle.drop()
  578. end
  579. end
  580. end
  581.  
  582. function offload()
  583. saveY = y
  584. while y < 0 do
  585. turtle.up()
  586. y = y+1
  587. end
  588.  
  589. turtle.turnLeft()
  590. turtle.turnLeft()
  591.  
  592. if turtle.detect() then
  593. dump()
  594. end
  595.  
  596. turtle.turnLeft()
  597. turtle.turnLeft()
  598.  
  599. while y > saveY do
  600. turtle.down()
  601. y = y-1
  602. end
  603. end
  604.  
  605. function displayTime(time)
  606. --print("time = ", time)
  607. rawSeconds = math.floor(time)
  608. rawMinutes = math.floor(rawSeconds/60)
  609. hours = math.floor(rawMinutes/60)
  610. seconds = rawSeconds - (rawMinutes * 60)
  611. minutes = rawMinutes - (hours * 60)
  612.  
  613. -- print("Raw: ", hours, "h ", rawMinutes, "m ", rawSeconds, "s")
  614. -- print(hours, "h ", minutes, "m ", seconds, "s")
  615.  
  616. timeString = "Elapsed time: "
  617.  
  618. hStr = " hrs "
  619. mStr = " mins "
  620. sStr = " secs"
  621.  
  622. if hours == 1 then hStr = " hr " end
  623. if minutes == 1 then mStr = " min " end
  624. if seconds == 1 then sStr = " sec " end
  625.  
  626. if hours > 0 then
  627. timeString = timeString .. hours .. hStr
  628. end
  629. if minutes > 0 then
  630. timeString = timeString .. minutes .. mStr
  631. end
  632. if seconds > 0 then
  633. timeString = timeString .. seconds .. sStr
  634. end
  635.  
  636. print(timeString)
  637. end
  638.  
  639. function thisLevelNeedsMining()
  640. need = false
  641.  
  642. if lowestLevelMined == 0 then
  643. -- print("thisLevelNeedsMining: lowestLevelMined is zero.")
  644. return true
  645. end
  646.  
  647. if lowestLevelReached or bedrockReached then
  648. -- print("thisLevelNeedsMining: lowestLevelReach ", lowestLevelReached, " bedrockRockReached ", bedrockReached)
  649. if y ~= lowestLevelMined and (y ~= lowestLevelMined - 1) then
  650. -- print("thisLevelNeedsMining: y ", y, " lowestLevelMined ", lowestLevelMined)
  651. need = true
  652. end
  653. else
  654. need = true
  655. end
  656. -- print("thislevelneedsmining: need")
  657. return need
  658. end
  659.  
  660. -- MAIN PROGRAM
  661.  
  662. turtle.select(1)
  663.  
  664. startFuel = turtle.getFuelLevel()
  665. --print("fuelLevel = ", startFuel)
  666. --print("length = ", length)
  667. --print("width = ", width)
  668. --print("face = ", face)
  669.  
  670. --print("Current Fuel Level: ", currentFuelLevel)
  671.  
  672. if currentFuelLevel ~= "unlimited" then
  673. if currentFuelLevel < minimumFuel then
  674. if not turtle.refuel() then
  675. print("No fuel")
  676. return
  677. end
  678. end
  679. end
  680.  
  681. if depth == 1 then end
  682. if depth == 2 then down() end
  683. if depth == 3 then down() down() end
  684.  
  685. notFinished = true
  686. depth = -depth
  687. lowestLevel = depth + 1 -- lowest level the turtle needs to go
  688. startTime = os.clock()
  689.  
  690. down()
  691. down()
  692.  
  693. while thisLevelNeedsMining() do
  694. setFace(0)
  695. mineArea()
  696. -- print("Loop: lowestLevelReached: ", lowestLevelReached, " bedrockReached: ", bedrockReached, " lowestLevelMined: ", lowestLevelMined)
  697.  
  698. -- print("Heading home to level ", y)
  699. home(y)
  700. -- print(" Made it. Now at level ", y)
  701.  
  702. purge()
  703. consolidate()
  704. if emptySlots() <= 3 then
  705. offload()
  706. end
  707.  
  708. if (not bedrockReached) and (not lowestLevelReached) then
  709. -- if y > lowestLevelMined then
  710. goToNextLevel()
  711. else
  712. break
  713. end
  714. end
  715.  
  716. --print("Break out: y ", y, " lowestLevelMined ", lowestLevelMined, " bedrockReached ", bedrockReached)
  717. --finalApproach = true
  718.  
  719. home(0)
  720. setFace(0)
  721. offload()
  722. print(missionMessage)
  723. --print("lowestLevelReached = ", lowestLevelReached, ", bedrockReached = ", bedrockReached)
  724. endTime = os.clock()
  725. elapsedTime = endTime - startTime
  726. endFuel = turtle.getFuelLevel()
  727. usedFuel = startFuel - endFuel
  728. displayTime(elapsedTime)
  729. print("Fuel used: ", usedFuel, " units")
  730.  
  731. print(" Current fuel level is ", turtle.getFuelLevel())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement