
--[[

[author]

class=1
attr=1
descript = ˸Ҫ12

]]--

script_name = "ŋָأͨ"



local g_szNameSence = {
	[1] = "" ,
	[2] = "ŋָ"
}

local g_objBoss = {

	[1] = "ˮ" , 
	[2] = "ɽͯ" , 
	[3] = "" , 

	[4] = ""
}



local g_objBossSmall = {

	[1] = "Ӱ"
}



local g_szNpcMission = ""




if G_bRevival == nil then
	G_bRevival = 0
end

if G_nStep == nil then
	G_nStep = 1
end



if G_bFinish == nil then
	G_bFinish = 0
end



if G_bCenter == nil then
	G_bCenter = 0
end

local g_nMaxR1 = 17


local g_nX_O = 140
local g_nY_O = 62
local g_nMemPai = -1

--boss 1
local g_nX_K1 = -1
local g_nY_K1 = -1
local g_nX_K4 = -1
local g_nY_K4 = -1

local g_nX_Keep = -1
local g_nY_Keep = -1

local g_bKillMonster = 0
local g_bYinGuai = 0
local g_bTeamFollow = 0
local g_bTeamAttack = 0
local g_bStopAssistSelf = 0
local g_bStopAssist = 0
local g_bStopSpecialAttack = 0
local g_bCallPet = 1
local g_bYinshen = 0
local g_FollowTarget = -1
local g_mainguard_r = 6
local g_nRFollowTarget = 1
local g_bPickUp = 0

local g_nNoFight = 0
local 	g_RecordTime = { { keys , nTime } }

function updateTime( szNameRecord )
	local nCount = 1
	if table.getn( g_RecordTime ) <= 1 and g_RecordTime[ 1 ].keys == nil then
		g_RecordTime[ 1 ] = { keys = szNameRecord , nTime = System:GetTickCount() }
		return 1
	else
		for i = 1 , table.getn( g_RecordTime ) do
			if g_RecordTime[ i ].keys == szNameRecord and g_RecordTime[ i ].keys ~= nil then
				g_RecordTime[ i ].nTime = System:GetTickCount()
				nResult = i
				break
			end	
				nCount = nCount + 1
		end

		if nCount >= table.getn( g_RecordTime ) then
			g_RecordTime[ nCount ] = { keys = szNameRecord , nTime = System:GetTickCount() }
			nResult = nCount
		end
		return nResult
	end

end

function isRecordTime( szNameRecord , nTimeSpan )

	for i = 1 , table.getn( g_RecordTime ) do
		if g_RecordTime[ i ].keys == szNameRecord then
			if (g_RecordTime[ i ].nTime + nTimeSpan) <= System:GetTickCount() then
				return 1
			else
				return 0
			end
		end
	end
	return -1

end

function table.removeKey( varKey )
	for i , v in pairs( g_RecordTime ) do 
		if v.keys == varKey then
			table.remove( g_RecordTime , i )
		end
	end
end

function delRecordTime( szNameRecord )
	table.removeKey( szNameRecord )
end


function onRide()
	if GetMount() == -1 then
		Player:EnableTeamFollow( false )
	end
	while true do
		if getCurrentBoss() ~= -1 then
			offRide()
			break
		end

		if GetMount() ~= -1 then
			break
		end
		DoMount( true )
		System:Sleep( 1000 )
	end
end

function offRide()
	if GetMount() ~= -1 then
		Player:EnableTeamFollow( false )
		Dismount()
	end
end

function mySleep( nSleep )
	local nCount = 1
	while true do
		if nCount > nSleep then
			break
		end
		nCount = nCount + 1
		System:Sleep( 1000 )
	end
end


function getTargetAngle( szName )

	local n = EnumObject(true)
	if n == 0 then
		return -1 , -1 , -1
	end

	for i = 0 , n - 1 do
		local idObj , classObj , nameObj , xObj , yObj , _, modelObj , nAngle = EnumObject( false, i )
		if tostring( nameObj ) == tostring( szNameObj ) then
			return xObj , yObj , nAngle
		end
	end
	return -1 , -1 , -1
	
end

function getTargetID( szNameTarget )
	local id , _ , _ = getTargetPos( szNameTarget )
	if id ~= -1 then
		return id
	else
		return -1
	end

end

function getTargetPos( szNameObj )

	local n = EnumObject(true)
	if n == 0 then
		return -1 , -1 , -1
	end

	for i = 0 , n - 1 do
		local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
		if tostring( nameObj ) == tostring( szNameObj ) then
			return idObj , xObj , yObj
		end
	end
	return -1 , -1 , -1
	
end

function isMonsterLive( szNameMonster )

	local n = EnumObject(true)

	if n == 0 then
		return false
	end

	for i = 0 , n - 1 do
		local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )

		if tostring( nameObj ) == tostring( szNameMonster ) then
			return true
		end
	end
	return false
end


function isMonster( szNameObj )

	local n = EnumObject(true)

	if n == 0 then
		return false
	end

	for i = 0 , n - 1 do
		local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
		if tostring( szNameObj ) == tostring( nameObj ) then
			if classObj == "MON" then
				return true
			else
				return false
			end
		end
	end
	return false
	
end

function isLiveID( idTarget )

	local n = EnumObject(true)
	if n == 0 then
		return false
	end
	for i = 0 , n - 1 do
		local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
		if idTarget == idObj then
			return true
		end
	end
	return false
end


function isLiveName( szName )

	local n = EnumObject(true)
	if n == 0 then
		return false
	end
	for i = 0 , n - 1 do
		local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
		if tostring( szName ) == nameObj then
			if Character:GetData( idObj , "DIE" ) == false then
				return true
			end
		end
	end
	return false
end

function getNumMemberDie()
	local nCount = DataPool:GetTeamMemCount()
	if nCount <= 0 then
		return 0
	end
	local nCountDie = 0
	for i = 0 , DataPool:GetTeamMemCount() - 1  do
		local szName , idPlayer , playerMempai , idMap , xCurPos , yCurPos , nHp , nMaxHp = DataPool:GetTeamMemInfo( i )
		if szName ~= Player:GetName() and getTargetID( szName ) ~= -1 and nHp <= 0 then
			nCountDie = nCountDie + 1
		end
	end
	return nCountDie
end


function isStopBuff( nMax )
	local nCount = DataPool:GetTeamMemCount()

	if nCount <= 0 then
		return 1
	end

	for i = 0 , nCount - 1 do

		local szName , idPlayer , playerMempai , idMap , xCurPos , yCurPos , nHp , nMaxHp = DataPool:GetTeamMemInfo( i )
		if szName ~= Player:GetName() and getTargetID( szName ) ~= -1 then
			if nHp < nMaxHp * ( nMax / 100 ) then
				return 0
			end

		end
	end

	return 1
end


function getMemberDieID( nMemPos )
	local nCount = DataPool:GetTeamMemCount()

	if nCount <= 0 then
		return -1 , -1 , -1
	end
	if nMemPos > nCount then
		return -1 , -1 , -1
	end

	for i = nMemPos , DataPool:GetTeamMemCount() - 1  do
		local szName , idPlayer , playerMempai , idMap , xCurPos , yCurPos , nHp , nMaxHp = DataPool:GetTeamMemInfo( i )
		if szName ~= Player:GetName() and getTargetID( szName ) ~= -1 and nHp <= 0 then
			return idPlayer , xCurPos , yCurPos , tostring( szName )
		end
	end
	
	return -1 , -1 , -1
end

function revivalFriend()
	AI:SetParameter( "" )
	AI:SetAI("ɱ") 
	AI:SetParameter( "ʼ")
	AI:SetParameter( "ʰȡ" , 0 ) 
	AI:SetParameter( "ͼ" , -1) 
	local xNow, yNow = Player:GetPos()
	AI:SetParameter("" , "" , xNow , yNow , 40 )
	AI:Start(true)
		
	AI:SetParameter("в" , "ɱ" , 0 ) 
	AI:SetParameter("в" , "ֹͣ" , 1 ) 
	AI:SetParameter("в" , "ٻ" , 0 ) 
	local nNumDie = getNumMemberDie()
	if nNumDie <= 0 then
		return 0
	else
		UpdateMsg( "revivalAll" )
	end
	while true do 
		
		if getNumMemberDie() <= 0 then
			break
		end
		if IsMsgCold( "revivalAll" , nNumDie*13000 ) == true then
			DelMsg( "revivalAll" )
			break
		end
		local idHpSlow , x , y , szName = getMemberDieID( 0 )
		if idHpSlow ~= -1 then
			MoveTo( x , y , -1 , "" , 0 , true , _callbackInSence )
			LockTarget( idHpSlow )
			System:Sleep( 500 )
			UseSkill_Target( "" , szName )
		end
		System:Sleep( 1000 )
	end
	
	AI:Stop()
	AI:SetParameter("ָ")
end



function revivalFight()

	g_bPickUp = 0
	g_bKillMonster = 0

	g_bStopAssistSelf = 1
	g_bStopAssist = 1
	g_bStopSpecialAttack = 1

	g_bCallPet = 0

	local idMemberDie , x , y , szName = getMemberDieID( 0 )
	if idMemberDie ~= -1 then
		MoveTo( x , y , -1 , "" , 0 , true , _callbackInSence )
		LockTarget( idMemberDie )
		System:Sleep( 500 )
		UseSkill_Target( "" , szName )
	end

end


function setRevival( nHpTeam )

	local nNumDie = getNumMemberDie()
	if nNumDie <= 0 then
		if G_bRevival == 1 then
			AI:Start()
			G_bRevival = 0
		end
		return 0
	else
		if Player:GetData( "HP" ) > Player:GetData( "MAXHP" )*0.5 then
			G_bRevival = 1
			if isStopBuff( nHpTeam ) == 1 then
				revivalFight()
			end
		end
	end
end

function isConditionTeam()
	local nCount = DataPool:GetTeamMemCount()
	if nCount < 3 then
		return true
	end
	for i = 0 , DataPool:GetTeamMemCount() - 1  do
		local szName , idPlayer ,  _, idMap , xCurPos , yCurPos = DataPool:GetTeamMemInfo( i )
		if szName ~= Player:GetName() and getTargetID( szName ) == -1 and GetActiveSceneName() ==  tostring( g_szNameSence[ 1 ] ) and getTargetID( tostring( g_szNpcMission ) ) ~= -1 then
			if DataPool:IsTeamLeader() then
				local szWait = string.format( " %s ϣ ׼븱 " , szName )
				Shout( "team" , szWait )
				mySleep( 5 )
			else
				if IsMsgCold( "WaitTeam" , 5000 ) == true then
					ShowMessage( " %s ûڸ " , szName )
					UpdateMsg( "WaitTeam" )
				end
			end	
			nCount = nCount - 1
		end
	end

	if nCount >= DataPool:GetTeamMemCount() then
		return true
	end
	return false
end

function isTeamHpLow( nPer )
	
	for i = 0 , DataPool:GetTeamMemCount() - 1  do
		local szName , idPlayer , nMemPai , idMap , xCurPos , yCurPos , nHp , nMaxHp = DataPool:GetTeamMemInfo( i )
		if nHp <= nMaxHp * ( nPer / 100 ) and nHp ~= 0 then
			return true
		end
	end
	return false
end

function buffHp()
	if isTeamHpLow( 80 ) or ( Player:GetData( "HP" ) < Player:GetData( "MAXHP" )*0.8 )  then
		buffMySeltAndFriend( 0 )				
	end			
end

function buffMySeltAndFriend( nFlag )

	AI:SetAI("߼ս") 
	AI:Start()
	local xPos , yPos = Player:GetPos()
	g_mainguard_r = 15
	AI:SetAdv( "Ȧ" , xPos , yPos , g_mainguard_r )
		
	g_bKillMonster = 0

	g_bStopAssistSelf = 0
	g_bStopAssist = 0
	g_bStopSpecialAttack = 0

	AI:SetParameter( "в" , "ٻ" , 1 ) 

	while true do
		
		System:Sleep( 100 )
			Player:EnableTeamFollow( false )
			offRide()

		if Player:GetData( "MP" ) <= 0 then
			ShowMessage( "" )
			break
		end

		if isStopBuff( 80 ) == 1 and ( Player:GetData( "HP" ) >= Player:GetData( "MAXHP" )*0.8 ) then
			ShowMessage( "Ѫ" )
			break
		end
		if nFlag == 1 then
			if  GetActiveSceneName() == tostring( g_szNameSence[ 2 ] ) then
				break
			end
		else
			if GetActiveSceneName() ~= tostring( g_szNameSence[ 2 ] ) then
				break
			end

			if isClassTarget( "MON" ) == true then
				break
			end
		end

		AI:SetParameter("в" , "" , 1 )
		AI:SetParameter("ʰȡ" , 0 )
		AI:SetParameter("в" , "ɱ" , g_bKillMonster )
		AI:SetParameter("в" , "" , 0 )
		AI:SetParameter("в" , "Ӹ" , 0 )
		AI:SetParameter("в" , "" , g_bTeamAttack )
		AI:SetParameter("в" , "ֹܸͣ" , g_bStopAssistSelf )
		AI:SetParameter("в" , "ֹͣ" , g_bStopAssist )
		AI:SetParameter("в" , "ֹͣ⹥" , g_bStopSpecialAttack )
		AI:SetParameter("в" , "ٻ" , 1 ) 
		AI:SetAdv("Ȧ뾶" , g_mainguard_r ) 
		AI:SetAdv("" , 0 )
	end

	AI:Stop()
end




function _callbackFollowMeIn( fX , fY , nScene )

	if GetActiveSceneName() ~= tostring( g_szNameSence[ 2 ] ) then
		return false
	end
	if isMemTeamNearXY( fX , fY , 3 ) == -1 then
		if g_nIndexMember ~= -1 then
			table.remove( g_objFriendFollow , g_nIndexMember )
		end
		return false
	end

	if isNearXY( fX , fY , 3 ) == 1 then

		Player:EnableTeamFollow( true )

		if g_nIndexMember ~= -1 then
			table.remove( g_objFriendFollow , g_nIndexMember )
			return false
		end
	end

	return true
end

function isMemTeamNearXY( nX , xY , nR )
	for i = 0 , DataPool:GetTeamMemCount() -1 do
		local playerName , playerID , _ , playerMap , playerX , playerY = DataPool:GetTeamMemInfo( i )			
		if Player:GetName() ~= playerName then
			if GetDist( nX , xY , playerX , playerY ) < nR then
				return playerID
			end
		end
	end	
	return -1
end


local g_objFriendFollow = { nX , nY }
local g_nIndexMember = -1

function setObjFriendFollow()
	local nNum = DataPool:GetTeamMemCount()
	if nNum <= 0 then
		return false
	end

	local xPos, yPos = Player:GetPos()
	if xPos ~= -1 then
		g_objFriendFollow = { nX , nY }
		local nIndex = 1
		for i = 0 , nNum do
			local szName , idPlayer ,  _, idMap , xCurPos , yCurPos = DataPool:GetTeamMemInfo( i )
			if szName ~= Player:GetName() and  GetDist( xPos , yPos , xCurPos , yCurPos ) > 10 then

				if xCurPos ~= 0 and xCurPos ~= -1 then

					g_objFriendFollow[ nIndex ] = { nX = xCurPos , nY = yCurPos }
					nIndex = nIndex + 1
				end
			end
		end	
	end
	return true
end


function getPosMemberFollow()
	if DataPool:GetTeamMemCount() <= 0 then
		return -1 , -1
	end
	local xPos, yPos = Player:GetPos()
	for i = 0 , DataPool:GetTeamMemCount() do
		local szName , idPlayer ,  _, idMap , xCurPos , yCurPos = DataPool:GetTeamMemInfo( i )
		if szName ~= Player:GetName() and GetDist( xPos , yPos , xCurPos , yCurPos ) > 9 then
			if xCurPos ~= 0 and xCurPos ~= -1 then
				return xCurPos , yCurPos
			end
		end
	end
	return -1 , -1
end


