
--[[
[author]
class=1
attr=1
descript = 默认第二步不杀鸠摩智，由队长卡怪。当勾选谨慎杀慕容复的选项时，脚本会等待慕容复主动使用斗转星移技能，并在之后的9秒安全时间内进行攻击，确保不会被慕容复的技能反死。
chk0str = 不卡怪，直接打
chk1str = 谨慎杀慕容复
]]--

script_name = "讨伐燕子坞"
local nFubenMapId = 0xec
local nSetNoFight = 0
local nSetKillJmz = 0
local nSetSafeKill = 0

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


local function reset_yzw()
	l_state_yzw = {}
	l_state_yzw["段延庆"] = BOSS_RECORD_INIT
	l_state_yzw["鸠摩智"] = BOSS_RECORD_INIT
	l_state_yzw["慕容复"] = BOSS_RECORD_INIT
	l_state_yzw["援军"] = 0
	l_state_yzw["卡位"] = 0
end

if l_state_yzw == nil then
	reset_yzw()
end

local l_boss = {
	["岳老三"] = INVALID_ID ,
	["云中鹤"] = INVALID_ID ,
	["叶二娘"] = INVALID_ID ,
	["段延庆"] = INVALID_ID ,
	["慕容复"] = INVALID_ID ,
	["钱宏宇"] = INVALID_ID ,
	["呼延庆"] = INVALID_ID ,
	["呼延豹"] = INVALID_ID ,
	["华赫艮"] = INVALID_ID ,
	["木人傀儡"] = INVALID_ID , 
	["吐蕃喇嘛"] = INVALID_ID , 
	["鸠摩智"] = INVALID_ID , 
	["青城派弟子"] = INVALID_ID ,
	["姚伯当"] = INVALID_ID ,
	["司马林"] = INVALID_ID ,
	["秦家寨亲兵"] = INVALID_ID
}

local l_npc = {
	["呼延豹"] = {x=79,y=202,qs="战斗吧！",name="呼延豹",next="钱宏宇"} ,
	["钱宏宇"] = {x=216,y=149,qs="战斗吧！",name="钱宏宇",next="华赫艮"} ,
	["华赫艮"] = {x=182,y=89,qs="从地道进入燕子坞",name="华赫艮",next="慕容复"} ,
	["慕容复"] = {x=52,y=72,qs="",name="慕容复",next="慕容复"}
}

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

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

function set_boss_record( name , npc , die )

	if name == "段延庆" then
		l_state_yzw["卡位"] = 0
		l_state_yzw["鸠摩智"] = BOSS_RECORD_INIT
		l_state_yzw["慕容复"] = BOSS_RECORD_INIT
		if npc == false then
			if die == true then
				l_state_yzw["段延庆"] = BOSS_RECORD_DEAD
			else
				l_state_yzw["段延庆"] = BOSS_RECORD_MET
			end
		else
			l_state_yzw["段延庆"] = BOSS_RECORD_INIT
		end
	elseif name == "木人傀儡" or name == "吐蕃喇嘛" or name == "青城派弟子" or name == "姚伯当" or name == "司马林" or name == "秦家寨亲兵" then
		l_state_yzw["慕容复"] = BOSS_RECORD_INI
		l_state_yzw["段延庆"] = BOSS_RECORD_DEAD
		if l_state_yzw["鸠摩智"] ~= BOSS_RECORD_DEAD then
			l_state_yzw["鸠摩智"] = BOSS_RECORD_MET
		end
	elseif name == "鸠摩智" then
		if l_state_yzw["慕容复"] == BOSS_RECORD_INIT then
			l_state_yzw["慕容复"] = BOSS_RECORD_INIT
			l_state_yzw["段延庆"] = BOSS_RECORD_DEAD
			if npc == false then
				if die == true then
					l_state_yzw["鸠摩智"] = BOSS_RECORD_DEAD
				else
					l_state_yzw["鸠摩智"] = BOSS_RECORD_MET
				end
			else
				l_state_yzw["卡位"] = 0
				l_state_yzw["鸠摩智"] = BOSS_RECORD_INIT
			end
		end
	elseif name == "慕容复" then
		l_state_yzw["鸠摩智"] = BOSS_RECORD_DEAD
		l_state_yzw["段延庆"] = BOSS_RECORD_DEAD
		if die == true then
			l_state_yzw["慕容复"] = BOSS_RECORD_DEAD
		else
			l_state_yzw["慕容复"] = BOSS_RECORD_MET
		end
	end
end

function SelectFuben2()
	if nSetKillJmz == 1 then
		return OnFuben2 , "鸠摩智"
	elseif DataPool:IsTeamLeader() == true then
		if l_state_yzw["卡位"] == 0 then
			return OnFuben2NoKill , "鸠摩智"
		end
	end
	return OnFuben2NoKillMember , "鸠摩智"
end

function UpdateData()

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

	ResetBoss()

	local n = EnumObject(true)

	for i=0 , n - 1 do
		local idObj, classObj, nameObj, xObj, yObj = EnumObject(false, i)
		if idObj == INVALID_ID then
			continue
		end

		if classObj == "MON" then
			if l_boss[nameObj] ~= nil then
				if Character:GetData(idObj, "DIE") == false then
					l_boss[nameObj] = idObj
				end
			end
			set_boss_record( nameObj , false , Character:GetData(idObj, "DIE") )
		elseif classObj == "NPC" then
			if l_boss[nameObj] ~= nil then
				l_boss[nameObj] = idObj
			end
		end
	end

	local isNext,szMsg = GetDebugMessage()
	while isNext == 1 do
		DbgPrintf("dbg msg %s" , tostring(szMsg))
		if szMsg ~= nil then
			if string.find( szMsg , "有高手要刺杀呼延豹将军") then
				set_boss_record("段延庆" , true , false)
			elseif string.find( szMsg , "协助钱宏宇将军阻击反贼援军") then
				set_boss_record( "鸠摩智" , true , false)
			elseif string.find( szMsg , "燕子坞援军已经赶") then
				set_boss_record( "鸠摩智" , false , false)
				local _,_,vTick = string.find( szMsg ,"第(%d+)拨燕子坞援军已经赶" )
				l_state_yzw["援军"] = tonumber(vTick)
				if l_state_yzw["援军"] == 9 and nSetKillJmz ~= 1 then
					set_boss_record( "慕容复" , false , false )
				end
			elseif string.find( szMsg , "深入燕子坞内部") then
				set_boss_record( "慕容复" , false , false )
			elseif string.find( szMsg , "慕容复已被打败" ) then
				set_boss_record( "慕容复" , false , true )
			end				
		end
		isNext,szMsg = GetDebugMessage()
	end

	--让队员提前做好准备
	if l_boss["岳老三"] ~= INVALID_ID or l_boss["云中鹤"] ~= INVALID_ID or l_boss["叶二娘"] ~= INVALID_ID or l_boss["段延庆"] ~= INVALID_ID then
		return OnFuben1 , "段延庆"
	elseif l_boss["慕容复"] ~= INVALID_ID then
		return process_boss_kill , "慕容复"
	elseif l_boss["木人傀儡"] ~= INVALID_ID or l_boss["吐蕃喇嘛"] ~= INVALID_ID or l_boss["鸠摩智"] ~= INVALID_ID or l_boss["秦家寨亲兵"]~= INVALID_ID then
		return SelectFuben2()
	elseif l_boss["青城派弟子"] ~= INVALID_ID or l_boss["姚伯当"] ~= INVALID_ID or l_boss["司马林"] ~= INVALID_ID then
		return SelectFuben2()
	end

	return nil , 0
end

function _callback(fX, fY, nScene)

	--赶往下个npc 进行对话
	if IsFuben() == false then
		return false
	end

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

	return false
end

function _callback_withoutfollow(fX, fY, nScene)

	--赶往下个npc 进行对话
	if IsFuben() == false then
		return false
	end

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

	return false
end

function _breakmove_infuben(fX, fY, nScene)

	if IsFuben() == false then
		return true
	end

	return false
end

function move_npc( p_npc )

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

		UpdateData()
		--if l_boss[p_npc] == INVALID_ID then
			--npc不存在
		--	ShowMessage("对象不存在")
		--	return move_npc , l_npc[p_npc].next
		--end
	end

	if p_npc == "慕容复" then
		MoveInMap( l_npc[p_npc].x , l_npc[p_npc].y , nFubenMapId , 2 , _callback )
	elseif p_npc == "钱宏宇" then
		MoveToNPC( l_npc[p_npc].x , l_npc[p_npc].y , nFubenMapId , l_npc[p_npc].name , true , false , _callback_withoutfollow )
	else
		MoveToNPC( l_npc[p_npc].x , l_npc[p_npc].y , nFubenMapId , l_npc[p_npc].name , true , false , _callback )
	end
	return move_npc , p_npc
