
--[[
[author]
class=1
attr=1
descript = 网络卡的时候，卡怪会出现问题。这个时候就直接打怪。
chk0str = 不卡怪，直接打
chk1str = 队员卡怪
]]--

perday_max=3
script_name = "燕子坞"

local g_Monster = {}
	g_Monster[ 1 ] = { szName = "岳老三"  }
	g_Monster[ 2 ] = { szName = "云中鹤"  }
	g_Monster[ 3 ] = { szName = "段延庆" }

local g_objCurrent = {
	[1] = "呼延庆" ,
	[2] = "呼延豹" ,
	[3] = "钱宏宇" ,
	[4] = "华赫艮" ,
	[5] = "燕子坞庄丁" ,
	[6] = "慕容复"
}


local g_szNameSence = {
	[1] = "太湖" ,
	[2] = "燕子坞"	
}

local g_bQuai = 0
local g_bMemberQuai = 0
local g_nNoFight = 0

function isTimeExeMission( subNameMember )
	for i = 0 , DataPool:GetTeamMemCount() - 1 do
		local strType,_,_,_,szText = DataPool:GetNPCEventList_Item( i )
		local szMsg = string.sub( szText , string.len( szText ) - 5 , string.len( szText ) )
		if string.find( szText , subNameMember ) and string.find( szMsg , "不满足" ) then
			return false
		end
	end
	return true
end
function isMsg( szSubFind )
	local isNext , szMsg = GetDebugMessage()
	while isNext == 1 do
		if string.find( szMsg , szSubFind ) then
			return true
		end
		isNext , szMsg = GetDebugMessage()
	end
	return false

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 isMsgItemID()
	local strType,_,_,_,szText = DataPool:GetNPCEventList_Item( 1 )
	if strType == "id" then				
		return true		
	end
	return false
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 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 getTargetPosID( idTarget )

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

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

end


function getCurrentObjID()
	for i = 1 , 6 do
		local id = getTargetID( g_objCurrent[ i ] )
		if id ~= -1 then
			return i
		end
	end
	return -1
end

function getCurrentMonsterID()
	local idResult = -1
	for i = 1 , 3 do
		local id = getTargetID( g_Monster[ i ].szName )
		if id ~= -1 then
			if Character:GetData( id , "DIE" ) == false then
				idResult = id
				break
			elseif Character:GetData( id , "DIE" ) then
				if id ~= idResult then
					idResult = id 
				end
			end
		end
	end
	return idResult
end

function getCurrentMonsterIndex( szNameCurrent )
	for i = 1 , 3 do
		if szNameCurrent == g_Monster[ i ].szName then
			return i
		end
	end
	return -1
end
local g_countMember = 0

function isOnceFollow( nDist )
	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 nameObj == "燕子坞庄丁" or nameObj == "秦家寨亲兵" then
			local xPos , yPos = Player:GetPos()
			local nDest = GetDist( xPos , yPos , xObj , yObj )
			if nDest <= nDist and nDest ~= -1 then
				return true
			end
		end
	end
	return false
end

function isCheckNpc()

	local nFlagFind = false

	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 nameObj == "宋军士兵" then
			return true
		end
	end
	return false
end
function isMonsterLive()

	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 nameObj == "燕子坞庄丁" or nameObj == "秦家寨亲兵" then
			return true
		end
	end
	return false
end

function teamLeaderSpeekNpc3( bMember )
	if bMember == true then
		local bBreak = false
		while true do
			if bBreak == true then
				break
			end

			if getTargetID( g_objCurrent[ 4 ] ) ~= -1 then
				break
			end

			local NpcID , x , y = getTargetPos( "钱宏宇" )
			local xPos , yPos = Player:GetPos()
			MoveTo( x , y , -1 , "" , 0 , true , _callbackMemberLoop )

			if NpcID ~= -1 then
				if GetDist( xPos , yPos , x , y ) <= 3 then
					for i = 1 , 2 do
						MoveToNPC_ById( x , y , -1 , NpcID , 0 , _callbackMemberLoop )
						System:Sleep( 100 )
						bBreak = true
						G_nStep = 4
						System:Sleep( 100 )
					end
					QuestFrameMissionClose()
				end
			end
			System:Sleep( 100 )
		end
		Player:EnableTeamFollow( true )
		MoveTo( 185 , 173 , -1 , "" , 0 , true , _callbackMemberLoop )

	else
		local bBreak = false
		while true do
			if bBreak == true then
				break
			end
			local NpcID , x , y = getTargetPos( "钱宏宇" )
			local xPos , yPos = Player:GetPos()
			MoveTo( x , y , -1 , "" , 0 , true , _callback )

			if NpcID ~= -1 then
				if GetDist( xPos , yPos , x , y ) <= 2 then
					for i = 1 , 2 do
						MoveToNPC_ById( x , y , -1 , NpcID )
						System:Sleep( 100 )
						bBreak = true
						G_nStep = 4
					end
				end
			end
			System:Sleep( 100 )
		end
		Player:EnableTeamFollow( true )
		MoveTo( 185 , 173 , -1 , "" , 0 , true , _callbackFollow )
	end
end

function goFindNpc3( bMember )
	while true do
		Player:EnableTeamFollow( true )
		local NpcID , x , y = getTargetPos( "钱宏宇" )
		if NpcID ~= -1 then
			return 0
		end
		if bMember == true then
			if getTargetID( g_objCurrent[ 4 ] ) ~= -1 then
				return 0
			end
		end

		MoveTo( 185 , 173 , -1 , "" , 0 , true , _callbackFollowNpc ) 
		local xPos , yPos = Player:GetPos()
		if GetDist(xPos , yPos , 185 , 173 ) <= 2 then
			Player:EnableTeamFollow( false )
--			Shout( "team" , "打打怪，赚点经验吧 :))" )
			Player:EnableTeamFollow( false )
			G_bFight = 1
			setFightMonster3()
			return 1

		end
		System:Sleep( 100 )				
	end

end



function _callbackMemberLoop( fX , fY , nScene )

	if getTargetID( g_objCurrent[ 4 ] ) ~= -1 then
		Player:StopAction()
		return false
	end

	if GetActiveSceneName() ~= "燕子坞" then
		Player:StopAction()
		return false
	end
	return true
end

function goLoop( bMember )
		
	if bMember then
		while true do
			MoveTo( 228 , 191 , -1 , "" , 0 , true , _callbackMemberLoop )
			if isOnceFollow( 5 ) then
				break
			end
			System:Sleep( 60 )
		end

		while true do


			if isOnceFollow( 3 ) then
				break
			end
			System:Sleep( 60 )

		end

		while true do
			MoveTo( 218 , 152 , -1 , "" , 0 , true , _callbackMemberLoop ) 
--			MoveTo( 219 , 158 , -1 , "" , 0 , true , _callbackMemberLoop )
			if isOnceFollow( 8 ) then
				break
			end
			System:Sleep( 60 )

		end


		while true do
			MoveTo( 204 , 155 , -1 , "" , 0 , true , _callbackMemberLoop )
			if isOnceFollow( 10 ) then
				break
			end
			System:Sleep( 60 )

		end

		while true do
			MoveTo( 191 , 161 , -1 , "" , 0 , true , _callbackMemberLoop )
			if isOnceFollow( 12 ) then
				break
			end
			System:Sleep( 60 )

		end



		local bNpcFailed = false
		local nWaitNpc = 1
		while true do
			if nWaitNpc > 30 then
				bNpcFailed = true
				break
			end
		
			if getTargetID( g_objCurrent[ 4 ] ) ~= -1 then
				break
			end

			Shout( "team" , "不要点NPC!" )
			MoveTo( 183 , 170 , -1 , "" , 0 , true , _callbackMemberLoop )
			if isOnceFollow( 12 ) and isCheckNpc() then
				break
			end
			System:Sleep( 60 )
			nWaitNpc = nWaitNpc + 1
		end
		
		if bNpcFailed == false then
			local nWaitM = 1
			while true do		
				if nWaitM > 30 then
					break
				end
				if getTargetID( g_objCurrent[ 4 ] ) ~= -1 then
					break
				end
				MoveTo( 183 , 196 , -1 , "" , 0 , true , _callbackMemberLoop )
				onRide()
				if isOnceFollow( 10 ) then
					break
				end
				System:Sleep( 100 )
				nWaitM = nWaitM + 1
			end
		end
		local NpcID , x , y = getTargetPos( "钱宏宇" )

		if NpcID == -1 then
			System:Sleep( 1000 )
			G_bFight = goFindNpc3( true )
		end

		if G_bFight == 0 then			
			teamLeaderSpeekNpc3( true )
		end


	else
		while true do
			MoveTo( 228 , 191 , -1 )
			if isOnceFollow( 5 ) then
				break
			end
			System:Sleep( 60 )
		end

		while true do
			MoveTo( 226 , 170 , -1 )
			if isOnceFollow( 3 ) then
				break
			end
			System:Sleep( 60 )

		end

		while true do

			MoveTo( 218 , 152 , -1 ) 
			if isOnceFollow( 8 ) then
				break
			end
			System:Sleep( 60 )

		end

		while true do
			MoveTo( 204 , 155 , -1 )
			if isOnceFollow( 10 ) then
				break
			end
			System:Sleep( 60 )

		end

		while true do
			MoveTo( 191 , 161 , -1 )
			if isOnceFollow( 12 ) then
				break
			end
			System:Sleep( 60 )

		end

		local bNpcFailed = false
		local nWaitNpc = 1
		while true do
			if nWaitNpc > 30 then
				bNpcFailed = true
				break
			end
