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

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

script_name="ľׯԹ"
local nSetNoFight = 0

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

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

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 IsFuben()
	if GetActiveDataSceneID() ~= nFubenMapId then
		return false 
	end
	return true 
end

local l_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 l_boss = {
	["ݼݼNPC"] = INVALID_ID ,
	["ī"] = INVALID_ID ,
	[""] = INVALID_ID ,
	[""] = INVALID_ID ,
	[""] = INVALID_ID ,
	[""] = INVALID_ID ,
	["ӥ"] = INVALID_ID ,
	["ī"] = INVALID_ID ,
	[""] = INVALID_ID ,
	[""] = INVALID_ID ,
	[""] = INVALID_ID ,
	["ʿ"] = INVALID_ID ,
	["ʮһɱ"] = INVALID_ID , 
	["ľׯͷ"] = INVALID_ID ,
	["ľׯ"] = INVALID_ID ,
	["ľ"] = INVALID_ID ,
	["Ӱҡ"] = INVALID_ID 
}

local BOSS_RECORD_INIT = 0
local BOSS_RECORD_MET = 1
local BOSS_RECORD_DEAD = 2

function reset_sjz()
	g_state_sjz= {}
	g_state_sjz["ľ"] = BOSS_RECORD_INIT
	g_state_sjz["ī"] = BOSS_RECORD_INIT
	g_state_sjz[""] = BOSS_RECORD_INIT
	g_state_sjz[""] = BOSS_RECORD_INIT
	g_state_sjz[""] = BOSS_RECORD_INIT
end



function set_boss_record( name , die )
	if name == "ľ" then
		--skip
	elseif name == "ī" then
		g_state_sjz["ľ"] = BOSS_RECORD_DEAD
	elseif name == "" then
		g_state_sjz["ľ"] = BOSS_RECORD_DEAD
		g_state_sjz["ī"] = BOSS_RECORD_DEAD
	elseif name == "" then
		g_state_sjz["ľ"] = BOSS_RECORD_DEAD
		g_state_sjz["ī"] = BOSS_RECORD_DEAD
		g_state_sjz[""] = BOSS_RECORD_DEAD
	elseif name == "" then
		g_state_sjz["ľ"] = BOSS_RECORD_DEAD
		g_state_sjz["ī"] = BOSS_RECORD_DEAD
		g_state_sjz[""] = BOSS_RECORD_DEAD
		g_state_sjz[""] = BOSS_RECORD_DEAD
	else
		return
	end

	if die == true then
		g_state_sjz[name] = BOSS_RECORD_DEAD
	else
		g_state_sjz[name] = BOSS_RECORD_MET
	end
end

function ResetBoss()
	l_boss["ݼݼ"] = INVALID_ID 
	l_boss["ī"] = INVALID_ID 
	l_boss[""] = INVALID_ID 
	l_boss[""] = INVALID_ID 
	l_boss[""] = INVALID_ID 
	l_boss[""] = INVALID_ID 
	l_boss["ӥ"] = INVALID_ID 
	l_boss["ī"] = INVALID_ID 
	l_boss[""] = INVALID_ID 
	l_boss[""] = INVALID_ID 
	l_boss[""] = INVALID_ID 
	l_boss["ʿ"] = INVALID_ID
	l_boss["ʮһɱ"] = INVALID_ID 
	l_boss["ľׯͷ"] = INVALID_ID 
	l_boss["ľׯ"] = INVALID_ID 
	l_boss["Ӱҡ"] = INVALID_ID
	l_boss["ľ"] = INVALID_ID 
end

if g_state_sjz == nil then
	reset_sjz()
end

