funkd0ct0r

Untitled

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