ShowMessage("剑荡三环除妖魔 $Rev: 100 $")

--[[
[author]
class=1
attr=1
descript="默认到了大boss才会开启自动拾取。"
chk0str=不引怪
]]--

script_name="剑荡三环除妖魔"
local nSetNoFight = 0

local g_MissionMap = 651
local g_nParam = {}
local g_nIndex, g_idMission, g_idScript = DataPool:GetPlayerMission(script_name)
local g_ConfirmId = 20150402
local g_ConfirmData = {}
local g_StartFuben2 = -1
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 )
	
	if nGate == 1 then
		if g_nParam[2] < 1 or g_nParam[3] < 1 or g_nParam[7] < 60 then
			return 1
		elseif g_nParam[4] < 1 then
			return 2
		end

		return 0
	end

	GetConfirmData()

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

	if g_ConfirmData[1] ~= nGate then
		if nGate == 2 and g_ConfirmData[1] == 1 and g_ConfirmData[2] == 4 then
			return 1
		end

		return 0
	end

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

	DbgPrintf("node=%d, nCount=%d" , tonumber(node), tonumber(nCount))
 
	if nGate == 2 then 
		if node == 1 then
			if nCount < 1 then 
				return 1
			else
				return 2
			end
		elseif node == 2 then
			if nCount < 1 then
				return 2
			else
				return 3
			end
		elseif node == 3 then
			if nCount < 1 then
				return 3
			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 6
			end
		elseif node == 6 then
			if nCount < 1 then
				return 6
			else
				g_StartFuben2 = 7
				return 7
			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=154,y=179},{x=137,y=181} , {x=130,y=166} , {x=114,y=180} , {x=110,y=157} , {x=90,y=181}	
	},
	[1] = {
		{x=154,y=179},{x=137,y=181} , {x=130,y=166} , {x=114,y=180} , {x=110,y=157} ,  
	},
	[2] = {
		{x=90,y=181}
	}
}


function OnFuben1()

	local vResult = GetMissionComplete()

	if vResult ~= 0 then
		return
	end

	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)

	--AI:SetParameter("运行参数" , "组队跟随" , 1)
	--AI:SetParameter("运行参数" , "辅助攻击" , 1)
	AI:SetParameter("运行参数" , "引怪" , g_yinguai )
	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 nComplete ~= 0 then 
			break
		else
			local break_process = false 
			if g_nParam[2] == 1 and g_nParam[3] == 1 and g_nParam[4] == 1 and g_nParam[7] == 60 then
				break_process = true 
			end

			local newStep = GetMissionStep(1)
			if break_process == true or newStep ~= nPreStep then
				--完成第一关
				break
			end
		end


		g_StartFuben2 = 1
	end

	AI:Stop()

	OnPickup(1)
			
end

local g_Mission_Pos2 = {
	[0] = {{x=56,y=81},{x=79,y=67},{x=83,y=39},{x=52,y=31},{x=32,y=64},{x=55,y=56}},
	[1] = {{x=56,y=81}},
	[2] = {{x=79,y=67}},
	[3] = {{x=83,y=39}},
	[4] = {{x=52,y=31}},
	[5] = {{x=32,y=64}},
	[6] = {{x=55,y=56}},
}

local g_Mission_Name2 = {
	[0] = "未知" ,
	[1] = "撕风魔雕像" ,
	[2] = "裂地行者雕像" ,
	[3] = "五毒魔使魔雕像" ,
	[4] = "武玄将雕像" , 
	[5] = "破焰尊者雕像" ,
	[6] = "洪棘妖王雕像" 
}