--			Shout( "team" , "我来点NPC!" )
--			MoveTo( 183 , 170 , -1 , "" , 0 , true , _callbackFollow )
			MoveTo( 183 , 170 , -1 )
			if isOnceFollow( 12 ) and isCheckNpc() then
				break
			end
			System:Sleep( 60 )
			nWaitNpc = nWaitNpc + 1
		end
		
		if bNpcFailed == false then
			local nWaitM = 1
			while true do		
				if nWaitM > 30 then
					break
				end
				MoveTo( 183 , 196 , -1 )
				onRide()
				if isOnceFollow( 10 ) then
					break
				end
				System:Sleep( 100 )
				nWaitM = nWaitM + 1
			end
		end
		local NpcID , x , y = getTargetPos( "钱宏宇" )

		if NpcID == -1 then
			System:Sleep( 1000 )
			G_bFight = goFindNpc3( false )
		end

		if G_bFight == 0 then
			
			teamLeaderSpeekNpc3( false )
		end
	end

	MoveTo( 180 , 89 , -1 , "" , 0 , true , _callbackFollow )
	G_nStep = 4
	Player:EnableTeamFollow( false )


end

function _callback( fX , fY , nScene )

	local n = EnumObject(true)
	if n == 0 then
		
		return true
	end
	for i = 0 , n - 1 do
		local idObj , classObj , nameObj , xObj , yObj , _, modelObj = EnumObject( false, i )
		if classObj == "NPC" and nameObj == "钱宏宇" then
			if GetDist( fX , fY , xObj , yObj ) <= 2 then
				return false
			end
		end
	end
	return true
end

function _callbackFollowNpc( fX , fY , nScene )
	if getTargetID( g_objCurrent[ 3 ] ) ~= -1 then
		return false
	end
	return true
end


function _callback1( fX , fY , nScene )

	if getTargetID( g_objCurrent[ 6 ] ) ~= -1 then
		Player:StopAction()
		return false
	end

	if getTargetID( g_objCurrent[ 1 ] ) ~= -1 then
		G_bInCity = 0
		return false
	end

	if GetActiveSceneName() ~= "燕子坞" then
		Player:StopAction()
		return false
	end
	return true
end

function _callbackDie( fX , fY , nScene )

	if getTargetID( g_objCurrent[ 1 ] ) ~= -1 then
		G_bInCity = 0
		return false
	end

	if GetActiveSceneName() ~= "燕子坞" then
		return false
	end
	return true
end


function _callbackFriend( fX , fY , nScene )

	if GetActiveSceneName() ~= "燕子坞" then
		return false
	end	
	if isTeamLeaderLive() == 1 then
		if getCurrentMonsterID() ~= -1 then			
			Player:EnableTeamFollow( false )
			G_bItemsInBoat = 1
			return true
		else
			G_nStep = 3
			G_bItemsInBoat = 0
			return false
		end
	end
	return true
end
function _callbackOutSence()

	if GetActiveSceneName() == "燕子坞" then
		return false
	end	
	return true
end

function _callbackFollow( fX , fY , nScene )

	if GetActiveSceneName() ~= "燕子坞" then
		return false
	end
	if isNearXY( 180 , 89 , 15 ) == false and isNearXY( 80 , 203 , 15 ) == false then
		Player:EnableTeamFollow( true )
	end

	local xPos , yPos = Player:GetPos()
	if xPos == fX and yPos == fY then
		return false
	end
	return true
end

function _callbackInSence( fX , fY , nScene )

	if GetActiveSceneName() ~= "燕子坞" then
		return false
	end

	if getTargetID( g_objCurrent[ 1 ] ) ~= -1 then
		G_bInCity = 0
		Player:StopAction()
		return false
	end
	return true
end


function _callbackInGoSence( fX , fY , nScene )

	if GetActiveSceneName() ~= "燕子坞" then
		return false
	end
	return true
end

function _callbackFollowMeIn( fX , fY , nScene )

	if GetActiveSceneName() ~= "燕子坞" then
		return false
	end
	if isMemTeamNearXY( fX , fY , 2 ) == -1 then
		return false
	end
	return true
end

function isNearXY( nX , nY , nR )
	local xPos , yPos = Player:GetPos()
	if GetDist( xPos , yPos , nX , nY ) <= nR then
		return true
	end
	
	return false

end

function _callbackFollowMe1()

	if GetActiveSceneName() ~= "燕子坞" then
		return false
	end
	if isMemTeamNearXY( 184 , 181 , 15 ) == -1 then
		return false
	end
	return true
end
function _callbackDisFollow( fX , fY , nScene )

	if GetActiveSceneName() ~= "燕子坞" then
		return false
	end
	Player:EnableTeamFollow( false )
	local xPos , yPos = Player:GetPos()
	if xPos == fX and yPos == fY then
		return false
	end
	return true
end

function isLag()
	if getCurrentObjID() == -1 and DataPool:GetTeamMemCount() <= 0 and isCheckNpc() == false and isCheckOneMonster() == false then
		return true
	end
	return false
end

function isTeamMemberLive()
	if DataPool:GetTeamMemCount() <= 0 then
		return 2
	end
	for i = 0 , DataPool:GetTeamMemCount() do
		local szName , idPlayer ,  _, idMap , xCurPos , yCurPos = DataPool:GetTeamMemInfo( i )
		if getTargetID( szName ) ~= -1 and szName ~= Player:GetName() then		
			return 1
		end
	end
	return 0
end


function isSeeMemberID( idMember )

	if DataPool:GetTeamMemCount() <= 0 then
		return 2
	end
	for i = 0 , DataPool:GetTeamMemCount() do
		local szName , idPlayer ,  _, idMap , xCurPos , yCurPos = DataPool:GetTeamMemInfo( i )
		if getTargetID( szName ) ~= -1 and szName ~= Player:GetName() and idPlayer == idMember then		
			return 1
		end
	end
	return 0
end

function isPlayerNearFollowID( idMember , nDist )

	if DataPool:GetTeamMemCount() <= 0 then
		return false
	end
	local xPos, yPos = Player:GetPos()
	for i = 0 , DataPool:GetTeamMemCount() do
		local szName , idPlayer ,  _, idMap , xCurPos , yCurPos = DataPool:GetTeamMemInfo( i )
		if idPlayer == idMember then
			if GetDist( xPos , yPos , xCurPos , yCurPos ) < nDist then
				return true
			end
		end
	end
	return false
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

function goFollowMe()

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

	while true do
		System:Sleep( 100 )

		if GetActiveSceneName() ~= "燕子坞" then
			break
		end

		if getTargetID( g_objCurrent[ 1 ] ) ~= -1 then
			break
		end

		local x , y = getPosMemberFollow( 100 )
		if x ~= -1 and y ~= -1 then
			onRide()
			Player:EnableTeamFollow( true )
			MoveTo( x , y , -1 , "" , 0 , true , _callbackFollowMeIn )
			Player:EnableTeamFollow( true )
		elseif x == -1 and y == -1 then
			onRide()
			Player:EnableTeamFollow( true )
			break
		end	
	end
	onRide()
end


function isTeamLeaderLive()
	if DataPool:GetTeamMemCount() <= 0 then
		return 2
	end
	local szName , idPlayer ,  _, idMap , xCurPos , yCurPos = DataPool:GetTeamMemInfo( 0 )
	if getTargetID( szName ) ~= -1 then		
		return 1
	end
	return 0
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_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 = 0
local g_bYinshen = 0
local g_FollowTarget = -1
local g_mainguard_r = 6
local g_nNumItems = 0
local g_bShowMsgFollow = false
local g_bPickUp = 0
function setFightMonster2( NpcID )
	Player:EnableTeamFollow( false )
	g_FollowTarget = NpcID
	g_mainguard_r = 15
	AI:SetAdv( "主防御圈" , 218 , 152 , g_mainguard_r )
	exeFightMonster( -1 )
	Player:EnableTeamFollow( true )
	goNpc4( true )
	if G_nStep ~= 4 then
		G_nStep = 3
	end
end


if G_nMsg9 == nil then
	G_nMsg9  = 0
end
if G_nMsg7 == nil then
	G_nMsg7  = 0
end

if G_nMsg4 == nil then
	G_nMsg4  = 0
end

if G_nMsg2 == nil then
	G_nMsg2  = 0
end
if G_bOut == nil then
	G_bOut = 1
end

if G_nMsgMonster3 == nil then
	G_nMsgMonster3 = 0
end
if G_nMsgMonster3Follow == nil then
	G_nMsgMonster3Follow = 0
end
if G_nMemberExe == nil then
	G_nMemberExe = 0
end


if G_bItemsInBoat == nil then
	G_bItemsInBoat = 0
end
if g_bBackFight == nil then
	g_bBackFight = 0
end

if G_bSeenNpc3 == nil then
	G_bSeenNpc3 = 0
end


if G_nStep == nil then
	G_nStep = 1
end
if G_bInCity == nil then
	G_bInCity = 0
end

if G_nFighting == nil then
	G_nFighting = 0
end

if G_bFight == nil then
	G_bFight = 0
end

if G_bFinish == nil then
	G_bFinish = 0
end
if G_bNoMyself == nil then
	G_bNoMyself = 0
end
if G_speakNpc3OK == nil then
	G_speakNpc3OK = 0
end

if G_bRevial == nil then
	G_bRevial = 0
end

if G_bPickup == nil then
	G_bPickup = 0 
end

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

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



		if string.find( data , "还有0拨燕子坞援军即将赶来"  ) then
			G_nMsg9 = 1
		end
		if string.find( data , "还有1拨燕子坞援军即将赶来"  ) then
			G_nMsg7 = 1
		end

		if string.find( data , "请大家速速从暗道处深入燕子坞内部" ) then 
			G_nMsg4 = 1
		end

		if string.find( data , "还有7拨燕子坞援军即将赶来" ) then 
			G_nMsg2 = 1
		end

		channel, data = GetDebugMessage()
	end
