ShowMessage("飘渺峰脚本 $Rev: 212 $")

--[[
[author]
]]--

script_name="初战飘渺峰"
local g_MissionMap = 261
local g_VirtualBoss = "非法目标"
local pre_state = "至阴至柔" 

local fuben_process_do = 0
local fuben_process_chk = 0 

--节点： 哈霸大-0 桑土公-1 乌老大-2 不平道人-3 李秋水-4

local g_boss = {
	["哈大霸"] = INVALID_ID ,
	["桑土公"] = INVALID_ID ,
	["乌老大"] = INVALID_ID ,
	["不平道人"] = INVALID_ID ,
	["卓不凡"] = INVALID_ID ,
	["任平生"] = INVALID_ID ,
	["李秋水"] = INVALID_ID ,
	["哈大霸NPC"] = INVALID_ID ,
	["桑土公NPC"] = INVALID_ID ,
	["乌老大NPC"] = INVALID_ID ,
	["不平道人NPC"] = INVALID_ID ,
	["卓不凡NPC"] = INVALID_ID ,
	["任平生NPC"] = INVALID_ID ,
	["李秋水NPC"] = INVALID_ID ,
	["符敏仪NPC"] = INVALID_ID , 
	["碧磷僵尸"] = INVALID_ID , 
	["非法目标"] = INVALID_ID 
}

local g_Process =
{
	[0] = { npc="哈大霸" , x=124 , y=86 , fight_boss="哈大霸" } ,
	[1] = { npc="桑土公" , x=41 , y=105 , fight_boss="桑土公" } ,
	[2] = { npc="乌老大" , x=116 , y=49 , fight_boss="乌老大"} ,
	[3] = { npc="符敏仪" , x=158 , y=54 , fight_boss="不平道人"} ,
	[4] = { npc="乌老大" , x=96 , y=40  , fight_boss ="李秋水"} ,
	[5] = { npc="乌老大" , x=96 , y=40  , fight_boss =""}
--挑战不平道人和卓不凡
-- 125,33 不平道人 卓不凡 任平生
-- 98,38 对话 乌老大 ， 选项 决战李秋水？
-- 120 41 对付李秋水
-- 对话乌老大 离开飘渺峰 确认离开
}


