Advertisement
Jharakn

Mining Program 3

Dec 5th, 2012
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.74 KB | None | 0 0
  1. --Global Variables--
  2. min_x = 1
  3. max_x = 1
  4. mem_x = 1
  5. home_x = 1
  6. ret_x = 1
  7. x = 1
  8.  
  9. min_y = 1
  10. max_y = 1
  11. mem_y = 1
  12. home_y = 1
  13. ret_y = 1
  14. y = 1
  15.  
  16. min_z = 1
  17. max_z = 1
  18. mem_z = 1
  19. home_z = 1
  20. ret_z = 1
  21. z = 1
  22.  
  23. f = 1
  24. mem_f = 1
  25. home_f = 1
  26. ret_f = 1
  27.  
  28. continue = false
  29. x_forward = true
  30. y_forward = true
  31. new_floor = true
  32. turtle_objective = "mine"
  33.  
  34. writeSchedulerCounter = 0
  35.  
  36. function crash()
  37. print("turtle has gone wrong, r to reboot")
  38. while true do
  39. local event, character = os.pullEvent()
  40. if event == "char" and character == "r" then
  41. os.restart()
  42. end
  43. end
  44. end
  45.  
  46. function debugStop()
  47. print("in debug stop")
  48. while true do
  49. local event, character = os.pullEvent()
  50. if event == "char" and character == "e" then os.restart() end
  51. end
  52. end
  53.  
  54. function forward()
  55. local i = 0
  56. local operation_output = true
  57. turtle.dig()
  58. while turtle.forward() == false do
  59. turtle.dig()
  60. turtle.attack()
  61. i = i + 1
  62. if i == 10 then
  63. operation_output = false
  64. break
  65. end
  66. sleep(1)
  67. end
  68. if operation_output == true then
  69. if f == 1 then
  70. y = y + 1
  71. elseif f == 2 then
  72. x = x + 1
  73. elseif f == 3 then
  74. y = y - 1
  75. elseif f == 4 then
  76. x = x - 1
  77. else
  78. print("ERROR: turtle has no direction?!?!?")
  79. crash()
  80. end
  81. end
  82. writeScheduler()
  83. return operation_output
  84. end
  85.  
  86. function forwardMine()
  87. local i = 0
  88. local operation_output = true
  89. turtle.dig()
  90. while turtle.forward() == false do
  91. turtle.dig()
  92. turtle.attack()
  93. i = i + 1
  94. if i == 10 then
  95. operation_output = false
  96. break
  97. end
  98. sleep(1)
  99. end
  100.  
  101. if operation_output == true then
  102. turtle.digDown()
  103. if f == 1 then
  104. y = y + 1
  105. elseif f == 2 then
  106. x = x + 1
  107. elseif f == 3 then
  108. y = y - 1
  109. elseif f == 4 then
  110. x = x - 1
  111. else
  112. print("ERROR: turtle has no direction?!?!?")
  113. crash()
  114. end
  115. end
  116. writeScheduler()
  117. return operation_output
  118. end
  119.  
  120. function up()
  121. local i = 0
  122. local operation_output = true
  123. turtle.digUp()
  124. while turtle.up() == false do
  125. turtle.digUp()
  126. turtle.attackUp()
  127. i = i + 1
  128. if i == 10 then
  129. operation_output = false
  130. break
  131. end
  132. sleep(1)
  133. end
  134. if operation_output == true then
  135. z = z + 1
  136. end
  137. writeScheduler()
  138. return operation_output
  139. end
  140.  
  141. function down()
  142. local i = 0
  143. local operation_output = true
  144. turtle.digDown()
  145. while turtle.down() == false do
  146. turtle.digDown()
  147. turtle.attackDown()
  148. i = i + 1
  149. if i == 10 then
  150. operation_output = false
  151. break
  152. end
  153. sleep(1)
  154. end
  155. if operation_output == true then
  156. z = z - 1
  157. end
  158. writeScheduler()
  159. return operation_output
  160. end
  161.  
  162. function turnLeft(i_end)
  163. for i = 1, i_end do
  164. turtle.turnLeft()
  165. f = f - 1
  166. if f == 0 then
  167. f = 4
  168. end
  169. end
  170. writeScheduler()
  171. end
  172.  
  173. function turnRight(i_end)
  174. for i = 1, i_end do
  175. turtle.turnRight()
  176. f = f + 1
  177. if f == 5 then
  178. f = 1
  179. end
  180. end
  181. writeScheduler()
  182. end
  183.  
  184. function turnRel(direction)
  185. local forward_f = home_f
  186. local right_f = home_f
  187. local backwards_f = home_f
  188. local left_f = home_f
  189.  
  190. for i = 1,1 do
  191. right_f = right_f + 1
  192. if right_f == 5 then right_f = 1 end
  193. end
  194. for i = 1,2 do
  195. backwards_f = backwards_f + 1
  196. if backwards_f == 5 then backwards_f = 1 end
  197. end
  198. for i = 1,3 do
  199. left_f = left_f + 1
  200. if left_f == 5 then left_f = 1 end
  201. end
  202.  
  203. if direction == "forwards" then
  204. while f ~= forward_f do
  205. if f < forward_f then
  206. if f == 1 and forward_f == 4 then
  207. turtle.turnLeft()
  208. f = 4
  209. else
  210. turtle.turnRight()
  211. f = f + 1
  212. end
  213. else
  214. if f == 4 and forward_f == 1 then
  215. turtle.turnRight()
  216. f = 1
  217. else
  218. turtle.turnLeft()
  219. f = f - 1
  220. end
  221. end
  222. end
  223. elseif direction == "right" then
  224. while f ~= right_f do
  225. if f < right_f then
  226. if f == 1 and right_f == 4 then
  227. turtle.turnLeft()
  228. f = 4
  229. else
  230. turtle.turnRight()
  231. f = f + 1
  232. end
  233. else
  234. if f == 4 and right_f == 1 then
  235. turtle.turnRight()
  236. f = 1
  237. else
  238. turtle.turnLeft()
  239. f = f - 1
  240. end
  241. end
  242. end
  243. elseif direction == "backwards" then
  244. while f ~= backwards_f do
  245. if f < backwards_f then
  246. if f == 1 and backwards_f == 4 then
  247. turtle.turnLeft()
  248. f = 4
  249. else
  250. turtle.turnRight()
  251. f = f + 1
  252. end
  253. else
  254. if f == 4 and backwards_f == 1 then
  255. turtle.turnRight()
  256. f = 1
  257. else
  258. turtle.turnLeft()
  259. f = f - 1
  260. end
  261. end
  262. end
  263. elseif direction == "left" then
  264. while f ~= left_f do
  265. if f < left_f then
  266. if f == 1 and left_f == 4 then
  267. turtle.turnLeft()
  268. f = 4
  269. else
  270. turtle.turnRight()
  271. f = f + 1
  272. end
  273. else
  274. if f == 4 and left_f == 1 then
  275. turtle.turnRight()
  276. f = 1
  277. else
  278. turtle.turnLeft()
  279. f = f - 1
  280. end
  281. end
  282. end
  283. else
  284. print("ERROR: incorrect reletive direction call")
  285. crash()
  286. end
  287. pcall(writeState)
  288. --print("forward_f:",forward_f," right_f:",right_f," backwards_f:",backwards_f," left_f:",left_f)
  289. --print("current_f:",f)
  290. end
  291.  
  292. function turn(goto_f)
  293. while f ~= goto_f do
  294. if f < goto_f then
  295. if f == 1 and goto_f == 4 then
  296. turtle.turnLeft()
  297. f = 4
  298. else
  299. turtle.turnRight()
  300. f = f + 1
  301. end
  302. else
  303. if f == 4 and goto_f == 1 then
  304. turtle.turnRight()
  305. f = 1
  306. else
  307. turtle.turnLeft()
  308. f = f - 1
  309. end
  310. end
  311. end
  312. end
  313.  
  314. function getLocation()
  315.  
  316. rednet.open("right")
  317. local x1, y1, z1 = gps.locate(5,false)
  318. if x == nil then
  319. print("out of GPS range")
  320. crash()
  321. end
  322. if forward() == false then
  323. crash()
  324. end
  325. local x2, y2, z2 = gps.locate(5,false)
  326. turnRight(2)
  327. if forward() == false then
  328. crash()
  329. end
  330. turnRight(2)
  331. rednet.close("right")
  332.  
  333. if y1 - y2 == -1 then
  334. f = 1
  335. elseif x1 - x2 == -1 then
  336. f = 2
  337. elseif y1 - y2 == 1 then
  338. f = 3
  339. elseif x1 - x2 == 1 then
  340. f = 4
  341. else
  342. print("ERROR: GPS wonkyness")
  343. crash()
  344. end
  345.  
  346. x = x1
  347. y = y1
  348. z = z1
  349. end
  350.  
  351. function readState() -- need to pcall this function so it doesn't break if it won't yield
  352. if fs.exists("MinerSavedData") == true then
  353. local file = fs.open("MinerSavedData","r")
  354. local data = file.readAll()
  355. file.close()
  356. local tablevar = {}
  357. tablevar = textutils.unserialize(data)
  358. min_x = tablevar["min_x"]
  359. max_x = tablevar["max_x"]
  360. mem_x = tablevar["mem_x"]
  361. home_x = tablevar["home_x"]
  362. ret_x = tablevar["ret_x"]
  363. min_y = tablevar["min_y"]
  364. max_y = tablevar["max_y"]
  365. mem_y = tablevar["mem_y"]
  366. home_y = tablevar["home_y"]
  367. ret_y = tablevar["ret_y"]
  368. min_z = tablevar["min_z"]
  369. max_z = tablevar["max_z"]
  370. mem_z = tablevar["mem_z"]
  371. home_z = tablevar["home_z"]
  372. ret_z = tablevar["ret_z"]
  373. mem_f = tablevar["mem_f"]
  374. home_f = tablevar["home_f"]
  375. ret_f = tablevar["ret_f"]
  376. continue = tablevar["continue"]
  377. x_forward = tablevar["x_forward"]
  378. y_forward = tablevar["y_forward"]
  379. turtle_objective = tablevar["turtle_objective"]
  380. new_floor = tablevar["new_floor"]
  381. end
  382. end
  383.  
  384. function updateMemory()
  385. mem_x = x
  386. mem_y = y
  387. mem_z = z
  388. mem_f = f
  389. end
  390.  
  391. function updateReturn()
  392. ret_x = x
  393. ret_y = y
  394. ret_z = z
  395. ret_f = f
  396. end
  397.  
  398. function writeState() -- need to pcall this function so it doesn't break if it won't yield
  399. local tablevar = {}
  400. tablevar["min_x"] = min_x
  401. tablevar["max_x"] = max_x
  402. tablevar["mem_x"] = mem_x
  403. tablevar["home_x"] = home_x
  404. tablevar["ret_x"] = ret_x
  405. tablevar["min_y"] = min_y
  406. tablevar["max_y"] = max_y
  407. tablevar["mem_y"] = mem_y
  408. tablevar["home_y"] = home_y
  409. tablevar["ret_y"] = ret_y
  410. tablevar["min_z"] = min_z
  411. tablevar["max_z"] = max_z
  412. tablevar["mem_z"] = mem_z
  413. tablevar["home_z"] = home_z
  414. tablevar["ret_z"] = ret_z
  415. tablevar["mem_f"] = mem_f
  416. tablevar["home_f"] = home_f
  417. tablevar["ret_f"] = ret_f
  418. tablevar["continue"] = continue
  419. tablevar["x_forward"] = x_forward
  420. tablevar["y_forward"] = y_forward
  421. tablevar["turtle_objective"] = turtle_objective
  422. tablevar["new_floor"] = new_floor
  423.  
  424. local file = fs.open("MinerSavedData","w")
  425. file.write(textutils.serialize(tablevar))
  426. file.close()
  427. end
  428.  
  429. function writeScheduler()
  430. writeSchedulerCounter = writeSchedulerCounter + 1
  431.  
  432. if writeSchedulerCounter == 10 then
  433. pcall(writeState)
  434. writeSchedulerCounter = 0
  435. end
  436.  
  437. end
  438.  
  439. function startup()
  440.  
  441. term.clear()
  442. term.setCursorPos(1, 1)
  443. pcall(readState)
  444.  
  445. if continue == true then
  446. print("existing instruction set found: (c)ontinue or (r)estart")
  447. while true do
  448. local event, character = os.pullEvent()
  449. if event == "char" and character == "r" then
  450. continue = false
  451. print("resetting instruction set")
  452. break
  453. end
  454. if event == "char" and character == "c" then
  455. print("Continuing...")
  456. break
  457. end
  458. end
  459. end
  460.  
  461. print("Using GPS to calculate location, please make sure nothing important is in front of me and press c to continue")
  462. while true do
  463. local event, character = os.pullEvent()
  464. if event == "char" and character == "c" then break end
  465. end
  466.  
  467. getLocation()
  468.  
  469. if continue == false then
  470.  
  471. term.clear()
  472. term.setCursorPos(1, 1)
  473.  
  474. print("Please set me up at the bottom left corner of the excavation with a chest for spoil behind me and a chest for fuel to my left")
  475. print("")
  476. print("am I ready to continue? Press c to begin")
  477.  
  478. while true do
  479. local event, character = os.pullEvent()
  480. if event == "char" and character == "c" then break end
  481. end
  482.  
  483. term.clear()
  484. term.setCursorPos(1, 1)
  485.  
  486. home_x = x
  487. home_y = y
  488. home_z = z
  489. home_f = f
  490.  
  491. if home_f == 1 then
  492. print("Width to dig?")
  493. min_x = x
  494. max_x = min_x + (tonumber(read()) - 1) --because its already at the 1st block
  495. print("length to dig (how far back)?")
  496. min_y = y
  497. max_y = min_y + (tonumber(read()) - 1)
  498. print("hight to dig? Please use an even number")
  499. min_z = z
  500. max_z = min_z + (tonumber(read()) - 1)
  501. continue = true
  502. x_forward = true
  503. y_forward = true
  504. new_floor = true
  505. turtle_objective = "mine"
  506. elseif home_f == 2 then
  507. print("Width to dig?")
  508. max_y = y
  509. min_y = max_y - (tonumber(read()) - 1)
  510. print("length to dig (how far back)?")
  511. min_x = x
  512. max_x = min_x + (tonumber(read()) - 1)
  513. print("hight to dig? Please use an even number")
  514. min_z = z
  515. max_z = min_z + (tonumber(read()) - 1)
  516. continue = true
  517. x_forward = true
  518. y_forward = false
  519. new_floor = true
  520. turtle_objective = "mine"
  521. elseif home_f == 3 then
  522. print("Width to dig?")
  523. max_x = x
  524. min_x = max_x - (tonumber(read()) - 1)
  525. print("length to dig (how far back)?")
  526. max_y = y
  527. min_y = max_y - (tonumber(read()) - 1)
  528. print("hight to dig? Please use an even number")
  529. min_z = z
  530. max_z = min_z + (tonumber(read()) - 1)
  531. continue = true
  532. x_forward = false
  533. y_forward = false
  534. new_floor = true
  535. turtle_objective = "mine"
  536. elseif home_f == 4 then
  537. print("Width to dig?")
  538. min_y = y
  539. max_y = min_y + (tonumber(read()) - 1)
  540. print("length to dig (how far back)?")
  541. max_x = x
  542. min_x = max_x - (tonumber(read()) - 1)
  543. print("hight to dig? Please use an even number")
  544. min_z = z
  545. max_z = min_z + (tonumber(read()) - 1)
  546. continue = true
  547. x_forward = false
  548. y_forward = true
  549. new_floor = true
  550. turtle_objective = "mine"
  551. else
  552. print("ERROR: home_f produced an incorrect value")
  553. crash()
  554. end
  555.  
  556. local skip_z = 0
  557. print("do you wish to skip to a certain hight?, even floors only and use 0 for no")
  558. skip_z = tonumber(read())
  559. if skip_z ~= 0 then
  560. local i = 0
  561. while i ~= skip_z do
  562. if up() == false then crash() end
  563. i = i + 1
  564. end
  565. end
  566.  
  567. updateMemory()
  568. writeState()
  569.  
  570. term.clear()
  571. term.setCursorPos(1, 1)
  572.  
  573. print("starting operation")
  574. textutils.slowPrint("************************************GO!")
  575. sleep(0.5)
  576.  
  577. term.clear()
  578. term.setCursorPos(1, 1)
  579.  
  580. if up() == false then crash() end
  581. updateMemory()
  582. end
  583. end
  584.  
  585. function checkFuel()
  586. local home_fuel = 20 -- bias added just to make sure
  587. if x > home_x then
  588. home_fuel = home_fuel + (x - home_x)
  589. else
  590. home_fuel = home_fuel + (home_x - x)
  591. end
  592. if y > home_y then
  593. home_fuel = home_fuel + (y - home_y)
  594. else
  595. home_fuel = home_fuel + (home_y - y)
  596. end
  597. if z > home_z then
  598. home_fuel = home_fuel + (z - home_z)
  599. else
  600. home_fuel = home_fuel + (home_z - z)
  601. end
  602.  
  603. print("DEBUG home_fuel:",home_fuel)
  604.  
  605. if turtle.getFuelLevel() < home_fuel then
  606. print("Fuel check failed")
  607. return false
  608. else
  609. return true
  610. end
  611. end
  612.  
  613. function checkInventory()
  614. local space = false
  615. local i = 1
  616. repeat
  617. if turtle.getItemCount(i) == 0 then
  618. space = true
  619. end
  620. i = i + 1
  621. until i == 17
  622. if space == false then
  623. print("Inventory check failed")
  624. end
  625. return space
  626. end
  627.  
  628. function unload()
  629. turnRel("backwards")
  630. local i = 1
  631. repeat
  632. turtle.select(i)
  633. if turtle.drop() == false then
  634. turtle.dropDown()
  635. end
  636. i = i + 1
  637. until i == 17
  638. turtle.select(1)
  639.  
  640. if turtle.getFuelLevel() < 500 then
  641. turnRel("left")
  642. turtle.select(1)
  643. if turtle.suck() == false then
  644. print("ERROR: no Fuel")
  645. crash()
  646. end
  647. turtle.refuel()
  648.  
  649. turnRel("backwards")
  650. turtle.select(1)
  651. if turtle.drop() == false then
  652. turtle.dropDown()
  653. end
  654. end
  655. end
  656.  
  657. function mineXStrip()
  658. if x_forward == true then
  659. turn(2)
  660. repeat
  661. if forwardMine() == false then crash() end
  662. updateMemory()
  663. if checkFuel() == false or checkInventory() == false then
  664. updateReturn()
  665. turtle_objective = "home"
  666. gotoHome()
  667. unload()
  668. gotoReturn()
  669. turtle_objective = "mine"
  670. end
  671. until x == max_x
  672. x_forward = false
  673. return
  674. elseif x_forward == false then
  675. turn(4)
  676. repeat
  677. if forwardMine() == false then crash() end
  678. updateMemory()
  679. if checkFuel() == false or checkInventory() == false then
  680. updateReturn()
  681. turtle_objective = "home"
  682. gotoHome()
  683. unload()
  684. gotoReturn()
  685. turtle_objective = "mine"
  686. end
  687. until x == min_x
  688. x_forward = true
  689. return
  690. else
  691. print("ERROR: incorrect x_forward value")
  692. crash()
  693. end
  694. end
  695.  
  696. function gotoMemory()
  697. while x ~= mem_x do
  698. if x > mem_x then
  699. turn(4)
  700. if forward() == false then crash() end
  701. elseif x < mem_x then
  702. turn(2)
  703. if forward() == false then crash() end
  704. end
  705. end
  706. while y ~= mem_y do
  707. if y > mem_y then
  708. turn(3)
  709. if forward() == false then crash() end
  710. elseif y < mem_y then
  711. turn(1)
  712. if forward() == false then crash() end
  713. end
  714. end
  715. while z ~= mem_z do
  716. if z > mem_z then
  717. if down() == false then crash() end
  718. elseif z < mem_z then
  719. if up() == false then crash() end
  720. end
  721. end
  722. turn(mem_f)
  723. end
  724.  
  725. function gotoHome()
  726. while z ~= home_z do
  727. if z > home_z then
  728. if down() == false then crash() end
  729. elseif z < home_z then
  730. if up() == false then crash() end
  731. end
  732. end
  733. while y ~= home_y do
  734. if y > home_y then
  735. turn(3)
  736. if forward() == false then crash() end
  737. elseif y < home_y then
  738. turn(1)
  739. if forward() == false then crash() end
  740. end
  741. end
  742. while x ~= home_x do
  743. if x > home_x then
  744. turn(4)
  745. if forward() == false then crash() end
  746. elseif x < home_x then
  747. turn(2)
  748. if forward() == false then crash() end
  749. end
  750. end
  751. end
  752.  
  753. function gotoReturn()
  754. while x ~= ret_x do
  755. if x > ret_x then
  756. turn(4)
  757. if forward() == false then crash() end
  758. elseif x < ret_x then
  759. turn(2)
  760. if forward() == false then crash() end
  761. end
  762. end
  763. while y ~= ret_y do
  764. if y > ret_y then
  765. turn(3)
  766. if forward() == false then crash() end
  767. elseif y < ret_y then
  768. turn(1)
  769. if forward() == false then crash() end
  770. end
  771. end
  772. while z ~= ret_z do
  773. if z > ret_z then
  774. if down() == false then crash() end
  775. elseif z < ret_z then
  776. if up() == false then crash() end
  777. end
  778. end
  779. turn(ret_f)
  780. end
  781.  
  782. ------------------
  783. -- Main Program --
  784. ------------------
  785.  
  786. local testmode = false
  787. if testmode == false then
  788.  
  789. startup()
  790.  
  791. if turtle_objective == "home" then
  792. gotoHome()
  793. unload()
  794. gotoMemory()
  795. turtle_objective = "mine"
  796. end
  797.  
  798. gotoMemory()
  799. new_floor = true
  800.  
  801. repeat
  802. if new_floor == false then
  803. if up() == false then crash() end
  804. if up() == false then crash() end
  805. new_floor = true
  806. updateMemory()
  807. end
  808.  
  809. if y_forward == true then
  810. repeat
  811. if new_floor == false then
  812. turn(1)
  813. if forwardMine() == false then crash() end
  814. updateMemory()
  815. end
  816. mineXStrip()
  817. new_floor = false
  818. until y == max_y
  819. y_forward = false
  820. elseif y_forward == false then
  821. repeat
  822. if new_floor == false then
  823. turn(3)
  824. if forwardMine() == false then crash() end
  825. updateMemory()
  826. end
  827. mineXStrip()
  828. new_floor = false
  829. until y == min_y
  830. y_forward = true
  831. else
  832. print("ERROR incorrect y_forward value")
  833. crash()
  834. end
  835.  
  836. until z == max_z
  837.  
  838. gotoHome()
  839. continue = false
  840. pcall(writeState)
  841.  
  842. term.clear()
  843. term.setCursorPos(1, 1)
  844. print("All Done")
  845.  
  846. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement