

--[[

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