funkd0ct0r

Untitled

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