function UpdateBossInfo()

	g_boss["哈大霸"] = INVALID_ID 
	g_boss["桑土公"] = INVALID_ID 
	g_boss["乌老大"] = INVALID_ID 
	g_boss["不平道人"] = INVALID_ID 
	g_boss["卓不凡"] = INVALID_ID 
	g_boss["任平生"] = INVALID_ID 
	g_boss["李秋水"] = INVALID_ID 
	g_boss["哈大霸NPC"] = INVALID_ID 
	g_boss["桑土公NPC"] = INVALID_ID 
	g_boss["乌老大NPC"] = INVALID_ID 
	g_boss["不平道人NPC"] = INVALID_ID 
	g_boss["卓不凡NPC"] = INVALID_ID 
	g_boss["任平生NPC"] = INVALID_ID 
	g_boss["李秋水NPC"] = INVALID_ID 
	g_boss["符敏仪NPC"] = INVALID_ID 
	g_boss["碧磷僵尸"] = INVALID_ID 
	g_boss["非法目标"] = INVALID_ID 

	
	local n = EnumObject(true)
	local pre_do = fuben_process_do

	for i=0, n - 1 do
		local idObj, classObj, nameObj, xObj, yObj = EnumObject(false, i)
		if idObj ~= INVALID_ID then
			if nameObj == "哈大霸" then
				if classObj == "MON" then
					if Character:GetData(idObj, "DIE") == false then
						g_boss["哈大霸"] = idObj
						fuben_process_do = 0 
					else
						fuben_process_do = 1
					end
				elseif classObj == "NPC" then
					g_boss["哈大霸NPC"] = idObj
					fuben_process_do = 0
				end
			elseif nameObj == "桑土公" then 
				if classObj == "MON" then
					if Character:GetData(idObj, "DIE") == false then
						g_boss["桑土公"] = idObj
						fuben_process_do = 1
					else
						fuben_process_do = 2
					end
				elseif classObj == "NPC" then
					g_boss["桑土公NPC"] = idObj

					if fuben_process_do > 1 then
						fuben_process_do = 0
					end
				end
			elseif nameObj == "乌老大" then 
				if classObj == "MON" then
					if Character:GetData(idObj, "DIE") == false then
						g_boss["乌老大"] = idObj
						fuben_process_do = 2
					else
						fuben_process_do = 3 
					end
				elseif classObj == "NPC" then
					g_boss["乌老大NPC"] = idObj
				end
			elseif nameObj == "不平道人" then
				if classObj == "MON" then
					if Character:GetData(idObj, "DIE") == false then
						g_boss["不平道人"] = idObj
						fuben_process_do = 3 
					else
						if fuben_process_do < 3 then
							fuben_process_do = 3 
						end
					end
				elseif classObj == "NPC" then
					g_boss["不平道人NPC"] = idObj
				end
			elseif nameObj == "卓不凡" then
				if classObj == "MON" then
					if Character:GetData(idObj, "DIE") == false then
						g_boss["卓不凡"] = idObj
						fuben_process_do = 3
					else
						if fuben_process_do < 3 then
							fuben_process_do = 3 
						end
					end
				elseif classObj == "NPC" then
					g_boss["卓不凡"] = idObj
				end
			elseif nameObj == "任平生" then
				if classObj == "MON" then
					if Character:GetData(idObj, "DIE") == false then
						g_boss["任平生"] = idObj
						fuben_process_do = 3
					else
						fuben_process_do = 4
					end
				elseif classObj == "NPC" then
					g_boss["任平生NPC"] = idObj
				end
			elseif nameObj == "李秋水" then
				if classObj == "MON" then
					if Character:GetData(idObj, "DIE") == false then
						g_boss["李秋水"] = idObj
						fuben_process_do = 4
					else
						fuben_process_do = 5 
					end
				elseif classObj == "NPC" then
					g_boss["李秋水NPC"] = idObj
				end
			elseif nameObj == "碧磷僵尸" and Character:GetData(idObj, "DIE") == false then
				g_boss["碧磷僵尸"] = idObj
				fuben_process_do = 1
				--AI:SetAdv("增加威胁对象" , idObj , 4 )
			elseif nameObj == "符敏仪" and classObj == "NPC" then
				g_boss["符敏仪NPC"] = idObj
			end
		end
	end

	--让队员提前做好准备
	if IsMsgCold("Quest_PM_VirtualBoss" , 10000) == false then
		if g_VirtualBoss ~= "非法目标" and g_boss[g_VirtualBoss] == INVALID_ID then
			g_boss[g_VirtualBoss] = INVALID_ID -1 
			DbgPrintf("生成虚拟boss %s" , tostring(g_VirtualBoss))
		end
	else
		g_VirtualBoss = "非法目标"
	end

	if pre_do ~= fuben_process_do then
		fuben_process_chk = fuben_process_do
	end
end

-- 0,跳过  1,战斗  2,处理相关事务
function CheckProcess()

	UpdateBossInfo()
	
	if g_boss["哈大霸"] ~= INVALID_ID then
		return 1,0
	elseif g_boss["桑土公"] ~= INVALID_ID or g_boss["碧磷僵尸"] ~= INVALID_ID then
		return 1,1
	elseif g_boss["乌老大"] ~= INVALID_ID then
		return 1,2
	elseif g_boss["卓不凡"] ~= INVALID_ID or g_boss["不平道人"] ~= INVALID_ID or g_boss["任平生"] ~= INVALID_ID then
		return 1,3
	elseif g_boss["李秋水"] ~= INVALID_ID then
		return 1,4
	end

	if fuben_process_do > 4 then
		return 3,5
	end

	if g_boss["哈大霸NPC"] ~= INVALID_ID then
		return 2,0
	elseif g_boss["桑土公NPC"] ~= INVALID_ID then
		return 2,1
	elseif g_boss["乌老大NPC"] ~= INVALID_ID then
		local npc_id , npc_x , npc_y = FindObjectByID( g_boss["乌老大NPC"], "NPC" )
		if GetDist( npc_x , npc_y , 95 , 39 ) < 3 then
			return 2,4
		end
		return 2,2
	elseif g_boss["符敏仪NPC"] ~= INVALID_ID then
		return 2,3
	end
	
	return 0,0