end

function getMsg( szSubMsg )
	local channel, data = GetDebugMessage()
	while channel == 1 do
		if string.find( data , szSubMsg ) then 
			return true
		end
		channel, data = GetDebugMessage()
	end
	return false
end
function isMsgTeam( subMsgTeam )
	while true do 
		local channel, time, name, data = GetChatMsg()
		if channel == -1 then
			return false
		end
		if string.find( data , subMsgTeam ) then
			return true
		end
	end

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 ) and Player:GetData( "HP" ) >= Player:GetData( "HP" ) * ( nMax / 100 ) then
				return 0
			end

		end
	end

	return 1
end


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

function buffMySeltAndFriend( nFlag )

	Player:EnableTeamFollow( false )
	offRide()
	AI:SetAI("高级战斗") 
	AI:Start()
	local xPos , yPos = Player:GetPos()
	AI:SetAdv( "主防御圈" , xPos , yPos , 4 )
		
	g_bKillMonster = 0
	g_bTeamAttack = 0
	g_bStopAssistSelf = 0
	g_bStopAssist = 0
	g_mainguard_r = 15

	while true do
		
		System:Sleep( 100 )

		if GetMount() ~= -1 then
			Dismount()	
		end
		

		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() ~= "燕子坞" then
				break
			end

			if G_nInCity == 0 then
				break
			end
			if getTargetID( g_objCurrent[ 6 ] ) ~= -1 then
				break
			end

		elseif 	nFlag == 2 then
			if  GetActiveSceneName() == "燕子坞" then
				break
			end
		else
			if  GetActiveSceneName() ~= "燕子坞" then
				break
			end

			if G_nInCity == 1 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 buffHp( nFlag )
	if Player:GetData( "MEMPAI" ) == 4 then
		if isTeamHpLow( 80 ) or ( Player:GetData( "HP" ) < Player:GetData( "MAXHP" )*0.8 )  then
			buffMySeltAndFriend( nFlag )
		end
	end			
end

function exeSpeekIntoCity()
	onRide()
	MoveTo( 180 , 89 , -1 , "" , 0 , true , _callbackInGoSence  )
	MoveToNPC( 180 , 89 , -1 , g_objCurrent[ 4 ] ,  0 , true , _callbackInGoSence )
	if isMsgItem( "公子爷也真是不知轻重" ) == true then		
		if isMsgItem2( " " , "从地道进入燕子坞" , 1 ) == 2 then
			QuestFrameOptionClicked( "从地道进入燕子坞", CMP_PART_MATCH )
			G_bInCity = 1
			G_nFighting = 0
			QuestFrameAcceptClicked()
			QuestFrameMissionClose()
		else
			QuestFrameMissionClose()	
			buffHp( 0 )
		end
	end
end

function _callback3()

	if getCurrentObjID() ~= -1 then
		return false
	end
	return true
end


local g_bHpSmall = fasle

function exeGoInCity()

	if isNearXY( 154 , 96 , 5 ) == true then
		if DataPool:IsTeamLeader() then	
			System:Sleep( 1000 ) 
			Player:EnableTeamFollow( true )
		end
	end

	MoveTo( 50 , 89 , -1 , "" , 0 , true , _callbackDie )
	if G_bInCity == 0 then
		return 0
	end
	if GetMount() ~= -1 then
		Dismount()
	end

	if Player:GetData( "HP" ) < Player:GetData( "MAXHP" )*0.3 then

		ShowMessage( "血量太低，加点再打吧 " )

		MoveTo( 50 , 89 , -1 , "" , 0 , true , _callbackDie )
		
		buffHp( 1 )

		System:Sleep( 1000 )
		if isTeamMemberLive() == 0 then
			MoveTo( 53 , 73 , -1 , "" , 0 , true , _callback1 )
			if getTargetID( g_objCurrent[ 6 ] ) == -1 then 
				System:Sleep( 1000 )
			end
			if getTargetID( g_objCurrent[ 6 ] ) == -1 and isTeamMemberLive() == 0 then
				onRide()
				if GetActiveSceneName() == "燕子坞" then
					if DataPool:IsTeamLeader() then
						goFollowMe()
					end
					MoveTo( 64 , 22 , -1 , "" , 0 , true , _callbackInSence )
					System:Sleep( 500 )
					EnterNewMap()
					System:Sleep( 1000 )				

				end
			end
		end

		while true do
			System:Sleep( 100 )
			if Player:GetData( "HP" ) >= Player:GetData( "MAXHP" )*0.5 then
				break
			end

			if getTargetID( g_objCurrent[ 1 ] ) ~= -1 then
				G_bInCity = 0
				break
			end
			if GetActiveSceneName() ~= "燕子坞" or G_bInCity == 0 then
				break
			end

			local idMonster = getTargetID( g_objCurrent[ 6 ] )

			if idMonster ~= -1 and Character:GetData( idMonster , "DIE" ) then
				break
			end

			if isTeamMemberLive() == 2 or isMissionFailed() then

				buffHp( 1 )
				if Player:GetData( "HP" ) < Player:GetData( "MAXHP" )*0.2 then
					onRide()
					if GetActiveSceneName() == "燕子坞" then
						PushDebugMessage( "任务失败!" )
						if DataPool:IsTeamLeader() then
							goFollowMe()
						end
						MoveTo( 64 , 22 , -1 , "" , 0 , true , _callbackInSence )
						System:Sleep( 500 )
						EnterNewMap()
						System:Sleep( 1000 )
					end
				end
			end
		end
	end

	if G_bInCity == 0 then
		return 0
	end

	local nWait = 1
	UpdateMsg( "WaitFriend2" )
	while true do
		System:Sleep( 100 )
		local nCount = DataPool:GetTeamMemCount()

		if nCount <= 0 then
			break
		end
		if getTargetID( g_objCurrent[ 1 ] ) ~= -1 then
			G_bInCity = 0
			break
		end
		buffHp( 1 )
		if getTargetID( g_objCurrent[ 6 ] ) ~= -1 then
			break
		end
		for i = 0 , DataPool:GetTeamMemCount() - 1  do
			local szName , idPlayer ,  _, idMap , xCurPos , yCurPos = DataPool:GetTeamMemInfo( i )

			if getTargetID( szName ) == -1 and szName ~= Player:GetName() then
				if IsMsgCold( "WaitFriend2" , 5000 ) == true then
					ShowMessage( "等待 %s !" , szName )
					UpdateMsg( "WaitFriend2" )
				end
				nCount = nCount - 1
			end
		end

		if nCount >= DataPool:GetTeamMemCount() then
			break
		else
			buffHp( 1 )
			
		end		
		if Player:GetData( "MEMPAI" ) == 4 then
			if nWait > 100 then
				break
			end
			buffHp( 1 )
	
		else
			if nWait > 150 then
				break
			end
		end

		nWait = nWait + 1
	end

	if G_bInCity == 0 then
		return 0
	end

	if GetActiveSceneName() == "燕子坞" then
		
		buffHp( 1 )

		MoveTo( 53 , 73 , -1 , "" , 0 , true , _callback1 )
		if getTargetID( g_objCurrent[ 6 ] ) == -1 then 
			System:Sleep( 1000 )
		end

		local idBoss = getTargetID( g_objCurrent[ 6 ] )

		if ( isTeamMemberLive() == 2 or isTeamMemberLive() == 0 ) and ( idBoss == -1 or Character:GetData( idBoss , "DIE" ) == true ) then
			onRide()
			if GetActiveSceneName() == "燕子坞" then
				if DataPool:IsTeamLeader() then
					goFollowMe()
				end

				MoveTo( 64 , 22 , -1 , "" , 0 , true , _callbackInSence )
				System:Sleep( 500 )
				EnterNewMap()
				System:Sleep( 1000 )
			end
		end
	end

end

local g_bCheckUseSkill = true


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 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 revivalGiveID( idHpSlow , szName )
	LockTarget( idHpSlow )	
	UseSkill_Target( "起死回生" , szName )
	DelMsg( "revival" )
	UpdateMsg( "revival" )
	
	while true do

		if Character:GetData( idHpSlow , "DIE" ) == false then
			ShowMessage( "%s 回生了" , szName )
			DelMsg( "revival" )
			G_bRevial = 1
			break
		end
		if getNumMemberDie() <= 0 then
			G_bRevial = 1
			break
		end
		if IsMsgCold( "revival" , 15000 ) == true then
			ShowMessage( "等待时间救人 %s 等于 0" , szName )
			DelMsg( "revival" )
			G_bRevial = 1
			break
		end
		UseSkill_Target( "起死回生" , szName )
		System:Sleep( 100 )
	end
end

