--8 for regular furnace, 10 for iron furnace
--if using electric furnace, will need code changes
local burnPerCoal=10

local tArgs={...}

local wood=tArgs[1]=="wood"

--run when facing the base of a tree, 
--one ABOVE ground
function harvestTree()
  if turtlex.detect() then
    local count=0
    turtlex.dig()
    turtlex.forward()
    turtlex.digDown()
    while turtlex.compareUp("birch log") do
      count=count+1
      turtlex.up(1,"dig")
    end
  
    turtlex.down(count)
    turtlex.placeDown("birch sapling")
  else
    turtlex.forward()
  end
end

local function checkTrees()
  print("checking trees..")
  turtlex.up(6)
  turtlex.left(4)
  turtlex.forward(4)
  function harvestRow()
    harvestTree()
    for i=1,2 do
      turtlex.forward(3)
      harvestTree()
    end
    turtlex.forward()
  end
  harvestRow()
  turtlex.turnRight()
  turtlex.forward(4)
  turtlex.turnRight()
  harvestRow()
  turtlex.turnLeft()
  turtlex.forward(4)
  turtlex.turnLeft()
  harvestRow()
  turtlex.left(2)
  turtlex.back(14)
  turtlex.left(2)
  turtlex.down(6)
  --error("stopping")
end

function cycleFurnaceWood()
   --empty furnace output
   turtlex.turnAround()
   turtlex.up()
   turtlex.suck()
  
   if turtlex.getItemCount("charcoal")<32 then
     
     --load furnace fuel
     turtlex.down()
     turtlex.forward()
     turtlex.dropUp(1,"charcoal")
     
     --load items
     turtlex.back()
     turtlex.up(2)
     turtlex.forward()
     turtlex.dropDown(10,"birch log")
     
   else
     turtlex.up(1)
     turtlex.forward()
   end
   --output   
   turtlex.dropUp(turtlex.getItemCount("birch log")-11,"birch log")
   
   --return
   turtlex.turnAround()
   turtlex.forward()
   turtlex.down(2)

end

function cycleFurnace()
  --unload output
  turtlex.turnAround()
  turtlex.up()
  turtlex.suck()
  local logs=turtlex.getItemCount("birch log")
  local keep=logs%burnPerCoal
  if keep==0 then keep=burnPerCoal end
  logs=logs-keep
  
  if logs>64 then
    logs=64
  end
  local coalNeeded=logs/burnPerCoal
  local coal=turtlex.getItemCount("charcoal")
  if coal<coalNeeded then
    print("critical coal shortage!")
    coalNeeded=math.max((coal-32)*burnPerCoal,0)
    logs=coalNeeded*burnPerCoal
  end
  
  turtlex.down()
  turtlex.forward()
  turtlex.dropUp(coalNeeded,"charcoal")
  turtlex.back()
  turtlex.up(2)
  turtlex.forward()
  turtlex.dropDown(logs,"birch log")
  coal=coal-coalNeeded
  
  if coal>32 then
    turtlex.dropUp(coal-32,"charcoal")
  end
  turtlex.turnAround()
  turtlex.forward()
  turtlex.down(2)
end

local function refuelTo(level)
  --first, burn surplus saplings
  local saplingCount=turtlex.getItemCount("birch sapling")
  while saplingCount>64 do
    turtlex.refuel(64,"birch sapling")
    saplingCount=turtlex.getItemCount("birch sapling")
  end
  local needed=level-turtlex.getFuelLevel()
  if needed>0 then
    turtlex.refuel(math.ceil(needed/80),"charcoal")
  end
end

---[[
--main loop
local checkTime=10
local suckCount=0
local suckTimer --=os.startTimer(1) 
local harvestTimer=os.startTimer(0)
while true do
  
  local e,p1,p2=os.pullEvent()
  if e=="timer" and p1==harvestTimer then
    checkTrees()
    refuelTo(250)
    if wood then
      cycleFurnaceWood()
    else
      cycleFurnace()
    end
   
    suckTimer=os.startTimer(1)
    suckCount=0
  elseif e=="timer" and p1==suckTimer then
    turtlex.suckDown()
    suckCount=suckCount+1
    if suckCount<60 then
      suckTimer=os.startTimer(10)
    else
      harvestTimer=os.startTimer(1)
    end
  end
    
end
--]]