#include "queue.lua"

function StartInstructions(update_on, instruction_func)
    local instruction = {}
    instruction.thread = coroutine.create(instruction_func)

    local function add()
        local status = coroutine.status(instruction.thread)
        if status == 'suspended' then
            coroutine.resume(instruction.thread)
        elseif status == 'dead' then
            RemoveFromQue(instruction.queTicket)
        end
    end

    add()
    instruction.queTicket = AddToQue(update_on, add)

    return instruction.queTicket
end

function wait()
    coroutine.yield()
end

function wait_for(condition, while_func)
    while not condition() do
        if while_func then while_func() end
        wait()
    end
end

function wait_on(thread, while_func)
    wait_for(function ()
        return coroutine.status(thread) == "dead"
    end, while_func)
end

function wait_sec(time, while_func)
    time = time or 0
    local start_time = GetTime()

    wait_for(function ()
        local speed_multi = --[[ InputDown('f1') and 40 or ]] 1
        return GetTime() > start_time + time / speed_multi
    end, while_func)
end

function wait_keypress(key, while_func)
    wait_for(function()
        if while_func then while_func() end
        return InputPressed(key)
    end)

    wait()
end