end

function OnProcessBoss( nProcess_do )

	local pos , menpai = GetTeamStandPos()
	AI:SetAI("高级战斗") 
	AI:SetParameter("地图" , g_MissionMap)

	local main_x , main_y = Player:GetPos()
	local main_r = 5
	local bRangeAttack = Player:IsRangeAttack()

	if nProcess_do == 0 then
		if pos ~= 1 then
			main_x = 114
			main_y = 94
			main_r = 5
		else
			main_x = 122
			main_y = 94
			main_r = 9
		end
	elseif nProcess_do == 1 then
		main_x = 47
		main_y = 115
		main_r = 8
	elseif nProcess_do == 2 then
		if pos == 1 then 
			main_x = 111
			main_y = 49
			main_r = 6
		else
			main_x = 110
			main_y = 54
			main_r = 6
		end
	elseif nProcess_do == 3 then
		main_x = 127
		main_y = 35
		main_r = 6
	elseif nProcess_do == 4 then
		main_x = 125
		main_y = 40
		main_r = 3
	end

	AI:SetAdv("主防御圈" , main_x , main_y , main_r )
	AI:Start()
	Dismount()
	local nPos = 0 

	while true do

		local bPickUp = 1
		local bKillMonster = 1
		local bYinGuai = 0
		local bTeamFollow = 0 
		local bTeamAttack = 0 
		local bStopAssistSelf = 0 
		local bStopAssist = 0
		local bStopSpecialAttack = 0 
		local bCallPet = 1
		local bYinshen = 0 
		local bFixPlace = 0
		local bSpeedUp = 0

		local FingerAttack = -1
		local FingerAttackName = ""
		local FollowTarget = -1

		System:Sleep(300)

		-- 启动了血迹 且 人物当前蓝少于 70% 时，允许召唤宠物
		-- 启动了共生 且 人物当前血少于 70% 时，允许召唤宠物
		local nHPPercent = Player:GetData("HP_PERCENT")
		local nMPPercent = Player:GetData("MP_PERCENT")
		if nHPPercent < 70 then
			if IsSkillSetUse(686) or IsSkillSetUse(687) then
				bCallPet = 1
			end
		end

		if nMPPercent < 70 then
			if IsSkillSetUse(696) or IsSkillSetUse(697) then
				bCallPet = 1
			end
		end	

		local nType , nStep = CheckProcess()

		UpdateBossInfo()

		if IsMsgCold("Quest_PM_lqs" , 15000 ) == false then

			local main_x = 125
			local main_y = 40
			if pre_state == "至阴至柔" then
				main_x = 114
				main_y = 28
			elseif pre_state == "刺骨的寒冷" then
				main_x = 136
				main_y = 28
			elseif pre_state == "腐骨之毒" then
				main_x = 147
				main_y = 48
			elseif pre_state == "烈焰的灼烧" then
				main_x = 135
				main_y = 66
			elseif pre_state == "五 雷 轰 顶" then
				main_x = 114
				main_y = 66
			else
				--"至刚至阳"
				main_x = 102
				main_y = 47
			end

			AI:SetAdv( "主防御圈" , tonumber(main_x) , tonumber(main_y) , 2 )
			UpdateMsg("Quest_PM_Complete")

			bPickUp = 0
			bKillMonster = 0 
			bCallPet = 0
			bFixPlace = 1
			bSpeedUp = 1

			local nHpPercent = Player:GetData("HP_PERCENT")
			if INVALID_ID == Player:GetBuffByName("剑舞免疫") then
				bStopAssist = 1
				bStopAssistSelf = 1

				if menpai == MENPAI_EMEI and nHpPercent < 60 then
					if INVALID_ID == Player:GetBuffByName("呆若木鸡") then
						AI:SetParameter("运行参数" , "使用技能" , "呆若木鸡" , "自身辅助" )
					end
				end
			end
		else

			-- 设置战斗地点
			local main_x , main_y = Player:GetPos()
			local main_r = 5

			if nProcess_do == 0 then
				if pos ~= 1 then
					main_x = 114
					main_y = 94
					main_r = 5
				else
					main_x = 122
					main_y = 94
					main_r = 9
				end
			elseif nProcess_do == 1 then
				main_x = 47
				main_y = 115
				main_r = 8
			elseif nProcess_do == 2 then
				if pos == 1 then 
					main_x = 111
					main_y = 49
					main_r = 6
				else
					main_x = 110
					main_y = 54
					main_r = 6
				end
			elseif nProcess_do == 3 then
				main_x = 127
				main_y = 35
				main_r = 6
			elseif nProcess_do == 4 then
				main_x = 125
				main_y = 40
				main_r = 3
			end

			AI:SetAdv("主防御圈" , main_x , main_y , main_r )

			--根据不同boss进行不同的策略

			if g_boss["哈大霸"] ~= INVALID_ID then

				if pos ~= 1 then
					bYinshen = 1				
				end

				local iPos,_,_ = CheckBufferByName( g_boss["哈大霸"] , "悬枢之痛")
				if INVALID_ID ~= iPos then
					AI:SetAdv("主防御圈" , 122 , 94 , 9 )
					AI:SetAdv("增加威胁对象" , g_boss["哈大霸"] , 5 , "哈大霸" )
					if bRangeAttack == false then
						bKillMonster = 0
					end
				else
					AI:SetAdv("删除威胁对象" , "哈大霸" )
					local iPos,_,_ = CheckBufferByName( g_boss["哈大霸"] , "丝竹空之痛")
					if INVALID_ID ~= iPos then
						if INVALID_ID ~= Player:GetBuffByName("隐身") then 
							bKillMonster = 0
						end
					end
				end

				if pos ~= 1 and Player:GetData("HP_PERCENT") < 60 then
					bKillMonster = 0
				end

				if menpai == MENPAI_GAIBANG then
					if INVALID_ID == Player:GetBuffByName("龙战于野") then
						AI:SetParameter("运行参数" , "使用技能" , "龙战于野" , "自身辅助" )
					else
						AI:SetAdv("删除威胁对象" , "哈大霸" )
						bKillMonster = 1
					end
				end

				if pos == 0 and menpai == 4 then
					bYinshen = 1
					bKillMonster = 0
				end

				UpdateMsg("Quest_PM_Complete")

			elseif g_boss["桑土公"] ~= INVALID_ID or g_boss["碧磷僵尸"] ~= INVALID_ID then

				if pos ~= 2 then 
					bYinshen = 1
				end

				if g_boss["桑土公"] == INVALID_ID then
					--隐身了
					bKillMonster = 0 
					bCallPet = 0

					if pos == 2 then
						-- 桑土公 跑圈 48,108  38,111  44,121   52,114
						if nPos == 0 then
							MoveTo( 48 , 108 , g_MissionMap )
							AI:Start()
						elseif nPos == 1 then
							MoveTo( 38 , 111 , g_MissionMap )
							AI:Start()
						elseif nPos == 2 then
							MoveTo( 44 , 121 , g_MissionMap )
							AI:Start()
						elseif nPos == 3 then
							MoveTo( 52 , 114 , g_MissionMap )
							AI:Start()
						else
							nPos = -1
						end

						nPos = nPos + 1
					end
				else
					FingerAttack = g_boss["桑土公"]
					FingerAttackName = "桑土公"
				end

				UpdateMsg("Quest_PM_Complete")

			elseif g_boss["乌老大"] ~= INVALID_ID then

				if pos ~= 1 then 
					local nDefencePoison = Player:GetData("DEFENCEPOISON")
					local nHpPercent = Player:GetData("HP_PERCENT")
					if nDefencePoison < 60 then
						bYinshen = 1
						if nHpPercent < 65 then
							bKillMonster = 0 
						end
					end

					if pos == 0 and menpai == 4 then
						bKillMonster = 0
					end
				end

				UpdateMsg("Quest_PM_Complete")

			elseif g_boss["不平道人"] ~= INVALID_ID or g_boss["卓不凡"] ~= INVALID_ID or g_boss["任平生"] ~= INVALID_ID then

				if g_boss["不平道人"] ~= INVALID_ID then
					FingerAttack = g_boss["不平道人"]
					FingerAttackName = "不平道人"
				end

				UpdateMsg("Quest_PM_Complete")			
		
			elseif g_boss["李秋水"] ~= INVALID_ID then

				if INVALID_ID ~= Player:GetBuffByName("无法使用任何攻击技能") then
					bKillMonster = 0
				end

				bFixPlace = 1

				UpdateMsg("Quest_PM_Complete")
			
			else
				--找不到boss的情况下
				local self_x , self_y = Player:GetPos()
				if GetDist( self_x , self_y , main_x , main_y ) > 3 then
					UpdateMsg("Quest_PM_Complete")
				else
					--人物得回到安全坐标点
					if IsMsgCold("Quest_PM_Complete" , 5000 ) then
						break
					else
						PushDebugMessage("准备前往下一个boss 点!" , 1)
					end
				end
			end
		end

		if GetActiveDataSceneID() ~= g_MissionMap then
			break 
		end

		local nMsgChannel = -1
		while true do 
			local channel, time, name, data = GetChatMsg()

			if channel == INVALID_ID then 
				break 
			end

			local now = System:timeGetTime()
			--channel == 0 and
			if now < time + 5 then

				if string.find(data, "至阴至柔") then
					pre_state = "至阴至柔"
					UpdateMsg("Quest_PM_lqs")
					nMsgChannel = channel 
				end

				if string.find(data, "刺骨的寒冷") then
					pre_state = "刺骨的寒冷"
					UpdateMsg("Quest_PM_lqs")
					nMsgChannel = channel
				end

				if string.find(data, "腐骨之毒") then
					pre_state = "腐骨之毒"
					UpdateMsg("Quest_PM_lqs")
					nMsgChannel = channel
				end

				if string.find(data, "烈焰的灼烧") then
					pre_state = "烈焰的灼烧"
					UpdateMsg("Quest_PM_lqs")
					nMsgChannel = channel
				end

				if string.find(data, "五雷轰顶") then
					pre_state = "五 雷 轰 顶"
					UpdateMsg("Quest_PM_lqs")
					nMsgChannel = channel
				end

				if string.find(data, "至刚至阳") then
					pre_state = "至刚至阳"
					UpdateMsg("Quest_PM_lqs")
					nMsgChannel = channel
				end
			end
		end

		if nMsgChannel == 0 then
			local sMsg = "当前剑舞属性是 => "..pre_state
			Shout( "team" , sMsg )
		end

		--判断自己宠物是否中瘟疫
