le_Fish

WheatFarm

Jul 28th, 2019
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.18 KB | None | 0 0
  1. local switch_slot = 1
  2. local blocksLeft, blocksRight, blocksForward = 0, 0, 0
  3. local x, y, z, direction
  4. local home_x, home_y, home_z, home_direction
  5. local currentSide = "left"
  6.  
  7. local task = "start_wheatfarming"
  8. local seeds_chest1, seeds_chest2, harvest_chest, planted, harvested = 0,0,0,0,0
  9.  
  10. -- [MAIN FUNCTIONS]
  11. function start_wheatfarming()
  12. while true do
  13. change_task("start_wheatfarming")
  14. if currentSide == "left" then
  15. fixInv()
  16. start_left()
  17. elseif currentSide == "right" then
  18. fixInv()
  19. start_right()
  20. end
  21. change_side()
  22. home()
  23. emptyInv()
  24. iddle()
  25. end
  26. end
  27.  
  28. function left_side()
  29. change_task("left_side")
  30. printInfo()
  31. plant()
  32. if home_direction == "S" then
  33. repeat
  34. if direction == "E" then
  35. repeat forward() plant() until x == home_x + blocksLeft
  36. change_backwards()
  37. elseif direction == "W" then
  38. repeat forward() plant() until x == home_x - 1
  39. change_backwards()
  40. end
  41. until y == home_y + blocksForward
  42. if direction == "E" then
  43. repeat forward() plant() until x == home_x + blocksLeft
  44. elseif direction == "W" then
  45. repeat forward() plant() until x == home_x - 1
  46. end
  47. elseif home_direction == "N" then
  48. repeat
  49. if direction == "W" then
  50. repeat forward() plant() until x == home_x - blocksLeft
  51. change_backwards()
  52. elseif direction == "E" then
  53. repeat forward() plant() until x == home_x - 1
  54. change_backwards()
  55. end
  56. until y == home_y - blocksForward
  57. if direction == "W" then
  58. repeat forward() plant() until x == home_x - blocksLeft
  59. elseif direction == "E" then
  60. repeat forward() plant() until x == home_x - 1
  61. end
  62. elseif home_direction == "W" then -- WEST SIDE
  63. repeat
  64. if direction == "S" then
  65. repeat forward() plant() until y == home_y + blocksLeft
  66. change_backwards()
  67. elseif direction == "N" then
  68. repeat forward() plant() until y == home_y + 1
  69. change_backwards()
  70. end
  71. until x == home_x - blocksForward
  72. if direction == "S" then
  73. repeat forward() plant() until y == home_y + blocksLeft
  74. elseif direction == "N" then
  75. repeat forward() plant() until y == home_y + 1
  76. end
  77. elseif home_direction == "E" then -- EAST SIDE
  78. repeat
  79. if direction == "N" then
  80. repeat forward() plant() until y == home_y - blocksLeft
  81. change_backwards()
  82. elseif direction == "S" then
  83. repeat forward() plant() until y == home_y - 1
  84. change_backwards()
  85. end
  86. until x == home_x + blocksForward
  87. if direction == "N" then
  88. repeat forward() plant() until y == home_y - blocksLeft
  89. elseif direction == "S" then
  90. repeat forward() plant() until y >= home_y - 1
  91. end
  92. end
  93. end
  94.  
  95. function right_side()
  96. change_task("right_side")
  97. printInfo()
  98. plant()
  99. if home_direction == "S" then
  100. repeat
  101. if direction == "W" then
  102. repeat forward() plant() until x == home_x - blocksRight
  103. change_backwards()
  104. elseif direction == "E" then
  105. repeat forward() plant() until x == home_x
  106. change_backwards()
  107. end
  108. until y == home_y + blocksForward
  109. if direction == "W" then
  110. repeat forward() plant() until x == home_x - blocksRight
  111. elseif direction == "E" then
  112. repeat forward() plant() until x == home_x
  113. end
  114. elseif home_direction == "N" then
  115. repeat
  116. if direction == "E" then
  117. repeat forward() plant() until x == home_x + blocksRight
  118. change_backwards()
  119. elseif direction == "W" then
  120. repeat forward() plant() until x == home_x
  121. change_backwards()
  122. end
  123. until y == home_y - blocksForward
  124. if direction == "E" then
  125. repeat forward() plant() until x == home_x + blocksRight
  126. elseif direction == "W" then
  127. repeat forward() plant() until x == home_x
  128. end
  129. elseif home_direction == "W" then
  130. repeat
  131. if direction == "N" then
  132. repeat forward() plant() until y == home_y - blocksRight
  133. change_backwards()
  134. elseif direction == "S" then
  135. repeat forward() plant() until y == home_y
  136. change_backwards()
  137. end
  138. until x == home_x - blocksForward
  139. if direction == "N" then
  140. repeat forward() plant() until y == home_y - blocksRight
  141. elseif direction == "S" then
  142. repeat forward() plant() until y == home_y
  143. end
  144. elseif home_direction == "E" then
  145. repeat
  146. if direction == "S" then
  147. repeat forward() plant() until y == home_y + blocksRight
  148. change_backwards()
  149. elseif direction == "N" then
  150. repeat forward() plant() until y == home_y
  151. change_backwards()
  152. end
  153. until x == home_x + blocksForward
  154. if direction == "S" then
  155. repeat forward() plant() until y == home_y + blocksRight
  156. elseif direction == "N" then
  157. repeat forward() plant() until y == home_y
  158. end
  159. end
  160. end
  161.  
  162. function home()
  163. change_task("home")
  164. if z ~= home_z then
  165. if z > home_z then
  166. repeat
  167. down()
  168. until z == home_z
  169. elseif z < home_z then
  170. repeat
  171. up()
  172. until z == home_z
  173. end
  174. end
  175. if home_x ~= x or home_y ~= y then
  176. if home_direction == "N" or home_direction == "S" then
  177. if x < home_x then
  178. while(direction ~= "E") do right() end
  179. elseif x > home_x then
  180. while(direction ~= "W") do right() end
  181. end
  182. while(x ~= home_x) do
  183. forward()
  184. end
  185.  
  186. if home_direction == "N" then
  187. while(direction ~= "S") do right() end
  188. elseif home_direction == "S" then
  189. while(direction ~= "N") do right() end
  190. end
  191. while(y ~= home_y) do
  192. forward()
  193. end
  194.  
  195. left()
  196. left()
  197.  
  198. -- W AND E
  199. elseif home_direction == "W" or home_direction == "E" then
  200. if y > home_y then
  201. while(direction ~= "N") do right() end
  202. elseif y < home_y then
  203. while(direction ~= "S") do right() end
  204. end
  205. while(y ~= home_y) do
  206. forward()
  207. end
  208.  
  209. if home_direction == "W" then
  210. while(direction ~= "E") do right() end
  211. elseif home_direction == "E" then
  212. while(direction ~= "W") do right() end
  213. end
  214. while(x ~= home_x) do
  215. forward()
  216. end
  217.  
  218. while(direction ~= home_direction) do
  219. left()
  220. end
  221. end
  222. else
  223. while(direction ~= home_direction) do
  224. left()
  225. end
  226. end
  227. end
  228.  
  229. function emptyInv()
  230. change_task("emptyInv")
  231. local empty_wheat = true
  232. local empty_seeds = true
  233. local empty_seeds2 = true
  234.  
  235. local total_seeds_counter = 0
  236.  
  237. for i=16,2,-1 do
  238. turtle.select(i)
  239. if turtle.getItemCount(i) ~= 0 then
  240. local data = turtle.getItemDetail(i)
  241. if data and string.find(data.name, "wheat_seeds") then
  242. total_seeds_counter = total_seeds_counter + turtle.getItemCount(i)
  243. end
  244. end
  245. end
  246.  
  247. left()
  248. forward()
  249.  
  250. local left_seeds_count = math.floor(total_seeds_counter / 2)
  251.  
  252. for i=16,2,-1 do
  253. turtle.select(i)
  254. if turtle.getItemCount(i) ~= 0 then
  255. local data = turtle.getItemDetail(i)
  256. local seeds = turtle.getItemCount(i)
  257. if data and string.find(data.name, "wheat_seeds") then
  258. if left_seeds_count > turtle.getItemCount(i) then
  259. local turtle_slot_seeds = turtle.getItemCount(i)
  260. if turtle.dropDown() == false then
  261. empty_seeds = false
  262. else
  263. seeds_chest1 = seeds_chest1 + seeds
  264. left_seeds_count = left_seeds_count - turtle_slot_seeds
  265. end
  266. else
  267. repeat
  268. if turtle.dropDown(left_seeds_count) == false then
  269. empty_seeds = false
  270. else
  271. left_seeds_count = left_seeds_count - left_seeds_count
  272. seeds_chest1 = seeds_chest1 + 1
  273. end
  274. until turtle.getItemDetail(i) == 0 or left_seeds_count == 0 or empty_seeds == false
  275. end
  276. end
  277. end
  278. end
  279.  
  280. back()
  281. back()
  282.  
  283. for i=16,2,-1 do
  284. turtle.select(i)
  285. if turtle.getItemCount(i) ~= 0 then
  286. local data = turtle.getItemDetail(i)
  287. local seeds = turtle.getItemCount(i)
  288. if data and string.find(data.name, "wheat_seeds") then
  289. if turtle.dropDown() == false then
  290. empty_seeds2 = false
  291. else
  292. seeds_chest2 = seeds_chest2 + 1
  293. end
  294. end
  295. end
  296. end
  297.  
  298. forward()
  299. right()
  300.  
  301. -- empty wheat
  302. for i=16,2,-1 do
  303. turtle.select(i)
  304. if turtle.getItemCount(i) ~= 0 then
  305. local data = turtle.getItemDetail(i)
  306. local wheat = turtle.getItemCount(i)
  307. if data then
  308. if turtle.dropDown() == false then
  309. empty_wheat = false
  310. else
  311. harvest_chest = harvest_chest + wheat
  312. end
  313. end
  314. end
  315. end
  316.  
  317. if empty_seeds2 == false and empty_wheat == false then
  318. clearScreen()
  319. print("Second seed chest and wheat chest are full.")
  320. print("")
  321. print("Press enter when the problem is fixed")
  322. local chest_full = false
  323. while (chest_full == false) do
  324. local event, param = os.pullEvent()
  325. if event == "key" and param == 28 then -- key 28 is the 'enter' key
  326. chest_full = true
  327. end
  328. end
  329. emptyInv()
  330. elseif empty_wheat == false then
  331. clearScreen()
  332. print("Wheat chest is full.")
  333. print("")
  334. print("Press enter when the problem is fixed")
  335. local chest_full = false
  336. while (chest_full == false) do
  337. local event, param = os.pullEvent()
  338. if event == "key" and param == 28 then -- key 28 is the 'enter' key
  339. chest_full = true
  340. end
  341. end
  342. emptyInv()
  343. end
  344. saveCounters()
  345.  
  346. end
  347.  
  348. function iddle()
  349. change_task("iddle")
  350. local sleep_timer = math.random(100,300)
  351.  
  352. while sleep_timer > 0 do
  353. sleep_timer = sleep_timer - 1
  354. printIddleInfo(sleep_timer)
  355. sleep(1)
  356. end
  357. end
  358.  
  359. function fixInv()
  360. change_task("fixInv")
  361. printInfo()
  362.  
  363. left()
  364. forward()
  365.  
  366. local seeds_needed = 0
  367.  
  368. if currentSide == "left" then
  369. seeds_needed = math.floor((blocksLeft * blocksForward)/100*25)
  370. else
  371. seeds_needed = math.floor(((blocksRight + 1) * blocksForward)/100*25)
  372. end
  373.  
  374. local total_seeds_needed = seeds_needed
  375.  
  376. turtle.select(2)
  377. local this_selected_slot = 2
  378. while seeds_needed > 1 do
  379. turtle.select(this_selected_slot)
  380. if seeds_needed > 64 then
  381. turtle.suckDown()
  382. seeds_needed = seeds_needed - seeds_needed
  383. elseif seeds_needed ~= 0 then
  384. turtle.suckDown(seeds_needed)
  385. seeds_needed = seeds_needed - seeds_needed
  386. end
  387. this_selected_slot = this_selected_slot + 1
  388. if this_selected_slot > 16 then
  389. break
  390. end
  391. end
  392.  
  393. local total_seeds_counter = 0
  394.  
  395. for i=16,2,-1 do
  396. turtle.select(i)
  397. if turtle.getItemCount(i) ~= 0 then
  398. local data = turtle.getItemDetail(i)
  399. if data and string.find(data.name, "wheat_seeds") then
  400. total_seeds_counter = total_seeds_counter + turtle.getItemCount(i)
  401. end
  402. end
  403. end
  404.  
  405.  
  406. for i=16,2,-1 do
  407. if total_seeds_counter > total_seeds_needed then
  408. turtle.select(i)
  409. if turtle.getItemCount(i) ~= 0 then
  410. if (total_seeds_counter - total_seeds_needed) > 64 then
  411. turtle.dropDown(64)
  412. total_seeds_counter = total_seeds_counter - 64
  413. elseif (total_seeds_counter - total_seeds_needed) > 0 then
  414. turtle.dropDown(total_seeds_counter - total_seeds_needed)
  415. total_seeds_counter = total_seeds_counter - total_seeds_needed
  416. end
  417. end
  418. end
  419. end
  420.  
  421. back()
  422. right()
  423. end
  424.  
  425.  
  426. -- [SUB FUNCTIONS]
  427.  
  428. function change_side()
  429. if currentSide == "left" then
  430. currentSide = "right"
  431. elseif currentSide == "right" then
  432. currentSide = "left"
  433. end
  434. local h = fs.open("wheatfarmFolder/currentSide", "w")
  435. h.writeLine(currentSide)
  436. h.close()
  437. end
  438.  
  439. function change_backwards()
  440. clearScreen()
  441.  
  442. if home_direction == "N" then
  443. if direction == "W" then
  444. changeRight()
  445. elseif direction == "E" then
  446. changeLeft()
  447. end
  448. elseif home_direction == "S" then
  449. if direction == "E" then
  450. changeRight()
  451. elseif direction == "W" then
  452. changeLeft()
  453. end
  454. elseif home_direction == "W" then
  455. if direction == "S" then
  456. changeRight()
  457. elseif direction == "N" then
  458. changeLeft()
  459. end
  460. elseif home_direction == "E" then
  461. if direction == "N" then
  462. changeRight()
  463. elseif direction == "S" then
  464. changeLeft()
  465. end
  466. end
  467. plant()
  468. end
  469.  
  470. function start_left()
  471. forward()
  472. left()
  473. forward()
  474. left_side()
  475. end
  476.  
  477. function start_right()
  478. forward()
  479. right()
  480. right_side()
  481. end
  482.  
  483. function changeLeft()
  484. left()
  485. forward()
  486. left()
  487. end
  488.  
  489. function changeRight()
  490. right()
  491. forward()
  492. right()
  493. end
  494.  
  495. function change_task(new_task)
  496. task = new_task
  497. local h = fs.open("wheatfarmFolder/task", "w")
  498. h.writeLine(task)
  499. h.close()
  500. end
  501. -- [FARM FUNCTIONS]
  502.  
  503. function plant()
  504. local slot = 2
  505.  
  506. local success, plant_data = turtle.inspectDown()
  507. if success and string.find(plant_data.name, "wheat") and plant_data.metadata == 7 then
  508. turtle.select(2)
  509. if turtle.digDown() then
  510. harvested = harvested + 1
  511. end
  512. for i=16,2,-1 do
  513. local p = turtle.getItemDetail(i)
  514. if p and string.find(p.name, "seed") ~= nil then
  515. slot = i
  516. end
  517. end
  518. turtle.select(slot)
  519. if turtle.placeDown() then
  520. planted = planted + 1
  521. end
  522. elseif not success then
  523. turtle.select(2)
  524. turtle.digDown()
  525. for i=16,2,-1 do
  526. local p = turtle.getItemDetail(i)
  527. if p and string.find(p.name, "seed") ~= nil then
  528. slot = i
  529. end
  530. end
  531. turtle.select(slot)
  532. if turtle.placeDown() then
  533. planted = planted + 1
  534. end
  535. end
  536. turtle.select(2)
  537. saveCounters()
  538. end
  539.  
  540. -- [TURTLE FUNCTIONS]
  541.  
  542. function forward()
  543. if turtle.forward() then
  544. if direction == "W" then
  545. x = x - 1
  546. elseif direction == "E" then
  547. x = x + 1
  548. elseif direction == "S" then
  549. y = y + 1
  550. elseif direction == "N" then
  551. y = y - 1
  552. end
  553. end
  554. printInfo()
  555. end
  556.  
  557. function back()
  558. if turtle.back() then
  559. if direction == "W" then
  560. x = x + 1
  561. elseif direction == "E" then
  562. x = x - 1
  563. elseif direction == "S" then
  564. y = y - 1
  565. elseif direction == "N" then
  566. y = y + 1
  567. end
  568. end
  569. printInfo()
  570. end
  571.  
  572. function left()
  573. turtle.turnLeft()
  574. if direction == "N" then
  575. direction = "W"
  576. elseif direction == "W" then
  577. direction = "S"
  578. elseif direction == "S" then
  579. direction = "E"
  580. elseif direction == "E" then
  581. direction = "N"
  582. end
  583. printInfo()
  584. end
  585.  
  586. function right()
  587. turtle.turnRight()
  588. if direction == "N" then
  589. direction = "E"
  590. elseif direction == "E" then
  591. direction = "S"
  592. elseif direction == "S" then
  593. direction = "W"
  594. elseif direction == "W" then
  595. direction = "N"
  596. end
  597. printInfo()
  598. end
  599.  
  600. function down()
  601. turtle.down()
  602. z = z - 1
  603. end
  604.  
  605. function up()
  606. turtle.up()
  607. z = z + 1
  608. end
  609.  
  610.  
  611. -- [OTHER FUNCTIONS]
  612.  
  613. function saveCounters()
  614. local h = fs.open("wheatfarmFolder/harvested", "w")
  615. h.writeLine(seeds_chest1)
  616. h.writeLine(seeds_chest2)
  617. h.writeLine(harvest_chest)
  618. h.writeLine(planted)
  619. h.writeLine(harvested)
  620. h.close()
  621. end
  622.  
  623. function init()
  624. if fs.exists("wheatfarmFolder") then
  625. print("reading first input, probably a crash occured")
  626. print("---------------------------------------------")
  627. print("Reading first input")
  628. local h = fs.open("wheatfarmFolder/firstInput", "r")
  629. blocksLeft = tonumber(h.readLine())
  630. blocksRight = tonumber(h.readLine())
  631. blocksForward = tonumber(h.readLine())
  632. h.close()
  633.  
  634. print("Reading currentSide and task variable")
  635. local h = fs.open("wheatfarmFolder/currentSide", "r")
  636. currentSide = h.readLine()
  637. h.close()
  638.  
  639. local h = fs.open("wheatfarmFolder/task", "r")
  640. task = h.readLine()
  641. h.close()
  642.  
  643. print("Reading counter variables")
  644. local h = fs.open("wheatfarmFolder/harvested", "r")
  645. seeds_chest1 = tonumber(h.readLine())
  646. seeds_chest2 = tonumber(h.readLine())
  647. harvest_chest = tonumber(h.readLine())
  648. planted = tonumber(h.readLine())
  649. harvested = tonumber(h.readLine())
  650. h.close()
  651.  
  652. print("Reading home position")
  653. local h = fs.open("wheatfarmFolder/home", "r")
  654. home_x = tonumber(h.readLine())
  655. home_y = tonumber(h.readLine())
  656. home_z = tonumber(h.readLine())
  657. home_direction = h.readLine()
  658. h.close()
  659.  
  660. print("Checking my x y z coordinates and direction")
  661. checkGps()
  662.  
  663. print(" ")
  664. print("My X: " .. x)
  665. print("My Y: " .. y)
  666. print("My Z: " .. z)
  667. print("My direction: " .. direction)
  668. print("")
  669. print("Home X: " .. home_x)
  670. print("Home Y: " .. home_y)
  671. print("Home Z: " .. home_z)
  672. print("Home direction: " .. home_direction)
  673. print("")
  674. print("Task: " .. task)
  675. sleep(2)
  676. printInfo()
  677.  
  678. if task == "emptyInv" then
  679. home()
  680. emptyInv()
  681. fixInv()
  682. start_wheatfarming()
  683. elseif task == "fixInv" then
  684. home()
  685. fixInv()
  686. start_wheatfarming()
  687. elseif task == "start_wheatfarming" then
  688. start_wheatfarming()
  689. elseif task == "left_side" then
  690. start_left()
  691. change_side()
  692. home()
  693. emptyInv()
  694. -- right side
  695. fixInv()
  696. start_right()
  697. change_side()
  698. home()
  699. emptyInv()
  700. start_wheatfarming()
  701. elseif task == "right_side" then
  702. start_right()
  703. change_side()
  704. home()
  705. emptyInv()
  706. start_wheatfarming()
  707. elseif task == "iddle" then
  708. home()
  709. fixInv()
  710. start_wheatfarming()
  711. elseif task == "home" then
  712. home()
  713. fixInv()
  714. start_wheatfarming()
  715. end
  716.  
  717. else
  718. startup()
  719. end
  720. end
  721.  
  722. function startup()
  723. clearScreen()
  724. print("Place a chest to the left, under me and to the right")
  725. print("Chest to the left will be filled with seeds till it's full")
  726. print("Chest under me will be filled with wheat")
  727. print("Chest to the right will be filled with seeds when the first one is full")
  728. print(" ")
  729. print(" ")
  730. print("Press enter when chest setup is done")
  731. local chest_setup = false
  732. while (chest_setup == false) do
  733. local event, param = os.pullEvent()
  734. if event == "key" and param == 28 then -- key 28 is the 'enter' key
  735. chest_setup = true
  736. end
  737. sleep(0.2)
  738. end
  739.  
  740. clearScreen()
  741.  
  742. print("Put a modem in slot 1")
  743. repeat
  744. local data = turtle.getItemDetail(switch_slot)
  745. sleep(0.2)
  746. until data ~= nil and string.find(data.name, "ComputerCraft")
  747.  
  748. clearScreen()
  749.  
  750. print("How many blocks can I go left?")
  751. blocksLeft = tonumber(read())
  752. print(" ")
  753. print("How many blocks can I go right?")
  754. blocksRight = tonumber(read())
  755. print(" ")
  756. print("How many blocks can I go forward?")
  757. blocksForward = tonumber(read())
  758. print(" ")
  759.  
  760. clearScreen()
  761.  
  762. print("Is this the right input: ")
  763. print("Left space: " .. blocksLeft)
  764. print("Right space: " .. blocksRight)
  765. print("Length: : " .. blocksForward)
  766. print("Total field: " .. (blocksLeft + blocksRight + 1)*blocksForward)
  767. print(" ")
  768. print(" ")
  769. print("Press enter if it's correct, otherwhise: Ctrl+R for reboot")
  770.  
  771. local rightInput = false
  772. while (rightInput == false) do
  773. local event, param = os.pullEvent()
  774. if event == "key" and param == 28 then -- key 28 is the 'enter' key
  775. rightInput = true
  776. end
  777. sleep(0.2)
  778. end
  779.  
  780. clearScreen()
  781. print("Saving settings")
  782. print("---------------")
  783. print("Making folder")
  784. fs.makeDir("wheatfarmFolder")
  785.  
  786. print("Saving first input")
  787. local h = fs.open("wheatfarmFolder/firstInput", "w")
  788. h.writeLine(blocksLeft)
  789. h.writeLine(blocksRight)
  790. h.writeLine(blocksForward)
  791. h.close()
  792.  
  793. print("Saving currentSide and task variable")
  794. local h = fs.open("wheatfarmFolder/currentSide", "w")
  795. h.writeLine(currentSide)
  796. h.close()
  797. local h = fs.open("wheatfarmFolder/task", "w")
  798. h.writeLine("start_wheatfarming")
  799. h.close()
  800.  
  801. print("Saving counter variables")
  802. local h = fs.open("wheatfarmFolder/harvested", "w")
  803. h.writeLine(seeds_chest1)
  804. h.writeLine(seeds_chest2)
  805. h.writeLine(harvest_chest)
  806. h.writeLine(planted)
  807. h.writeLine(harvested)
  808. h.close()
  809.  
  810. print("Checking my x y z coordinates and direction")
  811. checkGps()
  812. print("Saving home position")
  813. local h = fs.open("wheatfarmFolder/home", "w")
  814. h.writeLine(x)
  815. h.writeLine(y)
  816. h.writeLine(z)
  817. h.writeLine(direction)
  818. h.close()
  819.  
  820. home_direction = direction
  821.  
  822. print(" ")
  823. print("My X: " .. x)
  824. print("My Y: " .. y)
  825. print("My Z: " .. z)
  826. print("My direction: " .. direction)
  827. print("")
  828.  
  829. home_x = x
  830. home_y = y
  831. home_z = z
  832.  
  833. print("Making startup file")
  834. if fs.exists("startup") then
  835. fs.delete("startup")
  836. local h = fs.open("startup", "w")
  837. h.writeLine("sleep(5)")
  838. h.writeLine("shell.run('" .. shell.getRunningProgram() .. "')")
  839. h.close()
  840. else
  841. h.writeLine("sleep(5)")
  842. local h = fs.open("startup", "w")
  843. h.writeLine("shell.run('" .. shell.getRunningProgram() .. "')")
  844. h.close()
  845. end
  846.  
  847. print("Starting with wheat farm")
  848. start_wheatfarming()
  849. end
  850.  
  851. function printInfo()
  852. clearScreen()
  853. print("X: " .. x )
  854. print("Y: " .. y)
  855. print("Z: " .. z)
  856. print("Facing: " .. direction)
  857. print("--------------------------------")
  858. if task == "start_wheatfarming" then
  859. print("Currently starting up to farm")
  860. elseif task == "left_side" then
  861. print("Currently farming the left side of the farm")
  862. elseif task == "right_side" then
  863. print("Currently farming the right side of the farm")
  864. elseif task == "emptyInv" then
  865. print("Currenlty emptying inventory")
  866. elseif task == "home" then
  867. print("Going to home position")
  868. elseif task == "fixInv" then
  869. print("Fixing inventory")
  870. end
  871. print("--------------------------------")
  872. print("Home X: " .. home_x)
  873. print("Home Y: " .. home_y)
  874. print("Home Z: " .. home_z)
  875. print("Home direction: " .. home_direction)
  876.  
  877. end
  878.  
  879. function printIddleInfo(sleep_timer)
  880. clearScreen()
  881. print("Waiting " .. sleep_timer .. " seconds to start farming again")
  882. print("---------------------------------------")
  883. print("Seeds put in left chest: " .. seeds_chest1)
  884. print("Seeds put in right chest: " .. seeds_chest2)
  885. print("Wheat put in center chest: " .. harvest_chest)
  886. print("Wheat planted: " .. planted)
  887. print("Wheat harvested: " .. harvested)
  888. print("")
  889. print("(CTRL+R) Reboot to start farming.")
  890.  
  891. end
  892.  
  893. function clearScreen()
  894. term.clear()
  895. term.setCursorPos(1,1)
  896. end
  897.  
  898. function checkGps()
  899. -- switch modem and check if slot is now a hoe
  900. turtle.select(switch_slot)
  901. local data = turtle.getItemDetail(switch_slot)
  902. if data == nil then
  903. print("Put a modem in slot 1")
  904. repeat
  905. data = turtle.getItemDetail(switch_slot)
  906. sleep(0.2)
  907. until data ~= nil and string.find(data.name, "ComputerCraft")
  908. elseif string.find(data.name, "ComputerCraft") == nil then
  909. turtle.equipLeft()
  910. data = turtle.getItemDetail(switch_slot)
  911. if string.find(data.name, "ComputerCraft") == nil then
  912. turtle.equipLeft()
  913. print("Put a modem in slot 1")
  914. repeat
  915. data = turtle.getItemDetail(switch_slot)
  916. sleep(0.2)
  917. until data ~= nil and string.find(data.name, "ComputerCraft")
  918. end
  919. end
  920.  
  921. turtle.equipLeft()
  922. data = turtle.getItemDetail(switch_slot)
  923. if data == nil or string.find(data.name, "hoe") == nil then
  924. print("Put a diamond hoe in slot 1")
  925. repeat
  926. data = turtle.getItemDetail(switch_slot)
  927. sleep(0.2)
  928. until data ~= nil and string.find(data.name, "hoe")
  929. end
  930.  
  931. -- check gps
  932. gps_locate()
  933. turtle.equipLeft()
  934. end
  935.  
  936. function gps_locate()
  937. local new_x, new_y, new_z = gps.locate(5)
  938. if not new_x then
  939. gps_locate_broken()
  940. else
  941. x = math.floor(new_x)
  942. y = math.floor(new_y)
  943. z = math.floor(new_z)
  944. end
  945. -- check direction
  946. if turtle.forward() == true then
  947. local second_x, second_y, second_z = gps.locate(5)
  948. if not second_x then
  949. gps_locate_broken()
  950. else
  951. if math.floor(second_y) ~= y then
  952. if y < math.floor(second_y) then
  953. direction = "S"
  954. else
  955. direction = "N"
  956. end
  957. elseif math.floor(second_x) ~= x then
  958. if x > math.floor(second_x) then
  959. direction = "W"
  960. else
  961. direction = "E"
  962. end
  963. end
  964. turtle.back()
  965. end
  966. elseif turtle.back() == true then
  967. local second_x, second_y, second_z = gps.locate(5)
  968. if not second_x then
  969. gps_locate_broken()
  970. else
  971. if math.floor(second_y) ~= y then
  972. if y > math.floor(second_y) then
  973. direction = "S"
  974. else
  975. direction = "N"
  976. end
  977. elseif math.floor(second_x) ~= x then
  978. if x < math.floor(second_x) then
  979. direction = "W"
  980. else
  981. direction = "E"
  982. end
  983. end
  984. turtle.back()
  985. end
  986. end
  987.  
  988. end
  989.  
  990. function gps_locate_broken()
  991. clearScreen()
  992. print("Failed to get my location!")
  993. print("Press enter to retry")
  994. local checked_gps = false
  995. while (checked_gps == false) do
  996. local event, param = os.pullEvent()
  997. if event == "key" and param == 28 then -- key 28 is the 'enter' key
  998. checked_gps = true
  999. end
  1000. second_x, second_y, second_z = gps.locate(5)
  1001. if second_x then
  1002. checked_gps = true
  1003. end
  1004. sleep(0.2)
  1005. end
  1006. clearScreen()
  1007. end
  1008.  
  1009. init()
Advertisement
Add Comment
Please, Sign In to add comment