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 then
			break
		end

		if nWaitTick > 4 then
			return 0 		--查询超时,也可能是没货
		end
	end

	local nPay = 999999
	local nSel = -1 
	local nNum = 0
	local sName = ""
	for i=0,9 do

		local now_name,now_shopname,now_shopid, now_num,now_price, now_flag1, now_flag2 = PlayerShop:GetItemPSInfo(i)
		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 tostring(now_name) == sItemName and now_num > 0 then
			local now_pay = now_num * now_price 
			if now_pay < nPay then
				nSel = i
				nPay = now_pay
				nNum = now_num
				sName = tostring(now_name)
			elseif now_pay == nPay and now_num > nNum then
				nSel = i
				nPay = now_pay
				nNum = now_num
				sName = tostring(now_name)
			end
		end
	end

	if nNum <= 0 or nSel == -1 then
		return -2 	--没货物
	end
				
	if nPay > max_price then
		return -4	--大于设定值
	end

	if nMoney < nPay then
		return -1
	end

	if sName ~= sItemName then
		return -3
	end
	
	DbgPrintf("购买 %s 物品(第 %d 行,金钱 %d)" , tostring(sItemName) , nSel , nPay )
	PlayerShop:SearchPageBuyItem(nSel,"item") --买
	nResult = 1

	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

-----------------------------------------------------------------------
--请你使用XX，在XX中XX
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上四处看看，帮我找来5个XX。
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

-----------------------------------------------------------------------
--去杀死XX个XX。
function OnShimen8()
	ShowMessage("师门任务8：不支持该任务。")
	OnShimenBlock(g_idScript)
end

-----------------------------------------------------------------------
--找到XX，进入XX
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()