--[[		local nPetID = Player:GetFightingPetID()
		if nPetID ~= INVALID_ID then
			local iPos,_,_ = CheckBufferByName( nPetID, "瘟疫")
			if INVALID_ID ~= iPos then
				bCallPet = 0
			end
		end
]]--		


		--自己是主加血,且被攻击时,停止攻击
		if pos == 0 and menpai == 4 then
			bYinshen = 1

			if nHPPercent < 60 or nMPPercent < 15 then
				bKillMonster = 0
			end
		end

		AI:SetParameter("运行参数" , "答题" , 1 )
		AI:SetParameter("拾取" , bPickUp )
		AI:SetParameter("运行参数" , "杀怪" , bKillMonster )
		AI:SetParameter("运行参数" , "引怪" , bYinGuai )
		AI:SetParameter("运行参数" , "组队跟随" , bTeamFollow )
		AI:SetParameter("运行参数" , "辅助攻击" , bTeamAttack )
		AI:SetParameter("运行参数" , "停止自身技能辅助" , bStopAssistSelf )
		AI:SetParameter("运行参数" , "停止辅助技能" , bStopAssist )
		AI:SetParameter("运行参数" , "启用加速技能" , bSpeedUp )
		AI:SetParameter("运行参数" , "停止特殊攻击技能" , bStopSpecialAttack )
		AI:SetParameter("运行参数" , "召唤珍兽" , bCallPet )

		AI:SetAdv("跟随对象" , FollowTarget , 3 )
		--AI:SetAdv("主防御圈半径" , mainguard_r )
		AI:SetAdv("允许隐身" , bYinshen )
		AI:SetAdv("不允许出防御圈" , bFixPlace )

		if pre_FingerAttack ~= FingerAttack then
			AI:SetAdv("指定击杀" , -1 , "" )
			pre_FingerAttack = INVALID_ID
		end
		
		if FingerAttack ~= INVALID_ID then
			AI:SetAdv("指定击杀" , FingerAttack , FingerAttackName )
			pre_FingerAttack = FingerAttack
		end
	end

	AI:Stop()