end

function process_npc_t( p_npc )

	--只处理npc对话情况
	local s_npc = p_npc

	if p_npc == "钱宏宇" then
		DoMount(true)
		Pet_Relax()		
	end
	
	Player:EnableTeamFollow(false)

	local vSelect = 0

	while true do 

		System:Sleep(500)

		local result , v_npc = UpdateData()
		if result ~= nil then
			--有其他事情要做
			QuestFrameMissionClose()
			return result , v_npc
		end

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

		local list_count , npc_id = DataPool:GetNPCEventList_Num()
		if npc_id ~= l_boss[s_npc] then
			return nil , nil 
		elseif s_npc == "华赫艮" then
			if Player:CompareLocalArea( nFubenMapId , 51, 74) == true then
				return nil , nil 
			end
		elseif p_npc == "钱宏宇" then
			if l_state_yzw["鸠摩智"] ~= BOSS_RECORD_INIT then
				return nil,nil
			end
		end

		if list_count < 1 then
			-- 无选项
		else
			local t_click = ""
			for i=0, list_count - 1 do
				local strType,_,_,_,szText = DataPool:GetNPCEventList_Item(i)
				if strType == "text" then
					if string.find(szText , "不能让任何一个敌人站着通过我这里" ) then
						QuestFrameMissionClose()
						return SelectFuben2
					elseif string.find(szText , "我们才能击退他们的援军") then
						QuestFrameMissionClose()
						return nil,nil
					end						
				elseif strType == "id" then
					if string.find(szText , "战斗吧！") then
						t_click = "战斗吧！"
					elseif string.find(szText , "从地道进入燕子坞") then
						t_click = "从地道进入燕子坞"
					end
				end
			end
			
			if t_click == "" then
				--没有合适选项
				QuestFrameMissionClose()
				return nil,nil
			else
				vSelect = vSelect + 1
				QuestFrameOptionClicked( t_click , CMP_PART_MATCH)
			end	
		end
	end

end

function OnFuben1()

	AI:SetAI("杀怪")
	AI:SetParameter("初始化")
	AI:SetParameter( "拾取" , "指定" , "悲酥清风解药" ) 
	AI:SetParameter("地图", nFubenMapId )
	AI:SetParameter("坐标", "添加", 80, 204 ) 
--[[
	AI:SetParameter("优先目标", "添加", "岳老三" ) 
	AI:SetParameter("优先目标", "添加", "云中鹤" ) 
	AI:SetParameter("优先目标", "添加", "叶二娘" ) 
	AI:SetParameter("优先目标", "添加", "段延庆" ) 
]]--	
	AI:StartEx(0)


	AI:SetParameter("运行参数" , "清除杀怪循环次数")
	AI:SetParameter("运行参数" , "只拾取BOSS掉落" , 1)

	if nSetNoFight > 0 and DataPool:GetTeamMemCount() > 0 and DataPool:IsTeamLeader() == false then
		AI:SetParameter("运行参数" ,"引怪", 0 )
		AI:SetParameter("运行参数" ,"杀怪", 0 )
		AI:SetParameter("运行参数" ,"组队跟随", 1)
		AI:SetParameter("运行参数" ,"辅助攻击", 0)
		AI:SetParameter("运行参数" ,"停止辅助技能", 1)
	end

	AI:StartEx(1)

	while true do 

		System:Sleep(500)

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

		if IsFuben() == false then
			break
		end

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

	AI:Stop()

	OnSweepBattleField(1)
	
	return nil,nil	
end

function _CallBack_MoveQhy( fX , fY , nScene )

	if IsFuben() == false then
		return false
	end
	local result , param = UpdateData()
	if l_state_yzw["鸠摩智"] ~= BOSS_RECORD_MET then
		return false
	end
	if l_boss["钱宏宇"] == INVALID_ID then
		return false
	end

	return true
end

function _CheckQhyFight()
	
	if l_boss["钱宏宇"] == INVALID_ID then
		return INVALID_ID
	end

	local vQhyLockId = GetObjectAttackId( l_boss["钱宏宇"] , 5000 )
	local vCheckId , _ , _ , vDie = FindObjectByID( vQhyLockId , "怪物" )
	if vCheckId == INVALID_ID or vDie == 1 then
		return INVALID_ID
	end

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

	return vQhyLockId
end

function _CallBack_MoveCheckFight( fX , fY , nScene )

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

	if result == nil then
		return true
	end

	return false
end

function OnFightQhy( vFirstX , vFristY )

	--时刻注意钱宏宇的情况,准备对话卡位
	AI:SetAdv( "清除参数" )

	AI:SetAdv( "主防御圈" , vFirstX , vFristY , 15 )
	AI:SetAI("高级战斗") 

	AI:SetParameter("地图" , nFubenMapId ) 
	
	AI:StartEx(0)
	AI:SetParameter("运行参数" , "杀怪" , 0 )
	AI:StartEx(1)

	local vPreLockID = INVALID_ID

	while true do

		local bPickUp = 0
		local bKillMonster = 0
		local bYinGuai = 0
		local bTeamFollow = 0 
		local bTeamAttack = 0 
		local bStopAssistSelf = 0 
		local bStopAssist = 0
		local bStopSpecialAttack = 0 
		local bCallPet = 1
		local bYinshen = 0 
		local bFixPlace = 1
		local bSpeedUp = 0

		System:Sleep(500)

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

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

		if l_state_yzw["鸠摩智"] ~= BOSS_RECORD_MET then
			break
		end

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

			local vKillMon , vName = GetQhyNearMon( vFirstX , vFristY )
			if vKillMon == INVALID_ID then
				AI:SetAdv("指定击杀" , -1 , "" )
			else
				bKillMonster = 1
				AI:SetAdv("指定击杀" , vKillMon , vName )
			end
		else
			AI:SetAdv("指定击杀" , -1 , "" )
			bKillMonster = 1
		end

		if bKillMonster == 0 then
			break
		end

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

		AI:SetAdv("允许隐身" , bYinshen )
		AI:SetAdv("不允许出防御圈" , bFixPlace )
	end

	AI:Stop()		
end

function OnFuben2KillClear( )

	AI:SetAI("杀怪")
	AI:SetParameter("初始化")
	AI:SetParameter( "拾取" , 0 ) 
	AI:SetParameter("地图", nFubenMapId )
	AI:StartEx(0)

	AI:SetParameter("运行参数" , "清除杀怪循环次数")
	AI:SetParameter("运行参数" ,"组队跟随", 0)

	AI:StartEx(1)

	while true do 

		System:Sleep(500)

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

		if IsFuben() == false then
			break
		end

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

		if l_state_yzw["鸠摩智"] ~= BOSS_RECORD_MET then
			break
		end
		
	end

	AI:Stop()		
end

function OnFuben2KillObject( vLockId )

	AI:SetAI("杀怪")
	AI:SetParameter("初始化")
	AI:SetParameter( "拾取" , 0 ) 
	AI:SetParameter("地图", nFubenMapId )
	local vId,vX,vY,_,_,_,_,vName = FindObjectByID( vLockId , "怪物")
	if vId == INVALID_ID then
		return 
	end

	AI:SetParameter("坐标", "添加", vX, vY , 15 ) 
	AI:SetParameter("目标", "添加", tostring(vName) , tonumber(vLockId) ) 
	
	AI:StartEx(0)


	AI:SetParameter("运行参数" , "清除杀怪循环次数")
	AI:SetParameter("运行参数" ,"组队跟随", 0)

	AI:StartEx(1)

	while true do 

		System:Sleep(500)

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

		if IsFuben() == false then
			break
		end

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

		if l_state_yzw["鸠摩智"] ~= BOSS_RECORD_MET then
			break
		end

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

	AI:Stop()		
end

