quest new_quest_lv52 begin   
    state start begin
		when login or levelup with pc.get_level() >= 52 begin -- Quest for level 52 and higher
			set_state(letterfromboss)
			pc.setqf("ghostsleft", 4)
		end
	end
	
	-- starts with a quest-letter send to the player
	state letterfromboss begin
		when letter begin
			-- this is the letter
			send_letter(gameforge.new_quest_lv52._010_send_letter)
		end
		when button or info begin
			-- finding out which map (map1) the player has to go to meet his or her first contact
			local map_lookup = { "Yongan", "Joan", "Pyongmoo" }
			local mapname = map_lookup[pc.get_empire()]
			say_title(gameforge.new_quest_lv52._020_say_title)
			say_reward(gameforge.new_quest_lv52._021_say_reward)
			say(string.format(gameforge.new_quest_lv52._022_say, mapname)) -- where Baek-Go can be found. See if-then-else argument above.
			set_state(askbeakgo)			
		end
		
		when enter begin
			if pc.getqf("ghostsleft") == 0 then
				pc.setqf("ghostsleft", 4)
			end
		end
	end
	
	-- This state if for the conversation with Baek-Go which the player is told to go in the letter above. 
	state askbeakgo begin
		-- This Function is for finding out if the player is in the right map so we can place the questarrow right.
		when login or enter begin
			send_letter(gameforge.new_quest_lv52._030_set_title)
			q.start() -- creating a journal entery int the quest-journal
			q.set_title(gameforge.new_quest_lv52._030_set_title) -- setting the title of the new quest-journal
			if is_destination_village(1) then -- 
				local position = find_npc_by_vnum(20018) -- Locate Baek-Go
				if position != 0 then
					target.vid("__TARGET__", position, gameforge.new_quest_lv52._035_target) -- place questarrow on Baek-Go
				end
			end
		end
		when button or info begin -- this event is triggered when the quest-journal is clicked. A conversation window will open.
			say_title(gameforge.new_quest_lv52._030_set_title)
			say(gameforge.new_quest_lv52._040_say)
			say("")
			say(string.format(gameforge.new_quest_lv52._043_say, get_map_name_by_number(1)))
		end
				
		when 20018.chat.gameforge.new_quest_lv52._030_set_title begin -- when Baek-Go is clicked this event is triggered
			target.delete("__TARGET__")
			say_title(gameforge.new_quest_lv52._044_say_title)
			say(gameforge.new_quest_lv52._041_say)
			q.done()
			local doquest = select(gameforge.new_quest_lv52._042_select, gameforge.subquest_12._50_select)
			if doquest ==1 then 
				set_state(askyuhwan)
			else
				say_title(gameforge.new_quest_lv52._044_say_title)
				say(gameforge.new_quest_lv52._046_say)
				return
			end
		end
		when 20355.chat.gameforge.new_quest_lv52._030_set_title begin -- If the player should click the Boss he or she will get a reaction from him even though it is not needed for the quest.
			say_title(gameforge.new_quest_lv52._054_say_title)
			say(gameforge.new_quest_lv52._042_say)
			local doquest = select(gameforge.new_quest_lv52._055_select, gameforge.subquest_12._50_select, gameforge.new_quest_lv75._290_2_select)
			if doquest ==1 then 
				say_title(gameforge.new_quest_lv52._054_say_title)
				say(gameforge.new_quest_lv52._056_say)
			elseif doquest == 3 then
				say(gameforge.subquest_06._60_say)
				local abortquest = select(gameforge.subquest_06._70_select, gameforge.subquest_06._80_select)
				if abortquest == 1 then
					pc.setqf("ghostsleft", 0)
					target.delete("__TARGET__")
					set_state(__GIVEUP__)
				else
					say_title(gameforge.new_quest_lv52._054_say_title)
					say(gameforge.subquest_11._80_say)
				end
				return
			else
				say_title(gameforge.new_quest_lv52._054_say_title)
				say(gameforge.new_quest_lv52._85_say)
			end
		end
	end
	
	-- This state is for the second npc to talk to - Yu-Hwan. The player is send to him by Baek-Go in the state above.
	state askyuhwan begin
		
        when login or enter begin
			send_letter(gameforge.new_quest_lv52._030_set_title)
			q.start() -- starting a new quest-journal since the player may logout or changing maps during this quest
			q.set_title(gameforge.new_quest_lv52._030_set_title) -- this is the tilte of the quest-journal
			if is_destination_village(2) then 
				local position = find_npc_by_vnum(20017) -- locating Yu-Hwan in map2...
				if position != 0 then
					target.vid("__TARGET2__", position, gameforge.new_quest_lv52._050_target) -- ...and placing a questarrow on him
				end
			end
		end
		when button or info begin -- this event is triggered when the player looks into the quest-journal to find out what to do
			local mapname

			-- the following text will appear in the conversation window when clicking the quest-journal of this quest
			say_title(gameforge.new_quest_lv52._030_set_title)
			say(gameforge.new_quest_lv52._051_say)
			say("")
			say(string.format(gameforge.new_quest_lv52._045_say, get_map_name_by_number(3)))
		end
		when 20017.chat.gameforge.new_quest_lv52._030_set_title begin -- event triggered when Yu-Hwan is clicked. A conversation window will appear.
			target.delete("__TARGET2__")
			say_title(gameforge.new_quest_lv52._049_say_title)
			say(gameforge.new_quest_lv52._052_say)
			say(gameforge.new_quest_lv52._053_say)
			q.done()
			set_state(gotoboss)
		end
		when 20355.chat.gameforge.new_quest_lv52._030_set_title begin -- It is not neccessary for the quest but the player may get the idea to talk to the Boss who is the quest-giver and will get a reaction from him.
			say_title(gameforge.new_quest_lv52._054_say_title)
			say_reward(gameforge.new_quest_lv52._055_say_reward)
			wait()
			say_pc_name()
			say_reward(gameforge.new_quest_lv52._056_say_reward)
		end
	end
	
	-- After talking to Yu-Hwan the player is send to the Boss and to this state. 
	state gotoboss begin
		-- again the player has to switch maps. To figure out if it is the right one we use this function again.
		when login or enter begin
			send_letter(gameforge.new_quest_lv52._030_set_title)
			q.start() -- we need a new quest-journal entery since the player may logout or change maps
			q.set_title(gameforge.new_quest_lv52._030_set_title)
			if is_destination_village(1) then -- 
				local position = find_npc_by_vnum(20355) -- locate the Boss in map1
				if position != 0 then
					target.vid("__TARGET3__", position, gameforge.new_quest_lv52._060_target) -- place questarrow on Boss
				end
			end
		end
		when button or info begin -- this event is triggered when the quest-journal is clicked - it will open a conversation window
            local map_lookup = { "Yongan", "Joan", "Pyongmoo" }
			local mapname = map_lookup[pc.get_empire()]
			say_title(gameforge.new_quest_lv52._030_set_title)
			say(string.format(gameforge.new_quest_lv52._061_say, mapname))
		end
		when 20355.chat.gameforge.new_quest_lv52._030_set_title begin -- when the boss is clicked a conversation starts
			target.delete("__TARGET3__")
			local MorF = pc.get_sex() -- here we find out the characters sex
			say_pc_name()
			say(gameforge.new_quest_lv52._062_say_reward )
			wait()
			say_title(gameforge.new_quest_lv52._054_say_title)
			if MorF == 0 then 
				say(gameforge.new_quest_lv52._063_say_reward) -- say this if character is male
			else
				say(gameforge.new_quest_lv52._064_say_reward) -- say this if character is female
			end
			say(gameforge.new_quest_lv52._065_say_reward)
			say(gameforge.new_quest_lv52._066_say_reward)
			wait()
			say_pc_name()
			say(gameforge.new_quest_lv52._067_say_reward)
			wait()
			say_title(gameforge.new_quest_lv52._054_say_title)
			say(gameforge.new_quest_lv52._068_say_reward)
			say(gameforge.new_quest_lv52._069_say_reward)
			wait()
			say_title(gameforge.new_quest_lv52._054_say_title)
			say(gameforge.new_quest_lv52._070_say_reward)
			wait()
			say_pc_name()
			say(gameforge.new_quest_lv52._071_say_reward)
			local answer = select(gameforge.new_quest_lv52._080_1_select, gameforge.new_quest_lv52._080_2_select, gameforge.new_quest_lv52._080_3_select) -- multiple choice answer
			if answer == 1 then -- positive answer: Doing it for the village
				say_pc_name()
				say(gameforge.new_quest_lv52._081_say_reward)
			elseif answer == 2 then -- positive answer: proving oneself
				say_pc_name()
				say(gameforge.new_quest_lv52._082_say_reward)
			elseif answer == 3 then -- negative answer
				say_pc_name()
				say(gameforge.new_quest_lv52._083_say_reward)
				wait()
				say_title(gameforge.new_quest_lv52._054_say_title)
				say(gameforge.new_quest_lv52._084_say_reward)
				wait()
				say_pc_name()
				local answertwo = select(gameforge.new_quest_lv52._090_1_select, gameforge.new_quest_lv52._090_2_select)
				if answertwo == 1 then -- think twice positive answer
					say_pc_name()
					say(gameforge.new_quest_lv52._091_say_reward)
				elseif answertwo == 2 then -- don't wanne do this quest answer 
					say_pc_name()
					say(gameforge.new_quest_lv52._092_say_reward )
					wait()
					say_title(gameforge.new_quest_lv52._054_say_title)
					say(gameforge.new_quest_lv52._093_say_reward) -- finishing quest
					say(string.format(gameforge.new_quest_lv52._093_say_reward_exp, 20000))
					say(string.format(gameforge.new_quest_lv52._093_say_reward_gold, 25000))
					pc.change_money(25000)
					pc.give_exp2(20000)
					q.done()
					pc.setqf("ghostsleft", 0)
					clear_letter()
					set_state(__GIVEUP__)
					return
				else
					notice_multiline(gameforge.new_quest_lv52._100_notice, notice)
				end
			end
			wait() -- continue positive conversation
			say_title(gameforge.new_quest_lv52._054_say_title)
			say(gameforge.new_quest_lv52._110_say_reward)
			say(gameforge.new_quest_lv52._111_say_reward)
			q.done()
			set_state(revenge)
		end
	end
	
	-- in this stae the fighting is done
	state revenge begin
	
		when enter begin
			if pc.getqf("ghostsleft") == 0 then
				pc.setqf("ghostsleft", 4)
			end
		end
	
		when login or enter begin
			new_quest_lv52_mob1 = 706
			new_quest_lv52_mob2 = 707
			new_quest_lv52_mob3 = 756
			new_quest_lv52_mob4 = 757
			new_quest_lv52_successMulti = 2
			new_quest_lv52_neededAmount = 4
			
			letterindex = new_quest_lv52_neededAmount - pc.getqf("ghostsleft")
			if letterindex == 0 then
				send_letter(gameforge.new_quest_lv52._030_set_title)
			elseif letterindex == 1 then 
				send_letter(gameforge.new_quest_lv52._170_send_letter)
			elseif letterindex == 2 then
				send_letter(gameforge.new_quest_lv52._171_send_letter)
			elseif letterindex == 3 then
				send_letter(gameforge.new_quest_lv52._172_send_letter)
			else
				send_letter(gameforge.new_quest_lv52._173_send_letter)
			end
			q.start()
			q.set_title(gameforge.new_quest_lv52._030_set_title)
		end
		when button or info begin
			local sayMob1Name = mob_name(706)
			local sayMob2Name = mob_name(707)
			local sayMob3Name = mob_name(756)
			local sayMob4Name = mob_name(757)
			say_title(gameforge.new_quest_lv52._120_set_title)
			
			letterindex = new_quest_lv52_neededAmount - pc.getqf("ghostsleft")
			-- these ate the answers which will appear in the quest-journal after every successful kill
			if letterindex == 1 then 
				say_reward(gameforge.new_quest_lv52._130_say_reward)
				say(gameforge.new_quest_lv52._131_say)
				say(gameforge.new_quest_lv52._132_say)
			elseif letterindex == 2 then
				say_reward(gameforge.new_quest_lv52._140_say_reward)
				say(gameforge.new_quest_lv52._141_say)
				say(gameforge.new_quest_lv52._132_say)
			elseif letterindex == 3 then
				say_reward(gameforge.new_quest_lv52._150_say_reward)
				say(gameforge.new_quest_lv52._151_say)
				say(gameforge.new_quest_lv52._132_say)
			elseif letterindex == 4 then

			else
				-- this quest-journal entery is shown befor the first mob is killed
				say(gameforge.new_quest_lv52._160_say)
				say(gameforge.new_quest_lv52._161_say)
				say("")
				say(string.format(gameforge.new_quest_lv52._162_say,get_map_name_by_number(64)))
			end
		end	
		when 706.kill or 707.kill or 756.kill or 757.kill begin -- Mob1 - Mob4
			local minimum_y = 671 -- this is the minumum Y value
			local value_y = pc.get_local_y() - minimum_y
			-- this if-then-else construct controls if the the killed mob can be counted as a valid kill
			if pc.get_local_y() >= 671 and pc.get_local_y() <= 906 then 
				if pc.get_local_x() >= 603 and pc.get_local_x() < 778 then -- handles west side of the tower
					local minimum_x = 603
					local value_x = value_y*35/47 -- calculation of slope triangle
					if pc.get_local_x() >= value_x + minimum_x then 
						if pc.getqf("ghostsleft") > 0 then
							local successProb = pc.getqf("ghostsleft")*new_quest_lv52_successMulti
							if math.random(1,100) < successProb then 
								local index = pc.getqf("ghostsleft") -1
								pc.setqf("ghostsleft", index) -- one ghost less to be freed
								if index == 3 then 
									letterindex = 1
									send_letter(gameforge.new_quest_lv52._170_send_letter)
									q.set_title(gameforge.new_quest_lv52._030_set_title)
								elseif index == 2 then
									letterindex = 2
									send_letter(gameforge.new_quest_lv52._171_send_letter)
									q.set_title(gameforge.new_quest_lv52._030_set_title)
								elseif index == 1 then
									letterindex = 3
									send_letter(gameforge.new_quest_lv52._172_send_letter)
									q.set_title(gameforge.new_quest_lv52._030_set_title)
								elseif index == 0 then
									if pc.get_job() == 0 then -- class sensitive reward
										if pc.get_skill_group() == 1 then
											pc.setqf("lv52reward", 50403)
										elseif pc.get_skill_group() == 2 then
											pc.setqf("lv52reward", number(50416, 50420))
										else 
											pc.setqf("lv52reward", 50300)
										end
									elseif pc.get_job() == 1 then
										if pc.get_skill_group() == 1 then
											pc.setqf("lv52reward", number(50431, 50435))
										elseif pc.get_skill_group() == 2 then
											pc.setqf("lv52reward", 50449)
										else 
											pc.setqf("lv52reward", 50301)
										end
									elseif pc.get_job() == 2 then
										if pc.get_skill_group() == 1 then
											pc.setqf("lv52reward", 50464)
										elseif pc.get_skill_group() == 2 then
											pc.setqf("lv52reward", number(50476, 50481))
										else 
											pc.setqf("lv52reward", 50302)
										end
									elseif pc.get_job() == 3 then
										if pc.get_skill_group() == 1 then
											pc.setqf("lv52reward", number(50491, 50496))
										elseif pc.get_skill_group() == 2 then
											pc.setqf("lv52reward", 50509)
										else 
											pc.setqf("lv52reward", 50303)
										end
									else
										pc.setqf("lv52reward", 1)
									end
									q.done()
									set_state(backhome)
								else
									notice("There is something wrong with the questflag!") -- to be deleted after testing
								end
							else
		--						notice("Pech gehabt, nix passiert!") -- to be deleted after testing
							end
						else
