Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local x=0
- local y=0
- local dir=0
- local rows=4
- local columns=5
- local stop = false
- local function forward()
- local moved = true
- while not turtle.forward() do
- if not turtle.dig() and
- not turtle.attack() then
- moved = false
- print("can't move")
- break
- end
- end
- if moved then
- if dir == 0 then
- x=x+1
- elseif dir == 2 then
- x=x-1
- elseif dir == 1 then
- y=y+1
- elseif dir == 3 then
- y=y-1
- else
- print("Unknown dir")
- return false
- end
- return true
- end
- return false
- end
- local function turnRight()
- turtle.turnRight()
- dir = (dir+1)%4
- end
- local function turnLeft()
- turtle.turnLeft()
- dir = (dir-1)%4
- end
- local function cutTree()
- local height = 0
- turtle.dig()
- forward()
- turtle.digDown()
- turtle.select(3)
- while turtle.compareUp() do
- turtle.digUp()
- turtle.up()
- height = height +1
- end
- for i=1, height do
- turtle.down()
- end
- end
- local function plantTree()
- turtle.suckDown()
- if turtle.getItemCount(2)>1 and not turtle.detectDown() then
- turtle.select(2)
- return turtle.placeDown()
- end
- return false
- end
- local function detectTree()
- turtle.select(3)
- if turtle.compare() then
- return true
- else
- return false
- end
- end
- local function move()
- local mod = y%6
- if y==(rows-1)*3 and x==0 then
- stop = true
- print("Stoppinzg")
- return
- elseif (x== columns*3 and mod ==0 and dir == 0) or
- (x== columns*3 and mod == 3 and dir == 1)then
- turnRight()
- elseif (x==0 and mod==3 and dir == 2) or
- (x==0 and mod == 0 and dir == 1)then
- turnLeft()
- else
- if not forward() then
- stop = true
- return
- end
- end
- end
- local function manageInventory()
- local charcoal, saplings
- local time=0
- local charcoalOk = false
- local saplingsOk = false
- for i=4, 16 do
- turtle.select(i)
- turtle.dropDown()
- end
- turtle.down()
- while not (charcoalOk and saplingsOk) do
- turtle.select(1)
- turtle.refuel(turtle.getItemCount(1)-1)
- charcoal = (200-turtle.getFuelLevel())/96
- saplings = (rows*columns)-turtle.getItemCount(2)
- if charcoal > 0 then
- print("Waiting for "..charcoal.." charcoal")
- redstone.setOutput("left", true)
- time = charcoal
- else
- time=0
- redstone.setOutput("left", false)
- charcoalOk = true
- end
- if saplings > 0 then
- print("Waiting for "..saplings.." saplings")
- redstone.setOutput("right", true)
- if time > saplings or time == 0 then
- time = saplings
- end
- else
- redstone.setOutput("right", false)
- saplingsOk = true
- end
- os.sleep(time*2.5)
- end
- redstone.setOutput("left", false)
- redstone.setOutput("right", false)
- if(time>0) then
- os.sleep(10)
- end
- turtle.up()
- end
- local function returnHome()
- turnRight()
- for i=1,y do
- while not turtle.forward() do
- turtle.attack()
- end
- end
- x=0
- y=0
- dir=0
- turnRight()
- end
- local function mainLoop()
- while true do
- manageInventory()
- while not stop do
- move()
- if detectTree() then
- cutTree()
- end
- plantTree()
- end
- returnHome()
- print("finished")
- os.sleep(120)
- end
- end
- mainLoop()
Advertisement
Add Comment
Please, Sign In to add comment