function OnFuben2()

	AI:SetAI("杀怪")
	AI:SetParameter("初始化")
	AI:SetParameter( "拾取" , 1 ) 
	AI:SetParameter("地图", nFubenMapId )
	AI:SetParameter("坐标", "添加", 216 , 149) 
	
	AI:StartEx(0)


	AI:SetParameter("运行参数" , "清除杀怪循环次数")
	AI:SetParameter("运行参数" , "只拾取BOSS掉落" , 1)

	if nSetNoFight > 0 and DataPool:GetTeamMemCount() > 0 and DataPool:IsTeamLeader() == false then
		AI:SetParameter("运行参数" ,"引怪", 0 )
		AI:SetParameter("运行参数" ,"杀怪", 0 )
		AI:SetParameter("运行参数" ,"组队跟随", 1)
		AI:SetParameter("运行参数" ,"辅助攻击", 0)
		AI:SetParameter("运行参数" ,"停止辅助技能", 1)
	end

	AI:StartEx(1)

	while true do 

		System:Sleep(500)

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

		if IsFuben() == false then
			break
		end

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

	AI:Stop()

	OnSweepBattleField(1)
	
	return nil,nil	
end

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

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

	return false
end

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

	return true
end

function GetQhyNearMon( vX , vY )

	local vQhyId = FindObjectByName("钱宏宇" , "NPC")
	local vPlayerId = Player:GetData("ID")

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

	local vResultID = INVALID_ID 
	local vResultName = ""
	local vResultDist = 99999

	local n = EnumObject(true)

	for i=0 , n - 1 do
		local idObj, classObj, nameObj, xObj, yObj = EnumObject(false, i)
		if idObj == INVALID_ID then
			continue
		end

		if classObj == "MON" then
			if Character:GetData(idObj, "DIE") == false then
				--怪在6 格范围外, 无怪攻击钱 , 无怪攻击玩家 , 钱没有攻击怪物
				local vMonAttackId = GetObjectAttackId( idObj , 5000 )
				if vMonAttackId ~= INVALID_ID then
					if vPlayerId == vMonAttackId or vQhyId == vMonAttackId then
						return idObj , nameObj
					end					
				end

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

--[[
A-B 两点之间巡逻
 A->B 只拖怪,到达终点后等待 钱 
 B->A 遇怪清理

判定 钱 卡位成功的条件

1,钱在区域内 , 周围无怪
2,钱在区域内 , 周围有怪 , 怪在6 格范围外, 无怪攻击钱 , 无怪攻击玩家 , 钱没有攻击怪物
]]--

function OnFuben2NoKill()

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

	while true do

		System:Sleep(500)

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

			if vQhyId ~= INVALID_ID and IsQhyInArea(vQhyX , vQhyY) == true then
				if vResult == nil then
					--return
					ShowMessage("成功将 钱宏宇 卡位!")
					QuestFrameMissionClose()
					l_state_yzw["卡位"] = 1
				else
					local vSuccess = false
					local vQhyLockId = GetObjectAttackId( l_boss["钱宏宇"] , 5000 )
					local vCheckId , _ , _ , vDie = FindObjectByID( vQhyLockId , "怪物" )
					if vCheckId == INVALID_ID or vDie == 1 then
						vQhyLockId = INVALID_ID
					end
					if vPreLockId ~= vQhyLockId or UI:IsWindowShow("Quest") == false then
						--对话npc
						--MoveToNPC_ById( vQhyX , vQhyY , nFubenMapId , l_boss["钱宏宇"] , false , _CallBack_MoveQhy )
						ShowMessage("对话 钱宏宇 准备卡位")
						local vNear = MoveTo_MovingObj( l_boss["钱宏宇"] , "NPC" , 3 , nFubenMapId , _CallBackTalkQhy )
						if vNear == true and UI:IsWindowShow("Quest") == false then
							QuestFrameSpeak( l_boss["钱宏宇"] , "" , 500)
						end 
					elseif GetQhyNearMon(vQhyX , vQhyY) ~= INVALID_ID then
						--执行清怪流程(关注npc)
						ShowMessage("开始清场")
						OnFightQhy( vQhyX , vQhyY )
					else
						--npc卡位成功,如何处理??
						vSuccess = true
						if IsMsgCold("YZW_KAWEI" , 30*1000) then
							ShowMessage("成功将 钱宏宇 卡位!")
							l_state_yzw["卡位"] = 1
						end
					end

					vPreLockId = vQhyLockId

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

						if isNearPlayer == true then
							MoveInMap( vPos[vRunPos].x , vPos[vRunPos].y , nFubenMapId , 2 )
						else
							PushDebugMessage("等待 怪物 进入预定位置..." , 1)
							continue
						end
					end

					vRunPos = vRunPos + 1
					if vRunPos > vPosSize then
						UpdateMsg("YZW_WAIT_QHY")
					end
				else
					-- B->A 点遇怪即杀
					MoveInMap( vPos[vRunPos].x , vPos[vRunPos].y , nFubenMapId , 2 , _CallBack_MoveCheckFight )
					if UpdateData() == OnFuben2NoKill then
						OnFuben2KillClear()
						continue
					end
					
					if vRunPos > 0 then
						vRunPos = vRunPos - 1
					else
						vRunPos = 0
						vRunFlag = 0 
					end
				end
			elseif IsMsgCold("YZW_WAIT_QHY" , 10*1000) then
				--引怪失败
				ShowMessage("卡位失败,先清场")
				vRunPos = vPosSize
				vRunFlag = 1
				--执行清怪流程
				OnFuben2KillClear()
			else
				PushDebugMessage( "等待 钱宏宇 进入预定位置..." , 1 )
			end		
		end
	end