function OnFuben2()

	local nPreStep = GetMissionStep(2)
	if nPreStep < 7 then
		-- 设置流程
		AI:SetAI("杀怪")
		AI:SetParameter("初始化")
		AI:SetParameter("拾取", 1)
		AI:SetParameter("地图", g_MissionMap )

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

		if nPreStep == 0 then
			if g_StartFuben2 > 0 then
				nPreStep = g_StartFuben2
			end
		elseif nPreStep ~= 0 then 
			g_StartFuben2 = nPreStep
		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("运行参数" , "清除杀怪循环次数")
	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 AI:IsStop() == true then
			break
		end

		if IsInFuben() == false then
			break
		end

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

		local nRound = AI:GetParameter("杀怪循环次数")
		DbgPrintf("当前杀怪循环数 %d" , tonumber(nRound))
		local newStep = GetMissionStep(2)
		if g_StartFuben2 > 6 then
			break
		end

		if nRound > 0 then
			--and newStep ~= nPreStep 
			if FindObjectByName( g_Mission_Name2[nPreStep] , "NPC") ~= INVALID_ID then
				g_StartFuben2 = nPreStep
				System:Sleep(500)
				AI:SetParameter("运行参数" , "清除杀怪循环次数")
			else
				if newStep > 0 then
					g_StartFuben2 = newStep
				else
					g_StartFuben2 = nPreStep + 1 
					DbgPrintf("检查位置 Round %d , newStep %d , g_StartFuben2 %d", tonumber( nRound) , tonumber(newStep) , tonumber(g_StartFuben2))
				end
					
				System:Sleep(500)
				break 
			end
		end
	end

	AI:Stop()

	OnPickup(1)			
end

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

function OnFuben3()

	GetMissionComplete()
	if g_StartFuben2 > 6 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("运行参数" ,"引怪", g_yinguai)
	AI:SetParameter("运行参数" ,"组队跟随", 0)
	--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 nComplete ~= 0 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
			g_StartFuben2 = -1
			--在外面
			if nComplete == INVALID_ID then
				-- 判断队伍人数
				if GetQuestTimesInfo("剑荡三环除妖魔") == 0 then
					ShowMessage("今日三环任务已经完成！")
					return
				end

				--ShowMessage("%s 任务未接取" , script_name )
				MoveToNPC(295 , 67 , 246 , "何悦" )

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

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

				local strType,_,_,_,szText = DataPool:GetNPCEventList_Item(0)

				if strType == "text" then
					if string.find(szText, "一天最多只可以完成") then
						break
					end
				end

			elseif nComplete == 0 then
				--队长对话npc
				MoveToNPC(295 , 67 , 246 , "何悦" )

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

				QuestFrameOptionClicked( script_name , CMP_PART_MATCH)	-- 选择选项
				QuestFrameOptionClicked( "前往炎魔山" , CMP_PART_MATCH)	-- 选择选项
				System:Sleep(2000)

				local strType,_,_,_,szText = DataPool:GetNPCEventList_Item(0)

				if strType == "text" then
					if string.find(szText , "您领取的剑荡三环除妖魔任务已经失败") then
						QuestFrameMissionAbnegate(g_idScript)
					end
				end

			elseif nComplete == 1 then

				--完成任务
				--ShowMessage("执行交任务过程")
				MoveToNPC(295 , 67 , 246 , "何悦" )
				QuestFrameOptionClicked( script_name , CMP_PART_MATCH)	-- 选择选项
				QuestFrameMissionContinue()
				QuestFrameMissionComplete()

			else

				--放弃任务
				QuestFrameMissionAbnegate(g_idScript)

			end
		else
			--在副本中
			if nComplete == 0 then
				if g_nParam[2] ~= 1 or g_nParam[3] ~= 1 or g_nParam[4] ~=1 or g_nParam[7] ~=60 then
					OnFuben1()
				elseif g_StartFuben2 < 7 then
					OnFuben2()
				else
					OnFuben3()
				end
			else
				--出副本
				MoveToNPC(209,48,g_MissionMap,"玄真子")
				QuestFrameOptionClicked( "离开副本" , CMP_PART_MATCH)	-- 选择选项
				System:Sleep(2000)
			end			
		end			

		System:Sleep(500)
	end

end

_SMain()
