ShowMessage("除匪首力破三关 $Rev: 100 $")

--[[
[author]
perday_max=1
descript="在杀完都统后，1,3,5号位会堵上面的路，其他人则堵下面的路。默认到了大boss才会开启自动拾取。不可以在野熊清完之前击杀死熊王!"
]]--

script_name="除匪首力破三关"

local g_MissionMap = 653
local g_nParam = {}
local g_nIndex, g_idMission, g_idScript = DataPool:GetPlayerMission(script_name)
local g_ConfirmId = 2015040711
local g_ConfirmData = {}

function GetConfirmData()

	for i=0,9 do
		g_ConfirmData[i] = GetConfimDataBySid(g_ConfirmId,i,"data")
		DbgPrintf("ConfirmData [%d]%d", i, g_ConfirmData[i])
	end

end

function GetMissionComplete()

	g_nIndex, g_idMission, g_idScript = DataPool:GetPlayerMission(script_name)

	if g_nIndex ~= -1 then

		DbgPrintf("%s Index:%d MissionID:%d ScriptID:%d", script_name, 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 GetMissionStep( nGate )
	
	GetConfirmData()

	DbgPrintf("Gate=%d , data1=%d" , tonumber(nGate) , tonumber(g_ConfirmData[1]))

	if g_ConfirmData[1] ~= nGate then
		return 0
	end

	local node = g_ConfirmData[2]
	local nCount = g_ConfirmData[3]

	DbgPrintf("node=%d, nCount=%d" , tonumber(node), tonumber(nCount))
 
	if nGate == 1 then 
		if node == 1 then
			if nCount < 30 then 
				return 1
			else
				return 2
			end
		elseif node == 2 then
			if nCount < 1 then
				return 2
			else
				local nIdx = GetTeamMemberIndex(Player:GetName())
				if nIdx == 1 or nIdx == 3 or nIdx == 5 then 
					return 3
				else
					return 6
				end
			end
		elseif node == 3 then
			if nCount < 6 then
				local nIdx = GetTeamMemberIndex(Player:GetName())
				if nIdx == 1 or nIdx == 3 or nIdx == 5 then 
					return 3
				else
					return 6
				end
			else
				return 4
			end
		elseif node == 4 then
			if nCount < 1 then
				return 4
			else
				return 5
			end
		elseif node == 5 then
			if nCount < 1 then
				return 5
			else
				return 0
			end
		else
			return 0
		end
	elseif nGate == 2 then
		if node == 1 then
			if nCount < 30 then
				return 1
			else
				return 2
			end
		elseif node == 2 then
			if nCount < 1 then 
				return 2
			else
				return 0
			end
		else
			return 0
		end
	elseif nGate == 3 then
		return 0
	end

	return 0
end

function IsInFuben()

	local sceneNow = GetActiveDataSceneID()
	if sceneNow ~= g_MissionMap then
		return false
	end

	return true 
end

local g_Mission_Pos1 = {

	[0] = {
   		{x=186,y=159},{x=186, y=149} , {x=197,y=144} , {x=190,y=126} , {x=184,y=109} , {x=172,y=120} , {x=169,y=136} , {x=178,y=120} , {x=199,y=105} , {x=204,y=122} , {x=194, y=54}
	},
	[1] = {
		{x=186,y=159},{x=186, y=149} , {x=197,y=144} , {x=190,y=126} , {x=184,y=109} , {x=172,y=120} , {x=169,y=136}
	},
	[2] = {
		{x=178,y=120}
	},
	[3] = {
		{x=199,y=105}
	},
	[4] = {
		{x=194, y=54}
	},
	[5] = {
		{x=194, y=54}
	},
	-- 步骤3的分支
	[6] = {
		{x=204,y=122}
	}
}


function OnFuben1()

	GetMissionComplete()

	local nPreStep = 0
	if g_nParam[1] == 0 then
		-- 设置流程
		AI:SetAI("杀怪")
		AI:SetParameter("初始化")
		AI:SetParameter("拾取", 0)
		AI:SetParameter("地图", g_MissionMap )

		nPreStep = GetMissionStep(1)
		DbgPrintf("添加坐标组 %d" , nPreStep)

		for i, pos in ipairs( g_Mission_Pos1[nPreStep]) do
			AI:SetParameter("坐标", "添加", pos.x, pos.y ) 
		end
	else
		return
	end

	AI:Start()

	if nPreStep == 3 or nPreStep == 6 then
		DbgPrintf("设置流程为 %d" , nPreStep)
		AI:SetParameter("运行参数" ,"组队跟随", 0)
		AI:SetParameter("运行参数" ,"辅助攻击", 0)
		AI:SetParameter("运行参数" ,"引怪", 1 , 1)
		AI:SetParameter("运行参数" ,"停止辅助技能", 1)
	else
		AI:SetParameter("运行参数" ,"组队跟随", 1)
		AI:SetParameter("运行参数" ,"辅助攻击", 1)
		AI:SetParameter("运行参数" ,"引怪", 1)
		AI:SetParameter("运行参数" ,"停止辅助技能", 0)
	end

	while true do 

		System:Sleep(500)

		if IsInFuben() == false then
			break
		end
		
		nComplete = GetMissionComplete()
		if nComplete ~= 0 then 
			break
		else
			if g_nParam[1] == 1 then
				break 
			end
		end

		local newStep = GetMissionStep(1)
		if newStep ~= nPreStep then
			break
		end
	end

	AI:Stop()
			
end

local g_Mission_Pos2 = {
	[0] = {{x=155,y=66},{x=147,y=66},{x=136,y=76},{x=134,y=50},{x=119,y=44},{x=103,y=50},{x=79,y=40},{x=63,y=66},{x=54,y=44}},
	[1] = {{x=155,y=66},{x=147,y=66},{x=136,y=76},{x=134,y=50},{x=119,y=44},{x=103,y=50},{x=79,y=40},{x=63,y=66},{x=54,y=44}},
	[2] = {{x=63,y=66},{x=54,y=44}}
}

function OnFuben2()

	GetMissionComplete()
	local nPreStep = 0
	if g_nParam[2] == 0 then
		-- 设置流程
		AI:SetAI("杀怪")
		AI:SetParameter("初始化")
		AI:SetParameter("拾取", 0)
		AI:SetParameter("地图", g_MissionMap )

		nPreStep = GetMissionStep(2)
		DbgPrintf("添加坐标组 %d" , nPreStep)
		if nPreStep == 1 then
			AI:SetParameter("目标" , "添加" , "野熊")
		end

		for i, pos in ipairs( g_Mission_Pos2[nPreStep]) do
			AI:SetParameter("坐标", "添加", pos.x, pos.y ) 
		end

	else
		return
	end

	AI:Start()

	AI:SetParameter("运行参数" ,"引怪", 0)
	AI:SetParameter("运行参数" ,"组队跟随", 1)
	AI:SetParameter("运行参数" , "辅助攻击" , 1)

	while true do 

		System:Sleep(500)

		if IsInFuben() == false then
			break
		end
		
		nComplete = GetMissionComplete()
		if nComplete ~= 0 then 
			break
		else
			if g_nParam[2] == 1 then
				break 
			end
		end

		local newStep = GetMissionStep(2)
		if newStep ~= nPreStep then
			break
		end
	end

	AI:Stop()
			
end

local g_Mission_Pos3 = {
	[0] = {{x=49,y=182}}
	--{x=83,y=160},{x=85,y=181},{x=63,y=174},
}

function OnFuben3()

	GetMissionComplete()
	if g_nParam[3] == 0 then
		-- 设置流程
		AI:SetAI("杀怪")
		AI:SetParameter("初始化")
		AI:SetParameter("拾取", 0)
		AI:SetParameter("地图", g_MissionMap )

		for i, pos in ipairs( g_Mission_Pos3[0]) do
			AI:SetParameter("坐标", "添加", pos.x, pos.y ) 
		end

	else
		return
	end

	AI:Start()

	local IsTeamLeader = DataPool:IsTeamLeader()

	if IsTeamLeader == false then
		AI:SetParameter("运行参数" ,"引怪", 0)
	else
		AI:SetParameter("运行参数" ,"引怪", 1)
	end
	AI:SetParameter("运行参数" ,"组队跟随", 1)
	AI:SetParameter("运行参数" , "辅助攻击" , 1)

	while true do 

		System:Sleep(500)

		if IsInFuben() == false then
			break
		end
		
		nComplete = GetMissionComplete()
		if nComplete ~= 0 then 
			if nComplete == 1 then
				--停留一段时间
				if IsMsgCold("Quest_JF_Complete" , 15000) == true then
					break
				else
					OnPickup()
				end
			else
				break
			end
		else
			UpdateMsg("Quest_JF_Complete")
		end

	end

	AI:Stop()
			
end

function _SMain(...)

	-- 判断队伍人数
	if DataPool:GetTeamMemCount() < 3 then 
		ShowMessage("队伍人数不足 3 人！")
		return
	end

	while true do

		local nComplete = GetMissionComplete() 

		if IsInFuben() == false then
			--在外面
			if nComplete == INVALID_ID then

				--ShowMessage("%s 任务未接取" , script_name )
				MoveToNPC( 134 , 261 , 1 , "钱宏宇" )
				QuestFrameOptionClicked( script_name , CMP_PART_MATCH)	-- 选择选项
				QuestFrameAcceptClicked()
				System:Sleep(2000)

				local isNext,szMsg = GetDebugMessage()
				while isNext == 1 do
					if string.find(szMsg, "接取过5次此任务") then
						System:UpdateRecord( script_name )
						return
					end
					isNext,szMsg = GetDebugMessage()
				end

			elseif nComplete == 0 then

				--队长对话npc
				MoveToNPC( 134 , 261 , 1 , "钱宏宇" )
				QuestFrameOptionClicked( "出击：三才峡谷" , CMP_PART_MATCH)	-- 选择选项
				System:Sleep(2000)

			elseif nComplete == 1 then

				--完成任务
				--ShowMessage("执行交任务过程")
				MoveToNPC( 134 , 261 , 1 , "钱宏宇" )
				QuestFrameOptionClicked( script_name , CMP_PART_MATCH)	-- 选择选项
				QuestFrameMissionContinue()
				QuestFrameMissionComplete()

			else

				--放弃任务
				QuestFrameMissionAbnegate(g_idScript)

			end
		else
			--在副本中
			if nComplete == 0 then
				if g_nParam[1] == 0 then
					OnFuben1()
				elseif g_nParam[2] == 0 then
					OnFuben2()
				elseif g_nParam[3] == 0 then
					OnFuben3()
				end
			else
				--出副本
				MoveToNPC(63,196,g_MissionMap,"宋兵接应人")
				QuestFrameOptionClicked( "离开三才峡谷" , CMP_PART_MATCH)	-- 选择选项
				QuestFrameOptionClicked( "确定离开" , CMP_PART_MATCH)	-- 选择选项
				System:Sleep(2000)
			end			
		end			

		System:Sleep(500)
	end

end

_SMain()
