bigougit

treeFarm

Mar 14th, 2013
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.72 KB | None | 0 0
  1. --[[
  2. treeFarm v2.1 by bigougit
  3. youtube.com/user/bigougit
  4. http://twitter.com/bigougit
  5.  
  6. This program is for mining
  7. or felling turtles only.
  8.  
  9. To install in-game, make sure
  10. that http = true in your
  11. computercraft config.
  12.  
  13. To install, open your turtle
  14. and type:
  15. pastebin get w8GkTmYh treeFarm
  16.  
  17. The turtle needs 2 sapplings
  18. and 1 log to start. This
  19. program only handles 1x1
  20. trees, so no redwoods or
  21. large oak trees.
  22.  
  23. Place sapplings in slot 1
  24. and logs in slot 2. The
  25. turtle will use logs for
  26. fuel as needed.
  27.  
  28. --]]
  29.  
  30. local continue = true
  31. local treeCount = 0
  32. local moved = 0
  33. local fakeTrees = 0
  34.  
  35. turtle.select(1)
  36. term.clear()
  37. term.setCursorPos(1,1)
  38.  
  39. --return how many items in [slot]
  40. local function sCount(slot)
  41.   return turtle.getItemCount(slot)
  42. end
  43.  
  44. local function tryUp()
  45.   while not turtle.up() do
  46.     turtle.attackUp()
  47.     turtle.digUp()
  48.   end
  49. end
  50.  
  51. local function tryDown()
  52.   while not turtle.down() do
  53.     turtle.attackDown()
  54.     turtle.digDown()
  55.   end
  56. end
  57.  
  58. function stack(slot)
  59.  
  60.   local room = turtle.getItemSpace(slot)
  61.   local s = 0
  62.   while room > 0 do
  63.     s = s + 1
  64.     if s >= 16 then
  65.       room = 0
  66.     end
  67.     if s ~= slot then      
  68.       turtle.select(s)
  69.       if turtle.compareTo(slot) then
  70.         if sCount(s) >= room then        
  71.           turtle.transferTo(slot, room)
  72.           room = 0
  73.         else
  74.           local temp = sCount(s)
  75.           turtle.transferTo(slot,temp)
  76.           room = room - temp
  77.         end
  78.       end
  79.     end
  80.   end
  81.   turtle.select(slot)
  82. end
  83.  
  84. local function dispStats(message)
  85.   term.clear()
  86.   term.setCursorPos(1,1)
  87.   print("Bigougit's tree farming program.")
  88.   print("Trees farmed:    "..treeCount)
  89.   print("Fakes trees:     "..fakeTrees)
  90.   if message ~= nil then
  91.     print(message)
  92.   end
  93. end
  94.  
  95. function fuel(slot, need)
  96.   local iCount = sCount(slot)
  97.   --stack(slot)
  98.   local cont = true
  99.   while cont do
  100.     if turtle.getFuelLevel() < need then
  101.       if iCount > 1 then
  102.         turtle.select(slot)
  103.         turtle.refuel(1)
  104.         if iCount == sCount(slot) then
  105.           cont = false
  106.         else
  107.           iCount = sCount(slot)
  108.         end
  109.       else
  110.         cont = false
  111.       end
  112.     else
  113.       return true
  114.     end
  115.   end
  116.   if turtle.getFuelLevel() < need then
  117.     return false
  118.   else
  119.     return true
  120.   end
  121. end
  122.  
  123. -- start of main program
  124.  
  125. dispStats(nil)
  126. stack(1)
  127. while ( sCount(1) <= 1 ) do
  128.   dispStats("Need at least 2 sapplings in first slot to start.")
  129.   sleep(2)
  130.   term.clear()
  131.   term.setCursorPos(1,1)
  132. end
  133.  
  134. dispStats(nil)
  135.  
  136. if not turtle.detect() then
  137.   turtle.select(1)
  138.   turtle.place()
  139. end
  140.  
  141. while continue do
  142.   turtle.select(2)
  143.   if turtle.compare() then
  144.     if fuel(2, 18) then
  145.       dispStats("Chopping Tree...")
  146.       turtle.select(2)
  147.       while turtle.compare() do
  148.         turtle.dig()
  149.         tryUp()
  150.         moved = moved + 1
  151.       end
  152.       for i=1,moved do
  153.         tryDown()
  154.       end
  155.     end
  156.    -- stack(1)
  157.    -- stack(2)
  158.     turtle.select(1)
  159.     if sCount(1) > 1 then
  160.       turtle.place()
  161.     end
  162.     if moved > 0 then
  163.       treeCount = treeCount + 1
  164.       stack(1)
  165.       stack(2)
  166.       turtle.select(1)
  167.     else
  168.       --turtle.dig()
  169.       fakeTrees = fakeTrees + 1
  170.     end
  171.     moved = 0
  172.   end
  173.   turtle.select(2)
  174.   while not turtle.compare() do
  175.     dispStats("Waiting for tree to grow.")
  176.     redstone.setOutput("bottom",true)
  177.     sleep(0.5)
  178.     redstone.setOutput("bottom",false)
  179.     sleep(5)
  180.   end
  181.   while sCount(1) <= 1 do
  182.     dispStats("Need at least 2 sapplings in first slot to start.")
  183.     sleep(2)
  184.     term.clear()
  185.     term.setCursorPos(1,1)
  186.   end
  187.   dispStats(nil)
  188.   if not turtle.detect() then
  189.     turtle.select(1)
  190.     turtle.place()
  191.   end
  192.   sleep(.5)
  193. end
Advertisement
Add Comment
Please, Sign In to add comment