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