Advertisement
charonme

chtreefarm

Apr 5th, 2013
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- chtreefarm
  2. -- refill fuel in slot FUEL_SLOT
  3. -- requires at least farmLength saplings in slot SAPLING_SLOT
  4. -- wood deposit at the beginning of the farm, saplings deposit at the end
  5.  
  6. local FUEL_SLOT = 1
  7. local SAPLING_SLOT = 2
  8. local SAPLING_TMP_SLOT = 3
  9. local WOOD_SLOT = 4
  10. local MIN_FUEL_FOR_STEP = 26
  11. local args = {...}
  12. local farmLength = tonumber(args[1])
  13. local posX = 0
  14. local orient = 0
  15. local cycleCount = 0
  16. local treesChopped = 0
  17. local saplingsPlanted = 0
  18. local distanceTraveled = 0
  19. local farmingStarted = false
  20.  
  21. function forward(n)
  22.     for i=1,n do
  23.         turtle.forward()
  24.         distanceTraveled = distanceTraveled + 1
  25.         if orient==0 then
  26.             posX = posX+1
  27.         end
  28.         if orient==2 then
  29.             posX = posX-1
  30.         end
  31.     end
  32. end
  33. function left()
  34.     turtle.turnLeft()
  35.     orient = orient-1
  36.     while orient<0 do
  37.         orient = orient+4
  38.     end
  39. end
  40. function right()
  41.     turtle.turnRight()
  42.     orient = orient+1
  43.     while orient>3 do
  44.         orient = orient-4
  45.     end
  46. end
  47. function transferSaplings()
  48.     result = true
  49.     for i = 1,16 do
  50.         if not i==SAPLING_SLOT then
  51.             turtle.select(i)
  52.             if turtle.compareTo(SAPLING_SLOT) and turtle.getItemCount(i)>0 then
  53.                 turtle.transferTo(SAPLING_SLOT)
  54.             end
  55.         end
  56.     end
  57.  
  58.     if turtle.getItemCount(SAPLING_SLOT) < 2 then
  59.         print("Not enough saplings in slot "..SAPLING_SLOT.."\n")
  60.         -- result = false
  61.         while turtle.getItemCount(SAPLING_SLOT) < 2 do
  62.             os.sleep(10)
  63.         end
  64.     end
  65.     return result
  66. end
  67. function stripLeaves()
  68.     turtle.select(SAPLING_TMP_SLOT)
  69.     if turtle.detectUp() then
  70.         turtle.digUp()
  71.     end
  72.     for i=1,3 do
  73.         right()
  74.         if turtle.detect() then
  75.             turtle.dig()
  76.         end
  77.     end
  78.     transferSaplings()
  79.     right()
  80. end
  81.  
  82. function chop()
  83.     local height = 0
  84.     while turtle.detect() do
  85.         turtle.select(WOOD_SLOT)
  86.         turtle.dig()
  87.         stripLeaves()
  88.         if turtle.up() then
  89.             height = height + 1
  90.             distanceTraveled = distanceTraveled + 1
  91.         end
  92.     end
  93.     turtle.select(SAPLING_TMP_SLOT)
  94.     for i=1,height do
  95.         turtle.digDown()
  96.         turtle.down()
  97.         distanceTraveled = distanceTraveled + 1
  98.     end
  99.     if height>2 then
  100.         treesChopped = treesChopped + 1
  101.     end
  102. end
  103. function plant()
  104.     turtle.select(SAPLING_SLOT)
  105.     if turtle.getItemCount(SAPLING_SLOT) < 2 then
  106.         if not transferSaplings() then
  107.             return false
  108.         end
  109.     end
  110.  
  111.     turtle.place()
  112.     os.sleep(0.5)
  113.     if turtle.detect() and turtle.compare() then
  114.         saplingsPlanted = saplingsPlanted + 1
  115.     end
  116.     os.sleep(2)
  117.     return true
  118. end
  119. function waitForTree()
  120.     local treeDetectedCount = 0
  121.     local waitingTime = 0
  122.     local height = 0
  123.     turtle.select(SAPLING_SLOT)
  124.     while treeDetectedCount < 7 do -- because sometimes turtle does not compare reliably
  125.         if turtle.compare() then
  126.             treeDetectedCount = 0
  127.         else
  128.             treeDetectedCount = treeDetectedCount + 1
  129.         end
  130.         os.sleep(2)
  131.         waitingTime = waitingTime + 2
  132.         if waitingTime > 30 and turtle.detectUp() then
  133.             height = 0
  134.             while turtle.detectUp() do
  135.                 turtle.select(SAPLING_TMP_SLOT)
  136.                 turtle.digUp()
  137.                 if turtle.up() then
  138.                     height = height + 1
  139.                     distanceTraveled = distanceTraveled + 1
  140.                     turtle.dig()
  141.                 end
  142.             end
  143.             for i=1,height do
  144.                 turtle.down()
  145.                 distanceTraveled = distanceTraveled + 1
  146.             end
  147.         end
  148.     end
  149. end
  150. function handleTree()
  151.     if not turtle.detect() then
  152.         if not plant() then
  153.             return false
  154.         end
  155.     else
  156.         waitForTree()
  157.         chop()
  158.         if not plant() then
  159.             return false
  160.         end
  161.     end
  162.     return true
  163. end
  164. function dumpWood()
  165.     for i = 1,16 do
  166.         if (not i==SAPLING_SLOT) and (not i==FUEL_SLOT) then
  167.             turtle.select(i)
  168.             if not turtle.compareTo(SAPLING_SLOT) and turtle.getItemCount(i)>0 then
  169.                 turtle.dropDown()
  170.             end
  171.         end
  172.     end
  173. end
  174.  
  175. function step()
  176.     local result = true
  177.     forward(2)
  178.     if posX <= 0 then
  179.         right()
  180.         right()
  181.         dumpWood()
  182.         cycleCount = cycleCount + 1
  183.         print("cycles finished: "..cycleCount)
  184.         print("saplings planted: "..saplingsPlanted)
  185.         print("trees chopped: "..treesChopped)
  186.         print("distance traveled: "..distanceTraveled)
  187.         os.sleep(10)
  188.     elseif posX >= farmLength then
  189.         right()
  190.         right()
  191.         turtle.select(SAPLING_TMP_SLOT)
  192.         turtle.suckDown()
  193.         transferSaplings()
  194.         os.sleep(10)
  195.     elseif (posX % 4) == orient then
  196.         right()
  197.         result = handleTree()
  198.         left()
  199.     elseif (posX % 4) == (2-orient) then
  200.         left()
  201.         result = handleTree()
  202.         right()
  203.     end
  204.     return result
  205. end
  206. function checkFuel()
  207.     local result = true
  208.     if turtle.getFuelLevel() < MIN_FUEL_FOR_STEP then
  209.         turtle.select(FUEL_SLOT)
  210.         turtle.refuel()
  211.     end
  212.     if turtle.getFuelLevel() < MIN_FUEL_FOR_STEP then
  213.         result = false
  214.     end
  215.     return result
  216. end
  217.  
  218. if farmLength==nil or farmLength<2 then
  219.     print("Farm length too short.\n")
  220.     return
  221. end
  222. if turtle.getItemCount(SAPLING_SLOT) < farmLength then
  223.     print("Not enough saplings in slot "..SAPLING_SLOT.."\n")
  224.     return
  225. end
  226.  
  227. while true do
  228.     if not checkFuel() then
  229.         print("Not enough fuel\n")
  230.         os.sleep(10)
  231.         while not checkFuel() do
  232.             os.sleep(10)
  233.         end
  234.         return false
  235.     end
  236.     if not step() then
  237.         print("Step unsuccessfull, exiting.\n")
  238.         return false
  239.     end
  240.     os.sleep(1)
  241. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement