

--[[

[author]
class=1
attr=1
descript = 凤鸣王陵副本，每天能进2次  

]]--


script_name = "凤鸣王陵"

local nFubenMapId = 0x258
local nSetNoFight = 0
local l_bf_real = INVALID_ID
local l_finish = false
local l_start_wait = 0

local l_dead_xl = false
local l_dead_bf = false
local l_dead_zd = false
local l_open_boss = true

local l_boss_bf = {
	[0] = { id = INVALID_ID , x = 0 , y = 0 , dis = 0 } , 
	[1] = { id = INVALID_ID , x = 0 , y = 0 , dis = 0 } , 
	[2] = { id = INVALID_ID , x = 0 , y = 0 , dis = 0 } , 
	[3] = { id = INVALID_ID , x = 0 , y = 0 , dis = 0 }
}

function ResetBf()
	l_bf_real = INVALID_ID
	for i = 0 , 3 do 
		l_boss_bf[i].id = INVALID_ID
		l_boss_bf[i].x = 0
		l_boss_bf[i].y = 0
	end
end

function SetBfBorn( id , x , y )
	for i = 1 , 3 do 
		if l_boss_bf[i].id == INVALID_ID then
			l_boss_bf[i].id = id
			l_boss_bf[i].x = x
			l_boss_bf[i].y = y
			l_boss_bf[i].dis = GetDist( x , y , l_boss_bf[0].x , l_boss_bf[0].y )
			if i == 3 then
				l_bf_real = GetRealBf()
			end
		elseif l_boss_bf[i].id == id then
			return
		end
	end
end

function GetRealBf()

	local vDis = 9999
	local vID = INVALID_ID

	for i = 1 , 3 do 
		if l_boss_bf[i].id == INVALID_ID then
			break 
		end

		if l_boss_bf[i].dis < vDis then
			vDis = l_boss_bf[i].dis
			vID = l_boss_bf[i].id
		end		
	end

	return vID
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 ,
	["开明怒火"] = INVALID_ID ,
	["燃烧的怒火"] = INVALID_ID ,
	["引爆镇魔天雷"] = INVALID_ID ,
	["侍魔者"] = INVALID_ID ,
	["腐尸草"] = INVALID_ID
}

function ResetBoss()
	l_boss["相柳"] = INVALID_ID 
	l_boss["开明"] = INVALID_ID 
	l_boss["长右"] = INVALID_ID 
	l_boss["英招"] = INVALID_ID 
	l_boss["穷奇"] = INVALID_ID 
	l_boss["饕餮"] = INVALID_ID 
	l_boss["陆吾"] = INVALID_ID 
	l_boss["毕方"] = INVALID_ID 
	l_boss["蛊雕"] = INVALID_ID 
	l_boss["毕方幻象"] = INVALID_ID 
	l_boss["慑魂"] = INVALID_ID 
	l_boss["美味的食物"] = INVALID_ID 
	l_boss["穷奇·幻"] = INVALID_ID 
	l_boss["镇魔天雷"] = INVALID_ID 
	l_boss["冰冻三尺"] = INVALID_ID
	l_boss["开明怒火"] = INVALID_ID
	l_boss["燃烧的怒火"] = INVALID_ID
	l_boss["引爆镇魔天雷"] = INVALID_ID
	l_boss["腐烂的食物"] = INVALID_ID
	l_boss["侍魔者"] = INVALID_ID
	l_boss["腐尸草"] = INVALID_ID
end

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

function process_exit()

	MoveToNPC( 66 , 78 , nFubenMapId ,"萧陵" )

	System:Sleep(500)
	if FindQuestFrameOption( "离开副本" , CMP_PART_MATCH) == true then
		QuestFrameOptionClicked("离开副本", CMP_PART_MATCH)
	end
end

function getIdFromMsg( szMsg )

	local i , nStart = string.find( tostring( szMsg ) , "毕方幻象" )
	nStart = nStart + 2

	local nEnd = string.find( tostring( szMsg ) , ";" , nStart ) - 1
	local szID = string.sub( tostring( szMsg ) , tonumber( nStart ) , tonumber( nEnd ) )

	return tonumber( szID )

end