end
			

function OnNpcEvent( nProcess_chk )

	MoveToNPC( g_Process[nProcess_chk].x , g_Process[nProcess_chk].y , g_MissionMap , g_Process[nProcess_chk].npc )

	DbgPrintf("on npc %d" , tonumber(nProcess_chk))

	if nProcess_chk < 3 then
		QuestFrameOptionClicked("挑战", CMP_FULL_MATCH)
	elseif 3 == nProcess_chk then
		QuestFrameOptionClicked("挑战不平道人和卓不凡", CMP_FULL_MATCH)
	elseif 4 == nProcess_chk then
		QuestFrameOptionClicked("决战李秋水", CMP_PART_MATCH)
	else
		QuestFrameOptionClicked("离开缥缈峰", CMP_PART_MATCH)
		QuestFrameOptionClicked("确定离开", CMP_PART_MATCH)
	end

	if nProcess_chk > 4 then
		return
	end

	local sWait = 1 

	while true do

		System:Sleep(1000)

		local sWaitBoss = false
		local isNext,szMsg = GetDebugMessage()
		while isNext == 1 do
			DbgPrintf("dbg msg %s" , tostring(szMsg))
			if szMsg ~= nil then
				if string.find(szMsg ,"秒钟后开始") or string.find(szMsg,"正与卓不凡") then
					g_VirtualBoss = g_Process[nProcess_chk].fight_boss
					sWaitBoss = true 
					UpdateMsg("Quest_PM_VirtualBoss")						
				end
			end
			isNext,szMsg = GetDebugMessage()
		end
		
		if sWait > 3 then 
			break
		end

		if sWaitBoss == false then
			local list_count = DataPool:GetNPCEventList_Num()
			local strType,_,_,_,szText = DataPool:GetNPCEventList_Item(0);

			if strType == "text" then
				if string.find(szText, "先去找哈大霸") then
					ShowMessage("先去找哈大霸!")
					fuben_process_chk = 0 
					break
				elseif string.find(szText, "该死的乌老大") then

					fuben_process_chk = 2
					strType,_,_,_,szText = DataPool:GetNPCEventList_Item(1)
					if strType == "id" then
						if string.find(szText , "挑战不平道人和卓不凡") then
							fuben_process_chk = 3
						end
					end

					if fuben_process_chk == 2 then 
						ShowMessage("先去找乌老大!")
					end
					
					break
				elseif string.find(szText, "不配与我动手") then
					ShowMessage("先去找桑土公!")
					fuben_process_chk = 1
					break
				elseif string.find(szText, "既然事已至此") then

					fuben_process_chk = 3
					strType,_,_,_,szText = DataPool:GetNPCEventList_Item(1)
					if strType == "id" then

						if string.find(szText , "决战李秋水") then
							fuben_process_chk = 4
						elseif string.find(szText , "离开缥缈峰") then
							fuben_process_chk = 5
						end
					end

					if fuben_process_chk == 3 then 
						ShowMessage("先去找符敏仪!")
					end
					
					break
				end
			end
		end
	
		UpdateBossInfo()

		if g_boss[g_Process[nProcess_chk].npc] ~= INVALID_ID then
			ShowMessage("发现boss")
			break
		end

		sWait = sWait + 1

	end
		
