--2013.02.06 김용욱

--퀘스트 진행 npc
define YACKHWAN 20377 -- 약령도장 약환
define SCHOLAR 20380 -- 학자
define ALCHEMIST 20001 -- 연금술사
define BLACKSMITH 20016 -- 대장장이


define ITEM1_1 31074 -- 초마단의 명패
define ITEM1_2 31079 -- 약환 부하의 서신
define NPC1_1 20393 -- 약환 부하 
define DUNGEON_MAP_INDEX 351

define ITEM2_1 31080 -- 화염왕의 피

quest main_quest_flame_lv101 begin
	state start begin
	end
	state state0 begin
		when login or levelup or enter with pc.level >= 101 begin
			set_state (state1_1)
		end
	end
	
	-- 동굴의 정체
	state state1_1 begin 
		when letter begin 
			send_letter(gameforge.main_quest_flame_lv101._010_sendLetter) 
 			--npc 타겟 
			local v = find_npc_by_vnum(YACKHWAN) 
			if 0 != v then
				target.vid("__TARGET__", v, mob_name(YACKHWAN))
			end
		end

		when button or info begin 
			say_title(gameforge.main_quest_flame_lv101._010_sendLetter) 
			say(gameforge.main_quest_flame_lv101._020_say) 
			--npc 타겟 안내문
			--say_reward(locale.target_npc_info) 
		end

		when YACKHWAN.chat.gameforge.main_quest_flame_lv101._010_sendLetter begin 
			target.delete("__TARGET__") 
			say_title(mob_name(YACKHWAN)) 
			say(gameforge.main_quest_flame_lv101._030_say) 
			wait()--추가 대화 

			say_title(mob_name(YACKHWAN)) 
			say(gameforge.main_quest_flame_lv101._040_say) 
			wait()--추가 대화 

			say_title(mob_name(YACKHWAN)) 
			say(gameforge.main_quest_flame_lv101._050_say) 
			wait()--추가 대화 

			say_title(pc.getname()) 
			say(gameforge.main_quest_flame_lv101._070_say) 
			wait()--추가 대화 

			say_title(mob_name(YACKHWAN)) 
			say(gameforge.main_quest_flame_lv101._080_say)  
			--다음 스테이트로
			set_state (state1_2)
		end

		when leave begin 
			--q.done()
		end
	end
	state state1_2 begin  -- 단서 X, 부하 X
		when enter begin 
			-- notice 설정
			notice_multiline(gameforge.main_quest_flame_lv101._090_notice,notice)
		end

		when letter begin 
			send_letter(gameforge.main_quest_flame_lv101._010_sendLetter) 
			q.set_counter_name(gameforge.main_quest_lv97._670_counterName)
			q.set_counter_value(2)
 		end

		when button or info begin 
			say_title(gameforge.main_quest_flame_lv101._100_sayTitle) 
			say(gameforge.main_quest_flame_lv101._110_say) 
			
			say(gameforge.main_quest_lv97._670_counterName)
			say(string.format(gameforge.main_quest_lv90._1190_say, item_name(ITEM1_1), 1))
			say(string.format(gameforge.main_quest_lv90._1190_say, item_name(ITEM1_2), 1))
			say()
			say_reward(gameforge.main_quest_flame_lv101._120_sayReward)
		end

		when YACKHWAN.chat.gameforge.main_quest_flame_lv101._010_sendLetter begin 
			say_title(mob_name(YACKHWAN)) 
			say(gameforge.main_quest_flame_lv101._130_say) 
		end
		
		when NPC1_1.click begin
			say(gameforge.main_quest_flame_lv101._140_say)
			set_state (state1_3)  -- 단서 X, 부하 O
		end
		
		when kill with pc.get_map_index() >= DUNGEON_MAP_INDEX * 10000 and pc.get_map_index() < (DUNGEON_MAP_INDEX + 1) *10000 begin
			if number(1,100) == 1 then
				pc.give_item2(ITEM1_1, 1)
				set_state (state1_4)   -- 단서 O, 부하 X
			end
		end

		when leave begin 
			--q.done()
		end
	end
	state state1_3 begin  -- 단서 X, 부하 O
		when enter begin 
			-- notice 설정
			notice_multiline(gameforge.main_quest_flame_lv101._150_notice,notice)
		end

		when letter begin 
			send_letter(gameforge.main_quest_flame_lv101._010_sendLetter) 
			q.set_counter_name(gameforge.main_quest_lv97._670_counterName)
			q.set_counter_value(1)
 		end

		when button or info begin 
			say_title(gameforge.main_quest_flame_lv101._100_sayTitle) 
			say(gameforge.main_quest_flame_lv101._110_say) 
			
			say(gameforge.main_quest_lv97._670_counterName)
			say(string.format(gameforge.main_quest_lv90._1190_say, item_name(ITEM1_1), 1))
			say(string.format(gameforge.main_quest_lv90._1190_say, item_name(ITEM1_2), 0))
			say()
			say_reward(gameforge.main_quest_flame_lv101._120_sayReward)
		end

		when YACKHWAN.chat.gameforge.main_quest_flame_lv101._010_sendLetter begin 
			say_title(mob_name(YACKHWAN)) 
			say(gameforge.main_quest_flame_lv101._160_say) 
			wait()--추가 대화 

			say_title(mob_name(YACKHWAN)) 
			say(gameforge.main_quest_flame_lv101._180_say) 
		end
		
		when kill with pc.get_map_index() >= DUNGEON_MAP_INDEX * 10000 and pc.get_map_index() < (DUNGEON_MAP_INDEX + 1) *10000 begin
			if number(1,100) == 1 then
				pc.give_item2(ITEM1_1, 1)
				set_state ("state1_5")   -- 단서 O, 부하 O
			end
		end

		when leave begin 
			--q.done()
		end
	end
	state state1_4 begin  -- 단서 O, 부하 X
		when enter begin 
			-- notice 설정
			notice_multiline(gameforge.main_quest_flame_lv101._190_notice,notice)
		end

		when letter begin 
			send_letter(gameforge.main_quest_flame_lv101._010_sendLetter) 
			q.set_counter_name(gameforge.main_quest_lv97._670_counterName)
			q.set_counter_value(1)
 		end

		when button or info begin 
			say_title(gameforge.main_quest_flame_lv101._100_sayTitle) 
			say(gameforge.main_quest_flame_lv101._110_say) 
			
			say(gameforge.main_quest_lv97._670_counterName)
			say(string.format(gameforge.main_quest_lv90._1190_say, item_name(ITEM1_1), 0))
			say(string.format(gameforge.main_quest_lv90._1190_say, item_name(ITEM1_2), 1))
			say()
			say_reward(gameforge.main_quest_flame_lv101._120_sayReward)
		end

		when YACKHWAN.chat.gameforge.main_quest_flame_lv101._010_sendLetter begin 
			say_title(mob_name(YACKHWAN)) 
			say(gameforge.main_quest_flame_lv101._200_say) 
		end
		
		when NPC1_1.click begin
			say(gameforge.main_quest_flame_lv101._140_say)
			set_state (state1_5)
		end

		when leave begin 
			--q.done()
		end
	end
	state state1_5 begin 
		when enter begin 
			-- notice 설정
			notice_multiline(gameforge.main_quest_flame_lv101._210_notice,notice)
		end

		when letter begin 
			send_letter(gameforge.main_quest_flame_lv101._010_sendLetter) 
 			--npc 타겟 
			local v = find_npc_by_vnum(YACKHWAN) 
			if 0 != v then
				target.vid("__TARGET__", v, mob_name(YACKHWAN))
			end
		end

		when button or info begin 
			say_title(gameforge.main_quest_flame_lv101._010_sendLetter) 
			say(gameforge.main_quest_flame_lv101._220_say) 
			--npc 타겟 안내문
			--say_reward(locale.target_npc_info) 
		end

		when YACKHWAN.chat.gameforge.main_quest_flame_lv101._010_sendLetter begin 
			target.delete("__TARGET__") 
			--아이템 제거
			pc.remove_item(ITEM1_1, pc.count_item(ITEM1_1)) 
			say_title(mob_name(YACKHWAN)) 
			say(gameforge.main_quest_flame_lv101._230_say) 
			wait()--추가 대화 

			say_title(pc.getname()) 
			say(gameforge.main_quest_flame_lv101._250_say) 
			wait()--추가 대화 

			say_title(gameforge.main_quest_flame_lv101._260_sayTitle) 
			say(gameforge.main_quest_flame_lv101._270_say) 
			wait()--추가 대화 

			say_title(mob_name(YACKHWAN)) 
			say(gameforge.main_quest_flame_lv101._280_say) 
			--보상
			wait()--reward

			say_reward(string.format(gameforge.main_quest_lv60._190_sayReward, 37003000))
			pc.give_exp2(37003000)
			say_reward(string.format(gameforge.main_quest_lv60._200_sayReward, 153000))
			pc.change_money(153000)
			say_reward(string.format(gameforge.main_quest_lv60._210_sayReward, item_name(30047),2))
			pc.give_item2(30047, 2)
			say_reward(string.format(gameforge.main_quest_lv60._210_sayReward, item_name(27992),2)) -- 백진주
			pc.give_item2(27992, 1)
			pc.give_item2(27992, 1)
			say()
			--다음 스테이트로
			set_state (state2_1)
		end

		when leave begin 
			--q.done()
		end
	end
	
	-- 초마단의 명패 해독
	state state2_1 begin 
		when letter begin 
			send_letter(gameforge.main_quest_flame_lv101._290_sendLetter) 
 			--npc 타겟 
			local v = find_npc_by_vnum(SCHOLAR) 
			if 0 != v then
				target.vid("__TARGET__", v, mob_name(SCHOLAR))
			end
 		end

		when button or info begin 
			say_title(gameforge.main_quest_flame_lv101._290_sendLetter) 
			say(gameforge.main_quest_flame_lv101._300_say) 
			--npc 타겟 안내문
			--say_reward(locale.target_npc_info) 
		end

		when SCHOLAR.chat.gameforge.main_quest_flame_lv101._290_sendLetter begin 
			target.delete("__TARGET__") 
			say_title(mob_name(SCHOLAR)) 
			say(gameforge.main_quest_flame_lv101._310_say) 
			wait()--추가 대화 

			say_title(mob_name(SCHOLAR)) 
			say(gameforge.main_quest_flame_lv101._330_say) 
			wait()--추가 대화 

			say_title(pc.getname()) 
			say(gameforge.main_quest_flame_lv101._340_say) 
			wait()--추가 대화 

			say_title(mob_name(SCHOLAR)) 
			say(gameforge.main_quest_flame_lv101._350_say) 
			--다음 스테이트로
			set_state (state2_2)
		end

		when leave begin 
			--q.done()
		end
	end
	state state2_2 begin 
		when enter begin 
			-- notice 설정
			notice_multiline(gameforge.main_quest_flame_lv101._360_notice,notice)
		end

		when letter begin 
			send_letter(gameforge.main_quest_flame_lv101._290_sendLetter) 
 			--아이템 지급 설정
			q.set_counter_name(gameforge.main_quest_lv97._670_counterName)
			main_quest_flame_lv101.item_count() 
		end
		
		when button or info begin 
			say_title(gameforge.main_quest_flame_lv101._100_sayTitle) 
			say(gameforge.main_quest_flame_lv101._520_say) 
			--남은 아이템 안내문
			say(gameforge.main_quest_lv97._670_counterName)
			say(string.format(gameforge.main_quest_lv90._1190_say,item_name(ITEM2_1), 1 - pc.count_item(ITEM2_1)))
		end

		when SCHOLAR.chat.gameforge.main_quest_flame_lv101._290_sendLetter begin 
			say_title(mob_name(SCHOLAR)) 
			say(gameforge.main_quest_flame_lv101._370_say) 
		end

		when 2206.kill begin 
			if number(1,100) == 1 then
				pc.give_item2(ITEM2_1, 1)
				main_quest_flame_lv101.item_count() 
			end
		end

		function item_count()
			if pc.count_item(ITEM2_1) >= 1 then
				set_state ("state2_3")
			end
			q.set_counter_value(1 - pc.count_item(ITEM2_1))
		end

		when leave begin 
			--q.done()
		end
	end
	state state2_3 begin 
		when enter begin 
			-- notice 설정
			notice_multiline(gameforge.main_quest_flame_lv101._380_notice,notice)
		end

		when letter begin 
			send_letter(gameforge.main_quest_flame_lv101._290_sendLetter) 
 			--npc 타겟 
			local v = find_npc_by_vnum(SCHOLAR) 
			if 0 != v then
				target.vid("__TARGET__", v, mob_name(SCHOLAR))
			end
		end

		when button or info begin 
			say_title(gameforge.main_quest_flame_lv101._290_sendLetter) 
			say(gameforge.main_quest_flame_lv101._390_say) 
			--npc 타겟 안내문
			--say_reward(locale.target_npc_info) 
		end

		when SCHOLAR.chat.gameforge.main_quest_flame_lv101._290_sendLetter begin 
			target.delete("__TARGET__") 
			--아이템 제거
			pc.remove_item(ITEM2_1, pc.count_item(ITEM2_1)) 
			say_title(mob_name(SCHOLAR)) 
			say(gameforge.main_quest_flame_lv101._400_say) 
			--다음 스테이트로
			wait()--reward

			say_reward(string.format(gameforge.main_quest_lv60._190_sayReward, 48584000))
			pc.give_exp2( 48584000)
			say_reward(string.format(gameforge.main_quest_lv60._200_sayReward, 154000))
			pc.change_money(154000)
			say_reward(string.format(gameforge.main_quest_lv60._210_sayReward, item_name(72024),1))  -- 행운의 금화
			pc.give_item2_select(72024)
			item.set_socket(2, 180)
			say_reward(string.format(gameforge.main_quest_lv60._210_sayReward, item_name(71094),1)) -- 선인의 교훈
			pc.give_item2(71094, 1)
			say()
			set_state (state2_4)
		end

		when leave begin 
			--q.done()
		end
	end
	state state2_4 begin 
		when enter begin 
			--시간제한 설정
			pc.setqf("limit_time", get_time() + 60*60*24)
		end

		when login begin 
 			--시간제한 설정 
			local remain_time = pc.getqf("limit_time") - get_time()
			timer("time_over", remain_time)
		end
		
		when letter begin 
			send_letter(gameforge.main_quest_flame_lv101._290_sendLetter) 
 			--npc 타겟 
			local v = find_npc_by_vnum(SCHOLAR) 
			if 0 != v then
				target.vid("__TARGET__", v, mob_name(SCHOLAR))
			end
		end

		when button or info begin 
			say_title(gameforge.main_quest_flame_lv101._290_sendLetter) 
			say(gameforge.main_quest_flame_lv101._390_say) 
			--npc 타겟 안내문
			local remain_time = pc.getqf("limit_time") - get_time()
			local left_hour = math.mod(remain_time / (60 * 60), 24);
			local left_minute = math.mod(remain_time / 60, 60);
			say(string.format(gameforge.main_quest_flame_lv101._410_say, left_hour, left_minute))
			--say_reward(locale.target_npc_info) 
		end

		when SCHOLAR.chat.gameforge.main_quest_flame_lv101._290_sendLetter begin 
			say_title(mob_name(SCHOLAR)) 
			say(gameforge.main_quest_flame_lv101._420_say) 
		end

		when time_over.timer begin 
			set_state ("state3_1")
		end

		when leave begin 
			pc.setqf("limit_time", 0);
			--q.done()
		end
	end

	-- 명패의 진실
	state state3_1 begin 
		when letter begin 
			send_letter(gameforge.main_quest_flame_lv101._430_sendLetter) 
 			--npc 타겟 
			local v = find_npc_by_vnum(SCHOLAR) 
			if 0 != v then
				target.vid("__TARGET__", v, mob_name(SCHOLAR))
			end
		end

		when button or info begin 
			say_title(gameforge.main_quest_flame_lv101._430_sendLetter) 
			say(gameforge.main_quest_flame_lv101._300_say) 
			--npc 타겟 안내문
			--say_reward(locale.target_npc_info) 
		end

		when SCHOLAR.chat.gameforge.main_quest_flame_lv101._430_sendLetter begin 
			target.delete("__TARGET__") 
			say_title(mob_name(SCHOLAR)) 
			say(gameforge.main_quest_flame_lv101._440_say) 
			wait()--추가 대화 

			say_title(mob_name(SCHOLAR)) 
			say(gameforge.main_quest_flame_lv101._450_say) 
			wait()--추가 대화 

			say_title(pc.getname()) 
			say(gameforge.main_quest_flame_lv101._340_say) 
			wait()--추가 대화 

			set_state (state3_2)
		end

		when leave begin 
			--q.done()
		end
	end
	state state3_2 begin 
		when enter begin 
			-- notice 설정
			notice_multiline(gameforge.main_quest_flame_lv101._470_notice,notice)
		end

		when letter begin 
			send_letter(gameforge.main_quest_flame_lv101._430_sendLetter) 
 			--npc 타겟 
			local v = find_npc_by_vnum(YACKHWAN) 
			if 0 != v then
				target.vid("__TARGET__", v, mob_name(YACKHWAN))
			end
		end

		when button or info begin 
			say_title(gameforge.main_quest_flame_lv101._480_sayTitle) 
			say(gameforge.main_quest_flame_lv101._490_say) 
			--npc 타겟 안내문
			--say_reward(locale.target_npc_info) 
		end

		when YACKHWAN.chat.gameforge.main_quest_flame_lv101._430_sendLetter begin 
			target.delete("__TARGET__") 
			say_title(mob_name(YACKHWAN)) 
			say(gameforge.main_quest_flame_lv101._500_say) 
			wait()--추가 대화 

			say_title(mob_name(YACKHWAN)) 
			say(gameforge.main_quest_flame_lv101._510_say) 
			wait()--추가 대화 
			say_title(gameforge.main_quest_lv27._120_sayTitle)
			say_reward(string.format(gameforge.main_quest_lv60._190_sayReward, 48584000))
			pc.give_exp2( 48584000)
			say_reward(string.format(gameforge.main_quest_lv60._200_sayReward, 156000))
			pc.change_money( 156000)
			say_reward(string.format(gameforge.main_quest_lv60._210_sayReward, item_name(71095),1)) -- 통행증명서
			pc.give_item2(71095, 1)
			say_reward(string.format(gameforge.main_quest_lv60._210_sayReward, item_name(72016),1))  -- 제3의 손
			pc.give_item2_select(72016)
			item.set_socket(2, 60)
			say()
			say_title(string.format(gameforge.main_quest_flame_lv99._430_say, 102))
			--다음 스테이트로
			set_state (__COMPLETE__)
			set_quest_state ("main_quest_flame_lv102", "state0")
		end

		when leave begin 
			--q.done()
		end
	end
	
	state __COMPLETE__ begin
		when enter begin
			--q.done()
		end
	end
	
end