Advertisement
dadragon84

Large Tree Farm

Feb 26th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 31.50 KB | None | 0 0
  1. --[[
  2. TreeFarmer
  3.  
  4. by Severino
  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.   if not rednet.isOpen("right") then rednet.open("right") end
  153.   if rednet.isOpen("right") then
  154.     networkmsg = "Miner ".. minerID..": "..msg
  155.     if consoleID == 0 then
  156.       rednet.broadcast(networkmsg)
  157.     else
  158.       rednet.send(consoleID,networkmsg)
  159.     end
  160.   end
  161.   if not silent then print(msg) end
  162.   updateLog(msg)
  163. end
  164. function getConsoleID()
  165.   local fname = "ConsoleID"
  166.   if fs.exists(fname) then
  167.     file = fs.open(fname, "r")
  168.     consoleID = tonumber(file.readAll())
  169.     file.close()
  170.     print("Monitor Console ID: "..consoleID)
  171.   else
  172.     updateStatus("Waiting for Console ID...",true)
  173.     term.write("Monitor Console ID: ")
  174.     consoleID = io.read()
  175.     file = fs.open(fname, "w")
  176.     file.write(consoleID)
  177.     file.close()
  178.     consoleID = tonumber(consoleID)
  179.   end
  180. end
  181. function updateLog(msg)
  182.   local fname = 'log'
  183.   if fs.exists(fname) then
  184.     logfile = fs.open(fname, "a")
  185.   else
  186.     logfile = fs.open(fname, "w")
  187.   end
  188.   updateTime = textutils.formatTime(os.time())
  189.   logfile.write(updateTime.." "..msg.."\n")
  190.   logfile.close()
  191. end
  192. function selectSapling()
  193.   -- Look at all of the slots and if a slot other than the default sapling slot contains saplings, then return this slot.
  194.   local slot = 0
  195.   for slot=1,16 do
  196.     if slot ~= slotSapling then
  197.       if turtle.getItemCount(slot) > 1 then
  198.         turtle.select(slot)
  199.         if turtle.compareTo(slotSapling) then return true end
  200.       end
  201.     end
  202.   end
  203.   if turtle.getItemCount(slotSapling) > 1 then
  204.     turtle.select(slotSapling)
  205.     return true
  206.   end
  207.   return false
  208. end
  209. function selectWood()
  210.   local slot = 0
  211.   for slot = slotWood + 1,16 do
  212.     turtle.select(slot)
  213.     if turtle.compareTo(slotWood) then return true end
  214.   end
  215.   return false
  216. end
  217. function selectBonemeal()
  218.   -- 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.
  219.   local slot = 0
  220.   for slot=1,16 do
  221.     if slot ~= slotBonemeal then
  222.       if turtle.getItemCount(slot) > 1 then
  223.         turtle.select(slot)
  224.         if turtle.compareTo(slotBonemeal) then return true end
  225.       end
  226.     end
  227.   end
  228.   if turtle.getItemCount(slotBonemeal) > 1 then
  229.     turtle.select(slotBonemeal)
  230.     return true
  231.   end
  232.   return false
  233. end
  234. function plantSapling()
  235.   -- Plant a sapling report if there is a problem.
  236.   if plantTrees then
  237.     if selectSapling() then    
  238.       if turtle.place() then
  239.         return true
  240.       else
  241.         updateStatus("Unable to plant sapling.")
  242.         return false
  243.       end
  244.     else
  245.       updateStatus("Out of saplings.")
  246.       return false
  247.     end
  248.   else
  249.     return false
  250.   end
  251. end
  252. function applyBonemeal()
  253.   -- Apply fertilizer and report if there is a problem.
  254.   if selectBonemeal() then
  255.     local cnt = 0
  256.     local applied = false
  257.     while isSapling() and cnt < 10 do
  258.       if selectBonemeal() then
  259.         if turtle.place() then applied = true end
  260.         cnt = cnt + 1
  261.       end
  262.     end
  263.     if applied then
  264.       return true
  265.     else
  266.       updateStatus("Unable to fertilize.")
  267.       return false
  268.     end
  269.   else
  270.     updateStatus("Out of fertilizer.")
  271.     return false
  272.   end
  273. end
  274. function refuel()
  275.   -- Refuel from the slot specified if needed
  276.   turtle.select(slotFuel)
  277.   -- Check to see if any other slot has the same material
  278.   local s = 0
  279.   for s=1,16,1 do
  280.     if s ~= slotFuel and s ~= slotWood then
  281.       if turtle.compareTo(s) then
  282.         turtle.select(s)
  283.         turtle.transferTo(slotFuel)
  284.       end
  285.     end
  286.   end
  287.   -- Make sure there is enough fuel to harvest every tree and the distance between
  288.   while turtle.getFuelLevel() <= (treesPerRow * numberOfRows) * 10 * 2
  289.       + (treesPerRow * numberOfRows * spaceBetweenTrees)
  290.       + (spaceBetweenTrees * 2) do
  291.     turtle.select(slotFuel)
  292.     if turtle.getItemCount(slotFuel) <= 1 then
  293.       updateStatus("Out of fuel.")
  294.       return false
  295.     end
  296.     local q = turtle.getItemCount(slotFuel) - 1
  297.     if q > 5 then q = 5 else updateStatus("Low on fuel.") end
  298.     turtle.refuel(q)
  299.     updateStatus("Refueled to level: "..turtle.getFuelLevel())
  300.   end
  301.   turtle.select(slotWood)
  302.   if turtle.getFuelLevel() < treesPerRow * numberOfRows * spaceBetweenTrees then
  303.     abortCircuit = true
  304.     return false
  305.   else
  306.     return true
  307.   end
  308.  
  309. end
  310. function moveForward()
  311.   turtle.suck()
  312.   if not turtle.forward() then
  313.     updateStatus("Path blocked.")
  314.     if turtle.detect() then
  315.       -- Something is there so try destroy it!!!
  316.       turtle.dig()
  317.     end
  318.     while not turtle.forward() do
  319.       sleep(2)
  320.     end
  321.   end
  322. end
  323. function moveBack()
  324.   turtle.suck()
  325.   if not turtle.back() then
  326.     updateStatus("Path blocked.")
  327.     while not turtle.back() do
  328.       sleep(2)
  329.     end
  330.   end
  331. end
  332. function goForward()
  333.   turtle.suck()
  334.   return turtle.forward()
  335. end
  336. function gatherSaplings()
  337.   -- Should be facing sapling or where tree was.  Move around the tree collecting anything that has dropped.
  338.   turtle.select(slotWood)
  339.   refuel()
  340.   turtle.suck()
  341.   turtle.turnLeft()
  342.   moveForward()
  343.   turtle.suck()
  344.   turtle.turnRight()
  345.   moveForward()
  346.   turtle.suck()
  347.   turtle.turnLeft()
  348.   turtle.suck()
  349.   turtle.turnRight()
  350.   moveForward()
  351.   turtle.suck()
  352.   turtle.turnLeft()
  353.   turtle.suck()
  354.   turtle.turnRight()
  355.   turtle.turnRight()
  356.   moveForward()
  357.   turtle.suck()
  358.   turtle.turnLeft()
  359.   turtle.suck()
  360.   turtle.turnRight()
  361.   moveForward()
  362.   turtle.suck()
  363.   turtle.turnLeft()
  364.   turtle.suck()
  365.   turtle.turnRight()
  366.   turtle.turnRight()
  367.   moveForward()
  368.   turtle.suck()
  369.   turtle.turnLeft()
  370.   turtle.suck()
  371.   turtle.turnRight()
  372.   moveForward()
  373.   turtle.suck()
  374.   turtle.turnRight()
  375.   turtle.suck()
  376.   turtle.turnRight()
  377.   turtle.suck()
  378. end
  379. function isWood()
  380.   turtle.select(slotWood)
  381.   local woodFound = turtle.compare()
  382.   return woodFound
  383. end
  384. function isWoodUp()
  385.   turtle.select(slotWood)
  386.   local woodFound = turtle.compareUp()
  387.   return woodFound
  388. end
  389. function isSapling()
  390.   local saplingFound = false
  391.   turtle.select(slotSapling)
  392.   saplingFound = turtle.compare()
  393.   if not saplingFound then print("Not a sapling.") end
  394.   return saplingFound
  395. end
  396. function harvestLevel()
  397.   -- Don't go too far so limit expansion to half the distance between the trees.
  398.   local found = false
  399.   for d=1,4 do
  400.     if not refuel() then return false end
  401.     local c = 0
  402.     while turtle.detect() and c < spaceBetweenTrees-2 do
  403.       c = c + 1
  404.       found = true
  405.       turtle.dig()
  406.       turtle.forward()
  407.       turtle.turnLeft()
  408.       if turtle.detect() then
  409.         turtle.dig()
  410.         turtle.forward()
  411.         turtle.dig()
  412.         turtle.turnLeft()
  413.         turtle.dig()
  414.         turtle.turnLeft()
  415.         turtle.turnLeft()
  416.         turtle.dig()
  417.         turtle.turnLeft()        
  418.         turtle.back()
  419.       end
  420.       turtle.turnLeft()
  421.       turtle.turnLeft()
  422.       if turtle.detect() then
  423.         turtle.dig()
  424.         turtle.forward()
  425.         turtle.dig()
  426.         turtle.turnLeft()
  427.         turtle.dig()
  428.         turtle.turnLeft()
  429.         turtle.turnLeft()
  430.         turtle.dig()
  431.         turtle.turnLeft()        
  432.         turtle.back()
  433.       end
  434.       turtle.turnLeft()
  435.     end
  436.     for a=1,c do turtle.back() end
  437.     turtle.turnLeft()
  438.   end
  439.   return found
  440. end
  441. function harvestWood()
  442.   if isWood() then      
  443.     turtle.dig()
  444.     return true
  445.   else
  446.     return false
  447.   end
  448. end
  449. function harvestTrunk()
  450.   -- Go up the trunk and clear each level spreading out up to half the distance between the trees.
  451.   turtle.select(slotWood)
  452.   if turtle.detect() then
  453.     turtle.dig()
  454.     turtle.forward()
  455.     turtle.suck()
  456.     local h = 0
  457.     if harvestWide then
  458.       while harvestLevel() or isWoodUp() do
  459.         turtle.digUp()
  460.         if turtle.up() then h = h + 1 end
  461.       end
  462.     else
  463.       while isWoodUp() do
  464.         turtle.digUp()
  465.         if turtle.up() then h = h + 1 else break end
  466.       end
  467.     end
  468.     local d  = 1
  469.     refuel()
  470.     while d <= h do
  471.       if turtle.detectDown() then
  472.         turtle.digDown()
  473.       end
  474.       if turtle.down() then
  475.         d = d + 1
  476.       end
  477.     end
  478.     turtle.suck()
  479.     turtle.back()
  480.   end
  481. end
  482. function saplingsAvailable()
  483.   local saplingCount = 0
  484.   for slot=1,16 do
  485.     if slot == slotSapling then
  486.       saplingCount = saplingCount + turtle.getItemCount(slot)
  487.     else
  488.       turtle.select(slot)
  489.       if turtle.compareTo(slotSapling) then
  490.         saplingCount = saplingCount + turtle.getItemCount(slot)
  491.       end
  492.     end
  493.   end
  494.   return saplingCount
  495. end
  496. function facingAChest()
  497.   turtle.select(slotChest)
  498.   return turtle.compare()
  499. end
  500. function aboveAChest()
  501.   turtle.select(slotChest)
  502.   return turtle.compareDown()
  503. end
  504. function facingAFence()
  505.   turtle.select(slotFence)
  506.   if turtle.compare() then return true end
  507.   turtle.select(slotGate)
  508.   return turtle.compare()
  509. end
  510. function aboveAFence()
  511.   turtle.select(slotFence)
  512.   if turtle.compareDown() then return true end
  513.   turtle.select(slotGate)
  514.   return turtle.compareDown()
  515. end
  516. function serviceFurnaces()
  517.   -- Load up on wood first.
  518.   turtle.turnRight()
  519.   turtle.turnRight()
  520.   turtle.select(slotWood+1)
  521.   while turtle.suck() do
  522.     -- Keep taking wood until full.
  523.   end
  524.   -- Now unload the top 2 slots to get fuel
  525.   for slot=15,16 do
  526.     turtle.select(slot)
  527.     if turtle.compareTo(slotWood) then turtle.drop() end
  528.   end
  529.   turtle.turnRight()
  530.   turtle.turnRight()
  531.   updateStatus("Checking furnaces")
  532.   local distanceToFence = 0
  533.   moveForward()
  534.   turtle.turnLeft()
  535.   while not facingAFence() do
  536.     moveForward()
  537.     distanceToFence = distanceToFence + 1
  538.   end
  539.   turtle.up()
  540.   moveForward()
  541.   -- Should now be above the fence below the furnace.
  542.   if furnaceCount == nil then
  543.     if turtle.detectUp() then furnaceCount = 1 else furnaceCount = 0 end
  544.   end
  545.   furnaceIdx = 1
  546.   anotherFurnace = true
  547.   while turtle.detectUp() and anotherFurnace do
  548.     updateStatus("Loading furnace "..furnaceIdx)
  549.     while selectWood() and turtle.dropUp() do
  550.       -- Keep adding wood to furnace as fuel until out of wood or furnace is full.
  551.     end
  552.     -- Now move to the top to add material to smelt
  553.     if turtle.back() then
  554.       if turtle.up() then
  555.         if turtle.up() then
  556.           if turtle.forward() then
  557.             if turtle.detectDown() then
  558.               while selectWood() and turtle.dropDown() do
  559.                 -- Keep adding wood to furnace to smelt until out of wood or furnace is full.
  560.               end
  561.               -- Now move to the right of the furnace to remove fuel
  562.               turtle.turnLeft()
  563.               if turtle.back() then
  564.                 if turtle.down() then
  565.                   updateStatus("Unloading furnace "..furnaceIdx)
  566.                   turtle.select(slotFuel)
  567.                   while turtle.suck() do
  568.                     -- Remove all fuel in furnace
  569.                   end
  570.                   turtle.turnRight()
  571.                   turtle.turnRight()
  572.                   if not turtle.detect() then anotherFurnace = false end
  573.                   turtle.turnLeft()
  574.                   turtle.turnLeft()
  575.                   turtle.up()
  576.                 end
  577.                 turtle.forward()
  578.               end
  579.               turtle.turnRight()
  580.             end
  581.             moveBack()
  582.           end
  583.           turtle.down()
  584.         end
  585.         turtle.down()
  586.       end
  587.       turtle.forward()
  588.     end
  589.     -- Now go to the next furnace if there is wood available
  590.     if selectWood() and anotherFurnace then
  591.       turtle.turnRight()
  592.       if turtle.forward() then
  593.         if turtle.forward() then
  594.           furnaceIdx = furnaceIdx + 1
  595.           if turtle.detectUp() then furnaceCount = furnaceCount + 1 end
  596.         end
  597.       end
  598.       turtle.turnLeft()
  599.     end
  600.   end
  601.   -- Now return the first furnace
  602.   if furnaceIdx > 1 then
  603.     turtle.turnLeft()
  604.     for bi = 1,furnaceIdx-1 do
  605.       turtle.forward()
  606.       turtle.forward()
  607.     end
  608.     turtle.turnRight()
  609.   end
  610.   -- Now return to start and unload extra wood and fuel
  611.   updateStatus("Returning to start.")
  612.   turtle.turnLeft()
  613.   turtle.turnLeft()
  614.   moveForward()
  615.   if not turtle.down() then
  616.     turtle.digDown()
  617.     while not turtle.down() do
  618.       updateStatus("Down blocked.")
  619.       sleep(10)
  620.     end
  621.   end
  622.   for di = 1,distanceToFence do moveForward() end
  623.   turtle.turnRight()
  624.   moveForward()
  625.   -- Should be back at the start. - make sure.
  626.   turtle.select(slotChest)
  627.   if not turtle.compareDown() then
  628.     -- Lost so find home.
  629.     updateStatus("Lost. Returning to start.")
  630.     locateStartingPosition()
  631.     turtle.turnRight()
  632.     turtle.turnRight()
  633.   end
  634.   -- Unload extra fuel.
  635.   for s = slotWood+1,16 do
  636.     turtle.select(s)
  637.     if turtle.getItemCount(s) > 0 then
  638.       if not turtle.dropDown() then
  639.         updateStatus("Fuel chest full. Waiting...")
  640.         while not turtle.dropDown() do sleep(30) end
  641.       end
  642.     end
  643.   end
  644.   -- Unload extra wood
  645.   for s = slotWood+1,16 do
  646.     turtle.select(s)
  647.     if turtle.compareTo(slotWood) then
  648.       if not turtle.drop() then
  649.         updateStatus("Inventory chest full. Waiting...")
  650.         while not turtle.drop() do sleep(30) end
  651.       end
  652.     end
  653.   end
  654.   turtle.select(slotWood)
  655.   turtle.drop(turtle.getItemCount(slotWood)-1)
  656.   turtle.turnLeft()
  657.   turtle.turnLeft()
  658.   updateStatus("Finished servicing furnaces.")
  659. end
  660. function locateStartingPosition()
  661.   -- Move down and forward until a chest or fence is located.
  662.   local atHome = false
  663.   -- In case stopped while servicing furnaces.
  664.   if turtle.detectUp() or turtle.detectDown() then
  665.     if not turtle.back() then
  666.       turtle.turnRight()
  667.       turtle.back()
  668.     end
  669.   end
  670.   -- Now get down to the ground.
  671.   while not turtle.detectDown() do turtle.down() end
  672.   if aboveAChest() then
  673.     print("Above a chest")
  674.     while facingAChest() do
  675.       turtle.turnRight()
  676.     end
  677.     print("At home")
  678.     atHome = true
  679.   end
  680.   while not atHome do
  681.     updateStatus("Looking for a chest")
  682.     while not facingAChest() do
  683.       refuel()
  684.       while facingAFence() do
  685.         turtle.turnRight()
  686.         updateStatus("Found a fence")
  687.       end
  688.       if not goForward() then
  689.         print("Can't go fwd")
  690.         if isWood() then
  691.           turtle.turnRight()
  692.         elseif turtle.up() then
  693.           if goForward() then
  694.             if aboveAFence() then
  695.               updateStatus("Found a fence")
  696.               turtle.back()
  697.               turtle.turnRight()
  698.             end
  699.           else
  700.             turtle.turnRight()
  701.           end
  702.         else
  703.           turtle.turnLeft()
  704.         end
  705.       end
  706.       if aboveAFence() then
  707.         updateStatus("Found a fence")
  708.         turtle.back()
  709.         turtle.turnRight()
  710.       end
  711.       while not turtle.detectDown() do turtle.down() end
  712.     end
  713.     updateStatus("Found a chest")
  714.     -- Now we should be facing a Chest.
  715.     if aboveAChest() then
  716.       atHome = true
  717.     else
  718.       turtle.turnLeft()
  719.       if facingAFence() then
  720.         turtle.turnRight()
  721.         break
  722.       else
  723.         turtle.turnRight()
  724.         turtle.turnRight()
  725.         if facingAFence() then
  726.           turtle.turnLeft()
  727.           break
  728.         end
  729.       end
  730.     end
  731.   end
  732.   local overChests = true
  733.   if not atHome then
  734.     turtle.up()
  735.     turtle.forward()
  736.     sleep(.3)
  737.     if not aboveAChest() then overChests = false end
  738.     turtle.forward()
  739.     sleep(.3)
  740.     if not aboveAChest() then overChests = false end
  741.     turtle.forward()
  742.     turtle.down()
  743.     sleep(.3)
  744.     if not aboveAChest() then overChests = false end
  745.   end
  746.   if overChests then atHome = true else updateStatus("Lost a chest") end
  747.   if atHome then
  748.     -- Should now be in the home position.
  749.     -- Now determine the correct orientation.
  750.     local foundOpening = false
  751.     for d=1,4 do
  752.       if not facingAChest() then
  753.         foundOpening = true
  754.         updateStatus("In start position.")
  755.         break
  756.       end
  757.       turtle.turnLeft()
  758.     end
  759.     if not foundOpening then
  760.       while true do
  761.         updateStatus("I'm lost and need help...")
  762.         sleep(60)
  763.       end
  764.     end
  765.   else
  766.     while true do
  767.       updateStatus("I'm lost and need help...")
  768.       sleep(60)
  769.     end
  770.   end
  771. end
  772. function suckItUp(n)
  773.   local d
  774.   for d=1,4 do
  775.     if n%2 == 0 then turtle.turnLeft() else turtle.turnRight() end
  776.     turtle.suck()
  777.   end
  778. end
  779.  
  780. -- Program begins
  781. updateStatus("Starting")
  782. term.clear()
  783. term.setCursorPos(1,1)
  784. print("Velander's Tree Farmer "..version)
  785. print("Turtle ID: "..minerID)
  786. getConsoleID()
  787. checkSetup()
  788. local col = 0
  789. local row = 0
  790. local slot = 0
  791. local leftShift = 0
  792. fuelChestEmpty = false
  793. locateStartingPosition()
  794. abortCircuit = false
  795.  
  796. while true do
  797.   -- Before we start planting make sure the turtle is properly equiped
  798.   print("Checking fuel")  
  799.   if turtle.getItemCount(slotFuel) < 64 then
  800.     -- Load up on fuel.
  801.     print("Getting fuel "..64-turtle.getItemCount(slotFuel))  
  802.     turtle.select(slotFuel)
  803.     if not turtle.suckDown(64-turtle.getItemCount(slotFuel)) then fuelChestEmpty = true end
  804.   end
  805.   -- Put back any extra fuel
  806.   for d=1,16 do
  807.     turtle.select(d)
  808.     if d ~= slotFuel and turtle.compareTo(slotFuel) then turtle.dropDown() end
  809.   end
  810.   if fuelChestEmpty then serviceFurnaces() end
  811.   if not refuel() then
  812.     updateStatus("Out of fuel. Waiting.")
  813.     while not refuel() do sleep(30) end
  814.   end
  815.   print("Checking bone meal")
  816.   if turtle.getItemCount(slotBonemeal) < (treesPerRow * numberOfRows) then
  817.     -- Load up on bonemeal if available.
  818.     turtle.turnRight()
  819.     -- Get enough bonemeal to fertilize all trees if necessary
  820.     turtle.select(slotBonemeal)
  821.     turtle.suck((treesPerRow * numberOfRows)-turtle.getItemCount(slotBonemeal)+1)
  822.     turtle.turnLeft()
  823.   end
  824.   print("Checking saplings")
  825.   local saplingCount = saplingsAvailable()
  826.   if saplingCount <= (treesPerRow * numberOfRows) then
  827.     turtle.turnLeft()
  828.     turtle.select(slotSapling)
  829.     while turtle.suck((treesPerRow * numberOfRows)-saplingCount+1) do
  830.       saplingCount = saplingsAvailable()
  831.       if saplingCount > (treesPerRow * numberOfRows) then break end
  832.       turtle.select(slotSapling)
  833.     end
  834.     turtle.turnRight()
  835.   end
  836.   if saplingsAvailable() <= 1 then
  837.     -- Out of saplings so restock from chest
  838.     updateStatus("Out of saplings.")
  839.   end
  840.   local reachedEndOfCol = false
  841.   local reachedEndOfRows = false
  842.   local rowLength = 0
  843.   local row = 1
  844.   while not reachedEndOfRows do
  845.     -- Move to beginning of grove.
  846.     if row == 1 then
  847.       moveForward()
  848.       turtle.turnLeft()
  849.       while not facingAFence() do
  850.         moveForward()
  851.         leftShift = leftShift + 1
  852.       end
  853.       moveBack()
  854.       leftShift = leftShift - 1
  855.       moveBack()
  856.       leftShift = leftShift - 1
  857.       turtle.turnRight()
  858.     else
  859.       turtle.turnRight()
  860.       updateStatus("Moving to row "..row)
  861.       for p=1,spaceBetweenTrees do
  862.         if facingAFence() then
  863.           reachedEndOfRows = true
  864.           for l=1,p-1 do moveBack() end
  865.           row = row - 1
  866.           break
  867.         else
  868.           moveForward()
  869.           suckItUp(p)
  870.         end
  871.       end
  872.       if not reachedEndOfRows then
  873.         moveBack()
  874.         moveBack()
  875.       end
  876.       turtle.turnLeft()
  877.     end
  878.     if not reachedEndOfRows then
  879.       updateStatus("Starting row "..row)
  880.       reachedEndOfCol = false
  881.       treesPerRow = 0
  882.       rowLength = 0
  883.       while not reachedEndOfCol do
  884.         refuel()
  885.         if abortCircuit then break end
  886.         -- Move between trees sucking as you go.
  887.         for p=1,spaceBetweenTrees do
  888.           if facingAFence() then
  889.             updateStatus("Found a fence in row "..row)
  890.             reachedEndOfCol = true
  891.           else
  892.             moveForward()
  893.             suckItUp(p)
  894.             rowLength = rowLength + 1
  895.           end        
  896.         end
  897.         if facingAFence() then
  898.           reachedEndOfCol = true
  899.           moveBack()
  900.           suckItUp(row)
  901.           rowLength = rowLength - 1
  902.         end          
  903.         if not reachedEndOfCol then
  904.           treesPerRow = treesPerRow + 1
  905.           turtle.turnRight()
  906.           local found = false
  907.           if turtle.detect() then
  908.             -- Check to see if it is the wood we are harvesting
  909.             if isWood() then
  910.               updateStatus("Harvesting at "..row..","..treesPerRow)
  911.               if refuel() then harvestTrunk() else abortCircuit = true end
  912.             elseif facingAChest() then
  913.               abortCircuit = true
  914.             elseif facingAFence() then
  915.               abortCircuit = true
  916.             elseif turtle.detect() then
  917.               updateStatus("Sapling at "..row..","..treesPerRow)
  918.               found = true
  919.               applyBonemeal()
  920.               if isWood() then
  921.                 updateStatus("Harvesting at "..row..","..treesPerRow)
  922.                 if refuel() then harvestTrunk() else abortCircuit = true end
  923.               end
  924.             end
  925.           end
  926.           if not abortCircuit then
  927.             if not isWood() and not isSapling() then
  928.               if plantSapling() then
  929.                 updateStatus("Sapling planted at "..row..","..treesPerRow)
  930.               end
  931.             end
  932.           end
  933.           turtle.turnLeft()
  934.         end
  935.       end
  936.       if abortCircuit then
  937.         updateStatus("Aborting circuit.")
  938.         turtle.turnLeft()
  939.         turtle.turnLeft()
  940.         locateStartingPosition()
  941.         turtle.turnLeft()
  942.         turtle.turnLeft()
  943.         break
  944.       else
  945.         updateStatus("Reached end of row "..row)
  946.         moveForward()
  947.         suckItUp(row)
  948.         rowLength = rowLength + 1
  949.         turtle.turnRight()
  950.         turtle.turnRight()
  951.         -- returing to bottom of grove.
  952.         for p=1,rowLength do
  953.           suckItUp(p)
  954.           moveForward()
  955.         end
  956.         turtle.turnLeft()
  957.         moveForward()
  958.         moveForward()
  959.         turtle.turnLeft()
  960.       end
  961.       row = row + 1
  962.     end
  963.   end
  964.   if not abortCircuit then
  965.     numberOfRows = row
  966.     -- Should be back to the begging of the grove at the last column of trees.
  967.     turtle.turnLeft()
  968.     -- Return to start
  969.     updateStatus("Returning to start.")
  970.     local sr = (spaceBetweenTrees * (numberOfRows-1))+2 - leftShift
  971.     for p=1,sr do
  972.       turtle.suck()
  973.       if facingAChest() or facingAFence() then
  974.         updateStatus("Lost. Returning to start.")
  975.         locateStartingPosition()
  976.         abortCircuit = true
  977.         turtle.turnLeft()
  978.         turtle.turnLeft()
  979.       else
  980.         moveForward()
  981.       end
  982.     end
  983.     -- Should be back in the start position.
  984.     if not abortCircuit then
  985.       turtle.turnLeft()
  986.       turtle.forward()
  987.     end
  988.   end
  989.   abortCircuit = false
  990.   if not facingAChest() or not aboveAChest() then
  991.     locateStartingPosition()
  992.     turtle.turnLeft()
  993.     turtle.turnLeft()
  994.   end
  995.   -- Now facing inventory chest.
  996.   updateStatus("Storing wood.")
  997.   for slot=slotWood+1,16 do
  998.     turtle.select(slot)
  999.     local dropped = true
  1000.     if turtle.compareTo(slotSapling) then
  1001.       -- Leave for now
  1002.     elseif turtle.getItemCount(slot) > 1 then
  1003.       if not turtle.drop() then dropped = false end
  1004.     end
  1005.     if dropped and turtle.getItemCount(slotWood) > 1 then
  1006.       turtle.select(slotWood)
  1007.       if not turtle.drop(turtle.getItemCount(slotWood)-1) then dropped = false end
  1008.     end
  1009.     if not dropped and (furnaceCount == nil or furnaceCount > 0) then
  1010.       -- Inventory must be full.  Make sure that the furnaces are busy.
  1011.       updateStatus("Servicing furnaces...")
  1012.       turtle.turnRight()
  1013.       turtle.turnRight()
  1014.       refuel()
  1015.       serviceFurnaces()
  1016.       turtle.turnRight()
  1017.       turtle.turnRight()
  1018.       break
  1019.     end
  1020.   end
  1021.   turtle.turnRight()
  1022.   -- Now facing chest with saplings.  Unload extra saplings
  1023.   updateStatus("Storing saplings.")
  1024.   for slot=1,16 do
  1025.     turtle.select(slot)
  1026.     -- Check to see if it is a sapling
  1027.     if slot ~= slotSapling and turtle.compareTo(slotSapling) then
  1028.       if not turtle.drop() then
  1029.         updateStatus("Sapling chest full.")
  1030.         turtle.up()
  1031.         turtle.drop()
  1032.         turtle.down()
  1033.       end
  1034.     end
  1035.   end
  1036.   turtle.turnRight()
  1037.   -- Back in start position.
  1038.   updateStatus("Pausing before next circuit...")
  1039.   sleep(30)
  1040. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement