----------------------------------------------------
--COLLECT QUEST_lv92
--METIN2 수집 퀘스트  
----------------------------------------------------
quest collect_quest_lv92 begin
	state start begin
	end

	state run begin
		when login or levelup with pc.level >= 92 begin
			set_state(information)
		end	
	end

	state information begin
		when letter begin
			local v = find_npc_by_vnum(20091)
			if v != 0 then
				target.vid("__TARGET__", v, gameforge.collect_quest_lv92._010_target)
			end
			send_letter(gameforge.collect_quest_lv92._020_send_letter)
		end

		when button or info begin
			say_title(gameforge.collect_quest_lv92._020_send_letter)
			say(gameforge.collect_quest_lv92._030_say)
		end
		
		when __TARGET__.target.click or 20091.chat.gameforge.collect_quest_lv92._040_npcChat begin
			target.delete("__TARGET__")
			---                                                   l
			say_title(gameforge.collect_quest_lv92._050_say_title)
			say(gameforge.collect_quest_lv92._060_say)
			wait()
			say_title(gameforge.collect_quest_lv92._050_say_title)
			say(gameforge.collect_quest_lv92._070_say)
			set_state(go_to_disciple)
			pc.setqf("duration", 0)  
			pc.setqf("collect_count", 0) 
			pc.setqf("drink_drug", 0) 
		end
	end

	state go_to_disciple begin
		when letter begin
			send_letter(gameforge.collect_quest_lv92._020_send_letter)
		end

		when button or info begin
			say_title(gameforge.collect_quest_lv92._080_say_title)
			---                                                   l
			say(gameforge.collect_quest_lv92._090_say)
			say_item_vnum(30251) 
			say_reward(string.format(gameforge.collect_quest_lv92._100_sayReward, pc.getqf("collect_count")))
		end
		
		when 71035.use begin --현혹의 물약
			if get_time() < pc.getqf("duration") then
				say(gameforge.collect_quest_lv92._110_say)
				return
			end

			if pc.getqf("drink_drug") == 1 then
				say(gameforge.collect_quest_lv92._120_say)
				return
			end

			if pc.count_item(30251) == 0 then
				say(gameforge.collect_quest_lv92._130_say)
				return
			end

			pc.remove_item(71035, 1)
			pc.setqf("drink_drug",1)
		end

		when 1135.kill or 1137.kill begin
			local s = number(1, 200)
			if s <= 1 then
				pc.give_item2(30251, 1)
				send_letter(gameforge.collect_quest_lv92._140_send_letter)
			end	
		end
		
    	when 20091.chat.gameforge.collect_quest_lv92._145_npcChat with pc.count_item(30251) > 0 begin
			if get_time() < pc.getqf("duration") then
		  		say_title(gameforge.collect_quest_lv92._050_say_title)
				say(gameforge.collect_quest_lv92._150_say)
				return
			end

			if pc.count_item(30251) <= 0 then
				say_title(gameforge.collect_quest_lv92._050_say_title)
				say(gameforge.collect_quest_lv92._160_say)
				return
			end

			say_title(gameforge.collect_quest_lv92._050_say_title)
			say(gameforge.collect_quest_lv92._170_say)
			pc.remove_item(30251, 1)

			if is_test_server() then 
				pc.setqf("duration", get_time()+10) -- 테스트서버 10초
			else
				pc.setqf("duration", get_time()+60*60*6) -- 일반 6시간
			end

			wait()

			local pass_percent

			if pc.getqf("drink_drug") == 0 then
				pass_percent = 10
			else		
				pass_percent = 60
			end

			local s = number(1, 100)

			if s <= pass_percent then
				local collect_count = pc.getqf("collect_count")
				pc.setqf("collect_count", collect_count + 1) -- 하나 구했으니 +1 해준다
				pc.setqf("drink_drug", 0) -- 물약 초기화

				if collect_count < 9 then
					say_title(gameforge.collect_quest_lv92._050_say_title)
					say(gameforge.collect_quest_lv92._180_say)
					say(string.format(gameforge.collect_quest_lv92._190_say, 10-pc.getqf("collect_count")))
					say(gameforge.collect_quest_lv92._200_say)
				else
					pc.setqf("duration", 0) 

					say_title(gameforge.collect_quest_lv92._050_say_title)
					say(gameforge.collect_quest_lv92._210_say)

					local s = select(gameforge.collect_quest_lv92._220_1_select, gameforge.collect_quest_lv92._220_2_select, gameforge.collect_quest_lv92._220_3_select)

					if 1 == s then
						affect.add_collect(1,1000,60*60*24*365*60) -- hp index == 1, hp +1000
						pc.setqf("reward", 1)
					elseif 2 == s then
						affect.add_collect(apply.DEF_GRADE_BONUS, 120, 60*60*24*365*60)
						pc.setqf("reward", 2)
					elseif 3 == s then 
						affect.add_collect(apply.ATT_GRADE_BONUS, 51, 60*60*24*365*60)
						pc.setqf("reward", 3)
					end

					clear_letter()
					set_quest_state("collect_quest_lv94", "run")
					set_state(__complete)
				end

				return
			else								
				pc.setqf("drink_drug", 0)	 --물약 초기화
				say_title(gameforge.collect_quest_lv92._050_say_title)
				say(gameforge.collect_quest_lv92._230_say)
				return
			end
	   	end -- end of when 20091.chat."홍천의 보석을 가져온건가? " with pc.count_item(30251) > 0 begin
    end -- end of state 'go_to_disciple'
	
	state __complete begin
	end
end