function revivalFriend()
	AI:SetParameter( "保存" )
	AI:SetAI("杀怪") 
	AI:SetParameter( "初始化")
	AI:SetParameter( "拾取" , 0 )  
	AI:SetParameter( "地图" , -1) 
	local xNow, yNow = Player:GetPos()
	AI:SetParameter("坐标" , "添加" , xNow , yNow , 20 )
	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 revivalGiveFriend()

	local xPos , yPos = Player:GetPos()
	AI:SetAdv( "主防御圈" , xPos , yPos , 15 )
	AI:SetAI("高级战斗")
	Player:EnableTeamFollow( false )
	Dismount()
	AI:Start()
	local nMemPos = getNumMemberDie()
	if nMemPos > 0 then
		DelMsg( "revivalAll" )
	end
	while true do

		System:Sleep( 100 )
		local bPickUp = 0
		local bKillMonster = 0
		local bYinGuai = 0
		local bTeamFollow = 0
		local bTeamAttack = 0 
		local bStopAssistSelf = 1
		local bStopAssist = 1
		local bStopSpecialAttack = 1
		local bCallPet = 0

		if getNumMemberDie() <= 0 then
			G_bRevial = 1
			break
		end

		System:Sleep( 1000 )
		if IsMsgCold( "revivalAll" , nMemPos*15000 ) then
			G_bRevial = 1
			break
		end

		local idHpSlow , x , y , szName = getMemberDieID( 0 )
		if idHpSlow ~= -1 then
			MoveTo( x , y , -1 , "" , 0 , true , _callbackInSence )
			revivalGiveID( idHpSlow , szName )
		else
			G_bRevial = 1
			break
		end

		AI:SetParameter("运行参数" , "答题" , 1 ) 
		AI:SetParameter("拾取" , bPickUp ) 
		AI:SetParameter("运行参数" , "杀怪" , bKillMonster ) 
		AI:SetParameter("运行参数" , "引怪" , bYinGuai ) 
		AI:SetParameter("运行参数" , "组队跟随" , bTeamFollow ) 
		AI:SetParameter("运行参数" , "辅助攻击" , bTeamAttack ) 
		AI:SetParameter("运行参数" , "停止自身技能辅助" , bStopAssistSelf ) 
		AI:SetParameter("运行参数" , "停止辅助技能" , bStopAssist ) 
		AI:SetParameter("运行参数" , "停止特殊攻击技能" , bStopSpecialAttack ) 
		AI:SetParameter("运行参数" , "召唤珍兽" , bCallPet ) 
	end
	AI:Stop()
end

local bShowMsgSkill = false
local bShowMsgSkill2 = false
local nDemXuat = 1
local nDemDung = 1


function setVarMonster( monsterID )

	if monsterID == getTargetID( g_Monster[ 1 ].szName ) then
		g_bKillMonster = 1
		AI:SetAdv("指定击杀" , monsterID , g_Monster[ 1 ].szName )
	elseif monsterID == getTargetID( g_Monster[ 2 ].szName ) then
		g_bKillMonster = 1
		AI:SetAdv("指定击杀" , monsterID , g_Monster[ 2 ].szName )
	elseif monsterID == getTargetID( g_Monster[ 3 ].szName ) then
		g_bKillMonster = 1
		AI:SetAdv("指定击杀" , monsterID , g_Monster[ 3 ].szName )
	end

end

local g_bCheckStopKill = false
local g_bFirst = false

function exeSetSpecial( monsterID )
	local iPos ,_,_ = CheckBufferByName( monsterID , "斗转星移" )
	if -1 ~= iPos then

		if g_bFirst == false then
			Player:EnableTeamFollow( true )
			UpdateMsg( "Ussing_skill" )
			bCheck7s = true
			g_bCheckUseSkill = false

			g_bFirst = true
		end

		g_bCallPet = 0

		g_bStopSpecialAttack = 1
		g_bStopAssistSelf = 1
		g_bStopAssistSelf = 1

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

end


function exeSetSpecialPer( monsterID )
	
	local nHpBoss = Character:GetData( monsterID , "HP_PERCENT" )
	if nHpBoss <= 97.999992 then

		Player:EnableTeamFollow( true )
		UpdateMsg( "Ussing_skill" )
		bCheck7s = true

		g_bCallPet = 0
		g_bCheckUseSkill = false
		g_bStopSpecialAttack = 1
		g_bStopAssistSelf = 1
		g_bStopAssistSelf = 1
		g_bKillMonster = 0
	else
		g_bStopSpecialAttack = 0
		g_bStopAssistSelf = 0
		g_bStopAssistSelf = 0
		g_bKillMonster = 1
	end

end


function useItimeSpecial()
	UseItemById( 0x1cb5602 )
	System:Sleep( 500 )
end

function revivalFight()

	g_bPickUp = 0
	g_bKillMonster = 0
	g_bStopAssistSelf = 0
	g_bStopAssist = 1
	g_bStopSpecialAttack = 0
	g_bCallPet = 0

	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

end

local g_bUseSkill = false
local g_bFlagFirst = false


function exeFightMonsterSpecial( monsterID )

	AI:SetAI("高级战斗") 
	AI:SetParameter("地图" , -1 )
	AI:SetAdv("指定击杀" , monsterID , g_objCurrent[ 6 ] )
	AI:Start()
	G_nFighting = 1
	g_bPickUp = 0
	g_bKillMonster = 0
	g_bCallPet = 1

	UpdateMsg( "Ussing_item" )
	g_bCheckUseSkill = true
	local bRevialFriend = false
	local bCheck7s = true
	local bCheck10s = false
	local bCheckUse = false

	if DataPool:UserBag_CountItemByIDTable( 0x1cb5602 ) <= 0 then
		bCheckUse = true
	end
	while true do

		System:Sleep( 5 )

		if GetActiveSceneName() ~= "燕子坞" then			
			break
		end

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

		if bCheckUse == false then

			if DataPool:UserBag_CountItemByIDTable( 0x1cb5602 ) <= 0 then
				bCheckUse = true
			end	

			if Player:GetBuffByName( "就擒：无法使用" , 1 ) ~= -1 then
				useItimeSpecial()
			end
		end

		
		if g_bCheckUseSkill == true then
			if Character:GetData( monsterID , "HP_PERCENT" ) > 98.00 then

				exeSetSpecialPer( monsterID )
			else
				exeSetSpecial( monsterID )
			end
		else

			g_bFirst = false		

			if Player:GetData( "MEMPAI" ) == 4 then	

				if getNumMemberDie() > 0 then
					if IsMsgCold( "Ussing_skill" , 1500 ) == true then

						Player:EnableTeamFollow( false )
						if Player:GetData( "HP" ) >= Player:GetData( "MAXHP" )*0.5 then
							bRevialFriend = true
							PushDebugMessage( "在救人" , true )
							revivalFight()
						end
					end

				else
					if bRevialFriend == true then
						g_bYinGuai = 0
						g_bStopAssistSelf = 1
						g_bStopAssist = 1
						g_bStopSpecialAttack = 1

						g_bKillMonster = 0

						g_bCheckUseSkill = true

						bRevialFriend = false
						AI:Start()
					else
						if IsMsgCold( "Ussing_skill" , 1500 ) == true then
							Player:EnableTeamFollow( false )
							g_bStopSpecialAttack = 0
							g_bStopAssistSelf = 0
							g_bStopAssist = 0
							if Player:GetData( "MP" ) < Player:GetData( "MAXMP" )*0.3 then
								g_bCallPet = 1						
							end
						end

						if bCheck7s == true then

							if IsMsgCold( "Ussing_skill" , 7500 ) == true then
								local iPos ,_,_ = CheckBufferByName( monsterID , "斗转星移" )	
								if iPos == -1 then
									g_bKillMonster = 1
									bCheck7s = false
									bCheck10s = true
								end
							end
						end

						if bCheck10s == true then

							if IsMsgCold( "Ussing_skill" , 18500 ) == true then
--								Player:StopAction()
								g_bKillMonster = 0
								g_bStopSpecialAttack = 1
								g_bStopAssistSelf = 1
								g_bStopAssistSelf = 1
								local iPos ,_,_ = CheckBufferByName( monsterID , "斗转星移" )
								if iPos ~= -1 then
									Pet:Go_Relax( 0 )
									Player:EnableTeamFollow( true )
									bCheck7s = true
									bCheck10s = false
									UpdateMsg( "Ussing_skill" )
								end
						
								if IsMsgCold( "Ussing_skill" , 23000 ) == true then
									bCheck7s = true
									bCheck10s = false
									Player:EnableTeamFollow( false )
									g_bKillMonster = 1
									
									g_bStopSpecialAttack = 0
									g_bStopAssistSelf = 0
									g_bStopAssistSelf = 0
									g_bCheckUseSkill = true

								end								
							end

						end
					end
				end

			else

				if IsMsgCold( "Ussing_skill" , 1500 ) == true then
					Player:EnableTeamFollow( false )
					g_bStopSpecialAttack = 0
					g_bStopAssistSelf = 0
					g_bStopAssist = 0
					if Player:GetData( "MP" ) < Player:GetData( "MAXMP" )*0.5 then
						g_bCallPet = 1						
					end
				end
				if bCheck7s == true then

					if IsMsgCold( "Ussing_skill" , 7500 ) == true then
						local iPos ,_,_ = CheckBufferByName( monsterID , "斗转星移" )	
						if iPos == -1 then
							g_bKillMonster = 1
							g_bStopSpecialAttack = 0									
							g_bStopAssist = 0
							bCheck7s = false
							bCheck10s = true
						end
					end
				end
				if bCheck10s == true then

					if IsMsgCold( "Ussing_skill" , 18500 ) == true then
						g_bKillMonster = 0
						g_bStopSpecialAttack = 1
						g_bStopAssistSelf = 1
						g_bStopAssist = 1
						local iPos ,_,_ = CheckBufferByName( monsterID , "斗转星移" )
						if iPos ~= -1 then
							Pet:Go_Relax( 0 )
							Player:EnableTeamFollow( true )
							bCheck7s = true
							bCheck10s = false
							UpdateMsg( "Ussing_skill" )
						end
						
						if IsMsgCold( "Ussing_skill" , 23000 ) == true then
							
							g_bKillMonster = 1
							g_bStopSpecialAttack = 0
							g_bStopAssistSelf = 0
							g_bStopAssistSelf = 0
							g_bCheckUseSkill = true
							bCheck7s = true
							bCheck10s = false
						end								
					end
				end
			end

		end

		if g_nNoFight == 1 then
			g_bKillMonster = 0
			g_bYinshen = 1
			g_bStopAssist = 1
			g_mainguard_r = 15
			g_bCallPet = 0

			AI:SetAdv( "主防御圈" , 44 , 89 , g_mainguard_r ) 
		end

		AI:SetParameter("运行参数" , "答题" , 1 )
		AI:SetParameter("拾取" , g_bPickUp ) 
		AI:SetParameter("运行参数" , "杀怪" , g_bKillMonster )
		AI:SetParameter("运行参数" , "引怪" , 0 )
		AI:SetParameter("运行参数" , "组队跟随" , 0 )
		AI:SetParameter("运行参数" , "辅助攻击" , 0 ) --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("允许隐身" , 0 )

	end
	AI:Stop()
