ShowMessage("提升子女素养任务脚本 $Rev: 187 $")

--[[
[author]
descript="每天可以做1轮"
]]--
perday_max=1
script_name="子女五项"


local g_nParam = {}
local g_nIndex = 0
local g_idMission = 0
local g_idScript = 0
local g_CurSelInfantIndex = 0

local g_szMission = {
	[0] = "学习对诗", 
	[1] = "学习作画", 
	[2] = "学习礼仪",
	[3] = "切磋音律", 
	[4] = "学习射箭"
}

local g_MannerSkill1 =
{
	[0] = "称赞",
	[1] = "点头",
	[2] = "摇头",
}

local g_MannerSkill2 =
{
	[0] = "鞠躬",
	[1] = "鼓掌",
	[2] = "抱拳",
	[3] = "招手",
}

local g_MannerSkill3 =
{
	[0] = "奉茶",
	[1] = "作揖",
	[2] = "拜礼",
	[3] = "拱手",
	[4] = "执手",
}

local xInfant_DrawPos = {
    [1] = {
        [1] = {SceneID = 0, SceneName = "洛阳", PosX = 153, PosZ = 158},
        [2] = {SceneID = 0, SceneName = "洛阳", PosX = 295, PosZ = 260},
        [3] = {SceneID = 0, SceneName = "洛阳", PosX = 226, PosZ = 419},
        [4] = {SceneID = 1, SceneName = "苏州", PosX = 367, PosZ = 171},
        [5] = {SceneID = 1, SceneName = "苏州", PosX = 278, PosZ = 326},
        [6] = {SceneID = 1, SceneName = "苏州", PosX = 181, PosZ = 355},
    },
    [2] = {
        [1] = {SceneID = 2,SceneName = "大理", PosX = 70, PosZ = 49},
        [2] = {SceneID = 2,SceneName = "大理", PosX = 293, PosZ = 46},
        [3] = {SceneID = 2,SceneName = "大理", PosX = 69, PosZ = 235},
        [4] = {SceneID = 18,SceneName = "雁南", PosX = 156, PosZ = 277},
        [5] = {SceneID = 18,SceneName = "雁南", PosX = 250, PosZ = 82},
        [6] = {SceneID = 18,SceneName = "雁南", PosX = 138, PosZ = 242},
    },
    [3] = {
        [1] = {SceneID = 4,SceneName = "太湖", PosX = 168, PosZ = 189},
        [2] = {SceneID = 4,SceneName = "太湖", PosX = 62, PosZ = 74},
        [3] = {SceneID = 4,SceneName = "太湖", PosX = 100, PosZ = 175},
        [4] = {SceneID = 30,SceneName = "西湖", PosX = 124, PosZ = 50},
        [5] = {SceneID = 30,SceneName = "西湖", PosX = 246, PosZ = 193},
        [6] = {SceneID = 30,SceneName = "西湖", PosX = 154, PosZ = 132},
    },
}

local xMannerNPCTable = 
{
	[1] = {NPCName = "全小颜", PosX = 118,PosZ = 138, nSceneID=0},
	[2] = {NPCName = "曹小明", PosX = 118,PosZ = 132, nSceneID=0},
	[3] = {NPCName = "陆小祺", PosX = 127,PosZ = 132, nSceneID=0},
}

local g_NpcInfo = {
	[0] = {szNpcName="木子姐姐", fX=151, fY=187, nSceneID=0},
	[1] = {szNpcName="蔡丙臣", fX=220, fY=236, nSceneID=0},
	[2] = {szNpcName="全小颜", fX=118, fY=138, nSceneID=0},
	[3] = {szNpcName="张伯智", fX=162, fY=266, nSceneID=0},
}

function GetBuff(szBuffName)

	local nBuffCount = Player:GetBuffNumber()

	for i = 0, nBuffCount - 1 do
		local szBuffToolTips = Player:GetBuffToolTipsByIndex(i)

		if string.find(szBuffToolTips, szBuffName) then
			return i
		end
	end
	return -1
end


-- 诗
function OnMission_890511()

	MoveToNPC(g_NpcInfo[1].fX, g_NpcInfo[1].fY, g_NpcInfo[1].nSceneID, g_NpcInfo[1].szNpcName)
	QuestFrameOptionClicked("我要对诗", CMP_PART_MATCH)
	System:Sleep(1000)

	-- 枚举整个对话窗口对象
	local itemNum = DataPool:GetNPCEventList_Num()

	if itemNum == 0 then return end
	for n=1, 5 do
		for i=1, itemNum do
			QuestFrameOptionClicked(i)
			System:Sleep(600)
			if ( DataPool:IsMissionCanCommit(g_idMission) ) then
				break
			end
		end
	end
end



-- 书
function OnMission_890512()
	
	local level = g_nParam[2]
	local pos = g_nParam[3]

	MoveTo(xInfant_DrawPos[level][pos].PosX, xInfant_DrawPos[level][pos].PosZ, xInfant_DrawPos[level][pos].SceneID, "", 2)
	Dismount()
	UseItem("文房四宝", "")
	UI:WindowHide("Picture")
end

