
--[[


[author]
class=1
attr=1
descript = 񸱱ÿܽһ  

]]--

script_name = "þ"


local g_szNameSence = {
	[1] = "ɽ" ,
	[2] = "þ"
}

local g_objBoss = {

	[1] = { szName1 = "Ѻ" , szName2 = "Ѻħ" },
	[2] = { szName1 = "" , szName2 = "" },
	[3] = { szName1 = "ƻ" , szName2 = "ɻ껨" }
}


local g_objBossSmall = {

	[1] = "" ,
	[2] = ""
}




local g_szNpcMission = "ö"

local g_nMemPai = -1
local g_nNoFight = 0

if G_bRevival == nil then
	G_bRevival = 0
end

if G_nStep == nil then
	G_nStep = 1
end

if G_bChangeName == nil then
	G_bChangeName = 1
end

if G_bOut == nil then
	G_bOut = 0
end


if G_bMsgFun == nil then
	G_bMsgFun = 0
end
if G_bMsgLine == nil then
	G_bMsgLine = 0
end


if G_bGetItems == nil then
	G_bGetItems = 0
end



if G_bFinish == nil then
	G_bFinish = 0
end



if G_nX_Npc == nil then
	G_nX_Npc = -1
end

if G_nY_Npc == nil then
	G_nY_Npc = -1
end

if G_nID_Npc == nil then
	G_nID_Npc = -1
end


local g_nMaxR1 = 17



local g_nX_O = -1
local g_nY_O = -1



local g_nX_Keep = -1
local g_nY_Kepp = -1


local g_szPoison1 = "Ĺ" -- ܵߵӰ"
local g_szPoison2 = "ƶ"

local g_szNpcGetBoss = ""

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_bPickUp = 0

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

		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 isQMember()
	local nCount = DataPool:GetTeamMemCount()
	if nCount < 3 then
		return false
	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
			return true
		end
	end

	return false
end

function mySleep( nSleep )
	local nCount = 1
	while true do
		if isQMember() == false then
			break
		end
		if nCount > nSleep then
			break
		end
		nCount = nCount + 1
		System:Sleep( 1000 )
	end
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 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( "ټϣ׼븱" , 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 Player:GetData( "MEMPAI" ) == 4 then
		if isTeamHpLow( 80 ) or ( Player:GetData( "HP" ) < Player:GetData( "MAXHP" )*0.8 )  then
			buffMySeltAndFriend( 0 )				
		end
	end			
end


function buffMySeltAndFriend( nFlag )

	Player:EnableTeamFollow( false )
	offRide()
	AI:SetAI("߼ս") 
	AI:Start()
	local xPos , yPos = Player:GetPos()
	g_mainguard_r = 25
	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 )

		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 _callbackInSence( fX , fY , nScene )

	if GetActiveSceneName() ~= tostring( g_szNameSence[ 2 ] ) then
		return false
	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 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 isOutSpecial( nR )
	local n = EnumObject(true)
	if n == 0 then
		return false
	end
	local xPos , yPos = Player:GetPos()
	local bResult = true
	if xPos ~= -1 then
	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
	end
	return bResult
	
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 setKeepOffSpecial( nR )
	local n = EnumObject(true)
	if n == 0 then
		return false
	end
	local bResult = false
	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 ) )
			bResult = true
		end
	end
	return bResult
	
