--[[
[author]
class=2
cmd0str=ȥˢ
cmd0=30
cmd1str=
cmd1=24
cmd2str=
cmd2=18
cmd_default=0
chk0str=ֻʰȡBoss
chk1str=
]]--

script_name="ؾ"

local nSceneID = 30
local nFubenMapId = 272
local g_mapPos = {
	--
	[30] = {{x=83,y=77},{x=112,y=95},{x=105,y=175},{x=62,y=187},{x=112,y=229},{x=76,y=245},{x=101,y=257},{x=183,y=195},{x=210,y=210},{x=235,y=201},{x=231,y=253},{x=262,y=268},{x=232,y=122}} ,
	--
	[24] = { {x=206,y=130}, {x=187,y=139}, {x=149,y=99}, {x=135,y=143}, {x=77,y=100}, {x=62,y=148}, {x=44,y=185}, {x=96,y=196}, {x=81,y=236}, {x=87,y=266}, {x=155,y=234}, {x=170,y=266}, {x=181,y=232}, {x=230,y=269},{x=240,y=215}, {x=252,y=167}} ,
	--
	[18] = { {x=54,y=50},{x=56,y=103},{x=56,y=143},{x=137,y=116},{x=119,y=165},{x=83,y=180},{x=97,y=206},{x=81,y=245},{x=51,y=261},{x=192,y=270},{x=228,y=271},{x=238,y=233},{x=194,y=216},{x=266,y=179},{x=218,y=115},{x=249,y=47},{x=163,y=73},{x=102,y=44} }
}

local g_szNpcName = "ɮ"

local g_nPos = {
	[1] = {x=88, y=86},--
	[2] = {x=38, y=86},--
	[3] = {x=64, y=100},--м
	[4] = {x=63, y=26},--ɮ
	[5] = {x=71, y=18},--
}

local l_var = 
{
	["is_kill_boss"] = 0 ,
	["monster_dir"] = 0 ,
	["is_done"] = 0 ,
	["assist_monk"] = 0 ,
	["is_fighting"] = 0 ,
	["daoshu_eseng_id"] = INVALID_ID ,
	["mengmian_eseng_id"] = INVALID_ID ,
	["fudeng_id"] = INVALID_ID , 
	["yunyou_wuseng_id"] = INVALID_ID
}
local l_bosspick = 0
local l_yinguai = 1

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

function UpdateData()
	if IsFuben() == false then
		l_var["is_kill_boss"] = 0 
		l_var["monster_dir"] = 0
		l_var["is_done"] = 0
		l_var["assist_monk"] = 0
	end

	l_var["is_fighting"] = 0
	l_var["daoshu_eseng_id"] = INVALID_ID 
	l_var["mengmian_eseng_id"] = INVALID_ID
	l_var["fudeng_id"] = INVALID_ID
	l_var["yunyou_wuseng_id"] = INVALID_ID

	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" and Character:GetData(idObj, "DIE") == false then

			l_var["is_fighting"] = 1

			if nameObj == "ɮ" then
				l_var["daoshu_eseng_id"] = idObj
			elseif nameObj == "ɮ" then
				l_var["mengmian_eseng_id"] = idObj
				l_var["is_kill_boss"] = 1 
			end
		elseif classObj == "NPC" then
			if nameObj == "ɮ" then
				l_var["assist_monk"] = 0
			elseif nameObj == "ɫƻ" then
				l_var["fudeng_id"] = idObj
			elseif nameObj == "ɮ" then
				l_var["yunyou_wuseng_id"] = idObj
			end
		end
	end

	if IsFuben() == false then
		local isNext,szMsg = GetDebugMessage()
		while isNext == 1 do
			isNext,szMsg = GetDebugMessage()
		end
		return 
	end

	--һɮֻ105
	--ͬ鱻,ɮ˸͵Ľ.15ˢ1.
	--ǰΪ:8/10ڳ
	--ɱɮ:0/1.
	local isNext,szMsg = GetDebugMessage()
	while isNext == 1 do
		if szMsg ~= nil then
			if string.find(szMsg ,"") then
				l_var["monster_dir"] = 1
			elseif string.find(szMsg , "") then
				l_var["monster_dir"] = 2 
			elseif string.find(szMsg , "1/10") or string.find(szMsg , "3/10") or string.find(szMsg ,"5/10") or string.find(szMsg ,"7/10") or string.find(szMsg ,"9/10") then
				l_var["monster_dir"] = 1
			elseif string.find(szMsg , "2/10") or string.find(szMsg , "4/10") or string.find(szMsg ,"6/10") or string.find(szMsg ,"8/10") or string.find(szMsg ,"10/10") then
				l_var["monster_dir"] = 2
			elseif string.find(szMsg, "1/1") then
				l_var["is_done"] = 1
			elseif string.find(szMsg, "ͬﱻ") then
				if l_var["monster_dir"] == 1 then
					l_var["monster_dir"] = 2 
				else
					l_var["monster_dir"] = 1
				end
			elseif string.find(szMsg , "ɮѾڲؾ") then
				l_var["is_kill_boss"] = 1
			elseif string.find(szMsg , "Ȼһ˱ĺ") then
				l_var["is_kill_boss"] = 1	--Ϣ
			elseif string.find(szMsg , "ƶɮס" ) then
				l_var["assist_monk"] = 1
			elseif string.find(szMsg , "ƶɮػ" ) then
				--տʼ
			elseif string.find(szMsg , "Իɮʼ֮ս") then
				l_var["monster_dir"] = 0
			elseif string.find(szMsg , "Ѿʤķɮı" ) then
				l_var["is_done"] = 1
			end
		end
		isNext,szMsg = GetDebugMessage()
	end

	while true do 
		local channel, time, name, data = GetChatMsg()
		if channel == INVALID_ID then 
			break 
		end

		local now = System:timeGetTime()
		--channel == 0 and
		if now < time + 10 then

			if string.find(data, "CJG_090413_25") then
				--Ȼһ˱ĺ
				l_var["is_kill_boss"] = 1
			end
		end
	end
