dadragon84

Single Tree Farmer - NO FUEL -- Leaves Checker

Feb 27th, 2025 (edited)
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.97 KB | None | 0 0
  1.     --[[
  2.         TreeFarmerSingle
  3.          
  4.         by Severino
  5.          
  6.         Version 1.5
  7.          
  8.         Harvests a single tree and transfers wood to a chest, getting saplings and fuel from other chests.
  9.          
  10.         Create a square of fences with 10 on each side.  Leave the center fence out on one side.  Put a source water block in each corner.  
  11.          
  12.         + - - - - - - - - - +
  13.         | W               W |
  14.         |                   |
  15.         |                   |
  16.         |                   |
  17.         |         S         |
  18.         |                   |
  19.         |                   |
  20.         |                   |
  21.         | W               W |
  22.         + - - - -   - - - - +
  23.                HH T BB
  24.                 C P
  25.          
  26.         S  = Sapling.  Plant the first sapling.
  27.         W  = Source Water Block
  28.         HH = Stack of double chests for harvested wood starting at ground level.
  29.         BB = Double chest for bone meal at ground level.
  30.         P  = Single chest to store planks used for fuel at ground level
  31.         T  = Single chest below ground level to store saplings.  Felling or Mining turtle is placed above this chest.
  32.         C  = Crafting turtle at ground level
  33.          
  34.         Load Crafting turtle with PlankCrafter program and setup "startup" program to run this program. Leave turtle slots empty.  Run the startup program.
  35.          
  36.         Load Felling Turtle with TreeFarmerSingle program and setup "startup" program to run this program.
  37.         Prepare the Felling Turtle by placing the following items in it's slots.
  38.         Slot 1 - Stack of planks to use for fuel
  39.         Slot 2 - 2 Saplings
  40.         Slot 3 = Stack of Bone meal for fertilizer
  41.         Slot 4 = 1 Fence
  42.         Slot 5 = 1 Chest
  43.         Slot 6 = 1 wood block
  44.         Slot 7 = 1 leaves
  45.         The saplings, wood block and planks for fuel must all be the same type of tree.
  46.         Run the startup program.  The first time the program runs it will ask for the computer ID of the computer that the MinerMonitor program is running on.  Enter 0 if you don't have one.
  47.         Use the MinerMonitor computer to monitor the status of the turtles running the TreeFarmerSingle, TreeFarmer and Miner8 programs.
  48.         If you later want to start using a MinerMonitor program, delete the file ConsoleID from the turtle and restart the program.  It will ask for the computer ID again.
  49.          
  50.          
  51.     ]]--
  52.             version = "1.6 - NO FUEL - Leaves Check"
  53.             slotFuel = 1
  54.             slotSapling = 2
  55.             slotFertilizer = 3
  56.             slotFence = 4
  57.             slotChest = 5
  58.             slotWood = 6
  59.             slotLeaves = 7
  60.             minerID = os.getComputerID()
  61.             consoleID = nil
  62.             harvestWide = false
  63.             turtleName = nil
  64.             waitTime = 280
  65.            
  66.            
  67.             function waitCheck(waitTime)
  68.                 local wait = waitTime
  69.                 local i = 0
  70.                 repeat
  71.                   term.clear()
  72.                   term.setCursorPos(1,1)
  73.                   i = i + 1
  74.                   print("Waiting for Sapling to grow Timer: " .. i )
  75.                   updateStatus("Waiting for Sapling to grow")
  76.                   sleep(1)
  77.                 until i == wait
  78.             end
  79.            
  80.             function getDeviceSide(device)
  81.               dSide = nil
  82.              
  83.               local dName = device
  84.               if dName == "monitor" then
  85.                   dName = "monitor"
  86.               elseif dName == "modem" then
  87.                   dname = "modem"
  88.               else
  89.                   error("No Device Named: " .. device)
  90.               end
  91.  
  92.               -- Determine where device is.
  93.              
  94.               print("Looking for device,")
  95.               if peripheral.isPresent("left") and peripheral.getType("left")==dName then
  96.                 dSide = "left"
  97.               elseif peripheral.isPresent("right") and peripheral.getType("right")==dName then
  98.                 dSide = "right"
  99.               elseif peripheral.isPresent("top") and peripheral.getType("top")==dName then
  100.                 dSide = "top"
  101.               elseif peripheral.isPresent("back") and peripheral.getType("back")==dName then
  102.                 dSide = "back"
  103.               elseif peripheral.isPresent("bottom") and peripheral.getType("bottom")==dName then
  104.                 dSide = "bottom"
  105.               else
  106.                 print("No " .. device )
  107.                 return
  108.               end
  109.               print(dName .. " found on "..dSide)
  110.               return dSide
  111.                    
  112.             end
  113.            
  114.            
  115.              
  116.             function updateStatus(msg, silent)
  117.               if not rednet.isOpen(getDeviceSide("modem")) then rednet.open(getDeviceSide("modem")) end
  118.               if rednet.isOpen(getDeviceSide("modem")) then
  119.                 if turtleName == nil then return end
  120.                 networkmsg = "MM:"..turtleName..": "..msg
  121.                 if consoleID == 0 then
  122.                   rednet.broadcast(networkmsg)
  123.                 else
  124.                   rednet.send(consoleID,networkmsg)
  125.                 end
  126.               end
  127.               if not silent then print(msg) end
  128.             end
  129.             function getTurtleName()
  130.               local fname = "TurtleName"
  131.               if fs.exists(fname) then
  132.                 file = fs.open(fname, "r")
  133.                 turtleName = file.readAll()
  134.                 file.close()
  135.               else
  136.                 term.write("Turtle Name: ")
  137.                 turtleName = io.read()
  138.                 file = fs.open(fname, "w")
  139.                 file.write(turtleName)
  140.                 file.close()
  141.               end
  142.             end
  143.             function loadFuel()
  144.               turtle.turnRight()
  145.               turtle.turnRight()
  146.               turtle.select(slotFuel)
  147.               if turtle.suck() then
  148.                 for d=1,16 do
  149.                   if d ~= slotFuel then
  150.                     turtle.select(d)
  151.                     if turtle.compareTo(slotFuel) then turtle.drop() end
  152.                   end
  153.                 end
  154.               else
  155.                 updateStatus("No Fuel Required.")
  156.               end
  157.               turtle.turnRight()
  158.               turtle.turnRight()
  159.             end
  160.             function loadSaplings()
  161.               if turtle.getItemCount(slotSapling) < 2 then
  162.                 turtle.select(slotSapling)
  163.                 if turtle.suckDown(2) then
  164.                   for d=1,16 do
  165.                     if d ~= slotSapling then
  166.                       turtle.select(d)
  167.                       if turtle.compareTo(slotSapling) then turtle.dropDown() end
  168.                     end
  169.                   end
  170.                   return true
  171.                 else
  172.                   updateStatus("Sapling chest is empty.")
  173.                   return false
  174.                 end
  175.               end
  176.             end
  177.             function loadFertilizer()
  178.               turtle.turnRight()
  179.               turtle.select(slotFertilizer)
  180.               if turtle.suck() then
  181.                 for d=1,16 do
  182.                   if d ~= slotFertilizer then
  183.                     turtle.select(d)
  184.                     if turtle.compareTo(slotFertilizer) then turtle.drop() end
  185.                   end
  186.                 end
  187.               else
  188.                 updateStatus("Fertilizer chest is empty.")
  189.               end
  190.               turtle.turnLeft()
  191.             end
  192.             function unloadWood()
  193.               turtle.turnLeft()
  194.               for d = 1,16 do
  195.                 turtle.select(d)
  196.                 if d ~= slotFuel and d ~= slotSapling and d ~= slotFertilizer and d ~= slotFence and d ~= slotChest and d ~= slotWood and d ~= slotLeaves then
  197.                   if turtle.getItemCount(d) > 0 then
  198.                     if not turtle.drop() then
  199.                       updateStatus("Wood chest is full.")
  200.                       break
  201.                     end
  202.                   end
  203.                 end
  204.               end
  205.               if turtle.getItemCount(slotWood) > 1 then
  206.                 turtle.select(slotWood)
  207.                 turtle.drop(turtle.getItemCount(slotWood)-1)
  208.               end
  209.               --
  210.               turtle.turnRight()
  211.             end
  212.             function unloadSaplings()
  213.               for d=1,16 do
  214.                 if d ~= slotSapling then
  215.                   turtle.select(d)
  216.                   if turtle.compareTo(slotSapling) then turtle.dropDown() end
  217.                 end
  218.               end
  219.               if turtle.getItemCount(slotSapling)>2 then
  220.                 turtle.select(slotSapling)
  221.                 turtle.dropDown(turtle.getItemCount(slotSapling)-2)
  222.               end
  223.             end
  224.             function getConsoleID()
  225.               local fname = "ConsoleID"
  226.               if fs.exists(fname) then
  227.                 file = fs.open(fname, "r")
  228.                 consoleID = tonumber(file.readAll())
  229.                 file.close()
  230.                 print("Monitor Console ID: "..consoleID)
  231.               else
  232.                 updateStatus("Waiting for Console ID...",true)
  233.                 term.write("Monitor Console ID: ")
  234.                 consoleID = io.read()
  235.                 file = fs.open(fname, "w")
  236.                 file.write(consoleID)
  237.                 file.close()
  238.                 consoleID = tonumber(consoleID)
  239.               end
  240.             end
  241.             function locateStartingPosition()
  242.               turtle.select(slotChest)
  243.               if not turtle.compareDown() then
  244.                 -- Not above a chest.
  245.                 while turtle.back() do
  246.                   if turtle.compareDown() then break end
  247.                 end
  248.                 if not turtle.compareDown() then
  249.                   while true do
  250.                     updateStatus("Can't find starting position. Need help.")
  251.                     sleep(60)
  252.                   end
  253.                 end
  254.               end
  255.               for d=1,4 do
  256.                 if not turtle.detect() then break end
  257.                 turtle.turnLeft()
  258.               end
  259.               if turtle.detect() then
  260.                 while true do
  261.                   updateStatus("Can't find starting position. Need help.")
  262.                   sleep(60)
  263.                 end
  264.               end
  265.             end
  266.             function checkSetup()
  267.               while turtle.getItemCount(slotFuel) < 2 do
  268.                 updateStatus("Fuel needed in slot "..slotFuel)
  269.                 sleep(20)
  270.               end
  271.               while turtle.getItemCount(slotSapling) < 2 do
  272.                 if not loadSaplings() then
  273.                   updateStatus("Saplings needed in slot "..slotSapling)
  274.                   sleep(20)
  275.                 end
  276.               end
  277.               while turtle.getItemCount(slotFertilizer) < 1 do
  278.                 updateStatus("Fertilizer needed in slot "..slotFertilizer)
  279.                 sleep(20)
  280.               end
  281.               while turtle.getItemCount(slotLeaves) < 1 do
  282.                 updateStatus("Leaves needed in slot " .. slotLeaves)
  283.                 sleep(20)
  284.               end
  285.               while turtle.getItemCount(slotFence) < 1 do
  286.                 updateStatus("Fence needed in slot "..slotFence)
  287.                 sleep(20)
  288.               end
  289.               while turtle.getItemCount(slotChest) < 1 do
  290.                 updateStatus("Chest needed in slot "..slotChest)
  291.                 sleep(20)
  292.               end
  293.               while turtle.getItemCount(slotWood) < 1 do
  294.                 updateStatus("Wood needed in slot "..slotWood)
  295.                 sleep(20)
  296.               end
  297.             end
  298.             function harvestTrunk()
  299.               -- Go up the trunk and clear each level spreading out up to half the distance between the trees.
  300.               turtle.select(slotWood)
  301.               if turtle.detect() then
  302.                 turtle.dig()
  303.                 turtle.forward()
  304.                 local h = 0
  305.                 if harvestWide then
  306.                   while harvestLevel() or isWoodUp() do
  307.                     turtle.digUp()
  308.                     if turtle.up() then h = h + 1 end
  309.                   end
  310.                 else
  311.                   while isWoodUp() do
  312.                     turtle.digUp()
  313.                     updateStatus("Chopping Tree Down")
  314.                     if turtle.up() then h = h + 1 else break end
  315.                   end
  316.                 end
  317.                 local d  = 1
  318.                 while d <= h do
  319.                   if turtle.detectDown() then
  320.                     turtle.digDown()
  321.                   end
  322.                   if turtle.down() then
  323.                     d = d + 1
  324.                   end
  325.                 end
  326.                 turtle.back()
  327.               end
  328.             end
  329.             function selectSapling()
  330.               -- Look at all of the slots and if a slot other than the default sapling slot contains saplings, then return this slot.
  331.               local slot = 0
  332.               for slot=1,16 do
  333.                 if slot ~= slotSapling then
  334.                   if turtle.getItemCount(slot) > 1 then
  335.                     turtle.select(slot)
  336.                     if turtle.compareTo(slotSapling) then return true end
  337.                   end
  338.                 end
  339.               end
  340.               if turtle.getItemCount(slotSapling) > 1 then
  341.                 turtle.select(slotSapling)
  342.                 return true
  343.               end
  344.               return false
  345.             end
  346.             function selectFertilizer()
  347.               -- 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.
  348.               local slot = 0
  349.               for slot=1,16 do
  350.                 if slot ~= slotFertilizer then
  351.                   if turtle.getItemCount(slot) > 1 then
  352.                     turtle.select(slot)
  353.                     if turtle.compareTo(slotFertilizer) then return true end
  354.                   end
  355.                 end
  356.               end
  357.               if turtle.getItemCount(slotFertilizer) > 1 then
  358.                 turtle.select(slotFertilizer)
  359.                 return true
  360.               end
  361.               return false
  362.             end
  363.             function isWood()
  364.               turtle.select(slotWood)
  365.               local woodFound = turtle.compare()
  366.               return woodFound
  367.             end
  368.             function isWoodUp()
  369.               turtle.select(slotWood)
  370.               local woodFound = turtle.compareUp()
  371.               return woodFound
  372.             end
  373.             function isSapling()
  374.               local saplingFound = false
  375.               turtle.select(slotSapling)
  376.               saplingFound = turtle.compare()
  377.               if not saplingFound then print("Not a sapling.") end
  378.               return saplingFound
  379.             end
  380.             function isLeaves()
  381.               local leavesFound = false
  382.               turtle.select(slotLeaves)
  383.               leavesFound = turtle.compare()
  384.               if not leavesFound then
  385.                 print("Not Leaves.")
  386.               else
  387.                 turtle.select(slotLeaves)
  388.                 if turtle.detect() then
  389.                   turtle.dig()
  390.                   turtle.forward()
  391.                 end
  392.                  
  393.               end
  394.               return leavesFound
  395.             end
  396.             function applyFertilizer()
  397.             updateStatus("Applying Fertilizer.")
  398.               -- Apply fertilizer and report if there is a problem.
  399.               if turtle.detect() then
  400.                 if selectFertilizer() then
  401.                   local cnt = 0
  402.                   local applied = false
  403.                   while not isWood() and cnt < 10 do
  404.                     if selectFertilizer() then
  405.                       if turtle.place() then applied = true end
  406.                       cnt = cnt + 1
  407.                     end
  408.                   end
  409.                   if applied then
  410.                     return true
  411.                   else
  412.                     updateStatus("Unable to fertilize.")
  413.                     return false
  414.                   end
  415.                 else
  416.                   updateStatus("Out of fertilizer.")
  417.                   return false
  418.                 end
  419.               else
  420.                 updateStatus("Nothing to fertilizer.")
  421.                 return false
  422.               end
  423.             end
  424.             -- Main program
  425.             updateStatus("Starting")
  426.             term.clear()
  427.             term.setCursorPos(1,1)
  428.             print("MrJohnDowe's Tree Farmer Single "..version)
  429.             print("Turtle ID: "..minerID)
  430.             getTurtleName()
  431.             getConsoleID()
  432.             checkSetup()
  433.             locateStartingPosition()
  434.             -- Now start planting and harvensting trees.
  435.             while true do
  436.               -- Check for enouhg fuel.
  437.               if turtle.getItemCount(slotFuel) < 64 then loadFuel() end
  438.               if turtle.getItemCount(slotSapling) < 2 then loadSaplings() end
  439.               if turtle.getItemCount(slotFertilizer) < 30 then loadFertilizer() end
  440.               for l=1,5 do
  441.                 while not turtle.forward() and not isLeaves() do
  442.                   updateStatus("Blocked.")
  443.                   sleep(20)
  444.                 end
  445.               end
  446.               turtle.select(slotSapling)
  447.               turtle.suck()
  448.               turtle.select(slotWood)
  449.               if not turtle.compare() then applyFertilizer() end
  450.               turtle.select(slotWood)
  451.               if turtle.compare() then harvestTrunk() end
  452.               if not turtle.detect() then
  453.                 if selectSapling() then
  454.                   turtle.suck()
  455.                   turtle.place()
  456.                 end
  457.               end
  458.               for l=1,5 do
  459.                 while not turtle.back() and not isLeaves() do
  460.                   updateStatus("Blocked.")
  461.                   sleep(20)
  462.                 end
  463.               end
  464.               unloadWood()
  465.               unloadSaplings()
  466.               updateStatus("Waiting for Sapling to Grow.")
  467.               waitCheck(waitTime)
  468.             end
Add Comment
Please, Sign In to add comment