ShowMessage("ʦű汾 $Rev: 194 $")

--[[
[author]
descript=ʦ,ɱxxʱԶ,һ60Ʒĵ5,򲻻Զɵʱ,ǰ150ԶʹͶȡһΡ
perday_max=1
cmd0str=ֻ20
cmd0={count=20}
cmd1str=ֻ40
cmd1={count=40}
cmd2str=ȫ
cmd2={count=60}
cmd_default=0
chk0str=ͣɵ
chk1str=̻ṺƷ
]]--
script_name="ʦ"

local g_dwNextTime = 0
local g_MaxCount = 9999
local g_AutoBuy = 0

g_Shimen_Npc = {
	--
	[0] = {szNpcName="۷", fX=96, fY=83, nSceneID=9},
	--
	[1] = {szNpcName="", fX=98, fY=105, nSceneID=11},
	--ؤ
	[2] = {szNpcName="ͨ", fX=91, fY=77, nSceneID=10},
	--䵱
	[3] = {szNpcName="", fX=78, fY=95, nSceneID=12},
	--
	[4] = {szNpcName="", fX=96, fY=88, nSceneID=15},
	--
	[5] = {szNpcName="", fX=96, fY=92, nSceneID=16},
	--
	[6] = {szNpcName="", fX=95, fY=88, nSceneID=13},
	--ɽ
	[7] = {szNpcName="", fX=95, fY=60, nSceneID=17},
	--ң
	[8] = {szNpcName="ع", fX=119, fY=152, nSceneID=14},
	--
	[9] = {szNpcName="", fX=0, fY=0, nSceneID=0},
	--Ī
	[10] = {szNpcName="Ľʤ", fX=68, fY=127, nSceneID=284},
	--
	[11] = {szNpcName="", fX=100, fY=64, nSceneID=615} ,
	--
	[12] = {szNpcName="", fX=95, fY=98, nSceneID=678}
}

g_Shime_Pos = {
	-- 0
	["۱"] = {fX=94, fY=70, nSceneID=9},
	["ؾ"] = {fX=134, fY=135, nSceneID=9},
	["ɽ"] = {fX=96, fY=125, nSceneID=9},
	["¥"] = {fX=69, fY=62, nSceneID=9},
	-- 1
	[""] = {fX=59, fY=52, nSceneID=11},
	["ľ"] = {fX=132, fY=139, nSceneID=11},
	["׽"] = {fX=60, fY=141, nSceneID=11},
	["ˮ"] = {fX=143, fY=57, nSceneID=11},
	--ؤ 2
	["С԰"] = {fX=39, fY=147, nSceneID=10},
	["ſ"] = {fX=132, fY=112, nSceneID=10},
	["ݱ̳"] = {fX=46, fY=36, nSceneID=10},
	["᷿"] = {fX=53, fY=88, nSceneID=10},
	--䵱 3
	[""] = {fX=88, fY=53, nSceneID=12},
	["⽣"] = {fX=47, fY=177, nSceneID=12},
	["̨"] = {fX=79, fY=137, nSceneID=12},
	[""] = {fX=46, fY=75, nSceneID=12},
	-- 4
	[""] = {fX=45, fY=44, nSceneID=15},
	["ˮ"] = {fX=146, fY=48, nSceneID=15},
	["˽̨"] = {fX=41, fY=148, nSceneID=15},
	[""] = {fX=148, fY=153, nSceneID=15},
	-- 5
	[""] = {fX=95, fY=56, nSceneID=16},
	[""] = {fX=87, fY=97, nSceneID=16},
	["֩"] = {fX=104, fY=96, nSceneID=16},
	["Ы"] = {fX=126, fY=73, nSceneID=16},
	-- 6
	["̫ξͭ"] = {fX=62, fY=90, nSceneID=13},
	["ͭ"] = {fX=85, fY=85, nSceneID=13},
	["θͭ"] = {fX=107, fY=85, nSceneID=13},
	["̫Ƣͭ"] = {fX=127, fY=90, nSceneID=13},
	--ɽ 7
	[""] = {fX=61, fY=37, nSceneID=17},
	[""] = {fX=74, fY=63, nSceneID=17},
	[""] = {fX=123, fY=89, nSceneID=17},
	["ұ"] = {fX=128, fY=50, nSceneID=17},	
	--ң 8
	[""] = {fX=55, fY=146, nSceneID=14},
	["ÿ"] = {fX=152, fY=144, nSceneID=14},
	["ʥ"] = {fX=49, fY=51, nSceneID=14},
	["໭"] = {fX=145, fY=57, nSceneID=14},
	--Ī 9
	["ˮ"] = {fX=154, fY=93, nSceneID=284},
	["С"] = {fX=78, fY=142, nSceneID=284},
	["κׯ"] = {fX=28, fY=28, nSceneID=284},
	["԰"] = {fX=119, fY=36, nSceneID=284},
	-- 10
	["䳡"] = {fX=48, fY=80, nSceneID=615},
	["Ƽڱ"] = {fX=80, fY=46, nSceneID=615},
	["ǧ"] = {fX=56, fY=136, nSceneID=615},
	["¥"] = {fX=102, fY=97, nSceneID=615} ,
	-- 12
	["Է"] = {fX=139, fY=143, nSceneID=678},
	["ɽ"] = {fX=40, fY=100, nSceneID=678},
	["̨"] = {fX=60, fY=42, nSceneID=678},
	["춫"] = {fX=125, fY=70, nSceneID=678},
--	["ƺ"] = {fX=139, fY=143, nSceneID=678},
--	["ʦڷ̳"] = {fX=60, fY=42, nSceneID=678},
--	[""] = {fX=60, fY=42, nSceneID=678},
--	[""] = {fX=60, fY=42, nSceneID=678},
}