end


function OnSearch_Member()

	AI:SetAI("杀怪")	
	AI:SetParameter("初始化")
	AI:SetParameter("拾取", 0)
	AI:SetParameter("地图", g_MissionMap )

	AI:Start()
	AI:SetParameter("运行参数" ,"组队跟随", 1)
	AI:SetParameter("运行参数" ,"杀怪", 0)
		
	while true do

		System:Sleep(1000)
		if DataPool:IsTeamLeader() == true then
			break
		end

		if GetActiveDataSceneID() ~= g_MissionMap then
			break 
		end

		local isNext,szMsg = GetDebugMessage()
		while isNext == 1 do
			DbgPrintf("dbg msg %s" , tostring(szMsg))
			if szMsg ~= nil then
				if string.find(szMsg ,"秒钟后开始") then
					-- g_VirtualBoss = check_npc
					-- 检查队长离哪个boss最近
					local _,_,_,leader_mapid,leader_x,leader_y = DataPool:GetTeamMemInfo(-1)
					local vDistance = 999999
					local vBoss = "非法目标"

					for i,v in pairs(g_Process) do
						local this_distance = GetDist( leader_x , leader_y , v.x , v.y )
						if this_distance < vDistance then
							vDistance = this_distance
							vBoss = v.fight_boss
						end							 
					end

					if vBoss ~= "非法目标" then
						g_VirtualBoss = vBoss
						UpdateMsg("Quest_PM_VirtualBoss")
					end					
				end
			end
			isNext,szMsg = GetDebugMessage()
		end

		local nType , nStep = CheckProcess()
		if nType == 1 or nType == 3 then
			-- 无npc 或者有 npc 的情况下, 继续组队跟随
			break
		end

	end

