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

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

script_name="除匪首力破三关"
local nSetNoFight = 0

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 = {}
local g_yinguai = 1

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=150},{x=184,y=137},{x=203,y=137},{x=198,y=126},{x=184,y=137},{x=203,y=137},{x=192,y=121},{x=185,y=101},{x=167,y=120},{x=167,y=140},{x=183,y=127}, {x=178,y=120} , {x=199,y=105} , {x=204,y=122} , {x=194, y=54}
	},
	[1] = {
		{x=186,y=150},{x=184,y=137},{x=203,y=137},{x=198,y=126},{x=184,y=137},{x=203,y=137},{x=192,y=121},{x=185,y=101},{x=167,y=120},{x=167,y=140},{x=183,y=127}
	},
	[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 IsProcessing( val )
	if val ~= 1 and val ~= 2 then
		return true
	end
	return false
end

function OnFuben1()

	GetMissionComplete()

	local nPreStep = 0
	if g_nParam[1] == 0 then
		-- 设置流程
		AI:SetAI("杀怪")
		AI:SetParameter("初始化")
		AI:SetParameter("拾取", 1)
		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:StartEx(0)

	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("运行参数" ,"引怪", g_yinguai )
		AI:SetParameter("运行参数" ,"停止辅助技能", 0)
	end

	AI:SetParameter("运行参数" , "只拾取BOSS掉落" , 1)

	if nSetNoFight > 0 and DataPool:GetTeamMemCount() > 0 and DataPool:IsTeamLeader() == false then
		AI:SetParameter("运行参数" ,"引怪", 0 )
		AI:SetParameter("运行参数" ,"杀怪", 0 )
		AI:SetParameter("运行参数" ,"组队跟随", 1)
		AI:SetParameter("运行参数" ,"辅助攻击", 0)
		AI:SetParameter("运行参数" ,"停止辅助技能", 1)
	end

	AI:StartEx(1)

	while true do 

		System:Sleep(500)

		if IsInFuben() == false then
			break
		end
		
		nComplete = GetMissionComplete()
		if IsProcessing( nComplete ) == false then 
			break
		else
			local newStep = GetMissionStep(1)
			if g_nParam[1] == 1 or newStep ~= nPreStep then
				break
			end
		end
	end

	AI:Stop()

	OnPickup(1)			
end

local g_Mission_Pos2 = {
	[0] = {{x=136,y=80},{x=149,y=66},{x=135,y=63},{x=133,y=50},{x=125,y=41},{x=114,y=49},{x=101,y=51},{x=93,y=42},{x=80,y=46},{x=68,y=34},{x=67,y=46},{x=60,y=57},{x=70,y=63},{x=50,y=46}},
	[1] = {{x=136,y=80},{x=149,y=66},{x=135,y=63},{x=133,y=50},{x=125,y=41},{x=114,y=49},{x=101,y=51},{x=93,y=42},{x=80,y=46},{x=68,y=34},{x=67,y=46},{x=60,y=57},{x=70,y=63},{x=50,y=46}},
	[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("拾取", 1)
		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:StartEx(0)

	AI:SetParameter("运行参数" ,"引怪", g_yinguai )
	AI:SetParameter("运行参数" ,"组队跟随", 1)
	AI:SetParameter("运行参数" , "辅助攻击" , 1)
	AI:SetParameter("运行参数" , "只拾取BOSS掉落" , 1)

	if nSetNoFight > 0 and DataPool:GetTeamMemCount() > 0 and DataPool:IsTeamLeader() == false then
		AI:SetParameter("运行参数" ,"引怪", 0 )
		AI:SetParameter("运行参数" ,"杀怪", 0 )
		AI:SetParameter("运行参数" ,"组队跟随", 1)
		AI:SetParameter("运行参数" ,"辅助攻击", 0)
		AI:SetParameter("运行参数" ,"停止辅助技能", 1)
	end

	AI:StartEx(1)

	while true do 

		System:Sleep(500)

		if IsInFuben() == false then
			break
		end
		
		nComplete = GetMissionComplete()
		if IsProcessing( nComplete ) == false then 
			break
		else
			if INVALID_ID == LockNearestTarget("野熊" , CHA_TYPE_NPC|CHA_RELATION_ENEMY ) then
				local newStep = GetMissionStep(2)
				if g_nParam[2] == 1 or newStep ~= nPreStep then
					break
				end
			end
		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()

	MoveTo( 49, 182 , g_MissionMap )
	UseSkill("初级隐遁")

	GetMissionComplete()
	if g_nParam[3] == 0 then
		-- 设置流程
		AI:SetAI("杀怪")
		AI:SetParameter("初始化")
		AI:SetParameter("拾取", 1)
		AI:SetParameter("地图", g_MissionMap )
		AI:SetParameter("目标" , "添加" , "山寨大王" )

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

	else
		return
	end

	AI:StartEx(0)

	AI:SetParameter("运行参数" ,"组队跟随", 1)
	AI:SetParameter("运行参数" , "辅助攻击" , 1)
	AI:SetParameter("运行参数" , "只拾取BOSS掉落" , 1)

	if nSetNoFight > 0 and DataPool:GetTeamMemCount() > 0 and DataPool:IsTeamLeader() == false then
		AI:SetParameter("运行参数" ,"引怪", 0 )
		AI:SetParameter("运行参数" ,"杀怪", 0 )
		AI:SetParameter("运行参数" ,"组队跟随", 1)
		AI:SetParameter("运行参数" ,"辅助攻击", 0)
		AI:SetParameter("运行参数" ,"停止辅助技能", 1)
	end

	AI:StartEx(1)

	while true do 

		System:Sleep(500)

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

	end

	AI:Stop()
end

function _SMain(...)

	nSetNoFight = GetScriptParam( script_name , "attr" , SCRIPT_ATTR_NOFIGHT )
	if nSetNoFight > 0 and DataPool:IsTeamLeader() == true then
		ShowMessage("由于你是队长,因此不打怪的设置无效!")
	end

	if _CommandChk0 ~= nil and tonumber(_CommandChk0) > 0 then
		g_yinguai = 0
	end

	while true do

		local nComplete = GetMissionComplete() 

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

				if GetQuestTimesInfo("除匪首力破三关") == 0 then
					ShowMessage("今日已经昨晚力破三关任务!")
					return
				end

				--ShowMessage("%s 任务未接取" , script_name )
				MoveToNPC( 134 , 261 , 1 , "钱宏宇" )

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

				QuestFrameOptionClicked( script_name , CMP_PART_MATCH)	-- 选择选项
				QuestFrameAcceptClicked()
				System:Sleep(2000)

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

			elseif nComplete == 0 then

				MoveToNPC( 134 , 261 , 1 , "钱宏宇" )

				--队长对话npc
				if DataPool:GetTeamMemCount() < 3 then 
					ShowMessage("队伍人数不足 3 人！")
					return
				end

				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 == 1 or nComplete == 2 then
				--出副本
				MoveToNPC(63,196,g_MissionMap,"宋兵接应人")
				QuestFrameOptionClicked( "离开三才峡谷" , CMP_PART_MATCH)	-- 选择选项
				QuestFrameOptionClicked( "确定离开" , CMP_PART_MATCH)	-- 选择选项
				System:Sleep(2000)

			else
				if g_nParam[1] == 0 then
					OnFuben1()
				elseif g_nParam[2] == 0 then
					OnFuben2()
				elseif g_nParam[3] == 0 then
					MoveTo( g_Mission_Pos3[0].x , g_Mission_Pos3[0].y , g_MissionMap )
					Pet_Relax()
					UseSkill("初级隐遁")
					System:Sleep(1000)
					OnFuben3()
				end
			end			
		end			

		System:Sleep(500)
	end

end

_SMain()
