ShowMessage("ľɽׯű $Rev: 100 $")

--[[
[author]
class=1
chk0str=ȡ3
chk1str=Ͱ
]]--

--Ʒͬʱʱ,ʰȡƷ
--սʱ,һٻﵼʧ
--ᦳʱ,ҵĿ㲻ŵ
--ֹͣʱ,ɫƶ趨λ,׷Ŀ
--28,27  28,29  31,30  32,26

script_name="սľׯ"

local g_MissionMap = 291
local g_nParam = {}
local l_param_lzby = 0 
local l_param_third = 0

local g_XukongMission = 
{
	[0] = {name = "սɱ" , vin = "սɱ" } ,
	[1] = {name = "սľׯ" , vin = "սľׯ" } ,
	[2] = {name = "ս翷" , vin = "ս翷" } ,
	[3] = {name = "ս" , vin = "ս" }
}

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

		if nIndex == -1 then
			continue 
		end

		DbgPrintf("%s Index:%d MissionID:%d ScriptID:%d", script_name, nIndex, idMission, idScript)

		local nParam = {}
		-- ȡ
		for i=0,7 do
			nParam[i] = DataPool:GetPlayerMission_Variable( nIndex , i)
			DbgPrintf("[%d]%d", i, nParam[i])
		end
		
		return tonumber(nParam[0]) , idMission, idScript , g_XukongMission[i].name , g_XukongMission[i].vin
	end

	return INVALID_ID , 0, 0 , "" , ""
end

local g_pqq_pos =
{
	[0] = { x= 101, y = 114 } ,
	[1] = { x= 96 , y = 79 } ,
	[2] = { x= 35 , y = 87 } ,
	[3] = { x= 84 , y = 23 } ,
	[4] = { x= 23 , y = 17 } 
}

local g_boss = {
	["ݼݼNPC"] = INVALID_ID ,
	["ī"] = INVALID_ID ,
	["īNPC"] = INVALID_ID ,
	[""] = INVALID_ID ,
	[""] = INVALID_ID ,
	["NPC"] = INVALID_ID ,
	[""] = INVALID_ID ,
	[""] = INVALID_ID ,
	["NPC"] = INVALID_ID ,
	["ӥ"] = INVALID_ID ,
	["ī"] = INVALID_ID ,
	[""] = INVALID_ID ,
	[""] = INVALID_ID ,
	[""] = INVALID_ID ,
	["NPC"] = INVALID_ID ,
	["ʿ"] = INVALID_ID ,
	["ʮһɱ"] = INVALID_ID , 
	["ľׯͷ"] = INVALID_ID ,
	["ľׯ"] = INVALID_ID ,
	["NPC"] = INVALID_ID ,
	["Ӱҡ"] = INVALID_ID
}

local g_main_pos =
{
	[0] = {x=99 ,y=112, r=15} ,
	[1] = {x=101,y=94 , r=15} ,
	[2] = {x=27 ,y=103, r=15} ,
	[3] = {x=100,y=30 , r=15} ,
	[4] = {x=30 ,y=29 , r=15} 
}

local g_boss2_pos =
{
	--ڶboss,һ׶վλ
	[0] = {x=41,y=99} ,
	[1] = {x=41,y=86} ,
	[2] = {x=30,y=86} ,
	[3] = {x=31,y=98} ,
	[4] = {x=52,y=86} ,
	[5] = {x=52,y=99} 
}

local g_step2_pos = 
{
	--ڶboss,ڶ׶λ
	[0] = {x=36,y=93} ,	--Ͻλ
	[1] = {x=17,y=88} ,
	[2] = {x=17,y=113} ,
	[3] = {x=39,y=110} ,		
	[4] = {x=40,y=88} 			
}

function TestMenpai()

	local t_team_size = DataPool:GetTeamMemCount()
	if t_team_size < 1 then
		return false
	end

	for i = 0 , t_team_size - 1 do	
		local _,t_id, t_menpai = DataPool:GetTeamMemInfo(i)
		if t_menpai == 0 or t_menpai == 1 or t_menpai == 2 or t_menpai == 7 then
			return true
		end
	end
	return false
end

