nekosune

Untitled

Jun 25th, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.52 KB | None | 0 0
  1. --[[
  2. TreeFarmer
  3.  
  4. by Velander
  5.  
  6. Version 4.1.1
  7.  
  8. This program will plant a grove of trees, fertilizing as it goes, then will return to each tree and harvest it if it has grown and collect the saplings. The saplings and anything else dropped by the harvested trees will be collected. The saplings collected will be used to replant and extra will placed in the chest containing the sapling. Everything else that is collected will be placed in an inventory chest.
  9.  
  10. If the turtle runs into something not expected, it will report that it was blocked then destroy it. If the supply of fuel in the turtle drops to 1 then it reports that it is out of fuel and wait to be refueled. If the supply of saplings drops to 1 it will contintreeue to harvest trees and collect more saplings. It will never use the last sapling so it has something to compare with.
  11.  
  12. You can start the tree farm with two or more saplings or a single sapling and an existing tree. It will harvest the trees that it finds and collect saplings, and then continue to use those saplings to populate the tree farm.
  13.  
  14. At the end of each transit through the tree farm the excess saplings will be placed in the sapling chest. If the sapling chest becomes full, it will report that problem and wait for the chest to be emptied. It will also deposit any wood and other items collected from harvesting and place them in the inventory chest. If this chest becomes full it will report the problem and wait for it to be emptied. If it runs out of bonemeal it will continue to plant and harvest but the trees will not be harvested until they have grown.
  15.  
  16. Added the ability to automatically restart even stopped in the middle of harvesting. This is accomplished by placing a railing around the logging area, and placing a chest and railing in the turtle to he can determine the borders and the starting position.
  17.  
  18. Added furnaces so that if the turtle runs out of fuel, or if the inventory chest becomes full, the he can make charcoal, until the fuel chest becomes full.
  19.  
  20. Now doesn't use a set number of rows and column. Trees will be planted in all space available between the fences.
  21.  
  22. Materials Required:
  23. 1 mining or felling turtle
  24. 4 chests
  25. a sapling for each tree location (numberOfRows * treesPerRow)
  26. 1 log same type as the sapling
  27. fuel (suggest coal or coalcoke)
  28. 1 railing (used to identify borders of grove on startup.)
  29. 1 chest (used to identify starting position and orientation.)
  30. bone meal (optional)
  31.  
  32. Setup:
  33. Diagram:
  34. +-F-F-F----------------------------...-------------------------------------+
  35. | |
  36. : :
  37. | |
  38. |s |
  39. |a |
  40. |p |
  41. [inv]X |
  42. |b |
  43. |o |
  44. |n |
  45. | |
  46. : :
  47. | |
  48. +------------------------------------...-----------------------------------+
  49. [inv] = Inventory Chest
  50. sap = Sapling Chest
  51. bon = Bone meal Chest
  52. X = Fuel Chest below turtle will charcoal
  53. + - | = Fencing or Gates around area for grove of trees, any size.
  54. F = Furnace (0 or more, above the fence with 1 empty space between the fence and the furnace, and 1 empty space between each furnace)
  55.  
  56. There are variables in the beginning of the program that set the number of trees that will be in each row and the number of rows to plant, as well as the space between the trees. These should be modified to suit your preferences.
  57.  
  58. Place turtle over a chest in a level dirt field facing away. Place a chest to the left, right and behind the turtle. These can be single or double chests.
  59.  
  60. Place fuel in the chest under the turtle. Place saplings in the chest to the left. Place bone meal in the chest to the right. The chest behind the turtle starts out empty and will be filled with the wood that is harvested and anything else dropped by the harvested trees.
  61.  
  62. Put a fence around the level area that will contain the trees with the sapling and bone meal chest inside of the fence one block.
  63.  
  64. Put fuel in slot 1, fertilizer in slot 2, a chest in slot 3, a fence in slot 4, a gate in slot 5, saplings in slot 6, and a block of wood of the same type that is to be harvested in slot 7.
  65.  
  66.  
  67. Setup
  68. Slot 1 = Fuel (charcoal)
  69. Slot 2 = Fertilizer
  70. Slot 3 = Chest
  71. Slot 4 = Fence
  72. Slot 5 = Gate
  73. Slot 6 = Saplings
  74. Slot 7 = Wood same type as sapling
  75. ]]--
  76.  
  77. version = "4.1.1"
  78. treesPerRow = 6
  79. numberOfRows = 4
  80. spaceBetweenTrees = 4
  81. plantTrees = true -- Set to false to clear tree grove.
  82. harvestWide = false -- True means to harvest everything on each level of a tree, including leaves
  83. slotFuel = 1
  84. slotBonemeal = 2
  85. slotChest = 3
  86. slotFence = 4
  87. slotGate = 5
  88. slotSapling = 6
  89. -- You can use the wood it harvests as fuel if you want. If so then use slotWood = slotFuel
  90. slotWood = 7
  91. logfile = nil
  92.  
  93. abortCircuit = false
  94. minerID = os.getComputerID()
  95. consoleID = 0
  96. furnaceCount = nil
  97.  
  98. function checkSetup()
  99. updateStatus("Checking Setup")
  100. if turtle.getItemCount(slotFuel) < 2 then
  101. updateStatus("2+ Fuel needed in slot "..slotFuel)
  102. while turtle.getItemCount(slotFuel) < 2 do
  103. sleep(2)
  104. end
  105. else
  106. updateStatus(turtle.getItemCount(slotFuel).." fueld present in slot "..slotFuel)
  107. end
  108. if turtle.getItemCount(slotBonemeal) < 1 then
  109. updateStatus("Fertilizer recommended in slot "..slotBonemeal)
  110. else
  111. updateStatus("Fertilizer present in slot "..slotBonemeal)
  112. end
  113. if turtle.getItemCount(slotChest) < 1 then
  114. updateStatus("Chest needed in slot "..slotChest)
  115. while turtle.getItemCount(slotChest) < 1 do
  116. sleep(2)
  117. end
  118. else
  119. updateStatus("Chest present in slot "..slotChest)
  120. end
  121. if turtle.getItemCount(slotFence) < 1 then
  122. updateStatus("Fence needed in slot "..slotFence)
  123. while turtle.getItemCount(slotFence) < 1 do
  124. sleep(2)
  125. end
  126. else
  127. updateStatus("Fence present in slot "..slotFence)
  128. end
  129. if turtle.getItemCount(slotGate) < 1 then
  130. updateStatus("Gate may be needed in slot "..slotGate)
  131. else
  132. updateStatus("Gate present in slot "..slotGate)
  133. end
  134. if turtle.getItemCount(slotSapling) < 1 then
  135. updateStatus("Saplings needed in slot "..slotSapling)
  136. while turtle.getItemCount(slotSapling) < treesPerRow * numberOfRows do
  137. sleep(2)
  138. end
  139. else
  140. updateStatus(turtle.getItemCount(slotSapling) .. " Saplings present in slot "..slotSapling)
  141. end
  142. if turtle.getItemCount(slotWood) < 1 then
  143. updateStatus("Wood needed in slot "..slotWood)
  144. while turtle.getItemCount(slotWood) < 1 do
  145. sleep(2)
  146. end
  147. else
  148. updateStatus("Wood present in slot "..slotWood)
  149. end
  150. end
  151. function updateStatus(msg, silent)
  152. print(msg)
  153. updateLog(msg)
  154. end
  155. function getConsoleID()
  156. local fname = "ConsoleID"
  157. if fs.exists(fname) then
  158. file = fs.open(fname, "r")
  159. consoleID = tonumber(file.readAll())
  160. file.close()
  161. print("Monitor Console ID: "..consoleID)
  162. else
  163. updateStatus("Waiting for Console ID...",true)
  164. term.write("Monitor Console ID: ")
  165. consoleID = io.read()
  166. file = fs.open(fname, "w")
  167. file.write(consoleID)
  168. file.close()
  169. consoleID = tonumber(consoleID)
  170. end
  171. end
  172. function updateLog(msg)
  173. local fname = 'log'
  174. if fs.exists(fname) then
  175. logfile = fs.open(fname, "a")
  176. else
  177. logfile = fs.open(fname, "w")
  178. end
  179. updateTime = textutils.formatTime(os.time())
  180. logfile.write(updateTime.." "..msg.."\n")
  181. logfile.close()
  182. end
  183. function selectSapling()
  184. -- Look at all of the slots and if a slot other than the default sapling slot contains saplings, then return this slot.
  185. local slot = 0
  186. for slot=1,16 do
  187. if slot ~= slotSapling then
  188. if turtle.getItemCount(slot) > 1 then
  189. turtle.select(slot)
  190. if turtle.compareTo(slotSapling) then return true end
  191. end
  192. end
  193. end
  194. if turtle.getItemCount(slotSapling) > 1 then
  195. turtle.select(slotSapling)
  196. return true
  197. end
  198. return false
  199. end
  200. function selectWood()
  201. local slot = 0
  202. for slot = slotWood + 1,16 do
  203. turtle.select(slot)
  204. if turtle.compareTo(slotWood) then return true end
  205. end
  206. return false
  207. end
  208. function selectBonemeal()
  209. -- Look at all of the slots and if a slot other than the default bone meal slot contains bone meal, then move them into the default bone meal slot.
  210. local slot = 0
  211. for slot=1,16 do
  212. if slot ~= slotBonemeal then
  213. if turtle.getItemCount(slot) > 1 then
  214. turtle.select(slot)
  215. if turtle.compareTo(slotBonemeal) then return true end
  216. end
  217. end
  218. end
  219. if turtle.getItemCount(slotBonemeal) > 1 then
  220. turtle.select(slotBonemeal)
  221. return true
  222. end
  223. return false
  224. end
  225. function plantSapling()
  226. -- Plant a sapling report if there is a problem.
  227. if plantTrees then
  228. if selectSapling() then
  229. if turtle.place() then
  230. return true
  231. else
  232. updateStatus("Unable to plant sapling.")
  233. return false
  234. end
  235. else
  236. updateStatus("Out of saplings.")
  237. return false
  238. end
  239. else
  240. return false
  241. end
  242. end
  243. function applyBonemeal()
  244. -- Apply fertilizer and report if there is a problem.
  245. if selectBonemeal() then
  246. local cnt = 0
  247. local applied = false
  248. while isSapling() and cnt < 10 do
  249. if selectBonemeal() then
  250. if turtle.place() then applied = true end
  251. cnt = cnt + 1
  252. end
  253. end
  254. if applied then
  255. return true
  256. else
  257. updateStatus("Unable to fertilize.")
  258. return false
  259. end
  260. else
  261. updateStatus("Out of fertilizer.")
  262. return false
  263. end
  264. end
  265. function refuel()
  266.  
  267. end
  268. function moveForward()
  269. turtle.suck()
  270. if not turtle.forward() then
  271. updateStatus("Path blocked.")
  272. if turtle.detect() then
  273. -- Something is there so try destroy it!!!
  274. turtle.dig()
  275. end
  276. while not turtle.forward() do
  277. sleep(2)
  278. end
  279. end
  280. end
  281. function moveBack()
  282. turtle.suck()
  283. if not turtle.back() then
  284. updateStatus("Path blocked.")
  285. while not turtle.back() do
  286. sleep(2)
  287. end
  288. end
  289. end
  290. function goForward()
  291. turtle.suck()
  292. return turtle.forward()
  293. end
  294. function gatherSaplings()
  295. -- Should be facing sapling or where tree was. Move around the tree collecting anything that has dropped.
  296. turtle.select(slotWood)
  297. refuel()
  298. turtle.suck()
  299. turtle.turnLeft()
  300. moveForward()
  301. turtle.suck()
  302. turtle.turnRight()
  303. moveForward()
  304. turtle.suck()
  305. turtle.turnLeft()
  306. turtle.suck()
  307. turtle.turnRight()
  308. moveForward()
  309. turtle.suck()
  310. turtle.turnLeft()
  311. turtle.suck()
  312. turtle.turnRight()
  313. turtle.turnRight()
  314. moveForward()
  315. turtle.suck()
  316. turtle.turnLeft()
  317. turtle.suck()
  318. turtle.turnRight()
  319. moveForward()
  320. turtle.suck()
  321. turtle.turnLeft()
  322. turtle.suck()
  323. turtle.turnRight()
  324. turtle.turnRight()
  325. moveForward()
  326. turtle.suck()
  327. turtle.turnLeft()
  328. turtle.suck()
  329. turtle.turnRight()
  330. moveForward()
  331. turtle.suck()
  332. turtle.turnRight()
  333. turtle.suck()
  334. turtle.turnRight()
  335. turtle.suck()
  336. end
  337. function isWood()
  338. turtle.select(slotWood)
  339. local woodFound = turtle.compare()
  340. return woodFound
  341. end
  342. function isWoodUp()
  343. turtle.select(slotWood)
  344. local woodFound = turtle.compareUp()
  345. return woodFound
  346. end
  347. function isSapling()
  348. local saplingFound = false
  349. turtle.select(slotSapling)
  350. saplingFound = turtle.compare()
  351. if not saplingFound then print("Not a sapling.") end
  352. return saplingFound
  353. end
  354. function harvestLevel()
  355. -- Don't go too far so limit expansion to half the distance between the trees.
  356. local found = false
  357. for d=1,4 do
  358. if not refuel() then return false end
  359. local c = 0
  360. while turtle.detect() and c < spaceBetweenTrees-2 do
  361. c = c + 1
  362. found = true
  363. turtle.dig()
  364. turtle.forward()
  365. turtle.turnLeft()
  366. if turtle.detect() then
  367. turtle.dig()
  368. turtle.forward()
  369. turtle.dig()
  370. turtle.turnLeft()
  371. turtle.dig()
  372. turtle.turnLeft()
  373. turtle.turnLeft()
  374. turtle.dig()
  375. turtle.turnLeft()
  376. turtle.back()
  377. end
  378. turtle.turnLeft()
  379. turtle.turnLeft()
  380. if turtle.detect() then
  381. turtle.dig()
  382. turtle.forward()
  383. turtle.dig()
  384. turtle.turnLeft()
  385. turtle.dig()
  386. turtle.turnLeft()
  387. turtle.turnLeft()
  388. turtle.dig()
  389. turtle.turnLeft()
  390. turtle.back()
  391. end
  392. turtle.turnLeft()
  393. end
  394. for a=1,c do turtle.back() end
  395. turtle.turnLeft()
  396. end
  397. return found
  398. end
  399. function harvestWood()
  400. if isWood() then
  401. turtle.dig()
  402. return true
  403. else
  404. return false
  405. end
  406. end
  407. function harvestTrunk()
  408. -- Go up the trunk and clear each level spreading out up to half the distance between the trees.
  409. turtle.select(slotWood)
  410. if turtle.detect() then
  411. turtle.dig()
  412. turtle.forward()
  413. turtle.suck()
  414. local h = 0
  415. if harvestWide then
  416. while harvestLevel() or isWoodUp() do
  417. turtle.digUp()
  418. if turtle.up() then h = h + 1 end
  419. end
  420. else
  421. while isWoodUp() do
  422. turtle.digUp()
  423. if turtle.up() then h = h + 1 else break end
  424. end
  425. end
  426. local d = 1
  427. refuel()
  428. while d <= h do
  429. if turtle.detectDown() then
  430. turtle.digDown()
  431. end
  432. if turtle.down() then
  433. d = d + 1
  434. end
  435. end
  436. turtle.suck()
  437. turtle.back()
  438. end
  439. end
  440. function saplingsAvailable()
  441. local saplingCount = 0
  442. for slot=1,16 do
  443. if slot == slotSapling then
  444. saplingCount = saplingCount + turtle.getItemCount(slot)
  445. else
  446. turtle.select(slot)
  447. if turtle.compareTo(slotSapling) then
  448. saplingCount = saplingCount + turtle.getItemCount(slot)
  449. end
  450. end
  451. end
  452. return saplingCount
  453. end
  454. function facingAChest()
  455. turtle.select(slotChest)
  456. return turtle.compare()
  457. end
  458. function aboveAChest()
  459. turtle.select(slotChest)
  460. return turtle.compareDown()
  461. end
  462. function facingAFence()
  463. turtle.select(slotFence)
  464. if turtle.compare() then return true end
  465. turtle.select(slotGate)
  466. return turtle.compare()
  467. end
  468. function aboveAFence()
  469. turtle.select(slotFence)
  470. if turtle.compareDown() then return true end
  471. turtle.select(slotGate)
  472. return turtle.compareDown()
  473. end
  474. function serviceFurnaces()
  475. -- Load up on wood first.
  476. turtle.turnRight()
  477. turtle.turnRight()
  478. turtle.select(slotWood+1)
  479. while turtle.suck() do
  480. -- Keep taking wood until full.
  481. end
  482. -- Now unload the top 2 slots to get fuel
  483. for slot=15,16 do
  484. turtle.select(slot)
  485. if turtle.compareTo(slotWood) then turtle.drop() end
  486. end
  487. turtle.turnRight()
  488. turtle.turnRight()
  489. updateStatus("Checking furnaces")
  490. local distanceToFence = 0
  491. moveForward()
  492. turtle.turnLeft()
  493. while not facingAFence() do
  494. moveForward()
  495. distanceToFence = distanceToFence + 1
  496. end
  497. turtle.up()
  498. moveForward()
  499. -- Should now be above the fence below the furnace.
  500. if furnaceCount == nil then
  501. if turtle.detectUp() then furnaceCount = 1 else furnaceCount = 0 end
  502. end
  503. furnaceIdx = 1
  504. anotherFurnace = true
  505. while turtle.detectUp() and anotherFurnace do
  506. updateStatus("Loading furnace "..furnaceIdx)
  507. while selectWood() and turtle.dropUp() do
  508. -- Keep adding wood to furnace as fuel until out of wood or furnace is full.
  509. end
  510. -- Now move to the top to add material to smelt
  511. if turtle.back() then
  512. if turtle.up() then
  513. if turtle.up() then
  514. if turtle.forward() then
  515. if turtle.detectDown() then
  516. while selectWood() and turtle.dropDown() do
  517. -- Keep adding wood to furnace to smelt until out of wood or furnace is full.
  518. end
  519. -- Now move to the right of the furnace to remove fuel
  520. turtle.turnLeft()
  521. if turtle.back() then
  522. if turtle.down() then
  523. updateStatus("Unloading furnace "..furnaceIdx)
  524. turtle.select(slotFuel)
  525. while turtle.suck() do
  526. -- Remove all fuel in furnace
  527. end
  528. turtle.turnRight()
  529. turtle.turnRight()
  530. if not turtle.detect() then anotherFurnace = false end
  531. turtle.turnLeft()
  532. turtle.turnLeft()
  533. turtle.up()
  534. end
  535. turtle.forward()
  536. end
  537. turtle.turnRight()
  538. end
  539. moveBack()
  540. end
  541. turtle.down()
  542. end
  543. turtle.down()
  544. end
  545. turtle.forward()
  546. end
  547. -- Now go to the next furnace if there is wood available
  548. if selectWood() and anotherFurnace then
  549. turtle.turnRight()
  550. if turtle.forward() then
  551. if turtle.forward() then
  552. furnaceIdx = furnaceIdx + 1
  553. if turtle.detectUp() then furnaceCount = furnaceCount + 1 end
  554. end
  555. end
  556. turtle.turnLeft()
  557. end
  558. end
  559. -- Now return the first furnace
  560. if furnaceIdx > 1 then
  561. turtle.turnLeft()
  562. for bi = 1,furnaceIdx-1 do
  563. turtle.forward()
  564. turtle.forward()
  565. end
  566. turtle.turnRight()
  567. end
  568. -- Now return to start and unload extra wood and fuel
  569. updateStatus("Returning to start.")
  570. turtle.turnLeft()
  571. turtle.turnLeft()
  572. moveForward()
  573. if not turtle.down() then
  574. turtle.digDown()
  575. while not turtle.down() do
  576. updateStatus("Down blocked.")
  577. sleep(10)
  578. end
  579. end
  580. for di = 1,distanceToFence do moveForward() end
  581. turtle.turnRight()
  582. moveForward()
  583. -- Should be back at the start. - make sure.
  584. turtle.select(slotChest)
  585. if not turtle.compareDown() then
  586. -- Lost so find home.
  587. updateStatus("Lost. Returning to start.")
  588. locateStartingPosition()
  589. turtle.turnRight()
  590. turtle.turnRight()
  591. end
  592. -- Unload extra fuel.
  593. for s = slotWood+1,16 do
  594. turtle.select(s)
  595. if turtle.getItemCount(s) > 0 then
  596. if not turtle.dropDown() then
  597. updateStatus("Fuel chest full. Waiting...")
  598. while not turtle.dropDown() do sleep(30) end
  599. end
  600. end
  601. end
  602. -- Unload extra wood
  603. for s = slotWood+1,16 do
  604. turtle.select(s)
  605. if turtle.compareTo(slotWood) then
  606. if not turtle.drop() then
  607. updateStatus("Inventory chest full. Waiting...")
  608. while not turtle.drop() do sleep(30) end
  609. end
  610. end
  611. end
  612. turtle.select(slotWood)
  613. turtle.drop(turtle.getItemCount(slotWood)-1)
  614. turtle.turnLeft()
  615. turtle.turnLeft()
  616. updateStatus("Finished servicing furnaces.")
  617. end
  618. function locateStartingPosition()
  619. -- Move down and forward until a chest or fence is located.
  620. local atHome = false
  621. -- In case stopped while servicing furnaces.
  622. if turtle.detectUp() or turtle.detectDown() then
  623. if not turtle.back() then
  624. turtle.turnRight()
  625. turtle.back()
  626. end
  627. end
  628. -- Now get down to the ground.
  629. while not turtle.detectDown() do turtle.down() end
  630. if aboveAChest() then
  631. print("Above a chest")
  632. while facingAChest() do
  633. turtle.turnRight()
  634. end
  635. print("At home")
  636. atHome = true
  637. end
  638. while not atHome do
  639. updateStatus("Looking for a chest")
  640. while not facingAChest() do
  641. refuel()
  642. while facingAFence() do
  643. turtle.turnRight()
  644. updateStatus("Found a fence")
  645. end
  646. if not goForward() then
  647. print("Can't go fwd")
  648. if isWood() then
  649. turtle.turnRight()
  650. elseif turtle.up() then
  651. if goForward() then
  652. if aboveAFence() then
  653. updateStatus("Found a fence")
  654. turtle.back()
  655. turtle.turnRight()
  656. end
  657. else
  658. turtle.turnRight()
  659. end
  660. else
  661. turtle.turnLeft()
  662. end
  663. end
  664. if aboveAFence() then
  665. updateStatus("Found a fence")
  666. turtle.back()
  667. turtle.turnRight()
  668. end
  669. while not turtle.detectDown() do turtle.down() end
  670. end
  671. updateStatus("Found a chest")
  672. -- Now we should be facing a Chest.
  673. if aboveAChest() then
  674. atHome = true
  675. else
  676. turtle.turnLeft()
  677. if facingAFence() then
  678. turtle.turnRight()
  679. break
  680. else
  681. turtle.turnRight()
  682. turtle.turnRight()
  683. if facingAFence() then
  684. turtle.turnLeft()
  685. break
  686. end
  687. end
  688. end
  689. end
  690. local overChests = true
  691. if not atHome then
  692. turtle.up()
  693. turtle.forward()
  694. sleep(.3)
  695. if not aboveAChest() then overChests = false end
  696. turtle.forward()
  697. sleep(.3)
  698. if not aboveAChest() then overChests = false end
  699. turtle.forward()
  700. turtle.down()
  701. sleep(.3)
  702. if not aboveAChest() then overChests = false end
  703. end
  704. if overChests then atHome = true else updateStatus("Lost a chest") end
  705. if atHome then
  706. -- Should now be in the home position.
  707. -- Now determine the correct orientation.
  708. local foundOpening = false
  709. for d=1,4 do
  710. if not facingAChest() then
  711. foundOpening = true
  712. updateStatus("In start position.")
  713. break
  714. end
  715. turtle.turnLeft()
  716. end
  717. if not foundOpening then
  718. while true do
  719. updateStatus("I'm lost and need help...")
  720. sleep(60)
  721. end
  722. end
  723. else
  724. while true do
  725. updateStatus("I'm lost and need help...")
  726. sleep(60)
  727. end
  728. end
  729. end
  730. function suckItUp(n)
  731. local d
  732. for d=1,4 do
  733. if n%2 == 0 then turtle.turnLeft() else turtle.turnRight() end
  734. turtle.suck()
  735. end
  736. end
  737.  
  738. -- Program begins
  739. updateStatus("Starting")
  740. term.clear()
  741. term.setCursorPos(1,1)
  742. print("Velander's Tree Farmer "..version)
  743. print("Turtle ID: "..minerID)
  744. getConsoleID()
  745. checkSetup()
  746. local col = 0
  747. local row = 0
  748. local slot = 0
  749. local leftShift = 0
  750. fuelChestEmpty = false
  751. locateStartingPosition()
  752. abortCircuit = false
  753.  
  754. while true do
  755. -- Before we start planting make sure the turtle is properly equiped
  756. print("Checking bone meal")
  757. if turtle.getItemCount(slotBonemeal) < (treesPerRow * numberOfRows) then
  758. -- Load up on bonemeal if available.
  759. turtle.turnRight()
  760. -- Get enough bonemeal to fertilize all trees if necessary
  761. turtle.select(slotBonemeal)
  762. turtle.suck((treesPerRow * numberOfRows)-turtle.getItemCount(slotBonemeal)+1)
  763. turtle.turnLeft()
  764. end
  765. print("Checking saplings")
  766. local saplingCount = saplingsAvailable()
  767. if saplingCount <= (treesPerRow * numberOfRows) then
  768. turtle.turnLeft()
  769. turtle.select(slotSapling)
  770. while turtle.suck((treesPerRow * numberOfRows)-saplingCount+1) do
  771. saplingCount = saplingsAvailable()
  772. if saplingCount > (treesPerRow * numberOfRows) then break end
  773. turtle.select(slotSapling)
  774. end
  775. turtle.turnRight()
  776. end
  777. if saplingsAvailable() <= 1 then
  778. -- Out of saplings so restock from chest
  779. updateStatus("Out of saplings.")
  780. end
  781. local reachedEndOfCol = false
  782. local reachedEndOfRows = false
  783. local rowLength = 0
  784. local row = 1
  785. while not reachedEndOfRows do
  786. -- Move to beginning of grove.
  787. if row == 1 then
  788. moveForward()
  789. turtle.turnLeft()
  790. while not facingAFence() do
  791. moveForward()
  792. leftShift = leftShift + 1
  793. end
  794. moveBack()
  795. leftShift = leftShift - 1
  796. moveBack()
  797. leftShift = leftShift - 1
  798. turtle.turnRight()
  799. else
  800. turtle.turnRight()
  801. updateStatus("Moving to row "..row)
  802. for p=1,spaceBetweenTrees do
  803. if facingAFence() then
  804. reachedEndOfRows = true
  805. for l=1,p-1 do moveBack() end
  806. row = row - 1
  807. break
  808. else
  809. moveForward()
  810. suckItUp(p)
  811. end
  812. end
  813. if not reachedEndOfRows then
  814. moveBack()
  815. moveBack()
  816. end
  817. turtle.turnLeft()
  818. end
  819. if not reachedEndOfRows then
  820. updateStatus("Starting row "..row)
  821. reachedEndOfCol = false
  822. treesPerRow = 0
  823. rowLength = 0
  824. while not reachedEndOfCol do
  825. refuel()
  826. if abortCircuit then break end
  827. -- Move between trees sucking as you go.
  828. for p=1,spaceBetweenTrees do
  829. if facingAFence() then
  830. updateStatus("Found a fence in row "..row)
  831. reachedEndOfCol = true
  832. else
  833. moveForward()
  834. suckItUp(p)
  835. rowLength = rowLength + 1
  836. end
  837. end
  838. if facingAFence() then
  839. reachedEndOfCol = true
  840. moveBack()
  841. suckItUp(row)
  842. rowLength = rowLength - 1
  843. end
  844. if not reachedEndOfCol then
  845. treesPerRow = treesPerRow + 1
  846. turtle.turnRight()
  847. local found = false
  848. if turtle.detect() then
  849. -- Check to see if it is the wood we are harvesting
  850. if isWood() then
  851. updateStatus("Harvesting at "..row..","..treesPerRow)
  852. if refuel() then harvestTrunk() else abortCircuit = true end
  853. elseif facingAChest() then
  854. abortCircuit = true
  855. elseif facingAFence() then
  856. abortCircuit = true
  857. elseif turtle.detect() then
  858. updateStatus("Sapling at "..row..","..treesPerRow)
  859. found = true
  860. applyBonemeal()
  861. if isWood() then
  862. updateStatus("Harvesting at "..row..","..treesPerRow)
  863. if refuel() then harvestTrunk() else abortCircuit = true end
  864. end
  865. end
  866. end
  867. if not abortCircuit then
  868. if not isWood() and not isSapling() then
  869. if plantSapling() then
  870. updateStatus("Sapling planted at "..row..","..treesPerRow)
  871. end
  872. end
  873. end
  874. turtle.turnLeft()
  875. end
  876. end
  877. if abortCircuit then
  878. updateStatus("Aborting circuit.")
  879. turtle.turnLeft()
  880. turtle.turnLeft()
  881. locateStartingPosition()
  882. turtle.turnLeft()
  883. turtle.turnLeft()
  884. break
  885. else
  886. updateStatus("Reached end of row "..row)
  887. moveForward()
  888. suckItUp(row)
  889. rowLength = rowLength + 1
  890. turtle.turnRight()
  891. turtle.turnRight()
  892. -- returing to bottom of grove.
  893. for p=1,rowLength do
  894. suckItUp(p)
  895. moveForward()
  896. end
  897. turtle.turnLeft()
  898. moveForward()
  899. moveForward()
  900. turtle.turnLeft()
  901. end
  902. row = row + 1
  903. end
  904. end
  905. if not abortCircuit then
  906. numberOfRows = row
  907. -- Should be back to the begging of the grove at the last column of trees.
  908. turtle.turnLeft()
  909. -- Return to start
  910. updateStatus("Returning to start.")
  911. local sr = (spaceBetweenTrees * (numberOfRows-1))+2 - leftShift
  912. for p=1,sr do
  913. turtle.suck()
  914. if facingAChest() or facingAFence() then
  915. updateStatus("Lost. Returning to start.")
  916. locateStartingPosition()
  917. abortCircuit = true
  918. turtle.turnLeft()
  919. turtle.turnLeft()
  920. else
  921. moveForward()
  922. end
  923. end
  924. -- Should be back in the start position.
  925. if not abortCircuit then
  926. turtle.turnLeft()
  927. turtle.forward()
  928. end
  929. end
  930. abortCircuit = false
  931. if not facingAChest() or not aboveAChest() then
  932. locateStartingPosition()
  933. turtle.turnLeft()
  934. turtle.turnLeft()
  935. end
  936. -- Now facing inventory chest.
  937. updateStatus("Storing wood.")
  938. for slot=slotWood+1,16 do
  939. turtle.select(slot)
  940. local dropped = true
  941. if turtle.compareTo(slotSapling) then
  942. -- Leave for now
  943. elseif turtle.getItemCount(slot) > 1 then
  944. if not turtle.drop() then dropped = false end
  945. end
  946. if dropped and turtle.getItemCount(slotWood) > 1 then
  947. turtle.select(slotWood)
  948. if not turtle.drop(turtle.getItemCount(slotWood)-1) then dropped = false end
  949. end
  950. if not dropped and (furnaceCount == nil or furnaceCount > 0) then
  951. -- Inventory must be full. Make sure that the furnaces are busy.
  952. updateStatus("Servicing furnaces...")
  953. turtle.turnRight()
  954. turtle.turnRight()
  955. refuel()
  956. serviceFurnaces()
  957. turtle.turnRight()
  958. turtle.turnRight()
  959. break
  960. end
  961. end
  962. turtle.turnRight()
  963. -- Now facing chest with saplings. Unload extra saplings
  964. updateStatus("Storing saplings.")
  965. for slot=1,16 do
  966. turtle.select(slot)
  967. -- Check to see if it is a sapling
  968. if slot ~= slotSapling and turtle.compareTo(slotSapling) then
  969. if not turtle.drop() then
  970. updateStatus("Sapling chest full.")
  971. turtle.up()
  972. turtle.drop()
  973. turtle.down()
  974. end
  975. end
  976. end
  977. turtle.turnRight()
  978. -- Back in start position.
  979. updateStatus("Pausing before next circuit...")
  980. sleep(30)
  981. end
Advertisement
Add Comment
Please, Sign In to add comment