end

function OnFuben2NoKillMember()

	AI:SetAI("杀怪")
	AI:SetParameter("初始化")
	AI:SetParameter( "拾取" , 1 ) 
	AI:SetParameter("地图", nFubenMapId )
	AI:SetParameter("坐标", "添加", 180 , 89 , 5 ) 
	
	AI:StartEx(0)

	AI:SetParameter("运行参数" , "清除杀怪循环次数")
	AI:SetParameter("运行参数" , "只拾取BOSS掉落" , 1)

	if nSetNoFight > 0 and DataPool:GetTeamMemCount() > 0 and DataPool:IsTeamLeader() == false then
		AI:SetParameter("运行参数" ,"引怪", 0 )
		AI:SetParameter("运行参数" ,"杀怪", 0 )
		AI:SetParameter("运行参数" ,"组队跟随", 1)
		AI:SetParameter("运行参数" ,"辅助攻击", 0)
		AI:SetParameter("运行参数" ,"停止辅助技能", 1)
	end

	AI:StartEx(1)

	while true do 

		System:Sleep(500)

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

		if IsFuben() == false then
			break
		end

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

	AI:Stop()

	return nil,nil		
end

function process_boss_kill( npc )

	AI:SetAdv( "清除参数" )

	AI:SetAdv( "主防御圈" , 52 ,  72 , 15 )
	AI:SetAI("高级战斗") 

	AI:SetParameter("地图" , nFubenMapId ) 
	
	AI:StartEx(0)
	AI:SetParameter("运行参数" , "杀怪" , 0 )
	AI:StartEx(1)

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

	while true do

		local bPickUp = 1
		local bKillMonster = 1
		local bYinGuai = 0
		local bTeamFollow = 0 
		local bTeamAttack = 0 
		local bStopAssistSelf = 0 
		local bStopAssist = 0
		local bStopSpecialAttack = 0 
		local bCallPet = 1
		local bYinshen = 0 
		local bFixPlace = 0
		local bSpeedUp = 0

		local FingerAttack = -1
		local FingerAttackName = ""
		local FollowTarget = -1

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

		System:Sleep(500)

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

		if IsFuben() == false then
			break
		end

		result , r_param = UpdateData()

		if result ~= process_boss_kill then
			if npc == "慕容复" and l_state_yzw["慕容复"] == BOSS_RECORD_DEAD then
				break 
			else
					--人物必须走到挂机点
				if IsDestination( 52 ,  72 , nFubenMapId , 3 ) == false then
					
				end
			end
		end

		if bYinBoss == true and INVALID_ID ~= GetObjectAttackId( l_boss["慕容复"] , 15000 ) then
			if IsDestination( 52 ,  72 , nFubenMapId , 3 ) == true then
				bYinBoss = false
				AI:SetAdv("主防御圈半径" , 15)
			else
				AI:SetAdv("主防御圈半径" , 3)
				bFixPlace = 1	
			end
		end

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

		local teamfollow = false
		if l_boss["慕容复"] ~= INVALID_ID then
			AI:SetAdv("指定击杀" , 0 , "慕容复" )
			if CheckBufferByName( l_boss["慕容复"] , "斗转星移" )  ~= INVALID_ID then
				DelMsg("YZW_MRF_DZXY")
				bKillMonster = 0
				bCallPet=0
				PushDebugMessage("慕容复使用了技能 斗转星移,暂停攻击!" , 1 )
				teamfollow = true
				vDZXYCount = vDZXYCount + 1
			elseif bWaitMember == false then
				UpdateMsg("YZW_MRF_DZXY" , 1)
			end
		else
			AI:SetAdv("指定击杀" , -1 , "" )
			DelMsg("YZW_MRF_DZXY")
		end

		--组队跟随超过3秒,自动退出组队跟随
		if teamfollow ~= pre_teamfollow then
			pre_teamfollow = teamfollow
			if teamfollow == true then
				Player:EnableTeamFollow(true)
				UpdateMsg("YZW_TEAMFOLLOW")
			else
				Player:EnableTeamFollow(false)
				DelMsg("YZW_TEAMFOLLOW")
			end
		else
			if IsMsgCold("YZW_TEAMFOLLOW" , 3000) == true then
				Player:EnableTeamFollow(false)
			end
		end

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

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

		--启动前10秒,如果队员不齐 且 未开怪 且 慕容复可见
		if bWaitMember == true then
			if IsMsgCold("YZW_WAIT_MEMBER" , 10*1000) == true then
				ShowMessage("等待队员超时,直接开怪")
				bWaitMember = false
			else
				local nTeamCount = DataPool:GetTeamMemCount()
				if nTeamCount == 0 then
					ShowMessage("没有组队,直接开怪")
					bWaitMember = false
				else
					local nNearCount = 0
					for i=0,nTeamCount do
						local vName,vId,_,vMapId,vX,vY = DataPool:GetTeamMemInfo(i)
						if vId == Player:GetData("ID") then
							nNearCount = nNearCount + 1
						elseif vId ~= INVALID_ID then
							if nFubenMapId == vMapId and GetDist( 52 ,  72 , vX , vY ) < 10 then
								nNearCount = nNearCount + 1
							end
							if l_boss["慕容复"] ~= INVALID_ID and GetObjectAttackId( vId , 15000 ) == l_boss["慕容复"] then
								bWaitMember = false
								ShowMessage("队员 %s 已经开怪,停止等待" , tostring(vName))
							end					
						end
					end
					if nNearCount == nTeamCount then
						ShowMessage("队员全部就位 %d,开始战斗" , nNearCount)
						bWaitMember = false
					elseif GetObjectAttackId( l_boss["慕容复"] , 15000 ) ~= INVALID_ID then
						ShowMessage("慕容复已锁定目标 %d,开始战斗" , tonumber(GetObjectAttackId( l_boss["慕容复"] , 15000 )))
						bWaitMember = false
					end
				end	
			end
		end

		if bWaitMember == true then
			PushDebugMessage("等待所有队员到齐后开怪..." , 1)
			bKillMonster = 0
		end

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

		AI:SetAdv("跟随对象" , FollowTarget , 3 )
		AI:SetAdv("允许隐身" , bYinshen )
		AI:SetAdv("不允许出防御圈" , bFixPlace )

		if bWaitMember == false and nXBQFCount > 0 and IsMsgCold("YZW_USEITEM_XBQF" , 3000) == true and Player:GetBuffByName( "抵抗：就擒" , 1 ) == INVALID_ID then
			nXBQFCount = DataPool:UserBag_CountItemByIDTable( 0x1cb5602 )
			UseItemById( 0x1cb5602 )
			UpdateMsg("YZW_USEITEM_XBQF")
		end
	end

	AI:Stop()

	OnSweepBattleField()

	--正常返回 nil,""
	return nil , nil
