local args = { ... }

height = tonumber(args[1])
width = tonumber(args[2])
current_slot=1
last_slot=1
tally = 0

tx.placemark('start', true)

tx.select(1)
for i=1,16 do
    if turtle.compareTo(i) then
        last_slot = i
        tally = tally + turtle.getItemCount(i)
    else
        break
    end
end
print(tally..' blocks in slot 1 to '..last_slot)

function place_block()
    turtle.select(current_slot)    
    while not tx.compare() do
        turtle.select(last_slot)
        tx.dig()
        turtle.select(current_slot)
        tx.place()
        if turtle.getItemCount(current_slot) == 0 then
            current_slot = current_slot + 1
            turtle.select(current_slot)
        end
        if current_slot > last_slot then
            tx.goto_placemark('start')
            error('I was out of building materials before I could finish :(')
        end
    end
end

for w=1,width do
    
    for h=1,height do
        place_block()
        if w % 2 == 1 and h < height then
            tx.up()
        elseif w % 2 == 0 and h > 1 then
            tx.down()
        end
    end
    
    if w < width then
        place_block()
        tx.right()
        tx.forward()
        tx.left()
    end
    
end



tx.goto_placemark('start')