end

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

		if "SPECIAL" == tostring( classObj ) then
			if xPre ~= xObj and yPre ~= yObj then
				xPre = xObj
				yPre = yObj
				AI:SetAdv("в" , idObj , nR , tostring( nameObj ) )
			end
		end
	end
	if xPre ~= -1 then
		return true
	end
	return false
	
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 , nAngle = 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 getSpecialBoss1()

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

		if channel == -1 then
			break
		end
		if string.find( data , "npcpaopao" ) then
			if string.find( data , "694" , 16 ) then				
				return true
			end
		end
	end
	return false
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()

	clearMsg()

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

	g_mainguard_r = 20

	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_bKillMonster = 1
	g_bStopAssistSelf = 0
	g_bStopAssist = 0
	g_bStopSpecialAttack = 0

	g_bYinshen = 0
	g_FollowTarget = -1

	g_bCallPet = 0
	g_bPickUp = 0

	local bKeepOffSmall = false
	local bKeepoff = false
	local bContinue = false
	local bFirst = false
	local g_bMsgCircle = false
	local bStand = false

	while true do

		System:Sleep( 10 )

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

		if G_bChangeName == 0 then
			local idObjChange = getTargetID( tostring( g_objBoss[ 1 ].szName2 ) )
			if idObjChange ~= -1 then
				monsterID = idObjChange
				G_bChangeName = 1
			end
		end

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

		if getSpecialBoss1() == true then
			AI:SetParameter("в" , "üټ" , 1 )
			bFirst = false
			g_bMsgCircle = true
			UpdateMsg( "TheBig" )

		elseif g_bMsgCircle == true then			

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

			if IsMsgCold( "TheBig" , 500 ) == true then
				bContinue = false
			end
			
			if bContinue == false then

				keepOffTargetID( monsterID , 12 )

				bContinue = true
			end

			if IsMsgCold( "TheBig" , 6000 ) == true then

				bContinue = false
				g_bMsgCircle = false

				DelMsg( "TheBig" )

				g_nX_Keep = -1
				g_nY_Keep = -1

			end


		elseif isClassTarget( "SPECIAL" ) == true then

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

			if bKeepOff == false then

				if isTargetSpecial( "̹Ǻ˪" ) == true or isTargetSpecial( "籩" ) == true then

					keepOffTargetName( "SPECIAL" , 8 )
					bKeepOff = true

				elseif isTargetSpecial( "ѩ" ) == true then					

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

					keepOffTargetName( "SPECIAL" , 4 )
					bKeepOff = true
				end
			end

			if isTargetSpecial( "̹Ǻ˪" ) == true then
	
				if isOutSpecial( 5 ) == true then
					if Player:IsRangeAttack() == true then

						g_bKillMonster = 1
	
						g_bStopAssistSelf = 0
						g_bStopAssist = 0
						g_bStopSpecialAttack = 0
					else
						if isBossOutSpecial( monsterID , 4 ) == 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

			elseif isTargetSpecial( "籩" ) == true then

				if isOutSpecial( 8 ) == 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

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

			else
				g_bKillMonster = 1

				g_bStopAssistSelf = 0
				g_bStopAssist = 0
				g_bStopSpecialAttack = 0
	
				g_mainguard_r = 20
				AI:SetAdv( "Ȧ" , g_nX_O , g_nY_O , g_mainguard_r )
			end

		else
				bKeepOff = false
				bFirst = false
				bStand = false
				g_bMsgCircle = false

				g_bKillMonster = 1

				g_bStopAssistSelf = 0
				g_bStopAssist = 0
				g_bStopSpecialAttack = 0
	
				g_mainguard_r = 20
				AI:SetAdv( "Ȧ" , g_nX_O , g_nY_O , g_mainguard_r )

				g_nX_Keep = -1
				g_nY_Keep = -1

				if gscript_check( 0 , 0 , 1 ) == 1 then
					if g_nMemPai == 4 then
						setRevival( 40 )
					end

				end
				AI:SetParameter("в" , "üټ" , 0 )
		end

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

		if g_nNoFight == 1 then
			g_bKillMonster = 0
			g_bYinshen = 1
			g_bStopAssist = 1
			g_bCallPet = 0
		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 , 6 ) 
		AI:SetAdv("Ȧ뾶" , g_mainguard_r ) 
		AI:SetAdv("" , g_bYinshen ) 

	end
	AI:Stop()
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 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_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" and tostring( szNameObj ) == nameObj then
				g_objSpecial[ nIndex ] = { nX = xObj , nY = yObj }
				nIndex = nIndex + 1
			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
				g_objSpecial[ nIndex ] = { nX = xObj , nY = yObj }
				nIndex = nIndex + 1
			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()


	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 ) == 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 , 3 ) == 0 then
		setListSpecial( tostring( szNameCircle ) )
		g_objCoordinate = { nX , nY , nAngle }
		setPointCircle( xPos , yPos , 45 , 6 )
		setListPointOut()
		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 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 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
						g_objMonster[ nIndex ] = { nID = idObj , x = xObj , y = yObj }
						nIndex = nIndex + 1
					end
				end
			end
		end
	end

	return true

end

function getNearest( nX , nY , szNameMonster )

	setObjShort( tostring( szNameMonster ) )

	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 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

local idFightPre = -1

function fightNearest( szNameMonster )
	local xPos , yPos = Player:GetPos()

	local idFight = getNearest( xPos , yPos , tostring( szNameMonster ) )
	if idFight ~= -1 then
		AI:SetAdv( "ָɱ" , -1 , "" )
		AI:SetAdv( "ָɱ" , idFight , tostring( szNameMonster ) )
	end
end



function keepOffNearestDiff( idBoss )
	local xPos , yPos = Player:GetPos()
	local idFight = getNearestID( xPos , yPos , idBoss )
	if idFight ~= -1 then

		keepOffTargetID( idFight , 10 )
	end
end

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

	local idFight = getNearestID( xPos , yPos , idBoss )
	local _ , _ , nameObj = getTargetPosID( idFight )
	if idFight ~= -1 then
		if nameObj ~= nil then
			AI:SetAdv( "ָɱ" , -1 , "" )
			AI:SetAdv( "ָɱ" , idFight , tostring( nameObj ) )
		end
	end
end