end

local first_process = true

function _SMain(...)

	LogChatMsg(0)
	LogChatMsg(1)

	--战场初始化
	AI:SetAdv( "清除界限" )

	local nWaitBoss = 0 

	while true do
		
		if GetActiveDataSceneID() == g_MissionMap then

			if first_process == true and DataPool:IsTeamLeader() == true then
				first_process = false 
				local vDistance = 999999
				local self_x , self_y = Player:GetPos()

				for i,v in pairs(g_Process) do
					local this_distance = GetDist( self_x , self_y , v.x , v.y )
					DbgPrintf("%d 的距离是 %d" , tonumber(i) , tonumber(this_distance))
					if this_distance < vDistance then
						vDistance = this_distance
						fuben_process_chk = i
					end							 
				end
			end

			local nType , nStep = CheckProcess()
			local nPre_chk = fuben_process_chk

			DbgPrintf("check process %d %d, chk = %d" , nType , nStep , fuben_process_chk )

			if nType == 1 then
				nWaitBoss = 0 
				OnProcessBoss( nStep )
			else
				if DataPool:IsTeamLeader() == false then
					OnSearch_Member()
				else
					if nType == 0 then 
						if nWaitBoss > 4 then
							--该位置可能已经没有boss 了
							if fuben_process_chk == 4 or fuben_process_chk == 5 then
								fuben_process_chk = 3
							elseif fuben_process_chk == 2 then
								fuben_process_chk = 4
							elseif fuben_process_chk == 1 then
								fuben_process_chk = 2
							elseif fuben_process_chk == 0 then
								fuben_process_chk = 1
							else
								fuben_process_chk = 5
							end
							nWaitBoss = 0 
						else
							nWaitBoss = nWaitBoss + 1 
						end
					else
						nWaitBoss = 0 
					
						if nType == 3 then
							ShowMessage("出副本")
							OnNpcEvent( 5 )
						elseif nType == 2 and DataPool:IsTeamLeader() then
							OnNpcEvent( fuben_process_chk )
						end
					end
				end
			end

			if fuben_process_chk < fuben_process_do then
				fuben_process_chk = fuben_process_do 
			end				

			if DataPool:IsTeamLeader() == true then
				MoveTo( g_Process[fuben_process_chk].x , g_Process[fuben_process_chk].y , g_MissionMap )
			end			
		else
			first_process = false
			fuben_process_do = 0 
			fuben_process_chk = 0 

			LuoYangRestore()

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

			MoveToNPC(191, 223, 246, "程青霜")
			QuestFrameOptionClicked("初战缥缈峰", CMP_FULL_MATCH)

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

			if strType == "text" then
				if string.find(szText, "不满足条件") then
					ShowMessage("队伍条件不足,终止脚本运行!")
					break	-- 退出循环，结束脚本
				end
			end
		end

		System:Sleep(500)
	end


	LogChatMsg(-1)
end

_SMain()