end


function monster3Die()
	G_nFighting = 0
	g_bKillMonster = 0
	G_bItemsInBoat = 0
end

function exeFightMonster( monsterID )

	AI:SetAI("高级战斗") 
	AI:SetParameter("地图" , -1 )
	G_nFighting = 1
	g_bPickUp = 0
	AI:SetParameter( "拾取" , g_bPickUp ) 
	AI:Start()
	while true do
		System:Sleep( 100 )	
	
		if GetActiveSceneName() ~= "燕子坞" then
			AI:Stop()
			break
		end

		g_bKillMonster = 1
		g_bCallPet = 1
		if G_bItemsInBoat == 1 then
			g_bKillMonster = 0
		end
		if monsterID ~= -1 then

			if Player:GetData( "HP" ) <= Player:GetData( "MAXHP" )*0.3 then
				g_bKillMonster = 0
				g_bYinshen = 1
				AI:SetAdv( "主防御圈" , 83 , 192 , g_mainguard_r ) 
			elseif Player:GetData( "HP" ) >= Player:GetData( "MAXHP" )*0.4 then
				G_bItemsInBoat = 0
				g_bKillMonster = 1
				g_bYinshen = 0
			end

			if Character:GetData( monsterID , "DIE" ) then
				break
			end
		else

			getMsgCheck()
			
			if Player:GetData( "HP" ) <= Player:GetData( "MAXHP" )*0.3 then
				g_bKillMonster = 0
				g_bYinshen = 1
				AI:SetAdv( "主防御圈" , 190 , 160 , g_mainguard_r ) 
			elseif Player:GetData( "HP" ) >= Player:GetData( "MAXHP" )*0.4 then
				g_bKillMonster = 1
				g_bYinshen = 0
				AI:SetAdv( "主防御圈" , 218 , 150 , g_mainguard_r ) 
			end
			local NpcID , x , y = getTargetPos( "钱宏宇" )
			if NpcID ~= -1 then
				g_FollowTarget = NpcID

				if GetDist( x , y , 190 , 162 ) <= 15 or GetDist( x , y , 183 , 170 ) <= 15 and G_speakNpc3OK == 0 then
					System:Sleep( 1000 )
					for i = 1 , 2 do
						MoveToNPC_ById( x , y , -1 , NpcID )
						System:Sleep( 200 )
						G_speakNpc3OK = 1
					end
					QuestFrameMissionClose()
					g_bKillMonster = 1
					AI:SetAdv( "主防御圈" , x , y , g_mainguard_r ) 
					AI:Start()
				end
				if G_speakNpc3OK == 1 then					
					if isMonsterLive() == false then
						
						G_bFight = 0
						G_nStep = 4
						onRide()
						Player:EnableTeamFollow( true )
						break
					end
				end

			end

			if G_nMsg9 == 1 or G_nMsg4 == 1 then 

				local idBoss = getTargetID( "鸠摩智" )
				if idBoss ~= -1 then
					if Character:GetData( idBoss , "DIE" ) == true then
						G_bFight = 0
						G_nStep = 4
						G_nFighting = 0
						AI:Stop()
						onRide()
						Player:EnableTeamFollow( true )
						break

					end
				else
					G_bFight = 0
					G_nStep = 4
					G_nFighting = 0
					AI:Stop()
					onRide()	
					Player:EnableTeamFollow( true )
					break
				end
			end
		end

		if getTargetID( g_objCurrent[ 4 ] ) ~= -1 then
			G_nStep = 4
			G_bFight = 0
			break
		end

		if g_nNoFight == 1 then
			g_bKillMonster = 0
			g_bYinshen = 1
			g_bStopAssist = 1
			g_bCallPet = 0
			g_mainguard_r = 15
			AI:SetAdv( "主防御圈" , 83 , 192 , g_mainguard_r ) 
		end
	
		AI:SetParameter("运行参数" , "答题" , 1 )
		AI:SetParameter("拾取" , g_bPickUp ) 
		AI:SetParameter("运行参数" , "杀怪" , g_bKillMonster )
		AI:SetParameter("运行参数" , "引怪" , g_bYinGuai )

		AI:SetParameter("运行参数" , "组队跟随" , g_bTeamFollow )
		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 isFullBag( nID )
	
	local nResult = PlayerPackage:GetEmptySlot( nID )

	if nResult == 0 then
		return true
	end

	return false
end

function getNumBag( nID )
	
	return PlayerPackage:GetEmptySlot( nID )
end

function goNpc2( bFlag )
	if GetMount() == -1 then
		Player:EnableTeamFollow( false )
		DoMount( true )
		System:Sleep( 1000 )
	end
	if bFlag then
		Player:EnableTeamFollow( true )
		MoveTo( 80 , 203 , -1 , "" , 0 , true , _callbackFollow )
	else
		Player:EnableTeamFollow( false )
		MoveTo( 80 , 203 , -1 , "" , 0 , true , _callbackDisFollow )
	end
end

function goNpc3( bFlag )
	if GetMount() == -1 then
		Player:EnableTeamFollow( false )
		DoMount( true )
		System:Sleep( 1000 )
	end
	if bFlag then
		Player:EnableTeamFollow( true )
		MoveTo( 218 , 152 , -1 , "" , 0 , true , _callbackFollow )
	else
		Player:EnableTeamFollow( false )
		MoveTo( 218 , 152 , -1 , "" , 0 , true , _callbackDisFollow )
	end
end

function goNpc4( bFlag )

	if GetMount() == -1 then
		Player:EnableTeamFollow( false )
		DoMount( true )
		System:Sleep( 1000 )
	end
	if bFlag then
		
		Player:EnableTeamFollow( true )
		MoveTo( 180 , 89 , -1 , "" , 0 , true , _callbackFollow )
	else
		Player:EnableTeamFollow( false )
		MoveTo( 180 , 89 , -1 , "" , 0 , true , _callbackDisFollow )
	end

end

function speekGetMonster()
	while true do
		if getCurrentMonsterID() ~= -1 then
			break
		end
		G_nFighting = 1
		QuestFrameOptionClicked( "战斗吧" , CMP_PART_MATCH )
		System:Sleep( 100 )
	end
end

function exeSpeekNPCBoat()
	local NpcID = getTargetID( g_objCurrent[ 2 ] )	
	if NpcID ~= -1 then
		MoveToNPC_ById( 80 , 203 , -1 , NpcID , 0 , _callbackInGoSence )
		System:Sleep( 500 )
		if DataPool:IsTeamLeader() or isTeamMemberLive() == 2 then

			if isMsgItem2( "感谢你冒险前来通知我" , "没想到你的武功如此之高强" , 0 ) == 1 then
				if GetMount() ~= -1 then
					Dismount()
				end
				speekGetMonster()
				QuestFrameMissionClose()
				return 1
			elseif isMsgItem2( "感谢你冒险前来通知我" , "没想到你的武功如此之高强" , 0 ) == 2 then
				QuestFrameMissionClose()
				return 0
			else
				return -1
			end

		else

			if isMsgItem2( "感谢你冒险前来通知我" , "没想到你的武功如此之高强" , 0 ) == 1 then
				Dismount()
				G_nFighting = 1
				PushDebugMessage( "等待队长开怪" , true )
				QuestFrameMissionClose()
				return 1
			elseif isMsgItem2( "感谢你冒险前来通知我" , "没想到你的武功如此之高强" , 0 ) == 2 then
				QuestFrameMissionClose()
				return 0
			else
				return -1
			end
		end
	else
		return -1
	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 isConditionTeam2()
	local nCount = DataPool:GetTeamMemCount()
	for i = 0 , DataPool:GetTeamMemCount() - 1  do
		local szName , idPlayer ,  _, idMap , xCurPos , yCurPos = DataPool:GetTeamMemInfo( i )
		if szName ~= Player:GetName() and getTargetID( szName ) == -1 then
			nCount = nCount - 1
		end
	end

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

	return false
end



function isTeamNgaMi()

	for i = 0 , DataPool:GetTeamMemCount() - 1  do
		local szName , idPlayer , nMemPai , idMap , xCurPos , yCurPos, _ , _ , nMp , nMaxMp = DataPool:GetTeamMemInfo( i )
		if nMemPai == 4 and nMp > nMaxMp*0.1 then
			return true
		end
	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 isMissionFailed()
	
	local nCount = 0
	for i = 0 , DataPool:GetTeamMemCount() - 1  do		
		local szName , idPlayer , nMemPai , idMap , xCurPos , yCurPos , nHp , nMaxHp = DataPool:GetTeamMemInfo( i )
		if nHp <= nMaxHp*0.3 then
			nCount = nCount + 1
		end
	end
	if nCount >= DataPool:GetTeamMemCount() and isTeamNgaMi() == false then
		return true
	end
	return false
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() ==  "太湖" and getTargetID( "李纲" ) ~= -1 then
			if DataPool:IsTeamLeader() then
				local szWait = string.format( " %s 不在附近 " , szName )
				Shout( "%s 速速集合，准备进入副本了" , szWait )
				mySleep( 5 )
			else
				if IsMsgCold( "WaitFriend" , 5000 ) == true then
					ShowMessage( " %s 不在附近 !" , szName )
					UpdateMsg( "WaitFriend" )
				end
			end	
			nCount = nCount - 1
		end
	end

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