local g_objFriend = { nPos , 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 getMinDistToFriend( nX , nY )
	
	local nNumMember = table.getn( g_objFriend )
	if nNumMember <= 0 then
		return -1 , -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 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


function isManyMonster( idMonster )

	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 ) == "MON" then
			if idMonster ~= -1 and idObj ~= -1 then
				if idMonster ~= idObj then
					return true
				end
			end
		end
	end
	return false
end








function keepOffDifMonster( idFight , nR )

	local n = EnumObject(true)

	if n == 0 then
		return false
	end
	local bResult = false
	g_objTargetKeep = { nID , nX , nY , nR }
	for i = 0 , n - 1 do
		local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
		if tostring( classObj ) == "MON" then
			if idObj ~= tonumber( idFight ) then
				if xObj ~= -1 and yObj ~= -1 then
					addKeepOffTarget( idObj , xObj , yObj , nR )
					bResult = true
				end
			end
		end
	end

	return keepOffMulti()
end


function keepOffMon_Spe( idBoss )
	local xPos , yPos = Player:GetPos()
	local idFight = getNearestID( xPos , yPos , idBoss )
	if idFight ~= -1 then
		local xMon , yMon , _ = getTargetPosID( idBoss )
		if xMon ~= -1 then

			local n = EnumObject(true)

			if n == 0 then
				return false
			end

			g_objTargetKeep = { nID , nX , nY , nR }

			addKeepOffTarget( idFight , xMon , yMon , 10 )

			local bResult = false

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

			return keepOffMulti()

		end	
	end

end

function exeFightBoss2( monsterID )

	Player:EnableTeamFollow( false )
	offRide()
	AI:SetAI("߼ս") 
	AI:SetParameter( "ͼ" , -1 )

	g_mainguard_r = 20

	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 bKeepOffSmall = false
	local bKeepoff = false
	local bKeepOffMon = false
	local bFirst = false
	local bKillSmall = false
	local bCheckFire = false
	local bKeepFire = false

	while true do

		System:Sleep( 10 )

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

		if isNearXY( 41 , 163 , 2 ) == 1 then
			break
		end

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

		if G_bChangeName == 0 then
			local idObjChange = getTargetID( tostring( g_objBoss[ 2 ].szName2 ) )
			if idObjChange ~= -1 then
				monsterID = idObjChange
				G_bChangeName = 1
			end
		end

		if bCheckFire == false then
			if isClassTarget( "SPECIAL" ) == true then
				if 	   isTargetSpecial( "1" ) == true or isTargetSpecial( "2" ) == true or isTargetSpecial( "3" ) == true 
					or isTargetSpecial( "˭1" ) == true or isTargetSpecial( "˭2" ) == true or isTargetSpecial( "˭3" ) == true 

					or isTargetSpecial( "4" ) == true or isTargetSpecial( "5" ) == true or isTargetSpecial( "6" ) == true 
					or isTargetSpecial( "˭4" ) == true or isTargetSpecial( "˭5" ) == true or isTargetSpecial( "˭6" ) == true

					or isTargetSpecial( "7" ) == true or isTargetSpecial( "8" ) == true or isTargetSpecial( "9" ) == true 
					or isTargetSpecial( "˭7" ) == true or isTargetSpecial( "˭8" ) == true or isTargetSpecial( "˭9" ) == true then

					bKeepFire = true
					bCheckFire = true
					bFirst = false
				end
			end
		end

		if bKeepFire == true then

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

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

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

			if isManyMonster( monsterID ) == false then

				if bKeepOff == false then

					keepOffTargetName( "SPECIAL" , 12 )
					bKeepOff = true
	
				end
			else
				if bKeepOff == false then

					keepOffMon_Spe( monsterID )
					bKeepOff = true
	
				end

			end

			if IsMsgCold( "KeepOffFire" , 8000 ) == true then --and isManyMonster( monsterID ) == false then
				bKeepFire = false
				bCheckFire = false
			end

		elseif isManyMonster( monsterID ) == true then


			if IsMsgCold( "KillSmall1" , 100 ) == true then
				bKillSmall = false	
			end

			if bKillSmall == false then
				
					keepOffNearestDiff( monsterID )
					UpdateMsg( "KillSmall1" )
					bKillSmall = true				
			end

		else
			bKeepFire = false
			bCheckFire = false
			bKeepOff = false
			bKillSmall = false

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

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

			g_nX_Keep = -1
			g_nY_Keep = -1
			DelMsg( "KeepOffFire" )
			DelMsg( "KillSmall1" )
		end

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

		if g_nNoFight == 1 then
			g_bKillMonster = 0
			g_bYinshen = 1
			g_bStopAssist = 1
			g_bCallPet = 0
		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 , 6 ) 
		AI:SetAdv("Ȧ뾶" , g_mainguard_r ) 
		AI:SetAdv("" , g_bYinshen ) 

	end
	AI:Stop()
end