function UpdateBossInfo()

	g_boss["ݼݼNPC"] = 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["ӥ"] = 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["ľׯͷ"] = INVALID_ID 
	g_boss["ľׯ"] = INVALID_ID 
	g_boss["Ӱҡ"] = INVALID_ID

	local t_hyf_near_dis = 99999

	local n = EnumObject(true)

	local nBossCount = 0

	for i=0, n - 1 do
		local idObj, classObj, nameObj, xObj, yObj = EnumObject(false, i)
		if idObj == INVALID_ID then
			continue
		end
		
		if classObj == "MON" then
			if nameObj == "ī" then
				if Character:GetData(idObj, "DIE") == false then
					g_boss["ī"] = idObj
					nBossCount = nBossCount + 1
					g_boss["NPC"] = 99999
				end
			elseif nameObj == "" then 
				if Character:GetData(idObj, "DIE") == false then
					g_boss[""] = idObj
					nBossCount = nBossCount + 1
				end
			elseif nameObj == "" then 
				if Character:GetData(idObj, "DIE") == false then
					g_boss[""] = idObj
					nBossCount = nBossCount + 1
				end
			elseif nameObj == "ӥ" then
				if Character:GetData(idObj, "DIE") == false then
					g_boss["ӥ"] = idObj
					nBossCount = nBossCount + 1
				end
			elseif string.find(nameObj , "ī") then
				if Character:GetData(idObj, "DIE") == false then
					g_boss["ī"] = idObj
					nBossCount = nBossCount + 1
				end
			elseif string.find(nameObj , "ľׯͷ") then
				if Character:GetData(idObj, "DIE") == false then
					g_boss["ľׯͷ"] = idObj
					nBossCount = nBossCount + 1
				end
			elseif string.find(nameObj , "ľׯ") then
				if Character:GetData(idObj, "DIE") == false then
					g_boss["ľׯ"] = idObj
					nBossCount = nBossCount + 1
				end
			elseif nameObj == "ʿ" then
				if Character:GetData(idObj, "DIE") == false then
					g_boss["ʿ"] = idObj
					nBossCount = nBossCount + 1
				end
			elseif nameObj == "" then
				if Character:GetData(idObj, "DIE") == false then
					g_boss[""] = idObj
					nBossCount = nBossCount + 1
				end
			end
		elseif classObj == "NPC" then
			if nameObj == "ī" then
				g_boss["īNPC"] = idObj
				g_boss["NPC"] = 99999
			elseif nameObj == "" then 
				g_boss["NPC"] = idObj
			elseif nameObj == "" then 
				g_boss["NPC"] = idObj
			elseif nameObj == "ݼݼ" then
				g_boss["ݼݼNPC"] = idObj
			elseif nameObj == "" then

				--¼밲ȫһID
				local t_this_near_dis = 99999
				local t_this_step = 0
				for i = 1 , 4 do 
					local t_new_dis = GetDistSq( xObj, yObj , g_step2_pos[i].x , g_step2_pos[i].y )
					if t_new_dis < t_this_near_dis then
						t_this_near_dis = t_new_dis
						t_this_step  = i 
					end
				end

				if g_boss[""] == INVALID_ID or t_this_near_dis < t_hyf_near_dis then
					g_boss[""] = idObj
					nBossCount = nBossCount + 1
					t_hyf_near_dis = t_this_near_dis
					DbgPrintf(" %d  %d,%d, %d " , g_boss[""] , xObj , yObj , t_this_step )
				end

				AI:SetAdv("в" , idObj , 4 , "")
			elseif nameObj == "" then
				g_boss["NPC"] = idObj
			end			
		elseif classObj == "SPECIAL" then
			if string.find( nameObj , "ʮһɱ") then
				g_boss["ʮһɱ"] = idObj
				nBossCount = nBossCount + 1
				AI:SetAdv("в" , idObj , 4 , "ʮһɱ")
			elseif nameObj == "" then
				g_boss[""] = idObj
				nBossCount = nBossCount + 1
			elseif nameObj == "" then
				g_boss[""] = idObj
				nBossCount = nBossCount + 1
				if l_param_lzby == 1 then
					AI:SetAdv("в" , idObj , 4 , "")
				end
			elseif string.find( nameObj , "") then 
				g_boss[""] = idObj
				nBossCount = nBossCount + 1
				AI:SetAdv("в" , idObj , 4 , "")
			end
		elseif classObj == "PLAYER" or classObj == "PET" then
			local iPos,_,_ = CheckBufferByName( idObj, "Ӱҡ")
			if INVALID_ID ~= iPos then
				g_boss["Ӱҡ"] = idObj
			end
		end
	end

	return nBossCount
end


-- 0,  1,ս  2,
-- ȷǰʱӦʲô
-- ͼжӦnpc,ȷ
-- ѯ(1,2,3,4),Ϊǰ
-- 봫npcԻ,ȷǰ
function CheckProcess()

	UpdateBossInfo()
	
	if g_boss["ľׯ"] ~= INVALID_ID or g_boss["ľׯͷ"] ~= INVALID_ID then
		return false,0
	elseif g_boss["ī"] ~= INVALID_ID or g_boss[""] ~= INVALID_ID then
		return false,1
	elseif g_boss[""] ~= INVALID_ID or g_boss[""] ~= INVALID_ID then
		return false,2
	elseif g_boss[""] ~= INVALID_ID or g_boss["ӥ"] ~= INVALID_ID or g_boss["ī"] ~= INVALID_ID or g_boss[""]~= INVALID_ID or g_boss[""]~= INVALID_ID then
		return false,3
	elseif g_boss[""] ~= INVALID_ID or g_boss["ʿ"]~= INVALID_ID or g_boss["ʮһɱ"]~= INVALID_ID then
		return false,4
	end
	
	return true,INVALID_ID
