--spinds and check for ajcencent tree
--if it finds one it move up while
--chopping. It expects uppon moving
--down it goes to the bottom, 1 up
--and sucks, to enable collection of
--saplings via water transport.
--replants after gathering

local noop = function() end
print("Running built in improved 1way lumberjack mode")
rednet.open("right")

local function fell()
 local n=0
 while turtle.dig() do
  n=n+1
  turtle.digUp()
  turtle.up()
 end
 turtle.turnRight()
 for i=1,n do
  turtle.dig()
  turtle.down()
 end
 turtle.turnLeft()
end
sleep(5)
x,y,z=gps.locate(2)
if y ~= nil and (x==-150 and z==192) then
 print("recovering...")
 for i=(y-74),8 do
  turtle.up()
  turtle.dig()
  turtle.digUp()
 end
 turtle.forward()
 sleep(1)
 x,y,z=gps.locate(10)
 turtle.back()
 if not ( z==193) then
  print("facing the wrong way, attempting correction")
  turtle.turnLeft()
 end
 for i=1,(y-74) do
  -- print("going down")
  turtle.digDown()
  turtle.down()
  turtle.dig()
 end
 turtle.select(2)
 turtle.place()
 turtle.select(1) 
 else
 print("startlocation not safe,stopping")
 while true do
   sleep(600)
  end
end
print("location should be welldefined now")
while true do
 turtle.select(1)
 lumberAmnt=turtle.getItemCount(1)
 if lumberAmnt > 50 then
  turtle.down()
  turtle.dropDown(lumberAmnt-1)
  turtle.up()
 end
 sapAmmount=turtle.getItemCount(2)
 fertAmmount=turtle.getItemCount(16)
 if turtle.compare() then
  fell()
  turtle.select(2)
  turtle.place()
  rednet.broadcast(os.clock()..":"..math.floor(turtle.getFuelLevel()/10)..";"..lumberAmnt..":"..sapAmmount..":"..fertAmmount)
 end
 if sapAmmount < 5 then
  turtle.suck()
  turtle.suckDown()
  sleep(10)
  turtle.suck()
 end
 sleep(10)  -- EDIT should be 10
  if fertAmmount > 1 then
    turtle.select(16)
    turtle.place()
  end
 turtle.suckUp()
 turtle.suckDown()
 turtle.select(16)
 for i=3,15 do
  turtle.select(i)
  if turtle.compareTo(16) then
   turtle.transferTo(16)
  end
 end
end
