funkd0ct0r

Untitled

Dec 23rd, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. -- 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. turtle.turnRight()
  266. turtle.turnRight()
  267. end
  268.  
  269. limit = limit + curZ
  270. while subStep < limit do
  271. subStep = subStep + 1
  272. saveProgress()
  273. moveForward()
  274. end
  275.  
  276. limit = limit + 1
  277. while subStep < limit do
  278. subStep = subStep + 1
  279. saveProgress()
  280. turtle.turnRight()
  281. end
  282.  
  283. limit = limit + curX
  284. while subStep < limit do
  285. subStep = subStep + 1
  286. saveProgress()
  287. moveForward()
  288. end
  289.  
  290. limit = limit + curY
  291. while subStep < limit do
  292. subStep = subStep + 1
  293. saveProgress()
  294. moveDown()
  295. end
  296.  
  297. limit = limit + 1
  298. while subStep < limit do
  299. subStep = subStep + 1
  300. saveProgress()
  301. if (curX % 2 == 0) == isReversed then
  302. --facing = 1
  303. turtle.turnRight()
  304. else
  305. --facing = 3
  306. turtle.turnLeft()
  307. end
  308. end
  309.  
  310. turtle.select(cobbleSlot)
  311. programState = "quarry"
  312. subStep = 0
  313. saveProgress()
  314. end
  315.  
  316. local function doReturn()
  317. programState = "return"
  318. saveProgress()
  319.  
  320. --orient toward x
  321. local limit
  322.  
  323. limit = 1
  324. while subStep < limit do
  325. subStep = subStep + 1
  326. saveProgress()
  327. if (curX % 2 == 0) == isReversed then
  328. --facing = 1
  329. turtle.turnRight()
  330. else
  331. --facing = 3
  332. turtle.turnLeft()
  333. end
  334. end
  335.  
  336. limit = limit + curY
  337. while subStep < limit do
  338. subStep = subStep + 1
  339. saveProgress()
  340. moveUp()
  341. end
  342.  
  343. limit = limit + curX
  344. while subStep < limit do
  345. subStep = subStep + 1
  346. saveProgress()
  347. moveForward()
  348. end
  349.  
  350. limit = limit + 1
  351. while subStep < limit do
  352. subStep = subStep + 1
  353. saveProgress()
  354. turtle.turnLeft()
  355. end
  356.  
  357. limit = limit + curZ
  358. while subStep < limit do
  359. subStep = subStep + 1
  360. saveProgress()
  361. moveForward()
  362. end
  363.  
  364. if useChest then
  365. limit = limit + 1
  366. while subStep < limit do
  367. for slot = outputSlot, 16 do
  368. while turtle.getItemCount(slot) ~= 0 do
  369. turtle.select(slot)
  370. turtle.drop()
  371. sleep(0)
  372. end
  373. end
  374. subStep = subStep + 1
  375. saveProgress()
  376. end
  377. end
  378.  
  379. turtle.turnRight()
  380. turtle.turnRight()
  381. turtle.select(1)
  382. programState = "done"
  383. subStep = 0
  384. saveProgress()
  385. end
  386.  
  387. local function checkInventory()
  388.  
  389. if turtle.getItemCount(16) == 0 then
  390. return
  391. end
  392.  
  393. if useCobblestone then
  394. turtle.select(cobbleSlot)
  395. turtle.drop(turtle.getItemCount(cobbleSlot) - 1)
  396. for slot = outputSlot, 16 do
  397. while turtle.compareTo(slot) do
  398. turtle.select(slot)
  399. turtle.drop()
  400.  
  401. for swap = 16, slot, -1 do
  402. if turtle.getItemCount(swap) > 0 then
  403. turtle.select(swap)
  404. turtle.transferTo(slot)
  405. break
  406. end
  407. end
  408.  
  409. turtle.select(cobbleSlot)
  410. end
  411. end
  412. if turtle.getItemCount(16) == 0 then
  413. return
  414. end
  415. end
  416.  
  417.  
  418. if useEnderChest then
  419. doEmpty()
  420. elseif useChest then
  421. doChest()
  422. else
  423. print("Inventory Full")
  424. local full = true
  425. while full do
  426. full = false
  427. sleep(5)
  428. for slot = outputSlot, 16 do
  429. if turtle.getItemCount(slot) > 0 then
  430. full = true
  431. break
  432. end
  433. end
  434. end
  435. end
  436.  
  437. end
  438.  
  439. local function doQuarry()
  440.  
  441. if turtle.getFuelLevel() < 64 then
  442. if useEnderFuel then
  443. doRefuel()
  444. else
  445. if not requestFuel() then
  446. programState = "done"
  447. return
  448. end
  449. end
  450. end
  451.  
  452. turtle.select(cobbleSlot)
  453.  
  454. if curY > 0 then
  455. checkInventory()
  456. turtle.digUp()
  457. end
  458. if curY < sizeY - 1 then
  459. checkInventory()
  460. turtle.digDown()
  461. end
  462.  
  463. checkInventory()
  464. if curX % 2 == 0 then
  465. if isReversed then
  466.  
  467. if curZ == 0 then
  468. if curX == 0 then
  469. if curY >= sizeY - 2 then
  470. programState = "return"
  471. return
  472. else
  473. checkInventory()
  474. for y = 1, 3 do
  475. if curY >= sizeY - 2 then
  476. break
  477. end
  478. curY = curY + 1
  479. moveDown()
  480. end
  481. turtle.turnRight()
  482. turtle.turnRight()
  483. isReversed = false
  484. end
  485. else
  486. curX = curX - 1
  487. turtle.turnRight()
  488. moveForward()
  489. turtle.turnRight()
  490. end
  491. else
  492. curZ = curZ - 1
  493. moveForward()
  494. end
  495.  
  496. else
  497.  
  498. if curZ == sizeZ - 1 then
  499. if curX == sizeX - 1 then
  500. if curY >= sizeY - 2 then
  501. programState = "return"
  502. return
  503. else
  504. checkInventory()
  505. for y = 1, 3 do
  506. if curY >= sizeY - 2 then
  507. break
  508. end
  509. curY = curY + 1
  510. moveDown()
  511. end
  512. turtle.turnRight()
  513. turtle.turnRight()
  514. isReversed = true
  515. end
  516. else
  517. curX = curX + 1
  518. turtle.turnRight()
  519. moveForward()
  520. turtle.turnRight()
  521. end
  522. else
  523. curZ = curZ + 1
  524. moveForward()
  525. end
  526.  
  527. end
  528.  
  529. else -- curX % 2 ~= 0
  530.  
  531. if isReversed then
  532.  
  533. if curZ == sizeZ - 1 then
  534. if curX == 0 then
  535. if curY >= sizeY - 2 then
  536. programState = "return"
  537. return
  538. else
  539. checkInventory()
  540. for y = 1, 3 do
  541. if curY >= sizeY - 2 then
  542. break
  543. end
  544. curY = curY + 1
  545. moveDown()
  546. end
  547. turtle.turnLeft()
  548. turtle.turnLeft()
  549. isReversed = true
  550. end
  551. else
  552. curX = curX - 1
  553. turtle.turnLeft()
  554. moveForward()
  555. turtle.turnLeft()
  556. end
  557. else
  558. curZ = curZ + 1
  559. moveForward()
  560. end
  561.  
  562. else
  563.  
  564. if curZ == 0 then
  565. if curX == sizeX - 1 then
  566. if curY >= sizeY - 2 then
  567. programState = "return"
  568. return
  569. else
  570. checkInventory()
  571. for y = 1, 3 do
  572. if curY >= sizeY - 2 then
  573. break
  574. end
  575. curY = curY + 1
  576. moveDown()
  577. end
  578. turtle.turnLeft()
  579. turtle.turnLeft()
  580. isReversed = true
  581. end
  582. else
  583. curX = curX + 1
  584. turtle.turnLeft()
  585. moveForward()
  586. turtle.turnLeft()
  587. end
  588. else
  589. curZ = curZ - 1
  590. moveForward()
  591. end
  592.  
  593. end
  594.  
  595. end
  596. saveProgress()
  597. end
  598.  
  599. local function getArgs()
  600.  
  601. if sizeX == nil or sizeY == nil or sizeZ == nil then
  602. return false
  603. end
  604.  
  605. sizeX = tonumber(sizeX)
  606. sizeY = tonumber(sizeY)
  607. sizeZ = tonumber(sizeZ)
  608.  
  609. programState = "done"
  610. refuelingState = ""
  611. curX = 0
  612. curY = 0
  613. curZ = 0
  614. subStep = 0
  615.  
  616. useEnderChest = false
  617. useEnderFuel = false
  618. useChest = false
  619. useCobblestone = false
  620.  
  621. print("Excavate right " .. sizeX .. ", down " .. sizeY .. ", forward " .. sizeZ)
  622. print("Is this correct?")
  623. if not getYesNo() then
  624. return false
  625. end
  626.  
  627. print("Use Ender Chest in slot 1 for output?")
  628. useEnderChest = getYesNo()
  629. if useEnderChest then
  630. print("Use Ender Chest in slot 2 for fuel?")
  631. useEnderFuel = getYesNo()
  632. else
  633. print("Use Chest behind turtle for output?")
  634. useChest = getYesNo()
  635. end
  636.  
  637. if useEnderChest then
  638. if useEnderFuel then
  639. outputSlot = 3
  640. else
  641. outputSlot = 2
  642. end
  643. else
  644. outputSlot = 1
  645. end
  646.  
  647. print("Discard Cobblestone? (requires cobblestone in slot" .. outputSlot .. ")")
  648. useCobblestone = getYesNo()
  649.  
  650. local fuelRequired = (sizeX * sizeZ) * math.ceil(sizeY / 3) + sizeY
  651.  
  652. print("Fuel Required: " .. fuelRequired)
  653. print("Fuel Current: " .. turtle.getFuelLevel())
  654. if not useEnderFuel and turtle.getFuelLevel() < fuelRequired then
  655. if not requestFuel() then
  656. return false
  657. end
  658. end
  659.  
  660. print("Start Quarry with these parameters?")
  661. if not getYesNo() then
  662. return false
  663. end
  664.  
  665. programState = "init"
  666. saveProgress()
  667. return true
  668. end
  669.  
  670. local function main()
  671.  
  672. if refuelingstate == "refuel" then
  673. doRefuel()
  674. end
  675.  
  676. if refuelingstate == "empty" then
  677. doEmpty()
  678. end
  679.  
  680. if useEnderChest then
  681. if turtle.getItemCount(1) == 0 then
  682. print("Place Ender Chest in slot 1")
  683. while turtle.getItemCount(1) == 0 do
  684. sleep(1)
  685. end
  686. end
  687. if useEnderFuel then
  688. if turtle.getItemCount(2) == 0 then
  689. print("Place Ender Chest in slot 2")
  690. while turtle.getItemCount(2) == 0 do
  691. sleep(1)
  692. end
  693. end
  694. outputSlot = 3
  695. else
  696. outputSlot = 2
  697. end
  698. else
  699. outputSlot = 1
  700. end
  701. cobbleSlot = outputSlot
  702. if useCobblestone then
  703. if turtle.getItemCount(cobbleSlot) == 0 then
  704. print("Place Cobblestone in slot " .. cobbleSlot)
  705. while turtle.getItemCount(cobbleSlot) == 0 do
  706. sleep(1)
  707. end
  708. end
  709. outputSlot = outputSlot + 1
  710. end
  711.  
  712.  
  713. while true do
  714.  
  715. if programState == "quarry" then
  716. doQuarry()
  717. end
  718. if programState == "chest" then
  719. doChest()
  720. end
  721. if programState == "init" then
  722. if curY < sizeY - 1 then
  723. curY = curY + 1
  724. moveDown()
  725. end
  726. programState = "quarry"
  727. end
  728. if programState == "return" then
  729. doReturn()
  730. break
  731. end
  732.  
  733. end
  734.  
  735. end
  736.  
  737.  
  738. restoreProgress()
  739. if programState == "done" then
  740. sizeX, sizeY, sizeZ = ...
  741. if not getArgs() then
  742. print("Usage")
  743. print(" startup x y z")
  744. return
  745. end
  746. end
  747.  
  748. main()
Advertisement
Add Comment
Please, Sign In to add comment