quest new_quest_lv29 begin
    state start begin
		when login or levelup with pc.get_level() >= 29 begin
			pc.setqf("hayIndex", 6)
			pc.setqf("hayFromHarang", 1)
			pc.setqf("hayFromTaurean", 1)
			pc.setqf("hayFromStablemaster", 1)
			pc.setqf("yonahspatience", 2)
			pc.setqf("yonahHasHay", 0)
			pc.setqf("dropProb", 80)
			set_state(gotoyonah)
		end
	end
	
	-- Yonah is the entery point for this quest. He is highlited with a questarrow
	state gotoyonah begin
		when enter or login begin
		local goto_npc = 20005 -- Yonah
		local v=find_npc_by_vnum(goto_npc)
			if 0!=v then
				target.vid("__TARGET__", v, gameforge.new_quest_lv29._010_target) -- place questarrow
			end
		end

		when 20005.chat.gameforge.new_quest_lv29._015_npcChat begin --EDIT Arne, 22Sept09, Reason: adapting all quest steps to choices
			target.delete("__TARGET__") -- delete questarrow
			say_title(gameforge.new_quest_lv29._020_say_title) -- displays headline of the conversation with Yonah
			say(gameforge.new_quest_lv29._021_say)
			say(gameforge.new_quest_lv29._022_say)
			local goto_npc = 20005 -- Yonah
			local accept = select(gameforge.new_quest_lv29._023_1_select,gameforge.new_quest_lv29._023_2_select)
			if accept == 1 then
				local v=find_npc_by_vnum(20014) -- find Taurean
				if 0!=v then
					target.vid("__TARGET2__", v, gameforge.new_quest_lv29._030_target) -- place questarrow
				end
				set_state(find_taurean)
			else
				say(gameforge.new_quest_lv29._024_say)
				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("hayIndex", 0)
					pc.setqf("hayFromHarang", 0)
					pc.setqf("hayFromTaurean", 0)
					pc.setqf("hayFromStablemaster",0)
					pc.setqf("yonahspatience", 0)
					pc.setqf("yonahHasHay", 0)
					pc.setqf("dropProb", 0)
					set_state(__GIVEUP__)
				else
					local v=find_npc_by_vnum(goto_npc)
					if 0!=v then
						target.vid("__TARGET__", v, gameforge.new_quest_lv29._010_target) -- place questarrow
					end
					return
				end
			end
		end
	end
	
	-- this state handles the conversation with Taurean after we talked to Yonah
	state find_taurean begin
		when enter or login begin
			send_letter(gameforge.new_quest_lv29._015_npcChat)
			q.start()                            
			q.set_title(gameforge.new_quest_lv29._015_npcChat)
			if is_destination_village(2) then -- 
			local v=find_npc_by_vnum(20014) -- Taurean	
				if 0!=v then
					target.vid("__TARGET2__", v, gameforge.new_quest_lv29._030_target) -- place questarrow on Taurean

				end
			end
		end
		when button or info begin -- this handles what is displayed in the quest-journal conversation window
			say_title(gameforge.new_quest_lv29._015_npcChat)
			say(gameforge.new_quest_lv29._031_say)
			say("")
			say(string.format(gameforge.new_quest_lv29._032_say, get_map_name_by_number(3)))
		end
		when 20014.chat.gameforge.new_quest_lv29._015_npcChat begin --EDIT Arne, 22Sept09, Reason: adapting all quest steps to choices -- When Taurean is clicked
			target.delete("__TARGET2__") -- delete questarrow
			say_title(gameforge.new_quest_lv29._033_say_title)
			say(gameforge.new_quest_lv29._034_say_reward)
			wait()
			say_pc_name()
			say_reward(gameforge.new_quest_lv29._035_say_reward)
			wait()
			say_title(gameforge.new_quest_lv29._033_say_title)
			say(gameforge.new_quest_lv29._036_say_reward)
			wait()
			say_pc_name()
			say_reward(gameforge.new_quest_lv29._037_say_reward)
			wait()
			say_title(gameforge.new_quest_lv29._033_say_title)
			say(gameforge.new_quest_lv29._038_say_reward)
			q.done()
			set_state(witness_harang)
		end
	end
	
	-- here we talk to Harang which Taurean asked us to interview to prove her innocence
	state witness_harang begin 
		
        when enter or login begin
			send_letter(gameforge.new_quest_lv29._015_npcChat) --EDIT Arne, 22Sept09, Reason: quest starts now even without mapchange
			q.start()
			q.set_title(gameforge.new_quest_lv29._015_npcChat)
			if is_destination_village(2) then
				local v=find_npc_by_vnum(20024) -- Harang
				if 0!=v then
					target.vid("__TARGET3__", v, gameforge.new_quest_lv29._040_target) -- place questarrow on Harang
				end
			end
		end
		when button or info begin -- when clicked on the quest-journal this text will be displayed
			say_title(gameforge.new_quest_lv29._015_npcChat)
			say(gameforge.new_quest_lv29._041_say)
			say("")
			say(string.format(gameforge.new_quest_lv29._047_say, get_map_name_by_number(3))) --EDIT Arne, 22Sept09, Reason: mapname in desc
		end
		when 20024.chat.gameforge.new_quest_lv29._015_npcChat begin --EDIT Arne, 22Sept09, Reason: adapting all quest steps to choices 
			target.delete("__TARGET3__") -- delete questarrow
			say_pc_name()
			say_reward(gameforge.new_quest_lv29._042_say_reward)
			wait()
			say_title(gameforge.new_quest_lv29._043_say_title)
			say(gameforge.new_quest_lv29._044_say_reward)
			wait()
			say_pc_name()
			say_reward(gameforge.new_quest_lv29._045_say_reward)
			wait()
			say_title(gameforge.new_quest_lv29._043_say_title)
			say(gameforge.new_quest_lv29._046_say_reward) 
			q.done()
			set_state(fragile_china)
		end
	end
	
	-- conversation with stablemaster
	state fragile_china begin
		
        when enter or login begin
			send_letter(gameforge.new_quest_lv29._015_npcChat)
			q.start()
			q.set_title(gameforge.new_quest_lv29._015_npcChat) -- updating the quest-journal title
			if is_destination_village(3) then
				local v=find_npc_by_vnum(20349) -- find the Stablemaster
				if 0!=v then
					target.vid("__TARGET4__", v, gameforge.new_quest_lv29._050_target) -- place questarrow on Stablemaster
				end
			end
		end
		when button or info begin -- this text will be displayed in the conversation window if the quest-journal is opened
			say_title(gameforge.new_quest_lv29._015_npcChat)
			say(gameforge.new_quest_lv29._051_say)
			say("")
			say(string.format(gameforge.new_quest_lv29._054_say, get_map_name_by_number(1), get_map_name_by_number(3))) --EDIT Arne, 22Sept09, Reason: mapname in desc

		end
		when 20349.chat.gameforge.new_quest_lv29._015_npcChat begin --EDIT Arne, 22Sept09, Reason: adapting all quest steps to choices 
			target.delete("__TARGET4__") -- delete questarrow
			-- the stablemaster whats us to collect some hey for Yonah
			say_title(gameforge.new_quest_lv29._082_say_title)
			say(gameforge.new_quest_lv29._052_say)
			wait()
			say_title(gameforge.new_quest_lv29._082_say_title)
			say(gameforge.new_quest_lv29._053_say)
			local hayCount = pc.count_item(50054) -- even the hay the player already has counts for this quest
			if  hayCount < 6 then -- usually the player will not have enough hay so he goes hunting
				new_quest_lv29_neededAmountDrop = 6
				q.done()
				set_state(cutting_hay)
			else -- shortcut if the player has the required amount of hay 
				say(gameforge.new_quest_lv29._065_1_say)
				q.done()
				set_state(calming_yonah)
			end
		end
	end
	-- In this state the player gathers the hay for Yonah. The player may 
	-- ask the three people for help he talked to previously or slay mobs
	-- with straw hats. He may even obtain the hay from other sources. 
	-- Only the last piece needs to be gathered by slaying a mob.
	state cutting_hay begin
		when login or enter begin
			send_letter(gameforge.new_quest_lv29._015_npcChat)
			q.start()
			q.set_title(gameforge.new_quest_lv29._015_npcChat)
			new_quest_lv29_mob1 = 501
			new_quest_lv29_mob2 = 551
			new_quest_lv29_drop = 50054 -- hay
			new_quest_lv29_dropProb = pc.getqf("dropProb")		-- recomment using a value of 100 which is 10% drop probability
			
			-- ticket id: 0076498
			if new_quest_lv29_dropProb == 0 then
				pc.setqf("dropProb", 80)
			end
			
			new_quest_lv29_neededAmountDrop = 6
			if pc.getqf("yonahspatience") < 2 then
				pc.setqf("dropProb", new_quest_lv29_dropProb /10)
			end
			if new_quest_lv29_neededAmountDrop <=  pc.count_item(new_quest_lv29_drop) or pc.getqf("hayIndex") == 0 then
				q.done()
				set_state(calming_yonah)
			end	
		end
		when button or info begin -- this is the text displayed in the conversation window when the quest-journal is opened
			if new_quest_lv29_neededAmountDrop <=  pc.count_item(new_quest_lv29_drop) or pc.getqf("hayIndex") == 0 then
				say_title(gameforge.new_quest_lv29._015_npcChat)
				say(string.format(gameforge.new_quest_lv29._091_say, get_map_name_by_number(1)))
				q.done()
				set_state(calming_yonah)
			else
				local sayMob1Name = mob_name(501)
				local sayMob2Name = mob_name(551)
				say_title(gameforge.new_quest_lv29._015_npcChat)
				say(gameforge.new_quest_lv29._061_say)
				say_reward(gameforge.new_quest_lv29._066_say)
				say(gameforge.new_quest_lv29._066_1_say)
				if pc.count_item(new_quest_lv29_drop) <= new_quest_lv29_neededAmountDrop then
					say("")
					say_reward(string.format(gameforge.new_quest_lv29._067_say_reward, pc.count_item(new_quest_lv29_drop), new_quest_lv29_neededAmountDrop))
				end
			end
		end
		when 501.kill begin -- Mob1 with straw hat
			if pc.count_item(new_quest_lv29_drop) < new_quest_lv29_neededAmountDrop and pc.getqf("hayIndex") > 0 then
				if math.random(1,1000) <= pc.getqf("dropProb") then -- probability if s.th. drops
					pc.give_item2(new_quest_lv29_drop)
					local qfindex = pc.getqf("hayIndex") -1
					pc.setqf("hayIndex", qfindex)
					notice_multiline(gameforge.new_quest_lv29._068_notice, notice)
					if new_quest_lv29_neededAmountDrop <=  pc.count_item(new_quest_lv29_drop) or pc.getqf("hayIndex") == 0 then
						q.done()
						set_state(calming_yonah)
					end		
				else
					notice_multiline(gameforge.new_quest_lv29._064_notice, notice)
				end
			end
		end
		when 551.kill begin -- Mob2 with straw hat
			if pc.count_item(new_quest_lv29_drop) < new_quest_lv29_neededAmountDrop and pc.getqf("hayIndex") > 0 then
				if math.random(1,1000) <= pc.getqf("dropProb") then -- probability if s.th. drops
					pc.give_item2(new_quest_lv29_drop)
					local qfindex = pc.getqf("hayIndex") -1
					pc.setqf("hayIndex", qfindex)
					notice_multiline(gameforge.new_quest_lv29._068_notice, notice )
					if new_quest_lv29_neededAmountDrop <=  pc.count_item(new_quest_lv29_drop) or pc.getqf("hayIndex") == 0 then
						q.done()
						set_state(calming_yonah)
					end		
				else
					notice_multiline(gameforge.new_quest_lv29._064_notice, notice)
				end
			end
		end
		-- The player may get hay from the npcs if he goes to them and asks. 
		-- When the player read the quest-texts he will gain the hay much easier then by slaying mobs.
		-- However the last piece of hay must be hunted
		when 20014.chat.gameforge.new_quest_lv29._015_npcChat begin -- Ask Taurean for help
			if pc.count_item(new_quest_lv29_drop) < new_quest_lv29_neededAmountDrop and pc.getqf("hayFromTaurean") == 1 and pc.getqf("hayIndex") > 0 then
				say_pc_name()
				say_reward(gameforge.new_quest_lv29._071_say_reward)
				wait()
				say_title(gameforge.new_quest_lv29._033_say_title)
				say(gameforge.new_quest_lv29._072_say_reward)
				pc.setqf("hayFromTaurean", 0) -- Taurean will only give one piece of hay
				pc.give_item2(new_quest_lv29_drop)
				local qfindex = pc.getqf("hayIndex") -1
				pc.setqf("hayIndex", qfindex)
				notice_multiline(string.format(gameforge.new_quest_lv29._073_notice, new_quest_lv29_neededAmountDrop-pc.count_item(new_quest_lv29_drop)), notice)
				if new_quest_lv29_neededAmountDrop <=  pc.count_item(new_quest_lv29_drop) or pc.getqf("hayIndex") == 0 then
					q.done()
					set_state(calming_yonah)
				end
			else -- Taurean will not always give hay
				say_title(gameforge.new_quest_lv29._033_say_title)
				say(gameforge.new_quest_lv29._074_say_reward)
			end
		end
		when 20024.chat.gameforge.new_quest_lv29._015_npcChat begin -- Ask Harang for help
			if pc.count_item(new_quest_lv29_drop) < new_quest_lv29_neededAmountDrop and pc.getqf("hayFromHarang") == 1 and pc.getqf("hayIndex") > 0 then
				say_pc_name()
				say_reward(gameforge.new_quest_lv29._075_say_reward)
				wait()
				say_title(gameforge.new_quest_lv29._043_say_title)
				say(gameforge.new_quest_lv29._076_say_reward)
				wait()
				say_pc_name()
				say_reward(gameforge.new_quest_lv29._077_say_reward)
				wait()
				say_title(gameforge.new_quest_lv29._043_say_title)
				say(gameforge.new_quest_lv29._078_say_reward )
				pc.setqf("hayFromHarang", 0) -- Harang will only give one piece of hay
				pc.give_item2(new_quest_lv29_drop)
				local qfindex = pc.getqf("hayIndex") -1
				pc.setqf("hayIndex", qfindex)
				notice_multiline(string.format(gameforge.new_quest_lv29._085_notice, new_quest_lv29_neededAmountDrop-pc.count_item(new_quest_lv29_drop)), notice)
				if new_quest_lv29_neededAmountDrop <=  pc.count_item(new_quest_lv29_drop) or pc.getqf("hayIndex") == 0 then
					q.done()
					set_state(calming_yonah)
				end
			else -- Harang will not always give hay
				say_title(gameforge.new_quest_lv29._043_say_title)
				say(gameforge.new_quest_lv29._079_say_reward)
			end
		end
		when 20349.chat.gameforge.new_quest_lv29._015_npcChat begin -- Ask the Stablemaster for help
			if pc.count_item(new_quest_lv29_drop) < new_quest_lv29_neededAmountDrop and pc.getqf("hayFromStablemaster") == 1 and pc.getqf("hayIndex") > 0 then
				say_pc_name()
				say_reward(gameforge.new_quest_lv29._081_say_reward)
				wait()
				say(gameforge.new_quest_lv29._082_say_title)
				say(gameforge.new_quest_lv29._083_say_reward)
				say(gameforge.new_quest_lv29._084_say_reward)
				pc.setqf("hayFromStablemaster", 0) -- The Stablemaster will only give one piece of hay
				pc.give_item2(new_quest_lv29_drop)
				local qfindex = pc.getqf("hayIndex") -1
				pc.setqf("hayIndex", qfindex)
				notice_multiline(string.format(gameforge.new_quest_lv29._085_notice, new_quest_lv29_neededAmountDrop-pc.count_item(new_quest_lv29_drop)), notice)
				if new_quest_lv29_neededAmountDrop <=  pc.count_item(new_quest_lv29_drop) or pc.getqf("hayIndex") == 0 then
					q.done()
					set_state(calming_yonah)
				end
			else -- The Stablemaster will not always give hay
				say(gameforge.new_quest_lv29._082_say_title)
				say_reward(gameforge.new_quest_lv29._086_say_reward)
			end
		end
	end
	
	-- Player returns to Yonah with or without hay and tries to calm him.
	state calming_yonah begin 
		-- we need to check if the player is in the right map
		
        when enter or login begin
			new_quest_lv29_mob1 = 501
			new_quest_lv29_mob2 = 551
			new_quest_lv29_drop = 50054 -- hay
			new_quest_lv29_dropProb = pc.getqf("dropProb")		-- recomment using a value of 100 which is 10% drop probability
			
			-- ticket id: 0076498
			if new_quest_lv29_dropProb == 0 then
				pc.setqf("dropProb", 80)
			end
			
			new_quest_lv29_neededAmountDrop = 6
			if pc.getqf("yonahspatience") < 2 then
				pc.setqf("dropProb", new_quest_lv29_dropProb /10)
			end
			send_letter(gameforge.new_quest_lv29._015_npcChat)--EDIT Arne, 22Sept09, Reason: no letter
			q.start() --EDIT Arne, 22Sept09, Reason: quest now starts even on same map
			q.set_title(gameforge.new_quest_lv29._015_npcChat)
			if is_destination_village(1) then -- 
				local position=find_npc_by_vnum(20005) -- Yonah
				if 0!=v then
					target.vid("__TARGET5__", position, gameforge.new_quest_lv29._010_target) -- place questarrow on Yonah
				end
			end
		end
		when button or info begin -- this event is triggered when the quest-journal for this quest is opened
			say_title(gameforge.new_quest_lv29._015_npcChat)
			say(string.format(gameforge.new_quest_lv29._091_say, get_map_name_by_number(1)))
		end
		when 20005.chat.gameforge.new_quest_lv29._015_npcChat begin -- Starting to talk to Yonah about the broken china
			target.delete("__TARGET5__") -- delete questarrow
			local hayCount = pc.count_item(50054) + pc.getqf("yonahHasHay")
			say_pc_name()
			say_reward(gameforge.new_quest_lv29._101_say_reward)
			wait()
			say_title(gameforge.new_quest_lv29._102_say_title)
			say(gameforge.new_quest_lv29._103_say_reward)
			wait()
			say_pc_name()
			say_reward(gameforge.new_quest_lv29._104_say_reward)
			if hayCount >= 6 and pc.getqf("yonahspatience") > 0 then -- when the player retuns with all the hay he or she was ask for
				say_reward(gameforge.new_quest_lv29._105_say_reward)
				wait()
				say_title(gameforge.new_quest_lv29._102_say_title)
				say(gameforge.new_quest_lv29._106_say_reward)
				say(gameforge.new_quest_lv29._107_say_reward )
				say(string.format(gameforge.new_quest_lv29._115_say_reward, 38000))
				pc.remove_item(50054, 6)
				pc.change_money(38000)
				pc.give_exp2(600000)
				if pc.getqf("hayFromTaurean") == 0 and pc.getqf("hayFromHarang") == 0 and pc.getqf("hayFromStablemaster") == 0 then
					local bonusxp = 0
					-- when the player found all npcs with hay and did less fighting he gets bonus XP 
					if pc.getqf("yonahspatience") > 1 then 
						bonusxp = 820*4*3*1000/pc.getqf("dropProb") 
						-- 820 is the xp value of #501 and #551 at level 29; 
						-- *4 because they are never alone; 
						-- *3 because player did not kill the mobs for 3 hay; 
						-- multiplied by the approximated drop chance.
						-- At a drop propability of 10% this would be a bonus of 98,400xp!
					else
						-- if the dropProb got lowered because the player lost some hay their should not be an additional bonus 
						bonusxp = 820*4*3*1000/pc.getqf("dropProb")/4
					end
						
					pc.give_exp2(bonusxp)
					notice_multiline(string.format(gameforge.new_quest_lv29._108_notice, bonusxp), notice)
				end
				q.done()
				clear_letter()
				pc.setqf("hayIndex", 0)
				pc.setqf("hayFromHarang", 0)
				pc.setqf("hayFromTaurean", 0)
				pc.setqf("hayFromStablemaster",0)
				pc.setqf("yonahspatience", 0)
				pc.setqf("yonahHasHay", 0)
				pc.setqf("dropProb", 0)
				set_state(__COMPLETE__)
			elseif hayCount > 1 and hayCount < 6 and pc.getqf("yonahspatience") > 1 then -- when the player has lost some hay on the way Yonah asks for more and triggers the state above
				say_reward(gameforge.new_quest_lv29._109_say_reward)
				wait()
				local bringMeMore = 6 - hayCount
				local lowerpatience = pc.getqf("yonahspatience") -1
				pc.setqf("yonahspatience", lowerpatience)
				local new_quest_lv29_dropProb = pc.getqf("dropProb")
				pc.setqf("dropProb", new_quest_lv29_dropProb/4) -- it will be damn hard to get more hay
				pc.remove_item(50054, hayCount)
				pc.setqf("yonahHasHay", hayCount)
				say_title(gameforge.new_quest_lv29._102_say_title)
				say(string.format(gameforge.new_quest_lv29._110_say_reward, bringMeMore))
				say(gameforge.new_quest_lv29._111_say_reward)
				new_quest_lv29_neededAmountDrop = bringMeMore
				local qfindex = pc.getqf("hayIndex") + bringMeMore
				pc.setqf("hayIndex", qfindex)
				q.done()
				clear_letter()
				set_state(cutting_hay)
			else -- when the player has lost all the hay or brings only 1 bunch the quest is completed with only 25% XP and Yang.
				wait()
				say_title(gameforge.new_quest_lv29._102_say_title)
				say_reward(gameforge.new_quest_lv29._112_say_reward )
				say_reward(gameforge.new_quest_lv29._113_say_reward)
				say_reward(gameforge.new_quest_lv29._114_say_reward)
				say(string.format(gameforge.new_quest_lv29._115_say_reward, 9500))
				pc.change_money(9500)
				pc.give_exp2(150000)
				q.done()
				clear_letter()
				pc.setqf("hayIndex", 0)
				pc.setqf("hayFromHarang", 0)
				pc.setqf("hayFromTaurean", 0)
				pc.setqf("hayFromStablemaster",0)
				pc.setqf("yonahspatience", 0)
				pc.setqf("yonahHasHay", 0)
				pc.setqf("dropProb", 0)
				set_state(__COMPLETE__)
			end
		end
	end
	state __GIVEUP__ begin
	end
	state __COMPLETE__ begin
	end
end
