Advertisement
funkd0ct0r

Untitled

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