function keepOffFriend( idObjKeepOff )

	if idObjKeepOff == -1 then

		g_objCoordinate = { nX , nY , nAngle }
		setPointCircle( g_nX_O , g_nY_O , 45 , 17 )
		local x , y = getFarPointToFriend()
		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 )				
			return true
		end
	else

		g_bKillMonster = 0

		g_bStopAssistSelf = 1
		g_bStopAssist = 1
		g_bStopSpecialAttack = 1
	
		AI:SetAdv("в" , idObjKeepOff , 15 , "" )
		return true
	end

	return false
end



local g_objFriendID = { nID , nX , nY }

function setListFriendID()
	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
			g_objFriendID[ nIndex ] = { nID = idObj , nX = xCurPos , nY = yCurPos }
			nIndex = nIndex + 1 
			
		end
	end
	return true
end
function setFriendPoisoned( rObj )
	local nNumMember = table.getn( g_objFriendID )
	if nNumMember <= 0 then
		return false
	end

	g_objTargetKeep = { nID , nX , nY , nR }

	for i = 1 , nNumMember do
		if tonumber( g_objFriendID[ i ].nID ) ~= -1 and tonumber( g_objFriendID[ i ].nID ) ~= nil then
			local iPos1 ,_,_ = CheckBufferByName( tonumber( g_objFriendID[ i ].nID ) , tostring( g_szPoison1 ) )
			local iPos2 ,_,_ = CheckBufferByName( tonumber( g_objFriendID[ i ].nID ) , tostring( g_szPoison2 ) )
			if iPos1 ~= - 1 or iPos2 ~= -1 then

				if g_objFriendID[ i ].nX ~= -1 and g_objFriendID[ i ].nX ~= -1 then
					addKeepOffTarget( tonumber( g_objFriendID[ i ].nID ) , tonumber( g_objFriendID[ i ].nX ) , tonumber( g_objFriendID[ i ].nY ) , tonumber( rObj ) )
				end
			end
		end
	end
	return keepOffMulti()
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

	printCoordinate()

	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

	printTempCanPoint()

	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
	printCanPoint()

	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
	printCanPoint()

	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 )
			updateTime( "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 )	

		updateTime( "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 xPos ~= -1 and isOutPointKeep( xPos , yPos ) == true then
		g_mainguard_r = 1
		g_nX_Keep = xPos
		g_nX_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
	printObj()
	local xPos ,  yPos = Player:GetPos()
	if isOutPointKeep( xPos , yPos ) == true then
		g_mainguard_r = 1
		g_nX_Keep = xPos
		g_nX_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 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 printCoordinate()
	local nNum = table.getn( g_objCoordinate )

	if nNum <= 0 then
		return fasle
	end

	for i = 1 , nNum do
		
	end
end


function printCanPoint()

	local nNumMember = table.getn( g_objCanPoint )
	if nNumMember <= 0 then
		return false
	end	
	for i = 1 , nNumMember do
		
	end
end


function printObj()
	local nNum = table.getn( g_objTargetKeep )
	if nNum <= 0 then
		return false
	end
	for i = 1 , nNum do
		
	end
end

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

	if  nNumMember <= 0 then
		return false
	end

	for i = 1 , nNumMember do
		
	end
end






local g_objFriend = { nPos , 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 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 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 = 1
	g_bPickUp = 0

	local bKeepoff = true
	local bFightSmall = false
	local bPoisoned = false


	while true do

		System:Sleep( 10 )


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

		if isNearXY( 41 , 163 , 2 ) == 1 then
			break
		end

		if G_bChangeName == 0 then
			local idObjChange = getTargetID( tostring( g_objBoss[ 3 ].szName2 ) )
			if idObjChange ~= -1 then
				monsterID = idObjChange
				G_bChangeName = 1
			end
		end



		local idObj1 = getTargetID( tostring( g_objBossSmall[ 1 ] ) )
		local idObj2 = getTargetID( tostring( g_objBossSmall[ 2 ] ) )

		if Player:GetBuffByName( tostring( g_szPoison1 ) , 1 ) ~= -1 or Player:GetBuffByName( tostring( g_szPoison2 ) , 1 ) ~= -1 then

			g_bKillMonster = 0

			g_bStopAssistSelf = 1
			g_bStopAssist = 1
			g_bStopSpecialAttack = 1

			idObj1 = -1
			idObj2 = -1

			if bPoisoned == false then
				keepOffFriendCircle( 17 )
				UpdateMsg( "KeepOffFriend" )
				bPoisoned = true
			end

		elseif ( idObj1 ~= -1 and Character:GetData( idObj1 , "DIE" ) == false ) or ( idObj2 ~= -1 and Character:GetData( idObj2 , "DIE" ) == false ) then


			g_bKillMonster = 1

			g_bStopAssistSelf = 0
			g_bStopAssist = 0
			g_bStopSpecialAttack = 0

			g_mainguard_r = 20
			
			if IsMsgCold( "FightSmall" , 1000 ) == true then
				bFightSmall = false
			end
			
			if bFightSmall == false then

				if getTargetID( tostring( g_objBossSmall[ 2 ] ) ) ~= -1 then					
					fightNearest( tostring( g_objBossSmall[ 2 ] ) )
					bFightSmall = true
					UpdateMsg( "FightSmall" )
				elseif getTargetID( tostring( g_objBossSmall[ 1 ] ) ) ~= -1 then
					
					fightNearest( tostring( g_objBossSmall[ 1 ] ) )
					bFightSmall = true
					UpdateMsg( "FightSmall" )
				else
					AI:SetAdv( "ָɱ" , -1 ,"" )
				end
			end
		else	
			bPoisoned = false

			if gscript_check( 0 , 0 , 1 ) == 1 then
				if g_nMemPai == 4 then
					setRevival( 40 )
				end
			end

			DelMsg( "KeepOffFiend" )

			g_bKillMonster = 1

			g_bStopAssistSelf = 0
			g_bStopAssist = 0
			g_bStopSpecialAttack = 0
		
			AI:SetAdv( "ָɱ" , -1 ,"" )
			g_mainguard_r = 20
			AI:SetAdv( "Ȧ" , g_nX_O , g_nY_O , g_mainguard_r ) 
			g_nX_Keep = -1
			g_nY_Keep = -1

			bFightSmall = false
		end

		if Character:GetData( monsterID , "DIE" ) == true then
			
			G_bChangeName = 0
			clearMsg()		
			break
		end

		if g_nNoFight == 1 then
			g_bKillMonster = 0
			g_bYinshen = 1
			g_bStopAssist = 1
			g_bCallPet = 0
		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 , 6 ) 
		AI:SetAdv("Ȧ뾶" , g_mainguard_r ) 
		AI:SetAdv("" , g_bYinshen ) 

	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 )
		table.remove( g_objFriendFollow , g_nIndexMember )
	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 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

--[[
function getPosMemberFollow( nR )
	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 ) < nR 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
]]--

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 _callbackSpeakNpc(fX , fY , nScene)

	if GetActiveSceneName() ~= tostring( g_szNameSence[ 2 ] ) then
		return false
	end
	local NpcID , x , y = getTargetPos( tostring( g_szNpcGetBoss ) )
	if x ~= -1 then
		if x ~= fX and y ~= fX then
			return false
		end
	end
	return true

