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