function getPosFarFriend( nX , nY )
	local nNum = table.getn( g_objFriendFollow )

	if nNum <= 0 then
		return -1 , -1
	end



	local nIndex = 1
	local nMinDist = GetDist( g_objFriendFollow[ 1 ].nX , g_objFriendFollow[ 1 ].nY , nX , nY )


	for i = 2 , nNum do
		local nDist = GetDist( g_objFriendFollow[ i ].nX , g_objFriendFollow[ i ].nY , nX , nY )

		if nDist > nMinDist then			
			nMinDist = nDist
			nIndex = i
		end
	end
	g_nIndexMember = nIndex
	return tonumber( g_objFriendFollow[ nIndex ].nX ) , tonumber( g_objFriendFollow[ nIndex ].nY )

end

local g_nFollow = -1

function goFollowMe()

	if DataPool:GetTeamMemCount() <= 0 then
		return 2
	end

	local xPos , yPos = Player:GetPos()
	if xPos == -1 then
		return 2
	end

	Player:EnableTeamFollow( false )
	onRide()

	g_nIndexMember = -1
	setObjFriendFollow()

	while true do
		System:Sleep( 100 )

		if GetActiveSceneName() ~= tostring( g_szNameSence[ 2 ] ) then
			break
		end

		if isClassTarget( "MON" ) == true then
			break
		end
		if table.getn( g_objFriendFollow ) <= 0 then
			Player:EnableTeamFollow( true )
			return false
		end
		
		local x , y = getPosFarFriend( xPos , yPos )
		if x ~= -1 and y ~= -1 and x ~= 0 then

			local xCur = -1
			local yCur = -1
			
			if g_nFollow == 1 then
				xCur = 140
				yCur = 62

			elseif g_nFollow == 2 then

				xCur = 212
				yCur = 119

			elseif g_nFollow == 3 then

				xCur = 182
				yCur = 207

			elseif g_nFollow == 4 then

				xCur = 82
				yCur = 181

			end

--[[
			if isNearXY( xCur , yCur , 9 ) == 0 then
				Player:EnableTeamFollow( true )
			else
				Player:EnableTeamFollow( false )
			end
]]--

				if isNearXY( x , y , 3 ) == 0 then
					MoveTo( x , y , -1 , "" , 0 , true , _callbackFollowMeIn )
					System:Sleep( 1000 )
					if g_nIndexMember ~= -1 then
						table.remove( g_objFriendFollow , g_nIndexMember )
					end
				elseif isNearXY( x , y , 3 ) == 1 then

					if g_nIndexMember ~= -1 then
						table.remove( g_objFriendFollow , g_nIndexMember )
					end
				end

				Player:EnableTeamFollow( true )
		else
			break
		end	
	end
end
 
function _callbackInSence( fX , fY , nScene )

	if GetActiveSceneName() ~= tostring( g_szNameSence[ 2 ] ) then
		return false
	end
	return true
end

function _callbackInSenceFollow( fX , fY , nScene )

	if GetActiveSceneName() ~= tostring( g_szNameSence[ 2 ] ) then
		return false
	end

	if DataPool:IsTeamLeader() then
		if IsMsgCold( "CallFollowMe" , 1000 ) == true then
			Player:EnableTeamFollow( true )
			UpdateMsg( "CallFollowMe" )
		end
	end


	return true
end



function _callbackOutSence( fX , fY , nScene )

	if GetActiveSceneName() == tostring( g_szNameSence[ 2 ] ) then
		return false
	end
	return true
end



function setFixPlace()
	AI:SetAdv( "" )
	AI:SetAdv( "ս" ,  6 , 5 , 89 , 6 )
	AI:SetAdv( "ս" ,  89 , 6 , 88 , 88 )
	AI:SetAdv( "ս" ,  88 , 88 , 8 , 88 )
	AI:SetAdv( "ս" ,  8 , 88 , 6 , 5 )
end


function isMsgItem( szSub )
	local strType,_,_,_,szText = DataPool:GetNPCEventList_Item( 0 )	
	if strType == "text" then				
		if string.find(szText, szSub ) then
			return true
		end	
	end
	return false
end

function isTimeExeMission( subNameMember )
	for i = 1 , DataPool:GetTeamMemCount() - 1 do
		local strType,_,_,_,szText = DataPool:GetNPCEventList_Item( i )
		local szMsg = string.sub( szText , 10 , string.len( szText ) )



		if string.find( szText , subNameMember ) and string.find( szMsg , "" ) then
			return false
		end
	end
	return true
end

function speakNpcMission()

	local NpcID , x , y = getTargetPos( tostring( g_szNpcMission ) )

	if NpcID == -1 then
		return 1
	end

	if GetActiveSceneName() == tostring( g_szNameSence[ 2 ] ) then
		return 1
	end

	Player:EnableTeamFollow( true )
	MoveToNPC_ById( x , y , -1 , NpcID , 0 , _callbackOutSence )

	System:Sleep( 1000 )

	if GetActiveSceneName() == tostring( g_szNameSence[ 2 ] ) then
		return 1
	end

	QuestFrameOptionClicked( "ŋָأͨ" , CMP_PART_MATCH )

	if isMsgItem( "ַŋָֽ" ) then


		System:Sleep( 1000 )

		local bStopMission = false
		local nCounMember = DataPool:GetTeamMemCount() - 1
		for i = 0 , nCounMember do
			if GetActiveSceneName() == tostring( g_szNameSence[ 2 ] ) then
				return 1
			end
			local szName , idPlayer ,  _, idMap , xCurPos , yCurPos = DataPool:GetTeamMemInfo( i )
			if isTimeExeMission( szName ) == false then
				if szName == Player:GetName() then
					bStopMission = true
				end
				local szMsg = string.format( "%s " , szName )
				Shout( "team" , szMsg )
				mySleep( 5 )
			end
		end

		if bStopMission then
			
			ShowMessage( "űִ!" )
			Player:EnableTeamFollow( false )
			System:Sleep( 1000 )
			return 0
		end
	end
	QuestFrameMissionClose()
	return 1
	
end


function isClassTarget( szClassObj )

	local n = EnumObject(true)

	if n == 0 then
		return false
	end

	for i = 0 , n - 1 do
		local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
		if tostring( szClassObj ) == tostring( classObj ) then
			return true
		end
	end
	return false
	
end

function isTargetSpecial( szNameObj )

	local n = EnumObject(true)

	if n == 0 then
		return false
	end

	for i = 0 , n - 1 do
		local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
		if tostring( classObj ) == "SPECIAL" and tostring( szNameObj ) == nameObj then

			return true
		end
	end
	return false
	
end

function setListFightName( szNameFight )
	local n = EnumObject(true)
	if n == 0 then
		return 0
	end

	for i = 0 , n - 1 do
		local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
		if tostring( szNameFight ) == tostring( nameObj ) and Character:GetData( idObj , "DIE" ) == false then
			
			AI:SetAdv( "ָɱ" , idObj , tostring( szNameFight ) )
		end
	end
	return 1
end





function teamleaderSpeakBoss1()
	local NpcID , x , y = getTargetPos( tostring( g_objBoss[ 1 ] ) )
	if NpcID ~= -1 and x ~= -1 and y ~= -1 then

		Player:EnableTeamFollow( true )
		MoveTo( 144 , 62 , -1 , "" , 0 , true , _callbackInSence )
		System:Sleep( 2000 )
		Player:EnableTeamFollow( false )

		if g_nMemPai == 4 then
			buffHp()
		end
		MoveToNPC_ById( 138 , 62 , -1 , NpcID , 0 , _callbackInSence )

		System:Sleep( 200 )

		QuestFrameOptionClicked( "̾ѧ" , CMP_PART_MATCH )
		System:Sleep( 200 )
		if isMsgItem( "ǰǶ˶" ) == true then
			goFollowMe()
		end
	end
	QuestFrameMissionClose()
end

function teamleaderSpeakBoss2()
	local NpcID , x , y = getTargetPos( tostring( g_objBoss[ 2 ] ) )
	if NpcID ~= -1 then
		Player:EnableTeamFollow( true )
		MoveTo( 215 , 121 , -1 , "" , 0 , true , _callbackInSence )
		System:Sleep( 2000 )
		Player:EnableTeamFollow( false )

		if g_nMemPai == 4 then
			buffHp()
		end

		MoveToNPC_ById( x , y , -1 , NpcID , 0 , _callbackInSence )		
		System:Sleep( 200 )
		QuestFrameOptionClicked( "̾ѧ" , CMP_PART_MATCH )
		System:Sleep( 200 )
		if isMsgItem( "ǰǶ˶" ) == true then

			goFollowMe()
		end
	end
	QuestFrameMissionClose()

end

function teamleaderSpeakBoss3()
	local NpcID , x , y = getTargetPos( tostring( g_objBoss[ 3 ] ) )
	if NpcID ~= -1 then
		Player:EnableTeamFollow( true )
		MoveTo( 184 , 207 , -1 , "" , 0 , true , _callbackInSence )
		System:Sleep( 2000 )
		Player:EnableTeamFollow( false )

		if g_nMemPai == 4 then
			buffHp()
		end

		
		MoveToNPC_ById( x , y , -1 , NpcID , 0 , _callbackInSence )
		System:Sleep( 200 )
		QuestFrameOptionClicked( "̾ѧ" , CMP_PART_MATCH )
		Player:EnableTeamFollow( false )
 		System:Sleep( 200 )
		if isMsgItem( "ǰǶ˶" ) == true then

			goFollowMe()
		end
	end
	QuestFrameMissionClose()
end

function teamleaderSpeakBoss4()
	local NpcID , x , y = getTargetPos( tostring( g_objBoss[ 4 ] ) )
	if NpcID ~= -1 then
		Player:EnableTeamFollow( true )


		MoveTo( g_nX_O , g_nY_O , -1 , "" , 0 , true , _callbackInSenceFollow )
		System:Sleep( 2000 )
		Player:EnableTeamFollow( false )

		if g_nMemPai == 4 then
			buffHp()
		end

		MoveToNPC_ById( x , y , -1 , NpcID , 0 , _callbackInSence )
		System:Sleep( 200 )
		QuestFrameOptionClicked( "̾ѧ" , CMP_PART_MATCH )
		System:Sleep( 200 )
		Player:EnableTeamFollow( false )
		if isMsgItem( "ǰǶ˶" ) == true then

			goFollowMe()
		end
	end
	QuestFrameMissionClose()
end


function inTenObj( szClassObj )
	local n = EnumObject(true)
	if n == 0 then
		
		return 0
	end
	
	if szClassObj == nil then
		for i = 0 , n - 1 do
			local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
				if classObj ~= "PET" then
				end
		end

	else
		
		for i = 0 , n - 1 do
			local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
			if tostring( classObj ) == tostring( szClassObj ) then
				
			end
		end
	end

	return 1
end


function getTargetPosID( idTarget )

	local n = EnumObject(true)
	if n == 0 then		
		return -1 , -1 , nil
	end
	for i = 0 , n - 1 do
		local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
		if idObj == idTarget then
			return xObj , yObj , tostring( nameObj )
		end
	end
	return -1 , -1 , nil
	
end




local g_objCoordinate = { nX , nY , nAngle }

function getRadian( nAngle )
	local nResult = ( nAngle * math.pi ) / 180
	return nResult
end

function setPointCircle( x , y , nAngle , nR )
	local nCount = 360 / nAngle
	for i = 1 , nCount do
		local xResult = x + nR * math.cos( getRadian( nAngle*i ) )
		local yResult = y + nR * math.sin( getRadian( nAngle*i ) )

		

		g_objCoordinate[ i ] = { nX = xResult , nY = yResult , nAngle = nAngle*i }
	end
end

function getMinAngle( nX , nY )

	local nNumMember = table.getn( g_objCoordinate )

	if  nNumMember <= 0 then
		
		return -1
	end

	local nIndex = 1

	local nMinDist = GetDist( g_objCoordinate[ 1 ].nX , g_objCoordinate[ 1 ].nY , nX , nY )

		
	for i = 2 , nNumMember do

		local nDist = GetDist( g_objCoordinate[ i ].nX , g_objCoordinate[ i ].nY , nX , nY )
			
		if nDist < nMinDist then			
			nMinDist = nDist
			nIndex = i

		end
	end

	return nIndex
end

local g_objMaxPoit = { nX , nY }

function getMaxPoint( nX , nY )


	local nNumMember = table.getn( g_objCoordinate )

	if  nNumMember <= 0 then
		return -1 , -1
	end
	g_objMaxPoit = { nX , nY }

	for i = 1 , nNumMember do
		if GetDist( g_objCoordinate[ i ].nX , g_objCoordinate[ i ].nY , g_nX_O , g_nY_O ) <= tonumber( g_nMaxR1 ) then
			g_objMaxPoit[ i ] = { nX = g_objCoordinate[ i ].nX , nY = g_objCoordinate[ i ].nY }
		end
	end
	nNumMember = table.getn( g_objMaxPoit )
	if nNumMember <= 0 then
		return -1 , -1
	end
	local nIndex = -1
	local nMaxDist = GetDist( g_objMaxPoit[ 1 ].nX , g_objMaxPoit[ 1 ].nY , nX , nY )
	nIndex = 1
	for i = 2 , nNumMember do

		local nDist = GetDist( g_objMaxPoit[ i ].nX , g_objMaxPoit[ i ].nY , nX , nY )
		if nDist > nMaxDist then			
			nMaxDist = nDist
			nIndex = i			
		end
	end

	return g_objMaxPoit[ nIndex ].nX , g_objMaxPoit[ nIndex ].nY
end


function getMinPoint( nX , nY )

	local nNumMember = table.getn( g_objCoordinate )

	if  nNumMember <= 0 then
		return -1 , -1
	end

	local nIndex = -1
	local nMinDist = GetDist( g_objCoordinate[ 1 ].nX , g_objCoordinate[ 1 ].nY , nX , nY )
	nIndex = 1
		
	for i = 2 , nNumMember do

		local nDist = GetDist( g_objCoordinate[ i ].nX , g_objCoordinate[ i ].nY , nX , nY )
			
		if nDist < nMinDist then			
			nMinDist = nDist
			nIndex = i

		end
	end

	return g_objCoordinate[ nIndex ].nX , g_objCoordinate[ nIndex ].nY
end


function getGoodPoint( xO , yO , x , y , nR )

	local xPos , yPos = Player:GetPos()
	local nMyR = GetDist( xPos , yPos , xO , yO )

	local xResult = -1
	local yResult = -1

	if nR < 2 then
		if nMyR > 2 then

			nR = nMyR
		else
			nR = 4
		end
		g_objCoordinate = { nX , nY , nAngle }
		setPointCircle( xO , yO , 90 , nR )
	else
		g_objCoordinate = { nX , nY , nAngle }
		setPointCircle( xO , yO , 45 , nR )
	end


	local nMinAngle = getMinAngle( x , y )



	local nMyMinAngle = getMinAngle( xPos , yPos )

	

	

	if nMyMinAngle >= nMinAngle - 1 and nMyMinAngle <= nMinAngle + 1 then

		if nMyR < 2 then			
			xResult , yResult = getMaxPoint( x , y )
			return xResult , yResult
		end

		if nMinAngle >= 8 then
			nMinAngle = 2
			xResult = g_objCoordinate[ nMinAngle ].nX
			yResult = g_objCoordinate[ nMinAngle ].nY
			nMinAngle = 6
			local xTemp = g_objCoordinate[ nMinAngle ].nX
			local yTemp = g_objCoordinate[ nMinAngle ].nY

			if GetDist( xPos , yPos , xResult , yResult ) < GetDist( xPos , yPos , xTemp , yTemp ) then
				nMinAngle = 2
			end

			if GetDist( g_nX_O , g_nY_O , xResult , yResult ) > tonumber( g_nMaxR1 ) then
				nMinAngle = 6
				xResult = g_objCoordinate[ nMinAngle ].nX
				yResult = g_objCoordinate[ nMinAngle ].nY
			end
		elseif nMinAngle <= 1 then

			nMinAngle = 3
			xResult = g_objCoordinate[ nMinAngle ].nX
			yResult = g_objCoordinate[ nMinAngle ].nY
			nMinAngle = 7
			local xTemp = g_objCoordinate[ nMinAngle ].nX
			local yTemp = g_objCoordinate[ nMinAngle ].nY

			if GetDist( xPos , yPos , xResult , yResult ) < GetDist( xPos , yPos , xTemp , yTemp ) then
				nMinAngle = 3
			end

			if GetDist( g_nX_O , g_nY_O , xResult , yResult ) > tonumber( g_nMaxR1 ) then
				nMinAngle = 7
				xResult = g_objCoordinate[ nMinAngle ].nX
				yResult = g_objCoordinate[ nMinAngle ].nY
			end
		else

			local xTemp1 = g_objCoordinate[ nMinAngle + 2 ].nX
			local yTemp1 = g_objCoordinate[ nMinAngle + 2 ].nY

			local xTemp2 = g_objCoordinate[ nMinAngle - 2 ].nX
			local yTemp2 = g_objCoordinate[ nMinAngle - 2 ].nY

			if GetDist( xPos , yPos , xTemp1 , yTemp1 ) < GetDist( xPos , yPos , xTemp2 , yTemp2 ) then
				xResult = xTemp1
				yResult = yTemp1
				if GetDist( g_nX_O , g_nY_O , xTemp1 , yTemp1 ) > tonumber( g_nMaxR1 ) then
					xResult = xTemp2
					yResult = yTemp2
				end
			else
				xResult = xTemp2
				yResult = yTemp2
				if GetDist( g_nX_O , g_nY_O , xTemp2 , yTemp2 ) > tonumber( g_nMaxR1 ) then
					xResult = xTemp1
					yResult = yTemp1
				end


			end

		end


	else

		g_bKillMonster = 1

		g_bStopAssistSelf = 1
		g_bStopAssist = 1
		g_bStopSpecialAttack = 1

		xResult = xPos
		yResult = yPos

	end

	return xResult , yResult