--							notice("You already freed all the ghosts!") -- to be deleted after testing
						end
					else 
--						notice("This one does not count!") -- to be deleted after testing
					end
				elseif pc.get_local_x() >= 778 and pc.get_local_x() <= 906 then -- handles east side of the tower
					local maximum_x = 906
					local value_x = value_y*128/235 -- calculation of slope triangle
					if pc.get_local_x() <= maximum_x - value_x then
--						notice("This one may count!") -- to be deleted after testing
						if pc.getqf("ghostsleft") > 0 then
							local successProb = pc.getqf("ghostsleft")*new_quest_lv52_successMulti
							if math.random(1,100) < successProb then 
								local index = pc.getqf("ghostsleft") -1
								pc.setqf("ghostsleft", index) -- one ghost less to be freed
								if index == 3 then 
									letterindex = 1
									send_letter(gameforge.new_quest_lv52._170_send_letter)
									q.set_title(gameforge.new_quest_lv52._030_set_title)
								elseif index == 2 then
									letterindex = 2
									send_letter(gameforge.new_quest_lv52._171_send_letter)
									q.set_title(gameforge.new_quest_lv52._030_set_title)
								elseif index == 1 then
									letterindex = 3
									send_letter(gameforge.new_quest_lv52._172_send_letter)
									q.set_title(gameforge.new_quest_lv52._030_set_title)
								elseif index == 0 then
									if pc.get_job() == 0 then -- class sensitive reward
										if pc.get_skill_group() == 1 then
											pc.setqf("lv52reward", number(50401, 50405))
										elseif pc.get_skill_group() == 2 then
											pc.setqf("lv52reward", 50418)
										else 
											pc.setqf("lv52reward", 70037)
										end
									elseif pc.get_job() == 1 then
										if pc.get_skill_group() == 1 then
											pc.setqf("lv52reward", 50434)
										elseif pc.get_skill_group() == 2 then
											pc.setqf("lv52reward", number(50446, 50450))
										else 
											pc.setqf("lv52reward", 70037)
										end
									elseif pc.get_job() == 2 then
										if pc.get_skill_group() == 1 then
											pc.setqf("lv52reward", number(50461, 50466))
										elseif pc.get_skill_group() == 2 then
											pc.setqf("lv52reward", 50479)
										else 
											pc.setqf("lv52reward", 70037)
										end
									elseif pc.get_job() == 3 then
										if pc.get_skill_group() == 1 then
											pc.setqf("lv52reward", 50495)
										elseif pc.get_skill_group() == 2 then
											pc.setqf("lv52reward", number(50506, 50511))
										else 
											pc.setqf("lv52reward", 70037)
										end
									else
										pc.setqf("lv52reward", 1)
									end
									q.done()
									set_state(backhome)
								else
	--								notice("There is something wrong with the questflag!") -- to be deleted after testing
								end
							else
