
--[[
[author]
class=1
attr=1
descript = ĬϵڶɱĦǣɶӳ֡ѡɱĽݸѡʱűȴĽݸʹöתƼܣ֮9밲ȫʱڽйȷᱻĽݸļܷ
chk0str = ֱ֣Ӵ
chk1str = ɱĽݸ
]]--

script_name = "ַ"
local nFubenMapId = 0xec
local nSetNoFight = 0
local nSetKillJmz = 0
local nSetSafeKill = 0

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


local function reset_yzw()
	l_state_yzw = {}
	l_state_yzw[""] = BOSS_RECORD_INIT
	l_state_yzw["Ħ"] = BOSS_RECORD_INIT
	l_state_yzw["Ľݸ"] = BOSS_RECORD_INIT
	l_state_yzw["Ԯ"] = 0
	l_state_yzw["λ"] = 0
end

if l_state_yzw == nil then
	reset_yzw()
end

local l_boss = {
	[""] = 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 l_npc = {
	["ӱ"] = {x=79,y=202,qs="սɣ",name="ӱ",next="Ǯ"} ,
	["Ǯ"] = {x=216,y=149,qs="սɣ",name="Ǯ",next=""} ,
	[""] = {x=182,y=89,qs="ӵص",name="",next="Ľݸ"} ,
	["Ľݸ"] = {x=52,y=72,qs="",name="Ľݸ",next="Ľݸ"}
}

function ResetBoss()
	for i,v in pairs(l_boss) do
		l_boss[i] = INVALID_ID
	end
end

function IsFuben()
	if GetActiveDataSceneID() ~= nFubenMapId then
		return false 
	end
	return true 
end

function set_boss_record( name , npc , die )

	if name == "" then
		l_state_yzw["λ"] = 0
		l_state_yzw["Ħ"] = BOSS_RECORD_INIT
		l_state_yzw["Ľݸ"] = BOSS_RECORD_INIT
		if npc == false then
			if die == true then
				l_state_yzw[""] = BOSS_RECORD_DEAD
			else
				l_state_yzw[""] = BOSS_RECORD_MET
			end
		else
			l_state_yzw[""] = BOSS_RECORD_INIT
		end
	elseif name == "ľ˿" or name == "ެ" or name == "ɵ" or name == "Ҧ" or name == "˾" or name == "ؼկױ" then
		l_state_yzw["Ľݸ"] = BOSS_RECORD_INI
		l_state_yzw[""] = BOSS_RECORD_DEAD
		if l_state_yzw["Ħ"] ~= BOSS_RECORD_DEAD then
			l_state_yzw["Ħ"] = BOSS_RECORD_MET
		end
	elseif name == "Ħ" then
		if l_state_yzw["Ľݸ"] == BOSS_RECORD_INIT then
			l_state_yzw["Ľݸ"] = BOSS_RECORD_INIT
			l_state_yzw[""] = BOSS_RECORD_DEAD
			if npc == false then
				if die == true then
					l_state_yzw["Ħ"] = BOSS_RECORD_DEAD
				else
					l_state_yzw["Ħ"] = BOSS_RECORD_MET
				end
			else
				l_state_yzw["λ"] = 0
				l_state_yzw["Ħ"] = BOSS_RECORD_INIT
			end
		end
	elseif name == "Ľݸ" then
		l_state_yzw["Ħ"] = BOSS_RECORD_DEAD
		l_state_yzw[""] = BOSS_RECORD_DEAD
		if die == true then
			l_state_yzw["Ľݸ"] = BOSS_RECORD_DEAD
		else
			l_state_yzw["Ľݸ"] = BOSS_RECORD_MET
		end
	end
end

function SelectFuben2()
	if nSetKillJmz == 1 then
		return OnFuben2 , "Ħ"
	elseif DataPool:IsTeamLeader() == true then
		if l_state_yzw["λ"] == 0 then
			return OnFuben2NoKill , "Ħ"
		end
	end
	return OnFuben2NoKillMember , "Ħ"
end

function UpdateData()

	if IsFuben() == false then
		return nil , 0
	end

	ResetBoss()

	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 l_boss[nameObj] ~= nil then
				if Character:GetData(idObj, "DIE") == false then
					l_boss[nameObj] = idObj
				end
			end
			set_boss_record( nameObj , false , Character:GetData(idObj, "DIE") )
		elseif classObj == "NPC" then
			if l_boss[nameObj] ~= nil then
				l_boss[nameObj] = idObj
			end
		end
	end

	local isNext,szMsg = GetDebugMessage()
	while isNext == 1 do
		DbgPrintf("dbg msg %s" , tostring(szMsg))
		if szMsg ~= nil then
			if string.find( szMsg , "иҪɱӱ") then
				set_boss_record("" , true , false)
			elseif string.find( szMsg , "ЭǮԮ") then
				set_boss_record( "Ħ" , true , false)
			elseif string.find( szMsg , "ԮѾ") then
				set_boss_record( "Ħ" , false , false)
				local _,_,vTick = string.find( szMsg ,"(%d+)ԮѾ" )
				l_state_yzw["Ԯ"] = tonumber(vTick)
				if l_state_yzw["Ԯ"] == 9 and nSetKillJmz ~= 1 then
					set_boss_record( "Ľݸ" , false , false )
				end
			elseif string.find( szMsg , "ڲ") then
				set_boss_record( "Ľݸ" , false , false )
			elseif string.find( szMsg , "Ľݸѱ" ) then
				set_boss_record( "Ľݸ" , false , true )
			end				
		end
		isNext,szMsg = GetDebugMessage()
	end

	--öԱǰ׼
	if l_boss[""] ~= INVALID_ID or l_boss["к"] ~= INVALID_ID or l_boss["Ҷ"] ~= INVALID_ID or l_boss[""] ~= INVALID_ID then
		return OnFuben1 , ""
	elseif l_boss["Ľݸ"] ~= INVALID_ID then
		return process_boss_kill , "Ľݸ"
	elseif l_boss["ľ˿"] ~= INVALID_ID or l_boss["ެ"] ~= INVALID_ID or l_boss["Ħ"] ~= INVALID_ID or l_boss["ؼկױ"]~= INVALID_ID then
		return SelectFuben2()
	elseif l_boss["ɵ"] ~= INVALID_ID or l_boss["Ҧ"] ~= INVALID_ID or l_boss["˾"] ~= INVALID_ID then
		return SelectFuben2()
	end

	return nil , 0
end

function _callback(fX, fY, nScene)

	--¸npc жԻ
	if IsFuben() == false then
		return false
	end

	local result , param = UpdateData()
	if result == nil or result == process_npc_t then
		if DataPool:IsTeamLeader() == true and GetMount() ~= INVALID_ID then
			Player:EnableTeamFollow(true)
		end
		return true 
	end

	return false
end

function _callback_withoutfollow(fX, fY, nScene)

	--¸npc жԻ
	if IsFuben() == false then
		return false
	end

	local result , param = UpdateData()
	if result == nil or result == process_npc_t then
		return true 
	end

	return false
end

function _breakmove_infuben(fX, fY, nScene)

	if IsFuben() == false then
		return true
	end

	return false
end

function move_npc( p_npc )

	if IsDestination( l_npc[p_npc].x , l_npc[p_npc].y , nFubenMapId  , 3 ) == true  then

		UpdateData()
		--if l_boss[p_npc] == INVALID_ID then
			--npc
		--	ShowMessage("󲻴")
		--	return move_npc , l_npc[p_npc].next
		--end
	end

	if p_npc == "Ľݸ" then
		MoveInMap( l_npc[p_npc].x , l_npc[p_npc].y , nFubenMapId , 2 , _callback )
	elseif p_npc == "Ǯ" then
		MoveToNPC( l_npc[p_npc].x , l_npc[p_npc].y , nFubenMapId , l_npc[p_npc].name , true , false , _callback_withoutfollow )
	else
		MoveToNPC( l_npc[p_npc].x , l_npc[p_npc].y , nFubenMapId , l_npc[p_npc].name , true , false , _callback )
	end
	return move_npc , p_npc
end

function process_npc_t( p_npc )

	--ֻnpcԻ
	local s_npc = p_npc

	if p_npc == "Ǯ" then
		DoMount(true)
		Pet_Relax()		
	end
	
	Player:EnableTeamFollow(false)

	local vSelect = 0

	while true do 

		System:Sleep(500)

		local result , v_npc = UpdateData()
		if result ~= nil then
			--Ҫ
			QuestFrameMissionClose()
			return result , v_npc
		end

		if l_boss[s_npc] == INVALID_ID then
			return nil , nil
		end

		local list_count , npc_id = DataPool:GetNPCEventList_Num()
		if npc_id ~= l_boss[s_npc] then
			return nil , nil 
		elseif s_npc == "" then
			if Player:CompareLocalArea( nFubenMapId , 51, 74) == true then
				return nil , nil 
			end
		elseif p_npc == "Ǯ" then
			if l_state_yzw["Ħ"] ~= BOSS_RECORD_INIT then
				return nil,nil
			end
		end

		if list_count < 1 then
			-- ѡ
		else
			local t_click = ""
			for i=0, list_count - 1 do
				local strType,_,_,_,szText = DataPool:GetNPCEventList_Item(i)
				if strType == "text" then
					if string.find(szText , "κһվͨ" ) then
						QuestFrameMissionClose()
						return SelectFuben2
					elseif string.find(szText , "ǲܻǵԮ") then
						QuestFrameMissionClose()
						return nil,nil
					end						
				elseif strType == "id" then
					if string.find(szText , "սɣ") then
						t_click = "սɣ"
					elseif string.find(szText , "ӵص") then
						t_click = "ӵص"
					end
				end
			end
			
			if t_click == "" then
				--ûкѡ
				QuestFrameMissionClose()
				return nil,nil
			else
				vSelect = vSelect + 1
				QuestFrameOptionClicked( t_click , CMP_PART_MATCH)
			end	
		end
	end

end

function OnFuben1()

	AI:SetAI("ɱ")
	AI:SetParameter("ʼ")
	AI:SetParameter( "ʰȡ" , "ָ" , "ҩ" ) 
	AI:SetParameter("ͼ", nFubenMapId )
	AI:SetParameter("", "", 80, 204 ) 
--[[
	AI:SetParameter("Ŀ", "", "" ) 
	AI:SetParameter("Ŀ", "", "к" ) 
	AI:SetParameter("Ŀ", "", "Ҷ" ) 
	AI:SetParameter("Ŀ", "", "" ) 
]]--	
	AI:StartEx(0)


	AI:SetParameter("в" , "ɱѭ")
	AI:SetParameter("в" , "ֻʰȡBOSS" , 1)

	if nSetNoFight > 0 and DataPool:GetTeamMemCount() > 0 and DataPool:IsTeamLeader() == false then
		AI:SetParameter("в" ,"", 0 )
		AI:SetParameter("в" ,"ɱ", 0 )
		AI:SetParameter("в" ,"Ӹ", 1)
		AI:SetParameter("в" ,"", 0)
		AI:SetParameter("в" ,"ֹͣ", 1)
	end

	AI:StartEx(1)

	while true do 

		System:Sleep(500)

		if AI:IsStop() == true then
			break
		end

		if IsFuben() == false then
			break
		end

		local vResult,vParam = UpdateData()
		if vResult ~= OnFuben1 then
			if IsMsgCold("yzw_pick_xbqf" , 3000) == true then
				break
			end
		else
			UpdateMsg("yzw_pick_xbqf")
		end
	end

	AI:Stop()

	OnSweepBattleField(1)
	
	return nil,nil	
end

function _CallBack_MoveQhy( fX , fY , nScene )

	if IsFuben() == false then
		return false
	end
	local result , param = UpdateData()
	if l_state_yzw["Ħ"] ~= BOSS_RECORD_MET then
		return false
	end
	if l_boss["Ǯ"] == INVALID_ID then
		return false
	end

	return true
end

function _CheckQhyFight()
	
	if l_boss["Ǯ"] == INVALID_ID then
		return INVALID_ID
	end

	local vQhyLockId = GetObjectAttackId( l_boss["Ǯ"] , 5000 )
	local vCheckId , _ , _ , vDie = FindObjectByID( vQhyLockId , "" )
	if vCheckId == INVALID_ID or vDie == 1 then
		return INVALID_ID
	end

	local vMonLockId = GetObjectAttackId( vQhyLockId , 5000 )
	if vMonLockId == INVALID_ID or vMonLockId == Player:GetData("ID") then
		return INVALID_ID
	end

	return vQhyLockId
end

function _CallBack_MoveCheckFight( fX , fY , nScene )

	if IsFuben() == false then
		return false
	end
	local result , param = UpdateData()
	if l_state_yzw["Ħ"] ~= BOSS_RECORD_MET then
		return false
	end

	if result == nil then
		return true
	end

	return false
end

function OnFightQhy( vFirstX , vFristY )

	--ʱעǮ,׼Իλ
	AI:SetAdv( "" )

	AI:SetAdv( "Ȧ" , vFirstX , vFristY , 15 )
	AI:SetAI("߼ս") 

	AI:SetParameter("ͼ" , nFubenMapId ) 
	
	AI:StartEx(0)
	AI:SetParameter("в" , "ɱ" , 0 )
	AI:StartEx(1)

	local vPreLockID = INVALID_ID

	while true do

		local bPickUp = 0
		local bKillMonster = 0
		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 = 1
		local bSpeedUp = 0

		System:Sleep(500)

		if AI:IsStop() == true then
			break
		end

		local vResult,vParam = UpdateData()
		if vResult ~= OnFuben2NoKill then
			break
		end

		if l_state_yzw["Ħ"] ~= BOSS_RECORD_MET then
			break
		end

		if l_boss["Ǯ"] ~= INVALID_ID then
			local vLockID = GetObjectAttackId( l_boss["Ǯ"] , 5000 ) 
			if vLockID ~= vPreLockID then
				vPreLockID = vLockID
				QuestFrameSpeak( l_boss["Ǯ"] , "" , 500 )
			end

			local vKillMon , vName = GetQhyNearMon( vFirstX , vFristY )
			if vKillMon == INVALID_ID then
				AI:SetAdv("ָɱ" , -1 , "" )
			else
				bKillMonster = 1
				AI:SetAdv("ָɱ" , vKillMon , vName )
			end
		else
			AI:SetAdv("ָɱ" , -1 , "" )
			bKillMonster = 1
		end

		if bKillMonster == 0 then
			break
		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("" , bYinshen )
		AI:SetAdv("Ȧ" , bFixPlace )
	end

	AI:Stop()		
end

function OnFuben2KillClear( )

	AI:SetAI("ɱ")
	AI:SetParameter("ʼ")
	AI:SetParameter( "ʰȡ" , 0 ) 
	AI:SetParameter("ͼ", nFubenMapId )
	AI:StartEx(0)

	AI:SetParameter("в" , "ɱѭ")
	AI:SetParameter("в" ,"Ӹ", 0)

	AI:StartEx(1)

	while true do 

		System:Sleep(500)

		if AI:IsStop() == true then
			break
		end

		if IsFuben() == false then
			break
		end

		local vResult,vParam = UpdateData()
		if vResult ~= OnFuben2NoKill then
			break
		end

		if l_state_yzw["Ħ"] ~= BOSS_RECORD_MET then
			break
		end
		
	end

	AI:Stop()		
end

function OnFuben2KillObject( vLockId )

	AI:SetAI("ɱ")
	AI:SetParameter("ʼ")
	AI:SetParameter( "ʰȡ" , 0 ) 
	AI:SetParameter("ͼ", nFubenMapId )
	local vId,vX,vY,_,_,_,_,vName = FindObjectByID( vLockId , "")
	if vId == INVALID_ID then
		return 
	end

	AI:SetParameter("", "", vX, vY , 15 ) 
	AI:SetParameter("Ŀ", "", tostring(vName) , tonumber(vLockId) ) 
	
	AI:StartEx(0)


	AI:SetParameter("в" , "ɱѭ")
	AI:SetParameter("в" ,"Ӹ", 0)

	AI:StartEx(1)

	while true do 

		System:Sleep(500)

		if AI:IsStop() == true then
			break
		end

		if IsFuben() == false then
			break
		end

		local vResult,vParam = UpdateData()
		if vResult ~= OnFuben2NoKill then
			break
		end

		if l_state_yzw["Ħ"] ~= BOSS_RECORD_MET then
			break
		end

		local vId,_,_,vDie = FindObjectByID( vLockId , "")
		if vId ~= vLockId or vDie == 1 then
			break
		end		
	end

	AI:Stop()		
end

function OnFuben2()

	AI:SetAI("ɱ")
	AI:SetParameter("ʼ")
	AI:SetParameter( "ʰȡ" , 1 ) 
	AI:SetParameter("ͼ", nFubenMapId )
	AI:SetParameter("", "", 216 , 149) 
	
	AI:StartEx(0)


	AI:SetParameter("в" , "ɱѭ")
	AI:SetParameter("в" , "ֻʰȡBOSS" , 1)

	if nSetNoFight > 0 and DataPool:GetTeamMemCount() > 0 and DataPool:IsTeamLeader() == false then
		AI:SetParameter("в" ,"", 0 )
		AI:SetParameter("в" ,"ɱ", 0 )
		AI:SetParameter("в" ,"Ӹ", 1)
		AI:SetParameter("в" ,"", 0)
		AI:SetParameter("в" ,"ֹͣ", 1)
	end

	AI:StartEx(1)

	while true do 

		System:Sleep(500)

		if AI:IsStop() == true then
			break
		end

		if IsFuben() == false then
			break
		end

		local vResult,vParam = UpdateData()
		if vResult ~= OnFuben2 then
			if l_state_yzw["Ħ"] ~= BOSS_RECORD_MET then
				break
			end
		end
	end

	AI:Stop()

	OnSweepBattleField(1)
	
	return nil,nil	
end

--192,165
function IsQhyInArea( vQhyX , vQhyY )
	if GetDist( 191 , 161 , vQhyX , vQhyY ) < 15 then
		return true 
	end
	if GetDist( 182 , 172 , vQhyX , vQhyY ) < 12 then
		return true 
	end

--216,149  198,157 191 , 161
--199,150  198,157

	return false
end

function _CallBackTalkQhy( vQhyID , vType , vMapId )
		
	local vObjId , vObjX , vObjY , vDie = FindObjectByID( vQhyID , vType )
	if vObjId ~= INVALID_ID and IsDestination( vObjX , vObjY , vMapId , MAX_TALK_RANGE ) == true then
		ShowMessage("ԻǮ .........")
		QuestFrameSpeak(vObjId, "" , 500)
	end

	return true
end

function GetQhyNearMon( vX , vY )

	local vQhyId = FindObjectByName("Ǯ" , "NPC")
	local vPlayerId = Player:GetData("ID")

	if vQhyId ~= INVALID_ID then
		local vQhyAttackId = GetObjectAttackId( vQhyId , 5000 )
		local vCheckId , _ , _ , vDie ,_ ,_,_,vName = FindObjectByID( vQhyAttackId , "" )
		if vCheckId ~= INVALID_ID or vDie ~= 1 then
			return vCheckId , vName
		end
	end

	local vResultID = INVALID_ID 
	local vResultName = ""
	local vResultDist = 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 Character:GetData(idObj, "DIE") == false then
				--6 Χ, ޹ֹǮ , ޹ֹ , Ǯûй
				local vMonAttackId = GetObjectAttackId( idObj , 5000 )
				if vMonAttackId ~= INVALID_ID then
					if vPlayerId == vMonAttackId or vQhyId == vMonAttackId then
						return idObj , nameObj
					end					
				end

				local vDist = GetDist( vX , vY , xObj , yObj )
				if vDist < 8 then
					--Ĺ
					if vResultID == INVALID_ID or vDist < vResultDist then
						vResultID = idObj 
						vResultName = nameObj
						vResultDist = vDist
					end
				elseif IsQhyInArea( xObj , yObj ) == true then
					if vResultID == INVALID_ID or vDist < vResultDist then
						vResultID = idObj 
						vResultName = nameObj
						vResultDist = vDist
					end			
				end
			end
		end
	end
	
	return vResultID , vResultName 
end

--[[
A-B ֮Ѳ
 A->B ֻϹ,յȴ Ǯ 
 B->A 

ж Ǯ λɹ

1,Ǯ , Χ޹
2,Ǯ , Χй , 6 Χ, ޹ֹǮ , ޹ֹ , Ǯûй
]]--

function OnFuben2NoKill()

	local vPos = { [0]={x=228,y=191} , [1]={x=226,y=170} , [2]={x=218,y=152} , [3]={x=204,y=155} , [4]={x=191,y=161} , [5]={x=183,y=170} }
	local vPosSize = 5
	local vPreLockId = INVALID_ID
	local vRunPos = 0
	local vRunFlag = 0 
	UpdateMsg("YZW_KAWEI")

	while true do

		System:Sleep(500)

		if IsFuben() == false then
			return
		end
		local vResult , vParam = UpdateData()
		if l_state_yzw["Ħ"] ~= BOSS_RECORD_MET then
			break
		elseif SelectFuben2() ~= OnFuben2NoKill then
			break
		elseif vResult == OnFuben2NoKill or vResult == nil then
			local vQhyId , vQhyX , vQhyY = FindObjectByID( l_boss["Ǯ"] , "NPC" )  --182,172

			if vQhyId ~= INVALID_ID and IsQhyInArea(vQhyX , vQhyY) == true then
				if vResult == nil then
					--return
					ShowMessage("ɹ Ǯ λ!")
					QuestFrameMissionClose()
					l_state_yzw["λ"] = 1
				else
					local vSuccess = false
					local vQhyLockId = GetObjectAttackId( l_boss["Ǯ"] , 5000 )
					local vCheckId , _ , _ , vDie = FindObjectByID( vQhyLockId , "" )
					if vCheckId == INVALID_ID or vDie == 1 then
						vQhyLockId = INVALID_ID
					end
					if vPreLockId ~= vQhyLockId or UI:IsWindowShow("Quest") == false then
						--Իnpc
						--MoveToNPC_ById( vQhyX , vQhyY , nFubenMapId , l_boss["Ǯ"] , false , _CallBack_MoveQhy )
						ShowMessage("Ի Ǯ ׼λ")
						local vNear = MoveTo_MovingObj( l_boss["Ǯ"] , "NPC" , 3 , nFubenMapId , _CallBackTalkQhy )
						if vNear == true and UI:IsWindowShow("Quest") == false then
							QuestFrameSpeak( l_boss["Ǯ"] , "" , 500)
						end 
					elseif GetQhyNearMon(vQhyX , vQhyY) ~= INVALID_ID then
						--ִ(עnpc)
						ShowMessage("ʼ峡")
						OnFightQhy( vQhyX , vQhyY )
					else
						--npcλɹ,δ??
						vSuccess = true
						if IsMsgCold("YZW_KAWEI" , 30*1000) then
							ShowMessage("ɹ Ǯ λ!")
							l_state_yzw["λ"] = 1
						end
					end

					vPreLockId = vQhyLockId

					if vSuccess == true then
						UpdateMsg("YZW_KAWEI" , 1)
					else
						UpdateMsg("YZW_KAWEI")
					end
				end
			elseif vRunPos <= vPosSize then
				if vRunFlag == 0 then
					if vRunPos == 0 then
						MoveInMap( vPos[vRunPos].x , vPos[vRunPos].y , nFubenMapId , 2 )
					else
						local vMonId , _ , vMonX , vMonY = LockNearestTarget( "" , CHA_TYPE_NPC|CHA_RELATION_ENEMY|CHA_OCCUPANT_FREE|CHA_OCCUPANT_MYSELF|CHA_STATE_ALIVE )
						local isNearPlayer = false
						local vSelfX , vSelfY = Player:GetPos()
						if vMonId ~= INVALID_ID then
							if GetDist(vSelfX , vSelfY , vMonX , vMonY ) <= 10 then
								isNearPlayer = true
							elseif GetObjectAttackId( vMonId , 2000 ) == Player:GetData("ID") then
								isNearPlayer = true
							end
						end

						if isNearPlayer == true then
							MoveInMap( vPos[vRunPos].x , vPos[vRunPos].y , nFubenMapId , 2 )
						else
							PushDebugMessage("ȴ  Ԥλ..." , 1)
							continue
						end
					end

					vRunPos = vRunPos + 1
					if vRunPos > vPosSize then
						UpdateMsg("YZW_WAIT_QHY")
					end
				else
					-- B->A ּɱ
					MoveInMap( vPos[vRunPos].x , vPos[vRunPos].y , nFubenMapId , 2 , _CallBack_MoveCheckFight )
					if UpdateData() == OnFuben2NoKill then
						OnFuben2KillClear()
						continue
					end
					
					if vRunPos > 0 then
						vRunPos = vRunPos - 1
					else
						vRunPos = 0
						vRunFlag = 0 
					end
				end
			elseif IsMsgCold("YZW_WAIT_QHY" , 10*1000) then
				--ʧ
				ShowMessage("λʧ,峡")
				vRunPos = vPosSize
				vRunFlag = 1
				--ִ
				OnFuben2KillClear()
			else
				PushDebugMessage( "ȴ Ǯ Ԥλ..." , 1 )
			end		
		end
	end
end

function OnFuben2NoKillMember()

	AI:SetAI("ɱ")
	AI:SetParameter("ʼ")
	AI:SetParameter( "ʰȡ" , 1 ) 
	AI:SetParameter("ͼ", nFubenMapId )
	AI:SetParameter("", "", 180 , 89 , 5 ) 
	
	AI:StartEx(0)

	AI:SetParameter("в" , "ɱѭ")
	AI:SetParameter("в" , "ֻʰȡBOSS" , 1)

	if nSetNoFight > 0 and DataPool:GetTeamMemCount() > 0 and DataPool:IsTeamLeader() == false then
		AI:SetParameter("в" ,"", 0 )
		AI:SetParameter("в" ,"ɱ", 0 )
		AI:SetParameter("в" ,"Ӹ", 1)
		AI:SetParameter("в" ,"", 0)
		AI:SetParameter("в" ,"ֹͣ", 1)
	end

	AI:StartEx(1)

	while true do 

		System:Sleep(500)

		if AI:IsStop() == true then
			break
		end

		if IsFuben() == false then
			break
		end

		local vResult,vParam = UpdateData()
		if vResult ~= OnFuben2NoKillMember then
			if l_state_yzw["Ħ"] ~= BOSS_RECORD_MET then
				break
			end
		end
	end

	AI:Stop()

	return nil,nil		
end

function process_boss_kill( npc )

	AI:SetAdv( "" )

	AI:SetAdv( "Ȧ" , 52 ,  72 , 15 )
	AI:SetAI("߼ս") 

	AI:SetParameter("ͼ" , nFubenMapId ) 
	
	AI:StartEx(0)
	AI:SetParameter("в" , "ɱ" , 0 )
	AI:StartEx(1)

	local result = nil
	local r_param = ""
	local nXBQFCount = DataPool:UserBag_CountItemByIDTable( 0x1cb5602 )
	local pre_teamfollow = false
	local bYinBoss = true
	local bWaitMember = true
	local vDZXYCount = 0 
	UpdateMsg("YZW_WAIT_MEMBER")
	DelMsg("YZW_MRF_DZXY")

	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

		local nHPPercent = Player:GetData("HP_PERCENT")
		local nMPPercent = Player:GetData("MP_PERCENT")

		System:Sleep(500)

		if AI:IsStop() == true then
			break
		end

		if IsFuben() == false then
			break
		end

		result , r_param = UpdateData()

		if result ~= process_boss_kill then
			if npc == "Ľݸ" and l_state_yzw["Ľݸ"] == BOSS_RECORD_DEAD then
				break 
			else
					--ߵһ
				if IsDestination( 52 ,  72 , nFubenMapId , 3 ) == false then
					
				end
			end
		end

		if bYinBoss == true and INVALID_ID ~= GetObjectAttackId( l_boss["Ľݸ"] , 15000 ) then
			if IsDestination( 52 ,  72 , nFubenMapId , 3 ) == true then
				bYinBoss = false
				AI:SetAdv("Ȧ뾶" , 15)
			else
				AI:SetAdv("Ȧ뾶" , 3)
				bFixPlace = 1	
			end
		end

		if Player:GetBuffByName( "" , 1 ) ~= INVALID_ID then
			bKillMonster = 0
			bTeamAttack = 0 
			bStopAssist = 0 
			bStopAssistSelf = 0
		end

		local teamfollow = false
		if l_boss["Ľݸ"] ~= INVALID_ID then
			AI:SetAdv("ָɱ" , 0 , "Ľݸ" )
			if CheckBufferByName( l_boss["Ľݸ"] , "ת" )  ~= INVALID_ID then
				DelMsg("YZW_MRF_DZXY")
				bKillMonster = 0
				bCallPet=0
				PushDebugMessage("Ľݸʹ˼ ת,ͣ!" , 1 )
				teamfollow = true
				vDZXYCount = vDZXYCount + 1
			elseif bWaitMember == false then
				UpdateMsg("YZW_MRF_DZXY" , 1)
			end
		else
			AI:SetAdv("ָɱ" , -1 , "" )
			DelMsg("YZW_MRF_DZXY")
		end

		--Ӹ泬3,Զ˳Ӹ
		if teamfollow ~= pre_teamfollow then
			pre_teamfollow = teamfollow
			if teamfollow == true then
				Player:EnableTeamFollow(true)
				UpdateMsg("YZW_TEAMFOLLOW")
			else
				Player:EnableTeamFollow(false)
				DelMsg("YZW_TEAMFOLLOW")
			end
		else
			if IsMsgCold("YZW_TEAMFOLLOW" , 3000) == true then
				Player:EnableTeamFollow(false)
			end
		end

		if nSetSafeKill ~= 0 then
			local vWaitTime = 9000
			if vDZXYCount == 0 then
				vWaitTime = 3000
			end

			if l_boss["Ľݸ"] ~= INVALID_ID then
				if IsMsgCold("YZW_MRF_DZXY", vWaitTime) == false then
					bKillMonster = 1 
				else
					bStopSpecialAttack = 1
					bKillMonster = 0
					bCallPet=0
				end
			end
		end

		--ǰ10,Ա  δ  Ľݸɼ
		if bWaitMember == true then
			if IsMsgCold("YZW_WAIT_MEMBER" , 10*1000) == true then
				ShowMessage("ȴԱʱ,ֱӿ")
				bWaitMember = false
			else
				local nTeamCount = DataPool:GetTeamMemCount()
				if nTeamCount == 0 then
					ShowMessage("û,ֱӿ")
					bWaitMember = false
				else
					local nNearCount = 0
					for i=0,nTeamCount do
						local vName,vId,_,vMapId,vX,vY = DataPool:GetTeamMemInfo(i)
						if vId == Player:GetData("ID") then
							nNearCount = nNearCount + 1
						elseif vId ~= INVALID_ID then
							if nFubenMapId == vMapId and GetDist( 52 ,  72 , vX , vY ) < 10 then
								nNearCount = nNearCount + 1
							end
							if l_boss["Ľݸ"] ~= INVALID_ID and GetObjectAttackId( vId , 15000 ) == l_boss["Ľݸ"] then
								bWaitMember = false
								ShowMessage("Ա %s Ѿ,ֹͣȴ" , tostring(vName))
							end					
						end
					end
					if nNearCount == nTeamCount then
						ShowMessage("Աȫλ %d,ʼս" , nNearCount)
						bWaitMember = false
					elseif GetObjectAttackId( l_boss["Ľݸ"] , 15000 ) ~= INVALID_ID then
						ShowMessage("ĽݸĿ %d,ʼս" , tonumber(GetObjectAttackId( l_boss["Ľݸ"] , 15000 )))
						bWaitMember = false
					end
				end	
			end
		end

		if bWaitMember == true then
			PushDebugMessage("ȴжԱ󿪹..." , 1)
			bKillMonster = 0
		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 bWaitMember == false and nXBQFCount > 0 and IsMsgCold("YZW_USEITEM_XBQF" , 3000) == true and Player:GetBuffByName( "ֿ" , 1 ) == INVALID_ID then
			nXBQFCount = DataPool:UserBag_CountItemByIDTable( 0x1cb5602 )
			UseItemById( 0x1cb5602 )
			UpdateMsg("YZW_USEITEM_XBQF")
		end
	end

	AI:Stop()

	OnSweepBattleField()

	-- nil,""
	return nil , nil
end

function is_process_npc()

	if UI:IsWindowShow("Quest") == false then 
		return false , ""
	end
	
	if l_boss["ӱ"] ~= INVALID_ID then
		return  true , "ӱ"
	elseif l_boss["Ǯ"] ~= INVALID_ID then
		return true , "Ǯ"
	elseif l_boss[""] ~= INVALID_ID then
		return true , ""
	end

	return false , ""
end

function process_exit()
	MoveInMap( 64 , 22 , nFubenMapId , 2 )
	System:Sleep( 500 )
	EnterNewMap()
	System:Sleep(1000)
end

function FindProcess()

	if l_state_yzw["Ľݸ"] == BOSS_RECORD_DEAD then
		return process_exit, ""
	elseif l_state_yzw["Ħ"] == BOSS_RECORD_DEAD then
		if Player:CompareLocalArea( nFubenMapId , 51, 74) == true then
			return move_npc , "Ľݸ"
		else
			return move_npc , ""
		end
	elseif l_state_yzw["Ħ"] == BOSS_RECORD_MET then
		--ѾԮʾ
		return SelectFuben2()
	elseif l_state_yzw[""] == BOSS_RECORD_DEAD then
		if nSetKillJmz == 1 then
			return move_npc , "Ǯ"
		elseif DataPool:IsTeamLeader() == true then
			return move_npc , "Ǯ"
		else
			return move_npc , ""
		end 	
	end

	return move_npc , "ӱ"
end

local function OnCommonMission()

	if Player:GetData( "LEVEL" ) < 60 then
		ShowMessage( "ȼС 60 ,ִű" )
		return
	end

	local check_quest = false
	local task = nil 
	local task_param = nil

	if Player:CompareLocalArea( nFubenMapId , 51, 74) == true then
		l_state_yzw[""] = BOSS_RECORD_DEAD
		l_state_yzw["Ħ"] = BOSS_RECORD_DEAD
	end

	while true do

		System:Sleep(500) 

		if IsFuben() == true then

			check_quest = false
			
			local process , param = UpdateData()

			if process~= nil then
				task , task_param = process(param)
			else
				local is_talk , npc_name = is_process_npc()
				if is_talk == true then
					task , task_param = process_npc_t( npc_name )
				else
					if task ~= nil then
						task , task_param = task( task_param )
					else
						task , task_param = FindProcess()
					end
				end
			end

		else
			--Ұ
			reset_yzw()

			if check_quest == false then
				if GetQuestTimesInfo("ַ") == 0 and IsFuben()== false then
					ShowMessage("Ѿ븱!")
					break 
				end
			end

			check_quest = true 

			MoveToNPC(72 , 120 , 0x04, "" , true , true , _breakmove_infuben )

			if DataPool:IsTeamLeader() == true or DataPool:GetTeamMemCount() == 0 then
				if DataPool:GetTeamMemCount() < 3 and IsFuben()== false then
					ShowMessage("С 3 ,ִ 븱")
					return
				end
			end

			if DataPool:IsTeamLeader() == true then

				QuestFrameOptionClicked( "ַ" , CMP_PART_MATCH)
				System:Sleep(2000)

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

				if strType == "text" then
					if string.find(szText, "") then
						ShowMessage(",ֹű!")
						break	-- ˳ѭű
					end
				end
			end
		end

	end
end

function GetMissionComplete()

	local vIndex, vIdMission, vIdScript = DataPool:GetPlayerMission(script_name)
	local vParam = {}
	if vIndex ~= -1 then
		-- ȡ
		for i=0,7 do
			vParam[i] = DataPool:GetPlayerMission_Variable(vIndex, i)
			DbgPrintf("[%d]%d", i, vParam[i])
		end
	
		return tonumber(vParam[0])
	end

	return INVALID_ID
end

local function OnXuKongMission()

	if Player:GetData( "LEVEL" ) < 60 then
		g_XukongRet = 1
		ShowMessage( "ȼС 60 ,ִű" )
		return
	end

	script_name = "ս"

	local check_quest = false
	local task = nil 
	local task_param = nil

	if Player:CompareLocalArea( nFubenMapId , 51, 74) == true then
		l_state_yzw[""] = BOSS_RECORD_DEAD
		l_state_yzw["Ħ"] = BOSS_RECORD_DEAD
	end

	while true do

		System:Sleep(500) 

		if IsFuben() == true then

			check_quest = false
			
			local process , param = UpdateData()

			if process~= nil then
				task , task_param = process(param)
			else
				local is_talk , npc_name = is_process_npc()
				if is_talk == true then
					task , task_param = process_npc_t( npc_name )
				else
					if task ~= nil then
						task , task_param = task( task_param )
					else
						task , task_param = FindProcess()
					end
				end
			end

		else
			--Ұ
			reset_yzw()

			local nComplete = GetMissionComplete() 
			if nComplete == 0 then
				MoveToNPC(72 , 120 , 0x04, "" , true , true , _breakmove_infuben )

				if DataPool:IsTeamLeader() == true or DataPool:GetTeamMemCount() == 0 then
					if DataPool:GetTeamMemCount() < 3 and IsFuben()== false then
						g_XukongRet = 1
						ShowMessage("С 3 ,ִ 븱")
						return
					end
				end

				if DataPool:IsTeamLeader() == true then
					QuestFrameOptionClicked( "ս" , CMP_PART_MATCH)

					System:Sleep(2000)

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

					if strType == "text" then
						if string.find(szText, "") then
							ShowMessage(",ֹű!")
							break	-- ˳ѭű
						end
					end
				end
			else
				break
			end

			if check_quest == false then
				if GetQuestTimesInfo("ַ") == 0 and IsFuben()== false then
					ShowMessage("Ѿ븱!")
					break 
				end
			end

			check_quest = true 

			MoveToNPC(72 , 120 , 0x04, "" , true , true , _breakmove_infuben )

			if DataPool:IsTeamLeader() == true or DataPool:GetTeamMemCount() == 0 then
				if DataPool:GetTeamMemCount() < 3 and IsFuben()== false then
					ShowMessage("С 3 ,ִ 븱")
					return
				end
			end
		end

	end

end

local function _SMain(...)

	nSetNoFight = GetScriptParam(script_name,"attr", SCRIPT_ATTR_NOFIGHT )
	nSetKillJmz = GetScriptParam(script_name,"chk0" )
	nSetSafeKill = GetScriptParam(script_name,"chk1" )

	LogChatMsg( 0 )
	LogChatMsg( 1 )	

	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

AI:SetMode("ͣͼ" , nFubenMapId)
_SMain()
AI:SetMode("ͣͼ" , -1)