end

function OnTeamKill()
	
	AI:SetAI("ɱ")
	AI:SetParameter("ʼ")
	AI:SetParameter("Ŀ", "", "ɮ")
	AI:SetParameter("ʰȡ", PM_PICKUP)
	AI:SetParameter("ʱԶѡ" , true )
	
	AI:Start(false)
	AI:SetParameter("в" ,"Ӹ", 1)
	AI:SetParameter("в" ,"", 0)
	AI:SetParameter("в" , "ֻʰȡBOSS" , l_bosspick )
	AI:SetParameter("в" , "" , l_yinguai)

	while true do
			
		UpdateData()

		if IsFuben() == true then
			AI:SetParameter("в" ,"ɱ", 1)
		else
			AI:SetParameter("в" ,"ɱ", 0)
		end

		if DataPool:IsTeamLeader() == true then
			if l_var["is_fighting"] == 0 then
				break 
			end
			if IsFuben() == false then
				break
			end
		end

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

		System:Sleep(200)
	end
			
	AI:Stop()
end

local PreRide = false 
function _callback(fX, fY, nScene)

	UpdateData()
	
	if l_var["yunyou_wuseng_id"] ~= INVALID_ID then
		return false 
	end

	if DataPool:IsTeamLeader() == false then
		return false
	end

	if GetMount() ~= INVALID_ID then
		if PreRide == false then
			 Player:EnableTeamFollow(true)
			UpdateMsg("CheckTeamFollow")
		else
			if IsMsgCold("CheckTeamFollow" , 10000) == true then
				Player:EnableTeamFollow(true)
				UpdateMsg("CheckTeamFollow")
			end
		end

		PreRide = true 
	else
		if PreRide == true then 
			Player:EnableTeamFollow(false)
			UpdateMsg("CheckTeamFollow_False")
		else
			if IsMsgCold("CheckTeamFollow_False" , 10000) == true then
				Player:EnableTeamFollow(false)
				UpdateMsg("CheckTeamFollow_False")
			end
		end
		PreRide = false
	end

	return true
end


function OnFuben()

	UpdateData()

	if DataPool:IsTeamLeader() == false then
		return
	end

	if IsFuben() == false then
		return
	end

	if l_var["is_fighting"] > 0 then
		OnTeamKill()
		UpdateMsg("Cjg_End_Fight")
	elseif l_var["is_done"] > 0 then
		OnPickup( l_bosspick )
		DoMount(true)
		if IsMsgCold("Cjg_End_Fight" , 6000) == true then
			Player:EnableTeamFollow(true)
			UpdateMsg("CheckTeamFollow")
			MoveTo( g_nPos[5].x , g_nPos[5].y , nFubenMapId ) 
			System:Sleep(2000)
		end
	elseif l_var["assist_monk"] > 0 then
		MoveTo( g_nPos[3].x , g_nPos[3].y , nFubenMapId )
	elseif l_var["is_kill_boss"] > 0 then
		MoveTo( g_nPos[4].x , g_nPos[4].y , nFubenMapId )
	else
		if l_var["monster_dir"] == 1 then
			MoveTo( g_nPos[1].x , g_nPos[1].y , nFubenMapId )
			DoMount(false)
		elseif l_var["monster_dir"] == 2 then
			MoveTo( g_nPos[2].x , g_nPos[2].y , nFubenMapId )
			DoMount(false)
		else
			MoveToNPC( 64 , 104 , nFubenMapId , "ɮ" )
			System:Sleep(500)
			QuestFrameOptionClicked( "ʼ" , CMP_FULL_MATCH)	-- ѡѡ
			System:Sleep(500)
		end
	end
end

function _SMain(...)

	LogChatMsg(2)

	if _CommandLine ~= nil then 
		nSceneID =tonumber(_CommandLine)
		if g_mapPos[nSceneID] == nil then
			nSceneID = 30
		end
	end

	if _CommandChk0 ~= nil and tonumber(_CommandChk0) > 0 then
		l_bosspick = 1
	end

	if _CommandChk1 ~= nil and tonumber(_CommandChk1) > 0 then
		l_yinguai = 0
	end

	local index = 1 

	while true do 
		
		System:Sleep(200)

		UpdateData()

		if DataPool:IsTeamLeader() == true then

			if IsFuben() == true then
				OnFuben()
				UpdateMsg("Cjg_End_Hunt")
			else
				if index > table.getn(g_mapPos[nSceneID]) then
					index = 1
				end

				if l_var["yunyou_wuseng_id"] ~= INVALID_ID then
					QuestFrameSpeak(l_var["yunyou_wuseng_id"] , "")
					--ؾ
					System:Sleep(200)
					Player:EnableTeamFollow(false)
					QuestFrameOptionClicked( "ؾ" , CMP_FULL_MATCH)	-- ѡѡ
					System:Sleep(800)
				else
					while IsMsgCold("Cjg_End_Hunt" , 3000) == false do
						DoMount(true)
					end

					MoveTo( g_mapPos[nSceneID][index].x, g_mapPos[nSceneID][index].y, nSceneID, "", 3, true , _callback , true , true )
			
					if l_var["yunyou_wuseng_id"] == INVALID_ID then
						--һ
						index = index + 1
					end				
				end

			end
		else
			OnTeamKill()
		end

	end	
	
	LogChatMsg(-1)
end

_SMain()