end

--Իݼݼ
function OnNpcEvent( nProcess_chk )

	local _,nCheckResult = CheckProcess()
	if nCheckResult ~= INVALID_ID then
		return nCheckResult
	end

	MoveTo(g_pqq_pos[nProcess_chk].x , g_pqq_pos[nProcess_chk].y , g_MissionMap , "ݼݼ" , 4 , false , CheckProcess , true )
	local _,t_chk = CheckProcess()
	if t_chk ~= INVALID_ID then
		return t_chk
	end

	MoveToNPC( g_pqq_pos[nProcess_chk].x , g_pqq_pos[nProcess_chk].y , g_MissionMap , "ݼݼ" )

	while true do

		local list_count = DataPool:GetNPCEventList_Num()

		if list_count < 1 then
			if nProcess_chk == 1 then
				if g_boss["īNPC"] ~= INVALID_ID then
					return INVALID_ID
				end
			elseif nProcess_chk == 2 then
				if g_boss["NPC"] ~= INVALID_ID then
					return INVALID_ID
				end
			elseif nProcess_chk == 3 then
				if g_boss["NPC"] ~= INVALID_ID then
					return INVALID_ID
				end
			end

			local idNpc = FindObjectByName("ݼݼ" , "NPC")
			if idNpc ~= INVALID_ID then
				return nProcess_chk
			end

			return INVALID_ID 
		else
			local t_click = 0 
			for i=0, list_count - 1 do
				local strType,_,_,_,szText = DataPool:GetNPCEventList_Item(i)
				if strType == "id" then
					if string.find(szText, "͵̳") then
						if t_click < 4 then
							t_click = 4
						end
					elseif string.find(szText , "͵") then
						if t_click < 3 then
							t_click = 3
						end
					elseif string.find(szText , "͵ϸ") then
						if t_click < 2 then
							t_click = 2
						end
					elseif string.find(szText , "ȥȡ") then
						--ҪٴζԻ,Żһѡ
						if t_click < 5 then
							t_click = 5
						end
					elseif string.find(szText , "Эľׯ") then
						if t_click < 6 then
							t_click = 6
						end
					elseif string.find(szText , "ʼս")  then
						if t_click < 7 then
							t_click = 7
						end
					end
				elseif strType == "text" then
					if string.find(szText , "ҿԸǵĽս") then
						if t_click < 1 then
							t_click = 1
						end
					end
				end
			end

			if t_click == 7 then
				QuestFrameOptionClicked("ʼս", CMP_PART_MATCH)
				return nProcess_chk
			elseif t_click == 6 then
				QuestFrameOptionClicked("Эľׯ", CMP_PART_MATCH)
				return 4
			elseif t_click == 5 then
				QuestFrameOptionClicked("ȥȡ", CMP_PART_MATCH)
				MoveToNPC( g_pqq_pos[nProcess_chk].x , g_pqq_pos[nProcess_chk].y , g_MissionMap , "ݼݼ" )
				--ȴ
			elseif t_click == 4 then
				QuestFrameOptionClicked("͵̳", CMP_PART_MATCH)
				return INVALID_ID
			elseif t_click == 3 then
				QuestFrameOptionClicked("͵", CMP_PART_MATCH)
				return INVALID_ID
			elseif t_click == 2 then
				QuestFrameOptionClicked("͵ϸ", CMP_PART_MATCH)
				return INVALID_ID
			elseif t_click == 1 then
				QuestFrameOptionClicked("ȷ", CMP_PART_MATCH)
				return 0
			else
				--öԻ޺ѡ
				ShowMessage("ݼݼ ĶԻûзֺѡ")
				return INVALID_ID
			end
		end
	
		local _,nCheckResult = CheckProcess()

		if nCheckResult ~= INVALID_ID then
			return nCheckResult
		end
	end

	return INVALID_ID
end