function speekNpcMission()

	Player:EnableTeamFollow( true )
	MoveTo( 70 , 118 , -1 , "" , 0 , true , _callbackOutSence )
	local NpcID , x , y = getTargetPos( "李纲" )
	if NpcID == -1 then
		return 1
	end
	if GetActiveSceneName() == "燕子坞" then
		return 1
	end
	Player:EnableTeamFollow( true )
	if y == 119 then
		MoveToNPC( 70 , 118 , 0x04 , "李纲" , 0 , true , _callbackOutSence )		
	else
		MoveToNPC( 70 , 119 , 0x04 , "李纲" , 0 , true , _callbackOutSence )
	end


	System:Sleep( 1000 )

	QuestFrameOptionClicked( "讨伐燕子坞" , CMP_PART_MATCH)

	if isMsgItem( "目前您队伍中不满足条件的队员情况如下" ) then

		local bStopMission = false
		local nCounMember = DataPool:GetTeamMemCount() - 1
		for i = 0 , nCounMember do
			if GetActiveSceneName() == "燕子坞" 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 已刷3次了" , szName )
				Shout( "team" , szMsg )
				mySleep( 5 )
			end
		end

		if bStopMission then
			Shout( "team" , "队长已刷三次" )		
--			Shout( "team" , "队长已刷3次" )		
			ShowMessage( "脚本完毕" )
			System:Sleep( 1000 )
			return 0
		end
	end
	QuestFrameMissionClose()
	return 1
	
end

function isMsgMemberStop()
	local szMsgStop = string.format( "%s 刷3次了" , Player:GetName() )
	if isMsgTeam( szMsgStop ) then		
		return true
	end
	return false
end

function waitFollowGoBoat()
	ShowMessage( "等待队长" )
	while true do
		if getTargetID( g_objCurrent[ 2 ] ) ~= -1 then
			Player:EnableTeamFollow( false )
			G_nStep = 2
			break
		end
		if isTeamMemberLive() == 0 then
			Player:EnableTeamFollow( false )
			break
		end
		System:Sleep( 100 )
	end
end

function teamLeaderGoNpc3()

	while true do
		if GetMount() ~= -1 then
			break
		end
		Player:EnableTeamFollow( false )
		DoMount( true )
		System:Sleep( 1000 )
	end

	Player:EnableTeamFollow( true )

	if gscript_check( 1 , 0 , 1 ) == 1 then
		Player:EnableTeamFollow( true )
		MoveTo( 184 , 176 , -1 )
		memberGoNpc3()
		teamLeaderSpeekNpc3( false )
--		Player:EnableTeamFollow( true )
		G_nStep = 4
	else
		MoveTo( 184 , 176 , -1 , "" , 0 , true , _callbackFollow )
	end
	Player:EnableTeamFollow( false )
end


function setFightMonster3()
	if GetMount() ~= -1 then
		Dismount()
	end
	Player:EnableTeamFollow( false )
	g_mainguard_r = 15
	g_bPickUp = 0

	local NpcID = getTargetID( g_objCurrent[ 3 ] )
	if NpcID ~= -1 then
		g_FollowTarget = NpcID
	else
		g_FollowTarget = -1
	end
	AI:SetAdv( "主防御圈" , 218 , 150 , g_mainguard_r )	
	exeFightMonster( -1 )
end

function memberGoNpc3()
	onRide()
	if DataPool:IsTeamLeader() == true then
		Shout( "team" , "队员去卡怪" )
		PushDebugMessage( "等待队员卡怪" , true )
	else
		PushDebugMessage( "等队长带走" , true )
	end
	if g_bQuai == 1 then
		while true do
			System:Sleep( 100 )
	
			if isMonsterLive() then
				G_nFighting = 1
				break
			end

			if isTeamLeaderLive() == 0 then
				G_nFighting = 1
				break
			end
		end
		setFightMonster3()

	else


			if g_bShowMsg == false and isTeamMemberLive() == 1 then
--				Shout( "team" , "挂中" )
				g_bShowMsg = true
			end

		while true do
			System:Sleep( 100 )
			getMsgCheck()
			local NpcID , xNpc , yNpc = getTargetPos( g_objCurrent[ 3 ] )
			if NpcID ~= -1 then
				G_bSeenNpc3 = 1
				G_nStep = 3
			end
			if getTargetID( g_objCurrent[ 2 ] ) ~= -1 and isTeamLeaderLive() == 0 then
				break
			end
			if G_bSeenNpc3 == 0 then
				if isMonsterLive() then
					local nWaitNpc = 1
					while true do
						if getTargetID( g_objCurrent[ 3 ] ) ~= -1 then
							G_bSeenNpc3 = 1
							break
						end
						if nWaitNpc > 80 then
							if getTargetID( g_objCurrent[ 3 ] ) == -1 then
								G_nMsgMonster3 = 1
								G_bFight = 1
								setFightMonster3()
							end
							break
						end
						System:Sleep( 100 )
						nWaitNpc = nWaitNpc + 1
					end
				end
			end

			if isTeamMemberLive() == 0 then
				G_nStep = 3
				break
			end
			if getTargetID( g_objCurrent[ 4 ] ) ~= -1  or G_nMsg9 == 1 or G_nMsg4 == 1 then
				G_nStep = 4
				break
			end
			if DataPool:IsTeamLeader() == false then
				if G_nMsg2 == 1 and G_bSeenNpc3 == 0 then
					G_nMsgMonster3 = 1
					G_bFight = 1
					setFightMonster3()
					break
				end
			end

			if DataPool:IsTeamLeader() == true then
				local xPos , yPos = Player:GetPos()
				if G_bSeenNpc3 == 1 and ( GetDist( xNpc , yNpc , 190 , 162 ) <= 15 or GetDist( xNpc , yNpc , 183 , 170 ) <= 15 ) then
					break
				end
			end
		end
	end
	Player:EnableTeamFollow( false )
end

function exeObjCurrent1()

	if getTargetID( g_objCurrent[ 2 ] ) == -1 then	
		onRide()
	end

	if DataPool:IsTeamLeader() or isTeamLeaderLive() == 2 then
		Player:EnableTeamFollow( true )
		goNpc2( true )
		G_nStep = 2
		System:Sleep( 2000 )
		Player:EnableTeamFollow( false )
	else
--		Player:EnableTeamFollow( true )
--		if isTeamLeaderLive() == 1 then
--			waitFollowGoBoat()	
--		else
			Player:EnableTeamFollow( false )
			goNpc2( false )
			G_nStep = 2
--		end
	end
end

function onRide()
	if GetMount() == -1 then
		Player:EnableTeamFollow( false )
	end
	while true do
		if getTargetID( "孟婆" ) ~= -1 then
			break
		end
		if GetMount() ~= -1 then
			break
		end
		DoMount( true )
		System:Sleep( 1000 )
	end
end

function OnPickupName( szNameItem )
	if isConditionBag() == fasle then
		G_bPickup = 1
		return 0
	end
	AI:SetParameter( "保存" )
	AI:SetAI("杀怪") 
	AI:SetParameter( "初始化")
	AI:SetParameter( "拾取" , "指定" , szNameItem ) 
	AI:SetParameter( "地图" , -1) 
	local xNow, yNow = Player:GetPos()
	AI:SetParameter("坐标" , "添加" , xNow , yNow , 20 )

	local nNumItems = DataPool:UserBag_CountItemByName( szNameItem )
	UpdateMsg( "pickupOnBoat" )

	AI:StartEx( 0 )

	AI:SetParameter("运行参数" , "杀怪" , 0 ) 
	AI:SetParameter("运行参数" , "停止辅助技能" , 1 ) 

	AI:StartEx( 1 )
	while true do 

		System:Sleep( 100 )
		if IsMsgCold( "pickupOnBoat" , 6000 ) == true then
			G_bPickup = 1
			break
		end
		if DataPool:UserBag_CountItemByName( szNameItem ) > nNumItems then
			G_bPickup = 1
			break
		end
		if getCurrentMonsterID() == -1 then
			G_bPickup = 1
			break
		end

	end
	
	AI:Stop()
	AI:SetParameter("恢复")
end

function exeObjCurrent2()

	local monsterID = getCurrentMonsterID()
	if  monsterID ~= -1 and Character:GetData( monsterID , "DIE" ) == false then
		G_nFighting = 1
	end
	local idCheck , x , y , szNameMonster = getTargetPosID( monsterID  )
	local indexMonster
	if idCheck ~= -1 then
		indexMonster = getCurrentMonsterIndex( szNameMonster )
		if Character:GetData( monsterID , "DIE" ) == false then	
			isConditionBag()
			if DataPool:IsTeamLeader() then			
--				local szMsg = string.format( "只打 %s" , tostring( szNameMonster ) )
--				Shout( "near" , szMsg )
			end
		end
	end

	if monsterID == -1 then

		if exeSpeekNPCBoat() == 0 then
			
			if  getCurrentMonsterID() == -1 and ( isTeamLeaderLive() == 0 or isTeamMemberLive() == 2 or DataPool:IsTeamLeader() ) then
				G_bItemsInBoat = 0
				G_nFighting = 0
				G_nStep = 3
			end
		end
	elseif 	monsterID ~= -1 and idCheck ~= -1 and Character:GetData( monsterID , "DIE" ) == false then

		g_bYinshen = 0
		G_nFighting = 1
		g_bKillMonster = 1
		g_bCallPet = 1

		if G_bItemsInBoat == 1 then
			g_mainguard_r = 6
			g_FollowTarget = -1
			g_bCallPet = 0
			Pet:Go_Relax( 0 )
			AI:SetAdv( "主防御圈" , 83 , 192 , g_mainguard_r )
			g_bYinshen = 1
			G_nFighting = 1

			exeFightMonster( monsterID )
		else
			g_mainguard_r = 10
			AI:SetAdv( "主防御圈" , 80 , 203 , g_mainguard_r )
			g_bCallPet = 1
			g_FollowTarget = -1			
			g_bCallPet = 1			

			setVarMonster( monsterID )
			exeFightMonster( monsterID )
		end
	elseif 	Character:GetData( monsterID , "DIE" ) == true then
		if indexMonster == 3 then
			monster3Die()
			if DataPool:IsTeamLeader() then
				if isTeamMemberLive() == 1 then