g_Shime_Collect = {
	[0] = 
	{
		{x=69, y=42},{x=42,y=66},{x=56,y=147},{x=65,y=118},{x=62,y=87},{x=129,y=57},{x=133,y=143}
	},
	[1] = 
	{
		{x=97, y=33},{x=42,y=36},{x=38,y=150},{x=158,y=140},{x=145,y=35},{x=97,y=92}
	},
	[2] = {
		{x=124,y=60},{x=117,y=39},{x=149,y=36},{x=150,y=155},{x=129,y=110},{x=82,y=140},{x=44,y=114},{x=55,y=55}	
	},
	[3] = {
		{x=70,y=82},{x=100,y=128},{x=78,y=46},{x=94,y=59},{x=79,y=127},{x=75,y=135}
	},
	[4] = {
		{x=97,y=37},{x=144,y=39},{x=145,y=154},{x=89,y=139},{x=95,y=87},{x=38,y=150},{x=42,y=38}
	},
	[5] = {
		{x=67,y=143},{x=48,y=53},{x=95,y=50},{x=126,y=73},{x=140,y=93},{x=142,y=142},{x=95,y=127}
	},
	[6] = {
		{x=152,y=35},{x=42,y=43},{x=40,y=135},{x=64,y=150},{x=149,y=154},{x=150,y=90}
	},
	[7] = {
		{x=60,y=35},{x=74,y=67},{x=39,y=37},{x=41,y=145},{x=95,y=99},{x=146,y=140},{x=153,y=57}
	},
	[8] = {
		{x=52,y=148},{x=89,y=124},{x=50,y=60},{x=142,y=42},{x=148,y=150}
	},
	[10] = {
		{x=86,y=59},{x=28,y=58},{x=28,y=27},{x=130,y=34}	
	},
	[11] = {
		{x=80,y=30},{x=32,y=34},{x=45,y=135},{x=95,y=150},{x=126,y=108},{x=169,y=159}
	},
	[12] = {
		{x=81,y=141},{x=112,y=142},{x=146,y=131},{x=120,y=38},{x=34,y=65},{x=34,y=39},{x=56,y=31}
	}		
}