function OnFightBoss( nBossStep )

	UpdateMsg("Quest_SJZ_Complete")
	UpdateMsg("Quest_SJZ_Begin")
	local pos , menpai = GetTeamStandPos()

	if pos < 0 then
		pos = 0
	end

	local flag_step0 = 0 	-- ֽͷʱı
	local flag_step2 = 0	-- һֻ,ı
	local flag_step3 = 0	-- ӥ,ı
	local flag_step4 = 0	-- ʾϢ,ı , ʮһɱ,ı
	local default_wait = 10000

	SetEvent( "EVENT_PICKUP" , 0 )
	
	AI:SetAdv( "" )
	if nBossStep == 1 or nBossStep == 0 then
		--ս:117,78 - 117,108 - 85,107 - 85,78
		--42,85 - 42,115 - 13,115 - 13,85
		AI:SetAdv( "ս" ,  117 , 78 , 117 , 108 )
		AI:SetAdv( "ս" ,  117 , 108, 85  , 107 )
		AI:SetAdv( "ս" ,  85  , 107, 85  , 78 )
		AI:SetAdv( "ս" ,  85  , 78 , 117 , 78 )
		AI:SetAdv( "Ȧ" , g_main_pos[nBossStep].x , g_main_pos[nBossStep].y , g_main_pos[nBossStep].r )
	elseif nBossStep == 2 then
		AI:SetAdv( "ս" ,  42 ,  85 , 42 , 115 )
		AI:SetAdv( "ս" ,  42 , 115,  13 , 115 )
		AI:SetAdv( "ս" ,  13 , 115,  13 , 85 )
		AI:SetAdv( "ս" ,  13 ,  85,  42 , 85 )
		AI:SetAdv( "Ȧ" , g_main_pos[nBossStep].x , g_main_pos[nBossStep].y , g_main_pos[nBossStep].r )
	elseif nBossStep ==3 then
		AI:SetAdv( "ս" ,  82 ,  16 , 118 , 16 )
		AI:SetAdv( "ս" , 118 ,  16,  118 , 47 )
		AI:SetAdv( "ս" , 118 ,  47,   82 , 47 )
		AI:SetAdv( "ս" ,  82 ,  47,   82 , 16 )
		AI:SetAdv( "Ȧ" , g_main_pos[nBossStep].x , g_main_pos[nBossStep].y , g_main_pos[nBossStep].r )
	elseif nBossStep == 4 then
		AI:SetAdv( "ս" ,  41 ,  16 , 41 , 38 )
		AI:SetAdv( "ս" ,  41 ,  38 , 17 , 38 )
		AI:SetAdv( "ս" ,  17 ,  38 , 17 , 16 )
		AI:SetAdv( "ս" ,  17 ,  16 , 41 , 16 )
		if pos == 0 then
			AI:SetAdv( "Ȧ" , 33 , 26 , 15 )
		elseif pos == 1 then
			AI:SetAdv( "Ȧ" , 33 , 33 , 15 )
		elseif pos == 2 then
			AI:SetAdv( "Ȧ" , 25 , 32 , 15 )
		elseif pos == 3 then
			AI:SetAdv( "Ȧ" , 25 , 25 , 15 )
		else
			AI:SetAdv( "Ȧ" , g_main_pos[nBossStep].x  , g_main_pos[nBossStep].y  , g_main_pos[nBossStep].r )
		end
	end

	AI:SetAI("߼ս") 
	AI:SetParameter("ͼ" , g_MissionMap)
	AI:Start()

	local bRangeAttack = Player:IsRangeAttack()
	local pre_FingerAttack = INVALID_ID 
	Dismount()

	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 FingerAttack = -1
		local FingerAttackName = ""
		local FollowTarget = -1
		
		System:Sleep(300)

		local nBoss = UpdateBossInfo()
		if nBoss > 0 then
			default_wait = 10000
			UpdateMsg("Quest_SJZ_Complete")
		end


		if LockNearestTarget( "" , CHA_TYPE_BAG , 0 , 0 , 0 ) ~= -1 then
			UpdateMsg("Quest_SJZ_Complete")
		end

		local t_my_mapid = GetActiveDataSceneID()
		if t_my_mapid ~= g_MissionMap then
			break
		end

		--ȡӳλϢ
		local _,LeaderId,_,Leader_mapid,Leader_x,Leader_y = DataPool:GetTeamMemInfo(-1)
		local t_followleader = false
		if LeaderId ~= Player:GetData("ID") then
			t_followleader = Player:CompareLocalArea( Leader_mapid , Leader_x , Leader_y )
		end

		if nBossStep == 0 then
			--븱30߸û˻߷κι߶޽սְҵ
			--õȴ,ȴˢʱ

			if nBoss > 0 then
				flag_step0 = 1
				AI:SetAdv( "Ȧ" , g_main_pos[1].x , g_main_pos[1].y , g_main_pos[1].r )
			end

			if flag_step0 == 0 then
				local _,t_pqq_x , t_pqq_y = FindObjectByID( g_boss["ݼݼNPC"] , "NPC")
				local t_pqq_dis = GetDist( t_pqq_x , t_pqq_y , g_pqq_pos[0].x ,g_pqq_pos[0].y )
				if TestMenpai() == false or t_pqq_dis > 5 or IsMsgCold("Quest_SJZ_Begin" , 30000 ) then
					DbgPrintf("ݼݼԶ %d,%d  %d" , t_pqq_x , t_pqq_y , t_pqq_dis )
					--µ
					flag_step0 = 1
					AI:SetAdv( "Ȧ" , g_main_pos[1].x , g_main_pos[1].y , g_main_pos[1].r )
				else
					--ȴ
					UpdateMsg("Quest_SJZ_Complete")
				end
			else
				--ɱ
			end
			
		elseif nBossStep == 1 then
			--1 :ɱ ī
			--ӳ,սı
		elseif nBossStep == 2 then
			--2 :ɱ 
			if g_boss[""] ~= INVALID_ID then
				if flag_step2 == 0 then
					flag_step2 = 1 
				end
			end

			local t_BossHPPercent = 100
			if g_boss[""] ~= INVALID_ID then
				t_BossHPPercent = Character:GetData( g_boss[""] , "HP_PERCENT")
			end

			if flag_step2 == 0 and t_BossHPPercent > 50 then
				-- ԰ռȶλվ,սֹͣ,ճ,Сս
				-- AI:SetAdv( "Ȧ" , g_boss2_pos[pos].x , g_boss2_pos[pos].y , 3 )
				-- if bRangeAttack == false then
				-- 	bKillMonster = 0
				--	bStopAssist = 1
				-- end
				bCallPet = 0 
				-- bFixPlace = 1
			else
				if flag_step2 == 0 then
					flag_step2 = 1 
				end

				if t_followleader == true then
					--ɸӳʱ,Աӳ
					FollowTarget = LeaderId
					bTeamFollow = 1
				else
					--ɸʱ,ҪԼ,޸սұս
					if g_boss[""] ~= INVALID_ID and IsMsgCold("Quest_SJZ_SETHYF" , 10000) then
					
						local _,t_hyf_x,t_hyf_y = FindObjectByID( g_boss[""] , "NPC")

						if t_hyf_x ~= 0 or t_hyf_y ~= 0 then

							local t_this_far_dis = 0
							local t_self_x , t_self_y = Player:GetPos()
							--Զİȫ
							for i = 1 , 4 do 
								local t_new_dis = GetDistSq( t_hyf_x , t_hyf_y , g_step2_pos[i].x , g_step2_pos[i].y )
								if t_new_dis > t_this_far_dis then
									t_this_far_dis = t_new_dis
									flag_step2  = i 
								end
							end
