Advertisement
dadragon84

turtlefarm(no fuel required)

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