funkd0ct0r

Untitled

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