-- 礼
function OnMission_890513()

	local level = g_nParam[2]

	if GetBuff("礼仪动作") == -1 then

		MoveToNPC(xMannerNPCTable[level].PosX, xMannerNPCTable[level].PosZ, xMannerNPCTable[level].nSceneID, xMannerNPCTable[level].NPCName)
		Dismount()
		QuestFrameOptionClicked("学习礼仪", CMP_PART_MATCH)
		QuestFrameMissionClose()

	else
		
		if level == 1 then
			for i=0, 2 do
				UseSkill(g_MannerSkill1[i])
				System:Sleep(1000)
			end
		elseif level == 2 then
			for i=0, 3 do
				UseSkill(g_MannerSkill2[i])
				System:Sleep(1000)
			end
		elseif level == 3 then
			for i=0, 4 do
				UseSkill(g_MannerSkill3[i])
				System:Sleep(1000)
			end
		end
	end
end

-- 音律
function OnMission_890514()

	MoveToNPC(g_NpcInfo[3].fX, g_NpcInfo[3].fY, g_NpcInfo[3].nSceneID, g_NpcInfo[3].szNpcName)
	QuestFrameOptionClicked("切磋音律", CMP_PART_MATCH)
	System:Sleep(1000)

	-- 枚举整个对话窗口对象
	local itemNum = DataPool:GetNPCEventList_Num()
	if itemNum == 0 then return end
	for n=1, 5 do
		for i=1, itemNum do
			QuestFrameOptionClicked(i)
			System:Sleep(600)
			if ( DataPool:IsMissionCanCommit(g_idMission) ) then
				break
			end
		end
	end
end

-- 射箭
function OnMission_890515()

	MoveTo(176, 150, 0, "", 1)
	Dismount()

	while DataPool:IsMissionCanCommit(g_idMission) == false do

		local num = FindCharacterFirstByName("靶子")
		DbgPrintf("共%d个目标",num)
		for i=0, num-1 do
			local id, _,_ , x, y = FindCharacterNext()
			if id then
				local idx = Character:GetBuffNumber(id)
				DbgPrintf("射箭:%d ->buff(%d)" , id , idx)
				if idx == 1 then
					UseItem_Target("弓箭", id)
					System:Sleep(500)
					break
				end
			end
		end
		System:Sleep(1000)

	end
end


function _SMain(...)

	-- 判断是否有子女
	if Infant:GetInfantCount() == 0 then
		ShowMessage("天下父母心：还未拥有子女")
		return
	end

	while true do

		local nMaxAttr = Infant:GetInfantMaxAttr(g_CurSelInfantIndex)

		if nMaxAttr == 0 then
			ShowMessage("提升子女素养：子女能力数据错误。")
			return
		end

		local Attr = {}

		for i=0,4 do
			Attr[i] = Infant:GetInfantAttr(0, i)
			--DbgPrintf("attr[%d] = %d", i, Attr[i])
		end

		local misIndex = 0

		if Attr[0] < nMaxAttr then
			misIndex = 0
		elseif Attr[1] < nMaxAttr then
			misIndex = 1
		elseif Attr[2] < nMaxAttr then
			misIndex = 2
		elseif Attr[3] < nMaxAttr then
			misIndex = 3
		elseif Attr[4] < nMaxAttr then
			misIndex = 4
		else
			ShowMessage("提升子女五项能力任务：五项能力已满")
			System:UpdateRecord( script_name )
			break	-- 退出循环，结束脚本
		end

		--DbgPrintf("misIndex = %d", misIndex)

		g_nIndex, g_idMission, g_idScript = DataPool:GetPlayerMission(g_szMission[misIndex])

		if g_nIndex ~= -1 then

			DbgPrintf("%s Index:%d MissionID:%d ScriptID:%d", g_szMission[misIndex], 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("任务已完成")
				MoveToNPC(g_NpcInfo[0].fX, g_NpcInfo[0].fY, g_NpcInfo[0].nSceneID, g_NpcInfo[0].szNpcName)
				QuestFrameOptionClicked("综合素养提升", CMP_PART_MATCH)
				QuestFrameOptionClicked(g_szMission[misIndex], CMP_PART_MATCH)
				QuestFrameMissionContinue()
				QuestFrameMissionComplete()
				QuestFrameMissionClose()
				
			else
				-- 已接任务，未完成
				DbgPrintf("任务进行中")

				if misIndex == 0 then

					OnMission_890511()

				elseif misIndex == 1 then

					OnMission_890512()

				elseif misIndex == 2 then

					OnMission_890513()

				elseif misIndex == 3 then

					OnMission_890514()

				elseif misIndex == 4 then

					OnMission_890515()
				
				end
			end

		else
			-------------------------------------------------------------------------------------
			-- 领取任务

			-- 判断子女健康值
			InfantTreatBadStatus(g_CurSelInfantIndex)
			
			MoveToNPC(g_NpcInfo[0].fX, g_NpcInfo[0].fY, g_NpcInfo[0].nSceneID, g_NpcInfo[0].szNpcName)
			QuestFrameOptionClicked("综合素养提升", CMP_PART_MATCH)
			QuestFrameOptionClicked(g_szMission[misIndex], CMP_FULL_MATCH)
			QuestFrameAcceptClicked()
			QuestFrameMissionClose()
		end

		System:Sleep(500)
	end
end

_SMain()