end

function teamleaderSpeakBoss1()

	while true do

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

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

		local NpcID , x , y = getTargetPos( tostring( g_szNpcGetBoss ) )
		if NpcID ~= -1 then
			if x ~= -1 and y ~= -1 then
				if GetDist( x , y , 60 , 106 ) <= 3 then
					MoveToNPC_ById( x , y , -1 , NpcID , 0 , _callbackSpeakNpc )

					local xPos , yPos = Player:GetPos()
	
					if xPos ~= -1 then
	
						if GetDist( xPos , yPos , x ,  y ) <= 3 then
							Player:EnableTeamFollow( false )
							offRide()	
							System:Sleep( 500 )
							QuestFrameOptionClicked( "" , CMP_PART_MATCH )
		
							if isMsgItem( "MZFB_150810_46" ) == true then
								QuestFrameMissionClose()
								goFollowMe()
								break
							end		
						end
					end
				end
			end
		end
		QuestFrameMissionClose()
		System:Sleep( 10 )

	end
end


function speakOutSence()
	while true do
		if GetActiveSceneName() ~= tostring( g_szNameSence[ 2 ] ) then
			break
		end

		local NpcID , x , y = getTargetPos( tostring( g_szNpcGetBoss ) )
		if NpcID ~= -1 then	

			MoveToNPC_ById( x , y , -1 , NpcID , 0 , _callbackSpeakNpc )				
			local xPos , yPos = Player:GetPos()

			if xPos ~= -1 then

				if GetDist( xPos , yPos , x ,  y ) <= 3 then
					Player:EnableTeamFollow( false )
					offRide()
					MoveToNPC_ById( x , y , -1 , NpcID , 0 , _callbackSpeakNpc )

					System:Sleep( 500 )
					QuestFrameOptionClicked( "뿪" , CMP_PART_MATCH )

				end
			end
		end
		QuestFrameMissionClose()
		System:Sleep( 1500 )

	end
end


function teamleaderSpeakBoss2()

	while true do

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

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

		local NpcID , x , y = getTargetPos( tostring( g_szNpcGetBoss ) )
		if NpcID ~= -1 then
			if x ~= -1 and y ~= -1 then
				if GetDist( x , y , 129 , 74 ) <= 3 then
					MoveToNPC_ById( x , y , -1 , NpcID , 0 , _callbackSpeakNpc )				
					local xPos , yPos = Player:GetPos()
	
					if xPos ~= -1 then
	
						if GetDist( xPos , yPos , x ,  y ) <= 3 then
							Player:EnableTeamFollow( false )
							offRide()
 
	
							System:Sleep( 500 )
							QuestFrameOptionClicked( "" , CMP_PART_MATCH )
		
							if isMsgItem( "MZFB_150810_46" ) == true then
								QuestFrameMissionClose()
								goFollowMe()
								break
							end		
						end
					end
				end
			end
		end
		QuestFrameMissionClose()
		System:Sleep( 10 )

	end

