xRelPos = 0 -- "positive = forward"
yRelPos = 0 -- "positive = right"
zRelPos = 0 -- "positive = upwards"

facing = {
    x = 1,
    y = 0
}

slot_dust = 1
slot_empty = 2
num_dust = 4

function printPos()
    print("At (", xRelPos, ", ", yRelPos, ", ", zRelPos, ") facing (", facing.x, ", ", facing.y, ")")
end

-- helper functions for movement
function forward()
    xRelPos = xRelPos + facing.x
    yRelPos = yRelPos + facing.y

    if turtle.forward() then
        return true
    end
    
    xRelPos = xRelPos - facing.x
    yRelPos = yRelPos - facing.y

    turtle.dig()
    turtle.attack()
    return false
end

function forceForward()
    -- try moving forward until it works
    while not forward() do end
end

function upwards()
    zRelPos = zRelPos + 1
    if turtle.up() then
        return true
    end
    zRelPos = zRelPos - 1
    turtle.digUp()
    turtle.attackUp()
    return false
end

function forceUpwards()
    -- try moving upwards until it works
    while not upwards() do end
end

function downwards()
    zRelPos = zRelPos - 1
    if turtle.down() then
        return true
    end
    zRelPos = zRelPos + 1
    turtle.digDown()
    turtle.attackDown()
    return false
end

function forceDownwards()
    -- try moving upwards until it works
    while not downwards() do end
end

function forcePlace()
    -- try placing until it works
    while not turtle.place() do 
        turtle.dig()
        turtle.attack()
    end
end

function forcePlaceDown()
    -- try placing until it works
    while not turtle.placeDown() do 
        turtle.digDown()
        turtle.attackDown()
    end
end

function forcePlaceUp()
    -- try placing until it works
    while not turtle.placeUp() do 
        turtle.digUp()
        turtle.attackUp()
    end
end

function turnRight()
    if facing.x == 1 and facing.y == 0 then -- facing forward
        facing.x = 0
        facing.y = 1
    elseif facing.x == 0 and facing.y == 1 then -- facing right
        facing.x = -1
        facing.y = 0
    elseif facing.x == -1 and facing.y == 0 then -- facing backward
        facing.x = 0
        facing.y = -1
    elseif facing.x == 0 and facing.y == -1 then -- facing left
        facing.x = 1
        facing.y = 0
    else 
        print("turnRight: This should never happen")
    end
    turtle.turnRight()
end

function turnLeft()
    if facing.x == 1 and facing.y == 0 then -- facing forward
        facing.x = 0
        facing.y = -1
    elseif facing.x == 0 and facing.y == 1 then -- facing right
        facing.x = 1
        facing.y = 0
    elseif facing.x == -1 and facing.y == 0 then -- facing backward
        facing.x = 0
        facing.y = 1
    elseif facing.x == 0 and facing.y == -1 then -- facing left
        facing.x = -1
        facing.y = 0
    else 
        print("turnLeft: This should never happen")
    end
    turtle.turnLeft()
end

function faceTo(x, y)
    if (x == facing.x) and (y == facing.y) then
        return
    end
    if (x == 1 and facing.x == -1) or (x == -1 and facing.x == 1) or (y == 1 and facing.y == -1) or (y == -1 and facing.y == 1) then
        turnRight()
        turnRight()
    elseif (facing.y == -1 and x == 1) or (facing.y == 1 and x == -1) or (facing.x == 1 and y == 1) or (facing.x == -1 and y == -1) then
        turnRight()
    elseif (facing.y == -1 and x == -1) or (facing.y == 1 and x == 1) or (facing.x == 1 and y == -1) or (facing.x == -1 and y == 1) then
        turnLeft()
    else
        print("faceTo: This should never happen")
    end
end

function moveTo(x, y)
    if (yRelPos > y) then
        faceTo(0, -1)
        while yRelPos > y do
            forward()
        end
    elseif (yRelPos < y) then
        faceTo(0, 1)
        while yRelPos < y do
            forward()
        end
    end

    if (xRelPos > x) then
        faceTo(-1, 0)
        while xRelPos > x do
            forward()
        end
    elseif (xRelPos < x) then
        faceTo(1, 0)
        while xRelPos < x do
            forward()
        end
    end
end

function resupplyItem(slot, num)
    turtle.select(slot)
    while (turtle.getItemCount(slot) < num) do
        sleep(1)
        turtle.suckDown(num - turtle.getItemCount(slot))
    end
end

print("Industrial Blast Furnace even item distribution program")

while true do
    resupplyItem(slot_dust, num_dust)

    for i = 1, 4 do
        forceForward(); forceForward()
        
        -- check if anything is still in the furnace
        -- use different slot than dust slot since the item type may have changed
        turtle.select(slot_empty)
        while turtle.suckDown() do
            turtle.dropDown()
            sleep(5)
        end

        turtle.select(slot_dust)
        turtle.dropDown(1)

        forceForward(); forceForward()
        turnLeft()
    end
end