end

function is_process_npc()

	if UI:IsWindowShow("Quest") == false then 
		return false , ""
	end
	
	if l_boss["呼延豹"] ~= INVALID_ID then
		return  true , "呼延豹"
	elseif l_boss["钱宏宇"] ~= INVALID_ID then
		return true , "钱宏宇"
	elseif l_boss["华赫艮"] ~= INVALID_ID then
		return true , "华赫艮"
	end

	return false , ""
end

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

function FindProcess()

	if l_state_yzw["慕容复"] == BOSS_RECORD_DEAD then
		return process_exit, ""
	elseif l_state_yzw["鸠摩智"] == BOSS_RECORD_DEAD then
		if Player:CompareLocalArea( nFubenMapId , 51, 74) == true then
			return move_npc , "慕容复"
		else
			return move_npc , "华赫艮"
		end
	elseif l_state_yzw["鸠摩智"] == BOSS_RECORD_MET then
		--已经出现援军提示后
		return SelectFuben2()
	elseif l_state_yzw["段延庆"] == BOSS_RECORD_DEAD then
		if nSetKillJmz == 1 then
			return move_npc , "钱宏宇"
		elseif DataPool:IsTeamLeader() == true then
			return move_npc , "钱宏宇"
		else
			return move_npc , "华赫艮"
		end 	
	end

	return move_npc , "呼延豹"
