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