--[[
[author]
class=1
descript="自动接取当日虚空任务（大理杀星、四绝庄、缥缈峰、燕子坞），并自动调用相应脚本来完成副本。副本设置请直接在相应脚本上进行设置。缥缈峰对应的是【特殊副本】飘渺峰(小) 的设置。"
]]--


local l_XukongMission = 
{
	[0] = {name = "挑战虚空杀星" , vin = "挑战虚空杀星" , nFubenMap = 546 , subScript = "【特殊副本】大理杀星.txt" , subParam = "【特殊副本】大理杀星" } ,
	[1] = {name = "挑战虚空四绝庄" , vin = "挑战虚空四绝庄"  , nFubenMap = 291 , subScript = "【特殊副本】四绝庄.txt" , subParam = "【特殊副本】四绝庄"} ,
	[2] = {name = "挑战虚空缥缈峰" , vin = "挑战虚空缥缈峰" , nFubenMap = 261 , subScript = "【特殊副本】飘渺峰(小).txt" , subParam = "【特殊副本】飘渺峰(小)"} ,
	[3] = {name = "挑战虚空燕子坞" , vin = "挑战虚空燕子坞" , nFubenMap = 111 , subScript = "【特殊副本】燕子坞.txt" , subParam = "【特殊副本】燕子坞"}
}

local l_XukongPos =
{
	[0] = {name = "黄云禅" , mapid = 0 , x = 219 , y = 245 } ,
	[1] = {name = "金忆风" , mapid = 2 , x = 206 , y = 56 } ,
	[2] = {name = "独孤悔" , mapid = 1 , x = 200 , y = 334} ,
	[3] = {name = "金睿凡" , mapid = 246 , x = 163 , y = 153}
}

local function IsXukongFuben()

	local nowMap =  GetActiveDataSceneID()
	for i = 0 , 3 do
		if nowMap == l_XukongMission[i].nFubenMap then
			return i
		end
	end
	return INVALID_ID

end

local function GetXukongComplete()
	
	for i = 0 , 3 do
		local nIndex, idMission, idScript = DataPool:GetPlayerMission( l_XukongMission[i].name )

		if nIndex == -1 then
			continue 
		end

		DbgPrintf("%s Index:%d MissionID:%d ScriptID:%d", l_XukongMission[i].name , nIndex, idMission, idScript)

		local nParam = {}
		-- 取得任务参数
		for i=0,7 do
			nParam[i] = DataPool:GetPlayerMission_Variable( nIndex , i)
		end
		
		return i , tonumber(nParam[0]) , idScript
	end

	return INVALID_ID , 0 , 0 
end

g_XukongRet = 0
local function ExeScript( vIndex )
	g_XukongRet = 0
	System:LoadScript( l_XukongMission[vIndex].subScript , l_XukongMission[vIndex].subParam , "虚空任务" )

	return g_XukongRet
end

local function _breaxukong_kmove( fX , fY , nScene )
	if IsXukongFuben() == INVALID_ID then
		return true 
	end
	return false
end

local function MoveToXukongNPC()
	local nowMap =  GetActiveDataSceneID()

	local vPos = 0 
	for i = 0 , 3 do 
		if nowMap == l_XukongPos [i].mapid then
			vPos = i
			break
		end
	end

	MoveToNPC( l_XukongPos [vPos].x , l_XukongPos [vPos].y , l_XukongPos [vPos].mapid , l_XukongPos [vPos].name , true , true , _breaxukong_kmove )			
end

local l_today_mission = ""

local function AcceptXukongMission( vIndex )

	MoveToXukongNPC()

	local sOpt1 = tostring(GetQuestFrameOption(1))
	if string.find( sOpt1 , "挑战虚空") then
	else
		sOpt1 = ""
	end

	local vSel = ""

	if vIndex ~= INVALID_ID then
	
		local sOpt2 = tostring(GetQuestFrameOption(2))
		if string.find( sOpt2 , "挑战虚空") then
		else
			sOpt2 = ""
		end

		if sOpt1 == l_XukongMission[vIndex].vin then
			if sOpt2 ~= "" then
				vSel = sOpt2
			else
				vSel = sOpt1
			end
		elseif sOpt2 == l_XukongMission[vIndex].vin then
			if sOpt1 ~= "" then
				vSel = sOpt1
			else
				vSel = sOpt2
			end
		end
	else
		vSel = sOpt1
	end

	if vSel ~= "" then
		l_today_mission = vSel
		QuestFrameOptionClicked( vSel , CMP_FULL_MATCH)
		QuestFrameAcceptClicked()		
	end

end

local function _XuKong()

	local vGet = INVALID_ID
	--是否要放弃当前任务
	local vFangqi = false

	vGet = GetQuestTimesInfo("虚空任务")
	if vGet == 1 then
		vFangqi = true
	end
	
	while true do

		System:Sleep(1000)

		local vFuben = IsXukongFuben()

		if vFuben ~= INVALID_ID then
			if ExeScript( vFuben ) ~= 0 then
				break
			end
		else
			local vXukongPos , vDone , vIdScript = GetXukongComplete()

			if vXukongPos ~= INVALID_ID then

				if vDone == 1 then

					MoveToXukongNPC()
					QuestFrameOptionClicked( l_XukongMission[vXukongPos].name , CMP_FULL_MATCH)
					QuestFrameMissionComplete()	-- 完成	

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

					if strType == "text" then
						if string.find(szText , "空位不足") then
							ShowMessage("背包空格不足,暂停脚本") 
							break
						end
					end

				elseif vDone == 0 then
					if vFangqi == false then
						--当前任务就是虚空任务
						if ExeScript( vXukongPos ) ~= 0 then
							break
						end
					else
						--任务已经过期
						ShowMessage("任务 %s 已经过期,自动放弃" , tostring(l_XukongMission[vXukongPos].name))
						QuestFrameMissionAbnegate(vIdScript)
					end
				else
					QuestFrameMissionAbnegate(vIdScript)
				end
			else	
				if vGet == 0 then
					ShowMessage("今日已经完成虚空任务")
					break
				elseif vGet ~= INVALID_ID then

					vFangqi = false

					AcceptXukongMission(INVALID_ID)

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

					if strType == "text" then
						if string.find(szText, "明天再来") then
							ShowMessage("今日已经完成虚空任务！")
							break	-- 退出循环，结束脚本
						end
					end
				end			
			end
		end
	end
			
end

_XuKong()