g_Shimen_Fuben = {
	[0] = {szNpc="", X=61, Y=62, nSceneID=9, szName=""},
	[1] = {szNpc="", X=89, Y=56, nSceneID=11, szName=""},
	[2] = {szNpc="ӡ", X=41, Y=144, nSceneID=10, szName="ƽ"},
	[3] = {szNpc="", X=58, Y=73, nSceneID=12, szName="Է"},
	[4] = {szNpc="", X=96, Y=73, nSceneID=15, szName="һ"},
	[5] = {szNpc="", X=128, Y=78, nSceneID=16, szName=""},
	[6] = {szNpc="", X=35, Y=86, nSceneID=13, szName=""},
	[7] = {szNpc="ս", X=101, Y=44, nSceneID=17, szName="÷"},
	[8] = {szNpc="밢", X=62, Y=68, nSceneID=14, szName="ȵ"},
	[10] = {szNpc="Ľݴ", X=69, Y=109, nSceneID=284, szName="Ѳˮ"},
	[11] = {szNpc="", X=66, Y=30, nSceneID=615, szName=""} ,
	[12] = {szNpc="֮", X=94, Y=51, nSceneID=678, szName="̳"}
}


-- ȡ
local g_szMission = "ʦ"
local g_nMenPai = Player:GetData("MENPAI")
local g_nParam = {}
local g_nIndex = -1
local g_idMission = 0
local g_idScript = 0
local l_use_tjqq = 0

-----------------------------------------------------------------------

function OnShimenBlock( idScript )

	local vigor = Player:GetData("VIGOR")
	UpdateMsg("shimen_tjqq")

	if vigor > 150 and l_use_tjqq == 0 then
		local Npc = g_Shimen_Npc[g_nMenPai]
		while true do
			g_nIndex, g_idMission, g_idScript = DataPool:GetPlayerMission(g_szMission)

			if g_nIndex == -1 then
				--ûʦ,ɻ߷
				return
			end

			System:Sleep(500)
			MoveToNPC(Npc.fX, Npc.fY, Npc.nSceneID, Npc.szNpcName)
			QuestFrameOptionClicked( "Ͷȡ" , CMP_PART_MATCH)	-- ѡѡ
			QuestFrameOptionClicked( "ȷ" , CMP_PART_MATCH)	-- ѡѡ
			System:Sleep(1000)

			local strType,_,_,_,szText = DataPool:GetNPCEventList_Item(0)

			if strType == "text" then
				if string.find(szText, "ѾʹһͶȡ") then
					l_use_tjqq = 1
					break
				end
			end

			if IsMsgCold("shimen_tjqq" , 10000) then
				break 
			end
		end
	end

	MissionBlockProcess( idScript )
end

--þûм
function OnShimen0()
	MissionMoveToNPC(tonumber(g_nParam[5]))
	QuestFrameOptionClicked("ʦ", CMP_PART_MATCH)	-- ѡѡ
end

-- -1 ûǮ, -2 û , 0 ,߲ѯʱ,Ҳпû , 1 ˹ûյƷ 2 յƷ
function BuyItemFromShop( sItemName , item_id , max_price)

	if DataPool:UserBag_CountItemByIDTable(item_id) > 0 then
		return 2
	end

	local nMoney = Player:GetData("MONEY")
	if nMoney <= 0 then
		return -1 		--ûǮ
	end

	MoveToNPC(329, 297, 0, "Ǹʢ")

	local nResult = 0 		--
	local pre_name,pre_shopname,pre_shopid,pre_num,pre_price,pre_flag1,pre_flag2 = PlayerShop:GetItemPSInfo(0)

	DbgPrintf("ѯ %s ƷϢ" , tostring(sItemName) )
	PlayerShop:SearchItem(2,1,1,tostring(sItemName),1)
	local nWaitTick = 0 
	while true do

		System:Sleep(1000)
		nWaitTick = nWaitTick + 1
		local now_name,now_shopname,now_shopid, now_num,now_price, now_flag1, now_flag2 = PlayerShop:GetItemPSInfo(0)

		if now_shopid ~= pre_shopid or now_price ~= pre_shopid or now_flag1 ~= pre_flag1 or now_flag2 ~= pre_flag2 or nWaitTick > 4 then
			DbgPrintf("ǰϢ %s,%s,%d,%d,%d,%d,%d" , tostring(now_name) , tostring(now_shopname) , now_shopid,now_num,now_price,now_flag1,now_flag2)

			if now_num <= 0 then
				return -2 	--û
			end
				
			if now_price > max_price then
				return -4	--趨ֵ
			end

			if nMoney < now_price then
				return -1
			end

			if now_name ~= sItemName then
				return -3
			end
	
			DbgPrintf(" %s Ʒ" , tostring(sItemName) )
			PlayerShop:SearchPageBuyItem(0,"item") --
			nResult = 1
			break 
		end

		if nWaitTick > 4 then
			break 		--ѯʱ,Ҳû
		end
	end

	nWaitTick = 0 
	while nResult == 1 do 
		
		if DataPool:UserBag_CountItemByIDTable(item_id) > 0 then
			nResult = 2	--Ʒ
			break 
		end

		System:Sleep(1000)

		nWaitTick = nWaitTick + 1
		if nWaitTick > 4 then
			nResult = 1 	--Ѿ˹,ûյ
			break 
		end
	end

	return nResult
