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

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