--					Shout( "team" , "捡好悲酥清风解药再走" )
					mySleep( 2 )
				end
			end
			if getCurrentMonsterID() ~= -1 and G_bPickup == 0 then

				if getNumBag( 0 ) > 5 then
					OnSweepBattleField( 1 )
				else
					OnPickupName( "悲酥清风解药" )
				end
			end
			G_nStep = 3
			G_nFighting = 0
		
		end
	end

end


function exeObjCurrent3()

	if getNumMemberDie() > 0 then

		if DataPool:IsTeamLeader() then				
			if Player:GetData( "MEMPAI" ) == 4 and getNumMemberDie() > 0 then
				revivalFriend()
			else
				PushDebugMessage( "等待救人" , true )
				teamLeaderWaitRevival()
			end
		else
			if Player:GetData( "MEMPAI" ) == 4 and getNumMemberDie() > 0 then	
				revivalFriend()
			end
		end
	else
		onRide()
	end
	if DataPool:IsTeamLeader() or isTeamMemberLive() == 2 then
--[[
		local idMember = isMemTeamNearXY( 80 , 203 , 15 )
		if idMember ~= -1 and isSeeMemberID( idMember ) ~= 1 then
			onRide()
			MoveTo( 80 , 203 , -1 )
			Player:EnableTeamFollow( true )
		end
]]--
		if gscript_check( 1 , 0 , 1 ) == 1 then
			teamLeaderGoNpc3()
		else

			local NpcID , x , y = getTargetPos( g_objCurrent[ 3 ] )
			if NpcID == -1 then
				teamLeaderGoNpc3()
				if isTeamMemberLive() == 1 then
					Shout( "team" , "我去卡怪" )
					System:Sleep( 2000 )
				end
				goNpc3( false )
				System:Sleep( 1000 )
				if getTargetID( g_objCurrent[ 3 ] ) == - 1 then
					if isMonsterLive() then
						G_nStep = 4
					end
				end
			else
				if G_bFight == 0 then
					exeFollowMonster( false , false )
				end
			end
		end

	else
		if gscript_check( 1 , 0 , 1 ) == 1 then
			if G_bFight == 0 then
				exeFollowMonster( true , true )
			end
		else
			if isTeamLeaderLive() == 1 then
				memberGoNpc3()
			elseif isTeamLeaderLive() == 0 then

				MoveTo( 184 , 176 , -1 , "" , 0 , true , _callbackFollow )

				if isTeamMemberLive() == 0 then
	
					MoveTo( 190 , 162 , -1 , "" , 0 , true , _callbackFriend )

				end

				if isTeamMemberLive() == 1 then					
					memberGoNpc3()
				else
					G_nStep = 4
				end
			end
			waitTeamLeaderFollowMonster()	

		end
	end

end

function exeInCity()

	if Player:GetData( "HP" ) < Player:GetData( "MAXHP" )*0.3 then
		ShowMessage( "血量太低，加点再打吧 " )
		MoveTo( 50 , 89 , -1 , "" , 0 , true , _callbackDie )
		System:Sleep( 1000 )
		if G_bInCity == 0 then
			return 0
		end
		if isTeamMemberLive() == 0 then
			MoveTo( 53 , 73 , -1 , "" , 0 , true , _callback1 )
			if getTargetID( g_objCurrent[ 6 ] ) == -1 then 
				System:Sleep( 1000 )
			end
			if getTargetID( g_objCurrent[ 6 ] ) == -1 and isTeamMemberLive() == 0 then
				onRide()
				if GetActiveSceneName() == "燕子坞" then

					if DataPool:IsTeamLeader() then
						goFollowMe()
					end

					MoveTo( 64 , 22 , -1 , "" , 0 , true , _callbackInSence )
					System:Sleep( 500 )
					EnterNewMap()
					System:Sleep( 1000 )
					G_nStep = 0
				end
			end
		end
		while true do
			System:Sleep( 100 )
			if Player:GetData( "HP" ) >= Player:GetData( "MAXHP" )*0.5 then
				break
			end
			if getTargetID( g_objCurrent[ 1 ] ) ~= -1 then
				G_bInCity = 0
				ShowMessage( "死亡了" )
				break
			end
			if GetActiveSceneName() ~= "燕子坞" or G_bInCity == 0 then
				break
			end
			local idMonster = getTargetID( g_objCurrent[ 6 ] )
			if idMonster ~= -1 and Character:GetData( idMonster , "DIE" ) then
				break
			end
			if isTeamMemberLive() == 2 or isMissionFailed() then
				onRide()
				if GetActiveSceneName() == "燕子坞" then
					if DataPool:IsTeamLeader() then
						goFollowMe()
					end
					MoveTo( 64 , 22 , -1 , "" , 0 , true , _callbackInSence )
					System:Sleep( 500 )
					EnterNewMap()
					System:Sleep( 1000 )
					G_nStep = 0
				end
				break
			end
		end
	else
		if GetActiveSceneName() == "燕子坞" then				
			MoveTo( 53 , 73 , -1 , "" , 0 , true , _callback1 )
			if getTargetID( g_objCurrent[ 6 ] ) == -1 then
				System:Sleep( 1000 )
			end
		end
	end


end

function waitFollowTeamLeader()
	Player:EnableTeamFollow( true )
	if g_bShowMsgFollow == false  then --isTeamMemberLive() == 1
--		Shout( "team" , "带一下" )
		g_bShowMsgFollow = true
	end

	mySleep( 2 )
	PushDebugMessage( "等队长带走" , true )
	while true do

		System:Sleep( 1000 )
		getMsgCheck()
		if isTeamMemberLive() == 0 then
			G_nStep = 3
			Player:EnableTeamFollow( false )
			break
		end
		if getTargetID( g_objCurrent[ 4 ] ) ~= -1 or G_nMsg9 == 1 or G_nMsg4 == 1 then
			G_nFighting = 0
			G_nStep = 4
			break
		end
	end

end

function waitTeamLeaderFollowMonster()
	if G_bFigting2 == 0 then
		if  isTeamMemberLive() == 1 then 
			waitFollowTeamLeader() 
		elseif isMemTeamNearXY( 180 , 89 , 15 ) ~= -1 or isTeamMemberLive() == 2 or isTeamMemberLive() == 0 then
			G_nStep = 4
		end
	end

end

function exeFollowMonster( bFlag , bMember )
	if G_bFight == 0 then
		Player:EnableTeamFollow( false )
		onRide()
		if bFlag then
			goNpc3( false )
		end
		local NpcID , x , y = getTargetPos( g_objCurrent[ 3 ] )
		if NpcID ~= -1 then
			MoveTo( x , y , -1 , "" , 0 , true , _callbackDisFollow )
			MoveToNPC_ById( x , y , -1 , NpcID )
			if isMsgItem2( "难得呼延将军如此器重在下" , "我已经立下了军令状" , 0 ) == 1 then
				G_nMsgMonster3Follow = 1
				Pet:Go_Relax( 0 )
				onRide()
				QuestFrameOptionClicked( "战斗吧", CMP_PART_MATCH )
				QuestFrameMissionClose()
				Player:EnableTeamFollow( false )
				goLoop( bMember )
			else
				if isMonsterLive() then
					if G_nMsgMonster3Follow == 1 then
						G_bNoMyself = 0
					else
						G_bNoMyself = 1
					end

					G_nStep = 4
				end
			end
			QuestFrameMissionClose()
		else
			G_nStep = 4

		end
	else
		setFightMonster3()
	end

end



function isConditionBag()
	if isFullBag( 0 ) then
		if DataPool:UserBag_CountItemByIDTable( 0x1cb5602 ) > 0 then
			g_nNumItems = DataPool:UserBag_CountItemByIDTable( 0x1cb5602 )
			
			return true
		else
			PushDebugMessage( "背包满" , true )
			return false
		end
	else
		return true
	end
end

function isCallPetBuff()

	local petID = Player:GetFightingPetID()
	if petID ~= -1 then
		local iPos , _ , _ = CheckBufferByName( petID, "瘟疫" )
		if -1 ~= iPos then
			return 0
		end
	else
		return 0
	end

	if Player:GetData( "MP" ) < Player:GetData( "MAXMP" )*0.2 then
		if IsSkillSetUse( 696 ) or IsSkillSetUse( 697 ) then
			return 1
		end	
	end
end

