funkd0ct0r

Untitled

Apr 2nd, 2014
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.59 KB | None | 0 0
  1. --todo:
  2. -- autorefuel from found coal
  3. -- fix the edge extra block
  4. -- display current fuel level while refueling
  5. -- refueling during doChest etc
  6. -- maybe refuel limit = 1
  7. -- PT0SmjPx
  8. -- JYYEHcy5
  9. -- GeyqYHnM
  10. -- i6J1bA3Q
  11. -- sCpfzzgV
  12. -- xq91ssuy
  13. -- aC20GtgU
  14. -- gK7A19W4
  15. -- pGdmLjjr
  16. -- eYCve9Eb
  17. -- ge4QnKbY
  18.  
  19.  
  20. local programState = "done"
  21. local refuelingState = ""
  22.  
  23. local curX = 0
  24. local curY = 0
  25. local curZ = 0
  26. local subStep = 0
  27. local isReversed = false
  28.  
  29. local sizeX = 0
  30. local sizeY = 0
  31. local sizeZ = 0
  32.  
  33. local useEnderChest = false
  34. local useEnderFuel = false
  35. local useChest = false
  36. local useCobblestone = false
  37. local useCoal = false
  38.  
  39. local saveFile = "savedata"
  40.  
  41. local outputSlot = 0
  42. local cobbleSlot = 0
  43.  
  44. local function getYesNo()
  45. local i
  46. local event, param
  47.  
  48. while true do
  49. event, param = os.pullEvent("key")
  50.  
  51. if param == 21 then
  52. return true
  53. end
  54. if param == 49 then
  55. return false
  56. end
  57. end
  58. end
  59.  
  60. local function requestFuel()
  61. local i
  62. local c = false
  63. local event, param
  64.  
  65. if useCoal and turtle.getItemCount(coalSlot) > 1 then
  66. turtle.select(coalSlot)
  67. if turtle.refuel(1) then
  68. turtle.select(cobbleSlot)
  69. return true
  70. end
  71. end
  72.  
  73. print("Press F to refuel from slot 16")
  74. print("Press Q to exit the program")
  75.  
  76. if turtle.getFuelLevel() >= 64 then
  77. c = true
  78. print("Press C to continue quarry")
  79. end
  80.  
  81. while true do
  82. event, param = os.pullEvent("key")
  83.  
  84. if param == 33 then --f
  85. turtle.select(16)
  86. turtle.refuel()
  87. if not c and turtle.getFuelLevel() >= 64 then
  88. c = true
  89. print("Press C to continue quarry")
  90. end
  91. end
  92. if param == 16 then --q
  93. return false
  94. end
  95. if c and param == 46 then --c
  96. turtle.select(cobbleSlot)
  97. return true
  98. end
  99. end
  100. end
  101.  
  102.  
  103. local function restoreProgress()
  104. if fs.exists(saveFile) then
  105. local file = fs.open(saveFile,"r")
  106.  
  107. programState = tostring(file.readLine())
  108. refuelingState = tostring(file.readLine())
  109.  
  110. curX = tonumber(file.readLine())
  111. curY = tonumber(file.readLine())
  112. curZ = tonumber(file.readLine())
  113. subStep = tonumber(file.readLine())
  114. sizeX = tonumber(file.readLine())
  115. sizeY = tonumber(file.readLine())
  116. sizeZ = tonumber(file.readLine())
  117.  
  118. if file.readLine() == "true" then isReversed = true else isReversed = false end
  119.  
  120. if file.readLine() == "true" then useEnderChest = true else useEnderChest = false end
  121. if file.readLine() == "true" then useEnderFuel = true else useEnderFuel = false end
  122. if file.readLine() == "true" then useChest = true else useChest = false end
  123. if file.readLine() == "true" then useCobblestone = true else useCobblestone = false end
  124. if file.readLine() == "true" then useCoal = true else useCoal = false end
  125.  
  126. file.close()
  127. end
  128. end
  129.  
  130. local function saveProgress()
  131. local file = fs.open(saveFile,"w")
  132.  
  133. file.write(tostring(programState).."\n")
  134. file.write(tostring(refuelingState).."\n")
  135.  
  136. file.write(tostring(curX).."\n")
  137. file.write(tostring(curY).."\n")
  138. file.write(tostring(curZ).."\n")
  139. file.write(tostring(subStep).."\n")
  140.  
  141. file.write(tostring(sizeX).."\n")
  142. file.write(tostring(sizeY).."\n")
  143. file.write(tostring(sizeZ).."\n")
  144.  
  145. file.write(tostring(isReversed).."\n")
  146.  
  147. file.write(tostring(useEnderChest).."\n")
  148. file.write(tostring(useEnderFuel).."\n")
  149. file.write(tostring(useChest).."\n")
  150. file.write(tostring(useCobblestone).."\n")
  151. file.write(tostring(useCoal).."\n")
  152.  
  153. file.close()
  154. end
  155.  
  156. local function moveForward()
  157. turtle.dig()
  158. while not turtle.forward() do
  159. sleep(0)
  160. turtle.dig()
  161. end
  162. end
  163. local function moveUp()
  164. turtle.digUp()
  165. while not turtle.up() do
  166. sleep(0)
  167. turtle.digUp()
  168. end
  169. end
  170. local function moveDown()
  171. print("movedown")
  172. turtle.digDown()
  173. while not turtle.down() do
  174. print("cant")
  175. sleep(0)
  176. turtle.digDown()
  177. end
  178. end
  179.  
  180. local function doRefuel()
  181. refuelingState = "refuel"
  182. saveProgress()
  183.  
  184. while turtle.getFuelLevel() < 64 do
  185.  
  186. if turtle.getItemCount(2) > 0 then
  187. turtle.select(2)
  188. while not turtle.placeUp() do
  189. sleep(0)
  190. turtle.digUp()
  191. end
  192. end
  193.  
  194. turtle.select(16)
  195. turtle.drop()
  196. for maxstacks = 1, 1 do
  197. turtle.suckUp()
  198. if not turtle.refuel() then turtle.drop() end
  199. end
  200. end
  201.  
  202. turtle.select(2)
  203. turtle.digUp()
  204. refuelingState = ""
  205. saveProgress()
  206. end
  207.  
  208. local function doEmpty()
  209. refuelingState = "empty"
  210. saveProgress()
  211.  
  212. if turtle.getItemCount(1) > 0 then
  213. turtle.select(1)
  214. while not turtle.placeUp() do
  215. sleep(0)
  216. turtle.digUp()
  217. end
  218. end
  219.  
  220. for slot = outputSlot, 16 do
  221. while turtle.getItemCount(slot) > 0 do
  222. turtle.select(slot)
  223. turtle.dropUp()
  224. sleep(1)
  225. end
  226. end
  227.  
  228. turtle.select(1)
  229. turtle.digUp()
  230. refuelingState = ""
  231. saveProgress()
  232. end
  233.  
  234. local function doChest()
  235. programState = "chest"
  236. saveProgress()
  237.  
  238. --orient toward x
  239. local limit
  240.  
  241. limit = 1
  242. while subStep < limit do
  243. subStep = subStep + 1
  244. saveProgress()
  245. if (curX % 2 == 0) == isReversed then
  246. --facing = 1
  247. turtle.turnRight()
  248. else
  249. --facing = 3
  250. turtle.turnLeft()
  251. end
  252. end
  253.  
  254. limit = limit + curY
  255. while subStep < limit do
  256. subStep = subStep + 1
  257. saveProgress()
  258. moveUp()
  259. end
  260.  
  261. limit = limit + curX
  262. while subStep < limit do
  263. subStep = subStep + 1
  264. saveProgress()
  265. moveForward()
  266. end
  267.  
  268. limit = limit + 1
  269. while subStep < limit do
  270. subStep = subStep + 1
  271. saveProgress()
  272. turtle.turnLeft()
  273. end
  274.  
  275. limit = limit + curZ
  276. while subStep < limit do
  277. subStep = subStep + 1
  278. saveProgress()
  279. moveForward()
  280. end
  281.  
  282. limit = limit + 1
  283. while subStep < limit do
  284. for slot = outputSlot, 16 do
  285. while turtle.getItemCount(slot) ~= 0 do
  286. turtle.select(slot)
  287. turtle.drop()
  288. sleep(1)
  289. end
  290. end
  291. subStep = subStep + 1
  292. saveProgress()
  293. end
  294.  
  295. limit = limit + 2
  296. while subStep < limit do
  297. subStep = subStep + 1
  298. saveProgress()
  299. turtle.turnRight()
  300. end
  301.  
  302. limit = limit + curZ
  303. while subStep < limit do
  304. subStep = subStep + 1
  305. saveProgress()
  306. moveForward()
  307. end
  308.  
  309. limit = limit + 1
  310. while subStep < limit do
  311. subStep = subStep + 1
  312. saveProgress()
  313. turtle.turnRight()
  314. end
  315.  
  316. limit = limit + curX
  317. while subStep < limit do
  318. subStep = subStep + 1
  319. saveProgress()
  320. moveForward()
  321. end
  322.  
  323. limit = limit + curY
  324. while subStep < limit do
  325. subStep = subStep + 1
  326. saveProgress()
  327. moveDown()
  328. end
  329.  
  330. limit = limit + 1
  331. while subStep < limit do
  332. subStep = subStep + 1
  333. saveProgress()
  334. if (curX % 2 == 0) == isReversed then
  335. --facing = 1
  336. turtle.turnRight()
  337. else
  338. --facing = 3
  339. turtle.turnLeft()
  340. end
  341. end
  342.  
  343. turtle.select(cobbleSlot)
  344. programState = "quarry"
  345. subStep = 0
  346. saveProgress()
  347. end
  348.  
  349. local function doReturn()
  350. programState = "return"
  351. saveProgress()
  352.  
  353. --orient toward x
  354. local limit
  355.  
  356. limit = 1
  357. while subStep < limit do
  358. subStep = subStep + 1
  359. saveProgress()
  360. if (curX % 2 == 0) == isReversed then
  361. --facing = 1
  362. turtle.turnRight()
  363. else
  364. --facing = 3
  365. turtle.turnLeft()
  366. end
  367. end
  368.  
  369. limit = limit + curY
  370. while subStep < limit do
  371. subStep = subStep + 1
  372. saveProgress()
  373. moveUp()
  374. end
  375.  
  376. limit = limit + curX
  377. while subStep < limit do
  378. subStep = subStep + 1
  379. saveProgress()
  380. moveForward()
  381. end
  382.  
  383. limit = limit + 1
  384. while subStep < limit do
  385. subStep = subStep + 1
  386. saveProgress()
  387. turtle.turnLeft()
  388. end
  389.  
  390. limit = limit + curZ
  391. while subStep < limit do
  392. subStep = subStep + 1
  393. saveProgress()
  394. moveForward()
  395. end
  396.  
  397. if useChest then
  398. limit = limit + 1
  399. while subStep < limit do
  400. for slot = outputSlot, 16 do
  401. while turtle.getItemCount(slot) ~= 0 do
  402. turtle.select(slot)
  403. turtle.drop()
  404. sleep(0)
  405. end
  406. end
  407. subStep = subStep + 1
  408. saveProgress()
  409. end
  410. end
  411.  
  412. turtle.turnRight()
  413. turtle.turnRight()
  414. turtle.select(1)
  415. programState = "done"
  416. subStep = 0
  417. saveProgress()
  418. end
  419.  
  420. local function checkInventory()
  421.  
  422. if turtle.getItemCount(16) == 0 then
  423. return
  424. end
  425.  
  426. if useCobblestone then
  427. turtle.select(cobbleSlot)
  428. turtle.drop(turtle.getItemCount(cobbleSlot) - 1)
  429. for slot = outputSlot, 16 do
  430. while turtle.compareTo(slot) do
  431. turtle.select(slot)
  432. turtle.drop()
  433.  
  434. for swap = 16, slot, -1 do
  435. if turtle.getItemCount(swap) > 0 then
  436. turtle.select(swap)
  437. turtle.transferTo(slot)
  438. break
  439. end
  440. end
  441.  
  442. turtle.select(cobbleSlot)
  443. end
  444. end
  445. if turtle.getItemCount(16) == 0 then
  446. return
  447. end
  448. end
  449.  
  450.  
  451. if useEnderChest then
  452. doEmpty()
  453. elseif useChest then
  454. doChest()
  455. else
  456. print("Inventory Full")
  457. local full = true
  458. while full do
  459. full = false
  460. sleep(5)
  461. for slot = outputSlot, 16 do
  462. if turtle.getItemCount(slot) > 0 then
  463. full = true
  464. break
  465. end
  466. end
  467. end
  468. end
  469.  
  470. end
  471.  
  472. local function doQuarryDig()
  473.  
  474. if turtle.getFuelLevel() < 64 then
  475. if useEnderFuel then
  476. doRefuel()
  477. else
  478. if not requestFuel() then
  479. programState = "done"
  480. return
  481. end
  482. end
  483. end
  484.  
  485. if curY > 0 then
  486. checkInventory()
  487. turtle.digUp()
  488. end
  489. if curY < sizeY - 1 then
  490. checkInventory()
  491. turtle.digDown()
  492. end
  493.  
  494. checkInventory()
  495.  
  496. end
  497.  
  498. local function doQuarry()
  499.  
  500. turtle.select(cobbleSlot)
  501.  
  502. while true do
  503.  
  504.  
  505. if curX % 2 == 0 then
  506. if isReversed then
  507.  
  508. if curZ == 0 then
  509. if curX == 0 then
  510. if curY >= sizeY - 2 then
  511. programState = "return"
  512. return
  513. else
  514. if subStep < 3 then
  515. if curY > 0 then
  516. turtle.digUp()
  517. end
  518. subStep = subStep + 1
  519. if curY < sizeY - 2 then
  520. curY = curY + 1
  521. saveProgress()
  522. moveDown()
  523. end
  524. elseif subStep == 3 then
  525. subStep = subStep + 1
  526. saveProgress()
  527. turtle.turnRight()
  528. elseif subStep == 4 then
  529. subStep = 0
  530. isReversed = false
  531. saveProgress()
  532. turtle.turnRight()
  533. end
  534. end
  535. else
  536. if subStep == 0 then
  537. subStep = subStep + 1
  538. saveProgress()
  539. turtle.turnRight()
  540. elseif subStep == 1 then
  541.  
  542. if curY > 0 then
  543. turtle.digUp()
  544. end
  545. if curY < sizeY - 1 then
  546. turtle.digDown()
  547. end
  548.  
  549. subStep = subStep + 1
  550. saveProgress()
  551. moveForward()
  552. else
  553. subStep = 0
  554. curX = curX - 1
  555. saveProgress()
  556. turtle.turnRight()
  557. end
  558. end
  559. else
  560. doQuarryDig()
  561. saveProgress()
  562. curZ = curZ - 1
  563. moveForward()
  564. end
  565.  
  566. else
  567.  
  568. if curZ == sizeZ - 1 then
  569. if curX == sizeX - 1 then
  570. if curY >= sizeY - 2 then
  571. programState = "return"
  572. return
  573. else
  574. if subStep < 3 then
  575. if curY > 0 then
  576. turtle.digUp()
  577. end
  578. subStep = subStep + 1
  579. if curY < sizeY - 2 then
  580. curY = curY + 1
  581. saveProgress()
  582. moveDown()
  583. end
  584. elseif subStep == 3 then
  585. subStep = subStep + 1
  586. saveProgress()
  587. turtle.turnRight()
  588. elseif subStep == 4 then
  589. subStep = 0
  590. isReversed = true
  591. saveProgress()
  592. turtle.turnRight()
  593. end
  594. end
  595. else
  596. if subStep == 0 then
  597. subStep = subStep + 1
  598. saveProgress()
  599. turtle.turnRight()
  600. elseif subStep == 1 then
  601.  
  602. if curY > 0 then
  603. turtle.digUp()
  604. end
  605. if curY < sizeY - 1 then
  606. turtle.digDown()
  607. end
  608.  
  609. subStep = subStep + 1
  610. saveProgress()
  611. moveForward()
  612. else
  613. subStep = 0
  614. curX = curX + 1
  615. saveProgress()
  616. turtle.turnRight()
  617. end
  618. end
  619. else
  620. doQuarryDig()
  621. curZ = curZ + 1
  622. saveProgress()
  623. moveForward()
  624. end
  625.  
  626. end
  627.  
  628. else -- curX % 2 ~= 0
  629.  
  630. if isReversed then
  631.  
  632. if curZ == sizeZ - 1 then
  633. if curX == 0 then
  634. if curY >= sizeY - 2 then
  635. programState = "return"
  636. return
  637. else
  638. if subStep < 3 then
  639. if curY > 0 then
  640. turtle.digUp()
  641. end
  642. subStep = subStep + 1
  643. if curY < sizeY - 2 then
  644. curY = curY + 1
  645. saveProgress()
  646. moveDown()
  647. end
  648. elseif subStep == 3 then
  649. subStep = subStep + 1
  650. saveProgress()
  651. turtle.turnLeft()
  652. elseif subStep == 4 then
  653. subStep = 0
  654. isReversed = false
  655. saveProgress()
  656. turtle.turnLeft()
  657. end
  658. end
  659. else
  660. if subStep == 0 then
  661. subStep = subStep + 1
  662. saveProgress()
  663. turtle.turnLeft()
  664. elseif subStep == 1 then
  665.  
  666. if curY > 0 then
  667. turtle.digUp()
  668. end
  669. if curY < sizeY - 1 then
  670. turtle.digDown()
  671. end
  672.  
  673. subStep = subStep + 1
  674. saveProgress()
  675. moveForward()
  676. else
  677. subStep = 0
  678. curX = curX - 1
  679. saveProgress()
  680. turtle.turnLeft()
  681. end
  682. end
  683. else
  684. doQuarryDig()
  685. curZ = curZ + 1
  686. saveProgress()
  687. moveForward()
  688. end
  689.  
  690. else
  691.  
  692. if curZ == 0 then
  693. if curX == sizeX - 1 then
  694. if curY >= sizeY - 2 then
  695. programState = "return"
  696. return
  697. else
  698. if subStep < 3 then
  699. if curY > 0 then
  700. turtle.digUp()
  701. end
  702. subStep = subStep + 1
  703. if curY < sizeY - 2 then
  704. curY = curY + 1
  705. saveProgress()
  706. moveDown()
  707. end
  708. elseif subStep == 3 then
  709. subStep = subStep + 1
  710. saveProgress()
  711. turtle.turnLeft()
  712. elseif subStep == 4 then
  713. subStep = 0
  714. isReversed = true
  715. saveProgress()
  716. turtle.turnLeft()
  717. end
  718. end
  719. else
  720. if subStep == 0 then
  721. subStep = subStep + 1
  722. saveProgress()
  723. turtle.turnLeft()
  724. elseif subStep == 1 then
  725.  
  726. if curY > 0 then
  727. turtle.digUp()
  728. end
  729. if curY < sizeY - 1 then
  730. turtle.digDown()
  731. end
  732.  
  733. subStep = subStep + 1
  734. saveProgress()
  735. moveForward()
  736. else
  737. subStep = 0
  738. curX = curX + 1
  739. saveProgress()
  740. turtle.turnLeft()
  741. end
  742. end
  743. else
  744. doQuarryDig()
  745. curZ = curZ - 1
  746. saveProgress()
  747. moveForward()
  748. end
  749.  
  750. end
  751.  
  752. end
  753.  
  754. end
  755. end
  756.  
  757. local function getArgs()
  758.  
  759. if sizeX == nil or sizeY == nil or sizeZ == nil then
  760. return false
  761. end
  762.  
  763. sizeX = tonumber(sizeX)
  764. sizeY = tonumber(sizeY)
  765. sizeZ = tonumber(sizeZ)
  766.  
  767. programState = "done"
  768. refuelingState = ""
  769. curX = 0
  770. curY = 0
  771. curZ = 0
  772. subStep = 0
  773.  
  774. useEnderChest = false
  775. useEnderFuel = false
  776. useChest = false
  777. useCobblestone = false
  778.  
  779. print("Excavate right " .. sizeX .. ", down " .. sizeY .. ", forward " .. sizeZ)
  780. print("Is this correct?")
  781. if not getYesNo() then
  782. return false
  783. end
  784.  
  785. print("Use Ender Chest in slot 1 for output?")
  786. useEnderChest = getYesNo()
  787. if useEnderChest then
  788. print("Use Ender Chest in slot 2 for fuel?")
  789. useEnderFuel = getYesNo()
  790. else
  791. print("Use Chest behind turtle for output?")
  792. useChest = getYesNo()
  793. end
  794.  
  795. if useEnderChest then
  796. if useEnderFuel then
  797. outputSlot = 3
  798. else
  799. outputSlot = 2
  800. end
  801. else
  802. outputSlot = 1
  803. end
  804.  
  805. print("Discard Cobblestone? (requires cobblestone in slot" .. outputSlot .. ")")
  806. useCobblestone = getYesNo()
  807. cobbleSlot = outputSlot
  808. if useCobblestone then
  809. outputSlot = outputSlot + 1
  810. end
  811.  
  812. print("Use Mined Coal? (requires coal in slot" .. outputSlot .. ")")
  813. useCoal = getYesNo()
  814. coalSlot = outputSlot
  815.  
  816. local fuelRequired = (sizeX * sizeZ) * math.ceil(sizeY / 3) + sizeY
  817.  
  818. print("Fuel Required: " .. fuelRequired)
  819. print("Fuel Current: " .. turtle.getFuelLevel())
  820. if (not useEnderFuel) and (turtle.getFuelLevel() < fuelRequired) then
  821. if not requestFuel() then
  822. return false
  823. end
  824. end
  825.  
  826. print("Start Quarry with these parameters?")
  827. if not getYesNo() then
  828. return false
  829. end
  830.  
  831. programState = "init"
  832. saveProgress()
  833. return true
  834. end
  835.  
  836. local function main()
  837.  
  838. outputSlot = 1
  839. if useEnderChest then
  840. outputSlot = 2
  841. if useEnderFuel then
  842. outputSlot = 3
  843. end
  844. end
  845. cobbleSlot = outputSlot
  846. if useCobblestone then
  847. outputSlot = outputSlot + 1
  848. end
  849. coalSlot = outputSlot
  850. if useCoal then
  851. outputSlot = outputSlot + 1
  852. end
  853.  
  854. if refuelingState == "refuel" then
  855. doRefuel()
  856. end
  857.  
  858. if refuelingState == "empty" then
  859. doEmpty()
  860. end
  861.  
  862. if useEnderChest then
  863. if turtle.getItemCount(1) == 0 then
  864. print("Place Ender Chest in slot 1")
  865. while turtle.getItemCount(1) == 0 do
  866. sleep(1)
  867. end
  868. end
  869. if useEnderFuel then
  870. if turtle.getItemCount(2) == 0 then
  871. print("Place Ender Chest in slot 2")
  872. while turtle.getItemCount(2) == 0 do
  873. sleep(1)
  874. end
  875. end
  876. end
  877. end
  878. if useCobblestone then
  879. if turtle.getItemCount(cobbleSlot) == 0 then
  880. print("Place Cobblestone in slot " .. cobbleSlot)
  881. while turtle.getItemCount(cobbleSlot) == 0 do
  882. sleep(1)
  883. end
  884. end
  885. end
  886. if useCoal then
  887. if turtle.getItemCount(coalSlot) == 0 then
  888. print("Place Coal in slot " .. coalSlot)
  889. while turtle.getItemCount(coalSlot) == 0 do
  890. sleep(1)
  891. end
  892. end
  893. end
  894.  
  895.  
  896. while true do
  897.  
  898. if programState == "quarry" then
  899. doQuarry()
  900. end
  901. if programState == "chest" then
  902. doChest()
  903. end
  904. if programState == "init" then
  905. print("init")
  906. if useEnderFuel then doRefuel() end
  907. turtle.select(cobbleSlot)
  908. if curY < sizeY - 1 then
  909. print("init2")
  910. curY = curY + 1
  911. moveDown()
  912. end
  913. programState = "quarry"
  914. print("quarry")
  915. end
  916. if programState == "return" then
  917. doReturn()
  918. break
  919. end
  920.  
  921. end
  922.  
  923. end
  924.  
  925.  
  926. restoreProgress()
  927. if programState == "done" then
  928. sizeX, sizeY, sizeZ = ...
  929. if not getArgs() then
  930. print("Usage")
  931. print(" startup x y z")
  932. return
  933. end
  934. end
  935.  
  936. main()
Advertisement
Add Comment
Please, Sign In to add comment