Advertisement
charonme

chfirfarm

Apr 7th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- chfirfarm
  2. -- refill fuel in slot FUEL_SLOT
  3. -- requires at least 4 saplings in slot SAPLING_SLOT
  4. -- 2x2 planting area in front (and to the right) of the turtle
  5. -- must start with 4 saplings planted in the planting area or even the tree can be already grown
  6. -- wood deposit anywhere behind the turtle, but there has to be some stopping block
  7. -- auxiliary sapling storage somewhere below the starting turtle position (directly under it) where fallen saplings should be collected
  8. -- if the turtle stops at the wood deposit, it means it's full and needs to be emptied
  9.  
  10.  
  11. local FUEL_SLOT = 1
  12. local SAPLING_SLOT = 2
  13. local SAPLING_TMP_SLOT = 3
  14. local MIN_SAPLINGS = 5
  15. local SAPLINGS_BUFFER = 64
  16. local WOOD_SLOT = 4
  17. local MAX_WOOD = 620
  18. local MIN_FUEL_FOR_CYCLE = 360
  19. local MAX_WOOD_STORAGE_DISTANCE = 32
  20. local REPEATED_DETECTION_COUNT = 3
  21. local REPEATED_DETECTION_TICK = 1
  22. local NEXT_DETECTION_TICK = 1
  23. local TREE_GROW_SLEEP_TIME = 40
  24. local posX = 0
  25. local orient = 0
  26. local cycleCount = 0
  27. local distanceTraveled = 0
  28. local auxiliaryDepth = 0
  29.  
  30. function forward(n)
  31.     for i=1,n do
  32.         if turtle.forward() then
  33.             distanceTraveled = distanceTraveled + 1
  34.             if orient==0 then
  35.                 posX = posX+1
  36.             end
  37.             if orient==2 then
  38.                 posX = posX-1
  39.             end
  40.         else
  41.             return false
  42.         end
  43.     end
  44.     return true
  45. end
  46. function back(n)
  47.     for i=1,n do
  48.         if turtle.back() then
  49.             distanceTraveled = distanceTraveled + 1
  50.             if orient==0 then
  51.                 posX = posX-1
  52.             end
  53.             if orient==2 then
  54.                 posX = posX+1
  55.             end
  56.         else
  57.             return false
  58.         end
  59.     end
  60.     return true
  61. end
  62. function left()
  63.     turtle.turnLeft()
  64.     orient = orient-1
  65.     while orient<0 do
  66.         orient = orient+4
  67.     end
  68. end
  69. function right()
  70.     turtle.turnRight()
  71.     orient = orient+1
  72.     while orient>3 do
  73.         orient = orient-4
  74.     end
  75. end
  76. function reorderSaplings()
  77.     local saplingsReordered = 0
  78.     for i = 1,16 do
  79.         if i~=SAPLING_SLOT then
  80.             turtle.select(i)
  81.             os.sleep(0.1)
  82.             if turtle.compareTo(SAPLING_SLOT) and (turtle.getItemCount(i)>0) then
  83.                 while (turtle.getItemCount(i)>0) and (turtle.getItemCount(SAPLING_SLOT)<64) and (turtle.transferTo(SAPLING_SLOT,1)) do
  84.                     saplingsReordered = saplingsReordered + 1
  85.                     os.sleep(0.1)
  86.                 end
  87.             end
  88.         end
  89.     end
  90.     print("reordered "..saplingsReordered.." saplings")
  91. end
  92. function dumpExcessSaplings()
  93.     for i = 1,16 do
  94.         if (i~=SAPLING_SLOT) and (i~=FUEL_SLOT) then
  95.             turtle.select(i)
  96.             if turtle.compareTo(SAPLING_SLOT) and (turtle.getItemCount(i)>0) then
  97.                 turtle.drop()
  98.             end
  99.         end
  100.     end
  101. end
  102.  
  103. function checkSaplings()
  104.     reorderSaplings()
  105.     if turtle.getItemCount(SAPLING_SLOT) < MIN_SAPLINGS then
  106.         print("insufficient saplings, waiting for decaying leaves...")
  107.         os.sleep(60)
  108.         print("refilling saplings from auxiliary storage...")
  109.         while turtle.down() do
  110.             auxiliaryDepth = auxiliaryDepth + 1
  111.             distanceTraveled = distanceTraveled + 1
  112.         end
  113.  
  114.         turtle.select(SAPLING_TMP_SLOT)
  115.         if turtle.suckDown(SAPLINGS_BUFFER) then
  116.             print("some saplings found in auxiliary storage")
  117.         else
  118.             print("getting more saplings failed")
  119.         end
  120.         reorderSaplings()
  121.         if turtle.getItemCount(SAPLING_SLOT) >= SAPLINGS_BUFFER then
  122.             print("returning excess saplings...")
  123.             dumpExcessSaplings()
  124.         end
  125.         while auxiliaryDepth>0 do
  126.             turtle.up()
  127.             auxiliaryDepth = auxiliaryDepth - 1
  128.             distanceTraveled = distanceTraveled + 1
  129.         end
  130.     end
  131.     if turtle.getItemCount(SAPLING_SLOT) < MIN_SAPLINGS then
  132.         print("Not enough saplings in slot "..SAPLING_SLOT..", waiting for saplings...\n")
  133.         while turtle.getItemCount(SAPLING_SLOT) < MIN_SAPLINGS do
  134.             os.sleep(10)
  135.         end
  136.     end
  137.  
  138.  
  139. end
  140.  
  141. function chop() --needs 5 saplings and 80 fuel
  142.     print("chopping tree...")
  143.     local height = 0
  144.     turtle.select(WOOD_SLOT)
  145.     turtle.dig()
  146.     forward(1)
  147.  
  148.     while turtle.detect() or turtle.detectUp() do
  149.         turtle.select(WOOD_SLOT)
  150.         turtle.dig()
  151.         turtle.digUp()
  152.         if turtle.up() then
  153.             height = height + 1
  154.             distanceTraveled = distanceTraveled + 1
  155.         end
  156.     end
  157.     right()
  158.     turtle.dig() --just in case
  159.     forward(1)
  160.     left()
  161.  
  162.     --just in case
  163.     while turtle.detectUp() do
  164.         turtle.select(WOOD_SLOT)
  165.         turtle.dig()
  166.         turtle.digUp()
  167.         if turtle.up() then
  168.             height = height + 1
  169.             distanceTraveled = distanceTraveled + 1
  170.         end
  171.     end
  172.  
  173.     for i=1,height do
  174.         turtle.select(WOOD_SLOT)
  175.         turtle.dig()
  176.         turtle.digDown()
  177.         turtle.select(SAPLING_TMP_SLOT)
  178.         turtle.suckDown()
  179.         turtle.down()
  180.         turtle.suck()
  181.         distanceTraveled = distanceTraveled + 1
  182.     end
  183.     turtle.select(WOOD_SLOT)
  184.     turtle.dig()
  185.     -- replant while repositioning
  186.     print("replanting saplings...")
  187.     turtle.select(SAPLING_TMP_SLOT)
  188.     turtle.suck()
  189.     turtle.select(SAPLING_SLOT)
  190.     turtle.place()
  191.     right()
  192.     back(1)
  193.     turtle.select(SAPLING_TMP_SLOT)
  194.     turtle.suck()
  195.     turtle.select(SAPLING_SLOT)
  196.     turtle.place()
  197.     left()
  198.     turtle.select(SAPLING_TMP_SLOT)
  199.     turtle.suck()
  200.     turtle.select(SAPLING_SLOT)
  201.     turtle.place()
  202.     back(1)
  203.     turtle.select(SAPLING_TMP_SLOT)
  204.     turtle.suck()
  205.     turtle.select(SAPLING_SLOT)
  206.     turtle.place()
  207. end
  208.  
  209. function checkTreeIsReallyThere()
  210.     local result = false
  211.     if turtle.up() then
  212.         os.sleep(2)
  213.         turtle.select(SAPLING_SLOT)
  214.         os.sleep(2)
  215.         if turtle.detect() and (turtle.getItemCount(SAPLING_SLOT)>0) and (not turtle.compare()) then
  216.             result = true
  217.         end
  218.         turtle.down()
  219.         distanceTraveled = distanceTraveled + 2
  220.     else -- couldn't move up, assume there are tree leaves
  221.         result = true
  222.     end
  223.     return result
  224. end
  225.  
  226. function waitForTree() -- needs at least 1 sapling
  227.     print("waiting for tree...")
  228.     local result = false
  229.     local treeDetectedCount = 0
  230.     os.sleep(TREE_GROW_SLEEP_TIME)
  231.     while treeDetectedCount < REPEATED_DETECTION_COUNT do -- because sometimes the turtle does not compare reliably
  232.         turtle.select(SAPLING_SLOT)
  233.         os.sleep(1)
  234.         if turtle.compare() then
  235.             if treeDetectedCount>0 then
  236.                 print("correcting invalid tree detection/comparison ("..treeDetectedCount..")")
  237.             end
  238.             treeDetectedCount = 0
  239.         else
  240.             treeDetectedCount = treeDetectedCount + 1
  241.             left()
  242.             os.sleep(REPEATED_DETECTION_TICK)
  243.             right()
  244.         end
  245.         os.sleep(NEXT_DETECTION_TICK)
  246.     end
  247.     os.sleep(1)
  248.     if treeDetectedCount < REPEATED_DETECTION_COUNT then
  249.         print("correct tree detection failed")
  250.     else
  251.         if checkTreeIsReallyThere() then
  252.             print("tree detected")
  253.             result = true
  254.         else
  255.             print("tree detected, but not found")
  256.         end
  257.     end
  258.     return result
  259. end
  260. function dumpWood() -- dump everything that is not a sapling
  261.     while back(1) and (posX>(0-MAX_WOOD_STORAGE_DISTANCE)) do
  262.     end
  263.  
  264.     for i = 1,16 do
  265.         if (i~=SAPLING_SLOT) and (i~=FUEL_SLOT) then
  266.             turtle.select(i)
  267.             if not turtle.compareTo(SAPLING_SLOT) then
  268.                 turtle.dropDown()
  269.                 if turtle.getItemCount(i)>0 then
  270.                     print("cannot dump wood, storage probably full...")
  271.                     while turtle.getItemCount(i)>0 do
  272.                         turtle.dropDown()
  273.                         os.sleep(10)
  274.                     end
  275.                 end
  276.             end
  277.         end
  278.     end
  279.  
  280.     forward(0-posX)
  281. end
  282. function checkWood()
  283.     print("checking wood amount...")
  284.     local woodAmount = 0
  285.     for i = 1,16 do
  286.         if (i~=SAPLING_SLOT) and (i~=FUEL_SLOT) then
  287.             turtle.select(i)
  288.             if not turtle.compareTo(SAPLING_SLOT) then
  289.                 woodAmount = woodAmount + turtle.getItemCount(i)
  290.             end
  291.         end
  292.     end
  293.     if woodAmount > MAX_WOOD then
  294.         print("going to dump "..woodAmount.." of wood...")
  295.         dumpWood()
  296.     else
  297.         print("holding "..woodAmount.." of wood")
  298.     end
  299. end
  300. function checkFuel()
  301.     local result = true
  302.     if turtle.getFuelLevel() < MIN_FUEL_FOR_CYCLE then
  303.         turtle.select(FUEL_SLOT)
  304.         turtle.refuel()
  305.     end
  306.     if turtle.getFuelLevel() < MIN_FUEL_FOR_CYCLE then
  307.         result = false
  308.     end
  309.     return result
  310. end
  311. function cycle()
  312.     local result = true
  313.  
  314.     checkSaplings()
  315.     checkFuel()
  316.  
  317.     while not waitForTree() do
  318.         os.sleep(1)
  319.     end
  320.     chop()
  321.  
  322.     checkWood()
  323.  
  324.     cycleCount = cycleCount + 1
  325.     print("cycles finished: "..cycleCount)
  326.     print("distance traveled: "..distanceTraveled)
  327.     print("waiting between cycles...")
  328.     os.sleep(2)
  329.  
  330.     return result
  331. end
  332.  
  333. if turtle.getItemCount(SAPLING_SLOT) < MIN_SAPLINGS then
  334.     print("Not enough saplings in slot "..SAPLING_SLOT..", exiting.\n")
  335.     return false
  336. end
  337.  
  338. while true do
  339.     if not cycle() then
  340.         print("Cycle unsuccessfull, exiting.\n")
  341.         return false
  342.     end
  343. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement