ShowMessage("¸ĸű $Rev: 214 $")

--[[
[author]
descript="ÿ5"
]]--
perday_max=1
script_name="¸ĸ"


local g_szMission = "¸ĸ"
local g_nParam = {}
local g_nIndex = 0
local g_idMission = 0
local g_idScript = 0
local g_CurSelInfantIndex = 0

local g_NpcInfo = {
	[0] = {szNpcName="ľӽ", fX=151, fY=187, nSceneID=0},
}

local g_MissionTrack_Infant_SubMissionTypeEnum = {BaiFang=1, Caiji=2, DingDianYinDao=3, XunWu=4, BuZhuo=5,}

function OnMission_890500()

	local MissType = g_nParam[1]
	local BaoZhuangIndex = g_nParam[2]
	local MissIndex = g_nParam[3]

	local nMisId, nSceneID, szSceneName, szNpcName, X, Y = DataBase:GetInfantExpMission(MissIndex)

	--DbgPrintf("# %d %d %s %s %d,%d", nMisId, nSceneID, szSceneName, szNpcName, X, Y )

	if nMisId == -1 then
		
		ShowMessage("%sûҵNPCϢ[%d]", g_szMission, MissIndex)
		return
	end

	if MissType == g_MissionTrack_Infant_SubMissionTypeEnum.BaiFang then
		--[[
		if BaoZhuangIndex == 0 then
		elseif BaoZhuangIndex == 1 then
		elseif BaoZhuangIndex == 2 then
		elseif BaoZhuangIndex == 3 then
		end
		]]--

		MoveToNPC(X, Y, nSceneID, szNpcName)
		QuestFrameOptionClicked(g_szMission, CMP_PART_MATCH)	-- ѡѡ
		
	elseif MissType == g_MissionTrack_Infant_SubMissionTypeEnum.Caiji then
		
		--[[
		if BaoZhuangIndex == 0 then
		elseif BaoZhuangIndex == 1 then
		elseif BaoZhuangIndex == 2 then
		elseif BaoZhuangIndex == 3 then
		end
		]]--


		MoveTo(X, Y, nSceneID, "", 1)

		-- AIΪɼ
		AI:SetAI("ɼ")
		AI:SetParameter("ʼ")
		AI:SetParameter("Ŀ", "", szNpcName)
		AI:SetParameter("ͼ", nSceneID)
		AI:SetParameter("", "", X, Y)

		AI:Start(false)
		while DataPool:IsMissionCanCommit(g_idMission) == false do
			System:Sleep(1000)
		end
		AI:Stop()

	elseif MissType == g_MissionTrack_Infant_SubMissionTypeEnum.XunWu then

		--[[
		if BaoZhuangIndex == 0 then
		elseif BaoZhuangIndex == 1 then
		elseif BaoZhuangIndex == 2 then
		elseif BaoZhuangIndex == 3 then
		end
		]]--

		local _,_,_,nItemID = DataBase:GetInfantExpMissionData(MissIndex)

		-- ȡƷϢ
		if nItemID == -1 then
			ShowMessage("޷ȡùƷID")
			return
		end

		if MissIndex >= 47 and MissIndex <= 55 then
			DbgPrintf("nitemid = %d" , tonumber(nItemID))
			local _,_,BuyLink = DataBase:GetMissionItemInfo(nItemID)

			DbgPrintf("BuyLink=%s" , tostring(BuyLink))

			local _,_,_, tX , tY , tMapID , tNpcName = string.find(BuyLink ,"#.(.-)#.+_INFOAIM(%d+),(%d+),(%d+),(.+)}")

			if tMapID == nil then
				ShowMessage("%sûвѯƷĹַ[%d]", g_szMission, nItemID)
				MissionBlockProcess(g_idScript)
				return
			end

			X = tonumber(tX)
			Y = tonumber(tY)
			nSceneID = tonumber(tMapID)
			szNpcName = tostring(tNpcName)

		elseif MissIndex >= 56 and MissIndex <= 58 then

			szNpcName = ""
			X = 224
			Y = 296
			nSceneID = 0
		else
			ShowMessage("޷ʶƷ[nItemID=%d]", nItemID)
			return
		end

		DbgPrintf("%d,%d:%d %s", X, Y, nSceneID, szNpcName)
--[[
		-- ȡӵн
		local nMoney = Player:GetData("MONEY")

		-- ȡƷĹ۸
		local _, nBuyPrice = DataBase:GetCommonItemInfo(nItemID)

		if nBuyPrice == 0 then
			_, nBuyPrice = DataBase:GetEquipItemInfo(nItemID)
		end

		if nBuyPrice == 0 then
			ShowMessage("%sûвѯƷۼ۸[%d]", g_szMission, nItemID)
			return
		end

		-- жǷԹƷ
		if nBuyPrice > nMoney then
			ShowMessage("%s񣺽Ҳ㣬ƷҪ%d, Ŀǰӵ%d", nBuyPrice, nMoney)
			MissionBlockProcess(g_idScript)
			return
		end
]]--
		MoveToNPC(X, Y, nSceneID, 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 )
				MissionBlockProcess(g_idScript)
			end 
		end
		
	elseif MissType == g_MissionTrack_Infant_SubMissionTypeEnum.DingDianYinDao then

		if BaoZhuangIndex == 1 then
			
			szNpcName = ""

		elseif BaoZhuangIndex == 2 then

			szNpcName = "С"

		elseif BaoZhuangIndex == 3 then

			szNpcName = ""

		elseif BaoZhuangIndex == 4 then

			szNpcName = "ɻ"

		end

		MoveTo(X, Y, nSceneID, "", 1)
		Dismount()
		UseItem(szNpcName, "")
		
	elseif MissType == g_MissionTrack_Infant_SubMissionTypeEnum.BuZhuo then
		--[[
		if BaoZhuangIndex == 0 then
		elseif BaoZhuangIndex == 1 then
		elseif BaoZhuangIndex == 2 then
		elseif BaoZhuangIndex == 3 then
		end
		]]--

		if szNpcName == "ɡ" then  szNpcName = "" end

		if szNpcName == "걱" then 
			X = 227
			Y = 203
		end

		MoveTo(X, Y, 112)

		AI:SetAI("ɱ")
		AI:SetParameter("Ŀ", "", szNpcName)
		AI:SetParameter("", "", szNpcName)
		AI:SetParameter("ͼ", 112)
		AI:SetParameter("", "", X, Y)
		AI:Start(false)

		while Pet_GetPetByName(szNpcName) == -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
					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(200) 
		end
		AI:Stop()
	end

end


function _SMain(...)

	-- жǷŮ
	if Infant:GetInfantCount() == 0 then
		ShowMessage("¸ĸģδӵŮ")
		return
	end

	while true do

		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("")

				-- ƶNPC
				--MoveTo(147, 179, 0, "", 1)
				MoveToNPC(g_NpcInfo[0].fX, g_NpcInfo[0].fY, g_NpcInfo[0].nSceneID, g_NpcInfo[0].szNpcName)
				QuestFrameOptionClicked(g_szMission, CMP_PART_MATCH)	-- ѡѡ

				if g_nParam[1] == g_MissionTrack_Infant_SubMissionTypeEnum.BuZhuo then

					if UI:IsWindowShow("MissionReply") then
					
						-- ȡҪ
						local _, _, _, szNpcName = DataBase:GetInfantExpMission(g_nParam[3])
						
						if szNpcName == "ɡ" then  szNpcName = "" end

						-- 
						for i=1, PET_MAX_NUMBER do
							if szNpcName == Pet:GetName(i-1) then
								DoLuaString("MissionReply_Env", string.format("MissionReply:OnContinue(%d)", i-1))
								System:Sleep(500)
								break
							end
						end
					end
				else
					QuestFrameMissionContinue()	-- 
					QuestFrameMissionComplete()	-- 
					QuestFrameMissionClose()
				end
				
			else
				-- ѽδ
				DbgPrintf("")

				local xNow, yNow = Player:GetPos()
				local currentSceneID = GetActiveDataSceneID()
				-- жǷ񵽴
				--if currentSceneID == 0 then
				--	if GetDistSq(xNow, yNow, 147, 179) < 30 then
				--		MoveTo(147, 179, 0, "", 1)
				--	end
				--end

				OnMission_890500()
			end

		else
			-------------------------------------------------------------------------------------
			-- ȡ

			-- жŮֵ
			InfantTreatBadStatus(g_CurSelInfantIndex)

			-- жȼ
			if Infant:GetInfantLevel(g_CurSelInfantIndex) > 90 then
					ShowMessage("¸ĸģŮȼ91ȡ")
					System:UpdateRecord( script_name )
					break	-- ˳ѭű
			end

			MoveToNPC(g_NpcInfo[0].fX, g_NpcInfo[0].fY, g_NpcInfo[0].nSceneID, g_NpcInfo[0].szNpcName)
			QuestFrameOptionClicked(g_szMission, CMP_FULL_MATCH)	-- ѡѡ

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

			if strType == "text" then
				if string.find(szText, "δ⸸ĸ") then
					ShowMessage("¸ĸģ5")
					System:UpdateRecord( script_name )
					break	-- ˳ѭű
				end
			end

			QuestFrameMissionClose()
		end

		System:Sleep(500)

	end
end

_SMain()