end

function getGoodPointMySelt( xO , yO )

	local xPos , yPos = Player:GetPos()

	local nR = GetDist( xO , yO , xPos , yPos )
	if nR < 4 then

		nR = 4

		g_objCoordinate = { nX , nY , nAngle }	
		setPointCircle( xO , yO , 45 , nR )

		xResult , yResult = getMaxPoint( x , y )
		return xResult , yResult

	end

	g_objCoordinate = { nX , nY , nAngle }
	setPointCircle( xO , yO , 45 , nR )

	local nMinAngle = getMinAngle( xPos , yPos )

	
	local xResult = -1
	local yResult = -1

	if nMinAngle >= 8 then
		nMinAngle = 2
		xResult = g_objCoordinate[ nMinAngle ].nX
		yResult = g_objCoordinate[ nMinAngle ].nY
		nMinAngle = 6
		local xTemp = g_objCoordinate[ nMinAngle ].nX
		local yTemp = g_objCoordinate[ nMinAngle ].nY

		if GetDist( xPos , yPos , xResult , yResult ) < GetDist( xPos , yPos , xTemp , yTemp ) then
			nMinAngle = 2
		end
		if GetDist( g_nX_O , g_nY_O , xResult , yResult ) > tonumber( g_nMaxR1 ) then
			nMinAngle = 6
			xResult = g_objCoordinate[ nMinAngle ].nX
			yResult = g_objCoordinate[ nMinAngle ].nY
		end
	elseif nMinAngle <= 1 then
		nMinAngle = 3
		xResult = g_objCoordinate[ nMinAngle ].nX
		yResult = g_objCoordinate[ nMinAngle ].nY
		nMinAngle = 7
		local xTemp = g_objCoordinate[ nMinAngle ].nX
		local yTemp = g_objCoordinate[ nMinAngle ].nY

		if GetDist( xPos , yPos , xResult , yResult ) < GetDist( xPos , yPos , xTemp , yTemp ) then
			nMinAngle = 3
		end
		if GetDist( g_nX_O , g_nY_O , xResult , yResult ) > tonumber( g_nMaxR1 ) then
			nMinAngle = 7
			xResult = g_objCoordinate[ nMinAngle ].nX
			yResult = g_objCoordinate[ nMinAngle ].nY
		end
	else

		local xTemp1 = g_objCoordinate[ nMinAngle + 2 ].nX
		local yTemp1 = g_objCoordinate[ nMinAngle + 2 ].nY

		local xTemp2 = g_objCoordinate[ nMinAngle - 2 ].nX
		local yTemp2 = g_objCoordinate[ nMinAngle - 2 ].nY

		if GetDist( xPos , yPos , xTemp1 , yTemp1 ) < GetDist( xPos , yPos , xTemp2 , yTemp2 ) then
			xResult = xTemp1
			yResult = yTemp1
			if GetDist( g_nX_O , g_nY_O , xTemp1 , yTemp1 ) > tonumber( g_nMaxR1 ) then
				xResult = xTemp2
				yResult = yTemp2
			end
		else
			xResult = xTemp2
			yResult = yTemp2
			if GetDist( g_nX_O , g_nY_O , xTemp2 , yTemp2 ) > tonumber( g_nMaxR1 ) then
				xResult = xTemp1
				yResult = yTemp1
			end
		end
	end

	return xResult , yResult
end

function keepOffBoss1( idObj , idObjCenter )


	g_bKillMonster = 0

	g_bStopAssistSelf = 1
	g_bStopAssist = 1
	g_bStopSpecialAttack = 1

	local x = -1
	local y = -1
	local xGood , yGood
	local xO , yO , _  = getTargetPosID( idObjCenter )

	if idObj == -1 then

		xGood , yGood = getGoodPointMySelt( xO , yO )
	else
		x , y , _  = getTargetPosID( idObj )
		if x ~= -1 then
			xGood , yGood = getGoodPoint( xO , yO , x , y , GetDist( xO , yO , x , y ) )
		end
	end

	if xGood ~= -1 then

		g_mainguard_r = 1
		g_nX_K1 = xGood
		g_nY_K1 = yGood

		AI:SetAdv( "Ȧ" , xGood , yGood , g_mainguard_r )
		return true

	end

end





function getMsgCheck()
	local channel, data = GetDebugMessage()
	while channel == 1 do
		if string.find( data , "λѾնɱƳӡĺ" ) then
			G_bMsgFinish = 1
		end
		channel, data = GetDebugMessage()
	end
end

function getMsgNear( szMsg )

	while true do
		local channel, time, name, data , _ , idObj = GetChatMsg()

		if channel == -1 then
			return fasle
		end

		if string.find( data , tostring( szMsg ) ) then
			return true
		end
	end
	return false
end


function clearMsg()
	local channel, data = GetDebugMessage()
	while channel == 1 do
		channel, data = GetDebugMessage()
	end
end

 

function getNameFromMsg( szMsg , szSub )

	local i , nStart = string.find( tostring( szMsg ) , tostring( szSub ) )
	nStart = nStart + 6

	local nEnd = string.find( tostring( szMsg ) , "}" , nStart )
	nEnd = nEnd - 1

	local szName = string.sub( tostring( szMsg ) , tonumber( nStart ) , tonumber( nEnd ) )

	

	return tostring( szName )

end



function getMsgNameInfo( szSub )
	local channel, data = GetDebugMessage()
	while channel == 1 do

		

		if string.find( data , tostring( szSub ) ) then
			local szName = getNameFromMsg( tostring( data ) , tostring( szSub ) )
			if szName ~= nil then
				
				return tostring( szName )
			end
		end
		channel, data = GetDebugMessage()
	end
	return nil
end


function setKeepOffSpecial( nR )
	local n = EnumObject(true)
	if n == 0 then
		return 0
	end
	AI:SetAdv("в" )
	for i = 0 , n - 1 do
		local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )

		if "SPECIAL" == tostring( classObj ) then			
			AI:SetAdv("в" , idObj , nR , tostring( nameObj ) )
		end
	end
	return 1
	
end


function isOutSpecial( nR )

	local n = EnumObject(true)
	if n == 0 then
		return false
	end

	local xPos , yPos = Player:GetPos()
	local bResult = true
	for i = 0 , n - 1 do
		local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )

		if "SPECIAL" == tostring( classObj ) then
			if GetDist( xPos , yPos , xObj , yObj ) <= nR then
				bResult = false
			end
		end
	end

	return bResult
	
end

function isBossOutSpecial( idBoss , nR )

	local n = EnumObject(true)
	if n == 0 then
		return false
	end

	local xPos , yPos , _ = getTargetPosID( idBoss )
	if xPos ~= -1 and yPos ~= -1 then
		local bResult = true
		for i = 0 , n - 1 do
			local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )

			if "SPECIAL" == tostring( classObj ) then
				if GetDist( xPos , yPos , xObj , yObj ) <= nR then
					return false
				end
			end
		end
	end

	return true
	
end

function exeFightBoss1( monsterID )

	Player:EnableTeamFollow( false )
	offRide()
	AI:SetAI("߼ս") 
	AI:SetParameter( "ͼ" , -1 )
	g_mainguard_r = 17
	AI:SetAdv( "Ȧ" , g_nX_O , g_nY_O , g_mainguard_r ) 
	AI:Start()
	AI:SetAdv( "ָɱ" , -1 , "" )
	g_bYinGuai = 0

	g_bTeamFollow = 0
	g_bTeamAttack = 0

	g_bStopAssistSelf = 0
	g_bStopAssist = 0
	g_bStopSpecialAttack = 0

	g_bYinshen = 0
	g_FollowTarget = -1

	g_bKillMonster = 1
	g_bCallPet = 0
	g_bPickUp = 0
	local bKeepOff = false
	local bFightSmall = false
	local szNameKeep = nil
	local idOldSmall = -1
	local bGetID = false
	local bFirst = false

	while true do

		System:Sleep( 10 )

		if GetActiveSceneName() ~= tostring( g_szNameSence[ 2 ] ) then
			break
		end

		if isNearXY( 63 ,56 , 5 ) == 1 then
			break
		end

		if Player:GetData( "MP" ) < Player:GetData( "MAXMP" )*0.2 then
			g_bCallPet = 1
		else
			g_bCallPet = 0
		end

		local idBossSmall = getTargetID( tostring( g_objBossSmall[ 1 ] ) )
		local szNameKeepOff = getMsgNameInfo( "LHFD_160203_75" )

		if idBossSmall  ~= -1 and isLiveName( tostring( g_objBossSmall[ 1 ] ) ) == true then 

			g_bKillMonster = 1

			g_bStopAssistSelf = 0
			g_bStopAssist = 0
			g_bStopSpecialAttack = 0

			if IsMsgCold( "KillSmall" , 100 ) == true then
				bFightSmall = false
			end


			if bFightSmall == false then
				if bGetID == false then
					local idObj = fightNearestDiff( monsterID )
					if idObj ~= -1 then
						if idOldSmall ~= idObj then
							if idObj ~= -1 then
								idOldSmall = idObj

								AI:SetAdv( "ָɱ" , -1 , "" )
								AI:SetAdv( "ָɱ" , idObj , tostring( g_objBossSmall[ 1 ] ) )

								bFightSmall = true
								UpdateMsg( "KillSmall" )

								bGetID = true

							end
						end
					end
				end
			end

			if idOldSmall ~= -1 then
				if isLiveID( idOldSmall ) == false or Character:GetData( idOldSmall , "DIE" ) == true then
					bGetID = false
					bFightSmall = false
				end
			end

		elseif szNameKeepOff ~= nil then

			g_nX_K1 = -1
			g_nY_K1 = -1

			UpdateMsg( "KeepOff" )

			szNameKeep = tostring( szNameKeepOff )

		elseif szNameKeep ~= nil then

			if bKeepOff == false then
				local idKeepOff = getTargetID( tostring( g_objBoss[ 1 ] ) )
				if idKeepOff ~= -1 then

					if tostring( szNameKeep ) == Player:GetName() then
						keepOffBoss1( -1 , idKeepOff )
						UpdateMsg( "KeepOff" )
						bKeepOff = true
					else
						local idObjKeepOff = getTargetID( tostring( szNameKeep ) )
						if idObjKeepOff ~= -1 then
							keepOffBoss1( idObjKeepOff , idKeepOff )							
							bKeepOff = true
						end
					end
				end
			end
			local xPos  , yPos = Player:GetPos()

			if xPos ~= -1 then
				if g_nX_K1 ~= -1 and g_nY_K1 ~= -1 then
					if GetDist( xPos , yPos , g_nX_K1 , g_nY_K1 ) <= 1 then

						g_bKillMonster = 1

						g_bStopAssistSelf = 0
						g_bStopAssist = 0
						g_bStopSpecialAttack = 0

						bFightSmall = false
						g_mainguard_r = 17
						AI:SetAdv( "Ȧ" , g_nX_K1 , g_nY_K1 , g_mainguard_r ) 

					end
				end
			end

			if IsMsgCold( "KeepOff" , 5000 ) == true then
				szNameKeep = nil
				DelMsg( "KeepOff" )
				bKeepOff = false
			end


		else
				bFirst = false

				g_bKillMonster = 1

				g_bStopAssistSelf = 0
				g_bStopAssist = 0
				g_bStopSpecialAttack = 0

				AI:SetAdv( "ָɱ" , -1 ,"" )
				bFightSmall = false

				g_mainguard_r = 17
				AI:SetAdv( "Ȧ" , g_nX_O , g_nY_O , g_mainguard_r ) 


				if g_nMemPai == 4 then
					setRevival( 40 )
				end
		end

	
		if Character:GetData( monsterID , "DIE" ) == true then
			DelMsg( "KillSmall" )
			clearMsg()

			if DataPool:IsTeamLeader() == true then
				mySleep( 2 )
			end

			break
		end

		if g_nNoFight == 1 then
			g_bKillMonster = 0
			g_bYinshen = 1
			g_bStopAssist = 1
			g_mainguard_r = 15
			AI:SetAdv( "Ȧ" , 142 , 62 , g_mainguard_r ) 
		end

		AI:SetParameter("в" , "" , 1 ) 
		AI:SetParameter("ʰȡ" , g_bPickUp ) 
		AI:SetParameter("в" , "ɱ" , g_bKillMonster )
		AI:SetParameter("в" , "" , 0 )
		AI:SetParameter("в" , "Ӹ" , 0 )
		AI:SetParameter("в" , "" , g_bTeamAttack )
		AI:SetParameter("в" , "ֹܸͣ" , g_bStopAssistSelf )
		AI:SetParameter("в" , "ֹͣ" , g_bStopAssist )
		AI:SetParameter("в" , "ֹͣ⹥" , g_bStopSpecialAttack )
		AI:SetParameter("в" , "ٻ" , g_bCallPet ) 
		AI:SetAdv("" , g_FollowTarget , g_nRFollowTarget ) 
		AI:SetAdv("Ȧ뾶" , g_mainguard_r ) 
		AI:SetAdv("" , g_bYinshen ) 

	end
	AI:Stop()
end


function exeFightBoss2( monsterID )

	Player:EnableTeamFollow( false )
	offRide()

	AI:SetAI("߼ս") 
	AI:SetParameter( "ͼ" , -1 )
	if G_bCenter == 1 then
		g_mainguard_r = 2
		AI:SetAdv( "Ȧ" , g_nX_O , g_nY_O , g_mainguard_r )
	else
		g_mainguard_r = 17
		AI:SetAdv( "Ȧ" , g_nX_O , g_nY_O , g_mainguard_r ) 
	end
	AI:Start()
	AI:SetAdv( "ָɱ" , -1 , "" )
	g_bYinGuai = 0
	g_bTeamFollow = 0
	g_bTeamAttack = 0

	g_bStopAssistSelf = 0
	g_bStopAssist = 0
	g_bStopSpecialAttack = 0

	g_bYinshen = 0
	g_FollowTarget = -1

	g_bKillMonster = 1
	g_bCallPet = 1
	g_bPickUp = 0

	local bKeepoff = false
	while true do

		System:Sleep( 10 )

		if GetActiveSceneName() ~= tostring( g_szNameSence[ 2 ] ) then
			break
		end

		if isNearXY( 63 ,56 , 5 ) == 1 then
			break
		end

		if Player:GetData( "MP" ) < Player:GetData( "MAXMP" )*0.2 then
			g_bCallPet = 1
		else
			g_bCallPet = 0
		end

		if G_bCenter == 0 then
			if getTargetID( "" ) ~= -1  or getTargetID( "" ) ~= -1 or getTargetID( "÷" ) ~= -1 or getTargetID( "" ) ~= -1 then

				g_bKillMonster = 0

				g_bStopAssistSelf = 1
				g_bStopAssist = 1
				g_bStopSpecialAttack = 1
				G_bCenter = 1

				continue
			end


			if isClassTarget( "SPECIAL" ) == true and isOutSpecial( 8 ) == false then
				if bKeepOff == false then
					setKeepOffSpecial( 8 )
					bKeepOff = true
	
				end

			else			
				g_bKillMonster = 1

				g_bStopAssistSelf = 0
				g_bStopAssist = 0
				g_bStopSpecialAttack = 0

				bKeepOff = false
				g_mainguard_r = 17
				AI:SetAdv( "Ȧ" , g_nX_O , g_nY_O , g_mainguard_r )


					if g_nMemPai == 4 then
						setRevival( 40 )
					end

			end

		else
			g_mainguard_r = 2
			AI:SetAdv( "Ȧ" , g_nX_O , g_nY_O , g_mainguard_r ) 