end


function teamleaderSpeakBoss3()

	while true do

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

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

		local NpcID , x , y = getTargetPos( tostring( g_szNpcGetBoss ) )
		if NpcID ~= -1 then
			if x ~= -1 and y ~= -1 then
				if GetDist( x , y , 131 , 140 ) <= 3 then
					MoveToNPC_ById( x , y , -1 , NpcID , 0 , _callbackSpeakNpc )				
					local xPos , yPos = Player:GetPos()
	
					if xPos ~= -1 then
	
						if GetDist( xPos , yPos , x ,  y ) <= 3 then
							Player:EnableTeamFollow( false )
							offRide()

	
							System:Sleep( 500 )
							QuestFrameOptionClicked( "" , CMP_PART_MATCH )
		
							if isMsgItem( "MZFB_150810_46" ) == true then
								QuestFrameMissionClose()
								goFollowMe()
								break
							end		
						end
					end
				end
			end
		end
		QuestFrameMissionClose()
		System:Sleep( 10 )

	end
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 exeStep1()

	System:Sleep( 500 )

	if getTargetID( tostring( g_objBoss[ 1 ].szName1 ) ) == -1 and getTargetID( tostring( g_objBoss[ 1 ].szName2 ) ) == -1 then
		if isNearXY( g_nX_O , g_nY_O , 15 ) == 0 then
			onRide()
			if DataPool:IsTeamLeader() then
				Player:EnableTeamFollow( true )
				MoveTo( g_nX_O , g_nY_O , -1 , "" , 0 , true , _callbackInSence )
				if isClassTarget( "MON" ) == false then
					System:Sleep( 2000 )
				end
				Player:EnableTeamFollow( false )
			else
				MoveTo( g_nX_O , g_nY_O , -1 , "" , 0 , true , _callbackInSence )
				offRide()
			end
		end

		if getTargetID( tostring( g_objNpcPlace[ 1 ] ) ) == -1 and getTargetID( tostring( g_objBoss[ 1 ].szName1 ) ) == -1 and getTargetID( tostring( g_objBoss[ 1 ].szName2 ) ) == -1 and getTargetID( tostring( g_szNpcGetBoss ) ) == -1 then
			onRide()
			G_nStep = 2
			return false
		end


	end

	if isClassTarget( "MON" ) == false then

		buffHp()
		
		if DataPool:IsTeamLeader() then
			teamleaderSpeakBoss1()
		else
			offRide()
		end
	else
		Player:EnableTeamFollow( false )
		local monsterID = getTargetID( tostring( g_objBoss[ 1 ].szName1 ) )
		if monsterID == -1 then
			monsterID = getTargetID( tostring( g_objBoss[ 1 ].szName2 ) )
		end
		if monsterID ~= -1 then
			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 getTargetID( tostring( g_objBoss[ 2 ].szName1 ) ) == -1 and getTargetID( tostring( g_objBoss[ 2 ].szName2 ) ) == -1 then
		if isNearXY( g_nX_O , g_nY_O , 15 ) == 0 then
			onRide()
			if DataPool:IsTeamLeader() then
--				Player:EnableTeamFollow( true )
				MoveTo( g_nX_O , g_nY_O , -1 , "" , 0 , true , _callbackInSence )

				if isClassTarget( "MON" ) == false then
					System:Sleep( 2500 )
				end
				
				Player:EnableTeamFollow( false )
			else
				MoveTo( g_nX_O , g_nY_O , -1 , "" , 0 , true , _callbackInSence )
				offRide()
			end

		end

		if getTargetID( tostring( g_objNpcPlace[ 2 ] ) ) == -1 and getTargetID( tostring( g_objBoss[ 2 ].szName1 ) ) == -1 and getTargetID( tostring( g_objBoss[ 2 ].szName2 ) ) == -1 and getTargetID( tostring( g_szNpcGetBoss ) ) == -1 then
--			onRide()
			G_nStep = 3
			return false
		end


	end

	if isClassTarget( "MON" ) == false then

		buffHp()

		if DataPool:IsTeamLeader() then
			teamleaderSpeakBoss2()
		else
			offRide()
		end
	else

		Player:EnableTeamFollow( false )
		local monsterID = getTargetID( tostring( g_objBoss[ 2 ].szName1 ) )
		if monsterID == -1 then
			monsterID = getTargetID( tostring( g_objBoss[ 2 ].szName2 ) )
		end
		if monsterID ~= -1 then
			if Character:GetData( monsterID , "DIE" ) == false then 
				exeFightBoss2( monsterID )
			elseif Character:GetData( monsterID , "DIE" ) == true then
				OnSweepBattleField( 1 )
			end
		end
	end
