local args = { ... }
 
tx.state.x = 0
tx.state.z = -508
tx.state.y = 65
tx.state.o = tx.norm_direction('west')
tx.state.move_mode = 'stop'
sapling_slot = 16

function tree_pos(i)
    -- position and face tree number i
    dst = {}
    dst.x = -4
    dst.y = 65
    dst.z = -506 - (math.floor((i-1)/2) * 3) - (i+1) % 2
    if i % 2 == 1 then
        dst.o = 'e'
    else
        dst.o = 'w'
    end
    return dst
end

function log_big_fir()
    temp_pos = tx.get_pos()
    tx.down()
    tx.state.move_mode = 'dig'
    shell.run('log2')
    tx.state.move_mode = 'stop'
    tx.up()
    tx.left()
    tx.left()
    tx.select(16)
    shell.run('grid placedown 2x2')
    tx.goto_pos(temp_pos)
end

tx.face('north')
-- fill sapling_slot

tx.goto_placemark('tree_row')

for i=1,20 do
    p = tree_pos(i)
    tx.goto_pos(p)
    if tx.detect() then
        print('Tree at '..i)
        log_big_fir()
        --replant_big_fir()
        break -- one tree is enough for one run
    end
end

tx.goto_placemark('tree_row')
tx.goto_placemark('tree_station')

tx.face('s')
-- empty of wood
tx.face('w')
-- empty of sapplings