--[[
							DbgPrintf("ǰ %d " , t_this_step)
							--ҳǰһȫ
							if GetDist( t_hyf_x , t_hyf_y , g_step2_pos[t_this_step].x ,g_step2_pos[t_this_step].y ) < 12 then
								flag_step2 = t_this_step + 1
								if flag_step2 > 4 then
									flag_step2 = 1
								end
							else
								flag_step2 = t_this_step 
							end
]]--
							UpdateMsg("Quest_SJZ_SETHYF")
							AI:SetAdv( "Ȧ" , g_step2_pos[flag_step2].x , g_step2_pos[flag_step2].y , 4 )
							DbgPrintf(",ǰȦ %d , %d,%d" , flag_step2 , g_step2_pos[flag_step2].x , g_step2_pos[flag_step2].y ) ;
						end
					end
					bFixPlace = 1
				end
			end
		elseif nBossStep == 3 then
			--3 :ɱ  102,35  
			if flag_step3 == 1 then
				AI:SetAdv( "Ȧ" , 98 , 38 , 15 )
			else
				AI:SetAdv( "Ȧ" , 102 , 34 , 15 )

				if l_param_lzby == 1 then
					if t_followleader == false then
						if g_boss[""] ~= INVALID_ID then
						
							local _,t_by_x,t_by_y = FindObjectByID( g_boss[""] , "")
							--
							if t_by_x ~= 0 and t_by_y ~= 0 then
								AI:SetAdv( "Ȧ" , t_by_x , t_by_y , 3 )
							end
							DbgPrintf("ְ, %d,%d" , t_by_x , t_by_y )
						end
					else
						if g_boss[""] ~= INVALID_ID or g_boss[""] ~= INVALID_ID then
							bTeamFollow = 1
							FollowTarget = LeaderId
						end
					end
				end
			end

			--bFixPlace = 1

		elseif nBossStep == 4 then
			-- 4 :ɱ 
			if flag_step4 == 1 then
				if l_param_lzby == 0 then
					AI:SetAdv( "Ȧ" , g_main_pos[nBossStep].x , g_main_pos[nBossStep].y , g_main_pos[nBossStep].r )
				else
					if t_followleader == true then
						bTeamFollow = 1
						FollowTarget = LeaderId
					else
						bFixPlace = 1
					end
				end
			elseif flag_step4 == 2 then
				AI:SetAdv( "Ȧ" , g_main_pos[nBossStep].x , g_main_pos[nBossStep].y , g_main_pos[nBossStep].r )
			end
		end					
			
		local t_iPos,_,_ = CheckBufferByName(g_boss[""] , "" ) 
		if t_iPos ~= INVALID_ID then
			bStopSpecialAttack = 1 
			flag_step3 = 1
			bFixPlace = 1
			bKillMonster = 0
		end

		local t_iPos,_,_ = CheckBufferByName(g_boss[""] , "" ) 
		if t_iPos ~= INVALID_ID then
			bStopSpecialAttack = 1 
			bKillMonster = 0
		end
				
		if g_boss[""] ~= INVALID_ID then
			local t_self_x , t_self_y = Player:GetPos()
			if GetDist( t_self_x , t_self_y , g_step2_pos[flag_step2].x ,g_step2_pos[flag_step2].y ) > 4 then
				bKillMonster = 0
				bStopAssistSelf = 1
				bStopAssist = 1
			end
		elseif g_boss["ӥ"] ~= INVALID_ID then
			AI:SetAdv("ָɱ" , 0 , "ӥ" )
			flag_step3 = 1
			bStopSpecialAttack = 0
			bYinGuai = 1
			bKillMonster = 1
		elseif g_boss["ī"] ~= INVALID_ID then
			AI:SetAdv("ָɱ" , 0 , "ī" )
			AI:SetAdv("ָɱ" , 0 , "ī" )
			AI:SetAdv("ָɱ" , 0 , "ī" )
			AI:SetAdv("ָɱ" , 0 , "ī" )
			bStopSpecialAttack = 0
			flag_step3 = 1
			bYinGuai = 1
			bKillMonster = 1
		elseif g_boss["ʿ"] ~= INVALID_ID then
			AI:SetAdv("ָɱ" , 0 , "ʿ" )
			bStopSpecialAttack = 0
			bYinGuai = 1
			bKillMonster = 1
		elseif g_boss["ľׯ"] ~= INVALID_ID then
			bYinGuai = 1
		elseif g_boss["ľׯͷ"] ~= INVALID_ID then
			bYinGuai = 1
		else
			AI:SetAdv("ָɱ" , -1 , "" )
			bTeamFollow = 1
			bTeamAttack = 0
		end

		if g_boss[""] == INVALID_ID then
			AI:SetAdv("ɾв" , "")
		end

		if g_boss["ʮһɱ"] == INVALID_ID then
			AI:SetAdv("ɾв" , "ʮһɱ")
		else
			flag_step4 = 2
		end

		if g_boss[""] == INVALID_ID then
			AI:SetAdv("ɾв" , "")
		end

		if g_boss[""] == INVALID_ID then
			AI:SetAdv("ɾв" , "")
		end

		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 + 10 then

				--DbgPrintf("%s", name)
				DbgPrintf("channel %s", data)
				--DbgPrintf("%d %d [%d]", System:timeGetTime(), time, (System:timeGetTime() - time))
				--DbgPrintf(" ")
				if string.find(data, "SJZ_100129_139") then
					--ˮԱ
					if l_param_lzby == 1 then
						flag_step4 = 1
						AI:SetAdv( "Ȧ" , 20 , 37 , 3 )
					end
				elseif string.find(data, "SJZ_100129_137") then
					--Ա
					if l_param_lzby == 1 then
						flag_step4 = 1
						AI:SetAdv( "Ȧ" , 40 , 25 , 3 )
					end
				elseif string.find(data, "SJZ_100129_138") then
					--ľԱ
					if l_param_lzby == 1 then
						flag_step4 = 1
						AI:SetAdv( "Ȧ" , 38 , 37 , 3 )
					end
				elseif string.find(data, "SJZ_100129_141") then
					--Ա
					if l_param_lzby == 1 then
						flag_step4 = 1
						AI:SetAdv( "Ȧ" , 30 , 18 , 3 )
					end
				elseif string.find(data, "SJZ_100129_140") then
					--Ա
					if l_param_lzby == 1 then
						flag_step4 = 1
						AI:SetAdv( "Ȧ" , 19 , 25 , 3 )
					end
				elseif string.find(data, "SJZ_100129_131") then
					--ϵĶ
					UpdateMsg("Quest_SJZ_Complete")
				elseif string.find(data, "SJZ_100129_132") then
					--ᦳ
					UpdateMsg("Quest_SJZ_Complete")
					if flag_step2 == 0 then
						flag_step2 = 1 
					end
				elseif string.find(data, "SJZ_100129_60") then
					if g_boss["NPC"] == INVALID_ID then
						g_boss["NPC"] = 99999
					end
				elseif string.find(data, "SJZ_100129_46") then
					if g_boss["īNPC"] == INVALID_ID then
						g_boss["īNPC"] = 99999
					end
				elseif string.find(data, "SJZ_100129_71") then
					if g_boss["NPC"] == INVALID_ID then
						g_boss["NPC"] = 99999
					end
				elseif string.find(data, "SJZ_100129_105") then
					if g_boss["NPC"] == INVALID_ID then
						g_boss["NPC"] = 99999
					end
				elseif string.find(data, "SJZ_100129_41") then
					--ͷɱ,
					--bossѾ
					if g_boss["NPC"] == INVALID_ID then
						g_boss["NPC"] = 99999
					end
				elseif string.find(data,"ͽͷ׷뿪ľ") then
					if g_boss["NPC"] == INVALID_ID then
						g_boss["NPC"] = 99999
					end	
				end
			end
		end

		
		-- Ѫ  ﵱǰ 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		

		--жԼǷж
		if g_boss["Ӱҡ"] ~= INVALID_ID then
			UpdateMsg("Quest_SJZ_Complete")

			local nPetID = Player:GetFightingPetID()
			if nPetID == g_boss["Ӱҡ"] then
				bCallPet = 0
			elseif g_boss["Ӱҡ"] == Player:GetData("ID") then
				bCallPet = 0
			else
				--Σն
				AI:SetAdv("в" , g_boss["Ӱҡ"] , 5 , "Ӱҡ")
			end
		else
			AI:SetAdv("ɾв" , "Ӱҡ" )
		end

		if g_boss[""] ~= INVALID_ID then
			bCallPet = 0 
		end

		--ԼѪ,ұʱ,ֹͣ
		if pos == 0 and menpai == 4 then
			bYinshen = 1

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

		if g_boss["īNPC"] ~= INVALID_ID or g_boss["ī"] ~= INVALID_ID then
			g_boss["NPC"] = 99999
		end

		local boss_done = false 
		if nBossStep == 0 then
			if g_boss["NPC"] ~= INVALID_ID then
				boss_done = true
			end
		elseif nBossStep == 1 then
			if g_boss["īNPC"] ~= INVALID_ID then
				boss_done = true
			end
		elseif nBossStep == 2 then
			if g_boss["NPC"] ~= INVALID_ID then
				boss_done = true
			end
		elseif nBossStep == 3 then
			if  g_boss["NPC"] ~= INVALID_ID then
				boss_done = true
			end
		elseif nBossStep == 4 then
			if g_boss["NPC"] ~= INVALID_ID then
				boss_done = true
			end
		end

		local nComplete = GetMissionComplete() 
		if boss_done == true or nComplete > 0 then
			default_wait = 3000
		end

		if IsMsgCold("Quest_SJZ_Complete" , default_wait ) == true then

			if nComplete > 0 or boss_done == true then
				break
			else
				local t_self_x , t_self_y = Player:GetPos()
				local t_set_x = g_main_pos[nBossStep].x
				local t_set_y = g_main_pos[nBossStep].y
				local t_set_r = g_main_pos[nBossStep].r

				if nBossStep == 2 and flag_step2 == 0 then
					--Ͻλ
					t_set_x = g_step2_pos[0].x 
					t_set_y = g_step2_pos[0].y
					t_set_r = 5
				elseif nBossStep == 0 and flag_step0 == 1 then
					t_set_x = g_main_pos[1].x	
					t_set_y = g_main_pos[1].y
					t_set_r = g_main_pos[1].r				
				end

				if GetDist( t_self_x , t_self_y , t_set_x , t_set_y ) > 4 then
					ShowMessage("ʱλ %d" , tonumber(default_wait))
					AI:SetAdv( "Ȧ" , t_set_x , t_set_y , t_set_r )
					UpdateMsg("Quest_SJZ_Complete")
					default_wait = 5000
				else
					break 
				end
			end
		else
			if boss_done == true and GetEvent( "EVENT_PICKUP" , 0 ) > 0 then
				break 
			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("в" , "ֹͣ⹥" , bStopSpecialAttack )
		AI:SetParameter("в" , "ٻ" , bCallPet )

		AI:SetAdv("" , FollowTarget , 3 )
		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
			DbgPrintf("ָɱ yaoxin %s %d" , tostring(FingerAttackName) , tonumber(FingerAttack))
		end
	end
	AI:Stop()
