Advertisement
funkd0ct0r

Untitled

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