quest levelup begin
    state run begin
        when letter begin
            local current = pc.getqf("current")
            local completed_level = pc.getqf("complete");

            if current == 0 and 
                pc.get_level() > completed_level and 
                pc.get_level() >  1 and 
                pc.get_level() < 90 then

                local lev = pc.get_level()
                pc.setqf("buttonstate", 2)
                makequestbutton(string.format(gameforge.levelup._10_makequestbutton, lev))
                pc.setqf("current", lev)
                pc.setqf("select", 1)
                pc.setqf("remain", special.levelup_quest[lev][2])
                setskin(NOWINDOW)
            end

            setstate(start)
        end
    end

    state start begin
        when info begin
            local lev = pc.getqf("current")
            if lev == 0 then return end

            setskin(SCROLL)
            setbgimage('level_bg.tga')
            say("")
            say("")
            say("")
            local s = pc.getqf("select")
            if s == 0 then s = 1 end
            addimage(20,12,special.levelup_img[special.levelup_quest[lev][s*2-1]])
            setcolor(0,0,0)

            say(string.format(gameforge.levelup._20_say,mob_name(special.levelup_quest[lev][s*2-1])))
            say(string.format(gameforge.levelup._26_say,special.levelup_quest[lev][s*2]))

            raw_script(string.format(gameforge.levelup._27_say,special.levelup_quest[lev][5]))

            say("")

            if lev <= table.getn(special.levelup_reward1) then
                raw_script(item_name(special.levelup_reward1[lev][pc.job+1]))
            elseif lev> table.getn(special.levelup_reward1) and lev < 26 then
                raw_script(locale.levelup.random_item)
            end	
            if lev>=21 then
                local reward_gold = special.levelup_reward_gold21
                if lev >= 31 and lev <= 40 then
                    reward_gold = special.levelup_reward_gold31
                elseif lev >= 41 and lev <= 50 then
                    reward_gold = special.levelup_reward_gold41
                elseif lev >= 51 and lev <= 83 then
                    reward_gold = special.levelup_reward_gold51
                elseif lev >= 84 and lev <= 90 then
                    reward_gold = special.levelup_reward_gold51
                end
                local max_gold_i = table.getn(reward_gold)
                raw_script(reward_gold[1][1].."-"..reward_gold[max_gold_i][1].." "..locale.gold)
            end

            raw_script(newline)

            levelup.show_mob_pos(lev)

            select(gameforge.locale.confirm)

            clearmapsignal()
            setskin(NOWINDOW)
        end

        when login begin
            local lev = pc.getqf("current")

            if lev != 0 then
                if pc.getqf("remain") <= 0 then
                    setskin(NOWINDOW)
                    pc.setqf("buttonstate", 3)
                    makequestbutton(string.format(gameforge.levelup._30_makequestbutton, lev))
                else
                    setskin(NOWINDOW)
                    pc.setqf("buttonstate", 1)
                    makequestbutton(string.format(gameforge.levelup._40_makequestbutton, lev))
                end
            end
        end

        when button begin
            local lev = pc.getqf("current")

            if lev == 0 then return end

            if pc.getqf("buttonstate") == 3 then

                if pc.getqf("complete") != lev then
                    pc.setqf("complete", lev)

                local s = pc.getqf("select")
                if s == 0 then s = 1 end

                say_title(string.format(gameforge.levelup._50_sayTitle, lev))
                say(gameforge.levelup._60_say)
                    say("")

                if lev <= 20 then 
                    if lev <= table.getn(special.levelup_reward1) then
                        pc.give_item(gameforge.levelup._64_give..lev..gameforge.levelup._65_give, 
                        special.levelup_reward1[lev][pc.job+1])
                    else
                        local v = number(1,100)

                        if v <= special.levelup_reward3[1][1] then
                            pc.give_item(gameforge.levelup._64_give..lev..gameforge.levelup._65_give, special.levelup_reward3[1][2],special.levelup_reward3[1][3])
                        elseif v <= special.levelup_reward3[2][1] then
                            pc.give_item(gameforge.levelup._64_give..lev..gameforge.levelup._65_give, special.levelup_reward3[2][2],special.levelup_reward3[2][3])
                        elseif v <= special.levelup_reward3[3][1] then
                            pc.give_item(gameforge.levelup._64_give..lev..gameforge.levelup._65_give, special.levelup_reward3[3][2],special.levelup_reward3[3][3])
                        elseif v <= special.levelup_reward3[4][1] then
                            pc.give_item(gameforge.levelup._64_give..lev..gameforge.levelup._65_give, special.levelup_reward3[4][2],special.levelup_reward3[4][3])
                        else
                            pc.give_item(gameforge.levelup._64_give..lev..gameforge.levelup._65_give, special.levelup_reward3[5][2],special.levelup_reward3[5][3])
                        end
                    end

                    pc.give_exp_perc(gameforge.levelup._64_give..lev..gameforge.levelup._66_give, lev, special.levelup_quest[lev][5])

                    if lev == 12 or lev == 14 or lev == 16 or lev == 18 or lev == 20 then
                        pc.give_item2(50083)
                        say_reward(string.format(gameforge.levelup._70_sayReward, item_name(50083) ))
                        wait()
                    end

                else

                    local ll = lev - 20
                    if lev < 26 then

                        local n = table.getn(special.levelup_reward_item_21[ll])
                        local t = special.levelup_reward_item_21[ll][number(1, n)]
                        say_reward(gameforge.levelup._80_sayReward)
                        if type(t) == 'table' then
                            pc.give_item2(t[1], t[2])
                            say_reward(string.format(gameforge.levelup._90_sayReward, item_name(t[1])..  " "..t[2]))
                        else
                            pc.give_item2(t)
                            say_reward(string.format(gameforge.levelup._100_sayReward, item_name(t)))
                        end

                    end
                    local reward_gold = special.levelup_reward_gold21
                    local reward_exp = special.levelup_reward_exp21

                    if lev >= 31 and lev <= 40 then
                        reward_gold = special.levelup_reward_gold31
                        reward_exp = special.levelup_reward_exp31
                    elseif lev >= 41 and lev <= 50 then
                        reward_gold = special.levelup_reward_gold41
                        reward_exp = special.levelup_reward_exp41
                    elseif lev >= 51 and lev <= 83 then
                        reward_gold = special.levelup_reward_gold51
                        reward_exp = special.levelup_reward_exp51
                    elseif lev >= 84 and lev <= 90 then
                        reward_gold = special.levelup_reward_gold51
                        reward_exp = special.levelup_reward_exp84
                    end


                    n = number(0, 99)
                    local i = 0
                    for i = 1, table.getn(reward_gold) do
                        if n < reward_gold[i][2] then
                            pc.change_money(reward_gold[i][1])
                            say_reward(string.format(gameforge.levelup._110_sayReward, reward_gold[i][1]))
                            break
                        else
                            n = n - reward_gold[i][2]
                        end
                    end


                    n = number(0, 99)
                    i = 0
                    for i = 1, table.getn(reward_exp) do
                        if n < reward_exp[i][2] then
                            pc.give_exp_perc(gameforge.levelup._64_give..lev..gameforge.levelup._66_give, lev, reward_exp[i][1])
                            say_reward(string.format(gameforge.levelup._120_sayReward, reward_exp[i][1]))
                            break
                        else
                            n = n - reward_exp[i][2]
                        end
                    end

                        say("")

                        wait()
                    end
                end

                local old_lev = lev

                lev = lev + 1


                if lev > 1 and lev < 90 and pc.level >= lev then
                    pc.setqf("buttonstate", 2)
                    makequestbutton(string.format(gameforge.levelup._130_makequestbutton, lev))
                    pc.setqf("current", lev)
                    pc.setqf("select", 1)
                    pc.setqf("remain",special.levelup_quest[lev][2])
                    setskin(NOWINDOW)
                    return
                else
                    pc.setqf("current", 0)
                    q.done()
                end

            elseif pc.getqf("buttonstate") == 2 then

                local lev = pc.getqf("current")
                if lev == 0 then
                    q.done()
                end

                say_title(gameforge.levelup._140_sayTitle)

                say(gameforge.levelup._150_say)
                local sel = select(
                mob_name(special.levelup_quest[lev][1]).." "..special.levelup_quest[lev][2]..gameforge.levelup._155_say, 
                mob_name(special.levelup_quest[lev][3]).." "..special.levelup_quest[lev][4]..gameforge.levelup._155_say
                )
                
                
                setskin(SCROLL)
                setbgimage('level_bg.tga')
                say("")
                say("")
                say("")
                pc.setqf("select", sel)
                addimage(20, 12, special.levelup_img[special.levelup_quest[lev][sel*2-1]])
                setcolor(0,0,0)

                say(string.format(gameforge.levelup._20_say, mob_name(special.levelup_quest[lev][sel*2-1])))
                say(string.format(gameforge.levelup._26_say,special.levelup_quest[lev][sel*2]))

                say(string.format(gameforge.levelup._27_say,special.levelup_quest[lev][5]))

                if lev <= table.getn(special.levelup_reward1) then
                    raw_script(", "..item_name(special.levelup_reward1[lev][pc.job+1]))
                elseif lev> table.getn(special.levelup_reward1) and lev < 26 then
                    raw_script(", "..locale.levelup.random_item)
                end
                if lev>=21 then 
                    raw_script(", "..locale.gold)
                end
                raw_script(newline)

                levelup.show_mob_pos(lev)

                local s =  select(gameforge.levelup._160_select)

                if s == 1 then
                    setskin(NOWINDOW)
                    pc.setqf("remain",special.levelup_quest[lev][sel*2])
                    syschat(string.format(gameforge.levelup._170_chat,  mob_name(special.levelup_quest[lev][sel*2-1])," ".. special.levelup_quest[lev][sel*2]))
                    q.set_title(string.format(gameforge.levelup._180_qSetTitle, mob_name(special.levelup_quest[lev][sel*2-1])))
                    q.set_counter(gameforge.levelup._250_say, pc.getqf("remain"))
                    q.start()
                end

                setskin(NOWINDOW)
                clearmapsignal()

            elseif pc.getqf("buttonstate") == 1 then

                local s = pc.getqf("select")
                if s == 0 then s = 1 end

                local lev = pc.getqf("current")

                if lev != 0 then
                say_title(gameforge.levelup._140_sayTitle)
                say(string.format(gameforge.levelup._190_say, pc.getqf("remain")..gameforge.levelup._195_say..mob_name(special.levelup_quest[lev][s*2-1])))
                local ss =  select(gameforge.levelup._200_select, gameforge.locale.levelup.prev_quest_go)				
                
                    
                    if ss == 1 then
                        setskin(SCROLL)
                        setbgimage('level_bg.tga')
                        say("")
                        say("")
                        say("")
                        addimage(20,12,special.levelup_img[special.levelup_quest[lev][s*2-1]])
                        setcolor(0,0,0)

                        say(string.format(gameforge.levelup._20_say,mob_name(special.levelup_quest[lev][s*2-1])))
                        say(string.format(gameforge.levelup._26_say,special.levelup_quest[lev][s*2]))

                        raw_script(string.format(gameforge.levelup._27_say,special.levelup_quest[lev][5]))

                        say("")

                        if lev <= table.getn(special.levelup_reward1) then
                            raw_script(","..item_name(special.levelup_reward1[lev][pc.job+1]))
                        elseif lev> table.getn(special.levelup_reward1) and lev < 26 then
                            raw_script(","..locale.levelup.random_item)
                        end	

                        if lev>=21 then 
                            raw_script(","..locale.gold)
                        end

                        raw_script(newline)

                        select(gameforge.locale.confirm)
                        setskin(NOWINDOW)
                        clearmapsignal()

                    end

                    setskin(NOWINDOW)
                    q.set_title(string.format(gameforge.levelup._180_qSetTitle, mob_name(special.levelup_quest[lev][s*2-1])))
                    q.set_counter(gameforge.levelup._250_say,pc.getqf("remain")) 
                    q.start()
                end
            end

            pc.setqf("buttonstate", -1)
        end

        when levelup begin
            local lev = pc.getqf("current");

            if lev == 0 then
                lev = pc.level


                if lev < 2 or lev > 90 then return end

                pc.setqf("buttonstate", 2)
                makequestbutton(string.format(gameforge.levelup._10_makequestbutton, lev))
                pc.setqf("current", lev)
                pc.setqf("select", 1)
                pc.setqf("remain", special.levelup_quest[lev][2])
                setskin(NOWINDOW)
            end
        end

        when kill begin
            local sel = pc.getqf("select")

            if sel == 0 then
                sel = 1
            end

            local lev = pc.getqf("current")

            if lev != 0 and npc.race == (special.levelup_quest[lev][sel*2-1]) and pc.getqf("buttonstate") == -1 then
                local remain = pc.getqf("remain") - 1

                if remain <= 0 then
                    pc.setqf("remain", 0)
                    q.set_counter_value(0)
                    setskin(NOWINDOW)
                    makequestbutton(string.format(gameforge.levelup._50_sayTitle, lev))
                    pc.setqf("buttonstate", 3)
                else
                    pc.setqf("remain", remain)
                    q.set_counter_value(remain)
                end
            end
        end

        function show_mob_pos(lev)
            map_index = pc.get_map_index()

            if map_index <= 0 then
                test_chat(string.format(gameforge.levelup._210_chat, map_index))
                return
            end

            if map_index > table.getn(special.lvq_map) then
                test_chat(string.format(gameforge.levelup._220_chat, map_index..") > max("..table.getn(special.lvq_map)))
                return
            end

            lev_quest_list = special.lvq_map[map_index]
            if not lev_quest_list then
                test_chat(string.format(gameforge.levelup._230_chat, map_index))
                return
            end

            mob_pos_list = lev_quest_list[lev]
            if not mob_pos_list then
                test_chat(string.format(gameforge.levelup._230_chat, map_index..")][lev("..lev))
                return
            end

            table.foreachi(
            mob_pos_list, 
            function (n, mob_pos) 
                test_chat(string.format(gameforge.levelup._240_chat, mob_pos[1],mob_pos[2]))
                addmapsignal(mob_pos[1]*100, mob_pos[2]*100) 
            end
            )
            setmapcenterposition(200, 0)
        end
    end
end