end

function exeStep3()

	if getTargetID( tostring( g_objBoss[ 3 ].szName1 ) ) == -1 and getTargetID( tostring( g_objBoss[ 3 ].szName2 ) ) == -1 then
		if isNearXY( g_nX_O , g_nY_O , 20 ) == 0 then
			onRide()
			if DataPool:IsTeamLeader() then
--				Player:EnableTeamFollow( true )
				MoveTo( g_nX_O , g_nY_O , -1 , "" , 0 , true , _callbackInSence )

				if isClassTarget( "MON" ) == false then
					System:Sleep( 2500 )
				end

				Player:EnableTeamFollow( false )
			else
				MoveTo( g_nX_O , g_nY_O , -1 , "" , 0 , true , _callbackInSence )
				offRide()
			end
		end


	end

	if isClassTarget( "MON" ) == false then

		buffHp()

		if DataPool:IsTeamLeader() then
			teamleaderSpeakBoss3()
		else
			offRide()
		end
	else
		Player:EnableTeamFollow( false )

		local monsterID = getTargetID( tostring( g_objBoss[ 3 ].szName1 ) )
		if monsterID == -1 then
			monsterID = getTargetID( tostring( g_objBoss[ 3 ].szName2 ) )
		end

		if monsterID ~= -1 then
			if Character:GetData( monsterID , "DIE" ) == false then 
				exeFightBoss3( monsterID )
			elseif Character:GetData( monsterID , "DIE" ) == true then
				OnSweepBattleField( 1 )
				G_bGetItems = 1
			end
		end
	end
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
	onRide()
	Player:EnableTeamFollow( true )
	MoveToNPC_ById( x , y , -1 , NpcID , 0 , _callbackOutSence )
	System:Sleep( 1000 )

	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
			Shout( "team" , "ӳѾˢ" )
			ShowMessage( "űִϣ" )
			Player:EnableTeamFollow( false )
			System:Sleep( 1000 )
			return 0
		end
	end
	QuestFrameMissionClose()
	return 1
	
end


local g_bFirst = false
function isConditionTimes( szNameMission )

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

		DataPool:UpdateActivityList(3)
		System:Sleep( 1000 )
		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 clearMsg()
	local channel, data = GetDebugMessage()
	while channel == 1 do
		channel, data = GetDebugMessage()
	end
end

function getCurrentBoss()
	for i = 1 , table.getn( g_objBoss ) do
		local idObj1 = getTargetID( tostring( g_objBoss[ i ].szName1 ) )
		local idObj2 = getTargetID( tostring( g_objBoss[ i ].szName2 ) )
		if idObj1 ~= -1 then
			return i , idObj1		
		end
		if  idObj2 ~= -1 then
			return i , idObj2
		end
	end
	return -1 , -1
end

local g_objNpcPlace = { 
	[ 1 ] = "ӡѺ" , 
	[ 2 ] = "ӡ" ,
	[ 3 ] = "ӡƻ"
}
function getCurrentPlace()
	for i = 1 , table.getn( g_objNpcPlace ) do
		if getTargetID( tostring( g_objNpcPlace[ i ] ) ) ~= -1 then
			return i
		end
	end
	return -1
end


function gotoMapNPC()

	while true do

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

		if CityToAlliance() == true then
			break
		end
	end

	MoveTo( 171 , 185 , -1 , "" , 0 , true , _callbackOutSence )

	return true
end

 


function isMsgItem2( szSubText1 , szSubText2 , nPos )

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

	if strType == "id" or strType == "text" then

		if string.find( szText , szSubText1 ) then
			return 1
		elseif string.find( szText , szSubText2 ) then
			return 2
		end
	end

	return -1
end

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

	if nResult == 0 then
		return true
	end

	return false
end

