ShowMessage("天龙幻境脚本 $Rev: 1 $")

--[[
[author]
class=1
]]--


local script_name="天龙幻境试心魔"

local g_MissionMap = 300
local g_nIndex, g_idMission, g_idScript = DataPool:GetPlayerMission(script_name)

local StepKill = {{x=63,y=39,kc=30},{x=88,y=40,kc=60},{x=100,y=64,kc=90},{x=88,y=88,kc=120},{x=64,y=100,kc=150},{x=40,y=88,kc=180},{x=30,y=64,kc=210},{x=40,y=40,kc=239}, {x=56,y=56,kc=240}}

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 OnFighting()

	AI:SetAI("杀怪")
	AI:SetParameter("初始化")
	AI:SetParameter("地图", g_MissionMap )
	AI:SetParameter("拾取" , 1)
	if Player:IsRangeAttack() == true then
		AI:SetParameter("运行参数" ,"引怪", 1)
	end
	--AI:SetParameter("运行参数" ,"组队跟随", 1)
	AI:SetParameter("运行参数" ,"辅助攻击", 1)

	for i, v in pairs( StepKill ) do
		AI:SetParameter("坐标", "添加", v.x, v.y , 25 , v.kc )
	end

	AI:Start(false)

	while true do 

		System:Sleep(1000)

		if GetActiveDataSceneID() ~= g_MissionMap then
			break 
		end

		local nComplete = GetMissionComplete()
		if 0 ~= nComplete then
			if nComplete == 1 then
				--停留一段时间
				if IsMsgCold("Quest_TLHJ_Complete" , 5000) == true then
					break
				end
			else
				break
			end
		else
			UpdateMsg("Quest_TLHJ_Complete")
		end
	end

	AI:Stop()
end

function _SMain(...)

	while true do

		local nComplete = GetMissionComplete() 

		if GetActiveDataSceneID() ~= g_MissionMap then
			--在外面
			if nComplete == INVALID_ID then

				if GetQuestTimesInfo("天龙幻境") == 0 then
					ShowMessage("今日天龙幻境任务已做完!")
					return
				end

				if DataPool:IsTeamLeader() and DataPool:UserBag_CountItemByName("试炼令牌") <= 0 then
					ShowMessage("身上已经没有 试炼令牌 了!")
					return
				end

				MoveToNPC( 171 , 119 , 246 , "浮生" )


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

				QuestFrameOptionClicked( script_name , CMP_PART_MATCH)	-- 选择选项

				local strType,_,_,_,szText = DataPool:GetNPCEventList_Item(0)
				if strType == "text" then
					if string.find(szText, "天龙幻境达6次") then
						ShowMessage("本周天龙幻境已经完成！")
						break	-- 退出循环，结束脚本
					end
				end

				QuestFrameAcceptClicked()
				System:Sleep(2000)

			elseif nComplete == 0 then

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

				--队长对话npc
				MoveToNPC( 171 , 119 , 246 , "浮生" )
				QuestFrameOptionClicked( "试炼：天龙幻境" , CMP_PART_MATCH)	-- 选择选项
				System:Sleep(2000)

			elseif nComplete == 1 then

				--完成任务
				--ShowMessage("执行交任务过程")
				MoveToNPC( 171 , 119 , 246 , "浮生" )
				QuestFrameOptionClicked( script_name , CMP_PART_MATCH)	-- 选择选项
				QuestFrameMissionContinue()
				QuestFrameMissionComplete()

			else
				--放弃任务
				QuestFrameMissionAbnegate(g_idScript)

			end
		else
			--在副本中
			if nComplete == 0 then
				local LockId = LockNearestTarget("",CHA_TYPE_NPC|CHA_RELATION_ENEMY )
				if INVALID_ID == LockId and DataPool:IsTeamLeader() == true then
					MoveToNPC(66,58,g_MissionMap,"若梦")
					local sSelect = GetQuestFrameOption(1)
					if sSelect == "离开试炼：天龙幻境" then
						OnFighting()
					elseif sSelect == "召唤天龙八部幻象" then
						QuestFrameOptionClicked( "召唤天龙八部幻象" , CMP_PART_MATCH)	-- 选择选项
						QuestFrameOptionClicked( "确定召唤" , CMP_PART_MATCH)	-- 选择选项
						System:Sleep(1000)
					else
						System:Sleep(500)	
					end
				else
					OnFighting()
				end
			else
				--出副本
				MoveToNPC(66,58,g_MissionMap,"若梦")
				QuestFrameOptionClicked( "离开试炼：天龙幻境" , CMP_PART_MATCH)	-- 选择选项
				QuestFrameOptionClicked( "确定" , CMP_PART_MATCH)	-- 选择选项
				System:Sleep(2000)
			end			
		end			

		System:Sleep(500)
	end

end

_SMain()