end


-----------------------------------------------------------------------
--ҵXXôˣܰһ
function OnShimen1()
	DbgPrintf("OnShimen1")

	local nItemID = g_nParam[5]
	
	-- ȡƷϢ
	local _,szItem,BuyLink = DataBase:GetMissionItemInfo(nItemID)

	if DataPool:UserBag_CountItemByIDTable(nItemID) > 0 then
		return
	end

	local _,_,_, fX, fY, nScene , szNpcName = string.find(BuyLink ,"#.(.-)#.+_INFOAIM(%d+),(%d+),(%d+),(.+)}")

	-- жǷֱӹ
	if szNpcName == nil or szNpcName == "" then
		if g_AutoBuy == 1 and szItem ~= nil and string.find( BuyLink , "" ) then

			-- -1 ûǮ, -2 û , 0 ,߲ѯʱ,Ҳпû , 1 ˹ûյƷ 2 յƷ
			local buy_result = BuyItemFromShop( szItem , nItemID , 50000 ) 
			if buy_result < 0 then
				if buy_result == -1 then
					ShowMessage(" %s ȱٽǮ" , szItem)
				elseif buy_result == -4 then
					ShowMessage("%s ͼ۸ 5" , szItem)
				elseif buy_result == -2 then
					ShowMessage("̻û %s " , szitem)
				end
				OnShimenBlock(g_idScript)
			end
		else
			ShowMessage("ʦ1ûв鵽Ϣ %s", tostring(szItem))
			OnShimenBlock(g_idScript)	
			System:Sleep(2000)
			return
		end
	else
		MoveToNPC(tonumber(fX), tonumber(fY), tonumber(nScene), szNpcName)
		QuestFrameOptionClicked("", CMP_PART_MATCH)
		ShopBuy(nItemID, 1)

		System:Sleep(1000)

		if DataPool:IsMissionCanCommit(g_idMission) == false then
			if GetMyLastError() == LASTERROR_NOMONEY then
				ShowMessage("%s񣺽Ҳ", g_szMission )
				OnShimenBlock(g_idScript)
				System:Sleep(2000)
			end 
		end
	end
end

-----------------------------------------------------------------------
--XXΪȥѵһ£ϧûʱ
function OnShimen2()
	ShowMessage("ʦ2ָ֧")
	OnShimenBlock(g_idScript)
end