--			if Player:GetBuffByName( "" , 1 ) == -1 then
				local xPos , yPos = Player:GetPos()
				if GetDist( xPos , yPos , g_nX_O , g_nY_O ) <= 2 then
					g_bKillMonster = 1

					g_bStopAssistSelf = 0
					g_bStopAssist = 0
					g_bStopSpecialAttack = 0

				else
					g_bKillMonster = 0

					g_bStopAssistSelf = 1
					g_bStopAssist = 1
					g_bStopSpecialAttack = 1
				end
--			end

		end

		if Character:GetData( monsterID , "DIE" ) == true then			
			clearMsg()
			if DataPool:IsTeamLeader() == true then
				mySleep( 2 )
			end

			break
		end

		if g_nNoFight == 1 then
			g_bKillMonster = 0
			g_bYinshen = 1
			g_bStopAssist = 1
			g_mainguard_r = 20
			AI:SetAdv( "Ȧ" , g_nX_O , g_nY_O , g_mainguard_r ) 
		end


		AI:SetParameter("в" , "" , 1 ) 
		AI:SetParameter("ʰȡ" , g_bPickUp ) 
		AI:SetParameter("в" , "ɱ" , g_bKillMonster )
		AI:SetParameter("в" , "" , 0 )
		AI:SetParameter("в" , "Ӹ" , 0 )
		AI:SetParameter("в" , "" , g_bTeamAttack )
		AI:SetParameter("в" , "ֹܸͣ" , g_bStopAssistSelf )
		AI:SetParameter("в" , "ֹͣ" , g_bStopAssist )
		AI:SetParameter("в" , "ֹͣ⹥" , g_bStopSpecialAttack )
		AI:SetParameter("в" , "ٻ" , g_bCallPet ) 
		AI:SetAdv("" , g_FollowTarget , g_nRFollowTarget ) 
		AI:SetAdv("Ȧ뾶" , g_mainguard_r ) 
		AI:SetAdv("" , g_bYinshen ) 

	end
	AI:Stop()
end



function getMsgBlack()
	local channel, data = GetDebugMessage()
	while channel == 1 do
		if string.find( data , "ī֣֮λӢٻɱ" ) then
			
			return true
		end
		channel, data = GetDebugMessage()
	end
	return false
end

function goIntoNpcBoss3( szName )
	local idNpcInto , xNpc , yNpc = getTargetPos( tostring( szName ) )
	if xNpc ~= -1 then

		g_bKillMonster = 0

		g_bStopAssistSelf = 1
		g_bStopAssist = 1
		g_bStopSpecialAttack = 1

		g_nRFollowTarget = 1
		g_FollowTarget = idNpcInto
		
		AI:SetAdv( "Ȧ" , xNpc , yNpc , g_mainguard_r )

		return true
	end
	return false
end



local g_objSpecial = { nX , nY }

function setListSpecial( szNameObj )

	g_objSpecial = { nX , nY }

	local n = EnumObject(true)

	if n == 0 then
		
		return false
	end
	if szNameObj == nil then

		local nIndex = 1
		for i = 0 , n - 1 do
			local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
			if tostring( classObj ) == "SPECIAL" then
				if xObj ~= -1 and yObj ~= -1 then
					g_objSpecial[ nIndex ] = { nX = xObj , nY = yObj }
					nIndex = nIndex + 1
				end
			end
		end

	else

		local nIndex = 1
		for i = 0 , n - 1 do
			local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
			if tostring( classObj ) == "SPECIAL" then

				if tostring( szNameObj ) == nameObj then
					if xObj ~= -1 and yObj ~= -1 then
						g_objSpecial[ nIndex ] = { nX = xObj , nY = yObj }
						nIndex = nIndex + 1
					end
				end
			end
		end

	end
	return true
end

function getNearestSpecial( nX , nY )

	local nNumMember = table.getn( g_objSpecial )

	local nIndex = -1

	if  nNumMember <= 0 then
		
		return -1
	elseif nNumMember == 1 then

		return 1

	else
		local nMinDist = GetDist( g_objSpecial[ 1 ].x , g_objSpecial[ 1 ].y , nX , nY )
		nIndex = 1

		for i = 2 , nNumMember do

			local nDist = GetDist( g_objSpecial[ i ].x , g_objSpecial[ i ].y , nX , nY )

			if nDist < nMinDist then

				nMinDist = nDist
				nIndex = i
			end
		end
	end

	return tonumber( g_objSpecial[ nIndex ].x ) , tonumber( g_objSpecial[ nIndex ].y )
end



local g_objPointOut = { nX , nY }

function isPointOut( x , y , nR )

	local nNumMember = table.getn( g_objSpecial )

	if  nNumMember <= 0 then
		
		return -1
	end

	for i = 1 , nNumMember do

		if GetDist( x , y , g_objSpecial[ i ].nX , g_objSpecial[ i ].nY ) <= nR then
			return 0
		end

	end
	return 1
end

function setListPointOut( nR )


	local nNumMember = table.getn( g_objCoordinate )
	g_objPointOut = { nX , nY }
	if  nNumMember <= 0 then
		
		return -1
	end
	local nIndex = 1
	for i = 1 , nNumMember do
		if GetDist( g_objCoordinate[ i ].nX , g_objCoordinate[ i ].nY , g_nX_O , g_nY_O ) < tonumber( g_nMaxR1 ) and isPointOut( g_objCoordinate[ i ].nX , g_objCoordinate[ i ].nY , nR ) == 1 then

			g_objPointOut[ nIndex ] = { nX = g_objCoordinate[ i ].nX , nY = g_objCoordinate[ i ].nY }
			nIndex = nIndex + 1
		end

	end
	return 1

end


function getMinPointOut( nX , nY )

	local nNumMember = table.getn( g_objPointOut )

	if  nNumMember <= 0 then
		
		return -1 , -1
	end

	local nIndex = -1
	local nMinDist = GetDist( g_objPointOut[ 1 ].nX , g_objPointOut[ 1 ].nY , nX , nY )
	nIndex = 1
	for i = 2 , nNumMember do

		local nDist = GetDist( g_objPointOut[ i ].nX , g_objPointOut[ i ].nY , nX , nY )

		if nDist < nMinDist then			
			nMinDist = nDist
			nIndex = i

		end
	end

	return tonumber( g_objPointOut[ nIndex ].nX ) , tonumber( g_objPointOut[ nIndex ].nY )
end


function keepOffCircle( szNameCircle )
	local xPos , yPos = Player:GetPos()
	if isPointOut( xPos , yPos , 4 ) == 0 then

		if szNameCircle == nil then
			setListSpecial( nil )
		else
			setListSpecial( tostring( szNameCircle ) )
		end
		g_objCoordinate = { nX , nY , nAngle }
		setPointCircle( xPos , yPos , 45 , 8 )
		setListPointOut( 2 )
		local xResult , yResult = getMinPointOut( xPos , yPos )

		if xResult ~= -1 then

			g_bKillMonster = 0

			g_bStopAssistSelf = 1
			g_bStopAssist = 1
			g_bStopSpecialAttack = 1

			g_mainguard_r = 1
			AI:SetAdv( "Ȧ" , xResult , yResult , g_mainguard_r )
			return true
		end
	end
	return false		
end






local g_objMonster = { nID , x , y }
local g_idMonsterDiff = -1

function setObjShort( szNameObj )
	
	g_objMonster = { nID , x , y }

	local n = EnumObject(true)

	if n == 0 then
		return false
	end
	local nIndex = 1
	if tostring( szNameObj ) == "MON" then
		for i = 0 , n - 1 do
			local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
			if tostring( classObj ) == "MON" then
				if g_idMonsterDiff ~= -1 then
					if g_idMonsterDiff ~= idObj and idObj ~= -1 then
						if Character:GetData( idObj , "DIE" ) == false then
							g_objMonster[ nIndex ] = { nID = idObj , x = xObj , y = yObj }
							
							nIndex = nIndex + 1
						end
					end
				end
			end

		end
	else

		for i = 0 , n - 1 do
			local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
			if tostring( nameObj ) == tostring( szNameObj ) and Character:GetData( idObj , "DIE" ) == false then
				g_objMonster[ nIndex ] = { nID = idObj , x = xObj , y = yObj }
				 
				nIndex = nIndex + 1
			end
		end
	end
	return true

end

function countMonster( szNameObj )
	
	local n = EnumObject(true)

	if n == 0 then
		return -1
	end
	local nCount = 0
	for i = 0 , n - 1 do
		local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )

		if tostring( classObj ) == "MON" then
			if tostring( nameObj ) == tostring( szNameObj ) then
				if  idObj ~= -1 and Character:GetData( idObj , "DIE" ) == false then
					nCount = nCount + 1
				end
			end
		end
	end

	return nCount
end

function setObjShort2( szNameObj )
	
	g_objMonster = { nID , x , y }

	local n = EnumObject(true)

	if n == 0 then
		return false
	end
	local nIndex = 1
	for i = 0 , n - 1 do
		local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
		if tostring( nameObj ) == tostring( szNameObj ) then
			if idObj ~= -1 then
				if Character:GetData( idObj , "DIE" ) == false then
					g_objMonster[ nIndex ] = { nID = idObj , x = xObj , y = yObj }
					
					nIndex = nIndex + 1
				end
			end
		end
	end
	return true

end

function getNearest( nX , nY , szNameMonster )

	setObjShort2( tostring( szNameMonster ) )

	local nNumMember = table.getn( g_objMonster )

	if  nNumMember <= 0 then
		return -1 , -1 , -1
	end

	local nIndex = 1

	local nMinDist = GetDist( g_objMonster[ 1 ].x , g_objMonster[ 1 ].y , nX , nY )

	for i = 2 , nNumMember do

		local nDist = GetDist( g_objMonster[ i ].x , g_objMonster[ i ].y , nX , nY )

		if nDist < nMinDist then
			nMinDist = nDist
			 nIndex = i
		end
	end

	return g_objMonster[ nIndex ].nID , g_objMonster[ nIndex ].x , g_objMonster[ nIndex ].y
end


local g_objListAngle = { nAngle }
local g_objBigMore = { nAngle , nID , nX , nY }
local g_objClock = { nAngle , nID , nX , nY }


function isBigMore( nMyMinAngle )
	local nNum = table.getn( g_objListAngle )
	if nNum <= 0 then
		
		return false
	end

	g_objBigMore = { nAngle , nID , nX , nY }

	local nIndex = 1
	for i = 1 , nNum do
		if tonumber( g_objListAngle[ i ].nAngle ) >= nMyMinAngle then
			g_objBigMore[ nIndex ] = { nAngle = g_objListAngle[ i ].nAngle , nID = g_objMonster[ i ].nID , nX = g_objMonster[ i ].x , nY = g_objMonster[ i ].y  }
			
			nIndex = nIndex + 1
		end
	end
	return true
end


function isBigMoreMySelf( nMyAngle )

	for i = 1 , table.getn( g_objClock ) do
		if tonumber( g_objClock[ i ].nAngle ) >= nMyAngle then
			return true
		end
	end

	return false
end

function getBigMore( nMyAngle )
	for i = 1 , table.getn( g_objClock ) do
		if tonumber( g_objClock[ i ].nAngle ) >= nMyAngle then
			if isLiveID( tonumber( g_objClock[ i ].nAngle ) ) == true then
				if Character:GetData( tonumber( g_objClock[ i ].nAngle ) , "DIE" ) == false then
					return i
				end
			end
		end
	end
	return -1
end

function getObjFight()

	local nNum = table.getn( g_objClock )
	if nNum <= 0 then
		return -1 , -1 , -1
	end
	if nNum == 1 then
		if g_objClock[ 1 ].nAngle ~= nil then
			return tonumber( g_objClock[ 1 ].nID ) , tonumber( g_objClock[ 1 ].nX ) , tonumber( g_objClock[ 1 ].nY )
		end
	end

	local nMyAngle = getMyAngle()

	if nMyAngle ~= -1 then
		

		if isBigMoreMySelf( nMyAngle ) == true then
			
			local nIndex = getBigMore( nMyAngle )
			if nIndex ~= -1 then

				
				return tonumber( g_objClock[ nIndex ].nID ) , tonumber( g_objClock[ nIndex ].nX ) , tonumber( g_objClock[ nIndex ].nY )
			end
		else
			
			if g_objClock[ 1 ].nAngle ~= nil then

				
				return tonumber( g_objClock[ 1 ].nID ) , tonumber( g_objClock[ 1 ].nX ) , tonumber( g_objClock[ 1 ].nY )
			end
		end
	end

	return -1 , -1 , -1
end

function shortObjClock()

	local nNumMember = table.getn( g_objClock )	
	if nNumMember <= 0 then
		
		return false
	end

	
	local objTemp = { nAngle , nID , nX , nY }

	for i = 1 , nNumMember - 1 do
		for j = i + 1 , nNumMember do

			objTemp = { nAngle , nID , nX , nY }

			if tonumber( g_objClock[ j ].nAngle ) < tonumber( g_objClock[ i ].nAngle ) then

				objTemp[ 1 ] = { nAngle = tonumber( g_objClock[ j ].nAngle ) , nID = tonumber( g_objClock[ j ].nID ) , nX = tonumber( g_objClock[ j ].nX ) , nY = tonumber( g_objClock[ j ].nY ) }

				g_objClock[ j ].nAngle = tonumber( g_objClock[ i ].nAngle )
				g_objClock[ j ].nID = tonumber( g_objClock[ i ].nID )
				g_objClock[ j ].nX = tonumber( g_objClock[ i ].nX )
				g_objClock[ j ].nY = tonumber( g_objClock[ i ].nY )


				g_objClock[ i ].nAngle = tonumber( objTemp[ 1 ].nAngle )
				g_objClock[ i ].nID = tonumber( objTemp[ 1 ].nID )
				g_objClock[ i ].nX = tonumber( objTemp[ 1 ].nX )
				g_objClock[ i ].nY = tonumber( objTemp[ 1 ].nY )

			end
		end
	end

	return true
end


function setListAngleMonter()

	local nNum = table.getn( g_objMonster )
	if nNum <= 0 then
		
		return false
	end

	g_objClock = { nAngle , nID , nX , nY }
	local nIndex = 1
	for i = 1 , nNum do
		local nAngleM = getMinAngle( g_objMonster[ i ].x , g_objMonster[ i ].y )
		if nAngleM ~= -1 then
			g_objClock[ nIndex ] = { nAngle = nAngleM , nID = g_objMonster[ i ].nID , nX = g_objMonster[ i ].x , nY = g_objMonster[ i ].y }
			nIndex = nIndex + 1
		end
	end
	return shortObjClock()	
end

function isLiveClock( nIndex )
	local nNum = table.getn( g_objBigMore )

	for i = 1 , nNum do
		if nIndex == i then
			if getTargetID( tonumber( g_objBigMore[ i ].nID ) ) ~= -1 then
				if Character:GetData( tonumber( g_objBigMore[ i ].nID ) , "DIE" ) == false then
					return true
				else
					return false
				end
			else
				return false
			end
		end
	end
end

function getMyAngle()

	local xPos , yPos = Player:GetPos()
	if xPos ~= -1 then
		return getMinAngle( xPos , yPos )
	end
	return -1
end

