--[[
Multi farm
]]--

wait_time = 1*60 -- wait N seconds in between runs
fuel_threshold = 5120 -- fuel from a stack of coal
gx = 14
gy = 14

tf_x = 14
tf_y = 14

function reset_run()
    
    tree_y = tx.state.placemarks.tf_home.y
    pumpkin_y = tx.state.placemarks.pf_home.y
    if tx.state.y == pumpkin_y then
        print('I am at the pumpkin farm level...')
        tx.goto_placemark('pf_elevator')
        tx.goto_placemark('tf_elevator')
        tx.goto_placemark('tf_home')
    elseif tx.state.y == tree_y then
        print('I am at the tree farm level...')
        tx.goto_placemark('tf_home')
    else 
        print('I am unsure about how to proceed... :(')
        print('I am at y '..tostring(tx.state.y)..', tree_y = '..tostring(tree_y)..', pumpkin_y = '..tostring(pumpkin_y))
        return false
    end
    return true
end

function refuel()
    local remaining = turtle.getFuelLevel()
    turtle.select(1)
    while remaining < fuel_threshold do
        turtle.suckDown(1)
        turtle.refuel(64)
    end
    remaining = turtle.getFuelLevel()
    print('Got '..remaining..' fuel remaining now')
end

function pre_pumpkin_run()
    if tx.state.placemarks.pf_home.y ~= tx.state.y then
        tx.goto_placemark('tf_elevator')
        tx.goto_placemark('pf_elevator')
    end
    tx.goto_placemark('pf_dropoff')
    for i=1,16 do
        ic = tx.getItemCount(i)
        if ic>0 then
            tx.select(i)
            tx.dropDown(i)
        end
    end
    tx.goto_placemark('pf_home')
    turtle.select(1)
    turtle.suckUp()
    turtle.dropUp(turtle.getItemCount(1)-1)
    print('Got pumpkin(s) in slot 1 now')
end

function post_pumpkin_run()
    -- if stuff in inv go to pf_dropoff safely
    --- drop off stuff except one pumkin
    -- go to pf home
    -- store pumkin
    
    tx.goto_placemark('pf_home')
    turtle.dropUp(turtle.getItemCount(1)-1)
    tx.goto_placemark('pf_dropoff')
    for i=1,16 do
        ic = tx.getItemCount(i)
        if ic>0 then
            tx.select(i)
            tx.dropDown(i)
        end
    end    
end

function harvest_pumpkin()
    turtle.select(1)
    if turtle.compareDown(1) then
        turtle.digDown()
    end    
end

function pumpkin_run()
    pre_pumpkin_run()
    tx.goto_placemark('pf_start')
    tx.grid_move{gx=gx, gy=gy, hook=harvest_pumpkin}
    post_pumpkin_run()
end

function pre_tree_run()
    if tx.state.placemarks.tf_home.y ~= tx.state.y then
        tx.goto_placemark('pf_elevator')
        tx.goto_placemark('tf_elevator')
    end
    tx.goto_placemark('tf_dropoff')
    for i=1,16 do
        ic = tx.getItemCount(i)
        if ic>0 then
            tx.select(i)
            tx.dropDown(i)
        end
    end
    tx.select(1)
    tx.suckUp()   
    
    tx.goto_placemark('tf_home')
    refuel()
    tx.select(2)
    tx.suckUp()
    tx.dropUp(tx.getItemCount(2)-1)
end

function plant_sapling()
    tx.select(1)
    tx.placeDown()
    tx.suckDown()
end

function fell_tree()
    tx.select(2)
    if tx.compare() then
        print('detected tree, harvesting up')
        tx.placemark('bottom')
        tx.select(2)
        while tx.compare() do
            tx.dig()
            tx.select(1)
            if not tx.up() then break end
            tx.select(2)
        end
        print('going back down')
        tx.goto_placemark('bottom')
        tx.select(2)
        tx.down()
        tx.dig()
        tx.select(1)
        tx.place()
        tx.select(2)
        tx.up()
        tx.select(1)
        tx.placeDown()
        tx.select(2)
    else
        -- nothing to do
        return
    end
end

function post_tree_run()
    tx.goto_placemark('tf_dropoff')
    tx.select(1)
    tx.dropUp(64)
    tx.select(2)
    tx.dropDown(tx.getItemCount(2)-1)
    for i=3,16 do
        ic = tx.getItemCount(i)
        if ic>0 then
            tx.select(i)
            tx.dropDown(ic)
        end
    end
    tx.goto_placemark('tf_home')
    tx.select(2)
    tx.dropUp(1)
    refuel()
end

function tree_run()
    pre_tree_run()
    tx.goto_placemark('tf_start')
    tx.grid_move{gx=tf_x, gy=tf_y, hook=plant_sapling, prefw_hook=fell_tree}
    post_tree_run()
end

function main()
    if tx.gps_init() and reset_run() then 
        while true do
            tree_run()
            pumpkin_run()
            --os.sleep(wait_time)
        end
    else
        print('failed to init')
        return
    end
end

main()