-----------------------------------------------------------------------
--ʹXXXXXX
function OnShimen3()
	DbgPrintf("OnShimen3")

	local	szPos = ""
	local	szToolsName = ""

	local nMax = DataPool:CollectPlayerMission_StrList(g_idMission)
	for i=0,nMax do
		local Ret = DataPool:GetPlayerMission_StrListInCollect(i)
		DbgPrintf("===[%d]%s", i, tostring(Ret))
	end

	DbgPrintf("ǰ:%d" , tonumber(g_nMenPai))

	--ȡλ, ʹƷ
	if g_nMenPai == 0 or g_nMenPai == 1 or g_nMenPai == 2 or g_nMenPai == 3 then					
		szPos = DataPool:GetPlayerMission_StrList(g_nIndex,g_nParam[6])
		szToolsName = DataPool:GetPlayerMission_StrList(g_nIndex,g_nParam[5])
	elseif g_nMenPai == 4 then
		szPos = DataPool:GetPlayerMission_StrList(g_nIndex,g_nParam[5])
		szToolsName = DataPool:GetPlayerMission_StrList(g_nIndex,g_nParam[7])
	elseif g_nMenPai == 5 or g_nMenPai == 6 then
		szPos = DataPool:GetPlayerMission_StrList(g_nIndex,g_nParam[6])
		szToolsName = DataPool:GetPlayerMission_StrList(g_nIndex,g_nParam[7])
	elseif g_nMenPai == 7 then
		szPos = DataPool:GetPlayerMission_StrList(g_nIndex,g_nParam[5])
		szToolsName = DataPool:GetPlayerMission_StrList(g_nIndex,g_nParam[6])
	elseif g_nMenPai == 8 then
		szPos = DataPool:GetPlayerMission_StrList(g_nIndex,g_nParam[6])
		szToolsName = DataPool:GetPlayerMission_StrList(g_nIndex,g_nParam[7])
	elseif g_nMenPai == 10 then
		szPos = DataPool:GetPlayerMission_StrList(g_nIndex,g_nParam[7])
		szToolsName = DataPool:GetPlayerMission_StrList(g_nIndex,g_nParam[6])
	elseif  g_nMenPai == 11 or g_nMenPai == 12 then
		szPos = DataPool:GetPlayerMission_StrList(g_nIndex,g_nParam[6])
		szToolsName = DataPool:GetPlayerMission_StrList(g_nIndex,g_nParam[7])
	end

	DbgPrintf(" %s" ,tostring(DataPool:GetPlayerMission_StrList(g_nIndex,g_nParam[5])))
	DbgPrintf(" %s" ,tostring(DataPool:GetPlayerMission_StrList(g_nIndex,g_nParam[6])))
	DbgPrintf(" %s" ,tostring(DataPool:GetPlayerMission_StrList(g_nIndex,g_nParam[7])))

	local i = string.find(szPos, "#{")
	if i then
		szPos = string.sub(szPos, 0, i - 1)
	end
	DbgPrintf("%s, %s", szPos, szToolsName)

	-- жλǷȷ
	if g_Shime_Pos[szPos] == nil then
		ShowMessage("ʦ3ûҵ[%s]", tostring(szPos))
		return
	end

	-- жʹƷǷȷ
	if szToolsName == "" then
		ShowMessage("ʦ3ƷΪգ")
		return
	end

	MoveTo(g_Shime_Pos[szPos].fX, g_Shime_Pos[szPos].fY, g_Shime_Pos[szPos].nSceneID, "", 1)
	Dismount()
	UseItem(szToolsName, "")
end

-----------------------------------------------------------------------
--ȥҵXX ȥսXXġ
function OnShimen4()
	ShowMessage("ʦ4ָ֧")
	OnShimenBlock(g_idScript)
end

function MatchInfomation( szText )
	local _,_,szName,nX ,nY,nScene = string.find(szText ,"ץһֻ#.#R(.-)#.-_INFOAIM(%d+),(%d+),(%d+),")
	return tostring(szName),tonumber(nX),tonumber(nY),tonumber(nScene)
end