--								notice("Pech gehabt, nix passiert!") -- to be deleted after testing
							end
						else
--							notice("You already freed all the ghosts!") -- to be deleted after testing
						end
					else 
--						notice("This one does not count!") -- to be deleted after testing
					end
				end
			end
		end
	end
	
	-- when the killing is done we go back to the boss
	state backhome begin
		when login or enter begin 
			send_letter(gameforge.new_quest_lv52._030_set_title)
			q.start()
			q.set_title(gameforge.new_quest_lv52._030_set_title)
			if is_destination_village(1) then -- 
				local position = find_npc_by_vnum(20355) -- Boss
				if position != 0 then
					target.vid("__TARGET4__", position, gameforge.new_quest_lv52._060_target) -- place questarrow on Boss
				end
			end
		end
		when button or info begin -- here the last letter from the boss is handled
			say_title(gameforge.new_quest_lv52._010_send_letter)
			say_reward(gameforge.new_quest_lv52._180_say_reward)
			local reward = pc.getqf("lv52reward")
			say(string.format(gameforge.new_quest_lv52._181_say, item_name(reward)))
			 --EDIT Arne 24Sept09, Reason: Did not fit in UI
			say(string.format(gameforge.new_quest_lv52._182_say, get_map_name_by_number(1)))
		end
		when 20355.chat.gameforge.new_quest_lv52._030_set_title begin -- when the boss is clicked
			target.delete("__TARGET4__")
			say_title(gameforge.new_quest_lv52._054_say_title)
			local MorF = pc.get_sex() -- is the character male of female
			local isvalid = pc.getqf("ghostsleft")
			if isvalid == 0 then 
				local reward = pc.getqf("lv52reward")
				say(string.format(gameforge.new_quest_lv52._183_say_reward, item_name(reward)))
				say(gameforge.new_quest_lv52._184_say_reward)
				if MorF == 0 then
					say(gameforge.new_quest_lv52._185_say_reward) -- if character is male
				else
					say(gameforge.new_quest_lv52._186_say_reward) -- if character is female
				end
				pc.change_money(270000)
				pc.give_exp2(3300000)
				pc.give_item2(reward)
				pc.setqf("lv52reward", 0)
			else
				notice("Questflag got manipulated! You shouldn't be here!!!") -- if the player cheated
			end
			q.done()
			clear_letter()
			set_state(__COMPLETE__)
		end
	end
	
	------  give up ------------------------------------------------------------------------------------------------------
	state __GIVEUP__ begin
	end
	------  complete ------------------------------------------------------------------------------------------------------
	state __COMPLETE__ begin
	end
end
