quest entry_event_map begin
    state start begin

        function enter_level (vnum1,vnum2)
            lv_range={}
            lv_range[1] ={15,39}
            lv_range[2] ={40,64}
            lv_range[3] ={65,84}
            lv_range[4] ={85,99}
            lv_range[5] ={100,105}
            return lv_range [vnum1][vnum2]
        end

        function warp_to_map(modu)
            if modu == 1 then
                pc.warp(903*100, 83*100, 200)
            elseif modu == 2 then
                pc.warp(1025*100, 95*100, 200)
            else
                pc.warp(959*100, 176*100, 200)
            end
        end

        function check_limit()
            if game.get_event_flag("event_map_login_counter") == game.get_event_flag("event_map_player_max") then
                return 0
            else
                return 1
            end
        end

        function player_dialog()
            if pc.get_map_index() == 200 then
                say_title(gameforge.entry_event_map._010_sayTitle)
                say(gameforge.entry_event_map._020_say)

                local sub_sel = select (gameforge.locale.yes, gameforge.locale.no)

                if sub_sel == 2 then
                    return
                end

                warp_to_village()

                local counter = game.get_event_flag("event_map_login_counter")
                game.set_event_flag("event_map_login_counter", counter-1)
            end

            if game.get_event_flag("event_map_active") == 0 then
                say(locale.forked_man_say_cant)
            else
                say_title(gameforge.entry_event_map._010_sayTitle)
                say(gameforge.entry_event_map._030_say)

                local s = select(locale.forked_enter, locale.forked_no_enter)
                local min = game.get_event_flag("event_map_level_min")
                local max = game.get_event_flag("event_map_level_max")
                local empire = game.get_event_flag("event_map_empire")

                if s == 1 then
                  if pc.level < min or pc.level > max then
                    say_title(gameforge.entry_event_map._010_sayTitle)
                    say_reward(string.format(gameforge.entry_event_map._040_say, min, max))
                    return
                  end
                    
                    if pc.get_empire() != empire and empire != 0 then
                        say_title(gameforge.entry_event_map._010_sayTitle)
                        say(gameforge.entry_event_map._050_say)
                        return
                    end

                    if  entry_event_map.check_limit() == 0 then
                        say_title(gameforge.entry_event_map._010_sayTitle)
                        say(gameforge.entry_event_map._060_say)
                        return
                    end

                    say_title(gameforge.entry_event_map._010_sayTitle)
                    say(gameforge.entry_event_map._070_say)

                    if empire == 0 then 
                        entry_event_map.warp_to_map(pc.get_empire())
                    else 
                        local modu = math.mod(pc.get_player_id(),2)
                        entry_event_map.warp_to_map(modu)
                    end

                end
            end
        end

        function gm_exit_check()
            if pc.get_map_index() == 200 and pc.get_gm_level()== 5 then
                return 1
            else
                return 0
            end
        end

        when login or enter begin
            
            if pc.get_map_index() == 200 then

                if game.get_event_flag("event_map_active") == 0 then 
                    warp_to_village()
                end
                
                local counter = game.get_event_flag("event_map_login_counter")
                game.set_event_flag("event_map_login_counter", counter+1)
            end
        end



    when 11001.chat.gameforge.entry_event_map._080_npcChat or 11003.chat.gameforge.entry_event_map._080_npcChat or 11005.chat.gameforge.entry_event_map._080_npcChat with game.get_event_flag("event_map_active") == 1 begin
        entry_event_map.player_dialog()
    end



   when 11001.chat.gameforge.entry_event_map._090_npcChat or 11003.chat.gameforge.entry_event_map._090_npcChat or 11005.chat.gameforge.entry_event_map._090_npcChat with pc.get_gm_level()== 5 begin
        local s = select(gameforge.entry_event_map._100_select, gameforge.entry_event_map._110_select, gameforge.entry_event_map._120_select, gameforge.locale.cancel)
        
        if s == 3 then
            if entry_event_map.gm_exit_check() == 1 then
                say_title("GM: "..gameforge.entry_event_map._010_sayTitle)
                say(gameforge.entry_event_map._130_say)
                
                -- reset all setting for this event and nobody can enter it
                game.set_event_flag("event_map_active", 0)
                game.set_event_flag("event_map_choice", 0)
                game.set_event_flag("event_map_level_min", 0)
                game.set_event_flag("event_map_level_max", 0)
                game.set_event_flag("event_map_player_max",0)
                game.set_event_flag("event_map_empire", 0)
                game.set_event_flag("event_map_login_counter",0)
                
                -- teleport all out from the map
                warp_all_to_village(200, 30)
            else
                say_title("GM: "..gameforge.entry_event_map._010_sayTitle)
                say(gameforge.entry_event_map._140_say)
                return
            end
        end

      if s == 1 then
        say_title("GM: "..gameforge.entry_event_map._010_sayTitle)
        say(gameforge.entry_event_map._150_say)
        local choice = select ("1> min "..entry_event_map.enter_level(1,1).." max "..entry_event_map.enter_level(1,2),"2> min "..entry_event_map.enter_level(2,1).." max "..entry_event_map.enter_level(2,2), "3> min "..entry_event_map.enter_level(3,1).." max "..entry_event_map.enter_level(3,2), "4> min "..entry_event_map.enter_level(4,1).." max "..entry_event_map.enter_level(4,2),  "5> min "..entry_event_map.enter_level(5,1).." max "..entry_event_map.enter_level(5,2), gameforge.locale.cancel)

         if choice == 6 then
            return
        end
        
        say_title("GM: "..gameforge.entry_event_map._010_sayTitle)
        say(gameforge.entry_event_map._160_say)
        local amount_choice = input_number(gameforge.entry_event_map._240_say)

         if amount_choice > 200 or amount_choice <= 0 then
            say_title("GM: "..gameforge.entry_event_map._010_sayTitle)
            say(gameforge.entry_event_map._250_say)
            return
        end
       	
        local empire_names =
        {
            [0] = gameforge.priv_empire._33_array,
            [1] = gameforge.locale.empire_names_2,
            [2] = gameforge.locale.empire_names_3,
            [3] = gameforge.locale.empire_names_4,
        }

 
        say_title("GM: "..gameforge.entry_event_map._010_sayTitle)
        say(gameforge.entry_event_map._170_say)
        local kingdom_sub_set = select(empire_names[1], empire_names[2], empire_names[3], empire_names[0], gameforge.locale.cancel)

        if kingdom_sub_set == 5 then
          return
        end

        if kingdom_sub_set == 4 then
            kingdom_sub_set = 0
        end

        say_title("GM: "..gameforge.entry_event_map._010_sayTitle)
        say(gameforge.entry_event_map._180_say)
        say(gameforge.entry_event_map._190_say .. " " .. entry_event_map.enter_level(choice,1).. " - " ..entry_event_map.enter_level(choice,2))
        say(gameforge.entry_event_map._200_say .. " " .. amount_choice)
        say(gameforge.entry_event_map._210_say .. " " .. empire_names[kingdom_sub_set])

        local t = select(gameforge.locale.guild.yes, gameforge.locale.guild.no)

        if t == 2 then
          return
        end

            game.set_event_flag("event_map_active", 1)	
            game.set_event_flag("event_map_choice", choice)
            game.set_event_flag("event_map_level_min",entry_event_map.enter_level(choice,1))
            game.set_event_flag("event_map_level_max",entry_event_map.enter_level(choice,2))
            game.set_event_flag("event_map_player_max",amount_choice)
            game.set_event_flag("event_map_empire", kingdom_sub_set)
            
            if kingdom_sub_set == 0 then
                notice_multiline(string.format(gameforge.entry_event_map._220_notice_all, entry_event_map.enter_level(choice,1),entry_event_map.enter_level(choice,2)),notice_all) 
            else
                notice_multiline(string.format(gameforge.entry_event_map._230_notice_all, empire_names[kingdom_sub_set], entry_event_map.enter_level(choice,1),entry_event_map.enter_level(choice,2)),notice_all) 
            end
        elseif s== 2 then

            game.set_event_flag("event_map_active", 0)
            game.set_event_flag("event_map_choice", 0)
            game.set_event_flag("event_map_level_min", 0)
            game.set_event_flag("event_map_level_max", 0)
            game.set_event_flag("event_map_player_max",0)
            game.set_event_flag("event_map_empire", 0)
            game.set_event_flag("event_map_login_counter",0)

        end

            
        end
    end
end
