ShowMessage("战盟守备任务脚本 $Rev: 1 $")

--[[
[author]
perday_max=1
]]--
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
			System:UpdateRecord( script_name )
			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

	while true do

		-- 进入帮派城市
		if CityToAlliance() == false then
			ShowMessage("未加入任何战盟")
			return
		end

		if OnInteractiveMission() == false then
			break
		end

		System:Sleep(200)
	end

	AllianceToCity()

end

_SMain()


