dadragon84

turtlefarm(no fuel required)

Aug 22nd, 2015 (edited)
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.66 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 getDeviceSide(device)
  68.               dSide = nil
  69.              
  70.               local dName = device
  71.               if dName == "monitor" then
  72.                   dName = "monitor"
  73.               elseif dName == "modem" then
  74.                   dname = "modem"
  75.               else
  76.                   error("No Device Named: " .. device)
  77.               end
  78.  
  79.               -- Determine where device is.
  80.              
  81.               print("Looking for device,")
  82.               if peripheral.isPresent("left") and peripheral.getType("left")==dName then
  83.                 dSide = "left"
  84.               elseif peripheral.isPresent("right") and peripheral.getType("right")==dName then
  85.                 dSide = "right"
  86.               elseif peripheral.isPresent("top") and peripheral.getType("top")==dName then
  87.                 dSide = "top"
  88.               elseif peripheral.isPresent("back") and peripheral.getType("back")==dName then
  89.                 dSide = "back"
  90.               elseif peripheral.isPresent("bottom") and peripheral.getType("bottom")==dName then
  91.                 dSide = "bottom"
  92.               else
  93.                 print("No " .. device )
  94.                 return
  95.               end
  96.               print(dName .. " found on "..dSide)
  97.               return dSide
  98.                    
  99.             end
  100.            
  101.            
  102.              
  103.             function updateStatus(msg, silent)
  104.               if not rednet.isOpen(getDeviceSide("modem")) then rednet.open(getDeviceSide("modem")) end
  105.               if rednet.isOpen(getDeviceSide("modem")) then
  106.                 if turtleName == nil then return end
  107.                 networkmsg = "MM:"..turtleName..": "..msg
  108.                 if consoleID == 0 then
  109.                   rednet.broadcast(networkmsg)
  110.                 else
  111.                   rednet.send(consoleID,networkmsg)
  112.                 end
  113.               end
  114.               if not silent then print(msg) end
  115.             end
  116.             function getTurtleName()
  117.               local fname = "TurtleName"
  118.               if fs.exists(fname) then
  119.                 file = fs.open(fname, "r")
  120.                 turtleName = file.readAll()
  121.                 file.close()
  122.               else
  123.                 term.write("Turtle Name: ")
  124.                 turtleName = io.read()
  125.                 file = fs.open(fname, "w")
  126.                 file.write(turtleName)
  127.                 file.close()
  128.               end
  129.             end
  130.             function loadFuel()
  131.               turtle.turnRight()
  132.               turtle.turnRight()
  133.               turtle.select(slotFuel)
  134.               if turtle.suck() then
  135.                 for d=1,16 do
  136.                   if d ~= slotFuel then
  137.                     turtle.select(d)
  138.                     if turtle.compareTo(slotFuel) then turtle.drop() end
  139.                   end
  140.                 end
  141.               else
  142.                 updateStatus("No Fuel Required.")
  143.               end
  144.               turtle.turnRight()
  145.               turtle.turnRight()
  146.             end
  147.             function loadSaplings()
  148.               if turtle.getItemCount(slotSapling) < 2 then
  149.                 turtle.select(slotSapling)
  150.                 if turtle.suckDown(2) then
  151.                   for d=1,16 do
  152.                     if d ~= slotSapling then
  153.                       turtle.select(d)
  154.                       if turtle.compareTo(slotSapling) then turtle.dropDown() end
  155.                     end
  156.                   end
  157.                   return true
  158.                 else
  159.                   updateStatus("Sapling chest is empty.")
  160.                   return false
  161.                 end
  162.               end
  163.             end
  164.             function loadFertilizer()
  165.               turtle.turnRight()
  166.               turtle.select(slotFertilizer)
  167.               if turtle.suck() then
  168.                 for d=1,16 do
  169.                   if d ~= slotFertilizer then
  170.                     turtle.select(d)
  171.                     if turtle.compareTo(slotFertilizer) then turtle.drop() end
  172.                   end
  173.                 end
  174.               else
  175.                 updateStatus("Fertilizer chest is empty.")
  176.               end
  177.               turtle.turnLeft()
  178.             end
  179.             function unloadWood()
  180.               turtle.turnLeft()
  181.               for d = 1,16 do
  182.                 turtle.select(d)
  183.                 if d ~= slotFuel and d ~= slotSapling and d ~= slotFertilizer and d ~= slotFence and d ~= slotChest and d ~= slotWood then
  184.                   if turtle.getItemCount(d) > 0 then
  185.                     if not turtle.drop() then
  186.                       updateStatus("Wood chest is full.")
  187.                       break
  188.                     end
  189.                   end
  190.                 end
  191.               end
  192.               if turtle.getItemCount(slotWood) > 1 then
  193.                 turtle.select(slotWood)
  194.                 turtle.drop(turtle.getItemCount(slotWood)-1)
  195.               end
  196.               --
  197.               turtle.turnRight()
  198.             end
  199.             function unloadSaplings()
  200.               for d=1,16 do
  201.                 if d ~= slotSapling then
  202.                   turtle.select(d)
  203.                   if turtle.compareTo(slotSapling) then turtle.dropDown() end
  204.                 end
  205.               end
  206.               if turtle.getItemCount(slotSapling)>2 then
  207.                 turtle.select(slotSapling)
  208.                 turtle.dropDown(turtle.getItemCount(slotSapling)-2)
  209.               end
  210.             end
  211.             function getConsoleID()
  212.               local fname = "ConsoleID"
  213.               if fs.exists(fname) then
  214.                 file = fs.open(fname, "r")
  215.                 consoleID = tonumber(file.readAll())
  216.                 file.close()
  217.                 print("Monitor Console ID: "..consoleID)
  218.               else
  219.                 updateStatus("Waiting for Console ID...",true)
  220.                 term.write("Monitor Console ID: ")
  221.                 consoleID = io.read()
  222.                 file = fs.open(fname, "w")
  223.                 file.write(consoleID)
  224.                 file.close()
  225.                 consoleID = tonumber(consoleID)
  226.               end
  227.             end
  228.             function locateStartingPosition()
  229.               turtle.select(slotChest)
  230.               if not turtle.compareDown() then
  231.                 -- Not above a chest.
  232.                 while turtle.back() do
  233.                   if turtle.compareDown() then break end
  234.                 end
  235.                 if not turtle.compareDown() then
  236.                   while true do
  237.                     updateStatus("Can't find starting position. Need help.")
  238.                     sleep(60)
  239.                   end
  240.                 end
  241.               end
  242.               for d=1,4 do
  243.                 if not turtle.detect() then break end
  244.                 turtle.turnLeft()
  245.               end
  246.               if turtle.detect() then
  247.                 while true do
  248.                   updateStatus("Can't find starting position. Need help.")
  249.                   sleep(60)
  250.                 end
  251.               end
  252.             end
  253.             function checkSetup()
  254.               while turtle.getItemCount(slotFuel) < 2 do
  255.                 updateStatus("Fuel needed in slot "..slotFuel)
  256.                 sleep(20)
  257.               end
  258.               while turtle.getItemCount(slotSapling) < 2 do
  259.                 if not loadSaplings() then
  260.                   updateStatus("Saplings needed in slot "..slotSapling)
  261.                   sleep(20)
  262.                 end
  263.               end
  264.               if turtle.getItemCount(slotFertilizer) < 1 then
  265.                 updateStatus("Fertilizer needed in slot "..slotFertilizer)
  266.               end
  267.               while turtle.getItemCount(slotFence) < 1 do
  268.                 updateStatus("Fence needed in slot "..slotFence)
  269.                 sleep(20)
  270.               end
  271.               while turtle.getItemCount(slotChest) < 1 do
  272.                 updateStatus("Chest needed in slot "..slotChest)
  273.                 sleep(20)
  274.               end
  275.               while turtle.getItemCount(slotWood) < 1 do
  276.                 updateStatus("Wood needed in slot "..slotWood)
  277.                 sleep(20)
  278.               end
  279.             end
  280.             function harvestTrunk()
  281.               -- Go up the trunk and clear each level spreading out up to half the distance between the trees.
  282.               turtle.select(slotWood)
  283.               if turtle.detect() then
  284.                 turtle.dig()
  285.                 turtle.forward()
  286.                 local h = 0
  287.                 if harvestWide then
  288.                   while harvestLevel() or isWoodUp() do
  289.                     turtle.digUp()
  290.                     if turtle.up() then h = h + 1 end
  291.                   end
  292.                 else
  293.                   while isWoodUp() do
  294.                     turtle.digUp()
  295.                     updateStatus("Chopping Tree Down")
  296.                     if turtle.up() then h = h + 1 else break end
  297.                   end
  298.                 end
  299.                 local d  = 1
  300.                 while d <= h do
  301.                   if turtle.detectDown() then
  302.                     turtle.digDown()
  303.                   end
  304.                   if turtle.down() then
  305.                     d = d + 1
  306.                   end
  307.                 end
  308.                 turtle.back()
  309.               end
  310.             end
  311.             function selectSapling()
  312.               -- Look at all of the slots and if a slot other than the default sapling slot contains saplings, then return this slot.
  313.               local slot = 0
  314.               for slot=1,16 do
  315.                 if slot ~= slotSapling then
  316.                   if turtle.getItemCount(slot) > 1 then
  317.                     turtle.select(slot)
  318.                     if turtle.compareTo(slotSapling) then return true end
  319.                   end
  320.                 end
  321.               end
  322.               if turtle.getItemCount(slotSapling) > 1 then
  323.                 turtle.select(slotSapling)
  324.                 return true
  325.               end
  326.               return false
  327.             end
  328.             function selectFertilizer()
  329.               -- 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.
  330.               local slot = 0
  331.               for slot=1,16 do
  332.                 if slot ~= slotFertilizer then
  333.                   if turtle.getItemCount(slot) > 1 then
  334.                     turtle.select(slot)
  335.                     if turtle.compareTo(slotFertilizer) then return true end
  336.                   end
  337.                 end
  338.               end
  339.               if turtle.getItemCount(slotFertilizer) > 1 then
  340.                 turtle.select(slotFertilizer)
  341.                 return true
  342.               end
  343.               return false
  344.             end
  345.             function isWood()
  346.               turtle.select(slotWood)
  347.               local woodFound = turtle.compare()
  348.               return woodFound
  349.             end
  350.             function isWoodUp()
  351.               turtle.select(slotWood)
  352.               local woodFound = turtle.compareUp()
  353.               return woodFound
  354.             end
  355.             function isSapling()
  356.               local saplingFound = false
  357.               turtle.select(slotSapling)
  358.               saplingFound = turtle.compare()
  359.               if not saplingFound then print("Not a sapling.") end
  360.               return saplingFound
  361.             end
  362.             function applyFertilizer()
  363.             updateStatus("Applying Fertilizer.")
  364.               -- Apply fertilizer and report if there is a problem.
  365.               if turtle.detect() then
  366.                 if selectFertilizer() then
  367.                   local cnt = 0
  368.                   local applied = false
  369.                   while not isWood() and cnt < 10 do
  370.                     if selectFertilizer() then
  371.                       if turtle.place() then applied = true end
  372.                       cnt = cnt + 1
  373.                     end
  374.                   end
  375.                   if applied then
  376.                     return true
  377.                   else
  378.                     updateStatus("Unable to fertilize.")
  379.                     return false
  380.                   end
  381.                 else
  382.                   updateStatus("Out of fertilizer.")
  383.                   return false
  384.                 end
  385.               else
  386.                 updateStatus("Nothing to fertilizer.")
  387.                 return false
  388.               end
  389.             end
  390.             -- Main program
  391.             updateStatus("Starting")
  392.             term.clear()
  393.             term.setCursorPos(1,1)
  394.             print("Sev's Tree Farmer Single "..version)
  395.             print("Turtle ID: "..minerID)
  396.             getTurtleName()
  397.             getConsoleID()
  398.             checkSetup()
  399.             locateStartingPosition()
  400.             -- Now start planting and harvensting trees.
  401.             while true do
  402.               -- Check for enouhg fuel.
  403.               if turtle.getItemCount(slotFuel) < 64 then loadFuel() end
  404.               if turtle.getItemCount(slotSapling) < 2 then loadSaplings() end
  405.               if turtle.getItemCount(slotFertilizer) < 30 then loadFertilizer() end
  406.               for l=1,5 do
  407.                 while not turtle.forward() do
  408.                   updateStatus("Blocked.")
  409.                   sleep(20)
  410.                 end
  411.               end
  412.               turtle.select(slotSapling)
  413.               turtle.suck()
  414.               turtle.select(slotWood)
  415.               if not turtle.compare() then applyFertilizer() end
  416.               turtle.select(slotWood)
  417.               if turtle.compare() then harvestTrunk() end
  418.               if not turtle.detect() then
  419.                 if selectSapling() then
  420.                   turtle.suck()
  421.                   turtle.place()
  422.                 end
  423.               end
  424.               for l=1,5 do
  425.                 while not turtle.back() do
  426.                   updateStatus("Blocked.")
  427.                   sleep(20)
  428.                 end
  429.               end
  430.               unloadWood()
  431.               unloadSaplings()
  432.               updateStatus("Waiting for Sapling to Grow.")
  433.               sleep(140)
  434.             end
Add Comment
Please, Sign In to add comment