Advertisement
jironpaste

tree turtle farm minecraft computercraft

Apr 7th, 2020
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.46 KB | None | 0 0
  1. --[[Welcome to Jirons Tree Farming Turtle; in example the width = 5 and length = 6 ]]
  2. --[[
  3. x x x x x
  4. x x x x x
  5. x x x x x
  6. T R
  7. ]]
  8.  
  9. --                  --[[ T = Turtle, deposit chest placed under turtle,
  10. --                       R = refuel chest ]]--
  11. -- Use Odd numbers for Width (just cus me life is a mirror)
  12. -- https://i.imgur.com/q2pOFTh.png (Basic setup)
  13.  
  14. -- I use mainly oak for the farm.
  15. -- Place turtle on the chest facing away from the farm.
  16. -- feel free to make the farm 41 x 50 (free form)
  17. print("+-------------------------------------+")
  18. print("|    TimberFarmer 1.0, by Jiron       |")
  19. print("+-------------------------------------+")
  20. print("| Farm set-up: Place down 3 chests    |")
  21. print("| place the felling turtle on the     |")
  22. print("| chest to the right and facing away  |")
  23. print("| from the farm. Put saplings in the  |")
  24. print("| middle and fuel in the left chest.  |")
  25. print("|                                     |")
  26. print("|  Put some fuel in the turtle and    |")
  27. print("|  lets define the size of the farm.   |")
  28. print("+-------------------------------------+")
  29. print("Press enter to continue..")
  30. getready = io.read()
  31. print("+-------------------------------------+")
  32. print("+----------------Setup----------------+")
  33. print("| H = height, C = chest, T = turtle   |")
  34. print("|                                     |")
  35. print("| H = 1               T               |")
  36. print("| H = 0     C    C    C               |")
  37. print("|           L    M    R               |")
  38. print("|                                     |")
  39. print("|  Remember Turtle should be facing   |")
  40. print("|  away from intended farm area.      |")
  41. print("+-------------------------------------+")
  42. print("Press enter to continue..")
  43. print("https://i.imgur.com/q2pOFTh.png")
  44. print("Shows the basic setup")
  45. getsett = io.read()
  46. print("Width of the farm in ODD numbers only!")
  47. re = io.read()
  48.  
  49. width = tonumber(re)
  50. print("Length of farm?")
  51. rel = io.read()
  52. length = tonumber(rel)
  53. print("Farm size set to " .. width .. "x" .. length)
  54. print("Cycle time? in minutes")
  55. redir = io.read()
  56.  
  57. --Farm
  58. waitTime = tonumber(redir * 60)
  59. tRight = false
  60. tLeft = true
  61. start = true
  62. spinLeft = true
  63. treeHeight = 0
  64. count = 0
  65.  
  66. function noFaithInUser(number)
  67.     if (number % 2 == 0) then
  68.         print("Error: Width is not odd in size!")
  69.         return true
  70.     end
  71.     start = false
  72.     return flase
  73. end
  74.  
  75.  
  76.  
  77. function turnLeft()
  78.     -- print("left")
  79.     turtle.turnLeft()
  80.     block()
  81.     turtleMove()
  82.     turtleMove()
  83.     turtle.turnLeft()
  84.     tLeft = true
  85.     tRight = false
  86.     block()
  87. end
  88.  
  89. function turnRight()
  90.     --print("Right")
  91.     turtle.turnRight()
  92.     block()
  93.    
  94.     turtleMove()
  95.     turtleMove()
  96.     turtle.turnRight()
  97.     tRight = true
  98.     tLeft = false
  99.     block()
  100. end
  101.  
  102.  
  103.  
  104.  
  105. function block()
  106.     if turtle.detect() == true then
  107.         turtle.dig()
  108.         turtle.suck()
  109.         turtle.suckDown()
  110.     end
  111. end
  112.  
  113. function detectSapling(s)
  114.     res = string.find(s .. "", "sapling")
  115.     if res ~= nil then
  116.         return "yes";
  117.     else
  118.         return "no"
  119.     end
  120. end
  121.  
  122. function detectType(s, type)
  123.     res = string.find(s .. "", type)
  124.     if res ~= nil then
  125.         return "yes";
  126.     else
  127.         return "no"
  128.     end
  129. end
  130.  
  131.  
  132. function isSapling()
  133.     local suc, det = turtle.detect()
  134.     if suc then
  135.         local suc2, insp = turtle.inspect()
  136.         if suc2 then
  137.             local name = insp.name
  138.             return detectSapling(name)
  139.         end
  140.    
  141.     else
  142.         return "nothing"
  143.     end
  144. end
  145.  
  146. function evenOrOdd(number)
  147.     if number % 2 == 0 then
  148.         return true
  149.     else
  150.         return false
  151.     end
  152. end
  153.  
  154. function turtleMove()
  155.     turtle.dig()
  156.     turtle.suck()
  157.     local m = turtle.forward()
  158.     while m == false do
  159.         turtle.dig()
  160.         m = turtle.forward()
  161.     end
  162. end
  163.  
  164. function move(length, first)-- MOVE
  165.     if length >= 0 then
  166.         turtleMove()
  167.        
  168.         if first then
  169.             turtleMove()
  170.             turtleMove()
  171.             turtleMove()
  172.             length = length - 2
  173.         end
  174.        
  175.         if length > 1 then
  176.             turtle.turnLeft()
  177.             spinLeft = true
  178.             -- print("check L")
  179.             if isSapling() == "no" then
  180.                 harvest()
  181.            
  182.            
  183.             else
  184.                 planting()
  185.                 turtle.turnRight()
  186.             end
  187.            
  188.             turtle.turnRight()
  189.             spinLeft = false
  190.             --  print("check R")
  191.             if isSapling() == "no" then
  192.                 harvest()
  193.            
  194.            
  195.             else
  196.                 planting()
  197.                 turtle.turnLeft()
  198.             end
  199.         end
  200.        
  201.        
  202.         length = length - 1
  203.         --print("move")
  204.         move(length, false)
  205.    
  206.    
  207.     end
  208. end
  209.  
  210.  
  211.  
  212. function climDown(heightClimed, org)
  213.    
  214.     --  print("H: " .. heightClimed)
  215.     if heightClimed > 0 then
  216.         local isGrass = false
  217.         local isLeaves = false
  218.         local isDirt = false
  219.         local isLog = false
  220.         local suc, det = turtle.detectDown()
  221.         if suc then
  222.             local suc2, insp = turtle.inspectDown()
  223.             if suc2 then
  224.                 isLeaves = detectType(insp.name, "leaves")
  225.                 isLog = detectType(insp.name, "log")
  226.            
  227.             end
  228.        
  229.         end
  230.         if isLeaves == "yes" or isLog == "yes" then
  231.             --  print("climDown: " .. treeHeight)
  232.             turtle.digDown()
  233.             turtle.suck()
  234.             turtle.suckDown()
  235.        
  236.         end
  237.         turtle.down()
  238.         heightClimed = heightClimed - 1
  239.        
  240.         treeHeight = treeHeight - 1
  241.         climDown(heightClimed)
  242.     end
  243.  
  244.  
  245. end
  246.  
  247. function climDownAgain(height, original)
  248.     turtle.turnRight()
  249.     turtle.turnRight()
  250.     if turtle.detect() then
  251.         turtle.dig()
  252.         turtle.suck()
  253.         turtleMove()
  254.     end
  255.    
  256.     if treeHeight > 0 then
  257.         climDown(height, org)
  258.     end
  259. end
  260.  
  261. function checkForTimber()
  262.    
  263.     local isLeaves = false
  264.     local isLog = false
  265.     local suc, det = turtle.detectUp()
  266.     if suc then
  267.         local suc2, insp = turtle.inspectUp()
  268.         if suc2 then
  269.             isLeaves = detectType(insp.name, "leaves")
  270.             isLog = detectType(insp.name, "log")
  271.        
  272.         end
  273.    
  274.     end
  275.     if isLeaves == "yes" or isLog == "yes" then
  276.         --  print("climDown: " .. treeHeight)
  277.         return true
  278.     else
  279.         return false
  280.     end
  281.  
  282. end
  283.  
  284. function climUP()
  285.    
  286.     if checkForTimber() == true then
  287.         turtle.digUp()
  288.         turtle.suck()
  289.         turtle.suckUp()
  290.         turtle.up()
  291.         spinHarvest(4, treeHeight)
  292.         treeHeight = treeHeight + 1
  293.         climUP()
  294.    
  295.     else
  296.         climDownAgain(treeHeight, treeHeight)
  297.         if treeHeight == 0 then
  298.             turtleMove()
  299.             if spinLeft == true then
  300.                 turtle.turnLeft()
  301.             else
  302.                 turtle.turnRight()
  303.             end
  304.        
  305.        
  306.         end
  307.     end
  308. end
  309. function standardCut(spin)
  310.    
  311.     if spin > 0 then
  312.         turtle.turnLeft()
  313.         if isSapling() == "no" then
  314.             turtle.dig()
  315.         end
  316.         turtle.suck()
  317.         spin = spin - 1
  318.        
  319.         standardCut(spin)
  320.     else
  321.         return spin
  322.     end
  323. end
  324.  
  325. function chop()
  326.     turtle.dig()
  327.     turtle.suck()
  328. end
  329.  
  330.  
  331. function chopSides()
  332.     turtle.turnRight()
  333.     chop()
  334.     turtle.turnLeft()
  335.     chop()
  336.     turtle.turnLeft()
  337.     chop()
  338. end
  339.  
  340. function chopLeftTurn()
  341.     turtle.turnLeft()
  342.     chopMove()
  343. end
  344.  
  345. function chopMove()
  346.     turtleMove()
  347. end
  348.  
  349. function chopRight()
  350.     turtle.turnRight()
  351.     chop()
  352.     turtle.turnLeft()
  353. end
  354.  
  355.  
  356. function chopCorner()
  357.     chopMove()
  358.     chop()
  359.     chopLeftTurn()
  360.     chopSides()
  361. end
  362.  
  363. function chopUntilCorner()
  364.     chopMove()
  365.     chopRight()
  366.  
  367. end
  368.  
  369. function extendedCut()
  370.     chopCorner()
  371.     chopUntilCorner()
  372.     chopUntilCorner()
  373.     chopLeftTurn()
  374.     chopCorner()
  375.     chopMove()
  376.     turtle.turnRight()
  377.     return 0
  378.  
  379.  
  380. end
  381.  
  382. function spinHarvest(spinAround, h)
  383.     if h < 3 and spinAround > 0 then
  384.         spinAround = standardCut(spinAround)
  385.     else if h > 0 and spinAround > 0 then
  386.         spinAround = extendedCut()
  387.     end
  388.     end
  389. end
  390.  
  391. function checkSlotForSapling()
  392.     local suc, space = turtle.getItemSpace()
  393.     if suc then
  394.         local data = turtle.getItemDetail()
  395.         if data then
  396.             -- print(data.name)
  397.             local name = data.name
  398.             return detectSapling(name)
  399.         end
  400.     else return "no"
  401.     end
  402. end
  403.  
  404. function planting()
  405.     if checkSlotForSapling() == "yes" then
  406.         turtle.place()
  407.     else
  408.        
  409.         for i = 1, 16 do -- loop through the slots
  410.             turtle.select(i)-- change to the slot
  411.             if checkSlotForSapling() == "yes" then -- if it's valid
  412.                 turtle.place()
  413.             end
  414.         end
  415.     end
  416. end
  417.  
  418. function harvest()
  419.     turtle.dig()
  420.     turtle.suck()
  421.     turtle.suckDown()
  422.     turtleMove()
  423.     spinHarvest(4, 0)
  424.     climUP()
  425. end
  426.  
  427. function refuel(f)
  428.     for i = f, 16 do -- loop through the slots
  429.         turtle.select(i)-- change to the slot
  430.         if turtle.refuel(0) then -- if it's valid fuel
  431.             turtle.refuel()-- consume half the stack as fuel
  432.         end
  433.     end
  434. end
  435.  
  436.  
  437. function refuelTurtle()
  438.     turtleMove()
  439.     turtle.turnLeft()
  440.     turtle.turnLeft()
  441.     local fuelLevel = 0
  442.     local suc, fuelLevel = turtle.getFuelLevel
  443.     if turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() > 10000 then
  444.         turtle.suckDown()
  445.     else
  446.         for i = 2, 16 do -- loop through the slots
  447.             turtle.select(i)-- change to the slot
  448.             turtle.suckDown()
  449.         end
  450.     end
  451.    
  452.     refuel(2)
  453.     storeUnusedFuel()
  454.     turtleMove()
  455.     turtleMove()
  456.     turtle.turnLeft()
  457. end
  458.  
  459. function storeUnusedFuel()
  460.     for i = 2, 16 do -- loop through the slots
  461.         turtle.select(i)-- change to the slot
  462.         turtle.dropDown()
  463.     end
  464. end
  465.  
  466. function storeTimber()
  467.     for i = 1, 16 do -- loop through the slots
  468.         turtle.select(i)-- change to the slot
  469.         if checkSlotForSapling() == "no" then
  470.             turtle.dropDown()
  471.         end
  472.     end
  473. end
  474. function storeSapling()
  475.     turtle.turnRight()
  476.     turtleMove()
  477.     for i = 1, 16 do -- loop through the slots
  478.         turtle.select(i)-- change to the slot
  479.         if checkSlotForSapling() == "yes" then
  480.             turtle.dropDown()
  481.         end
  482.     end
  483.     -- resupply
  484.     turtle.select(1)
  485.     turtle.suckDown()
  486. end
  487.  
  488.  
  489.  
  490.  
  491.  
  492. function turtleRun()
  493.     for newWidth = width, 1, -2 do
  494.        
  495.         if newWidth == width then
  496.             move(length - 2, true)
  497.         else if newWidth < width and newWidth > 1 then
  498.             move(length - 2, false)
  499.         else
  500.             move(length - 1, false)-- set to -1 for mirror setup
  501.         end
  502.         end
  503.        
  504.        
  505.         if newWidth > 1 then
  506.             if tRight == false then
  507.                 turnRight()
  508.            
  509.             else
  510.                 turnLeft()
  511.            
  512.             end
  513.         end
  514.        
  515.         if newWidth == 1 then
  516.             return newWidth
  517.         end
  518.     end
  519. end
  520.  
  521.  
  522. function run()
  523.     local state = turtleRun()
  524.     if state == 1 then
  525.         returnToStart()
  526.     end
  527. end
  528.  
  529. function returnToStart()
  530.     turtle.turnLeft()
  531.     turtle.turnLeft()
  532.     turtleRun()
  533. end
  534.  
  535.  
  536.  
  537.  
  538.  
  539.  
  540. while true do
  541.    
  542.     if start == true then
  543.         if noFaithInUser(width) == true then
  544.             break
  545.         end
  546.         refuel(1)
  547.         storeSapling()
  548.         refuelTurtle()
  549.     end
  550.    
  551.     count = count + 1
  552.     print("Done " .. count .. " rounds ")
  553.    
  554.     run()
  555.     storeTimber()
  556.     storeSapling()
  557.     refuelTurtle()
  558.     sleep(waitTime)
  559. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement