quest pony_buy begin
	state start begin
		when 20349.chat.gameforge.pony_buy._10_npcChat with horse.get_grade()==0 begin
			if pc.level<=24 then
				say_title(gameforge.horse_exchange_ticket._20_sayTitle)
				say(gameforge.pony_buy._20_say)
			elseif pc.countitem("50050")<1 then
				say_title(gameforge.horse_levelup._240_sayTitle)
				say(gameforge.pony_buy._30_say)
				setstate(need_item50050)
			elseif pc.countitem("50050")>=1 and pc.level>=25 then
				say_title(gameforge.horse_levelup._240_sayTitle)
				say(gameforge.pony_buy._40_say)
				local b= select(gameforge.find_brother_article._90_select, gameforge.find_brother_article._100_select)
				if 1==b then
					if pc.countitem("50050")>=1 then
						pc.removeitem("50050", 1)
						setstate(test)
					end
				elseif 2==b then
					say_title(gameforge.horse_exchange_ticket._20_sayTitle)
					say(gameforge.pony_buy._50_say)
				else
					say(string.format(gameforge.find_squareguard._70_say, b))
				end
			else
				say_title(gameforge.horse_exchange_ticket._20_sayTitle)
				say(gameforge.pony_buy._60_say)
			end
		end
	end
	state need_item50050 begin
		when letter begin
			setskin(NOWINDOW)
			makequestbutton(gameforge.horse_upgrade2._100_makequestbutton)
			q.set_title(gameforge.horse_upgrade2._100_makequestbutton)
			q.start()
		end
		when button begin
			say_title(gameforge.buy_fishrod._210_sayTitle)
						say_reward(gameforge.pony_buy._70_sayReward)
			setstate(start)
			q.done()
		end
		when info begin
			say_title(gameforge.buy_fishrod._210_sayTitle)
						say_reward(gameforge.pony_buy._70_sayReward)
			setstate(start)
			q.done()
		end
	end
	state test begin
		when letter begin
			q.set_counter(gameforge.pony_buy._test_503_count, 20-pc.getqf("kill_count"))
		end
		when 503.kill begin
			pc.setqf("kill_count", pc.getqf("kill_count")+1)
			q.set_counter(gameforge.pony_buy._test_503_count, 20-pc.getqf("kill_count"))
			if get_time()>=pc.getqf("limit_time") then
				setstate(failure)
			end
		end
		when letter begin
			q.set_clock(gameforge.horse_upgrade2.rest_time, pc.getqf("limit_time")-get_time())
		end
		when enter begin
			pc.setqf("limit_time", get_time()+30*60)
			pc.setqf("kill_count", 0)
		end
		when leave begin
			q.done()
		end
		when letter begin
			setskin(NOWINDOW)
			makequestbutton(gameforge.pony_buy._80_makequestbutton)
			q.set_title(gameforge.pony_buy._80_makequestbutton)
			q.start()
		end
		when button begin
			say_title(gameforge.pony_buy._80_makequestbutton)
						say_reward(gameforge.pony_buy._90_sayReward)
					end
		when info begin
			say_title(gameforge.pony_buy._80_makequestbutton)
			say(gameforge.pony_buy._100_say)
		end
		when 503.kill with pc.getqf("kill_count") >= 20 and pc.getqf("limit_time")>=get_time() begin
			setstate(report)
		end
		when 20349.chat.gameforge.pony_buy._110_npcChat with horse.get_grade()!=0 begin
			setstate(start)
			q.done()
		end
		when 20349.chat.gameforge.pony_buy._120_npcChat begin
			say_title(gameforge.horse_exchange_ticket._20_sayTitle)
						say_reward(gameforge.pony_buy._130_sayReward)
						local b= select(gameforge.locale.levelup.prev_quest_go, gameforge.pony_buy._140_select)
			if 1==b then
			elseif 2==b then
				say_title(gameforge.horse_exchange_ticket._20_sayTitle)
				say(gameforge.pony_buy._150_say)
				local b= select(gameforge.locale.guild.yes, gameforge.pony_buy._160_select)
				if 1==b then
					say_title(gameforge.horse_exchange_ticket._20_sayTitle)
					say(gameforge.pony_buy._170_say)
					setstate(start)
					q.done()
				elseif 2==b then
					say_title(gameforge.pony_buy._180_sayTitle)
					say(gameforge.pony_buy._190_say)
				else
					say(string.format(gameforge.find_squareguard._70_say, b))
				end
			else
				say(string.format(gameforge.find_squareguard._70_say, b))
			end
		end
	end
	state report begin
		when letter begin
			setskin(NOWINDOW)
			makequestbutton(gameforge.horse_upgrade2._230_makequestbutton)
			q.set_title(gameforge.horse_upgrade2._230_makequestbutton)
			q.start()
		end
		when button begin
			say_title(gameforge.horse_upgrade2._230_makequestbutton)
						say_reward(gameforge.horse_upgrade2._240_sayReward)
					end
		when info begin
			say_title(gameforge.horse_upgrade2._230_makequestbutton)
						say_reward(gameforge.horse_upgrade2._240_sayReward)
					end
		when 20349.chat.gameforge.horse_upgrade2._250_npcChat with horse.get_grade()!=0 begin
			setstate(start)
			q.done()
		end
		when 20349.chat.gameforge.horse_upgrade2._260_npcChat with horse.get_grade()==0 begin
			say_title(gameforge.horse_exchange_ticket._20_sayTitle)
			say(gameforge.pony_buy._200_say)
			-- The Stable Boy used to disappear for 8 to 16 hours to fetch the
			-- horse, parked in state "wait" behind a "make_time" flag.
			setstate(buy)
		end
	end
	state wait begin
		when letter begin
			setskin(NOWINDOW)
			makequestbutton(gameforge.pony_buy._210_makequestbutton)
			q.set_title(gameforge.pony_buy._210_makequestbutton)
			q.start()
		end
		when button begin
			say_title(gameforge.pony_buy._210_makequestbutton)
			say(gameforge.pony_buy._220_sayReward)
		end
		when info begin
			say_title(gameforge.pony_buy._210_makequestbutton)
			say(gameforge.pony_buy._220_sayReward)
		end
		when login begin
			setstate(buy)
		end
		when 20349.chat.gameforge.horse_upgrade2._250_npcChat with horse.get_grade()!=0 begin
			setstate(start)
			q.done()
		end
		when 20349.chat.gameforge.pony_buy._230_npcChat with horse.get_grade()==0 begin
			say_title(gameforge.horse_exchange_ticket._20_sayTitle)
			say(gameforge.pony_buy._240_say)
		end
	end
	state buy begin
		when letter begin
			setskin(NOWINDOW)
			makequestbutton(gameforge.pony_buy._250_makequestbutton)
			q.set_title(gameforge.pony_buy._250_makequestbutton)
			q.start()
		end
		when button begin
			say_title(gameforge.pony_buy._250_makequestbutton)
			say(gameforge.pony_buy._260_sayReward)
		end
		when info begin
			say_title(gameforge.pony_buy._250_makequestbutton)
			say(gameforge.pony_buy._260_sayReward)
		end
		when 20349.chat.gameforge.pony_buy._270_npcChat with horse.get_grade()==0 begin
			say_title(gameforge.pony_buy._280_sayTitle)
			say(gameforge.pony_buy._290_say)
			local b= select(gameforge.buy_fishrod._40_select, gameforge.pony_buy._300_select, gameforge.pony_buy._140_select)
			if 1==b then
				if pc.money>=100000 then
					char_log(0, " HORSE_BUY", "BEGIN")
					pc.changemoney(-100000)
					char_log(0, " HORSE_BUY", " DEC money 100000")
					horse.unride()
					horse.advance()
					horse.ride()
					char_log(0, " HORSE_BUY", "INC horse_advance 1")
					pc.give_item2("50051", 1)
					char_log(0, " HORSE_BUY", "INC 50051 1")
					char_log(0, " HORSE_BUY", "END")
					say_title(gameforge.horse_exchange_ticket._20_sayTitle)
					say(gameforge.pony_buy._310_say)
					setstate(start)
					q.done()
				else
					say_title(gameforge.horse_exchange_ticket._20_sayTitle)
					say(gameforge.pony_buy._320_say)
				end
			elseif 2==b then
				say_title(gameforge.horse_exchange_ticket._20_sayTitle)

				say(gameforge.pony_buy._330_say)
			elseif 3==b then
				say_title(gameforge.horse_exchange_ticket._20_sayTitle)
				say(gameforge.pony_buy._340_say)
				local b= select(gameforge.locale.guild.yes, gameforge.pony_buy._350_select)
				if 1==b then
					setstate(start)
				elseif 2==b then
				else
					say(string.format(gameforge.find_squareguard._70_say, b))
				end
			else
				say(string.format(gameforge.find_squareguard._70_say, b))
			end
		end
	end
	state failure begin
		when letter begin
			setskin(NOWINDOW)
			makequestbutton(gameforge.pony_buy._360_makequestbutton)
			q.set_title(gameforge.pony_buy._360_makequestbutton)
			q.start()
		end
		when button begin
			say_title(gameforge.pony_buy._360_makequestbutton)
						say_reward(gameforge.pony_buy._370_sayReward)
						say_reward(gameforge.pony_buy._380_sayReward)
			setstate(start)
			q.done()
		end
		when info begin
			say_title(gameforge.pony_buy._360_makequestbutton)
						say_reward(gameforge.pony_buy._370_sayReward)
						say_reward(gameforge.pony_buy._380_sayReward)
			setstate(start)
			q.done()
		end
	end
	state __COMPLETE__ begin
		when enter begin
			q.done()
		end
	end
end