function getCounterClockWise( szNameMonster )

	setObjShort( tostring( szNameMonster ) )

	local nNumMember = table.getn( g_objMonster )

	if  nNumMember <= 0 then
		
		return -1 , -1 , -1
	end

	local nIndex = 1

	g_objCoordinate = { nX , nY , nAngle }
	setPointCircle( g_nX_O , g_nY_O , 45 , GetDist( g_objMonster[ 1 ].x , g_objMonster[ 1 ].y , g_nX_O , g_nY_O ) )

	return setListAngleMonter()

end
function setObjShortID( idDiff )
	
	g_objMonster = { nID , x , y }

	local n = EnumObject(true)

	if n == 0 then
		return false
	end

	local nIndex = 1
	for i = 0 , n - 1 do
		local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
		if tostring( classObj ) == "MON" then
			if idObj ~= -1 then
				if idObj ~= idDiff then
					if Character:GetData( idObj , "DIE" ) == false then
						if xObj ~= -1 and yObj ~= -1 then
							g_objMonster[ nIndex ] = { nID = idObj , x = xObj , y = yObj }
							nIndex = nIndex + 1
						end
					end
				end
			end
		end
	end

	return true

end

function getNearestID( nX , nY , idBoss )

	setObjShortID( idBoss )

	local nNumMember = table.getn( g_objMonster )

	local idMonster = -1
	if  nNumMember <= 0 then
		return -1
	elseif nNumMember == 1 then

		return g_objMonster[ 1 ].nID

	else
		local nMinDist = GetDist( g_objMonster[ 1 ].x , g_objMonster[ 1 ].y , nX , nY )
		idMonster = g_objMonster[ 1 ].nID

		for i = 2 , nNumMember do

			local nDist = GetDist( g_objMonster[ i ].x , g_objMonster[ i ].y , nX , nY )

			if nDist < nMinDist then

				nMinDist = nDist
				idMonster = g_objMonster[ i ].nID
			end
		end
	end

	return idMonster
end


function fightNearestDiff( idBoss )
	local xPos , yPos = Player:GetPos()

	local idFight = getNearestID( xPos , yPos , idBoss )
	if idFight ~= -1 then
		return idFight
	end
	return -1
end


function getTargetNearest( szNameMonster )
	local xPos , yPos = Player:GetPos()
	local idFight = -1
	local x = -1
	local y = -1
	if xPos ~= -1 then
		idFight , x , y = getNearest( xPos , yPos , tostring( szNameMonster ) )
	end
	return idFight , x , y
end


local g_nX_Black = -1
local g_nY_Black = -1
local g_nAngleOld = -1


function keepOffBlack()

	local _ , x , y = getTargetPos( tostring( g_objBoss[ 3 ] ) )

	if x ~= -1 then
		g_objCoordinate = { nX , nY , nAngle }
		local nR = GetDist( x , y , g_nX_O , g_nY_O )
		if nR > 17 then
			nR = 17
		elseif nR < 15 then
			nR = 17
		end
		setPointCircle( g_nX_O , g_nY_O , 45 , nR )

--		local xPos , yPos = Player:GetPos()
--		local nMyMinAngle = getMinAngle( xPos , yPos )
		local nMyMinAngle = getMinAngle( x , y )

		if nMyMinAngle >= 8 then

			g_mainguard_r = 1

			if GetDist( g_objCoordinate[ 1 ].nX , g_objCoordinate[ 1 ].nY , x , y ) > 4 then
				g_bKillMonster = 0

				g_bStopAssistSelf = 1
				g_bStopAssist = 1
				g_bStopSpecialAttack = 1

				g_nX_Black = g_objCoordinate[ 1 ].nX
				g_nY_Black = g_objCoordinate[ 1 ].nY
				g_nAngleOld = 1

				AI:SetAdv( "Ȧ" , g_objCoordinate[ 1 ].nX , g_objCoordinate[ 1 ].nY , g_mainguard_r )
				return true
			else
				g_bKillMonster = 0

				g_bStopSpecialAttack = 1
				g_bStopAssistSelf = 1
				g_bStopAssistSelf = 1

				g_nX_Black = g_objCoordinate[ 2 ].nX
				g_nY_Black = g_objCoordinate[ 2 ].nY
				g_nAngleOld = 2

				AI:SetAdv( "Ȧ" , g_objCoordinate[ 2 ].nX , g_objCoordinate[ 2 ].nY , g_mainguard_r )
				return true
			end

		else
			if GetDist( g_objCoordinate[ nMyMinAngle + 1 ].nX , g_objCoordinate[ nMyMinAngle + 1 ].nY , x , y ) > 4 then

				g_bKillMonster = 0

				g_bStopAssistSelf = 1
				g_bStopAssist = 1
				g_bStopSpecialAttack = 1

				g_mainguard_r = 1

				g_nX_Black = g_objCoordinate[ nMyMinAngle + 1 ].nX
				g_nY_Black = g_objCoordinate[ nMyMinAngle + 1 ].nY
				g_nAngleOld = nMyMinAngle + 1

				AI:SetAdv( "Ȧ" , g_objCoordinate[ nMyMinAngle + 1 ].nX , g_objCoordinate[ nMyMinAngle + 1 ].nY , g_mainguard_r )
				return true
			else
				g_mainguard_r = 1

				if nMyMinAngle >= 7 then

					g_bKillMonster = 0

					g_bStopAssistSelf = 1
					g_bStopAssist = 1
					g_bStopSpecialAttack = 1

					g_nX_Black = g_objCoordinate[ 1 ].nX
					g_nY_Black = g_objCoordinate[ 1 ].nY
					g_nAngleOld = 1

					AI:SetAdv( "Ȧ" , g_objCoordinate[ 1 ].nX , g_objCoordinate[ 1 ].nY , g_mainguard_r )
					return true
				else
					g_bKillMonster = 0

					g_bStopAssistSelf = 1
					g_bStopAssist = 1
					g_bStopSpecialAttack = 1


					g_nX_Black = g_objCoordinate[ nMyMinAngle + 2 ].nX
					g_nY_Black = g_objCoordinate[ nMyMinAngle + 2 ].nY
					g_nAngleOld = nMyMinAngle + 2

					AI:SetAdv( "Ȧ" , g_objCoordinate[ nMyMinAngle + 2 ].nX , g_objCoordinate[ nMyMinAngle + 2 ].nY , g_mainguard_r )

					return true
				end
			end

		end

	end
	
	return false
end



function isManyNpc()

	local n = EnumObject(true)

	if n <= 0 then
		return false
	end

	for i = 0 , n - 1 do
		local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
		if tostring( classObj ) == "NPC" then
			if tostring( nameObj ) == "Ǭ" or tostring( nameObj ) == "" or tostring( nameObj ) == "" or tostring( nameObj ) == ""
			or tostring( nameObj ) == "" or tostring( nameObj ) == "" or tostring( nameObj ) == "" or tostring( nameObj ) == "" then
				return true
			end
		end
	end
	return false
end





local g_objCanPoint = { nX , nY }
local g_objTempCanPoint = { nX , nY , nR }
local g_objTargetKeep = { nID , nX , nY , nR }

function setCanPoint()

	local nNumMember = table.getn( g_objCoordinate )

	if  nNumMember <= 0 then
		return -1 , -1
	end

	g_objCanPoint = { nX , nY }

	local nIndex = 1
	for i = 1 , nNumMember do
		if GetDist( g_objCoordinate[ i ].nX , g_objCoordinate[ i ].nY , g_nX_O , g_nY_O ) < tonumber( g_nMaxR1 ) then
			g_objCanPoint[ nIndex ] = { nX = g_objCoordinate[ i ].nX , nY = g_objCoordinate[ i ].nY }
			nIndex = nIndex + 1
		end
	end
end


function setTempCanPoint( rObj )

	local nNumMember = table.getn( g_objCoordinate )

	if  nNumMember <= 0 then
		return false
	end

	

	local nIndex = 1
	local nCount = table.getn( g_objTempCanPoint )

	if nCount > 0  then
		nIndex = nCount + 1
	end

	
	for i = 1 , nNumMember do
		if GetDist( g_objCoordinate[ i ].nX , g_objCoordinate[ i ].nY , g_nX_O , g_nY_O ) <= tonumber( g_nMaxR1 ) then

			g_objTempCanPoint[ nIndex ] = { nX = g_objCoordinate[ i ].nX , nY = g_objCoordinate[ i ].nY , nR = rObj }
			nIndex = nIndex + 1
		end
	end

	return true
end


function isOutPointKeep( nX , nY )
	local nNumMember = table.getn( g_objTargetKeep )
	if nNumMember <= 0 then
		return false
	end

	for i = 1 , nNumMember do
		if GetDist( g_objTargetKeep[ i ].nX , g_objTargetKeep[ i ].nY , nX , nY ) < g_objTargetKeep[ i ].nR then
			return false
		end
	end

	return true
end




function setManyCanPoint()
	local nNumMember = table.getn( g_objTempCanPoint )

	if  nNumMember <= 0 then
		return false
	end

	local nIndex = 1

	for i = 1 , nNumMember do		
		if isOutPointKeep( g_objTempCanPoint[ i ].nX , g_objTempCanPoint[ i ].nY ) == true then

			g_objCanPoint[ nIndex ] = { nX = g_objTempCanPoint[ i ].nX , nY = g_objTempCanPoint[ i ].nY }
			nIndex = nIndex + 1
		end

	end

	return true	
end


function getMinCanPoint( nX , nY )
	


	local nNumMember = table.getn( g_objCanPoint )
	if nNumMember <= 0 then
		return -1 , -1
	end


	local nIndex = 1
	local nMinDist = GetDist( g_objCanPoint[ 1 ].nX , g_objCanPoint[ 1 ].nY , nX , nY )
		
	for i = 2 , nNumMember do
		local nDist = GetDist( g_objCanPoint[ i ].nX , g_objCanPoint[ i ].nY , nX , nY )

		

		if nDist < nMinDist then			
			nMinDist = nDist
			nIndex = i
		end
	end
	return g_objCanPoint[ nIndex ].nX , g_objCanPoint[ nIndex ].nY
end




function getMaxCanPoint( nX , nY )
	


	local nNumMember = table.getn( g_objCanPoint )
	if nNumMember <= 0 then
		return -1 , -1
	end


	local nIndex = 1
	local nMinDist = GetDist( g_objCanPoint[ 1 ].nX , g_objCanPoint[ 1 ].nY , nX , nY )
		
	for i = 2 , nNumMember do
		local nDist = GetDist( g_objCanPoint[ i ].nX , g_objCanPoint[ i ].nY , nX , nY )

		

		if nDist > nMinDist then			
			nMinDist = nDist
			nIndex = i
		end
	end
	return g_objCanPoint[ nIndex ].nX , g_objCanPoint[ nIndex ].nY
end


function keepOffOneTarget( idObjKeep , nR )
	local xKeep , yKeep , _ = getTargetPosID( idObjKeep )
	if xKeep ~= -1 then
		g_objCoordinate = { nX , nY , nAngle }
		setPointCircle( xKeep , yKeep , 45 , nR )
		g_objCanPoint = { nX , nY }
		setCanPoint()
		local x , y = getMinCanPoint( xKeep , yKeep )
		if x ~= -1 and y ~= -1 then
			g_bKillMonster = 0

			g_bStopAssistSelf = 1
			g_bStopAssist = 1
			g_bStopSpecialAttack = 1
	
			g_mainguard_r = 1
			
			AI:SetAdv( "Ȧ" , x , y , g_mainguard_r )
			UpdateMsg( "keepOffOneTarget" )
			return true
		end
	end
	return false
end


function keepOffTarget( nR )
	local nNumMember = table.getn( g_objTargetKeep )
	if nNumMember <= 0 then
		return false
	end

	g_objTempCanPoint = { nX , nY , nR }

	for i = 1 , nNumMember do

		g_objCoordinate = { nX , nY , nAngle }
		setPointCircle( g_objTargetKeep[ i ].nX , g_objTargetKeep[ i ].nY , 45 , nR )
		setTempCanPoint( nR )
	end
	g_objCanPoint = { nX , nY }
	setManyCanPoint()

	local xPos , yPos = Player:GetPos()
	local x , y = getMinCanPoint( xPos , yPos )
	if x ~= -1 and y ~= -1 then
		g_bKillMonster = 0

		g_bStopAssistSelf = 1
		g_bStopAssist = 1
		g_bStopSpecialAttack = 1
	
		g_mainguard_r = 1
			
		AI:SetAdv( "Ȧ" , x , y , g_mainguard_r )	

		UpdateMsg( "KeepOffTarget" )
		return true
	end

end



function keepOffMulti()
	local nNumMember = table.getn( g_objTargetKeep )
	if nNumMember <= 0 then
		return false
	end

	local xPos ,  yPos = Player:GetPos()
	if isOutPointKeep( xPos , yPos ) == true then
		g_mainguard_r = 1
		g_nX_Keep = xPos
		g_nY_Keep = yPos
		return true	
	else
		g_objTempCanPoint = { nX , nY , nR }

		for i = 1 , nNumMember do
			g_objCoordinate = { nX , nY , nAngle }
			setPointCircle( g_objTargetKeep[ i ].nX , g_objTargetKeep[ i ].nY , 45 , g_objTargetKeep[ i ].nR + 1 )
			setTempCanPoint( g_objTargetKeep[ i ].nR )
		end

		g_objCanPoint = { nX , nY }
		setManyCanPoint()

		local xPos , yPos = Player:GetPos()
		local x , y = getMinCanPoint( xPos , yPos )
		if x ~= -1 and y ~= -1 then
			g_bKillMonster = 0

			g_bStopAssistSelf = 1
			g_bStopAssist = 1
			g_bStopSpecialAttack = 1
	
			g_mainguard_r = 1
			g_nX_Keep = x
			g_nY_Keep = y			
			AI:SetAdv( "Ȧ" , x , y , g_mainguard_r )	


			return true
		end
	end
	return false
end






function keepOffMultiMax()
	local nNumMember = table.getn( g_objTargetKeep )
	if nNumMember <= 0 then
		return false
	end
	
	local xPos ,  yPos = Player:GetPos()
	if isOutPointKeep( xPos , yPos ) == true then
		g_mainguard_r = 1
		g_nX_Keep = xPos
		g_nY_Keep = yPos
		return true	
	else
		g_objTempCanPoint = { nX , nY , nR }

		for i = 1 , nNumMember do
			g_objCoordinate = { nX , nY , nAngle }
			setPointCircle( g_objTargetKeep[ i ].nX , g_objTargetKeep[ i ].nY , 45 , g_objTargetKeep[ i ].nR + 1 )
			setTempCanPoint( g_objTargetKeep[ i ].nR )
		end

		g_objCanPoint = { nX , nY }
		setManyCanPoint()

		local xPos , yPos = Player:GetPos()
		local x , y = getMaxCanPoint( xPos , yPos )
		if x ~= -1 and y ~= -1 then
			g_bKillMonster = 0

			g_bStopAssistSelf = 1
			g_bStopAssist = 1
			g_bStopSpecialAttack = 1
	
			g_mainguard_r = 1
			g_nX_Keep = x
			g_nY_Keep = y			
			AI:SetAdv( "Ȧ" , x , y , g_mainguard_r )	


			return true
		end
	end
	return false
end


function addKeepOffTarget( idObj , xObj , yObj , rObj )

	local nNumMember = table.getn( g_objTargetKeep )
	local nIndex = 1
	if nNumMember > 0 then
		nIndex = nNumMember + 1
	end

	g_objTargetKeep[ nIndex ] = { nID = idObj , nX = xObj , nY = yObj , nR = rObj }
end





local g_objTarget = { nID , nX , nY }