function UpdateData()

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

	ResetBoss()

	local zd_id = INVALID_ID 
	local zd_x = 0
	local zd_y = 0

	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 nameObj == "毕方幻象" then
				if Character:GetData(idObj, "DIE") == false then

					l_dead_bf = false

					SetBfBorn( idObj , xObj , yObj )
						
					if l_boss[nameObj] == INVALID_ID then
						l_boss[nameObj] = idObj
					elseif idObj == l_bf_real then
						--尽量只保存真身的id
						l_boss[nameObj] = idObj
					end
				end
			elseif nameObj == "相柳" then
				if Character:GetData(idObj, "DIE") == false then
					l_boss[nameObj] = idObj
					l_dead_xl = false
				else
					l_dead_xl = true
				end	
			elseif nameObj == "镇魔天雷" then
				if Character:GetData(idObj, "DIE") == false then
					l_boss[nameObj] = idObj
					if zd_id == INVALID_ID then
						zd_id,zd_x,zd_y = FindObjectByName("蛊雕" , "NPC")
					end

					if zd_id ~= INVALID_ID then
						local vDis = GetDist( zd_x , zd_y , xObj , yObj )
						DbgPrintf("镇魔天雷 %d 坐标 %d,%d , 距离boss %d,%d距离 %d" , tonumber(idObj) , tonumber(xObj) , tonumber(yObj) , tonumber(zd_x) , tonumber(zd_y) , tonumber(vDis))
						if vDis < 5 then
							l_boss["引爆镇魔天雷"] = idObj
						end
					end
				end
			elseif nameObj == "毕方真身" then
				l_dead_bf = true
				l_boss["毕方"] = idObj
			elseif l_boss[nameObj] ~= nil then
				if Character:GetData(idObj, "DIE") == false then
					l_boss[nameObj] = idObj
				end
			end
		elseif classObj == "NPC" then
			if nameObj == "蛊雕" then
				if Character:GetData(idObj, "DIE") == false then
					l_dead_zd = false
					l_boss[nameObj] = idObj
				else
					l_dead_zd = true
				end
			elseif nameObj == "长右" then
				if Character:GetData(idObj, "DIE") == false then
					l_boss[nameObj] = idObj
				end
			elseif nameObj == "毕方真身" then
				l_dead_bf = true
				l_boss["毕方"] = idObj
			elseif nameObj == "毕方" then
				l_dead_bf = false
				
				l_boss["毕方"] = idObj
				ResetBf()
			elseif nameObj == "毕方幻象" then
				l_dead_bf = false
				l_boss["毕方"] = idObj
				if idObj == l_boss_bf[0].id then
					l_boss_bf[0].x = xObj
					l_boss_bf[0].y = yObj 
				end
			end
		elseif classObj == "SPECIAL" then
			if string.find( nameObj , "怒火冲天" ) then
				l_boss["开明怒火"] = idObj
			elseif l_boss[nameObj] ~= nil then
				l_boss[nameObj] = idObj
			end

			if l_boss["冰冻三尺"] == idObj then
				AI:SetAdv("增加威胁对象" , l_boss["冰冻三尺"] , 7 , "冰冻三尺")
			elseif l_boss["开明怒火"] == idObj then
				AI:SetAdv("增加威胁对象" , l_boss["开明怒火"] , 4 , "开明怒火")
			elseif l_boss["燃烧的怒火"] == idObj then
				AI:SetAdv("增加威胁对象" , l_boss["燃烧的怒火"] , 4 , "燃烧的怒火")
			end
		elseif classObj == "PLAYER" then
			if CheckBufferByName( idObj , "腐尸草") ~= INVALID_ID then
				l_boss["腐尸草"] = 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,"各位高手已经斩杀了王陵中所有破除封印的洪荒异兽") or string.find(szMsg , "浊气已动") then
				l_finish = true
			elseif string.find(szMsg,"XWLFB_140218_38") then
				l_finish = false
			elseif string.find(szMsg,"XWLFB_140218_34") then
				l_finish = false
				l_start_wait = 20000
				UpdateMsg("fmwl_fight_start")
			elseif string.find(szMsg,"XWLFB_140218_35") then
				l_finish = false
				l_start_wait = 10000
				UpdateMsg("fmwl_fight_start")
			elseif string.find(szMsg,"XWLFB_140218_36") then
				l_finish = false
				l_open_boss = true
				l_start_wait = 5000 + 2000
				UpdateMsg("fmwl_fight_start")
			elseif string.find(szMsg,"尽快远离英招") then
				UpdateMsg("fmwl_yz_emeng")
			elseif string.find(szMsg,"各位高手请注意，请找出毕方真身后速速击杀") then
				l_open_boss = false
			elseif string.find(szMsg,"各位高手请注意，蛊雕已开始逃窜") then
				l_open_boss = false
			elseif string.find(szMsg,"各位高手请注意四周，切勿让长右食得美味的食物") then
				l_open_boss = false
			end
		end
		isNext,szMsg = GetDebugMessage()
	end

	while true do

		local channel, time, name, data , _ , idObj = GetChatMsg()
		if channel == -1 then
			break
		end

		if string.find( data , "各位高手已经斩杀了王陵中所有破除封印的洪荒异兽" ) then
		elseif string.find( data , "没想到尔等眼力如此之强，竟能识得本尊真身" ) then
		elseif string.find( data , "本尊真身在此，尔等看清"  ) then
			l_boss_bf[0].id = getIdFromMsg(data)
			DbgPrintf("毕方幻象id=%d , %s" , tonumber(l_boss_bf[0].id) , tostring(data))
		end
	end

	if l_boss["相柳"] ~= INVALID_ID or l_boss["慑魂"] ~= INVALID_ID then 
		return process_boss_kill , "相柳"
	elseif l_boss["开明"] ~= INVALID_ID then
		return process_boss_kill , "开明"
	elseif l_boss["美味的食物"] ~= INVALID_ID or l_boss["腐烂的食物"] ~= INVALID_ID then
		l_open_boss = false
		return process_boss_kill , "长右"
	elseif l_boss["长右"] ~= INVALID_ID then 
		if DataPool:GetTeamMemCount() == 0 or DataPool:IsTeamLeader() == true then
			if l_open_boss == true then
				return open_boss , "长右"
			end
		end
		return process_boss_kill , "长右"
	elseif l_boss["英招"] ~= INVALID_ID then
		return process_boss_kill , "英招"
	elseif l_boss["穷奇"] ~= INVALID_ID or l_boss["穷奇·幻"] ~= INVALID_ID then
		return process_boss_kill , "穷奇"
	elseif l_boss["饕餮"] ~= INVALID_ID or l_boss["侍魔者"] ~= INVALID_ID then
		return process_boss_kill , "饕餮" 
	elseif l_boss["陆吾"] ~= INVALID_ID or l_boss["冰冻三尺"] ~= INVALID_ID then
		return process_boss_kill , "陆吾"
	elseif l_boss["毕方幻象"] ~= INVALID_ID then
		l_open_boss = false
		return process_boss_kill , "毕方"
	elseif l_boss["毕方"] ~= INVALID_ID then
		if DataPool:GetTeamMemCount() == 0 or DataPool:IsTeamLeader() == true then
			if l_open_boss == true then
				return open_boss , "毕方"
			end
		end
		return process_boss_kill , "毕方"
	elseif l_boss["镇魔天雷"] ~= INVALID_ID then
		l_open_boss = false
		return process_boss_kill , "蛊雕"	
	elseif l_boss["蛊雕"] ~= INVALID_ID then
		if DataPool:GetTeamMemCount() == 0 or DataPool:IsTeamLeader() == true then
			if l_open_boss == true then
				return open_boss , "蛊雕"
			end
		end
		return process_boss_kill , "蛊雕"
	end

	if IsMsgCold("fmwl_fight_start" , l_start_wait) == false then
		return process_boss_kill , "战前准备"
	end

	return nil , ""
