----------------------------------
-- 거미굴 3층과 보스룸 관련 전부
--  2012.2.3 김용욱
----------------------------------
define lair_time 1200
define lair_enter_wait_time 300
define king_kill_wait_time 180
define access_limit_time 3600

quest spider_dungeon_3floor_boss begin
	state start begin
	--2, 던전 끝 시간.
		when spider_end_timer.server_timer with game.get_event_flag("spider_dead"..get_channel_id()) == 0 begin  --
			warp_all_in_area_to_area(82700, 612700, 93700,625700, 69100, 614800, 69100, 614800)
			game.set_event_flag("spider_lair_ongoing_"..get_channel_id(),0)
			game.set_event_flag("spider_lair_leader_"..get_channel_id(), 0)
			game.set_event_flag("spider_dead"..get_channel_id(),0)
			purge_area(82700, 612700, 93700,625700)
		end
		
		when spider_dead_timer.server_timer begin
			warp_all_in_area_to_area(82700, 612700, 93700,625700, 69100, 614800, 69100, 614800)
			game.set_event_flag("spider_lair_ongoing_"..get_channel_id(), 0)
			game.set_event_flag("spider_lair_leader_"..get_channel_id(), 0)
			game.set_event_flag("spider_dead"..get_channel_id(),0)
			purge_area(82700, 612700, 93700,625700)
		end

		when login or levelup or enter with pc.get_level() >= 50 begin
			set_state( step1 )
		end
		
		when 30130.chat.gameforge.spider_dungeon_3floor_boss._10_npcChat begin
			say(gameforge.spider_dungeon_3floor_boss._20_say)
		end
	end

	state step1 begin
		when 20011.chat.gameforge.spider_dungeon_3floor_boss._30_npcChat with is_test_server() begin   --테스트용
			set_state(start)
			say(gameforge.spider_dungeon_3floor_boss._40_say)
		end
		
		when 30130.chat.gameforge.spider_dungeon_3floor_boss._10_npcChat begin
			say(gameforge.spider_dungeon_3floor_boss._20_say)
		end
		
		when 20355.chat.gameforge.spider_dungeon_3floor_boss._50_npcChat begin   --경비대장에게 말을 걸어 퀘스트를 시작한다.
			
			say_title(mob_name(20355)) 
			----"12345678901234567890123456789012345678901234567890"|
			say(gameforge.spider_dungeon_3floor_boss._60_say)
			set_state( step2 )
			pc.give_item2(76019)
		end
	end

	state step2 begin --비석에 탁본을 뜨러 가야한다.
		when 20011.chat.gameforge.spider_dungeon_3floor_boss._30_npcChat with is_test_server() begin   --테스트용
			set_state(start)
			say(gameforge.spider_dungeon_3floor_boss._40_say)
		end
		
		-----------퀘스트편지2------------
		when letter begin
			send_letter(gameforge.spider_dungeon_3floor_boss._70_sendLetter)

			local v = find_npc_by_vnum(30130)
			if 0 != v then
			    target.vid("__TARGET__", v, mob_name(30130))
			end
		end

		when button or info begin
			say_title(gameforge.spider_dungeon_3floor_boss._70_sendLetter)
			say(gameforge.spider_dungeon_3floor_boss._80_say)
			say("")
		end

		when 30130.chat.gameforge.spider_dungeon_3floor_boss._70_sendLetter begin
			target.delete("__TARGET__")
			
			say_title(pc.getname())
			----"12345678901234567890123456789012345678901234567890"|
			say(gameforge.spider_dungeon_3floor_boss._90_say)
			set_state(step3)
		end
		
		
		when 30130.chat.gameforge.spider_dungeon_3floor_boss._10_npcChat begin
			say(gameforge.spider_dungeon_3floor_boss._20_say)
		end
		
		when 20355.chat.gameforge.spider_dungeon_3floor_boss._50_npcChat begin
			
			say_title(mob_name(20355))
			----"12345678901234567890123456789012345678901234567890"|
			say(gameforge.spider_dungeon_3floor_boss._100_say)
		end
		
	end

	state step3 begin --탁본을 떴으니 경비에게 돌아간다.
		when 20011.chat.gameforge.spider_dungeon_3floor_boss._30_npcChat with is_test_server() begin   --테스트용
			set_state(start)
			say(gameforge.spider_dungeon_3floor_boss._40_say)
		end
		
		when 30130.chat.gameforge.spider_dungeon_3floor_boss._10_npcChat begin
			say(gameforge.spider_dungeon_3floor_boss._20_say)
		end
		
		when letter begin
			send_letter(gameforge.spider_dungeon_3floor_boss._110_sendLetter)

			--경비대장을 가리킨다.
			local v = find_npc_by_vnum(20355)
			if 0 != v then
			    target.vid("__TARGET__", v, mob_name(20355))
			end
		end

		when button or info begin
			say_title(gameforge.spider_dungeon_3floor_boss._110_sendLetter)
			say(gameforge.spider_dungeon_3floor_boss._120_say)
			say("")
		end
		
		--을두지를 선택하면 해당 퀘스트에 관련된 대화를 나눌 수 있다.
		when 20355.chat.gameforge.spider_dungeon_3floor_boss._110_sendLetter begin
			say_title(mob_name(20355))
			say(gameforge.spider_dungeon_3floor_boss._130_say)
			set_state(step4)
		end
		
	end
		
	state step4 begin  --을두지에게 탁본을 맡기자.
		when 20011.chat.gameforge.spider_dungeon_3floor_boss._30_npcChat with is_test_server() begin   --테스트용
			set_state(start)
			say(gameforge.spider_dungeon_3floor_boss._40_say)
		end
		
		when 30130.chat.gameforge.spider_dungeon_3floor_boss._10_npcChat begin
			say(gameforge.spider_dungeon_3floor_boss._20_say)
		end
		
		-----------퀘스트편지2------------
		when letter begin
			send_letter(gameforge.spider_dungeon_3floor_boss._110_sendLetter)

			-- 을두지를 가리킨다.
			local v = find_npc_by_vnum(20011)
			if 0 != v then
			    target.vid("__TARGET__", v, mob_name(20011))
			end
		end

		when button or info begin
			say_title(gameforge.spider_dungeon_3floor_boss._110_sendLetter)
			say(gameforge.spider_dungeon_3floor_boss._140_say)
			say("")
		end
		
		when 20011.chat.gameforge.spider_dungeon_3floor_boss._110_sendLetter begin
			target.delete("__TARGET__")
			
			say_title(mob_name(20011))
			say(gameforge.spider_dungeon_3floor_boss._150_say)
			say("")
			
			set_state(step5)
		end

		when 20355.chat.gameforge.spider_dungeon_3floor_boss._110_sendLetter begin
			say_title(mob_name(20355))
			say(gameforge.spider_dungeon_3floor_boss._130_say)
		end
	end

	state step5 begin  --경비한테 편지 배달
		when 20011.chat.gameforge.spider_dungeon_3floor_boss._30_npcChat with is_test_server() begin   --테스트용
			set_state(start)
			say(gameforge.spider_dungeon_3floor_boss._40_say)
		end
		
		when 30130.chat.gameforge.spider_dungeon_3floor_boss._10_npcChat begin
			say(gameforge.spider_dungeon_3floor_boss._20_say)
		end
		
		when letter begin
			send_letter(gameforge.spider_dungeon_3floor_boss._110_sendLetter)

			-- 을두지를 가리킨다.
			local v = find_npc_by_vnum(20355)
			if 0 != v then
			    target.vid("__TARGET__", v, mob_name(20355))
			end
		end

		when button or info begin
			say_title(gameforge.spider_dungeon_3floor_boss._170_sayTitle)
			say(gameforge.spider_dungeon_3floor_boss._180_say)
			say("")
		end
		
		when 20355.chat.gameforge.spider_dungeon_3floor_boss._190_npcChat begin
			target.delete("__TARGET__")
			say_title(mob_name(20355))
			----"12345678901234567890123456789012345678901234567890"|
			say(gameforge.spider_dungeon_3floor_boss._200_say)
			say("")
			
			set_state(step6) pc.give_item2(76019)
		end	
		
		when 20011.chat.gameforge.spider_dungeon_3floor_boss._110_sendLetter begin
			say_title(mob_name(20011))
			say(gameforge.spider_dungeon_3floor_boss._160_say)
			say("")
		end
		
	end

	state step6 begin --경비는 나만 일시킨다. 을두지와 상의하자.
		when 20011.chat.gameforge.spider_dungeon_3floor_boss._30_npcChat with is_test_server() begin   --테스트용
			set_state(start)
			say(gameforge.spider_dungeon_3floor_boss._40_say)
		end
		
		when 30130.chat.gameforge.spider_dungeon_3floor_boss._10_npcChat begin
			say(gameforge.spider_dungeon_3floor_boss._20_say)
		end
		
		when letter begin
			send_letter(gameforge.spider_dungeon_3floor_boss._190_npcChat)
			
			-- 을두지를 가리킨다.
			local v = find_npc_by_vnum(20011)
			if 0 != v then
			    target.vid("__TARGET__", v, mob_name(20011))
			end
		end

		when button or info begin
			say_title(gameforge.spider_dungeon_3floor_boss._190_npcChat)
			say(gameforge.spider_dungeon_3floor_boss._200_say)
			say("")
		end

		--을두지를 선택하면 해당 퀘스트에 관련된 대화를 나눌 수 있다.
		when 20011.chat.gameforge.spider_dungeon_3floor_boss._190_npcChat begin
			target.delete("__TARGET__")
			say_title(mob_name(20011))
			say(gameforge.spider_dungeon_3floor_boss._230_say)
			
			set_state(boss_ok)
			
		end
		
	end

	state boss_ok begin  -- 열쇠를 만들수 있게 된 최종상태. 이제부터 거미굴 보스룸에 입장할 수 있다.
		when 20011.chat.gameforge.spider_dungeon_3floor_boss._30_npcChat with is_test_server() begin   --테스트용
			set_state(start)
			say(gameforge.spider_dungeon_3floor_boss._40_say)
		end

		when 20011.chat.gameforge.spider_dungeon_3floor_boss._210_npcChat begin
			
			if pc.count_item(30025) > 0 and pc.count_item(30056) > 0 and pc.count_item(30057) > 0 and pc.count_item(30058) > 0 and pc.count_item(30059) > 0 and pc.count_item(30326) > 0 then
			    say_title(mob_name(20011))
			    say(gameforge.spider_dungeon_3floor_boss._250_say)
			    pc.remove_item(30025, 1)
			    pc.remove_item(30056, 1)
			    pc.remove_item(30057, 1)
			    pc.remove_item(30058, 1)
			    pc.remove_item(30059, 1)
			    pc.remove_item(30326, 1)
			    if math.random(1,2000) <= 1000 then
			        say(gameforge.spider_dungeon_3floor_boss._260_say)
			        pc.give_item2(30324, 1)
			    else
			        say(gameforge.spider_dungeon_3floor_boss._270_say)
			    end
			else
			    say_title(mob_name(20011))
			    say(gameforge.spider_dungeon_3floor_boss._280_say)
			end
		end
		
		


		when 30130.chat.gameforge.spider_dungeon_3floor_boss._10_npcChat begin

						if game.get_event_flag("spider_lair_ongoing_"..get_channel_id()) == 1 then
			
			    local starttime = game.get_event_flag("spider_lair_start_time_"..get_channel_id())
			    local current_time = get_global_time()
			    
			    if party.get_leader_pid() == game.get_event_flag("spider_lair_leader_"..get_channel_id()) then
			    
			        if  starttime +  lair_enter_wait_time >= current_time  and party.get_leader_pid() == game.get_event_flag("spider_lair_leader_"..get_channel_id()) and game.get_event_flag("spider_lair_ongoing_"..get_channel_id()) == 1 then
			            say(gameforge.spider_dungeon_3floor_boss._300_say) -- 테스트용
			            local warp =  select(locale.confirm,locale.cancel)
			            if warp == 1 then
			                pc.setqf("firstIn",1)
			                pc.setqf("retry_limit_time", starttime + lair_time + access_limit_time )
			                set_state(inLair)
			                pc.warp(88100,614800)
			                return
			            end
			        else
			            say(gameforge.spider_dungeon_3floor_boss._310_say) -- 테스트용
			            return
			        end
			    else
										local remain_time = (game.get_event_flag("spider_lair_end_time_"..get_channel_id() ) - get_global_time()) / 60
										if remain_time < 0 then
											say(gameforge.spider_dungeon_3floor_boss._410_say)
										 	game.set_event_flag("spider_lair_ongoing_"..get_channel_id(),0)
								game.set_event_flag("spider_lair_leader_"..get_channel_id(), 0)
  		  					game.set_event_flag("spider_dead"..get_channel_id(),0)
											return
										end
			        say(gameforge.spider_dungeon_3floor_boss._320_say)
			        --- here we should change to string.format %s key
			        say(string.format(gameforge.spider_dungeon_3floor_boss._330_say, math.ceil(remain_time)))
			        return
			    end	
			elseif  pc.getqf("retry_limit_time") > get_global_time() then
								local access_limit = (pc.getqf("retry_limit_time") - get_global_time()) / 60
			    say(gameforge.spider_dungeon_3floor_boss._340_say)
			    -- here we should also change to string.format %s key
			    say(string.format(gameforge.spider_dungeon_3floor_boss._350_say, math.ceil(access_limit)))
			    return
			elseif party.is_leader()   then
			     if false == pc.can_warp() then
			          say(gameforge.spider_dungeon_3floor_boss._360_say)
			          return
			     end
			    if pc.count_item(30324) > 0 then
			        say(gameforge.spider_dungeon_3floor_boss._370_say)
			        local warp = select(locale.confirm,locale.cancel)
			        if warp == 1 then
			            
			            set_state(inLair)
			            
			            pc.remove_item(30324, 1)
			            
			            mob.spawn(2094,369,551,0,0)
			            
			            --값 설정.
			            game.set_event_flag("spider_lair_ongoing_"..get_channel_id()		,			1)
			            game.set_event_flag("spider_lair_start_time_"..get_channel_id()	, get_global_time())
			            game.set_event_flag("spider_lair_end_time_"..get_channel_id()		, get_global_time() + lair_time)
			            game.set_event_flag("spider_lair_leader_"..get_channel_id()			, party.get_leader_pid())
			            
			            pc.setqf("retry_limit_time", get_global_time() + lair_time + access_limit_time )
			            pc.setqf("firstIn",1)
			            
			            
			            server_timer("spider_end_timer", lair_time )
			            
			            
			            pc.warp(88100,614800)
			        end
			        return
			    else
			        say(gameforge.spider_dungeon_3floor_boss._380_say)
			        return
			    end
			else --리더 아니다.
			    say(gameforge.spider_dungeon_3floor_boss._390_say)
			    return
			end
			
		end
		

	end

	state inLair begin
		when 20011.chat.gameforge.spider_dungeon_3floor_boss._30_npcChat with is_test_server() begin   --테스트용
			set_state(start)
			say(gameforge.spider_dungeon_3floor_boss._40_say)
		end
		


		when login  begin 
			-- 정상적으로 보스룸에 입장. 
			--if pc.getqf("firstIn") == 1  and party.get_leader_pid() == game.get_event_flag("spider_lair_leader_"..get_channel_id()) then
			if get_global_time() < pc.getqf("retry_limit_time") -  access_limit_time and party.get_leader_pid() == game.get_event_flag("spider_lair_leader_"..get_channel_id()) then
				pc.setqf("firstIn",0)
			elseif pc.getx() >= 827 and pc.getx() <= 937 and pc.gety() >= 6127 and  pc.gety() <= 6257 then 
				pc.warp(95900,571000)
				pc.remove_item(30327, 1)
			else -- 위에서 팅겨낸 유저가 밖에서 로그인 하게 되면, 접근 제한 시간을 셋팅해줌.
				pc.remove_item(30327, 1)
			    set_state( boss_ok )
			    local limit = pc.getqf("retry_limit_time")
			    
			    if limit > ( get_global_time() + access_limit_time ) then
			        limit = ( get_global_time() + access_limit_time )
			    end
			    pc.setqf("retry_limit_time", limit )
			    
			    local duration = limit - get_global_time()
			end
		end

		when 30130.chat.gameforge.spider_dungeon_3floor_boss._10_npcChat begin
			if game.get_event_flag("spider_lair_ongoing_"..get_channel_id()) == 1 then
			    local starttime = game.get_event_flag("spider_lair_start_time_"..get_channel_id())
			    local current_time = get_global_time()
			    
			    if party.get_leader_pid() == game.get_event_flag("spider_lair_leader_"..get_channel_id()) then
			        if  starttime +  lair_enter_wait_time >= current_time  and party.get_leader_pid() == game.get_event_flag("spider_lair_leader_"..get_channel_id()) and game.get_event_flag("spider_lair_ongoing_"..get_channel_id()) == 1 then
			            say(gameforge.spider_dungeon_3floor_boss._300_say) -- 테스트용
			            local warp =  select(locale.confirm,locale.cancel)
			            if warp == 1 then
			                pc.warp(88100,614800)
			                return
			            end
					end
				end
			end
		end
 
		when 2092.kill begin --대왕거미 죽임
			server_timer("spider_dead_timer", king_kill_wait_time)
			game.set_event_flag("spider_dead"..get_channel_id(), 1)
			notice_multiline(gameforge.spider_dungeon_3floor_boss._400_say,notice_in_map)
		end
		
		
		when 2095.kill begin --거미알 죽임
			local kingVid = game.get_event_flag("king_vid")
			local remain_egg = game.get_event_flag("remain_egg"..get_channel_id())
			notice_multiline(gameforge.spider_dungeon_3floor_boss._420_notice,notice_in_map)
			
			if remain_egg > 0 then
			    remain_egg = remain_egg - 1
			    game.set_event_flag("remain_egg"..get_channel_id(), remain_egg)
			end
			
			--거미의 공격력 보정
			 npc.set_vid_attack_mul(kingVid, 10/(remain_egg + 1))
			 
			 --거미가 받는 피해량 보정
			 npc.set_vid_damage_mul(kingVid, 10/(remain_egg + 1))
		end
		
		
		--  2012.4.5 이상현 리젠위치 수정
		when 30327.use with pc.getx() >= 827 and pc.get_map_index() == 217 and pc.getx() <= 937 and pc.gety() >= 6127 and  pc.gety() <= 6257 begin
			--say("맵번호 : "..pc.get_map_index())
			pc.remove_item(30327, 1)
			i = mob.spawn(2095,400,566,0,0)
			mob.spawn(2095,400,594,0,0)
			mob.spawn(2095,362,600,0,0)
			mob.spawn(2095,337,599,0,0)
			mob.spawn(2095,335,581,0,0)
			mob.spawn(2095,344,562,0,0)
			mob.spawn(2095,364,588,0,0)
			mob.spawn(2095,379,562,0,0)
			mob.spawn(2095,368,525,0,0)
			
			
			local kingVid = mob.spawn(2092,367,588,0,0)
			game.set_event_flag("king_vid",kingVid)
			--game.set_event_flag("remain_egg",9)
			game.set_event_flag("remain_egg"..get_channel_id(), 9)
		end
		
	end
end
