Advertisement
WhiteFire_Sondergaar

Tree Farm

May 7th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.27 KB | None | 0 0
  1. -- Tree Farm
  2. -- By Fenthis
  3.  
  4. spacing_forward = 3 -- Spaces between trees
  5. spacing_left = 4
  6. trees = 3 -- Trees in row
  7. rows = 3 -- Rows of trees
  8. offset = 5 -- Start position from first trees
  9. sapling_slot = 1 -- Where to keep the reference saplings
  10. delay = 120 -- Delay in seconds between runs
  11.  
  12. -- Misg globals
  13. total_count = 0
  14. saplings = 0
  15.  
  16. if not act then
  17.     os.loadAPI("act")
  18.     if not act then
  19.         print("act api required.")
  20.         exit()
  21.     end
  22. end
  23.  
  24. -- We have arrived in front of what might be a trunk
  25. function do_tree()
  26.     if turtle.dig() then -- Got a tree?
  27.         act.try()
  28.         turtle.digDown() -- get the stump
  29.         height = 0
  30.         while turtle.digUp() do
  31.             turtle.up()
  32.             height = height + 1
  33.         end
  34.         for i = 1, height do
  35.             act.tryDown()
  36.         end
  37.     else
  38.         act.try()
  39.     end  
  40.  
  41.     if not turtle.detectDown() and saplings > 1 then
  42.         saplings = saplings - 1
  43.         act.placeDown()
  44.     end
  45.  
  46.     -- And just in case something landed on the dirt
  47.     turtle.suckDown()
  48. end
  49.  
  50. function do_left_or_right(dir)
  51.     if dir then
  52.         turtle.turnLeft()
  53.     else
  54.         turtle.turnRight()
  55.     end
  56. end
  57.  
  58. function is_odd(n)
  59.     return ((n % 2) == 1)
  60. end
  61.  
  62. function try_n(times)
  63.     for i = 1, times do
  64.         act.try()
  65.     end
  66. end
  67.  
  68. function do_round ()
  69.     local count = 0
  70.  
  71.     saplings = count_saplings()
  72.     print("Starting round with "..saplings.." saplings.")
  73.  
  74.     act.tryUp()
  75.     try_n(offset - 1)
  76.  
  77.     for row = 1, rows do
  78.         for tree = 1, trees do
  79.             do_tree() -- this should leave us above the tree
  80.             if tree ~= trees then
  81.                 try_n(spacing_forward)
  82.             end
  83.         end
  84.         if row ~= rows then
  85.             act.try()
  86.             do_left_or_right(is_odd(row))
  87.             try_n(spacing_left + 1)
  88.             do_left_or_right(is_odd(row))
  89.         end
  90.     end
  91.  
  92.     -- Now return
  93.     if not is_odd(rows) then
  94.         act.try()
  95.     else
  96.         turtle.turnLeft()
  97.         act.try()
  98.         turtle.turnLeft()
  99.         try_n(((spacing_forward + 1) * (trees - 1)) + 1)
  100.         turtle.turnLeft()
  101.         act.try()
  102.         turtle.turnRight()
  103.     end
  104.  
  105.     do_left_or_right(is_odd(rows))
  106.     try_n((spacing_left + 1) * (rows - 1))
  107.     do_left_or_right(not is_odd(rows))
  108.     try_n(offset - 1)
  109.     turtle.down()
  110.  
  111.     -- Dump logs
  112.     act.select(sapling_slot)
  113.     for i = 1, 16 do
  114.         if i ~= sapling_slot then
  115.             if not turtle.compareTo(i) then
  116.                 act.select(i)
  117.                 count = count + turtle.getItemCount(i)
  118.                 turtle.drop()
  119.                 act.select(sapling_slot)
  120.             end
  121.         end
  122.     end
  123.  
  124.     -- Turn around
  125.     turtle.turnLeft()
  126.     turtle.turnLeft()
  127.  
  128.     -- Are we low sapplings?
  129.     saplings = count_saplings() -- Recount, sometimes we pick them up
  130.  
  131.     if saplings < ((trees * rows) + 1) then
  132.         old = saplings
  133.         -- Move to second chest and face it.
  134.         turtle.turnLeft()
  135.         act.try()
  136.         turtle.turnLeft()
  137.         -- Try and get two stacks
  138.         turtle.suck()
  139.         turtle.suck()
  140.         -- Return to starting
  141.         turtle.turnLeft()
  142.         act.try()
  143.         turtle.turnLeft()
  144.         -- Recount
  145.         saplings = count_saplings()
  146.         print("Picked up "..(saplings - old).." more saplings.")
  147.     end
  148.  
  149.     -- Print something
  150.     total_count = total_count + count
  151.     print("Loop complete, collected " .. count .. " items for a total of " ..
  152.         total_count .. " with " .. turtle.getFuelLevel() .. " fuel " ..
  153.         saplings .. " saplings remaining.")
  154. end
  155.  
  156. function count_saplings()
  157.     s = 0
  158.     act.select(sapling_slot)
  159.     for i = 1,16 do
  160.         if turtle.compareTo(i) then
  161.             s = s + turtle.getItemCount(i)
  162.         end
  163.     end
  164.     return s
  165. end
  166.  
  167. -- Setup
  168. act.select(sapling_slot)
  169.  
  170. -- Do something
  171. print("Running with a delay of " .. delay .. " seconds. Press Q to quit.")
  172. while true do
  173.     -- First, go for it.
  174.     do_round()
  175.  
  176.     -- Then wait
  177.     os.startTimer(delay)
  178.     repeat
  179.         e, a1 = os.pullEvent()
  180.         if e == "char" and (a1 == "Q" or a1 == "q") then
  181.             print("Quitting!")
  182.             return
  183.         end
  184.     until e == "timer"
  185. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement