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

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


local l_nParam = {}

local l_nIndex = INVALID_ID 
local l_idMission = INVALID_ID 
local l_idScript = INVALID_ID
local l_npc = {
	[0x297] = { map_name = "ǧ" , map_id = 0x297 , npc_name = "ǳ" , npc_x = 167 , npc_y = 197 } ,
	[0x298] = { map_name = "" , map_id = 0x298 , npc_name = "精" , npc_x = 167 , npc_y = 197 } ,
	[0x299] = { map_name = "ĺ" , map_id = 0x299 , npc_name = "ˮ" , npc_x = 167 , npc_y = 197 } ,
	[0x29a] = { map_name = "" , map_id = 0x29a , npc_name = "" , npc_x = 167 , npc_y = 197 } ,
	[0x29b] = { map_name = "ɽ" , map_id = 0x29b , npc_name = "ɽ" , npc_x = 167 , npc_y = 197 } 
}

local l_CityID = Player:GetAllianceCityID()
local l_zbg_index = INVALID_ID
local l_CityID_Attack = INVALID_ID
local l_zbg = {
	[0] = { item_sid = 0x243db43 , npc_name = "ǰӪս١" , npc_x = 373 , npc_y = 370 } ,
	[1] = { item_sid = 0x243db44 , npc_name = "ǰӪս١" , npc_x = 368 , npc_y = 10 } ,
	[2] = { item_sid = 0x243db45 , npc_name = "ǰӪս١" , npc_x = 15 , npc_y = 371 } ,
	[3] = { item_sid = 0x243db46 , npc_name = "ǰӪս١" , npc_x = 9 , npc_y = 11 } 
}

function GetZbgIndex()

	for i = 0 , 3 do 
		local nCount = DataPool:UserBag_CountItemByIDTable( l_zbg[i].item_sid )
		if nCount > 0 then
			return i
		end
	end

	return INVALID_ID
end

function GetMissionComplete( mission_name )

	l_nIndex, l_idMission, l_idScript = DataPool:GetPlayerMission( mission_name )

	if l_nIndex ~= -1 then

		DbgPrintf("%s Index:%d MissionID:%d ScriptID:%d", l_mission[nIndex].misName, l_nIndex, l_idMission, l_idScript)

		-- ȡ
		for i=0,7 do
			l_nParam[i] = DataPool:GetPlayerMission_Variable(l_nIndex, i)
			DbgPrintf("[%d]%d", i, l_nParam[i])
		end
		
		return tonumber(l_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 GetDestAllianceMap()
	return tonumber(l_CityID_Attack)
end

function IsDestAllianceMap()
	if l_CityID_Attack == INVALID_ID or l_CityID_Attack ~= GetActiveDataSceneID() then
		return false
	end
	return true  
end

function OnFightFanji( dX , dY , dMap , sMission )

	AI:SetAI("ɱ")
	AI:SetParameter("ʼ")
 	AI:SetParameter("ʰȡ", 0 )
	AI:SetParameter("ͼ", dMap )
 	AI:SetParameter("", "", dX, dY , 8 ) 

	AI:StartEx(0)

	AI:SetParameter("в" ,"", 0 )
        AI:SetParameter("в" ,"ѡģʽ" , "" )
	AI:SetParameter("в" ,"ɱѭ")
	AI:SetParameter("в" ,"Ӹ", 0)

	AI:StartEx(1)

	while true do 

		System:Sleep(500)

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

		if GetActiveDataSceneID() ~= dMap then
			break
		end

		if GetMissionComplete( sMission ) ~= 0 then
			break
		end

		local nRound = AI:GetParameter("ɱѭ")

		if nRound > 0 then
			break
		end
	end

	AI:Stop()
end

function OnFightShouwei( dX , dY , dMap , dNpcName , sMission )

	AI:SetAI("ɱ")
	AI:SetParameter("ʼ")
	AI:SetParameter("ʰȡ", 0 )
	AI:SetParameter("ͼ", dMap )
 	AI:SetParameter("", "", dX, dY , 15 ) 

	AI:StartEx(0)

	AI:SetParameter( "Ŀ", "" , dNpcName )
	AI:SetParameter("в" , "ɱѭ")
	AI:SetParameter("в" ,"Ӹ", 0)

	AI:StartEx(1)

	while true do 

		System:Sleep(500)

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

		if GetActiveDataSceneID() ~= dMap then
			break
		end

		if GetMissionComplete( sMission ) ~= 0 then
			break
		end
	end

	AI:Stop()
end

function InteractiveMission1()

	local l_pos = {
		[1] = {x=139,y=85} ,
		[2] = {x=229,y=87} ,
		[3] = {x=291,y=134} ,
		[4] = {x=289,y=233} ,
		[5] = {x=230,y=290} ,
		[6] = {x=154,y=291} ,
		[7] = {x=89,y=227} ,
		[8] = {x=62,y=161} 
	}

	local nRound = 0 

	while GetMissionComplete("ǰ߲") == 0 do

		System:Sleep(1000)

		local tMap = tonumber(GetDestAllianceMap())
		if IsDestAllianceMap() == false then
			break 
		end

		local nPos = l_nParam[2]
		if l_pos[nPos] ~= nil then
			MoveTo( l_pos[nPos].x , l_pos[nPos].y , tMap )
		else
			ShowMessage("ǰ߲Ҳ %d Ӧ!" , tonumber(nPos))
			break 
		end

		if nRound > 0 then
			OnFightFanji( l_pos[nPos].x , l_pos[nPos].y , tMap , "ǰ߲")
		end

		--ʹõ
		-- Ѳʿ
		Dismount()
		UseSkill("")
		UseItem("ս")

		nRound = nRound + 1
	end	

end


function InteractiveMission2()
	
	local l_pos = {
		[1] = {x=147,y=35} ,
		[2] = {x=230,y=39} ,
		[3] = {x=342,y=150} ,
		[4] = {x=349,y=224} ,
		[5] = {x=232,y=346} ,
		[6] = {x=162,y=349} ,
		[7] = {x=33,y=232} ,
		[8] = {x=30,y=160} 
	}

	while GetMissionComplete("ǰ") == 0 do

		System:Sleep(1000)

		local tMap = tonumber(GetDestAllianceMap())
		if IsDestAllianceMap() == false then
			break 
		end

		local nPos = l_nParam[2]
		if l_pos[nPos] ~= nil then
			MoveTo( l_pos[nPos].x , l_pos[nPos].y , tMap )
		else
			ShowMessage("ǰҲ %d Ӧ!" , tonumber(nPos))
			break 
		end

		local tId,_,tX,tY = LockNearestTarget("̽" , CHA_TYPE_NPC|CHA_RELATION_FRIEND)
		if tId ~= INVALID_ID then
			MoveToNPC_ById( tX , tY , tMap , tonumber(tId) )
			QuestFrameOptionClicked(1)
			System:Sleep(1000)
		end	
	end	

end


function InteractiveMission3()
	
	local l_pos = {
		[1] = {x=312,y=62} ,
		[2] = {x=315,y=311} ,
		[3] = {x=67,y=314} ,
		[4] = {x=64,y=68} 
	}

	local nRound = 0

	while GetMissionComplete("޽") == 0 do

		System:Sleep(1000)

		local tMap = tonumber(GetDestAllianceMap())
		if IsDestAllianceMap() == false then
			break 
		end

		local nPos = l_nParam[2]
		if l_pos[nPos] ~= nil then
			MoveTo( l_pos[nPos].x , l_pos[nPos].y , tMap )
		else
			ShowMessage("޽Ҳ %d Ӧ!" , tonumber(nPos))
			break 
		end

		--ʹõ	
		local tId,_,tX,tY = LockNearestTarget("" , CHA_TYPE_NPC|CHA_RELATION_FRIEND)
		if tId ~= INVALID_ID then
			MoveTo( tX , tY , tMap )

			if nRound > 0 then
				OnFightFanji( tX , tY , tMap , "޽")
			end

			Dismount()
			UseSkill("")
			--ǷΧ"Ѳʿ"
			UseItem_Target("ľ" , tId )

			nRound = nRound + 1
		end
	end

end

function InteractiveMission4()

	local l_pos = {
		[1] = {x=159,y=66} ,
		[2] = {x=231,y=60} ,
		[3] = {x=314,y=150} ,
		[4] = {x=311,y=232} ,
		[5] = {x=222,y=312} ,
		[6] = {x=150,y=317} ,
		[7] = {x=63,y=230} ,
		[8] = {x=62,y=149} 
	}

	while GetMissionComplete("Ӧ鱨") == 0 do

		System:Sleep(1000)

		local tMap = tonumber(GetDestAllianceMap())
		if IsDestAllianceMap() == false then
			break 
		end

		local nPos = l_nParam[2]
		if l_pos[nPos] ~= nil then
			MoveTo( l_pos[nPos].x , l_pos[nPos].y , tMap )
		else
			ShowMessage("Ӧ鱨Ҳ %d Ӧ!" , tonumber(nPos))
			break 
		end

		local tId,_,tX,tY = LockNearestTarget("鱨̽" , CHA_TYPE_NPC|CHA_RELATION_FRIEND)
		if tId ~= INVALID_ID then
			MoveToNPC_ById( tX , tY , tMap , tonumber(tId) )
			QuestFrameOptionClicked("Ӧ鱨", 1)
			System:Sleep(1000)
		end		
	end	
	

end


function InteractiveMission5()
	
	local l_pos = {
		[1] = {x=82,y=134} ,
		[2] = {x=84,y=246} ,
		[3] = {x=143,y=298} ,
		[4] = {x=241,y=305} ,
		[5] = {x=287,y=244} ,
		[6] = {x=297,y=133} ,
		[7] = {x=240,y=78} ,
		[8] = {x=133,y=82} 
	}

	local nRound = 0 
	while GetMissionComplete("") == 0 do

		System:Sleep(1000)

		local tMap = tonumber(GetDestAllianceMap())
		if IsDestAllianceMap() == false then
			break 
		end

		local nPos = l_nParam[2]
		if l_pos[nPos] ~= nil then
			MoveTo( l_pos[nPos].x , l_pos[nPos].y , tMap )
		else
			ShowMessage("Ҳ %d Ӧ!" , tonumber(nPos))
			break 
		end

		local tId,_,tX,tY = LockNearestTarget("" , CHA_TYPE_NPC|CHA_RELATION_FRIEND)
		if tId ~= INVALID_ID then
			MoveTo( tX , tY , tMap )

			if nRound > 0 then
				OnFightFanji( tX , tY , tMap , "")
			end
		
			Dismount()
			UseSkill("")
			--ǷΧ"Ѳʿ"
			UseItem_Target("" , tId )
			nRound = nRound + 1
		end
	end

end

function InteractiveMission6()
	
	local l_pos = {
		[1] = {x=78,y=162} ,
		[2] = {x=80,y=241} ,
		[3] = {x=157,y=299} ,
		[4] = {x=223,y=297} ,
		[5] = {x=300,y=220} ,
		[6] = {x=299,y=157} ,
		[7] = {x=230,y=80} ,
		[8] = {x=146,y=83} 
	}

	local nRound = 0 

	while GetMissionComplete("Ȱ") == 0 do

		System:Sleep(1000)

		local tMap = tonumber(GetDestAllianceMap())
		if IsDestAllianceMap() == false then
			break 
		end

		local nPos = l_nParam[2]
		if l_pos[nPos] ~= nil then
			MoveTo( l_pos[nPos].x , l_pos[nPos].y , tMap )
		else
			ShowMessage("ȰҲ %d Ӧ!" , tonumber(nPos))
			break 
		end


		if nRound > 0 then
			OnFightFanji( l_pos[nPos].x , l_pos[nPos].y , tMap , "Ȱ")
		end
			
		Dismount()
		UseSkill("")
		UseItem("Ȱ")
		nRound = nRound + 1	
	end

end

function InteractiveMission7()
	
	local l_pos = {
		[1] = {x=299,y=189, npc = ""} ,
		[2] = {x=77,y=180,  npc = ""} ,
		[3] = {x=190,y=300, npc = ""} ,
		[4] = {x=189,y=78,  npc = ""}
	}

	while GetMissionComplete("ɱ") == 0 do

		System:Sleep(1000)

		local tMap = tonumber(GetDestAllianceMap())
		if IsDestAllianceMap() == false then
			break 
		end

		local nPos = l_nParam[2]
		if l_pos[nPos] ~= nil then
			MoveTo( l_pos[nPos].x , l_pos[nPos].y , tMap )
			OnFightShouwei( l_pos[nPos].x , l_pos[nPos].y , tMap , l_pos[nPos].npc , "ɱ" )
		else
			ShowMessage("ɱҲ %d Ӧ!" , tonumber(nPos))
			break 
		end
	end

end

local l_mission = 
{
	[1] = { misName = "ǰ߲" , misFun = InteractiveMission1 } ,
	[2] = { misName = "ǰ" , misFun = InteractiveMission2 } ,
	[3] = { misName = "޽" , misFun = InteractiveMission3 } ,
	[4] = { misName = "Ӧ鱨" , misFun = InteractiveMission4 } ,
	[5] = { misName = "" , misFun = InteractiveMission5 } ,
	[6] = { misName = "Ȱ" , misFun = InteractiveMission6 } ,
	[7] = { misName = "ɱ" , misFun = InteractiveMission7 } 
}

function FindMission()

	for i = 1, 7 do

		-- жǷȡ
		if GetMissionComplete(l_mission[i].misName) ~= -1 then

			DbgPrintf("ѽȡ %s idx=%d id=%d script=%d", l_mission[i].misName , l_nIndex , l_idMission , l_idScript )
			return i
		end

	end

	return INVALID_ID
end

function GetZbInfo( SelfCity )

	DataPool:SendAskAboutXZInfo(1)

	local nTick = 0 

	while true do 
		
		System:Sleep(1000)
		local vGuild , vAttack , vDefend = DataPool:GetXZInfo()
		if vGuild ~= INVALID_ID then
			local vGuild_City = Player:GetAllianceCityID( vGuild )
			if vGuild_City == SelfCity then
				return Player:GetAllianceCityID( vAttack )
			end
			break
		else
			PushDebugMessage("ڼսϢ" , 1)
		end

		nTick = nTick + 1

		if nTick > 10 then
			break 
		end
	end

	return INVALID_ID
end

function _SMain(...)

	l_CityID = Player:GetAllianceCityID()
	if l_CityID == INVALID_ID then
		ShowMessage("δκս")
		return
	end

	local bFinish = false

	while true do

		if Player:IsKvkServer() == true and l_CityID_Attack == INVALID_ID then
			l_CityID_Attack = GetZbInfo( l_CityID )

			if UI:IsWindowShow("KVKLeague_War") == true then
				UI:WindowHide()
			end

			if l_CityID_Attack == INVALID_ID then
				ShowMessage("ûмӦַĿ,ֹսű")
				break
			end
		end

		l_zbg_index = GetZbgIndex()

		if l_zbg[l_zbg_index] ~= nil then

			local nowScene = GetActiveDataSceneID()
			local dMap = GetDestAllianceMap()

			if nowScene == l_CityID then

				if bFinish == false then
					MoveToNPC( l_npc[l_CityID].npc_x , l_npc[l_CityID].npc_y , l_npc[l_CityID].map_id , l_npc[l_CityID].npc_name )
					QuestFrameOptionClicked("͵ǰӪ" , 1)
				else
					break
				end

			elseif nowScene == dMap then
				
				local vIndex = FindMission()

				if vIndex == INVALID_ID then
					--û
					MoveToNPC( l_zbg[l_zbg_index].npc_x , l_zbg[l_zbg_index].npc_y , dMap , l_zbg[l_zbg_index].npc_name )
					QuestFrameOptionClicked("ս" , 1)
					QuestFrameAcceptClicked()

					local isNext,szMsg = GetDebugMessage()
					while isNext == 1 do
						if string.find(szMsg, "ȡ5ս") then
							bFinish = true
							break 
						end
						isNext,szMsg = GetDebugMessage()
					end

					if bFinish == true then
						MoveToNPC( l_zbg[l_zbg_index].npc_x , l_zbg[l_zbg_index].npc_y , dMap , l_zbg[l_zbg_index].npc_name )
                                                QuestFrameOptionClicked("ͻս" , 1)
					end
					--ͻս
				elseif l_nParam[0] == 0 then
					--
					l_mission[vIndex].misFun()
				elseif l_nParam[0] == 1 then
					MoveToNPC( l_zbg[l_zbg_index].npc_x , l_zbg[l_zbg_index].npc_y , dMap , l_zbg[l_zbg_index].npc_name )
					QuestFrameOptionClicked("ս" , 1)
					QuestFrameMissionComplete()
				else
					QuestFrameMissionAbnegate(l_idScript)
				end
			else
				--ǰ
				CityToAlliance()
			end

		else
			CityToAlliance()
			
			if l_npc[l_CityID] ~= nil then
				MoveToNPC( l_npc[l_CityID].npc_x , l_npc[l_CityID].npc_y , l_npc[l_CityID].map_id , l_npc[l_CityID].npc_name )
				QuestFrameOptionClicked("ȡ" , 1)
			end	
	
		end

		System:Sleep(1000)
	end

	AllianceToCity()

end

_SMain()