local g_objNpc = { nID , nX , nY }
function speakByStep( idNpc , xNpc , yNPc )
	if G_nID_Npc ~= -1 then
		
		while true do

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

			if G_nX_Npc ~= -1 and G_nY_Npc ~= -1 then
				MoveToNPC_ById( G_nX_Npc , G_nY_Npc , -1 , G_nID_Npc , 0 , _callbackInSence )
				System:Sleep( 500 )
				QuestFrameOptionClicked( "" , CMP_PART_MATCH )
				System:Sleep( 500 )

				if isMsgItem( "Ѿ˻þڵ䣬" ) == true then
					QuestFrameMissionClose()
					return true
				end

				QuestFrameOptionClicked( "ֱӻȡ" , CMP_PART_MATCH )
				System:Sleep( 1000 )
				QuestFrameMissionClose()
			end

			System:Sleep( 10 )
		end
	else
		local nNum = table.getn( g_objNpc )
		if nNum <= 0 then
			return false
		end
		for i = 1 , nNum do
			local j = nNum - i + 1
			MoveToNPC_ById( g_objNpc[ j ].nX , g_objNpc[ j ].nY , -1 , g_objNpc[ j ].nID , 0 , _callbackInSence )
			System:Sleep( 500 )

			QuestFrameOptionClicked( "" , CMP_PART_MATCH )
			System:Sleep( 500 )

			if isMsgItem2( " " , "ֱӻȡ" , 1 ) == 2 then
	
				QuestFrameOptionClicked( "ֱӻȡ" , CMP_PART_MATCH )
	
				System:Sleep( 500 )

				if isMsgItem( "λ㣬ûеطɴ֮һλ" ) == true then

					UpdateMsg( "FullBagBoss3" )
					ShowMessage( "!" )

					while true do
	
						if GetActiveSceneName() ~= tostring( g_szNameSence[ 2 ] ) then
							break
						end
						if isFullBag( 1 ) == false then
	
							G_nID_Npc = tonumber( g_objNpc[ j ].nID )
							G_nX_Npc = tonumber( g_objNpc[ j ].nX )
							G_nY_Npc = tonumber( g_objNpc[ j ].nY )
	
							g_objNpc = { nID , nX , nY }
						
							return false
						end

						if IsMsgCold( "FullBagBoss3" , 5000 ) == true then
							ShowMessage( "!" )
							UpdateMsg( "FullBagBoss3" )
						end

						System:Sleep( 100 )
					end
				end
				return true
			elseif isMsgItem( "Ѿ˻þڵ䣬" ) == true then
				return true
			end

			QuestFrameMissionClose()
		end
		return false
	end


end

function getByStepNpc()
	if G_nID_Npc ~= -1 then
		return speakByStep()
	else
		local n = EnumObject(true)
		if n == 0 then		
			return false
		end
		g_objNpc = { nID , nX , nY }
		local bResult = false
		local nIndex = 1
		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 ) then
					if idObj ~= -1 then
						if xObj ~= -1 and yObj ~= -1 then
							g_objNpc[ nIndex ] = { nID = idObj , nX = xObj , nY = yObj }
							nIndex = nIndex + 1
						end
					end	
				end
			end
		end
		return speakByStep()
	end	
end

function speakGetItems()

	while true do

		if GetActiveSceneName() ~= tostring( g_szNameSence[ 2 ] ) then
			break
		end
		if getByStepNpc() == true then
			G_bFinish = 1
			return true
		end
		System:Sleep( 1000 )
	end

end

function _SMain(...)	


	if Player:GetData( "LEVEL" ) < 85 then
		ShowMessage( "ȼ>= 85 " )
		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( 8 )
	while true do

		System:Sleep( 100 )

		local szCurrentScene = GetActiveSceneName()

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

			clearMsg()

			G_bRevival = 0
			G_nStep = 1
			G_bChangeName = 1
			G_bOut = 0
			G_bFinish = 0

			G_bMsgFun = 0
			G_bMsgLine = 0

			G_bRevival = 0

			if GetQuestTimesInfo( "þ" , 10000 ) == 0 and GetActiveSceneName() ~= tostring( g_szNameSence[ 2 ] ) then 

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


			if gotoMapNPC() == true then

				if DataPool:GetTeamMemCount() < 3 and GetActiveSceneName() ~= tostring( g_szNameSence[ 2 ] ) then 
					ShowMessage(" 3ˣ")
					Player:EnableTeamFollow( false )
					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() then
					if speakNpcMission() == 0 then
						Player:EnableTeamFollow( false )
						return
					end
				end
			end	

		else
			if G_bOut == 0 then				
				clearMsg()
				G_bOut = 1
			end

			local nCurrent , idCurrent  = getCurrentBoss()

			if nCurrent ~= -1 then
				if Character:GetData( idCurrent , "DIE" ) == true then
					OnSweepBattleField( 1 )
					if nCurrent == 3 then
						G_bGetItems = 1
					end
					G_nStep = nCurrent + 1
				else
					G_nStep = nCurrent
				end
			end

			if G_bGetItems == 1 or getTargetID( "䡤" ) ~= -1 then				
				if G_bFinish == 0 then
					if speakGetItems() == true then
						speakOutSence()
					end
				else
					speakOutSence()
				end
				continue
			end
			local nIndexPlace = getCurrentPlace()
			if nIndexPlace ~= -1 then
				G_nStep = nIndexPlace
			end

			if G_nStep == 1 then
				g_nX_O = 54
				g_nY_O = 112
				g_nFollow = 1
				exeStep1()

			elseif G_nStep == 2 then
				g_nMaxR1 = 20
				g_nX_O = 126
				g_nY_O = 65
				g_nFollow = 2

				exeStep2()

			elseif G_nStep == 3 then

				setListFriendID()

				g_nMaxR1 = 17
				g_nX_O = 132
				g_nY_O = 152
				g_nFollow = 3
				exeStep3()

			end

		end
	end

	LogChatMsg( -1 )
end

_SMain()