-----------------------------------------------------------------------
--ץһֻXX
function OnShimen5()
	DbgPrintf("OnShimen5")

	local nMax = DataPool:CollectPlayerMission_StrList(g_idMission)
	for i=0,nMax do
		local Ret = DataPool:GetPlayerMission_StrListInCollect(i)
		DbgPrintf("[%d]%s", i, tostring(Ret))
	end

	local szName = ""
	local nX = 0
	local nY = 0
	local nScene = -1

	if nMax > 0 then
		local MapInfomation = DataPool:GetPlayerMission_StrListInCollect(0)
		szName, nX, nY, nScene = GetMapInfomationLink(MapInfomation)
	else
		local MapInfomation = DataPool:GetPlayerMission_StrListInCollect(-1)
		szName, nX, nY, nScene = MatchInfomation( MapInfomation)
	end

	DbgPrintf("%s %d %d %d" , tostring(szName) , tonumber(nX) , tonumber(nY) , tonumber(nScene))

	if szName == "" then
		ShowMessage("ʦ5޷ȡץ")
		DataPool:GetPlayerMission_Display(g_idMission)
		return
	end

	-- 
	--if DataPool:IsMissionCanCommit(g_idMission) then
		for i=1, PET_MAX_NUMBER do			--õǰ޵ĿΪмĳЩΪ
			if Pet:IsPresent(i-1) == true then
				if szName == Pet:GetName(i-1) then
					local Npc = g_Shimen_Npc[g_nMenPai]
					MoveToNPC(Npc.fX, Npc.fY, Npc.nSceneID, Npc.szNpcName)
					QuestFrameOptionClicked(g_szMission, CMP_PART_MATCH)	-- ѡѡ

					if UI:IsWindowShow("MissionReply") then
						local txt = string.format("MissionReply:OnContinue(%d);", i-1)
						DoLuaString("MissionReply_Env", txt)
					end

					DbgPrintf("")
					System:Sleep(500)
					QuestFrameMissionClose()
					return
				end
			end
		end
	--end

	if szName == "걱" then 
		nX = 227
		nY = 203
	end

	MoveTo(nX, nY, nScene)

	AI:SetAI("ɱ")
	AI:SetParameter("Ŀ", "", szName)
	AI:SetParameter("", "", szName)
	AI:SetParameter("ʰȡ", PM_NOPICKUP)
	AI:SetParameter("ͼ", nScene)
	AI:SetParameter("", "", nX, nY)
	AI:Start(false)

	while Pet_GetPetByName(szName) == -1 do 

		local sid, x, y, die = FindCharacterByName("")
		if sid ~= INVALID_ID and die == 0 then
			local xNow, yNow = Player:GetPos()
			if GetDistSq(xNow, yNow, x, y) < 10 then

				System:OutputDebugString("")

				AI:Stop()
				while Player_GetBuff("") == -1 do
					Pet_Relax()
					UseSkill("")
					System:Sleep(200)
				end

				while Player_GetBuff("") ~= -1 do
					System:Sleep(200)
				end
				break
			end
		end

		System:Sleep(1000) 
	end

	AI:Stop()
end

-----------------------------------------------------------------------
--XXĴ5XX
function OnShimen6()

	local nSceneID = GetActiveDataSceneID()

	if nSceneID ~= g_Shimen_Npc[g_nMenPai].nSceneID then
		local Npc = g_Shimen_Npc[g_nMenPai]
		MoveToNPC(Npc.fX, Npc.fY, Npc.nSceneID, Npc.szNpcName)
		return
	end

--	local szItemName = DataPool:GetPlayerMission_StrList(g_nParam[5])
	local szItemName = DataPool:GetItemText(g_nParam[6])
	DbgPrintf("ѰƷΪ:%s", tostring(szItemName))

	if g_nMenPai == 4 then
		if szItemName == "ʧ" then
			szItemName = "Сƺ"
		elseif szItemName == "ʧļƷ" then
			szItemName = "Сں"
		elseif szItemName == "ʧ鱦" then
			szItemName = "С׺"
		end
	end

	if szItemName == "" then
		ShowMessage("ʦ6޷ȡòɼƷ")
		return
	end

	--DbgPrintf("[%s]", szItemName)

	if g_Shime_Collect[g_nMenPai] == nil then
		ShowMessage("ʦ6ûҵɵĲɼ")
		return
	end
	
	AI:SetAI("ɼ")
	AI:SetParameter("ʼ")
	AI:SetParameter("Ŀ", "", szItemName)
	AI:SetParameter("ͼ", g_Shimen_Npc[g_nMenPai].nSceneID)

	for i, pos in ipairs(g_Shime_Collect[g_nMenPai]) do
		AI:SetParameter("", "", pos.x, pos.y)
	end

	AI:Start(false)

	while DataPool:IsMissionCanCommit(g_idMission) == false do
		System:Sleep(500)
	end

	AI:Stop()