end

function _CallBack_OpenNPC( fX , fY , nScene )
	if IsFuben() == false then
		return false
	elseif UpdateData() ~= open_boss then
		return false
	end
	return true
end

function open_boss( vBossName )

	MoveToNPC( 48 , 48 , nFubenMapId , vBossName , false , false , _CallBack_OpenNPC )

	System:Sleep(500)
	if FindQuestFrameOption( "开启考验" , CMP_PART_MATCH) == true then
		QuestFrameOptionClicked( "开启考验", CMP_PART_MATCH)
	end	
end

function process_boss_kill( vBossName )

	SetEvent( "EVENT_PICKUP" , 0 )
	AI:SetAdv( "清除参数" )
	AI:SetAdv( "主防御圈" , 48 , 48 , 20 ) 
	AI:SetAI("高级战斗") 
	AI:SetParameter("地图" , nFubenMapId ) 
	AI:Start()

	local result = nil
	local r_param
	local bRangeAttack = Player:IsRangeAttack()

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

		result , r_param = UpdateData()

		if result ~= process_boss_kill then
			if result == open_boss then
				break
				--人物必须走到挂机点
			elseif vBossName == "相柳" then
				if l_dead_xl == true then
					break
				elseif IsMsgCold("fmwl_xl" , 2000) == true then
					break
				end
			elseif vBossName == "毕方" then
				if l_dead_bf == true then
					break
				elseif IsMsgCold("fmwl_bf" , 2000) == true then
					break
				end
			elseif vBossName == "蛊雕" then
				if l_dead_zd == true then
					break
				elseif IsMsgCold("fmwl_zd" , 2000) == true then
					break
				end
			else
				break
			end
		else
			vBossName = r_param

			if vBossName == "相柳" then
				UpdateMsg("fmwl_xl")
			elseif vBossName == "毕方" then
				UpdateMsg("fmwl_bf")
			elseif vBossName == "蛊雕" then
				UpdateMsg("fmwl_zd") 
			elseif vBossName == "长右" then
				if bRangeAttack == true then
					AI:SetAdv("主防御圈半径" , 15)
				end
			end
		end

		if l_boss["英招"] ~= INVALID_ID then
			local iPos,_,_ = CheckBufferByName( l_boss["英招"] , "魂牵梦绕")
			if iPos ~= INVALID_ID then
				AI:SetAdv("增加威胁对象" , l_boss["英招"] , 6 , "英招")
			elseif IsMsgCold("fmwl_yz_emeng", 2500) == false then
				AI:SetAdv("增加威胁对象" , l_boss["英招"] , 6 , "英招")
			else
				AI:SetAdv("删除威胁对象" , "英招")
			end
		else
			AI:SetAdv("删除威胁对象" , "英招")
		end

		if l_boss["腐尸草"] ~= INVALID_ID then
			if l_boss["腐尸草"] ~= Player:GetData("ID") then
				AI:SetAdv("增加威胁对象" , l_boss["腐尸草"] , 6 , "腐尸草")
			else
				bCallPet = 0
			end
		else
			AI:SetAdv("删除威胁对象" , "腐尸草")
		end 

		if l_boss["冰冻三尺"] == INVALID_ID then
			AI:SetAdv("删除威胁对象" , "冰冻三尺" )
		end

		if l_boss["开明怒火"] == INVALID_ID then
			AI:SetAdv("删除威胁对象" , "开明怒火" )
		end

		if l_boss["燃烧的怒火"] == INVALID_ID then
			AI:SetAdv("删除威胁对象" , "燃烧的怒火" )
		end

		if l_boss["蛊雕"] ~= INVALID_ID then
			FollowTarget = l_boss["蛊雕"]
		end


		if l_boss["慑魂"] ~= INVALID_ID then
			AI:SetAdv("指定击杀" , 0 , "慑魂" )
		elseif l_boss["穷奇·幻"] ~= INVALID_ID then
			AI:SetAdv("指定击杀" , 0 , "穷奇·幻" )
			bStopAssistSelf = 1
			bStopAssist = 1
		elseif l_boss["侍魔者"] ~= INVALID_ID then
			AI:SetAdv("指定击杀" , 0 , "侍魔者" )
			bStopAssistSelf = 1
			bStopAssist = 1
		elseif l_boss["毕方幻象"] ~= INVALID_ID then
			if l_bf_real == l_boss["毕方幻象"] then
				AI:SetAdv("指定击杀" , l_boss["毕方幻象"] , "毕方幻象" )
				AI:SetAdv("指定击杀" , -1 , "毕方" )
			else
				bKillMonster = 0 
				AI:SetAdv("指定击杀" , -1 , "毕方幻象" )
			end
		elseif l_boss["镇魔天雷"] ~= INVALID_ID then
			if l_boss["引爆镇魔天雷"] ~= INVALID_ID then
				AI:SetAdv("指定击杀" , l_boss["引爆镇魔天雷"] , "镇魔天雷" )
			else
				bKillMonster = 0
				AI:SetAdv("指定击杀" , -1 , "" )
			end
		elseif l_boss["长右"] ~= INVALID_ID or l_boss["美味的食物"] ~= INVALID_ID or l_boss["腐烂的食物"] ~= INVALID_ID then
			AI:SetAdv("指定击杀" , 0 , "美味的食物" )
			bStopRangeAttack = 1
		else
			AI:SetAdv("指定击杀" , -1 , "" )
		end

		if nSetNoFight > 0 then
			bKillMonster = 0 
			bYinGuai = 0 
			bTeamAttack = 0 
			bYinshen = 1
			bStopAssist = 1
		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:SetParameter("运行参数" , "停止群体攻击技能" , bStopRangeAttack )

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

	AI:Stop()

	OnSweepBattleField()

	return result , r_param 
end

function _breakmove_infuben(fX , fY , nScene )

	if IsFuben() == false then
		return true
	end

	return false
end


function _SMain()	

	if Player:GetData( "LEVEL" ) < 85 then
		ShowMessage( "等级>= 85 才可以进入凤鸣王陵" )
		return
	end

	LogChatMsg( 0 )
	LogChatMsg( 1 )

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

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

	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
				if task ~= nil then
					task , task_param = task( task_param )
				elseif l_finish == true then
					process_exit()
				end
			end

		else
			l_finish = false

			if check_quest == false then
				if GetQuestTimesInfo("凤鸣王陵") == 0 and IsFuben()== false then
					ShowMessage("今日已经完成凤鸣王陵副本!")
					break 
				end
			end

			check_quest = true

			--在野外
			MoveToNPC( 286 , 65 , 0x244 , "萧陵" , 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 )
				QuestFrameMissionClose()
				System:Sleep(2000)
			else
				PushDebugMessage("等待队长进入副本..." , 1)
			end
		end

	end

end

_SMain()