function setListObjTarget( szNameKeep )

	g_objTarget = { nID , nX , nY }

	local n = EnumObject(true)
	if n == 0 then		
		return false
	end

	g_objTargetKeep = { nID , nX , nY , nR }

	if tostring( szNameKeep ) == "PLAYER" or tostring( szNameKeep ) == "SPECIAL" then

		local nIndex = 1
		for i = 0 , n - 1 do
			local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
			if classObj == tostring( szNameKeep ) then
				if xObj ~= -1 and yObj ~= -1 then

					g_objTarget[ nIndex ] = { nID = idObj , nX = xObj , nY = yObj }
					nIndex = nIndex + 1
				end
			end
		end
	elseif tostring( szNameKeep ) == "FRIEND" then

		local nCount = DataPool:GetTeamMemCount()
		if nCount <= 0  then
			return false
		end
		local nIndex = 1
		for i = 0 , nCount - 1  do
			local szName , idPlayer ,  _, idMap , xCurPos , yCurPos = DataPool:GetTeamMemInfo( i )
			if szName ~= Player:GetName() and szName ~= nil then
				if xCurPos ~= -1 and yCurPos ~= -1 then
					g_objTarget[ nIndex ] = { nID = idPlayer , nX = xCurPos , nY = yCurPos }
					nIndex = nIndex + 1
				end
			end
		end

	else
		local nIndex = 1
		for i = 0 , n - 1 do
			local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
			if nameObj == tostring( szNameKeep ) then
				if xObj ~= -1 and yObj ~= -1 then

					g_objTarget[ nIndex ] = { nID = idObj , nX = xObj , nY = yObj }
					nIndex = nIndex + 1
				end
			end
		end


	end

	return true
end

function isNearTarget( szName , rObj )

	setListObjTarget( szName )
	local nNum = table.getn( g_objTarget )
	if nNum <= 0 then
		return false
	end
	local xPos , yPos = Player:GetPos()
	for i = 1 , nNum do
		if GetDist( xPos , yPos ,  g_objTarget[ i ].nX , g_objTarget[ i ].nY ) <= rObj then
			return true

		end
	end

	return false
end

function keepOffTargetName( szNameKeep , rObj )

	if isNearTarget( szNameKeep , rObj ) == true then

		g_objTargetKeep = { nID , nX , nY , nR }

		local nNum = table.getn( g_objTarget )
		if nNum <= 0 then
			return false
		end

		for i = 1 , nNum do
			addKeepOffTarget( g_objTarget[ i ].nID , g_objTarget[ i ].nX , g_objTarget[ i ].nY , rObj )
		end
	
		return keepOffMulti()

	else
		return true
	end	
end


function keepOffTargetNameDist( szNameKeep , rObj , nDistMySelf )

	if isNearTarget( szNameKeep , rObj ) == true then

		g_objTargetKeep = { nID , nX , nY , nR }

		local nNum = table.getn( g_objTarget )
		if nNum <= 0 then
			return false
		end
		local xPos , yPos = Player:GetPos()
		if xPos ~= -1 then
			for i = 1 , nNum do
				
				if GetDist( xPos , yPos , tonumber( g_objTarget[ i ].nX ) , tonumber( g_objTarget[ i ].nY ) ) <= ( nDistMySelf + rObj ) then
					addKeepOffTarget( g_objTarget[ i ].nID , g_objTarget[ i ].nX , g_objTarget[ i ].nY , rObj )
				end
			end
	
			return keepOffMulti()
		else
			return false
		end
	end
	return true
end


function keepOffTargetNameMax( szNameKeep , rObj )

	if isNearTarget( szNameKeep , rObj ) == true then

		g_objTargetKeep = { nID , nX , nY , nR }

		local nNum = table.getn( g_objTarget )
		if nNum <= 0 then
			return false
		end

		for i = 1 , nNum do
			addKeepOffTarget( g_objTarget[ i ].nID , g_objTarget[ i ].nX , g_objTarget[ i ].nY , rObj )
		end
	
		return keepOffMultiMax()

	else
		return true
	end	
end



function keepOffTargetID( idTarget , rObj )

	local n = EnumObject(true)
	if n == 0 then		
		return flase
	end

	g_objTargetKeep = { nID , nX , nY , nR }

	local nIndex = 1
	for i = 0 , n - 1 do
		local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
		if idTarget == idObj then
			if xObj ~= -1 and yObj ~= -1 then
				local xPos , yPos = Player:GetPos()
				if GetDist( xPos , yPos , xObj , yObj ) < rObj then
					addKeepOffTarget( idObj , xObj , yObj , rObj )
					break
				else
					return true
				end
			end
		end
	end

	return keepOffMulti()
	
end



function getMsgBossInto()

	local channel, data = GetDebugMessage()
	while channel == 1 do

		

		if string.find( data , "LHFD_160203_84" ) then

			return getNameFromMsg( tostring( data ) , "LHFD_160203_84" )
		end

		channel, data = GetDebugMessage()
	end
	return nil

end


function getMsgBlackKeep()
	local channel, data = GetDebugMessage()
	while channel == 1 do

		if string.find( data , "ī֣֮λӢٻɱ" ) then
			return true
		end

		channel, data = GetDebugMessage()
	end
	return false
end

function isTeamPos()

	local nCount = DataPool:GetTeamMemCount()
	if nCount <= 0 then
		return false
	end
	local nX_C = -1
	local nY_C = -1

	for i = 0 , nCount - 1  do

		local _ , idPlayer ,  _, _ , xCurPos , yCurPos = DataPool:GetTeamMemInfo( i )

		if idPlayer ~= -1 then
			if Character:GetData( idPlayer , "DIE" ) == false then
				if xCurPos ~= -1 then
					if yCurPos ~= -1 then
						nX_C = xCurPos
						nY_C = yCurPos
						break
					end
				end
			end
		end
	end

	if nX_C == -1 or nY_C == -1 then
		return false
	end
	
	for i = 0 , nCount - 1  do
		local szName , idPlayer ,  _, idMap , xCurPos , yCurPos = DataPool:GetTeamMemInfo( i )
		if idPlayer ~= -1 then
			if Character:GetData( idPlayer , "DIE" ) == false then
				if xCurPos ~= -1 then
					if yCurPos ~= -1 then
						if nX_C ~= xCurPos or nY_C ~= yCurPos then
							return false
						end
					end
				end
			end
		end
	end

	return true

end

function isNearMon( szNameMon )

	local n = EnumObject(true)

	if n <= 0 then
		return false
	end

	local xPos , yPos = Player:GetPos()
	if xPos ~= -1 then

		for i = 0 , n - 1 do
			local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
			if tostring( classObj ) == "MON" then
				if tostring( szNameMon ) == tostring( nameObj ) then
					if xObj ~= -1 and yObj ~= -1 then
						if GetDist( xObj , yObj , xPos , yPos ) <= 1 then
							return true
						end
					end
				end
			end
		end
	end

	return false
end






function isCompare1( nMyAngle )
	if nMyAngle > 5 or nMyAngle <= 1 then
		return true
	end
	return false
end

function isCompare2( nMyAngle )
	if nMyAngle > 6 or nMyAngle <= 2 then
		return true
	end
	return false
end

function isCompare3( nMyAngle )
	if nMyAngle > 7 or nMyAngle <= 3 then
		return true
	end

	return false
end

function isCompare4( nMyAngle )
	if nMyAngle <= 4 then
		return true
	end
	return false
end

function isCompare5( nMyAngle )
	if nMyAngle <= 5 then
		return true
	end
	return false
end

function isCompare6( nMyAngle )
	if nMyAngle <= 6 and nMyAngle > 2 then
		return true
	end
	return false
end

function isCompare7( nMyAngle )

	if nMyAngle <= 7 and nMyAngle > 3 then
		return true
	end
	return false
end

function isCompare8( nMyAngle )
	if nMyAngle > 4 then
		return true
	end
	return false
end

function isCompare( nMyAngle , nBossAngle )
	if nBossAngle == 1 then
		return isCompare1( nMyAngle )
	elseif nBossAngle == 2 then
		return isCompare2( nMyAngle )
	elseif nBossAngle == 3 then
		return isCompare3( nMyAngle )
	elseif nBossAngle == 4 then
		return isCompare4( nMyAngle )
	elseif nBossAngle == 5 then
		return isCompare5( nMyAngle )
	elseif nBossAngle == 6 then
		return isCompare6( nMyAngle )
	elseif nBossAngle == 7 then
		return isCompare7( nMyAngle )
	elseif nBossAngle == 8 then
		return isCompare8( nMyAngle )
	end
end

function isMyRun( idBoss )

	local xBoss , yBoss = getTargetPosID( idBoss )
	if xBoss ~= -1 then
		if yBoss ~= -1 then
			local xPos , yPos = Player:GetPos()
			if xPos ~= -1 then
				if yPos ~= -1 then

					g_objCoordinate = { nX , nY , nAngle }

					local nR = GetDist( xBoss , yBoss , g_nX_O , g_nY_O )

					if nR > 17 then
						nR = 17
					end

					setPointCircle( g_nX_O , g_nY_O , 45 , nR )
		
					local nMyMinAngle = getMinAngle( xPos , yPos )

					if nMyMinAngle ~= -1 then

						local nBossMinAngle = getMinAngle( xBoss , yBoss )

						if nBossMinAngle ~= -1 then
							return isCompare( nMyMinAngle , nBossMinAngle )
						end
			
					end
				end
			end
		end
	end
	return false

end




function isInNpc( idBoss , nR )
	local n = EnumObject(true)
	if n == 0 then
		return false
	end
	local xPos , yPos = Player:GetPos()
	local bResult = false
	if xPos ~= -1 then
		local xBoss , yBoss , _ = getTargetPosID( idBoss )
		if xBoss ~= -1 then
			if yBoss ~= -1 then
				for i = 0 , n - 1 do
					local idObj , classObj , nameObj , xObj , yObj , _, modelObj , nAngle = EnumObject( false, i )

					if "NPC" == tostring( classObj ) then
						if "ͨö̬赲" ~= tostring( classObj ) then
--							if 3.14 == tonumber( nAngle ) then
								if xObj ~= -1 then
									if GetDist( xPos , yPos , xObj , yObj ) <= nR then
										bResult = true
									end
								end
--							end
						end
					end
				end
			end
		end
	end
	return bResult
	
end

