Guest User

treefarm

a guest
Oct 10th, 2012
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.55 KB | None | 0 0
  1. sap=true --enables dropping of saplings in front
  2.  
  3.  
  4. function Refuel()
  5.     if turtle.getFuelLevel()~=unlimited and turtle.getFuelLevel()<=75 then --gets fuel level, if unlimited is false (you get unlimited if fuel is disabled)
  6.         turtle.digUp()
  7.         turtle.select(3) --chest placing
  8.         turtle.placeUp()
  9.         turtle.placeDown()
  10.         turtle.select(2)
  11.         turtle.dropUp()
  12.         turtle.select(1)
  13.         turtle.dropUp()
  14.         for i=4, 16 do --unloads everything else
  15.             turtle.select(i)
  16.             if turtle.getItemCount(i)>0 then
  17.                 turtle.dropDown(turtle.getItemCount(i))
  18.             end
  19.         end
  20.         while turtle.getFuelLevel()<=150 do --refuels until it has at least 150
  21.             turtle.select(1)
  22.             turtle.suckUp()
  23.             turtle.dropUp(turtle.getItemCount(1)-1)
  24.             turtle.craft()
  25.             turtle.refuel(4)
  26.         end
  27.         for i=1,16 do
  28.             turtle.select(i)
  29.             if i<3 then
  30.                 turtle.suckUp()
  31.             elseif i>3 then
  32.                 turtle.suckDown()
  33.             end
  34.         end
  35.         for i=1,2 do
  36.             turtle.select(i)
  37.             turtle.dropUp()
  38.         end
  39.         turtle.select(2)--swaps sapling back to slot 1
  40.         turtle.suckUp()
  41.         turtle.select(1)
  42.         turtle.suckUp()
  43.         turtle.select(3)
  44.         turtle.digUp()
  45.         turtle.digDown()
  46.     end
  47. end
  48.  
  49. function Climb() --allows the turtle to climb to the leaves and count along the way
  50. local climb=0 --set variable
  51.     while turtle.detectUp()==false do --waits for a leaf block and counts until then
  52.         turtle.up()
  53.         climb=climb+1
  54.         if climb>=15 then --prevents the turtle going obscenely high up
  55.             print("climbing too high. Aborting...")
  56.             break
  57.         end
  58.     end
  59.     return(climb) --returns the height
  60. end
  61.  
  62. function ezBreak() --makes the leafharvest code easier :)
  63.     turtle.dig()
  64.     turtle.forward()
  65.     turtle.digUp()
  66.     turtle.digDown()
  67. end
  68.  
  69. function LeafHarvest() --a very un-clean peice of code for harvesting leaves
  70.     turtle.digUp()
  71.     turtle.up()
  72.     turtle.digUp()
  73.     turtle.up()
  74.     turtle.turnLeft()
  75.     turtle.turnLeft()
  76.     ezBreak()
  77.     turtle.turnRight()
  78.     ezBreak()
  79.     ezBreak()
  80.     turtle.turnRight()
  81.     ezBreak()
  82.     ezBreak()
  83.     ezBreak()
  84.     ezBreak()
  85.     turtle.turnRight()
  86.     ezBreak()
  87.     ezBreak()
  88.     ezBreak()
  89.     ezBreak()
  90.     turtle.turnRight()
  91.     ezBreak()
  92.     ezBreak()
  93.     ezBreak()
  94.     ezBreak()
  95.     turtle.turnRight()
  96.     ezBreak()
  97.     ezBreak()
  98.     turtle.turnRight()
  99.     ezBreak()
  100.     turtle.turnLeft()
  101.     ezBreak()
  102.     turtle.turnRight()
  103.     ezBreak()
  104.     ezBreak()
  105.     turtle.turnRight()
  106.     ezBreak()
  107.     ezBreak()
  108.     turtle.turnRight()
  109.     ezBreak()
  110.     ezBreak()
  111.     turtle.turnRight()
  112.     ezBreak()
  113. end
  114.  
  115. function TreeHarvest() --harvests the trees (obviously)
  116.     turtle.select(1)
  117.     local climb=Climb() --climbs to leaves and stores height
  118.     LeafHarvest() --harvests leaves
  119.     turtle.turnRight()
  120.     ezBreak()
  121.     for i=0,climb do --goes back down the tree, the 0 is because of how the counting works
  122.         turtle.down()
  123.         turtle.digDown()
  124.     end
  125.     turtle.down()
  126.     turtle.back()
  127.     print("Completed.")
  128. end
  129.  
  130. function CheckHarvest() --allows the turtle to execute the harvest function seemlessly
  131.     if turtle.detect() then --function only does something if it finds a tree
  132.         turtle.down()
  133.         TreeHarvest()
  134.         turtle.select(1) --selects sapling
  135.         turtle.place() --places sapling
  136.         turtle.up() --reassumes position
  137.     end
  138.     Refuel()
  139. end
  140.  
  141. function Move(x) --made this to add more control to movement
  142.     turtle.select(2) --selects wood for comparing since leaves can be low
  143.     for i=1,x do --made it so i didnt have to do turtle.forward() a lot
  144.         if turtle.detect() and turtle.compare()==false then --digs through anything but wood (leaves)
  145.             turtle.dig()
  146.         end
  147.         turtle.forward()
  148.     end
  149. end
  150.  
  151. turtle.forward()
  152. while true do  --goes through the area (i know the code is anything but clean but hey, it works)
  153.     Move(4)
  154.     CheckHarvest()
  155.     Move(5)
  156.     CheckHarvest()
  157.     turtle.forward()
  158.     turtle.turnRight()
  159.     Move(4)
  160.     CheckHarvest()
  161.     Move(5)
  162.     CheckHarvest()
  163.     Move(5)
  164.     CheckHarvest()
  165.     Move(1)
  166.     turtle.turnRight()
  167.     Move(4)
  168.     CheckHarvest()
  169.     Move(5)
  170.     CheckHarvest()
  171.     Move(1)
  172.     turtle.turnRight()
  173.     Move(4)
  174.     CheckHarvest()
  175.     Move(1)
  176.     turtle.turnRight()
  177.     Move(4)
  178.     CheckHarvest()
  179.     Move(1)
  180.     turtle.turnLeft()
  181.     Move(4)
  182.     CheckHarvest()
  183.     Move(1)
  184.     turtle.turnLeft()
  185.     Move(4)
  186.     CheckHarvest()
  187.     Move(1)
  188.     turtle.turnRight()
  189.     Move(4)
  190.     CheckHarvest() --this is the last tree (add a wait if you want to be fuel efficient)
  191.     Move(3)
  192.     for i=4,16 do --drops off items
  193.         turtle.select(i)
  194.         if turtle.getItemCount(i)>0 then
  195.             if turtle.compare(1) and sap then --drops saplings in front
  196.                 turtle.drop()
  197.             else
  198.                 turtle.dropDown()
  199.             end
  200.         end
  201.     end
  202.     rs.setOutput("bottom",true) --pulses for minecarts
  203.     sleep(.2)
  204.     rs.setOutput("bottom",false)
  205.     sleep(.2)
  206.     turtle.back() --reassumes loop position
  207.     turtle.back()
  208.     turtle.turnRight()
  209. end
Advertisement
Add Comment
Please, Sign In to add comment