Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Tree Farm
- -- By Fenthis
- spacing_forward = 3 -- Spaces between trees
- spacing_left = 4
- trees = 3 -- Trees in row
- rows = 3 -- Rows of trees
- offset = 5 -- Start position from first trees
- sapling_slot = 1 -- Where to keep the reference saplings
- delay = 120 -- Delay in seconds between runs
- -- Misg globals
- total_count = 0
- saplings = 0
- if not act then
- os.loadAPI("act")
- if not act then
- print("act api required.")
- exit()
- end
- end
- -- We have arrived in front of what might be a trunk
- function do_tree()
- if turtle.dig() then -- Got a tree?
- act.try()
- turtle.digDown() -- get the stump
- height = 0
- while turtle.digUp() do
- turtle.up()
- height = height + 1
- end
- for i = 1, height do
- act.tryDown()
- end
- else
- act.try()
- end
- if not turtle.detectDown() and saplings > 1 then
- saplings = saplings - 1
- act.placeDown()
- end
- -- And just in case something landed on the dirt
- turtle.suckDown()
- end
- function do_left_or_right(dir)
- if dir then
- turtle.turnLeft()
- else
- turtle.turnRight()
- end
- end
- function is_odd(n)
- return ((n % 2) == 1)
- end
- function try_n(times)
- for i = 1, times do
- act.try()
- end
- end
- function do_round ()
- local count = 0
- saplings = count_saplings()
- print("Starting round with "..saplings.." saplings.")
- act.tryUp()
- try_n(offset - 1)
- for row = 1, rows do
- for tree = 1, trees do
- do_tree() -- this should leave us above the tree
- if tree ~= trees then
- try_n(spacing_forward)
- end
- end
- if row ~= rows then
- act.try()
- do_left_or_right(is_odd(row))
- try_n(spacing_left + 1)
- do_left_or_right(is_odd(row))
- end
- end
- -- Now return
- if not is_odd(rows) then
- act.try()
- else
- turtle.turnLeft()
- act.try()
- turtle.turnLeft()
- try_n(((spacing_forward + 1) * (trees - 1)) + 1)
- turtle.turnLeft()
- act.try()
- turtle.turnRight()
- end
- do_left_or_right(is_odd(rows))
- try_n((spacing_left + 1) * (rows - 1))
- do_left_or_right(not is_odd(rows))
- try_n(offset - 1)
- turtle.down()
- -- Dump logs
- act.select(sapling_slot)
- for i = 1, 16 do
- if i ~= sapling_slot then
- if not turtle.compareTo(i) then
- act.select(i)
- count = count + turtle.getItemCount(i)
- turtle.drop()
- act.select(sapling_slot)
- end
- end
- end
- -- Turn around
- turtle.turnLeft()
- turtle.turnLeft()
- -- Are we low sapplings?
- saplings = count_saplings() -- Recount, sometimes we pick them up
- if saplings < ((trees * rows) + 1) then
- old = saplings
- -- Move to second chest and face it.
- turtle.turnLeft()
- act.try()
- turtle.turnLeft()
- -- Try and get two stacks
- turtle.suck()
- turtle.suck()
- -- Return to starting
- turtle.turnLeft()
- act.try()
- turtle.turnLeft()
- -- Recount
- saplings = count_saplings()
- print("Picked up "..(saplings - old).." more saplings.")
- end
- -- Print something
- total_count = total_count + count
- print("Loop complete, collected " .. count .. " items for a total of " ..
- total_count .. " with " .. turtle.getFuelLevel() .. " fuel " ..
- saplings .. " saplings remaining.")
- end
- function count_saplings()
- s = 0
- act.select(sapling_slot)
- for i = 1,16 do
- if turtle.compareTo(i) then
- s = s + turtle.getItemCount(i)
- end
- end
- return s
- end
- -- Setup
- act.select(sapling_slot)
- -- Do something
- print("Running with a delay of " .. delay .. " seconds. Press Q to quit.")
- while true do
- -- First, go for it.
- do_round()
- -- Then wait
- os.startTimer(delay)
- repeat
- e, a1 = os.pullEvent()
- if e == "char" and (a1 == "Q" or a1 == "q") then
- print("Quitting!")
- return
- end
- until e == "timer"
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement