funkd0ct0r

Untitled

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