function exeFigtQuai()

	if DataPool:IsTeamLeader() then
		if getTargetID( g_objCurrent[ 2 ] ) ~= -1 then
			onRide()
			Player:EnableTeamFollow( true )

			MoveTo( 184 , 176 , -1 , "" , 0 , true , _callbackInGoSence )

			
			System:Sleep( 1000 )

		else
			MoveTo( 218 , 152 , -1 , "" , 0 , true , _callbackInGoSence )
			System:Sleep( 1000 )
		end		
		local NpcID , x , y = getTargetPos( g_objCurrent[ 3 ] )
		if NpcID ~= -1 then
			MoveToNPC_ById( x , y , -1 , NpcID , 0 , _callbackInGoSence )
			if isMsgItem2( "难得呼延将军如此器重在下" , "我已经立下了军令状" , 0 ) == 1 then
				G_nMsgMonster3 = 1
				QuestFrameOptionClicked( "战斗吧", CMP_PART_MATCH )
				QuestFrameMissionClose()
				Player:EnableTeamFollow( false )
			end
			QuestFrameMissionClose()
		end
		Player:EnableTeamFollow( false )
		if G_nMsgMonster3 == 1 then
			MoveTo( 218 , 152 , -1 , "" , 0 , true , _callbackInGoSence )
			setFightMonster3()
		end
	else
		MoveTo( 218 , 152 , -1 , "" , 0 , true , _callbackInGoSence )
		setFightMonster3()
	end

end

function teamLeaderWaitRevival()
	local nMemberDie = getNumMemberDie()
	if nMemberDie > 0 then
		Shout( "team" , "救人" )
		UpdateMsg( "revivalWait" )
		while true do
			if GetActiveSceneName() ~= "燕子坞" then
				break
			end
			if getNumMemberDie() <= 0 then
				break
			end
			if IsMsgCold( "revivalWait" , nMemberDie*15000 ) == true or getNumMemberDie() <= 0 then
				break
			end
			PushDebugMessage( "有人死亡" , true )
			System:Sleep( 100 )										
		end
	end
end
function isConditionTimes( szNameMission )

	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


local g_bShowMsg = false

function _SMain(...)
		

	if Player:GetData( "LEVEL" ) < 60 then
		ShowMessage( "等级 >= 60 " )
		System:Sleep( 1000 )
		return
	end
	local NpcID , _ , _ = getTargetPos( "李纲" )
	if GetActiveSceneName() ~= "燕子坞" and DataPool:GetTeamMemCount() < 3 and NpcID == -1 then
		ShowMessage( "队伍人数不足 3 人！ " )
	end

	g_bQuai = gscript_check( 0 , 0 , 1 )

	if gscript_check( 0 , 0 , 1 ) == 1 and gscript_check( 1 , 0 , 1 ) == 1 then
		ShowMessage( "不能同时选择2个" )
		ShowMessage( "不能同时打勾2个" )
		System:Sleep( 1000 )
		return
	end

	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 ~= nil and tostring( szCurrentScene ) ~= "燕子坞" then
			if GetQuestTimesInfo( "讨伐燕子坞" , 10000 , tostring( g_szNameSence[ 2 ] ) ) == 0 then
				Player:EnableTeamFollow( false )
				ShowMessage( "今天任务已经完成" )
				System:Sleep( 1000 )
				System:UpdateRecord( "燕子坞" )
				return				
			end

			clearMsg()

			G_bOut = 1
			G_bInCity = 0
			G_nStep = 1
			G_nMsg9 = 0
			G_nMsg7 = 0
			G_nMsg4 = 0
			G_nMsg2 = 0
			G_nMsgMonster3 = 0
			G_nFighting = 0	
			G_bItemsInBoat = 0
			G_bSeenNpc3 = 0
			G_bFight = 0
			G_nMsgMonster3Follow = 0
			G_bNoMyself = 0
			G_speakNpc3OK = 0
			G_bFinish = 0
			G_bRevial = 0
			G_bPickup = 0
			if GetMount() == -1 then
				Player:EnableTeamFollow( false )
				DoMount( true )
				System:Sleep( 1000 )
			end

			MoveTo( 76 , 122 , 0x04 )
			local szHereSence = GetActiveSceneName()
			if DataPool:GetTeamMemCount() < 3 and szHereSence ~= nil and tostring( szHereSence ) == tostring( g_szNameSence[ 1 ] ) then 
				ShowMessage("队伍人数不足 3 人！")	
				System:UpdateRecord( "燕子坞" )
				System:Sleep( 1000 )
				return
			end

			local nWaitBag = 1

			while true do
				if GetActiveSceneName() == "燕子坞" then
					break
				end
				if nWaitBag > 3 then
					break
				end
				if isConditionBag() then
					break
				end				
				System:Sleep( 1000 )
				nWaitBag = nWaitBag + 1
			end

			buffHp( 2 )

			UpdateMsg( "WaitFriend" )

			while true do
				local szHereSence2 = GetActiveSceneName()
				if szHereSence2 ~= nil and tostring( szHereSence2 ) == "燕子坞" then
					break
				else
					if isConditionTeam() then
						break
					end

				end
				System:Sleep( 1000 )
			end

			Player:EnableTeamFollow( true )

			if DataPool:IsTeamLeader() then

				if speekNpcMission() == 0 then
					Player:EnableTeamFollow( false )
					System:UpdateRecord( "燕子坞" )
					return
				end
--			else
--				PushDebugMessage( "等待队长点击进入副本" , true )
			end

		elseif 	szCurrentScene ~= nil and tostring( szCurrentScene ) == "燕子坞" then

				if G_bOut == 1 then
					clearMsg()
					G_nMemberExe = G_nMemberExe + 1
					G_bOut = 0
				end

				local nHp = Player:GetData( "HP" )
				local nMaxHp =  Player:GetData( "MAXHP" )
				local nObjCurrent = getCurrentObjID()
	
				if nObjCurrent == 4 then
					G_bFinish = 0
					AI:Stop()
					G_nStep = 4
					G_bInCity = 0
				elseif ( nObjCurrent == 5 and nHp > 0 ) or ( nObjCurrent == 6 and nHp > 0 ) then
					G_nStep = 4
					G_bInCity = 1
				elseif nObjCurrent == 1 then
					G_bInCity = 0
					G_bFinish = 0
					if G_nStep == 4 then
						if G_nFighting == 1 then						
							G_nFighting = 0	
							AI:Stop()
						end
					end
				end
				local idMonsster = getCurrentMonsterID()
				if idMonsster ~= -1 and Character:GetData( idMonsster , "DIE" ) == false then
					G_bFinish = 0
					G_nStep = 2
				end
				if G_nStep == 2 and nHp < nMaxHp*0.3 then
					G_bFinish = 0
					G_bItemsInBoat = 1
				end

				if Player:GetData( "HP" ) <= 0 then
					continue
				end

				if G_nStep == 1 then

					exeObjCurrent1()

				elseif G_nStep == 2 then
	
					if G_bItemsInBoat == 1 and getCurrentMonsterID() == -1 then
						onRide()
						if DataPool:IsTeamLeader() or isTeamMemberLive() == 2 then

							MoveTo( 83 , 192 , -1 , "" , 0 , true , _callbackInGoSence )
						else
							MoveTo( 83 , 192 , -1 , "" , 0 , true , _callbackFriend )
						end
					end
					exeObjCurrent2()
	
				elseif G_nStep == 3 then

					if gscript_check( 0 , 0 , 1 ) == 1 then	

						exeFigtQuai()
					else
						if G_bFight == 1 then
							setFightMonster3()
						else
							exeObjCurrent3()
						end
					end

				elseif G_nStep == 4 then
					local monsterID , x , y = getTargetPos( g_objCurrent[ 6] )
					if monsterID == -1 then

						if G_bFinish == 1 then
						
							onRide()
							if DataPool:IsTeamLeader() then
								mySleep( 3 )	
								goFollowMe()
							end
							MoveTo( 64 , 22 , -1 , "" , 0 , true , _callbackInSence )
							System:Sleep( 500 )
							EnterNewMap()
							System:Sleep( 1000 )

						else								

							if G_bInCity == 1 then								
								if G_nFighting == 0 then

									if isNearXY( 50 , 89 , 20 ) == false then
										onRide()
										Player:EnableTeamFollow( true )	
									end
									exeGoInCity()
								else
									exeInCity()
								end	
							else
								G_bFinish = 0
								if DataPool:IsTeamLeader() then
									onRide()
									if G_bNoMyself == 1 then
										MoveTo( 180 , 89 , -1 , "" ,  0 , true , _callbackInGoSence )
									end
									if isMemTeamNearXY( 184 , 181 , 15 ) ~= -1 and gscript_check( 1 , 0 , 1 ) == 0 then
										if isNearXY( 180 , 89 , 15 ) == true then
											Player:EnableTeamFollow( false )	
										end
										MoveTo( 184 , 181 , -1 , "" ,  0 , true , _callbackFollowMe1 )
										Player:EnableTeamFollow( true )	
									end
								end
								exeSpeekIntoCity()
								Player:EnableTeamFollow( false )
							end
							
						end
					else

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

							OnSweepBattleField( 1 )

							if getNumMemberDie() > 0 then

								if DataPool:IsTeamLeader() == true then
									if Player:GetData( "MEMPAI" ) == 4 then									
										revivalFriend()
	
									else
										teamLeaderWaitRevival()
									end
								else
									if Player:GetData( "MEMPAI" ) == 4 then					
										revivalFriend()
									end
								end
	
							end

							G_bFinish = 1

							if DataPool:IsTeamLeader() then
								mySleep( 3 )	
								goFollowMe()
							end
							Player:EnableTeamFollow( true )
							MoveTo( 64 , 22 , -1 , "" , 0 , true , _callbackInSence )
							System:Sleep( 500 )
							EnterNewMap()
							System:Sleep( 1000 )

						elseif Character:GetData( monsterID , "DIE" ) == false then
							G_bFinish = 0
							Player:EnableTeamFollow( false )
							if GetMount() ~= -1 then
								Dismount()
								System:Sleep( 1000 )
							end
							g_mainguard_r = 15
							AI:SetAdv( "主防御圈" , 53 , 73 , g_mainguard_r )
							exeFightMonsterSpecial( monsterID )

						end

					end

				end

		end
	end
	LogChatMsg( -1 )
end

_SMain()