ShowMessage("սرű $Rev: 1 $")

--[[
[author]
class=3
]]--
script_name="սر"

local g_Map = Player:GetAllianceCityID()
local g_MissionType = {			-- 
[ 1 ] = { misName = "޲", misFun = InteractiveMission1 },
[ 2 ] = { misName = "ɣȻɫ", misFun = InteractiveMission2 },
[ 3 ] = { misName = "¯", misFun = InteractiveMission3 },
[ 4 ] = { misName = "֮Ѱ滨", misFun = InteractiveMission4 },
[ 5 ] = { misName = "Ѱ̽湦", misFun = InteractiveMission5 },

--ǧ 663
[ 6 ] = { misName = "󳵳׾", misFun = InteractiveMission1 },
[ 7 ] = { misName = "ɽͭ", misFun = InteractiveMission2 },	--ζϡ  ɽͭ  ɽͭ
[ 8 ] = { misName = "֪̽", misFun = InteractiveMission3 },
[ 9 ] = { misName = "ɽ", misFun = InteractiveMission4 },	--ҩ г  ɽ  ɽ
[ 10 ] = { misName = "Ѱ̽ǧ湦", misFun = InteractiveMission5 },

[ 11 ] = { misName = "Ļ", misFun = InteractiveMission1 },
[ 12 ] = { misName = "컷", misFun = InteractiveMission2 },	--ú   컷   xxx ٻ
[ 13 ] = { misName = "Ʊϰ", misFun = InteractiveMission3 },  --"Ҫ" ǵһѡ
[ 14 ] = { misName = "̾", misFun = InteractiveMission4 },	--ҩ г     xxxĵ  һԻѡ xxxҩ ֱӲɼ
[ 15 ] = { misName = "Ѱ̽湦", misFun = InteractiveMission5 },  

--ɽ 667
[ 16 ] = { misName = "ճɽ", misFun = InteractiveMission1 },
[ 17 ] = { misName = "Ϭľα", misFun = InteractiveMission2 },
[ 18 ] = { misName = "̨", misFun = InteractiveMission3 },
[ 19 ] = { misName = "ɽݷ", misFun = InteractiveMission4 },
[ 20 ] = { misName = "Ѱ̽湦", misFun = InteractiveMission5 },

--
[ 21 ] = { misName = "ƮȻЯ", misFun = InteractiveMission1 },
[ 22 ] = { misName = "ľǧ", misFun = InteractiveMission2 },
[ 23 ] = { misName = "ףƹľ̨", misFun = InteractiveMission3 },
[ 24 ] = { misName = "ҩѰɲ", misFun = InteractiveMission4 },
[ 25 ] = { misName = "Ѱ̽湦", misFun = InteractiveMission5 },

}

local g_MapString =
{
--
	[666] = { ["n10"] = "С" , ["n11"] = "ʼ" , ["n12"] = 891611 , ["n20"] = "ɣ" , ["n30"]="¯", ["n31"] = "ɱ" , ["n50"]="" } ,
--ɽ
	[667] = { ["n10"] = "" , ["n11"] = "ʼʿ"   , ["n12"] = 891631 , ["n20"] = "" , ["n30"]="̨", ["n31"] = "" , ["n50"]="仪" } ,
--ǧ
	[663] = { ["n10"] = ""   , ["n11"] = "ʼͿ"       , ["n12"] = 891508 , ["n20"] = "ζϡ",["n30"]=""  , ["n31"] = "" , ["n50"]="" },
--ĺ
	[665] = { ["n10"] = "" , ["n11"] = "ʼ;"   , ["n12"] = 891591 , ["n20"] = "ú" , ["n30"]="Ʊ",["n31"]= "Ӯ" , ["n50"]="ˮ" },
--
	[664] = { ["n10"] = "" , ["n11"] = "ʼйŮʷ"   , ["n12"] = 891571 , ["n20"] = "ľ" , ["n30"]="ľ̨",["n31"]= "羴֮" , ["n50"]="ʮ" }
}