local l_main_pos =
{
	[0] = {x=101,y=94 , r=15} , --{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 l_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 l_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

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

function UpdateData()

	if GetActiveDataSceneID() ~= nFubenMapId then
		return nil,""
	end

	ResetBoss()

	local t_hyf_near_dis = 99999

	local n = EnumObject(true)

	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
				l_boss["ľ"] = idObj
			elseif string.find(nameObj , "ī") then
				if Character:GetData(idObj, "DIE") == false then
					l_boss["ī"] = idObj
				end
			elseif string.find(nameObj , "ľׯͷ") then
				if Character:GetData(idObj, "DIE") == false then
					l_boss["ľׯͷ"] = idObj
				end
			elseif string.find(nameObj , "ľׯ") then
				if Character:GetData(idObj, "DIE") == false then
					l_boss["ľׯ"] = idObj
				end
			else
				if l_boss[nameObj] ~= nil then
					if Character:GetData(idObj, "DIE") == false then
						l_boss[nameObj] = idObj
					end
				end
			end
			set_boss_record( nameObj , Character:GetData(idObj, "DIE") )
		elseif classObj == "NPC" then
			if nameObj == "ݼݼ" then
				l_boss["ݼݼ"] = 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 , l_step2_pos[i].x , l_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 l_boss[""] == INVALID_ID or t_this_near_dis < t_hyf_near_dis then
					l_boss[""] = idObj
					t_hyf_near_dis = t_this_near_dis
					DbgPrintf(" %d  %d,%d, %d " , l_boss[""] , xObj , yObj , t_this_step )
				end

				AI:SetAdv("в" , idObj , 4 , "")
			end

			if nameObj ~= "ľ" then
				set_boss_record( nameObj , true )
			else
				set_boss_record( nameObj , false )
			end			
		elseif classObj == "SPECIAL" then
			if string.find( nameObj , "ʮһɱ") then
				l_boss["ʮһɱ"] = idObj
				AI:SetAdv("в" , idObj , 4 , "ʮһɱ")
			elseif nameObj == "" then
				l_boss[""] = idObj
			elseif nameObj == "" then
				l_boss[""] = idObj
				if l_param_lzby == 1 then
					AI:SetAdv("в" , idObj , 4 , "")
				end
			elseif string.find( nameObj , "") then 
				l_boss[""] = idObj
				AI:SetAdv("в" , idObj , 4 , "")
			end
		elseif classObj == "PLAYER" or classObj == "PET" then
			local iPos,_,_ = CheckBufferByName( idObj, "Ӱҡ")
			if INVALID_ID ~= iPos then
				l_boss["Ӱҡ"] = idObj
			end
		end
	end

	local now = System:timeGetTime()

	while true do 
		local channel, time, name, data = GetChatMsg()
		if channel == INVALID_ID then 
			break 
		end

		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
				--ᦳ
				if flag_step2 == 0 then
					flag_step2 = 1 
				end
			elseif string.find(data, "SJZ_100129_60") then
				set_boss_record("" , true)
			elseif string.find(data, "SJZ_100129_46") then
				set_boss_record("ī" , true)
			elseif string.find(data, "SJZ_100129_71") then
				set_boss_record("" , true)
			elseif string.find(data, "SJZ_100129_105") then
				set_boss_record("" , true)
			elseif string.find(data, "SJZ_100129_41") then
				--
				set_boss_record( "ľ" , true )
			elseif string.find(data,"ͽͷ׷뿪ľ") then
				--Ѿ
				set_boss_record( "ľ" , true )
			end
		end
	end

	if l_boss["ľׯ"] ~= INVALID_ID or l_boss["ľׯͷ"] ~= INVALID_ID then
		return process_boss_kill , 0
	elseif l_boss["ī"] ~= INVALID_ID or l_boss[""] ~= INVALID_ID then
		return process_boss_kill , 1
	elseif l_boss[""] ~= INVALID_ID or l_boss[""] ~= INVALID_ID then
		return process_boss_kill , 2
	elseif l_boss[""] ~= INVALID_ID or l_boss["ӥ"] ~= INVALID_ID or l_boss["ī"] ~= INVALID_ID or l_boss[""]~= INVALID_ID or l_boss[""]~= INVALID_ID then
		return process_boss_kill , 3
	elseif l_boss[""] ~= INVALID_ID or l_boss["ʿ"]~= INVALID_ID or l_boss["ʮһɱ"]~= INVALID_ID then
		return process_boss_kill , 4
	end

	return nil,""
end

function check_pqq()
	local vResult = UpdateData()
	if vResult ~= nil then
		return false
	end

	return true
end

local process_npc_faild = 0 
--Իݼݼ
function process_npc_t( nProcess_chk )

	MoveTo(l_pqq_pos[nProcess_chk].x , l_pqq_pos[nProcess_chk].y , nFubenMapId , "ݼݼ" , 4 , false , check_pqq , true )
	if check_pqq() == false then
		return nil,nil
	end

	MoveToNPC( l_pqq_pos[nProcess_chk].x , l_pqq_pos[nProcess_chk].y , nFubenMapId , "ݼݼ" )

	--жԻѡ±ȽϺô,ֱӿȷǰҪʲô
	--ûжԻѡ,翨,Ҳս
	--жһ,Ϊ翨,
	--boss die=1 Ϊ翨
	--Ѷͬһ,Ϊbossδɱ
	while true do

		local list_count , npc_id = DataPool:GetNPCEventList_Num()
		if npc_id == INVALID_ID then
			--Իʧ,ҲѾս,ԶԻ3,ɹ򵽳Ѱboss
			process_npc_faild = process_npc_faild + 1
			if process_npc_faild >= 3 then
				process_npc_faild = 0 
				return process_boss_kill , nProcess_chk
			else
				return nil,nil
			end
		end

		process_npc_faild = 0

		if list_count < 1 then

			--bossû , ,ܷboss
			return process_boss_kill , nProcess_chk

		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
						set_boss_record("" , true)
						if t_click < 4 then
							t_click = 4
						end
					elseif string.find(szText , "͵") then
						set_boss_record("" , true)
						if t_click < 3 then
							t_click = 3
						end
					elseif string.find(szText , "͵ϸ") then
						set_boss_record("ī" , true)
						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 process_boss_kill , nProcess_chk
			elseif t_click == 6 then
				QuestFrameOptionClicked("Эľׯ", CMP_PART_MATCH)
				return nil,nil
			elseif t_click == 5 then
				QuestFrameOptionClicked("ȥȡ", CMP_PART_MATCH)
				MoveToNPC( l_pqq_pos[nProcess_chk].x , l_pqq_pos[nProcess_chk].y , nFubenMapId , "ݼݼ" )
				--ȴ
			elseif t_click == 4 then
				QuestFrameOptionClicked("͵̳", CMP_PART_MATCH)
				return nil,nil
			elseif t_click == 3 then
				QuestFrameOptionClicked("͵", CMP_PART_MATCH)
				return nil,nil
			elseif t_click == 2 then
				QuestFrameOptionClicked("͵ϸ", CMP_PART_MATCH)
				return nil,nil
			elseif t_click == 1 then
				QuestFrameOptionClicked("ȷ", CMP_PART_MATCH)
				return nil,nil
			else
				--öԻ޺ѡ
				ShowMessage("ݼݼ ĶԻûзֺѡ")
				return nil,nil
			end
		end
	
		local cur_process , cur_param = UpdateData()
		if cur_process~= nil then
			return nil,nil
		end
	end

end



function process_boss_kill( nBossStep )

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

	if pos < 0 then
		pos = 0
	end

	flag_step0 = 0 	-- ֽͷʱı
	flag_step2 = 0	-- һֻ,ı
	flag_step3 = 0	-- ӥ,ı
	flag_step4 = 0	-- ʾϢ,ı , ʮһɱ,ı
	
	AI:SetAdv( "" )
	if nBossStep == 1 or nBossStep == 0 then
		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( "Ȧ" , l_main_pos[nBossStep].x , l_main_pos[nBossStep].y , l_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( "Ȧ" , l_main_pos[nBossStep].x , l_main_pos[nBossStep].y , l_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( "Ȧ" , l_main_pos[nBossStep].x , l_main_pos[nBossStep].y , l_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( "Ȧ" , l_main_pos[nBossStep].x  , l_main_pos[nBossStep].y  , l_main_pos[nBossStep].r )
		end
	end

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

	local result = nil
	local r_param = nil

	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)

		result , r_param = UpdateData()

		local find_boss = true
		if result ~= process_boss_kill or r_param ~= nBossStep then
			if IsMsgCold("Quest_SJZ_Complete" , 5000 ) then
				break
			elseif nBossStep == 1 and g_state_sjz["ī"] == BOSS_RECORD_DEAD then
				break 
			elseif nBossStep == 2 and g_state_sjz[""] == BOSS_RECORD_DEAD then
				break 
			elseif nBossStep == 3 and g_state_sjz[""] == BOSS_RECORD_DEAD then
				break 
			elseif nBossStep == 4 and g_state_sjz[""] == BOSS_RECORD_DEAD then
				break 
			else
				--5δboss,Զ
				find_boss = false 
			end
				
		else
			UpdateMsg("Quest_SJZ_Complete")
		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
			--ͷ
		elseif nBossStep == 1 then
			--1 :ɱ ī
			--ӳ,սı
		elseif nBossStep == 2 then
			--2 :ɱ 
			if l_boss[""] ~= INVALID_ID then
				if flag_step2 == 0 then
					flag_step2 = 1 
				end
			end

			--ܻ 1,ӳ 2,ĸ 3,Խ

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

			if flag_step2 == 0 and t_BossHPPercent > 50 then
				bCallPet = 0 
			else
				if flag_step2 == 0 then
					flag_step2 = 1 
				end

				if t_followleader == true then
					--ɸӳʱ,Աӳ
					FollowTarget = LeaderId
					bTeamFollow = 1
				else
					--ɸʱ,ҪԼ,޸սұս
					if l_boss[""] ~= INVALID_ID and IsMsgCold("Quest_SJZ_SETHYF" , 10000) then
					
						local _,t_hyf_x,t_hyf_y = FindObjectByID( l_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 , l_step2_pos[i].x , l_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

							UpdateMsg("Quest_SJZ_SETHYF")
							AI:SetAdv( "Ȧ" , l_step2_pos[flag_step2].x , l_step2_pos[flag_step2].y , 4 )
							DbgPrintf(",ǰȦ %d , %d,%d" , flag_step2 , l_step2_pos[flag_step2].x , l_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 l_boss[""] ~= INVALID_ID then
						
							local _,t_by_x,t_by_y = FindObjectByID( l_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 l_boss[""] ~= INVALID_ID or l_boss[""] ~= INVALID_ID then
							bTeamFollow = 1
							FollowTarget = LeaderId
						end
					end
				end
			end

		elseif nBossStep == 4 then
			-- 4 :ɱ 
			if flag_step4 == 1 then
				if l_param_lzby == 0 then
					AI:SetAdv( "Ȧ" , l_main_pos[nBossStep].x , l_main_pos[nBossStep].y , l_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( "Ȧ" , l_main_pos[nBossStep].x , l_main_pos[nBossStep].y , l_main_pos[nBossStep].r )
			end
		end					
		
		if l_boss[""] ~= INVALID_ID then
			local t_iPos,_,_ = CheckBufferByName(l_boss[""] , "" ) 
			if t_iPos ~= INVALID_ID then
				bStopSpecialAttack = 1 
				flag_step3 = 1
				bFixPlace = 1
				bKillMonster = 0
			end
		elseif l_boss[""] ~= INVALID_ID then
			local t_iPos,_,_ = CheckBufferByName(l_boss[""] , "" ) 
			if t_iPos ~= INVALID_ID then
				bStopSpecialAttack = 1 
				bKillMonster = 0
			end
		end
				
		if l_boss[""] ~= INVALID_ID then
			local t_self_x , t_self_y = Player:GetPos()
			if GetDist( t_self_x , t_self_y , l_step2_pos[flag_step2].x ,l_step2_pos[flag_step2].y ) > 4 then
				bKillMonster = 0
				bStopAssistSelf = 1
				bStopAssist = 1
			end
		elseif l_boss["ӥ"] ~= INVALID_ID then
			AI:SetAdv("ָɱ" , 0 , "ӥ" )
			flag_step3 = 1
			bStopSpecialAttack = 0
			bYinGuai = 1
			bKillMonster = 1
		elseif l_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 l_boss["ʿ"] ~= INVALID_ID then
			AI:SetAdv("ָɱ" , 0 , "ʿ" )
			bStopSpecialAttack = 0
			bYinGuai = 1
			bKillMonster = 1
		elseif l_boss["ľׯ"] ~= INVALID_ID then
			bYinGuai = 1
		elseif l_boss["ľׯͷ"] ~= INVALID_ID then
			bYinGuai = 1
		else
			AI:SetAdv("ָɱ" , -1 , "" )
			bTeamFollow = 1
			bTeamAttack = 0
		end

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

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

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

		if l_boss[""] == INVALID_ID then
			AI:SetAdv("ɾв" , "")
		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 l_boss["Ӱҡ"] ~= INVALID_ID then
			UpdateMsg("Quest_SJZ_Complete")

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

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

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

			if nHPPercent < 60 or nMPPercent < 10 then
				bStopSpecialAttack = 1
			end
		end

		if find_boss == false then

			local t_self_x , t_self_y = Player:GetPos()
			local t_set_x = l_main_pos[nBossStep].x
			local t_set_y = l_main_pos[nBossStep].y
			local t_set_r = l_main_pos[nBossStep].r

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

			if GetDist( t_self_x , t_self_y , t_set_x , t_set_y ) > 4 then
				AI:SetAdv( "Ȧ" , t_set_x , t_set_y , t_set_r )
				UpdateMsg("Quest_SJZ_Complete")
			else
				break 
			end
		end

		if nSetNoFight > 0 then
			bKillMonster = 0 
			bYinGuai = 0 
			bTeamAttack = 0 
			bYinshen = 1
			bStopAssist = 1
			bStopAssist = 1
		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()


	OnSweepBattleField()

	-- nil,""
	return nil , nil
end

function process_exit()
	ShowMessage("뿪ľׯ")
	local t_self_x , t_self_y = Player:GetPos()
	local t_area = GetLocalID( nFubenMapId , t_self_x , t_self_y )
	if t_area < 0 then
		t_area = 0 
	end
	MoveToNPC(l_pqq_pos[t_area].x , l_pqq_pos[t_area].y , nFubenMapId , "ݼݼ" )
	QuestFrameOptionClicked("뿪", CMP_FULL_MATCH)
end

function check_sjz()
	local vResult = UpdateData()
	if vResult ~= nil then
		return false
	end

	if g_state_sjz["ľ"] == BOSS_RECORD_DEAD then
		return false
	end
	
	return true
end

function process_qiao_zhong()
	--udpate  process_qiao_zhang  nil , űڵһ , ҽ븱ʱ 30

	if Player:IsRangeAttack() == true then
		return 
	end

	Pet_Relax()

	if INVALID_ID ~= Player:GetBuffByName("") then
		--ƶԱ,Ի
		Dismount()
		UpdateMsg("Stop_UseItem")
		MoveTo(101,91,nFubenMapId,"" ,2,false,check_sjz,false,1)
		UpdateMsg("Stop_UseItem")
		MoveTo(103,80,nFubenMapId,"" ,2,false,check_sjz,false,1)
		UpdateMsg("Stop_UseItem")
		AI:SetAI("ɱ")
		AI:SetParameter("ʼ")
		AI:SetParameter("ͼ", nFubenMapId)
		AI:SetParameter("", "" , 103,80 )
		AI:SetParameter("Ŀ" , "" , "ľ")
		AI:StartEx(0)
		AI:SetParameter("в", "ٻ" , 0 )
		AI:SetParameter("в", "ֹͣ" , 1 )
		AI:SetParameter("в", "ֹܸͣ" , 1 )
		AI:SetParameter("в", "ֹͣ⹥" , 1 )
		AI:StartEx(1)
		local vResult , vParam = UpdateData()
		while true do 
			if vResult ~= nil then
				if vResult ~= process_boss_kill and vParam ~= 0 then
					break
				end
			end 

			UpdateMsg("Stop_UseItem")
			if INVALID_ID == Player:GetBuffByName("") then
				break
			end

			System:Sleep(1000)

			vResult , vParam = UpdateData()
		end
		AI:Stop()
		DelMsg("Stop_UseItem")
	else
		Dismount()
		MoveToNPC(l_pqq_pos[0].x , l_pqq_pos[0].y , nFubenMapId , "ݼݼ" )
		QuestFrameOptionClicked("ȷ", CMP_PART_MATCH)
	end
end

function FindProcess()

	--ȷǰҪʲô,Ǹս븱Ҫ
	local _,t_pqq_x , t_pqq_y = FindObjectByName( "ݼݼ" , "NPC")
	local t_pqq_dis = GetDist( t_pqq_x , t_pqq_y , l_pqq_pos[0].x ,l_pqq_pos[0].y )
	--ݼݼ1  нսְҵ  ľӵ״̬Ϊ INIT  ڽ븱30 
	DbgPrintf("test:%s , %d , %d , %s" , tostring(TestMenpai()) , tonumber(t_pqq_dis) , tonumber(g_state_sjz["ľ"]) , tostring(IsMsgCold("Quest_Map_Enter" , 30000 )) )
	if TestMenpai() == true and t_pqq_dis < 6 and g_state_sjz["ľ"] == BOSS_RECORD_INIT and IsMsgCold("Quest_Map_Enter" , 30000 ) == false then
		-- нսְҵ  븱30
		if Player:IsRangeAttack() then
			PushDebugMessage("ȴ..." ,1)
			return nil , ""
		else
			return process_qiao_zhong , ""
		end
	elseif IsMsgCold("Quest_Map_Enter" , 3000 ) == false then
		--npc ûˢ
		return nil , ""
	end
	
	--ȷǰҪʲô,ҵǰnpc("ݼݼ"),ͨԻȷһ
	local t_self_x , t_self_y = Player:GetPos()
	local t_area = GetLocalID( nFubenMapId , t_self_x , t_self_y )
	if t_area < 1 then
		t_area = 1
	end

	return process_npc_t , t_area
end

local function OnCommonMission()
	local ask_third = false 
	local task = nil 
	local task_param = nil

	while true do
		
		-- жǷȡ
		local nComplete = GetMissionComplete() 

		if IsFuben() == false then
		--ڸ

			DelMsg("Quest_Map_Enter")

			if nComplete == INVALID_ID then
				--δ
				--б־
				reset_sjz()
				-- ж϶
				if DataPool:GetTeamMemCount() < 3 and false then 
					ShowMessage(" 3 ˣ")
					return
				end

				MoveToNPC(194, 214, 1, "ݼݼ")
				if ask_third == true then
					QuestFrameOptionClicked("ľׯ", CMP_FULL_MATCH)
					QuestFrameOptionClicked("ȷ", CMP_FULL_MATCH)
				else
					QuestFrameOptionClicked("ľׯԹ", CMP_FULL_MATCH)
				end

				QuestFrameAcceptClicked()

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

				if strType == "text" then
					if string.find(szText, "Ѿδׯ") then
						if l_param_third == 1 then
							ask_third = true 
						else
							ShowMessage("Ѿľ")
							break	-- ˳ѭű
						end
					elseif string.find(szText , "ϺûôǮ") then
						ShowMessage("Ǯ,޷ӵ3ľ")
						break	-- ˳ѭű
					elseif string.find(szText , "δׯ" ) then
						ShowMessage("Ѿľ")
						break	-- ˳ѭű
					end
				end

				System:Sleep(1000)

			elseif nComplete == 1 then
				MoveToNPC(194, 214, 1, "ݼݼ")
				QuestFrameOptionClicked("ľׯԹ", CMP_FULL_MATCH)
				QuestFrameMissionComplete()	-- 
			elseif nComplete == 0 then
				MoveToNPC(194, 214, 1, "ݼݼ")
				QuestFrameOptionClicked("ľׯԹ", CMP_FULL_MATCH)
				QuestFrameOptionClicked("ǰľׯ", CMP_FULL_MATCH)
			else
				reset_sjz()
				QuestFrameMissionAbnegate(g_idScript)
			end

		else
			UpdateMsg( "Quest_Map_Enter" , 1 )	--Quest_Map_Enter Ϣʱһ

			if nComplete ~= 0 then
				process_exit()
			else
				local process , param = UpdateData()
				if process~= nil then
					task , task_param = process(param)
				elseif task ~= nil then
					task , task_param = task( task_param )
				else
					task , task_param = FindProcess()
				end
			end

		end

		System:Sleep(1000)
	end
end

local function OnXuKongMission()

	script_name = "սľׯ"
	local ask_third = false 
	local task = nil 
	local task_param = nil
	reset_sjz()

	while true do
		
		-- жǷȡ
		local nComplete = GetMissionComplete() 

		if IsFuben() == false then
		--ڸ

			DelMsg("Quest_Map_Enter")

			if nComplete == 0 then
				MoveToNPC(194, 214, 1, "ݼݼ")
				if DataPool:GetTeamMemCount() < 3 then 
					g_XukongRet = 1
					ShowMessage(" 3 ˣ")
					return
				end

				QuestFrameOptionClicked("սľׯ", CMP_FULL_MATCH)
				QuestFrameOptionClicked("ǰľׯ", CMP_FULL_MATCH)
			else
				break
			end

		else
			UpdateMsg( "Quest_Map_Enter" , 1 )	--Quest_Map_Enter Ϣʱһ

			if nComplete ~= 0 then
				process_exit()
			else
				local process , param = UpdateData()
				if process~= nil then
					task , task_param = process(param)
				elseif task ~= nil then
					task , task_param = task( task_param )
				else
					task , task_param = FindProcess()
				end
			end

		end

		System:Sleep(1000)
	end
end

function _SMain(...)

	l_param_third = gscript_check( 0 , 0 , 1 )
	l_param_lzby = gscript_check( 1 , 0 , 1 )
	nSetNoFight = GetScriptParam( script_name , "attr" , SCRIPT_ATTR_NOFIGHT )

	LogChatMsg(0)
	LogChatMsg(2)

	local isXK = false
	if _CommandLoad ~= nil then
		local vCmd = tostring(_CommandLoad)
		if vCmd == "" then
			isXK = true
		end
	end

	if isXK == true then
		OnXuKongMission()
	else
		OnCommonMission()
	end

	LogChatMsg(-1)
end

_SMain()