end

-----------------------------------------------------------------------
--XXȥһXXɣ³֮һ㱨ģ
function OnShimen7()
	ShowMessage("ʦ7ָ֧")
	OnShimenBlock(g_idScript)
end

-----------------------------------------------------------------------
--ȥɱXXXX
function OnShimen8()
	ShowMessage("ʦ8ָ֧")
	OnShimenBlock(g_idScript)
end

-----------------------------------------------------------------------
--ҵXXXX
function OnShimen9()

	if g_Shimen_Fuben[g_nMenPai] == nil then
		PushDebugMessage("ʦ9޷ȡø" , 1)
		return
	end

	local Fuben = g_Shimen_Fuben[g_nMenPai]

	MoveToNPC(Fuben.X, Fuben.Y, Fuben.nSceneID, Fuben.szNpc)
	QuestFrameOptionClicked(Fuben.szName, CMP_PART_MATCH)
	QuestFrameAcceptClicked()
end

-----------------------------------------------------------------------
-- NPC
function OnShimen_Visit_NPC_Of_Other()
	
	local szPosString = DataPool:GetPlayerMission_StrList(g_nIndex,g_nParam[6])

	if szPosString == nil then
		ShowMessage("ʦŷ޷Ҫȡ÷ʵNPCַ")
		return
	end

	--DbgPrintf("%s", szPosString)

	local fX, fY, nSceneID, szNpcName

	-- ⴦ȡóӵĵַ
	if string.find(szPosString, "_INFOAIM") then	-- Ʒûгӣؿֵ
		
		szPosString = string.gsub(szPosString, "_INFOAIM", "")

		 _, _, fX, fY, nSceneID, szNpcName = string.find(szPosString, "{(%d+),(%d+),(%d+),(.+)}")

		if szNpcName == "-1" then
			szNpcName = ""
		end
	end

	if szNpcName == "" then
		ShowMessage("ʦŷ޷ҪʵNPC")
		return
	end

	OptionItem = {
		[0] = {npc="", item=""},
		[1] = {npc="", item=""},
		[2] = {npc="ӡ", item="ƽ"},
		[3] = {npc="", item="Է"},
		[4] = {npc="", item="һ"},
		[5] = {npc="", item=""},
		[6] = {npc="", item=""},
		[7] = {npc="ս", item="÷"},
		[8] = {npc="밢", item="ȵ"},
		[10] = {npc="Ľݴ", item="Ѳˮ"},
		[11] = {npc="", item=""},
		}

	local szOptItem = ""
	for i, v in pairs(OptionItem) do
		if v.npc == szNpcName then
			szOptItem = v.item
			break
		end
	end

	if szOptItem == nil then
		ShowMessage("ʦŷûҵΧ˵ѡ")
		return
	end

	MoveToNPC(tonumber(fX), tonumber(fY), tonumber(nSceneID), szNpcName)
	QuestFrameOptionClicked(szOptItem, CMP_FULL_MATCH)	-- ѡѡ
	QuestFrameAcceptClicked()
end


-----------------------------------------------------------------------

