quest training_grandmaster_skill begin
    state start begin
		when 50513.use begin
			if pc.get_skill_group() == 0 then
				say_title(gameforge.training_grandmaster_skill._10_sayTitle)
				say(gameforge.training_grandmaster_skill._20_say)
				return
			end

			if get_time() < pc.getqf("next_time") then
				if pc.is_skill_book_no_delay() then
					say_title(gameforge.training_grandmaster_skill._10_sayTitle)
					say(gameforge.training_grandmaster_skill._30_say)
					wait()
				else
					say_title(gameforge.training_grandmaster_skill._10_sayTitle)
					say(gameforge.training_grandmaster_skill._40_say)
					return
				end
			end

			local result = training_grandmaster_skill.BuildGrandMasterSkillList(pc.get_job(), pc.get_skill_group())

			local vnum_list = result[1]
			local name_list = result[2]

			if table.getn(vnum_list) == 0 then
				say_title(gameforge.training_grandmaster_skill._10_sayTitle)
				say(gameforge.training_grandmaster_skill._50_say)
				return
			end
			say_title(gameforge.training_grandmaster_skill._10_sayTitle)
			say(gameforge.training_grandmaster_skill._60_say)

			local menu_list = {}
			table.foreach(name_list, function(i, name) table.insert(menu_list, name) end)
			table.insert(menu_list, gameforge.locale.cancel) 

			local s=select_table(menu_list)
			
			if table.getn(menu_list) == s then
				return
			end

			local skill_name=name_list[s]
			local skill_vnum=vnum_list[s]
			local skill_level = pc.get_skill_level(skill_vnum)
			local cur_alignment = pc.get_real_alignment()
			local need_alignment = 1000+500*(skill_level-30)

			test_chat(string.format(gameforge.training_grandmaster_skill._70_chat, cur_alignment..gameforge.training_grandmaster_skill._75_chat..need_alignment))

			local title=string.format(gameforge.training_grandmaster_skill._80_stringFormat, skill_name, skill_level-30+1)

			say_title(gameforge.training_grandmaster_skill._10_sayTitle)
			say(gameforge.training_grandmaster_skill._90_say)

			if cur_alignment<-19000+need_alignment then
				say_reward(gameforge.training_grandmaster_skill._100_sayReward)
								return
			end

			if cur_alignment<0 then
say_reward(string.format(gameforge.training_grandmaster_skill._110_sayReward, need_alignment, need_alignment*2))
				say_reward(gameforge.training_grandmaster_skill._120_sayReward)
				need_alignment=need_alignment*2
			elseif cur_alignment<need_alignment then
say_reward(string.format(gameforge.training_grandmaster_skill._130_sayReward, need_alignment))
				say_reward(gameforge.training_grandmaster_skill._140_sayReward)
			else
say_reward(string.format(gameforge.training_grandmaster_skill._130_sayReward, need_alignment))
			end
				
			local s= select(gameforge.locale.levelup.prev_quest_go, gameforge.locale.cancel)	
			
			if s==2 then
				return
			end


			if cur_alignment>=0 and cur_alignment<need_alignment then
say_title(title)
				say_reward(gameforge.training_grandmaster_skill._150_sayReward)
				say(gameforge.training_grandmaster_skill._160_say)
				say_reward(gameforge.training_grandmaster_skill._170_sayReward)
				say(gameforge.training_grandmaster_skill._180_say)
				local s=input()
				s = string.gsub(s, "(%a*)%s*", "%1")
				s = string.lower(string.gsub(s, "(%a*)%s*", "%1"))

				local t = string.gsub(gameforge.training_grandmaster_skill._10_answer, "(%a*)%s*", "%1")
				t = string.lower(string.gsub(gameforge.training_grandmaster_skill._10_answer, "(%a*)%s*", "%1"))
				
				if s!=t then
					return
				end
			end

			if get_time() < pc.getqf("next_time") then
				if pc.is_skill_book_no_delay() then
					pc.remove_skill_book_no_delay()
				else
					say_title(gameforge.training_grandmaster_skill._10_sayTitle)
					return
				end
			end

			pc.setqf("next_time", get_time()+time_hour_to_sec(number(8, 12)))


			if need_alignment>0 then
				if pc.count_item(50513) > 0 then
					if pc.learn_grand_master_skill(skill_vnum) then
						pc.change_alignment(-need_alignment)
				
						say_title(title)
						say_reward(gameforge.training_grandmaster_skill._190_sayReward)

						if 40 == pc.get_skill_level(skill_vnum) then
							say(gameforge.training_grandmaster_skill._200_say)
							say(string.format(gameforge.training_grandmaster_skill._210_say, skill_name))
							say(gameforge.training_grandmaster_skill._220_say)
						else
							say(gameforge.training_grandmaster_skill._230_say)
							say(string.format(gameforge.training_grandmaster_skill._240_say, skill_name, skill_level-30+1+1))
						end
						say_reward(gameforge.training_grandmaster_skill._250_sayReward)
						say_reward(string.format(gameforge.training_grandmaster_skill._260_sayReward, need_alignment))
					else
						say_title(title)
						say_reward(gameforge.training_grandmaster_skill._270_sayReward)
						say(gameforge.training_grandmaster_skill._280_say)
						say_reward(gameforge.training_grandmaster_skill._290_sayReward)
						pc.change_alignment(-number(need_alignment/3, need_alignment/2))
					end
					pc.remove_item(50513)
				else
					char_log(0, "HACK 50513", pc.getname())
				end
			end
		end

		function BuildGrandMasterSkillList(job, group)
			GRAND_MASTER_SKILL_LEVEL = 30
			PERFECT_MASTER_SKILL_LEVEL = 40

			local skill_list = special.active_skill_list[job+1][group]
			local ret_vnum_list = {}
			local ret_name_list = {}

			table.foreach(skill_list, 
			function(i, skill_vnum) 
			local skill_level = pc.get_skill_level(skill_vnum)

			if skill_level >= GRAND_MASTER_SKILL_LEVEL and skill_level < PERFECT_MASTER_SKILL_LEVEL then
				table.insert(ret_vnum_list, skill_vnum)
				local name=locale.GM_SKILL_NAME_DICT[skill_vnum]
				
				if name == nil then name=skill_vnum end
					table.insert(ret_name_list, name)
				end
			end)
			return {ret_vnum_list, ret_name_list}
		end
    end
end
