Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local x,y,z = 0,0,0 -- starting position
- -- ^ dont change unless you know what you're doing ^
- local dir = "n" -- starting direction, example: n for north
- local dirs = {"w","n","e","s"}
- local saplingslot = 1
- local dirtslot = 2
- local trees = {
- -- positions are relative to the starting position.
- -- does not use gps
- -- example:
- -- {x=-2,y=-1,z=-4,dir="n",real={x=-2,y=-1,z=-5}},
- -- x,y,z is where to stand when chopping it
- -- dir is direction the tree is in realtion to the position ^
- -- real x,y,z is the position of the sapling
- }
- local function checkfuel()
- local fuel = turtle.getFuelLevel()
- if fuel >= 5 then return end
- print("looking for fuel")
- while fuel < 5 do
- for slot = 1,16 do
- if turtle.getItemCount(slot) > 0 then
- turtle.select(slot)
- turtle.refuel(1)
- end
- end
- fuel = turtle.getFuelLevel()
- end
- turtle.select(1)
- print("refueled")
- end
- local function forcemove(func,dig)
- local state = false
- repeat
- checkfuel()
- if turtle[func] == nil then error("Cant find function \""..func.."\"") end
- state = turtle[func]()
- if dig then
- if func=="up" or func=="down"
- or func=="forward" then
- local face = func
- if face=="forward" then face="" end
- face=func:gsub("^%l", string.upper)
- local tmpfunc=func:gsub("^%l", string.upper)
- if turtle["detect"..face] == true then
- forcemove("dig"..tmpfunc)
- end
- end
- end
- until state
- end
- local function forward()
- if dir=="n" then
- z=z-1
- elseif dir=="e" then
- x=x+1
- elseif dir=="s" then
- z=z+1
- elseif dir=="w" then
- x=x-1
- end
- end
- local function back()
- if dir=="n" then
- z=z-1
- elseif dir=="e" then
- x=x-1
- elseif dir=="s" then
- z=z+1
- elseif dir=="w" then
- x=x+1
- end
- end
- local function turnclockwise()
- local dirid = 0
- for id,face in ipairs(dirs) do
- if face==dir then dirid=id break end
- end
- dirid=dirid+1
- if dirid > #dirs then dirid = 1 end
- dir=dirs[dirid] or error("Unable to calculate direction")
- end
- local function turncounterclockwise()
- local dirid = 0
- for id,face in ipairs(dirs) do
- if face==dir then dirid=id break end
- end
- dirid = dirid - 1
- if dirid < 1 then dirid = #dirs end
- dir=dirs[dirid] or error("Unable to calculate direction")
- end
- local go = {}
- go.u=function(dig)
- forcemove("up",dig)
- y=y+1
- end
- go.d=function(dig)
- forcemove("down",dig)
- y=y-1
- end
- go.f=function(dig)
- forcemove("forward",dig)
- forward()
- end
- go.b=function()
- forcemove("back")
- back()
- end
- go.rt=function()
- turtle.turnRight()
- turnclockwise()
- end
- go.lt=function()
- turtle.turnLeft()
- turncounterclockwise()
- end
- go.turn=function(face)
- local dirid = 0
- for id,face in ipairs(dirs) do
- if face==dir then dirid=id break end
- end
- local faceid = 0
- for id,vface in ipairs(dirs) do
- if vface==face then faceid=id break end
- end
- dirid = dirid - 1
- if dirid < 1 then dirid = #dirs end
- if dirid == faceid then go.lt() end
- while face ~= dir do
- go.rt()
- end
- end
- local function init()
- print("expecting saplings in slot "..saplingslot)
- print("and dirt in slot "..dirtslot)
- print("press any button to continue")
- repeat
- os.pullEvent("key")
- until turtle.getItemCount(saplingslot) >= tset.xcount*tset.zcount
- and turtle.getItemCount(dirtslot) >= tset.xcount*tset.zcount
- end
- local function chop()
- if not turtle.detect() then
- go.f()
- if turtle.detectDown() then
- repeat until turtle.digDown()
- end
- return
- end
- turtle.select(saplingslot)
- if turtle.compare() then
- forcemove("dig")
- go.f()
- forcemove("digDown")
- return
- elseif turtle.detect() then
- forcemove("dig")
- go.f()
- local height = 0
- while turtle.detectUp() do
- forcemove("digUp")
- go.u()
- height = height + 1
- end
- for count = 1,height do
- go.d()
- end
- end
- if turtle.detectDown() then
- forcemove("digDown")
- end
- end
- local function goto(xpos,ypos,zpos,newdir)
- while xpos~=x
- or ypos~=y
- or zpos~=z do
- -- vertical
- for count=1,y-ypos do
- if turtle.detectDown() then break end
- go.d(true)
- end
- for count=1,ypos-y do
- if turtle.detectUp() then break end
- go.u(true)
- end
- -- x
- for count=1,x-xpos do
- go.turn("w")
- if turtle.detect() then
- go.rt() go.f() go.lt()
- else
- go.f()
- end
- end
- for count=1,xpos-x do
- go.turn("e")
- if turtle.detect() then
- go.lt() go.f() go.rt()
- else
- go.f()
- end
- end
- -- z
- for count=1,z-zpos do
- go.turn("n")
- if turtle.detect() then
- go.rt() go.f() go.lt()
- else
- go.f()
- end
- end
- for count=1,zpos-z do
- go.turn("s")
- if turtle.detect() then
- go.lt() go.f() go.rt()
- else
- go.f()
- end
- end
- end
- if newdir then
- go.turn(newdir)
- end
- end
- local function gohome()
- goto(0,0,0,"n")
- end
- local function collect()
- goto(-4,-5,-3,"e")
- repeat until not turtle.suckDown()
- for count=1,tset.xcount*tset.xspace do
- go.f()
- repeat until not turtle.suckDown()
- end
- gohome()
- end
- local function replant()
- print("replanting")
- for id,tree in pairs(trees) do
- local real = tree.real
- goto(tree.x,tree.y,tree.z,tree.dir)
- if not turtle.detect() then
- goto(real.x,real.y,real.z)
- if not turtle.detectDown() then
- repeat
- turtle.select(dirtslot)
- until turtle.placeDown()
- end
- go.u()
- repeat
- turtle.select(saplingslot)
- until turtle.placeDown()
- end
- end
- end
- local function harvest()
- for id,tree in ipairs(trees) do
- print("moving to x"..tree.x.." y"..tree.y.." z"..tree.z)
- goto(tree.x,tree.y,tree.z,tree.dir)
- print("chopping tree")
- chop()
- end
- gohome()
- end
- local function usage()
- print("Usage: "..shell.getRunningProgram().." <task>")
- print("Tasks avavible: harvest, replant, collect, cycle")
- end
- local args = {...}
- local task = args[1]
- if task == nil then
- usage()
- elseif task == "harvest" then
- harvest()
- gohome()
- print("all done.")
- elseif task == "replant" then
- init()
- replant()
- gohome()
- print("all done.")
- elseif task == "collect" then
- collect()
- print("all done.")
- elseif task == "cycle" then
- init()
- harvest()
- for i=1,3 do
- collect()
- print("waiting 1min for leaves to despawn")
- sleep(60)
- print("collecting drops")
- collect()
- end
- replant()
- gohome()
- print("all done.")
- end
Advertisement
Add Comment
Please, Sign In to add comment