function _SMain(...)

	if _CommandLine ~= nil then 
		if _CommandLine.skip ~= nil then
			BLOCK_PROCESS_MODE = _CommandLine.skip
		else
			BLOCK_PROCESS_MODE = BPM_ABNEGATE
		end
		if _CommandLine.count ~= nil then
			g_MaxCount = tonumber(_CommandLine.count)
		end
	end

	if _CommandChk0 ~= nil and tonumber(_CommandChk0) > 0 then
		BLOCK_PROCESS_MODE = BPM_TIP
	else
		BLOCK_PROCESS_MODE = BPM_ABNEGATE
	end
	
	if _CommandChk1 ~= nil and tonumber(_CommandChk1) > 0 then
		g_AutoBuy = 1
	else
		g_AutoBuy = 0
	end 	

	--DbgPrintf("ʦ %d , :%d , Զ:%d" , tonumber(g_MaxCount) , tonumber(BLOCK_PROCESS_MODE) , tonumber(g_AutoBuy)) 

	while true do

		LuoYangRestore()

		g_nIndex, g_idMission, g_idScript = DataPool:GetPlayerMission(g_szMission)

		if g_nIndex ~= -1 then

			DbgPrintf("%s Index:%d MissionID:%d ScriptID:%d", g_szMission, g_nIndex, g_idMission, g_idScript)

			-- ȡ
			for i=0,7 do
				g_nParam[i] = DataPool:GetPlayerMission_Variable(g_nIndex, i)
				DbgPrintf("[%d]%d", i, g_nParam[i])
			end

			if DataPool:IsMissionCanCommit(g_idMission) then

				DbgPrintf("")
				-- 

				-- ץ⴦
				if g_nParam[4] == 5 then
					OnShimen5()
				else
					local Npc = g_Shimen_Npc[g_nMenPai]
					MoveToNPC(Npc.fX, Npc.fY, Npc.nSceneID, Npc.szNpcName)
					QuestFrameOptionClicked(g_szMission, CMP_PART_MATCH)	-- ѡѡ
					QuestFrameMissionContinue()	-- 
					QuestFrameMissionComplete()	-- 
					QuestFrameMissionClose()
				end

			else

				DbgPrintf("")

				-- ʦŷNPC
				if g_idScript == 229010 then

					OnShimen_Visit_NPC_Of_Other()
				else

					local nFunc = g_nParam[4]

					if g_nMenPai == 0 then
						if nFunc > 8 then
							nFunc = nFunc - 1
						end
					elseif g_nMenPai == 5 then
						if nFunc == 8 then
							nFunc = 9
						end
					end

					-- ʦų
					-- δ
					if nFunc == 0 then
						OnShimen0()
					elseif nFunc == 1 then
						OnShimen1()
					elseif nFunc == 2 then
						OnShimen2()
					elseif nFunc == 3 then
						OnShimen3()
					elseif nFunc == 4 then
						OnShimen4()
					elseif nFunc == 5 then
						OnShimen5()
					elseif nFunc == 6 then
						OnShimen6()
					elseif nFunc == 7 then
						OnShimen7()
					elseif nFunc == 8 then
						OnShimen8()
					elseif nFunc == 9 then
						OnShimen9()
					end
				end
			end
		else
			-------------------------------------------------------------------------------------
			-- ȡ
			local nRound = 0 

			local isNext,szMsg = GetDebugMessage()
			while isNext == 1 do
				local _,_,nRd = string.find(szMsg ,"Ѿ#.(%d+)#.ʦ")
				if nRd ~= nil then
					nRound = tonumber(nRd)
				end
				isNext,szMsg = GetDebugMessage()
			end

			if nRound >= g_MaxCount then
				ShowMessage("ʦѾ %d " , nRound )
				System:UpdateRecord( script_name )
				break 
			end
			
			local dwNow = System:GetTickCount()

			if dwNow > g_dwNextTime then

				local Npc = g_Shimen_Npc[g_nMenPai]

				MoveToNPC(Npc.fX, Npc.fY, Npc.nSceneID, Npc.szNpcName)
				QuestFrameOptionClicked(g_szMission, CMP_PART_MATCH)	-- ѡѡ

				local strType,_,_,_,szText = DataPool:GetNPCEventList_Item(1);

				if strType == "text" then
					if string.find(szText, "15") then
						ShowMessage("ʦ15ڲܽʦ")
						g_dwNextTime = dwNow + 1000 * 60
					elseif string.find(szText, "һֻ60") then
						ShowMessage("ʦѾ60ʦ")
						System:UpdateRecord( script_name )
						break	-- ˳ѭű
					end
				end

				QuestFrameMissionClose()
			else
				-- ȴʦ
				ShowMessage("ʦ15ڲܽʦ")
				System:Sleep(1500)
			end
		end

		System:Sleep(500)
	end
end

--BPM_TIP
_SMain()