-------------------------------------------------------------------------------------------------------------------

local g_nParam = {}

local g_nIndex, g_idMission, g_idScript = DataPool:GetPlayerMission(g_MissionType[1].misName)

function GetMissionComplete( nIndex )

	g_nIndex, g_idMission, g_idScript = DataPool:GetPlayerMission(g_MissionType[nIndex].misName)

	if g_nIndex ~= -1 then

		DbgPrintf("%s Index:%d MissionID:%d ScriptID:%d", g_MissionType[nIndex].misName, 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
		
		return tonumber(g_nParam[0])
	end

	return -1
end

function FindLanluNPC( uX , uY )
	
	local nMax = EnumObject(true)

	for i = 0 , nMax do 
		local id,type,name,nX,nY,title = EnumObject(false,i)
		--DbgPrintf("%s %s" , name , title)
		if title == "·" and GetDist( nX , nY , uX , uY ) < 7 then
			if type == "NPC" then
				return 0,id,name,nX,nY
			elseif type == "MON" and Character:GetData(id, "DIE") == false then
				return 1,id,name,nX,nY
			end
		end
	end

	return INVALID_ID,0,"",0,0
end

function InteractiveMission1( uIndex )

	while GetMissionComplete(uIndex) == 0 do
		
		if g_nParam[1] == 0 then

			MoveToNPC( 195 , 218 , g_Map , g_MapString[g_Map]["n10"] )
			Dismount()
			QuestFrameOptionClicked( g_MapString[g_Map]["n11"] , CMP_PART_MATCH )
			System:Sleep(1000)
			
		elseif g_nParam[2] == 0 then
			
			local szPos = GetConfimDataBySid( tonumber(g_MapString[g_Map]["n12"]) , 1 ,"text")
			DbgPrintf("%s" , tostring(szPos))
			local _,_,sDesc,sName,tX,tY = string.find( szPos , "#{([%w_]-)\042.-\042.(.-)\42.(%d+)\42.(%d+)")
			if tX ~= nil then
				tX = tonumber(tX)
			end
			if tY ~= nil then 
				tY = tonumber(tY)
			end
			if tX ~= nil and tY ~= nil and tX > 0 and tY > 0 then
				local nAction , npc_id , npc_name , npc_x , npc_y = FindLanluNPC( tX , tY )
				DbgPrintf("result %d, %d,%s,%d,%d" , nAction , npc_id , npc_name , npc_x , npc_y)
				if nAction == INVALID_ID then
					AutoRunToTarget( tX , tY , g_Map , "" )
					System:Sleep(2000)
				elseif nAction == 0 then
					DbgPrintf("nAction = 0 , tX = %d , tY = %d , name= %s" , tX , tY , tostring(npc_name))
					MoveToNPC_ById( npc_x , npc_y, g_Map , npc_id , 2 )
					Dismount()
					QuestFrameOptionClicked( "ʼս" , CMP_PART_MATCH )
					System:Sleep(1000)
				elseif nAction == 1 then
					AI:SetAI("ɱ")
					AI:SetParameter("ʼ")
					AI:SetParameter("ʰȡ", 0)
					AI:SetParameter("ͼ", g_Map )
					AI:SetParameter("", "", npc_x , npc_y ) 
					AI:SetParameter("Ŀ" , "" , npc_name ) 
					AI:Start()
					while true do 
						System:Sleep(1000)
						nAction , npc_id , npc_name , npc_x , npc_y = FindLanluNPC( tX , tY )
						if nAction ~= 1 then
							break 
						end

						if AI:IsStop() == true then
							break
						end
					end
					AI:Stop()
				end
			else
				System:Sleep(1000)
			end

		end
		
	end	
	
end


function FindSpecial2()

	local myName = Player:GetName()
	local nMax = EnumObject(true)
	local a_id = INVALID_ID
	local a_model = INVALID_ID
	local a_count = 0 
	local a_x = 0 
	local a_y = 0 
	local b_id = INVALID_ID
	local b_model = INVALID_ID
	local b_count = 0
	local b_x = 0 
	local b_y = 0 

	for i = 0 , nMax do 
		local id ,type,name,nX ,nY,title,model = EnumObject(false,i)
		--DbgPrintf("%s %s" , name , title)
		if string.find(title,myName) and type == "NPC" then
			if model == a_model then
				a_id = id 
				a_count = a_count +1 
				a_x = nX 
				a_y = nY 
			elseif model == b_model then
				b_id = id 
				b_count = b_count +1 
				b_x = nX 
				b_y = nY 
			elseif a_model == INVALID_ID then
				a_id = id
				a_count = a_count + 1 
				a_model = model 
				a_x = nX 
				a_y = nY 
			else
				b_id = id
				b_count = b_count + 1
				b_model = model
				b_x = nX 
				b_y = nY 
			end
		end
	end

	if a_count < b_count then
		return 0,a_id,a_x,a_y
	end

	if b_count == 0 then
		return 1,a_id,a_x,a_y
	end

	return 0,b_id,b_x,b_y
end


function InteractiveMission2(uIndex)
	
	while GetMissionComplete(uIndex) == 0 do

		local action,special_id ,special_x , special_y = FindSpecial2()
		DbgPrintf("result: %d, %d,%d,%d" , action,special_id ,special_x , special_y)
		if special_id == INVALID_ID then
			Dismount()
			UseItem(g_MapString[g_Map]["n20"])
			System:Sleep(2000)

			local _,sDesc, tX , tY 
			local isNext,szMsg = GetDebugMessage()
			while isNext == 1 do
				DbgPrintf("msg:%s" , tostring(szMsg))
				tX = 0 
				tY = 0
				_,_,sDesc,tX,tY= string.find( szMsg , "#{MZPVE_([%w_]-)\042.-\042.(%d+)\42.(%d+)}" )
				tX = tonumber(tX)
				tY = tonumber(tY)
				if tX ~= nil and tY ~= nil and tX > 0 and tY > 0 then
					break 
				end
				isNext,szMsg = GetDebugMessage()
			end
			if tX ~= nil and tY ~= nil and tX > 0 and tY > 0 then
				DbgPrintf("go to postion %d , %d" , tonumber(tX) , tonumber(tY))
				MoveTo( tX , tY , g_Map )
			end
		else
			if action == 0 then
				DbgPrintf("find object %d , %d , %d" , special_id , special_x , special_y )
				MoveToNPC_ById( special_x , special_y , g_Map , tonumber(special_id) )
				QuestFrameOptionClicked(1)
				System:Sleep(1000)
			else
				MoveTo(special_x , special_y , g_Map )
				Dismount()
				Player_UseSkill_Collect(3, special_id)
			end
		end

		System:Sleep(1000)
		
	end
end


function InteractiveMission3(uIndex)
	
	while GetMissionComplete(uIndex) == 0 do

		System:Sleep(1000)

		if g_nParam[1] == 0 then
			
			MoveTo( 262 , 255 , g_Map )
			Dismount()
			local uID = FindObjectByName(g_MapString[g_Map]["n30"] , "NPC")
			if uID ~= INVALID_ID then
			
				if INVALID_ID ~= CheckBufferByName( uID, "Դ¯ʹõߣͭԭ") then
					UseItem_Target("ͭԭ" , uID)
				elseif INVALID_ID ~= CheckBufferByName( uID, "Դ¯ʹõߣԭ") then
					UseItem_Target("ԭ" , uID)
				elseif INVALID_ID ~= CheckBufferByName( uID, "Դ¯ʹõߣԭ") then
					UseItem_Target("ԭ" , uID)
				elseif INVALID_ID ~= CheckBufferByName( uID, "Դ¯ʹõߣԭ") then
					UseItem_Target("ԭ" , uID)
				elseif INVALID_ID ~= CheckBufferByName( uID, "Դ¯ʹõߣѿԭ") then
					UseItem_Target("ѿԭ" , uID)

				elseif INVALID_ID ~= CheckBufferByName( uID, "ʹõߣ") then
					UseItem_Target("" , uID)
				elseif INVALID_ID ~= CheckBufferByName( uID, "ʹõߣյ") then
					UseItem_Target("յ" , uID)
				elseif INVALID_ID ~= CheckBufferByName( uID, "ʹõߣ") then
					UseItem_Target("" , uID)
				elseif INVALID_ID ~= CheckBufferByName( uID, "ʹõߣϺ") then
					UseItem_Target("Ϻ" , uID)
				elseif INVALID_ID ~= CheckBufferByName( uID, "ʹõߣҰɽ") then
					UseItem_Target("Ұɽ" , uID)

				elseif INVALID_ID ~= CheckBufferByName( uID, "Խľ̨ʹõߣ") then
					UseItem_Target("" , uID)
				elseif INVALID_ID ~= CheckBufferByName( uID, "Խľ̨ʹõߣɨ") then
					UseItem_Target("ɨ" , uID)
				elseif INVALID_ID ~= CheckBufferByName( uID, "Խľ̨ʹõߣ") then
					UseItem_Target("" , uID)
				elseif INVALID_ID ~= CheckBufferByName( uID, "Խľ̨ʹõߣٺϻ") then
					UseItem_Target("ٺϻ" , uID)
				elseif INVALID_ID ~= CheckBufferByName( uID, "Խľ̨ʹõߣ") then
					UseItem_Target("" , uID)

				elseif INVALID_ID ~= CheckBufferByName( uID, "Ʊʹõߣ") then
					UseItem_Target("" , uID)
				elseif INVALID_ID ~= CheckBufferByName( uID, "Ʊʹõߣ") then
					UseItem_Target("" , uID)
				elseif INVALID_ID ~= CheckBufferByName( uID, "Ʊʹõߣ") then
					UseItem_Target("" , uID)
				elseif INVALID_ID ~= CheckBufferByName( uID, "Ʊʹõߣ") then
					UseItem_Target("" , uID)
				elseif INVALID_ID ~= CheckBufferByName( uID, "Ʊʹõߣҩ") then
					UseItem_Target("ҩ" , uID)

				elseif INVALID_ID ~= CheckBufferByName( uID, "̨ʹõߣ") then
					UseItem_Target("" , uID)
				elseif INVALID_ID ~= CheckBufferByName( uID, "̨ʹõߣͭ") then
					UseItem_Target("ͭ" , uID)
				elseif INVALID_ID ~= CheckBufferByName( uID, "̨ʹõߣ") then
					UseItem_Target("" , uID)
				elseif INVALID_ID ~= CheckBufferByName( uID, "̨ʹõߣ") then
					UseItem_Target("" , uID)
				elseif INVALID_ID ~= CheckBufferByName( uID, "̨ʹõߣͭ") then
					UseItem_Target("ͭ" , uID)
				end
			end
		elseif g_nParam[2] == 0 then
			
			MoveToNPC( 243 , 254 , g_Map , g_MapString[g_Map]["n31"] , 2)
			QuestFrameOptionClicked(1)
			System:Sleep(1000)
		end

	end
end


function FindSpecial4()

	local myName = Player:GetName()
	local r1_string = myName.."ҩ"
	local r2_string = myName.."ĵ"
	local r3_string = myName..""
	local nMax = EnumObject(true)

	for i = 0 , nMax do 
		local id ,type,name,nX ,nY,title,model = EnumObject(false,i)
		--DbgPrintf("%s %s" , name , title)
		if type == "NPC" then
			if string.find(title,r1_string) then
				return 0,id,nX,nY
			elseif string.find(title,r2_string) then
				return 1,id,nX,nY
			elseif string.find(title,r3_string) then
				return 3,id,nX,nY
			end
		elseif type == "MON" and Character:GetData(id, "DIE") == false then
			if string.find(title,r2_string) then
				return 2,id,nX,nY
			end
		end
	end
	return 0,INVALID_ID,0,0
end

function InteractiveMission4(uIndex)

	local choose_sel = 1

	while GetMissionComplete(uIndex) == 0 do

		local action,t_id,t_x , t_y = FindSpecial4()
		if t_id == INVALID_ID then
			Dismount()
			UseItem("г")
			System:Sleep(2000)
			local isNext,szMsg = GetDebugMessage()
			while isNext == 1 do
				DbgPrintf("msg:%s" , tostring(szMsg))
				local _,_,sDesc,tX,tY= string.find( szMsg , "#{MZPVE_([%w_]-)\042.-\042.(%d+)\42.(%d+)}" )
				if tX ~= nil and tY ~= nil then
					tX = tonumber(tX)
					tY = tonumber(tY)
					if tX > 0 and tY > 0 then
						MoveTo( tX , tY, g_Map )
						break 
					end
				end

				if string.find(szMsg,"ʹҩ") then
					UseItem("ҩ")
					break
				end
				isNext,szMsg = GetDebugMessage()
			end
		else
			if action == 1 then
				DbgPrintf("find object %d , %d , %d" , special_id , special_x , special_y )
				MoveToNPC_ById( t_x , t_y , g_Map , tonumber(t_id) )
				QuestFrameOptionClicked(1)
				System:Sleep(1000)
			elseif action == 0 then
				MoveTo(t_x , t_y , g_Map )
				Dismount()
				Player_UseSkill_Collect(3, t_id)
			elseif action == 2 then
				AI:SetAI("ɱ")
				AI:SetParameter("ʼ")
				AI:SetParameter("ʰȡ", 1)
				AI:SetParameter("ͼ", g_Map )
				AI:SetParameter("Ŀ" , "" , t_id) 
				AI:Start()
				AI:SetParameter("в" , "ɱѭ")
				while true do 
					System:Sleep(1000)

					if AI:IsStop() == true then
						break
					end

					local nRound = AI:GetParameter("ɱѭ")
					DbgPrintf("ǰɱѭ %d" , tonumber(nRound))
					if nRound > 0 then
						break 
					end
				end
				AI:Stop()
				OnPickup(1)
			elseif action == 3 then
				MoveToNPC_ById( t_x , t_y , g_Map , tonumber(t_id) )
				QuestFrameOptionClicked(1)
				if choose_sel > 2 then 
					choose_sel = 1
				end
				QuestFrameOptionClicked(choose_sel)
				choose_sel = choose_sel + 1 
				System:Sleep(1000)
			end				
		end
		System:Sleep(1000)
	end
end


function InteractiveMission5(uIndex)
	
	while GetMissionComplete(uIndex) == 0 do

		MoveToNPC( 128 , 258 , g_Map , g_MapString[g_Map]["n50"] )
		Dismount()
		QuestFrameOptionClicked( "Իҩ" , CMP_PART_MATCH)
		System:Sleep(2000)

		AI:SetAI("ɱ")
		AI:SetParameter("ʼ")
		AI:SetParameter("ʰȡ", 0)
		AI:SetParameter("ͼ", g_Map )
		AI:Start()
		AI:SetParameter("в" , "ɱѭ")
				
		while true do 

			if AI:IsStop() == true then
				break
			end
				
			local nRound = AI:GetParameter("ɱѭ")
			DbgPrintf("ǰɱѭ %d" , tonumber(nRound))
			if nRound > 0 then
				break 
			end

			System:Sleep(1000)
		end
			
		AI:Stop()		
	end	

end

-------------------------------------------------------------------------------------------------------------------
local g_MissionFun = {
[ 1 ] = InteractiveMission1,
[ 2 ] = InteractiveMission2,
[ 3 ] = InteractiveMission3,
[ 4 ] = InteractiveMission4,
[ 5 ] = InteractiveMission5,

[ 6 ] = InteractiveMission1,
[ 7 ] = InteractiveMission2,
[ 8 ] = InteractiveMission3,
[ 9 ] = InteractiveMission4,
[ 10 ] = InteractiveMission5 ,

[ 11 ] = InteractiveMission1,
[ 12 ] = InteractiveMission2,
[ 13 ] = InteractiveMission3,
[ 14 ] = InteractiveMission4,
[ 15 ] = InteractiveMission5 ,

[ 16 ] = InteractiveMission1,
[ 17 ] = InteractiveMission2,
[ 18 ] = InteractiveMission3,
[ 19 ] = InteractiveMission4,
[ 20 ] = InteractiveMission5 ,

[ 21 ] = InteractiveMission1,
[ 22 ] = InteractiveMission2,
[ 23 ] = InteractiveMission3,
[ 24 ] = InteractiveMission4,
[ 25 ] = InteractiveMission5 ,
}

function OpenQuestFrame()
	if UI:IsWindowShow("CityWarDailyQuest") == true then
		return true 
	else
		MoveTo( 157 , 201, g_Map )
		local uID = FindObjectByName("ڹ" , "NPC")
		if uID ~= INVALID_ID then
			QuestFrameSpeak( uID , "" , 0 )
			-- wait 2 sec
			local dwTimeOut = System:GetTickCount() + 3000
			while System:GetTickCount() < dwTimeOut do
				if UI:IsWindowShow("CityWarDailyQuest") then 
					return true
				end
				System:Sleep(100)
			end
		end
	end
	return false	
end

function OnInteractiveMission()

	for i = 1, 25 do

		-- жǷȡ
		local nIndex, idMission, idScript = DataPool:GetPlayerMission(g_MissionType[i].misName)

		if nIndex ~= -1 then

			DbgPrintf("ѽȡ %s idx=%d id=%d script=%d", g_MissionType[i].misName , nIndex , idMission , idScript )

			-- ȡ
			for i = 0, 7 do
				g_nParam[i] = DataPool:GetPlayerMission_Variable(nIndex, i)
				DbgPrintf("[%d] = %d", i, g_nParam[i])
			end

			if g_MissionFun[i] == nil then

				ShowMessage("űûҵӦ")
				return false
			end

			if g_nParam[0] == 0 then

				g_MissionFun[i](i)

			elseif g_nParam[0] == 1 then

				DbgPrintf("") 
				if OpenQuestFrame() == true then
					DoLuaString("CityWarDailyQuest_Env" , "CityWarDailyQuest_Client1_Finish_Clicked()")
				end

			elseif g_nParam[0] == 2 then

				QuestFrameMissionAbnegate(idScript)

			end

			return true
		end
	end

	DbgPrintf("δȡ")

	if OpenQuestFrame() == true then
		DoLuaString("CityWarDailyQuest_Env" , "CityWarDailyQuest_Client1_Enter_Clicked()")
	end
	
	System:Sleep(1500)

	local isNext,szMsg = GetDebugMessage()
	while isNext == 1 do
		if string.find(szMsg, "㹻սر") then
			return false
		elseif string.find(szMsg , "ıλ") then
			return false
		end
		isNext,szMsg = GetDebugMessage()
	end
	--end

	return true
end

function _SMain(...)

	if Player:GetData("LEVEL") < 85 then
		ShowMessage("ȼ85")
		return
	end

	if PlayerPackage:GetEmptySlot(0) < 1 then
		ShowMessage("ռ䲻!")
		return
	end

	local have_quest = false
	for i = 1, 25 do
		-- жǷȡ
		local nIndex, idMission, idScript = DataPool:GetPlayerMission(g_MissionType[i].misName)
		if nIndex ~= -1 then
			have_quest = true
			break
		end
	end

	if have_quest == false then
		if GetQuestTimesInfo("սر") == 0 then
			ShowMessage("սرѾ!")
			AllianceToCity()
			return
		end
	end

	while true do

		-- ɳ
		if CityToAlliance() == false then
			ShowMessage("δκս")
			return
		end

		if OnInteractiveMission() == false then
			break
		end

		System:Sleep(200)
	end

	AllianceToCity()

end

_SMain()