end

local function OnCommonMission()

	if Player:GetData( "LEVEL" ) < 60 then
		ShowMessage( "等级小于 60 级,不能执行燕子坞脚本" )
		return
	end

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

	if Player:CompareLocalArea( nFubenMapId , 51, 74) == true then
		l_state_yzw["段延庆"] = BOSS_RECORD_DEAD
		l_state_yzw["鸠摩智"] = BOSS_RECORD_DEAD
	end

	while true do

		System:Sleep(500) 

		if IsFuben() == true then

			check_quest = false
			
			local process , param = UpdateData()

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

		else
			--在野外
			reset_yzw()

			if check_quest == false then
				if GetQuestTimesInfo("讨伐燕子坞") == 0 and IsFuben()== false then
					ShowMessage("今日已经完成燕子坞副本!")
					break 
				end
			end

			check_quest = true 

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

			if DataPool:IsTeamLeader() == true or DataPool:GetTeamMemCount() == 0 then
				if DataPool:GetTeamMemCount() < 3 and IsFuben()== false then
					ShowMessage("队伍人数小于 3 人,不能执行 燕子坞副本")
					return
				end
			end

			if DataPool:IsTeamLeader() == true then

				QuestFrameOptionClicked( "讨伐燕子坞" , CMP_PART_MATCH)
				System:Sleep(2000)

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

				if strType == "text" then
					if string.find(szText, "不满足条件") then
						ShowMessage("队伍条件不足,终止脚本运行!")
						break	-- 退出循环，结束脚本
					end
				end
			end
		end

	end
end

function GetMissionComplete()

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

	return INVALID_ID
end

local function OnXuKongMission()

	if Player:GetData( "LEVEL" ) < 60 then
		g_XukongRet = 1
		ShowMessage( "等级小于 60 级,不能执行燕子坞脚本" )
		return
	end

	script_name = "挑战虚空燕子坞"

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

	if Player:CompareLocalArea( nFubenMapId , 51, 74) == true then
		l_state_yzw["段延庆"] = BOSS_RECORD_DEAD
		l_state_yzw["鸠摩智"] = BOSS_RECORD_DEAD
	end

	while true do

		System:Sleep(500) 

		if IsFuben() == true then

			check_quest = false
			
			local process , param = UpdateData()

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

		else
			--在野外
			reset_yzw()

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

				if DataPool:IsTeamLeader() == true or DataPool:GetTeamMemCount() == 0 then
					if DataPool:GetTeamMemCount() < 3 and IsFuben()== false then
						g_XukongRet = 1
						ShowMessage("队伍人数小于 3 人,不能执行 燕子坞副本")
						return
					end
				end

				if DataPool:IsTeamLeader() == true then
					QuestFrameOptionClicked( "挑战虚空燕子坞" , CMP_PART_MATCH)

					System:Sleep(2000)

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

					if strType == "text" then
						if string.find(szText, "不满足条件") then
							ShowMessage("队伍条件不足,终止脚本运行!")
							break	-- 退出循环，结束脚本
						end
					end
				end
			else
				break
			end

			if check_quest == false then
				if GetQuestTimesInfo("讨伐燕子坞") == 0 and IsFuben()== false then
					ShowMessage("今日已经完成燕子坞副本!")
					break 
				end
			end

			check_quest = true 

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

			if DataPool:IsTeamLeader() == true or DataPool:GetTeamMemCount() == 0 then
				if DataPool:GetTeamMemCount() < 3 and IsFuben()== false then
					ShowMessage("队伍人数小于 3 人,不能执行 燕子坞副本")
					return
				end
			end
		end

	end

end

local function _SMain(...)

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

	LogChatMsg( 0 )
	LogChatMsg( 1 )	

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

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

	LogChatMsg( -1 )
end

AI:SetMode("暂停复活地图" , nFubenMapId)
_SMain()
AI:SetMode("暂停复活地图" , -1)