end


function _SMain(...)

	l_param_third = gscript_check( 0 , 0 , 1 )
	l_param_lzby = gscript_check( 1 , 0 , 1 )
	local ask_third = false 

	LogChatMsg(0)
	LogChatMsg(2)

	while true do
		
		-- жǷȡ
		local nComplete , idMission, idScript , sMissionName , sIn = GetMissionComplete() 

		if GetActiveDataSceneID() ~= g_MissionMap then
		--ڸ
			if nComplete == INVALID_ID then

				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

				-- ж϶
				if DataPool:GetTeamMemCount() < 3 and false then 
					ShowMessage(" 3 ˣ")
					return
				end

				MoveToNPC(200, 333, 1, "»")
				QuestFrameOptionClicked( g_XukongMission[1].vin , CMP_FULL_MATCH)
				QuestFrameAcceptClicked()

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

				if strType == "text" then
					if string.find(szText, "") then
						ShowMessage("Ѿ")
						break	-- ˳ѭű
					elseif string.find(szText , "λ") then
						ShowMessage("ո,ͣű") 
						break
					end
				end

				System:Sleep(1000)

			elseif nComplete == 1 then

				MoveToNPC(200, 333, 1, "»")
				QuestFrameOptionClicked( sMissionName , CMP_FULL_MATCH)
				QuestFrameMissionComplete()	-- 


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

				if strType == "text" then
					if string.find(szText, "") then
						ShowMessage("Ѿ")
						break	-- ˳ѭű
					elseif string.find(szText , "λ") then
						ShowMessage("ո,ͣű") 
						break
					end
				end

			elseif nComplete == 0 then
				if sMissionName == script_name then
					MoveToNPC(194, 214, 1, "ݼݼ")
					QuestFrameOptionClicked( sIn , CMP_FULL_MATCH)
					QuestFrameOptionClicked("ǰľׯ", CMP_FULL_MATCH)
				else
					MoveToNPC(200, 333, 1, "»")
					System:Sleep(1000)
					local sOpt = GetQuestFrameOption(1)
					if sOpt == script_name then
						--,npcṩҪִе,߲һʱ,ϵ
						QuestFrameMissionAbnegate(idScript)
					elseif sOpt ~= "" then
						ShowMessage(" %s , ִнű" , sOpt)
						break
					end
					
				end
			else
				QuestFrameMissionAbnegate(idScript)
			end

		else
			if nComplete ~= 0 then
				ShowMessage("뿪ľׯ")
				local t_self_x , t_self_y = Player:GetPos()
				local t_area = GetLocalID( g_MissionMap , t_self_x , t_self_y )
				if t_area < 0 then
					t_area = 0 
				end
				MoveToNPC(g_pqq_pos[t_area].x , g_pqq_pos[t_area].y , g_MissionMap , "ݼݼ" )
				QuestFrameOptionClicked("뿪", CMP_FULL_MATCH)
			else

				-- ȡǰ
				local t_self_x , t_self_y = Player:GetPos()
				local t_area = GetLocalID( g_MissionMap , t_self_x , t_self_y )
				if t_area < 1 then
					t_area = 1
				end

				local _,t_pqq_x , t_pqq_y = FindObjectByName( "ݼݼ" , "NPC")
				local t_pqq_dis = GetDist( t_pqq_x , t_pqq_y , g_pqq_pos[0].x ,g_pqq_pos[0].y )
				if t_pqq_dis < 6 then
					if Player:IsRangeAttack() == false then
						Pet_Relax()
						if INVALID_ID ~= Player:GetBuffByName("") then
							--ƶԱ,Ի
							Dismount()
							UpdateMsg("Stop_UseItem")
							MoveTo(101,91,g_MissionMap,"" ,2,false,nil,false,1)
							UpdateMsg("Stop_UseItem")
							MoveTo(103,80,g_MissionMap,"" ,2,false,nil,false,1)
							UpdateMsg("Stop_UseItem")
							AI:SetAI("ɱ")
							AI:SetParameter("ʼ")
							AI:SetParameter("ͼ", g_MissionMap)
							AI:SetParameter("", "" , 103,80 )
							AI:SetParameter("Ŀ" , "" , "ľ")
							AI:StartEx(0)
							AI:SetParameter("в", "ٻ" , 0 )
							AI:SetParameter("в", "ֹͣ" , 1 )
							AI:SetParameter("в", "ֹܸͣ" , 1 )
							AI:SetParameter("в", "ֹͣ⹥" , 1 )
							AI:StartEx(1)
							while true do 
								UpdateMsg("Stop_UseItem")
								local channel, time, name, data = GetChatMsg()
								local now = System:timeGetTime()
								if channel ~= INVALID_ID and now < time + 10 then 
									if string.find(data, "SJZ_100129_41") then
										if g_boss["NPC"] == INVALID_ID then
											g_boss["NPC"] = 99999
										end
										break
									elseif string.find(data,"ͽͷ׷뿪ľ") then
										if g_boss["NPC"] == INVALID_ID then
											g_boss["NPC"] = 99999
										end	
										break
									end
								end

								if INVALID_ID == Player:GetBuffByName("") then
									break
								end
								System:Sleep(1000)
							end
							AI:Stop()
							DelMsg("Stop_UseItem")
						else
							Dismount()
							MoveToNPC(g_pqq_pos[0].x , g_pqq_pos[0].y , g_MissionMap , "ݼݼ" )
							QuestFrameOptionClicked("ȷ", CMP_PART_MATCH)
						end
					else
						OnFightBoss( 0 )
					end
				else
					local t_fight_pos = OnNpcEvent( t_area )
					DbgPrintf("ǰսΪ %d" , t_fight_pos)
					if t_fight_pos ~= INVALID_ID then
						OnFightBoss( t_fight_pos )
					end
				end
			end

		-- жǷڸ
		end

		System:Sleep(1000)
	end

	LogChatMsg(-1)
end

_SMain()