function exeFightBoss3( monsterID )


	Player:EnableTeamFollow( false )
	offRide()

	AI:SetAI("߼ս") 
	AI:SetParameter( "ͼ" , -1 )
	g_mainguard_r = 17
	AI:SetAdv( "Ȧ" , g_nX_O , g_nY_O , g_mainguard_r ) 
	AI:Start()
	AI:SetAdv( "ָɱ" , -1 , "" )
	g_bYinGuai = 0
	g_bTeamFollow = 0
	g_bTeamAttack = 0

	g_bStopAssistSelf = 0
	g_bStopAssist = 0
	g_bStopSpecialAttack = 0

	g_bYinshen = 0
	g_FollowTarget = -1

	g_bKillMonster = 1
	g_bCallPet = 0
	g_bPickUp = 0


	local bKeepoff = false

	local bIntoNpc = false
	
	local bMsgInto = false

	local bSetObjClock = false
	local idFirstMon = -1
	local xFirst = -1
	local yFirst = -1
	local szIntoNpc = nil

	local bKeepBlack = false


	local bStandPos = false

	local nXStand = -1
	local nYStand = -1
	local bFirst = false

	local idOldSmall = -1
	local bFightSmall = false
	local bGetID = false
	local bCheckSmall = false
	local bGetMsg = false
	local bCheckMsgBlack = false
	local bCheck6s = false
	local bSetPoint = false
	
	local bStandFight = false

	while true do

		System:Sleep( 10 )


		if GetActiveSceneName() ~= tostring( g_szNameSence[ 2 ] ) then
			break
		end

		if isNearXY( 63 ,56 , 5 ) == 1 then
			break
		end

		if Player:GetData( "MP" ) < Player:GetData( "MAXMP" )*0.2 then
			g_bCallPet = 1
		else
			g_bCallPet = 0
		end

		if isManyNpc() == fale and isClassTarget( "SPECIAL" ) == false and getTargetID( "ɱ" ) == -1 then

				if g_nMemPai == 4 then
					setRevival( 40 )
				end

		end

		if Character:GetData( monsterID , "HP_PERCENT" ) <= 20 then
			bGetMsg = true
		end

		if bGetMsg == false then

			local szNameGet = getMsgBossInto()

			if szNameGet ~= nil then
				AI:SetParameter("в" , "üټ" , 1 )
				szIntoNpc = tostring( szNameGet )
			end
		elseif bGetMsg == true then
			if getMsgBlackKeep() == true then
				UpdateMsg( "keepOffBlack" )
				UpdateMsg( "CheckOut" )
				bKeepBlack = true
				bSetPoint = false
				bFirst = false
			end

		end

		if bCheckSmall == false then
			local _ , xBoss , yBoss = getTargetPos( tostring( g_objBoss[ 3 ] ))
	
			if xBoss == 182 then
				if yBoss == 207 then
					if isTeamPos() == true then
						if isNearMon( "ɱ" ) == false then
							bStandPos = true
							bCheckSmall = true

							g_bKillMonster = 0

							g_bStopAssistSelf = 1
							g_bStopAssist = 1
							g_bStopSpecialAttack = 1
						end
					end
	
				end
			end
		else

			if isTargetSpecial( "ڰչʾ" ) == true then
				bCheckSmall = false
			end


		end


		if szIntoNpc ~= nil then



			g_bKillMonster = 0

			g_bStopAssistSelf = 1
			g_bStopAssist = 1
			g_bStopSpecialAttack = 1

			if bFirst == false then
				UpdateMsg( "IntoNpc" )
				bFirst = true
			end

			if bIntoNpc == false then

				if goIntoNpcBoss3( tostring( szIntoNpc ) ) == true then
					bIntoNpc = true
				end

			end

			if IsMsgCold( "IntoNpc" , 5000 ) == true then
				Player:EnableTeamFollow( false )
				g_bKillMonster = 0

				g_bStopAssistSelf = 0
				g_bStopAssist = 0
				g_bStopSpecialAttack = 0

			end

			if IsMsgCold( "IntoNpc" , 11000 ) == true then

				g_bKillMonster = 1

				g_bStopAssistSelf = 0
				g_bStopAssist = 0
				g_bStopSpecialAttack = 0

				DelMsg( "IntoNpc" )

				szIntoNpc = nil
				AI:SetParameter("в" , "üټ" , 0 )
				bIntoNpc = false
			end
		elseif bStandPos == true then

			if bFirst == false then
				local xPos , yPos = Player:GetPos()
				if xPos ~= -1 and yPos ~= -1 then
					nXStand = xPos
					nYStand = yPos
					bFirst = true
				end
			else
				if nXStand ~= -1 and nYStand ~= -1 then
					g_bKillMonster = 0

					g_bStopAssistSelf = 1
					g_bStopAssist = 1
					g_bStopSpecialAttack = 1

					g_mainguard_r = 1
					AI:SetAdv( "Ȧ" , nXStand , nYStand , g_mainguard_r )
				end
			end

			if countMonster( "ɱ" ) > 2 then
				bStandPos = false
				bFirst = false
			end

		elseif getTargetID( "ɱ" ) ~= -1 then

			if idOldSmall == -1 then
				g_mainguard_r = 20
				g_bKillMonster = 0

				g_bStopAssistSelf = 1
				g_bStopAssist = 1
				g_bStopSpecialAttack = 1

			else
				g_mainguard_r = 1
				g_bKillMonster = 1

				g_bStopAssistSelf = 0
				g_bStopAssist = 0
				g_bStopSpecialAttack = 0

			end

			if IsMsgCold( "KillSmall" , 100 ) == true then
				bFightSmall = false
			end

			if bFightSmall == false then
				if bGetID == false then
					local idObj = fightNearestDiff( monsterID )
					if idObj ~= -1 then
						if idOldSmall ~= idObj then
							if idObj ~= -1 then

								idOldSmall = idObj
								g_FollowTarget = idObj
								g_nRFollowTarget = 1
								
								AI:SetAdv( "ָɱ" , -1 , "" )
								AI:SetAdv( "ָɱ" , idObj , "ɱ" )

								bFightSmall = true
								UpdateMsg( "KillSmall" )

								bGetID = true

							end
						end
					end
				end
			end

			if idOldSmall ~= -1 then
				if isLiveID( idOldSmall ) == false or Character:GetData( idOldSmall , "DIE" ) == true then
					bGetID = false
					bFightSmall = false
					continue
				end

				local xMon , yMon , _ = getTargetPosID( idOldSmall )
				if xMon ~= -1 then

					g_FollowTarget = idOldSmall
					g_nRFollowTarget = 1
					g_mainguard_r = 1
					AI:SetAdv( "Ȧ" , xMon , yMon , g_mainguard_r ) 

					if isNearXY( xMon , yMon , 2 ) == 1 then
						g_FollowTarget = -1
						g_bKillMonster = 1
						g_bStopAssistSelf = 0
						g_bStopAssist = 0
						g_bStopSpecialAttack = 0

					end
				end
			end

		elseif bKeepBlack == true then 			

			if isMyRun( monsterID ) == true then

				if bFirst == false then

					g_bKillMonster = 0
					g_bStopAssistSelf = 1
					g_bStopAssist = 1
					g_bStopSpecialAttack = 1

					keepOffBlack()
					bFirst = true
					bCheck6s = false
				else
					if bCheck6s == true then
						if IsMsgCold( "CheckOut" , 12000 ) == true then

	
							g_bKillMonster = 0
							g_bStopAssistSelf = 1
							g_bStopAssist = 1
							g_bStopSpecialAttack = 1

							keepOffBlack()
							bCheckMsgBlack = true
							bSetPoint = false
							UpdateMsg( "CheckOut" )
						end
					end
				end

			else
				if bSetPoint == false then

					if g_nX_Black ~= -1 then
						if g_nY_Black ~= -1 then

							g_mainguard_r = 1

							if isNearXY( g_nX_Black , g_nY_Black , 2 ) == 1 then

								if Player:IsRangeAttack() == true then
									UpdateMsg( "CheckOut" )
									bCheck6s = true
									g_bKillMonster = 1

									g_bStopAssistSelf = 0
									g_bStopAssist = 0
									g_bStopSpecialAttack = 0

									g_mainguard_r = 1

									AI:SetAdv( "Ȧ" , g_nX_Black , g_nY_Black , g_mainguard_r )
									bSetPoint = true
									g_nX_Black = -1
									g_nY_Black = -1
								
									if bCheckMsgBlack == true then
										bStandFight = true
										bKeepBlack = false
										bCheckMsgBlack = false
										DelMsg( "CheckOut" )
									end
								else
									if IsMsgCold( "CheckOut" , 5000 ) == true then
										UpdateMsg( "CheckOut" )
										bCheck6s = true
										g_bKillMonster = 1

										g_bStopAssistSelf = 0
										g_bStopAssist = 0
										g_bStopSpecialAttack = 0

										g_mainguard_r = 1

										AI:SetAdv( "Ȧ" , g_nX_Black , g_nY_Black , g_mainguard_r )
										bSetPoint = true
										g_nX_Black = -1
										g_nY_Black = -1
								
										if bCheckMsgBlack == true then
											bStandFight = true
											bKeepBlack = false
											bCheckMsgBlack = false
											DelMsg( "CheckOut" )
										end

									end
								end

							else	
								g_bKillMonster = 0
								g_bStopAssistSelf = 1
								g_bStopAssist = 1
								g_bStopSpecialAttack = 1
							end
						end
					end
				end
	
			end

		elseif isTargetSpecial( "ڰչʾ" ) == true then 
			
			if IsMsgCold( "KeepOff1" , 100 ) == true then
				bKeepOff = false
			end

			if bKeepOff == false then
				keepOffTargetName( "SPECIAL" , 4 )
				UpdateMsg( "KeepOff1" )
				bKeepOff = true
			end

			if isOutSpecial( 3 ) == true then
				if Player:IsRangeAttack() == true then

					g_bKillMonster = 1

					g_bStopAssistSelf = 0
					g_bStopAssist = 0
					g_bStopSpecialAttack = 0
				else

					if isBossOutSpecial( monsterID , 3 ) == true then

						g_bKillMonster = 1

						g_bStopAssistSelf = 0
						g_bStopAssist = 0
						g_bStopSpecialAttack = 0
						g_mainguard_r = 17
						bKeepOff = true
					end
				end
			else

				g_bKillMonster = 0

				g_bStopAssistSelf = 1
				g_bStopAssist = 1
				g_bStopSpecialAttack = 1
			end
		elseif isTargetSpecial( "" ) == true then

			if IsMsgCold( "KeepOff1" , 100 ) == true then
				bKeepOff = false
			end

			if bKeepOff == false then
				keepOffTargetName( "SPECIAL" , 4 )
				UpdateMsg( "KeepOff1" )
				bKeepOff = true
			end

			if isOutSpecial( 3 ) == true then
				if Player:IsRangeAttack() == true then

					g_bKillMonster = 1

					g_bStopAssistSelf = 0
					g_bStopAssist = 0
					g_bStopSpecialAttack = 0

				end
			else

				g_bKillMonster = 0

				g_bStopAssistSelf = 1
				g_bStopAssist = 1
				g_bStopSpecialAttack = 1
			end

		elseif isTargetSpecial( "貨΢˺" ) == true then

			if IsMsgCold( "KeepOff1" , 500 ) == true then
				bKeepOff = false
			end

			if bKeepOff == false then

				keepOffTargetName( "SPECIAL" , 8 )
				bKeepOff = true
				UpdateMsg( "KeepOff1" )
			end

			if isOutSpecial( 8 ) == true then
				if Player:IsRangeAttack() == true then

					g_bKillMonster = 1

					g_bStopAssistSelf = 0
					g_bStopAssist = 0
					g_bStopSpecialAttack = 0
				else

					if isBossOutSpecial( monsterID , 6 ) == true then

						g_bKillMonster = 1

						g_bStopAssistSelf = 0
						g_bStopAssist = 0
						g_bStopSpecialAttack = 0
						g_mainguard_r = 17
					end
				end
			else

				g_bKillMonster = 0

				g_bStopAssistSelf = 1
				g_bStopAssist = 1
				g_bStopSpecialAttack = 1
			end


		else
				AI:SetAdv( "ָɱ" , -1 , "" )
				bStandPos = false
				bKeepOff = false
				bSetObjClock = false
				bFirst = false

				g_FollowTarget = -1

				g_bKillMonster = 1

				g_bStopAssistSelf = 0
				g_bStopAssist = 0
				g_bStopSpecialAttack = 0

				if bStandFight == true then 
					g_mainguard_r = 1
				else
					AI:SetAdv( "Ȧ" , g_nX_O , g_nY_O , g_mainguard_r ) 
					g_mainguard_r = 17
				end
		end


		if Character:GetData( monsterID , "DIE" ) == true then			
			clearMsg()

			if DataPool:IsTeamLeader() == true then
				mySleep( 2 )
			end

			break
		end

		if g_nNoFight == 1 then
			g_bKillMonster = 0
			g_bYinshen = 1
			g_bStopAssist = 1
			g_mainguard_r = 20
			AI:SetAdv( "Ȧ" , g_nX_O , g_nY_O , g_mainguard_r ) 
		end

		AI:SetParameter("в" , "" , 1 ) 
		AI:SetParameter("ʰȡ" , g_bPickUp ) 
		AI:SetParameter("в" , "ɱ" , g_bKillMonster )
		AI:SetParameter("в" , "" , 0 )
		AI:SetParameter("в" , "Ӹ" , 0 )
		AI:SetParameter("в" , "" , g_bTeamAttack )
		AI:SetParameter("в" , "ֹܸͣ" , g_bStopAssistSelf )
		AI:SetParameter("в" , "ֹͣ" , g_bStopAssist )
		AI:SetParameter("в" , "ֹͣ⹥" , g_bStopSpecialAttack )
		AI:SetParameter("в" , "ٻ" , g_bCallPet ) 
		AI:SetAdv("" , g_FollowTarget , g_nRFollowTarget ) 
		AI:SetAdv("Ȧ뾶" , g_mainguard_r ) 
		AI:SetAdv("" , g_bYinshen ) 

	end
	AI:Stop()
end




function getNameMsgBoss3( szMsg )

	local i , nStart = string.find( tostring( szMsg ) , "LHFD_160203_92" )
	nStart = nStart + 6

	local nEnd = string.find( tostring( szMsg ) , "*" , nStart )
	nEnd = nEnd - 1

	local szName = string.sub( tostring( szMsg ) , tonumber( nStart ) , tonumber( nEnd ) )

	

	return tostring( szName )

end


function getMsgNameKeepOff()
	local channel, data = GetDebugMessage()
	while channel == 1 do
		if string.find( data , "LHFD_160203_92" ) then
			
			local szName = getNameMsgBoss3( tostring( data ) )
			if szName ~= nil then
				return tostring( szName )
			end
		end
		channel, data = GetDebugMessage()
	end
	return nil
end



local g_objFriend = { nPos , nX , nY }
local g_objFriendID = { nID , nX , nY }

function setListFriend()
	g_objFriend = { nPos , nX , nY }
	local nCount = DataPool:GetTeamMemCount()
	if nCount <= 0  then
		return false
	end
	local nIndex = 1
	for i = 0 , nCount - 1  do
		local szName , idPlayer ,  _, idMap , xCurPos , yCurPos = DataPool:GetTeamMemInfo( i )
		if szName ~= Player:GetName() and szName ~= nil then
			g_objFriend[ nIndex ] = { nPos = i , nX = xCurPos , nY = yCurPos }
			nIndex = nIndex + 1 
		end
	end
	return true
end

function setListObjFriend()
	g_objFriendID = { nID , nX , nY }
	local nCount = DataPool:GetTeamMemCount()
	if nCount <= 0  then
		return false
	end
	local nIndex = 1
	for i = 0 , nCount - 1  do
		local szName , idPlayer ,  _, idMap , xCurPos , yCurPos = DataPool:GetTeamMemInfo( i )
		if szName ~= Player:GetName() and szName ~= nil then
			if idPlayer ~= -1 then
				g_objFriendID[ nIndex ] = { nID = idPlayer , nX = xCurPos , nY = yCurPos }
				nIndex = nIndex + 1 
			end
		end
	end
	return true
end

function isAllFriendBuffName( szNameBuff )

	setListObjFriend()

	local nNumMember = table.getn( g_objFriendID )
	if nNumMember <= 0 then
		return false
	end

	for i = 1 , nNumMember do
		if tonumber( g_objFriendID[ i ].nID ) ~= -1 and tonumber( g_objFriendID[ i ].nID ) ~= nil then

			if CheckBufferByName( tonumber( g_objFriendID[ i ].nID ) , tostring( szNameBuff ) ) ~= -1 then
				return false
			end
		end
	end
	return true
end

function getMinDistToFriend( nX , nY )
	
	local nNumMember = table.getn( g_objFriend )
	if nNumMember <= 0 then
		return -1
	end
	
	local nMinDist = GetDist( g_objFriend[ 1 ].nX , g_objFriend[ 1 ].nY , nX , nY )

	for i = 2 , nNumMember do
		local nDist = GetDist( g_objFriend[ i ].nX , g_objFriend[ i ].nY , nX , nY )
		if nDist < nMinDist then			
			nMinDist = nDist
		end
	end

	return nMinDist
end

function setDistMinPoint()

	local nNumMember = table.getn( g_objCoordinate )
	if nNumMember <= 0 then
		return false
	end
	for i = 1 , nNumMember do
		g_objCoordinate[ i ].nAngle =  getMinDistToFriend( g_objCoordinate[ i ].nX , g_objCoordinate[ i ].nY )
	end
	return true
end


function getFarPoint()

	local nNumMember = table.getn( g_objCoordinate )

	if  nNumMember <= 0 then
		return -1 , -1
	end

	local nIndex = -1

	local nMaxDist = g_objCoordinate[ 1 ].nAngle
	nIndex = 1

	for i = 2 , nNumMember do
		local nDist = g_objCoordinate[ i ].nAngle
		if nDist > nMaxDist then			
			nMaxDist = nDist
			nIndex = i
		end
	end

	return tonumber( g_objCoordinate[ nIndex ].nX ) , tonumber(g_objCoordinate[ nIndex ].nY )
end

function getFarPointToFriend()
	if setDistMinPoint() == true then
		local x , y = getFarPoint()
		return x , y
	end
	return -1 , -1
end



function keepOffFriendCircle( nR )
	setListFriend()
	g_objCoordinate = { nX , nY , nAngle }
	setPointCircle( g_nX_O , g_nY_O , 45 , nR )
	local xKeep , yKeep = getFarPointToFriend()
	if xKeep ~= -1 and yKeep ~= -1 then
		g_bKillMonster = 0

		g_bStopAssistSelf = 1
		g_bStopAssist = 1
		g_bStopSpecialAttack = 1


		g_mainguard_r = 1
		AI:SetAdv( "Ȧ" , xKeep , yKeep , g_mainguard_r )
		return true
	end
	return false
end



function keepOffFriend( szName )

	if tostring( szName ) == Player:GetName() then

		g_bKillMonster = 0
		g_bStopAssistSelf = 1
		g_bStopAssist = 1
		g_bStopSpecialAttack = 1

		return keepOffFriendCircle( 17 )

	else
		if keepOffTargetName( tostring( szName ) , 12 ) == true then
			return true
		end

	end
	return false
end


function isViabilityPos( nX , nY )	
	for i = 1 , table.getn( g_objSpecial ) do
		if g_objSpecial[ i ].nX == nX and g_objSpecial[ i ].nY == nY then
			return true
		end
	end
	return false
end

local g_objBomb = { nX , nY }

function setListPointBomb()
	if setListSpecial( nil ) == true then

		local nNum = table.getn( g_objSpecial )

		if nNum <= 0 then
			return false
		end


		local nY0 = 160
		local nX0 = 62

		g_objBomb = { nX , nY }

		local nIndex = 1
		

		for i = 1 , 9 do
			nX0 = 62
			for j = 1 , 9 do
				if isViabilityPos( nX0 , nY0 ) == false then
					g_objBomb[ nIndex ] = { nX = nX0 , nY = nY0 }
					
					nIndex = nIndex + 1

				end
				nX0 = nX0 + 5
			end
			nY0 = nY0 + 5
		end
		return true
	end
	return false
end

function getPointGoodBomb( nX , nY )

	local nNum = table.getn( g_objBomb )

	if nNum <= 0 then
		return -1 , -1
	end

	local nIndex = 1
	local nMinDist = GetDist( g_objBomb[ 1 ].nX , g_objBomb[ 1 ].nY , nX , nY )

	for i = 2 , nNum do

		local nDist = GetDist( g_objBomb[ i ].nX , g_objBomb[ i ].nY , nX , nY )

		if nDist < nMinDist then			
			nMinDist = nDist
			nIndex = i
		end
	end

	return tonumber( g_objBomb[ nIndex ].nX ) , tonumber( g_objBomb[ nIndex ].nY )
	
end

function keepOffBomb()

	if setListPointBomb() == true then
		local xPos , yPos = Player:GetPos()
		if xPos ~= -1 then
			local xKeep , yKeep = getPointGoodBomb( xPos , yPos )
			if xKeep ~= -1 and yKeep ~= -1 then

				g_mainguard_r = 1
				g_nX_K4 = xKeep
				g_nY_K4 = yKeep
				AI:SetAdv( "Ȧ" , xKeep , yKeep , g_mainguard_r )
				return true
			end
		end 
	end
	return false
end

function exeFightBoss4( monsterID )

	Player:EnableTeamFollow( false )
	offRide()

	AI:SetAI("߼ս") 
	AI:SetParameter( "ͼ" , -1 )

	g_mainguard_r = 17

	AI:SetAdv( "Ȧ" , g_nX_O , g_nY_O , g_mainguard_r )

	AI:Start()
	AI:SetAdv( "ָɱ" , -1 , "" )
	g_bYinGuai = 0
	g_bTeamFollow = 0
	g_bTeamAttack = 0

	g_bStopAssistSelf = 0
	g_bStopAssist = 0
	g_bStopSpecialAttack = 0

	g_bYinshen = 0
	g_FollowTarget = -1

	g_bKillMonster = 1
	g_bCallPet = 0
	g_bPickUp = 0

	local szNameKeepOff = nil

	local bKeepoff = false
	local bFirst = false


	while true do

		System:Sleep( 10 )

		if GetActiveSceneName() ~= tostring( g_szNameSence[ 2 ] ) then
			break
		end


		if isNearXY( 63 ,56 , 5 ) == 1 then
			break
		end

		if Player:GetData( "MP" ) < Player:GetData( "MAXMP" )*0.2 then
			g_bCallPet = 1
		else
			g_bCallPet = 0
		end
		
		local szName = getMsgNameKeepOff()
		if szName ~= nil then
			bFirst = false
			szNameKeepOff = szName
		end

		if Player:GetBuffByName( "ڤ" , 1 ) ~= -1 or Player:GetBuffByName( "ڤ" , 1 ) ~= -1 then

			g_bKillMonster = 0
			g_bStopAssistSelf = 1
			g_bStopAssist = 1
			g_bStopSpecialAttack = 1

		elseif isClassTarget( "SPECIAL" ) == true then

			if isTargetSpecial( "ֺ" ) == true or isTargetSpecial( "ְ" ) == true then

				g_bKillMonster = 0
				g_bStopAssistSelf = 1
				g_bStopAssist = 1
				g_bStopSpecialAttack = 1

				if bFirst == false then
					UpdateMsg( "keepOffBomb" )
					bFirst = true
				end

				if IsMsgCold( "keepOffBomb" , 500 ) == true then
					bKeepOff = false
				end

				if bKeepOff == false then

					keepOffBomb()
					bKeepOff = true
				end
--day
				if Player:IsRangeAttack() == true then
					local xPos , yPos = Player:GetPos()
					if xPos ~= -1 then
						if g_nX_K4 ~= -1 and g_nY_K4 ~= -1 then
							if GetDist( xPos , yPos , g_nX_K4 , g_nY_K4 ) <= 1 then
								g_bKillMonster = 1
								g_bStopAssistSelf = 0
								g_bStopAssist = 0
								g_bStopSpecialAttack = 0
								g_mainguard_r = 1
							end
						end
					end
				end
			else

				g_bKillMonster = 1
				g_bStopAssistSelf = 0
				g_bStopAssist = 0
				g_bStopSpecialAttack = 0

				if bKeepOff == false then

					setKeepOffSpecial( 6 )					
					bKeepOff = true
				end

				g_mainguard_r = 17
				AI:SetAdv( "Ȧ" , g_nX_O , g_nY_O , g_mainguard_r )

			end
	
		elseif szNameKeepOff ~= nil then
			
			if bFirst == false then
				UpdateMsg( "keepOffFriend" )
				bFirst = true
			end

			if IsMsgCold( "keepOffFriend" , 500 ) == true then
				bKeepOff = false
			end
			if bKeepOff == false then
				keepOffFriend( tostring( szNameKeepOff ) )
				bKeepOff = true
			end

			if IsMsgCold( "keepOffFriend" , 5000 ) == true then
				g_bKillMonster = 1
				g_bStopAssistSelf = 0
				g_bStopAssist = 0
				g_bStopSpecialAttack = 0
				szNameKeepOff = nil
				DelMsg( "keepOffFriend" )
			end
		else
			g_bKillMonster = 1
			g_bStopAssistSelf = 0
			g_bStopAssist = 0
			g_bStopSpecialAttack = 0

			g_nX_K4 = -1
			g_nY_K4 = -1

			AI:SetAdv("в" )
			bKeepOff = false
			bFirst = false

			g_mainguard_r = 17
			AI:SetAdv( "Ȧ" , g_nX_O , g_nY_O , g_mainguard_r )	


				if g_nMemPai == 4 then
					setRevival( 40 )
				end

		end

		if Character:GetData( monsterID , "DIE" ) == true then
			G_nFinish = 1
			break
		end

		if g_nNoFight == 1 then
			g_bKillMonster = 0
			g_bYinshen = 1
			g_bStopAssist = 1
			g_mainguard_r = 20
			AI:SetAdv( "Ȧ" , g_nX_O , g_nY_O , g_mainguard_r ) 
		end

		AI:SetParameter("в" , "" , 1 ) 
		AI:SetParameter("ʰȡ" , g_bPickUp ) 
		AI:SetParameter("в" , "ɱ" , g_bKillMonster )
		AI:SetParameter("в" , "" , 0 )
		AI:SetParameter("в" , "Ӹ" , 0 )
		AI:SetParameter("в" , "" , g_bTeamAttack )
		AI:SetParameter("в" , "ֹܸͣ" , g_bStopAssistSelf )
		AI:SetParameter("в" , "ֹͣ" , g_bStopAssist )
		AI:SetParameter("в" , "ֹͣ⹥" , g_bStopSpecialAttack )
		AI:SetParameter("в" , "ٻ" , g_bCallPet ) 
		AI:SetAdv("" , g_FollowTarget , g_nRFollowTarget ) 
		AI:SetAdv("Ȧ뾶" , g_mainguard_r ) 
		AI:SetAdv("" , g_bYinshen ) 

	end
	AI:Stop()
end


local g_bFirst = false
function isConditionTimes( szNameMission )

	if g_bFirst == false then
		g_bFirst = true
	end
	while true do

		DataPool:UpdateActivityList(3)
		mySleep( 1 )
		local nResult , sName,_,_, play_times, play_maxtimes = DataPool:FindActiveMission( szNameMission )
		if nResult == 1 then
			if play_times >= play_maxtimes then
				return 0
			else
				return 1
			end
		end
	end
	return -1
end

function isFullBag( nID )
	
	local nResult = PlayerPackage:GetEmptySlot( nID )

	if nResult == 0 then
		return true
	end

	return false
end

function isNearXY( nX , nY , nR )
	local xPos , yPos = Player:GetPos()
	if xPos ~= -1 then
		if GetDist( xPos , yPos , nX , nY ) <= nR then
			return 1
		else
			return 0
		end
	end
	return -1
end

local g_bSeeNpc = false
function exeStep1()

	System:Sleep( 500 )
	if isMonster( tostring( g_objBoss[ 1 ] ) ) == false then
		if isNearXY( g_nX_O , g_nY_O , 20 ) == 0 then
			onRide()
			if DataPool:IsTeamLeader() then
				Player:EnableTeamFollow( true )
			end
			MoveTo( g_nX_O , g_nY_O , -1 , "" , 0 , true , _callbackInSenceFollow )
		else
			MoveTo( g_nX_O , g_nY_O , -1 , "" , 0 , true , _callbackInSenceFollow )
			offRide()
		end
	end

	if getTargetID( tostring( g_objBoss[ 1 ] ) ) ~= -1 then
		g_bSeeNpc = true
	end

	if g_bSeeNpc == false then
		if getTargetID( tostring( g_objBoss[ 1 ] ) ) == -1 then
			G_nStep = 2
			onRide()
			return false
		end
	end
	
	if isMonster( tostring( g_objBoss[ 1 ] ) ) == false then

		if DataPool:IsTeamLeader() then
			teamleaderSpeakBoss1()
		else

			if g_nMemPai == 4 then
				buffHp()
			end
			
		end
	else
		Player:EnableTeamFollow( false )
		local monsterID = getTargetID( tostring( g_objBoss[ 1 ] ) )
		if monsterID ~= -1 then
			g_bSeeNpc = false
			if Character:GetData( monsterID , "DIE" ) == false then 
				exeFightBoss1( monsterID )
			elseif Character:GetData( monsterID , "DIE" ) == true then 
				OnSweepBattleField( 1 )
				G_nStep = 2
			end
		end
	end
end

function exeStep2()

	if isNearXY( 63 ,56 , 10 ) == 1 then
		G_bCenter = 0
	end

	if isMonster( tostring( g_objBoss[ 2 ] ) ) == false then
		if isNearXY( g_nX_O , g_nY_O , 20 ) == 0 then
			onRide()
		end
		if DataPool:IsTeamLeader() then
--			Player:EnableTeamFollow( true )
			MoveTo( g_nX_O , g_nY_O , -1 , "" , 0 , true , _callbackInSence )
		else
			MoveTo( g_nX_O , g_nY_O , -1 , "" , 0 , true , _callbackInSence )
			offRide()
		end
	end

	if getTargetID( tostring( g_objBoss[ 2 ] ) ) ~= -1 then
		g_bSeeNpc = true
	end

	if g_bSeeNpc == false then
		if getTargetID( tostring( g_objBoss[ 2 ] ) ) == -1 then
			G_nStep = 3
			onRide()
			return false
		end
	end


	if isMonster( tostring( g_objBoss[ 2 ] ) ) == false then
	
		if DataPool:IsTeamLeader() then
			teamleaderSpeakBoss2()
		else

			if g_nMemPai == 4 then
				buffHp()
			end
			
		end
	else
		Player:EnableTeamFollow( false )
		local monsterID = getTargetID( tostring( g_objBoss[ 2 ] ) )
		if monsterID ~= -1 then
			g_bSeeNpc = false
			if Character:GetData( monsterID , "DIE" ) == false then 
				G_bFinish = 0
				exeFightBoss2( monsterID )
			elseif Character:GetData( monsterID , "DIE" ) == true then 
				OnSweepBattleField( 1 )
				G_nStep = 3
			end
		end
	end

end

function exeStep3()

	if isMonster( tostring( g_objBoss[ 3 ] ) ) == false then
		if isNearXY( g_nX_O , g_nY_O , 20 ) == 0 then
			onRide()
		end
		if DataPool:IsTeamLeader() == true then
--			Player:EnableTeamFollow( true )
			if isNearXY( g_nX_O , g_nY_O , 15 ) == 0 then
				MoveTo( 158 , 199 , -1 , "" , 0 , true , _callbackInSence )
			end
			MoveTo( g_nX_O , g_nY_O , -1 , "" , 0 , true , _callbackInSence )
		else
			MoveTo( g_nX_O , g_nY_O , -1 , "" , 0 , true , _callbackInSence )
			offRide()
		end
	end

	if getTargetID( tostring( g_objBoss[ 3 ] ) ) ~= -1 then
		g_bSeeNpc = true
	end

	if g_bSeeNpc == false then
		if getTargetID( tostring( g_objBoss[ 3 ] ) ) == -1 then
			G_nStep = 4
			onRide()
			return false
		end
	end

	if isMonster( tostring( g_objBoss[ 3 ] ) ) == false then

		if DataPool:IsTeamLeader() then
			teamleaderSpeakBoss3()
		else

			if g_nMemPai == 4 then
				buffHp()
			end
			
		end
	else
		Player:EnableTeamFollow( false )
		local monsterID = getTargetID( tostring( g_objBoss[ 3 ] ) )
		if monsterID ~= -1 then
			g_bSeeNpc = false
			if Character:GetData( monsterID , "DIE" ) == false then 
				exeFightBoss3( monsterID )
			elseif Character:GetData( monsterID , "DIE" ) == true then 
				OnSweepBattleField( 1 )
				G_nStep = 4
			end
		end
	end
end


function exeStep4()

	if isMonster( tostring( g_objBoss[ 4 ] ) ) == false then
		if isNearXY( g_nX_O , g_nY_O , 20 ) == 0 then
			onRide()
		end
		if DataPool:IsTeamLeader() then
--			Player:EnableTeamFollow( true )
			MoveTo( g_nX_O , g_nY_O , -1 , "" , 0 , true , _callbackInSence )
		else
			MoveTo( g_nX_O , g_nY_O , -1 , "" , 0 , true , _callbackInSence )
			offRide()
		end
	end

	if isMonster( tostring( g_objBoss[ 4 ] ) ) == false then

		if DataPool:IsTeamLeader() then
			teamleaderSpeakBoss4()
		else

			if g_nMemPai == 4 then
				buffHp()
			end
			
		end
	else
		Player:EnableTeamFollow( false )
		local monsterID = getTargetID( tostring( g_objBoss[ 4 ] ) )
		if monsterID ~= -1 then
			if Character:GetData( monsterID , "DIE" ) == false then 
				G_bFinish = 0
				exeFightBoss4( monsterID )
			else
				OnSweepBattleField( 1 )
				G_bFinish = 1
				G_nStep = 4
			end
		end
	end
end

function getCurrentBoss()
	for i = 1 , table.getn( g_objBoss ) do
		local idObj = getTargetID( tostring( g_objBoss[ i ]) )
		if idObj ~= -1 and Character:GetData( idObj , "DIE" ) == false then
			return i			
		end
	end
	return -1
end

function _callbackOutSence( fX , fY , nScene )

	if GetActiveSceneName() == tostring( g_szNameSence[ 2 ] ) then
		return false
	end
	return true
end
function speakOutSence()
	local NpcID , x , y = getTargetPos( "" )
		if NpcID ~= -1 then
			MoveToNPC_ById( x , y , -1 , NpcID , 0 , _callbackInSence )
			QuestFrameOptionClicked( "뿪ŋָ" , CMP_PART_MATCH )

			System:Sleep( 500 )
			EnterNewMap()
		end
		QuestFrameMissionClose()
		System:Sleep( 1000 )
end

local g_bOnPikup = false

function _SMain(...)


	if Player:GetData( "LEVEL" ) < 90 then
		ShowMessage( "ȼ>= 90 " )
		System:Sleep( 1000 )
		Player:EnableTeamFollow( false )
		return
	end

	local NpcID , _ , _ = getTargetPos( tostring( g_szNpcMission ) )
	if GetActiveSceneName() ~= tostring( g_szNameSence[ 2 ] ) and DataPool:GetTeamMemCount() < 3 and NpcID == -1 then

		onRide()

		ShowMessage( " 3" )
		System:Sleep( 1000 )
		if isFullBag( 0 ) == true then
			ShowMessage( "" )
			mySleep( 2 )
		end
	end

	g_nMemPai = Player:GetData( "MEMPAI" )

	g_nNoFight = GetScriptParam(script_name,"attr", SCRIPT_ATTR_NOFIGHT ) 

	if g_nNoFight == 1 then
		ShowMessage( "Ѿò֣ " )
	end

	LogChatMsg( 0 )
	LogChatMsg( 1 )
	while true do

		System:Sleep( 100 )

		local szCurrentScene = GetActiveSceneName()

		if szCurrentScene ~= tostring( g_szNameSence[ 2 ] ) and GetActiveSceneName() ~= nil then

			G_bRevival = 0
			G_bCenter = 0


			G_bRevival = 0
			G_nStep = 1
			G_bFinish = 0
			G_bCenter = 0



			if GetQuestTimesInfo( "ŋָ(ͨ)" , 5000 ) == 0 and GetActiveSceneName() ~= tostring( g_szNameSence[ 2 ] ) then 

				Player:EnableTeamFollow( false )
				ShowMessage( "Ѿ" )
				System:Sleep( 1000 )
				return
			end


			MoveTo( 286 , 95 , 0x02 , "" , 0 , true , _callbackOutSence )

			if DataPool:GetTeamMemCount() < 3 and GetActiveSceneName() == tostring( g_szNameSence[ 1 ] ) then 
				ShowMessage(" 3 ")
				Player:EnableTeamFollow( false )
				System:UpdateRecord( "ŋָأͨ" )
				return
			end
			UpdateMsg( "WaitTeam" )
			while true do

				if GetActiveSceneName() == tostring( g_szNameSence[ 2 ] ) then
					break
				else
					if isConditionTeam() == true then
						break
					end

				end
				System:Sleep( 1000 )
			end

			if DataPool:IsTeamLeader() and GetActiveSceneName() == tostring( g_szNameSence[ 1 ] ) then
				onRide()
				Player:EnableTeamFollow( true )
				if speakNpcMission() == 0 then
					Player:EnableTeamFollow( false )
					return
				end
			end	

		else

			if G_bOut == 0 then
				clearMsg()
				G_bOut = 1
			end
			
			local nCurrent = getCurrentBoss()

			if nCurrent ~= -1 then
				G_nStep = nCurrent
			end

			if G_bFinish == 1 or getTargetID( "" ) ~= -1 and isNearXY( 82 , 181 , 25 ) == 1 then

				OnSweepBattleField( 1 )
				speakOutSence()
				continue
			end

			if G_nStep == 1 then
				g_nX_O = 140
				g_nY_O = 62
				g_nFollow = 1

				UpdateMsg( "CallFollowMe" )

				exeStep1()

			elseif G_nStep == 2 then
				g_nX_O = 212
				g_nY_O = 119
				g_nFollow = 2

				UpdateMsg( "CallFollowMe" )

				exeStep2()

			elseif G_nStep == 3 then
				
				g_nFollow = 3
				g_nX_O = 182
				g_nY_O = 207

				UpdateMsg( "CallFollowMe" )

				exeStep3()
			elseif G_nStep == 4 then
				g_nX_O = 82
				g_nY_O = 181
				g_nFollow = 4

				UpdateMsg( "CallFollowMe" )

				exeStep4()
			end

		end
	end

	LogChatMsg( -1 )
end

_SMain()
