ShowMessage("四绝山庄脚本 $Rev: 100 $")

--[[
[author]
class=1
attr=1
chk0str=接取第3次任务
chk1str=检查龙柱和白云
]]--

script_name="四绝庄恩怨"
local nSetNoFight = 0

local nFubenMapId = 291
local g_nParam = {}
local l_param_lzby = 0 
local l_param_third = 0

local g_nIndex, g_idMission, g_idScript = DataPool:GetPlayerMission(script_name)

function GetMissionComplete()

	g_nIndex, g_idMission, g_idScript = DataPool:GetPlayerMission(script_name)

	if g_nIndex ~= -1 then

		DbgPrintf("%s Index:%d MissionID:%d ScriptID:%d", script_name, g_nIndex, g_idMission, g_idScript)

		-- 取得任务参数
		for i=0,7 do
			g_nParam[i] = DataPool:GetPlayerMission_Variable(g_nIndex, i)
			DbgPrintf("[%d]%d", i, g_nParam[i])
		end
		
		return tonumber(g_nParam[0])
	end

	return -1
end

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

local l_pqq_pos =
{
	[0] = { x= 101, y = 114 } ,
	[1] = { x= 96 , y = 79 } ,
	[2] = { x= 35 , y = 87 } ,
	[3] = { x= 84 , y = 23 } ,
	[4] = { x= 23 , y = 17 } 
}

local l_boss = {
	["潘菁菁NPC"] = 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 
}

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

function reset_sjz()
	g_state_sjz= {}
	g_state_sjz["四绝钟"] = BOSS_RECORD_INIT
	g_state_sjz["闵墨"] = BOSS_RECORD_INIT
	g_state_sjz["秦韵"] = BOSS_RECORD_INIT
	g_state_sjz["陶青"] = BOSS_RECORD_INIT
	g_state_sjz["庞企"] = BOSS_RECORD_INIT
end



function set_boss_record( name , die )
	if name == "四绝钟" then
		--skip
	elseif name == "闵墨" then
		g_state_sjz["四绝钟"] = BOSS_RECORD_DEAD
	elseif name == "秦韵" then
		g_state_sjz["四绝钟"] = BOSS_RECORD_DEAD
		g_state_sjz["闵墨"] = BOSS_RECORD_DEAD
	elseif name == "陶青" then
		g_state_sjz["四绝钟"] = BOSS_RECORD_DEAD
		g_state_sjz["闵墨"] = BOSS_RECORD_DEAD
		g_state_sjz["秦韵"] = BOSS_RECORD_DEAD
	elseif name == "庞企" then
		g_state_sjz["四绝钟"] = BOSS_RECORD_DEAD
		g_state_sjz["闵墨"] = BOSS_RECORD_DEAD
		g_state_sjz["秦韵"] = BOSS_RECORD_DEAD
		g_state_sjz["陶青"] = BOSS_RECORD_DEAD
	else
		return
	end

	if die == true then
		g_state_sjz[name] = BOSS_RECORD_DEAD
	else
		g_state_sjz[name] = BOSS_RECORD_MET
	end
end

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 
end

if g_state_sjz == nil then
	reset_sjz()
end

local l_main_pos =
{
	[0] = {x=101,y=94 , r=15} , --{x=99 ,y=112, r=15}
	[1] = {x=101,y=94 , r=15} ,
	[2] = {x=27 ,y=103, r=15} ,
	[3] = {x=100,y=30 , r=15} ,
	[4] = {x=30 ,y=29 , r=15} 
}

local l_boss2_pos =
{
	--第二个boss,第一阶段站位
	[0] = {x=41,y=99} ,
	[1] = {x=41,y=86} ,
	[2] = {x=30,y=86} ,
	[3] = {x=31,y=98} ,
	[4] = {x=52,y=86} ,
	[5] = {x=52,y=99} 
}

local l_step2_pos = 
{
	--第二个boss,第二阶段跑位
	[0] = {x=36,y=93} ,	--左上角中心位置
	[1] = {x=17,y=88} ,
	[2] = {x=17,y=113} ,
	[3] = {x=39,y=110} ,		
	[4] = {x=40,y=88} 			
}

function TestMenpai()

	local t_team_size = DataPool:GetTeamMemCount()
	if t_team_size < 1 then
		return false
	end

	for i = 0 , t_team_size - 1 do	
		local _,t_id, t_menpai = DataPool:GetTeamMemInfo(i)
		if t_menpai == 0 or t_menpai == 1 or t_menpai == 2 or t_menpai == 7 then
			return true
		end
	end
	return false
end

local flag_step0 = 0 	-- 出现教头或其他怪物时改变策略
local flag_step2 = 0	-- 一旦出现回音幡,改变策略
local flag_step3 = 0	-- 出现青鹰,改变策略
local flag_step4 = 0	-- 出现龙柱提示信息,改变策略 , 出现十步一杀,改变策略

function UpdateData()

	if GetActiveDataSceneID() ~= nFubenMapId then
		return nil,""
	end

	ResetBoss()

	local t_hyf_near_dis = 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 nameObj == "四绝钟" then
				l_boss["四绝钟"] = idObj
			elseif string.find(nameObj , "墨虎") then
				if Character:GetData(idObj, "DIE") == false then
					l_boss["墨虎"] = idObj
				end
			elseif string.find(nameObj , "四绝庄教头") then
				if Character:GetData(idObj, "DIE") == false then
					l_boss["四绝庄教头"] = idObj
				end
			elseif string.find(nameObj , "四绝庄护卫") then
				if Character:GetData(idObj, "DIE") == false then
					l_boss["四绝庄护卫"] = idObj
				end
			else
				if l_boss[nameObj] ~= nil then
					if Character:GetData(idObj, "DIE") == false then
						l_boss[nameObj] = idObj
					end
				end
			end
			set_boss_record( nameObj , Character:GetData(idObj, "DIE") )
		elseif classObj == "NPC" then
			if nameObj == "潘菁菁" then
				l_boss["潘菁菁"] = idObj
			elseif nameObj == "回音幡" then

				--记录离安全中心最近的那一个对象ID
				local t_this_near_dis = 99999
				local t_this_step = 0
				for i = 1 , 4 do 
					local t_new_dis = GetDistSq( xObj, yObj , l_step2_pos[i].x , l_step2_pos[i].y )
					if t_new_dis < t_this_near_dis then
						t_this_near_dis = t_new_dis
						t_this_step  = i 
					end
				end

				if l_boss["回音幡"] == INVALID_ID or t_this_near_dis < t_hyf_near_dis then
					l_boss["回音幡"] = idObj
					t_hyf_near_dis = t_this_near_dis
					DbgPrintf("回音幡 %d  %d,%d,距离 %d 坐标最近" , l_boss["回音幡"] , xObj , yObj , t_this_step )
				end

				AI:SetAdv("增加威胁对象" , idObj , 4 , "回音幡")
			end

			if nameObj ~= "四绝钟" then
				set_boss_record( nameObj , true )
			else
				set_boss_record( nameObj , false )
			end			
		elseif classObj == "SPECIAL" then
			if string.find( nameObj , "十步一杀") then
				l_boss["十步一杀"] = idObj
				AI:SetAdv("增加威胁对象" , idObj , 4 , "十步一杀")
			elseif nameObj == "白云" then
				l_boss["白云"] = idObj
			elseif nameObj == "黑云" then
				l_boss["黑云"] = idObj
				if l_param_lzby == 1 then
					AI:SetAdv("增加威胁对象" , idObj , 4 , "黑云")
				end
			elseif string.find( nameObj , "将进酒") then 
				l_boss["将进酒"] = idObj
				AI:SetAdv("增加威胁对象" , idObj , 4 , "将进酒")
			end
		elseif classObj == "PLAYER" or classObj == "PET" then
			local iPos,_,_ = CheckBufferByName( idObj, "烛影摇红")
			if INVALID_ID ~= iPos then
				l_boss["烛影摇红"] = idObj
			end
		end
	end

	local now = System:timeGetTime()

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

		if now < time + 10 then

			--DbgPrintf("%s", name)
			DbgPrintf("channel %s", data)
			--DbgPrintf("%d %d [%d]", System:timeGetTime(), time, (System:timeGetTime() - time))
			--DbgPrintf(" ")
			if string.find(data, "SJZ_100129_139") then
				--水龙柱旁边
				if l_param_lzby == 1 then
					flag_step4 = 1
					AI:SetAdv( "主防御圈" , 20 , 37 , 3 )
				end
			elseif string.find(data, "SJZ_100129_137") then
				--金龙柱旁边
				if l_param_lzby == 1 then
					flag_step4 = 1
					AI:SetAdv( "主防御圈" , 40 , 25 , 3 )
				end
			elseif string.find(data, "SJZ_100129_138") then
				--木龙柱旁边
				if l_param_lzby == 1 then
					flag_step4 = 1
					AI:SetAdv( "主防御圈" , 38 , 37 , 3 )
				end
			elseif string.find(data, "SJZ_100129_141") then
				--土龙柱旁边
				if l_param_lzby == 1 then
					flag_step4 = 1
					AI:SetAdv( "主防御圈" , 30 , 18 , 3 )
				end
			elseif string.find(data, "SJZ_100129_140") then
				--火龙柱旁边
				if l_param_lzby == 1 then
					flag_step4 = 1
					AI:SetAdv( "主防御圈" , 19 , 25 , 3 )
				end
			elseif string.find(data, "SJZ_100129_131") then
				--中了秦韵的毒
				UpdateMsg("Quest_SJZ_Complete")
			elseif string.find(data, "SJZ_100129_132") then
				--回音幡出现
				if flag_step2 == 0 then
					flag_step2 = 1 
				end
			elseif string.find(data, "SJZ_100129_60") then
				set_boss_record("秦韵" , true)
			elseif string.find(data, "SJZ_100129_46") then
				set_boss_record("闵墨" , true)
			elseif string.find(data, "SJZ_100129_71") then
				set_boss_record("陶青" , true)
			elseif string.find(data, "SJZ_100129_105") then
				set_boss_record("庞企" , true)
			elseif string.find(data, "SJZ_100129_41") then
				--无需敲钟了
				set_boss_record( "四绝钟" , true )
			elseif string.find(data,"护卫和教头听到钟声，纷纷离开了四绝殿") then
				--已经敲完钟
				set_boss_record( "四绝钟" , true )
			end
		end
	end

	if l_boss["四绝庄护卫"] ~= INVALID_ID or l_boss["四绝庄教头"] ~= INVALID_ID then
		return process_boss_kill , 0
	elseif l_boss["闵墨"] ~= INVALID_ID or l_boss["将进酒"] ~= INVALID_ID then
		return process_boss_kill , 1
	elseif l_boss["秦韵"] ~= INVALID_ID or l_boss["回音幡"] ~= INVALID_ID then
		return process_boss_kill , 2
	elseif l_boss["陶青"] ~= INVALID_ID or l_boss["青鹰"] ~= INVALID_ID or l_boss["墨虎"] ~= INVALID_ID or l_boss["白云"]~= INVALID_ID or l_boss["黑云"]~= INVALID_ID then
		return process_boss_kill , 3
	elseif l_boss["庞企"] ~= INVALID_ID or l_boss["星罗武士"]~= INVALID_ID or l_boss["十步一杀"]~= INVALID_ID then
		return process_boss_kill , 4
	end

	return nil,""
end

function check_pqq()
	local vResult = UpdateData()
	if vResult ~= nil then
		return false
	end

	return true
end

local process_npc_faild = 0 
--对话潘菁菁
function process_npc_t( nProcess_chk )

	MoveTo(l_pqq_pos[nProcess_chk].x , l_pqq_pos[nProcess_chk].y , nFubenMapId , "潘菁菁" , 4 , false , check_pqq , true )
	if check_pqq() == false then
		return nil,nil
	end

	MoveToNPC( l_pqq_pos[nProcess_chk].x , l_pqq_pos[nProcess_chk].y , nFubenMapId , "潘菁菁" )

	--有对话选项的情况下比较好处理,直接可以确定当前要做什么
	--如果没有对话选项,可能是网络卡了,也可能是正在战斗中
	--有队友在下一个区域,则认为是网络卡,
	--boss die=1 则认为是网络卡
	--队友都在同一个区域,则认为是boss未杀
	while true do

		local list_count , npc_id = DataPool:GetNPCEventList_Num()
		if npc_id == INVALID_ID then
			--对话失败,也可能是已经在战斗中,尝试对话3次,不成功则到场地中寻找boss
			process_npc_faild = process_npc_faild + 1
			if process_npc_faild >= 3 then
				process_npc_faild = 0 
				return process_boss_kill , nProcess_chk
			else
				return nil,nil
			end
		end

		process_npc_faild = 0

		if list_count < 1 then

			--boss还没有死亡 , 走向该区域中心,看看能否碰到boss
			return process_boss_kill , nProcess_chk

		else
			local t_click = 0 
			for i=0, list_count - 1 do
				local strType,_,_,_,szText = DataPool:GetNPCEventList_Item(i)
				if strType == "id" then
					if string.find(szText, "传送到星罗坛") then
						set_boss_record("陶青" , true)
						if t_click < 4 then
							t_click = 4
						end
					elseif string.find(szText , "传送到盘龙阁") then
						set_boss_record("秦韵" , true)
						if t_click < 3 then
							t_click = 3
						end
					elseif string.find(szText , "传送到西韵阁") then
						set_boss_record("闵墨" , true)
						if t_click < 2 then
							t_click = 2
						end
					elseif string.find(szText , "上去取宝") then
						--需要再次对话,才会出后一个选项
						if t_click < 5 then
							t_click = 5
						end
					elseif string.find(szText , "将攻补过，协助四绝庄抵御外敌") then
						if t_click < 6 then
							t_click = 6
						end
					elseif string.find(szText , "开始战斗")  then
						if t_click < 7 then
							t_click = 7
						end
					end
				elseif strType == "text" then
					if string.find(szText , "我可以给你们的近战门派隐身") then
						if t_click < 1 then
							t_click = 1
						end
					end
				end
			end

			if t_click == 7 then
				QuestFrameOptionClicked("开始战斗", CMP_PART_MATCH)
				return process_boss_kill , nProcess_chk
			elseif t_click == 6 then
				QuestFrameOptionClicked("将攻补过，协助四绝庄抵御外敌", CMP_PART_MATCH)
				return nil,nil
			elseif t_click == 5 then
				QuestFrameOptionClicked("上去取宝", CMP_PART_MATCH)
				MoveToNPC( l_pqq_pos[nProcess_chk].x , l_pqq_pos[nProcess_chk].y , nFubenMapId , "潘菁菁" )
				--继续等待
			elseif t_click == 4 then
				QuestFrameOptionClicked("传送到星罗坛", CMP_PART_MATCH)
				return nil,nil
			elseif t_click == 3 then
				QuestFrameOptionClicked("传送到盘龙阁", CMP_PART_MATCH)
				return nil,nil
			elseif t_click == 2 then
				QuestFrameOptionClicked("传送到西韵阁", CMP_PART_MATCH)
				return nil,nil
			elseif t_click == 1 then
				QuestFrameOptionClicked("确定", CMP_PART_MATCH)
				return nil,nil
			else
				--该对话中无合适选项
				ShowMessage("潘菁菁 的对话中没有发现合适选项")
				return nil,nil
			end
		end
	
		local cur_process , cur_param = UpdateData()
		if cur_process~= nil then
			return nil,nil
		end
	end

end



function process_boss_kill( nBossStep )

	UpdateMsg("Quest_SJZ_Complete")
	local pos , menpai = GetTeamStandPos()

	if pos < 0 then
		pos = 0
	end

	flag_step0 = 0 	-- 出现教头或其他怪物时改变策略
	flag_step2 = 0	-- 一旦出现回音幡,改变策略
	flag_step3 = 0	-- 出现青鹰,改变策略
	flag_step4 = 0	-- 出现龙柱提示信息,改变策略 , 出现十步一杀,改变策略
	
	AI:SetAdv( "清除参数" )
	if nBossStep == 1 or nBossStep == 0 then
		AI:SetAdv( "战场界限" ,  117 , 78 , 117 , 108 )
		AI:SetAdv( "战场界限" ,  117 , 108, 85  , 107 )
		AI:SetAdv( "战场界限" ,  85  , 107, 85  , 78 )
		AI:SetAdv( "战场界限" ,  85  , 78 , 117 , 78 )
		AI:SetAdv( "主防御圈" , l_main_pos[nBossStep].x , l_main_pos[nBossStep].y , l_main_pos[nBossStep].r )
	elseif nBossStep == 2 then
		AI:SetAdv( "战场界限" ,  42 ,  85 , 42 , 115 )
		AI:SetAdv( "战场界限" ,  42 , 115,  13 , 115 )
		AI:SetAdv( "战场界限" ,  13 , 115,  13 , 85 )
		AI:SetAdv( "战场界限" ,  13 ,  85,  42 , 85 )
		AI:SetAdv( "主防御圈" , l_main_pos[nBossStep].x , l_main_pos[nBossStep].y , l_main_pos[nBossStep].r )
	elseif nBossStep ==3 then
		AI:SetAdv( "战场界限" ,  82 ,  16 , 118 , 16 )
		AI:SetAdv( "战场界限" , 118 ,  16,  118 , 47 )
		AI:SetAdv( "战场界限" , 118 ,  47,   82 , 47 )
		AI:SetAdv( "战场界限" ,  82 ,  47,   82 , 16 )
		AI:SetAdv( "主防御圈" , l_main_pos[nBossStep].x , l_main_pos[nBossStep].y , l_main_pos[nBossStep].r )
	elseif nBossStep == 4 then
		AI:SetAdv( "战场界限" ,  41 ,  16 , 41 , 38 )
		AI:SetAdv( "战场界限" ,  41 ,  38 , 17 , 38 )
		AI:SetAdv( "战场界限" ,  17 ,  38 , 17 , 16 )
		AI:SetAdv( "战场界限" ,  17 ,  16 , 41 , 16 )
		if pos == 0 then
			AI:SetAdv( "主防御圈" , 33 , 26 , 15 )
		elseif pos == 1 then
			AI:SetAdv( "主防御圈" , 33 , 33 , 15 )
		elseif pos == 2 then
			AI:SetAdv( "主防御圈" , 25 , 32 , 15 )
		elseif pos == 3 then
			AI:SetAdv( "主防御圈" , 25 , 25 , 15 )
		else
			AI:SetAdv( "主防御圈" , l_main_pos[nBossStep].x  , l_main_pos[nBossStep].y  , l_main_pos[nBossStep].r )
		end
	end

	AI:SetAI("高级战斗") 
	AI:SetParameter("地图" , nFubenMapId)
	AI:Start()

	local result = nil
	local r_param = nil

	local bRangeAttack = Player:IsRangeAttack()
	local pre_FingerAttack = INVALID_ID 
	Dismount()

	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 FingerAttack = -1
		local FingerAttackName = ""
		local FollowTarget = -1
		
		System:Sleep(300)

		result , r_param = UpdateData()

		local find_boss = true
		if result ~= process_boss_kill or r_param ~= nBossStep then
			if IsMsgCold("Quest_SJZ_Complete" , 5000 ) then
				break
			elseif nBossStep == 1 and g_state_sjz["闵墨"] == BOSS_RECORD_DEAD then
				break 
			elseif nBossStep == 2 and g_state_sjz["秦韵"] == BOSS_RECORD_DEAD then
				break 
			elseif nBossStep == 3 and g_state_sjz["陶青"] == BOSS_RECORD_DEAD then
				break 
			elseif nBossStep == 4 and g_state_sjz["庞企"] == BOSS_RECORD_DEAD then
				break 
			else
				--5秒内未见到boss,可能是走远了
				find_boss = false 
			end
				
		else
			UpdateMsg("Quest_SJZ_Complete")
		end

		--获取队长的位置信息
		local _,LeaderId,_,Leader_mapid,Leader_x,Leader_y = DataPool:GetTeamMemInfo(-1)
		local t_followleader = false
		if LeaderId ~= Player:GetData("ID") then
			t_followleader = Player:CompareLocalArea( Leader_mapid , Leader_x , Leader_y )
		end

		if nBossStep == 0 then
			--清理教头
		elseif nBossStep == 1 then
			--1 :杀 闵墨
			--无需跟随队长,战斗中心无需改变
		elseif nBossStep == 2 then
			--2 :杀 秦韵
			if l_boss["回音幡"] ~= INVALID_ID then
				if flag_step2 == 0 then
					flag_step2 = 1 
				end
			end

			--躲避回音幡 1,跟随队长 2,四个角跑 3,对角线跑

			local t_BossHPPercent = 100
			if l_boss["秦韵"] ~= INVALID_ID then
				t_BossHPPercent = Character:GetData( l_boss["秦韵"] , "HP_PERCENT")
			end

			if flag_step2 == 0 and t_BossHPPercent > 50 then
				bCallPet = 0 
			else
				if flag_step2 == 0 then
					flag_step2 = 1 
				end

				if t_followleader == true then
					--可跟随队长时,队员跟随队长
					FollowTarget = LeaderId
					bTeamFollow = 1
				else
					--不可跟随时,需要自己抉择,修改战斗中心且必须在战斗中心内
					if l_boss["回音幡"] ~= INVALID_ID and IsMsgCold("Quest_SJZ_SETHYF" , 10000) then
					
						local _,t_hyf_x,t_hyf_y = FindObjectByID( l_boss["回音幡"] , "NPC")

						if t_hyf_x ~= 0 or t_hyf_y ~= 0 then

							local t_this_far_dis = 0
							local t_self_x , t_self_y = Player:GetPos()
							--查找离回音幡最远的安全中心
							for i = 1 , 4 do 
								local t_new_dis = GetDistSq( t_hyf_x , t_hyf_y , l_step2_pos[i].x , l_step2_pos[i].y )
								if t_new_dis > t_this_far_dis then
									t_this_far_dis = t_new_dis
									flag_step2  = i 
								end
							end

							UpdateMsg("Quest_SJZ_SETHYF")
							AI:SetAdv( "主防御圈" , l_step2_pos[flag_step2].x , l_step2_pos[flag_step2].y , 4 )
							DbgPrintf("回音幡,调整当前防御圈是 %d , %d,%d" , flag_step2 , l_step2_pos[flag_step2].x , l_step2_pos[flag_step2].y ) ;
						end
					end
					bFixPlace = 1
				end
			end
		elseif nBossStep == 3 then
			--3 :杀 陶青 102,35  
			if flag_step3 == 1 then
				AI:SetAdv( "主防御圈" , 98 , 38 , 15 )
			else
				AI:SetAdv( "主防御圈" , 102 , 34 , 15 )

				if l_param_lzby == 1 then
					if t_followleader == false then
						if l_boss["白云"] ~= INVALID_ID then
						
							local _,t_by_x,t_by_y = FindObjectByID( l_boss["白云"] , "特殊对象")
							--检查这里
							if t_by_x ~= 0 and t_by_y ~= 0 then
								AI:SetAdv( "主防御圈" , t_by_x , t_by_y , 3 )
							end
							DbgPrintf("发现白云,坐标 %d,%d" , t_by_x , t_by_y )
						end
					else
						if l_boss["白云"] ~= INVALID_ID or l_boss["黑云"] ~= INVALID_ID then
							bTeamFollow = 1
							FollowTarget = LeaderId
						end
					end
				end
			end

		elseif nBossStep == 4 then
			-- 4 :杀 庞企
			if flag_step4 == 1 then
				if l_param_lzby == 0 then
					AI:SetAdv( "主防御圈" , l_main_pos[nBossStep].x , l_main_pos[nBossStep].y , l_main_pos[nBossStep].r )
				else
					if t_followleader == true then
						bTeamFollow = 1
						FollowTarget = LeaderId
					else
						bFixPlace = 1
					end
				end
			elseif flag_step4 == 2 then
				AI:SetAdv( "主防御圈" , l_main_pos[nBossStep].x , l_main_pos[nBossStep].y , l_main_pos[nBossStep].r )
			end
		end					
		
		if l_boss["陶青"] ~= INVALID_ID then
			local t_iPos,_,_ = CheckBufferByName(l_boss["陶青"] , "天罡护体" ) 
			if t_iPos ~= INVALID_ID then
				bStopSpecialAttack = 1 
				flag_step3 = 1
				bFixPlace = 1
				bKillMonster = 0
			end
		elseif l_boss["庞企"] ~= INVALID_ID then
			local t_iPos,_,_ = CheckBufferByName(l_boss["庞企"] , "天罡护体" ) 
			if t_iPos ~= INVALID_ID then
				bStopSpecialAttack = 1 
				bKillMonster = 0
			end
		end
				
		if l_boss["回音幡"] ~= INVALID_ID then
			local t_self_x , t_self_y = Player:GetPos()
			if GetDist( t_self_x , t_self_y , l_step2_pos[flag_step2].x ,l_step2_pos[flag_step2].y ) > 4 then
				bKillMonster = 0
				bStopAssistSelf = 1
				bStopAssist = 1
			end
		elseif l_boss["青鹰"] ~= INVALID_ID then
			AI:SetAdv("指定击杀" , 0 , "青鹰" )
			flag_step3 = 1
			bStopSpecialAttack = 0
			bYinGuai = 1
			bKillMonster = 1
		elseif l_boss["墨虎"] ~= INVALID_ID then
			AI:SetAdv("指定击杀" , 0 , "毒墨虎" )
			AI:SetAdv("指定击杀" , 0 , "玄墨虎" )
			AI:SetAdv("指定击杀" , 0 , "冰墨虎" )
			AI:SetAdv("指定击杀" , 0 , "火墨虎" )
			bStopSpecialAttack = 0
			flag_step3 = 1
			bYinGuai = 1
			bKillMonster = 1
		elseif l_boss["星罗武士"] ~= INVALID_ID then
			AI:SetAdv("指定击杀" , 0 , "星罗武士" )
			bStopSpecialAttack = 0
			bYinGuai = 1
			bKillMonster = 1
		elseif l_boss["四绝庄护卫"] ~= INVALID_ID then
			bYinGuai = 1
		elseif l_boss["四绝庄教头"] ~= INVALID_ID then
			bYinGuai = 1
		else
			AI:SetAdv("指定击杀" , -1 , "" )
			bTeamFollow = 1
			bTeamAttack = 0
		end

		if l_boss["将进酒"] == INVALID_ID then
			AI:SetAdv("删除威胁对象" , "将进酒")
		end

		if l_boss["十步一杀"] == INVALID_ID then
			AI:SetAdv("删除威胁对象" , "十步一杀")
		else
			flag_step4 = 2
		end

		if l_boss["回音幡"] == INVALID_ID then
			AI:SetAdv("删除威胁对象" , "回音幡")
		end

		if l_boss["黑云"] == INVALID_ID then
			AI:SetAdv("删除威胁对象" , "黑云")
		end
		
		-- 启动了血迹 且 人物当前蓝少于 70% 时，允许召唤宠物
		-- 启动了共生 且 人物当前血少于 70% 时，允许召唤宠物
		local nHPPercent = Player:GetData("HP_PERCENT")
		local nMPPercent = Player:GetData("MP_PERCENT")
		if nHPPercent < 70 then
			if IsSkillSetUse(686) or IsSkillSetUse(687) then
				bCallPet = 1
			end
		end

		if nMPPercent < 70 then
			if IsSkillSetUse(696) or IsSkillSetUse(697) then
				bCallPet = 1
			end
		end		

		--判断自己宠物是否中毒
		if l_boss["烛影摇红"] ~= INVALID_ID then
			UpdateMsg("Quest_SJZ_Complete")

			local nPetID = Player:GetFightingPetID()
			if nPetID == l_boss["烛影摇红"] then
				bCallPet = 0
			elseif l_boss["烛影摇红"] == Player:GetData("ID") then
				bCallPet = 0
			else
				--设置危险对象
				AI:SetAdv("增加威胁对象" , l_boss["烛影摇红"] , 5 , "烛影摇红")
			end
		else
			AI:SetAdv("删除威胁对象" , "烛影摇红" )
		end

		if l_boss["回音幡"] ~= INVALID_ID then
			bCallPet = 0 
		end

		--自己是主加血,且被攻击时,停止攻击
		if pos == 0 and menpai == 4 then
			bYinshen = 1

			if nHPPercent < 60 or nMPPercent < 10 then
				bStopSpecialAttack = 1
			end
		end

		if find_boss == false then

			local t_self_x , t_self_y = Player:GetPos()
			local t_set_x = l_main_pos[nBossStep].x
			local t_set_y = l_main_pos[nBossStep].y
			local t_set_r = l_main_pos[nBossStep].r

			if nBossStep == 2 and flag_step2 == 0 then
				--左上角中心位置
				t_set_x = l_step2_pos[0].x 
				t_set_y = l_step2_pos[0].y
				t_set_r = 5
			elseif nBossStep == 0 and flag_step0 == 1 then
				t_set_x = l_main_pos[1].x	
				t_set_y = l_main_pos[1].y
				t_set_r = l_main_pos[1].r				
			end

			if GetDist( t_self_x , t_self_y , t_set_x , t_set_y ) > 4 then
				AI:SetAdv( "主防御圈" , t_set_x , t_set_y , t_set_r )
				UpdateMsg("Quest_SJZ_Complete")
			else
				break 
			end
		end

		if nSetNoFight > 0 then
			bKillMonster = 0 
			bYinGuai = 0 
			bTeamAttack = 0 
			bYinshen = 1
			bStopAssist = 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:SetAdv("跟随对象" , FollowTarget , 3 )
		AI:SetAdv("允许隐身" , bYinshen )
		AI:SetAdv("不允许出防御圈" , bFixPlace )

		if pre_FingerAttack ~= FingerAttack then
			AI:SetAdv("指定击杀" , -1 , "" )
			pre_FingerAttack = INVALID_ID
		end
		
		if FingerAttack ~= INVALID_ID then
			AI:SetAdv("指定击杀" , FingerAttack , FingerAttackName )
			pre_FingerAttack = FingerAttack
			DbgPrintf("指定击杀 yaoxin %s %d" , tostring(FingerAttackName) , tonumber(FingerAttack))
		end
	end
	AI:Stop()


	OnSweepBattleField()

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

function process_exit()
	ShowMessage("离开四绝庄副本")
	local t_self_x , t_self_y = Player:GetPos()
	local t_area = GetLocalID( nFubenMapId , t_self_x , t_self_y )
	if t_area < 0 then
		t_area = 0 
	end
	MoveToNPC(l_pqq_pos[t_area].x , l_pqq_pos[t_area].y , nFubenMapId , "潘菁菁" )
	QuestFrameOptionClicked("离开副本", CMP_FULL_MATCH)
end

function check_sjz()
	local vResult = UpdateData()
	if vResult ~= nil then
		return false
	end

	if g_state_sjz["四绝钟"] == BOSS_RECORD_DEAD then
		return false
	end
	
	return true
end

function process_qiao_zhong()
	--udpate 返回 process_qiao_zhang 或者 nil , 脚本处于第一步 , 且进入副本时间在 30秒内

	if Player:IsRangeAttack() == true then
		return 
	end

	Pet_Relax()

	if INVALID_ID ~= Player:GetBuffByName("隐身") then
		--移动到钟旁边,并对话钟
		Dismount()
		UpdateMsg("Stop_UseItem")
		MoveTo(101,91,nFubenMapId,"" ,2,false,check_sjz,false,1)
		UpdateMsg("Stop_UseItem")
		MoveTo(103,80,nFubenMapId,"" ,2,false,check_sjz,false,1)
		UpdateMsg("Stop_UseItem")
		AI:SetAI("杀怪")
		AI:SetParameter("初始化")
		AI:SetParameter("地图", nFubenMapId)
		AI:SetParameter("坐标", "添加" , 103,80 )
		AI:SetParameter("目标" , "添加" , "四绝钟")
		AI:StartEx(0)
		AI:SetParameter("运行参数", "召唤珍兽" , 0 )
		AI:SetParameter("运行参数", "停止辅助技能" , 1 )
		AI:SetParameter("运行参数", "停止自身技能辅助" , 1 )
		AI:SetParameter("运行参数", "停止特殊攻击技能" , 1 )
		AI:StartEx(1)
		local vResult , vParam = UpdateData()
		while true do 
			if vResult ~= nil then
				if vResult ~= process_boss_kill and vParam ~= 0 then
					break
				end
			end 

			UpdateMsg("Stop_UseItem")
			if INVALID_ID == Player:GetBuffByName("隐身") then
				break
			end

			System:Sleep(1000)

			vResult , vParam = UpdateData()
		end
		AI:Stop()
		DelMsg("Stop_UseItem")
	else
		Dismount()
		MoveToNPC(l_pqq_pos[0].x , l_pqq_pos[0].y , nFubenMapId , "潘菁菁" )
		QuestFrameOptionClicked("确定", CMP_PART_MATCH)
	end
end

function FindProcess()

	--不明确当前要做什么的情况下,如果是刚进入副本需要敲钟
	local _,t_pqq_x , t_pqq_y = FindObjectByName( "潘菁菁" , "NPC")
	local t_pqq_dis = GetDist( t_pqq_x , t_pqq_y , l_pqq_pos[0].x ,l_pqq_pos[0].y )
	--潘菁菁第1组坐标上 且 队伍有近战职业 且 四绝钟的状态为 INIT 且 在进入副本30秒内 则处理敲钟流程
	DbgPrintf("test:%s , %d , %d , %s" , tostring(TestMenpai()) , tonumber(t_pqq_dis) , tonumber(g_state_sjz["四绝钟"]) , tostring(IsMsgCold("Quest_Map_Enter" , 30000 )) )
	if TestMenpai() == true and t_pqq_dis < 6 and g_state_sjz["四绝钟"] == BOSS_RECORD_INIT and IsMsgCold("Quest_Map_Enter" , 30000 ) == false then
		-- 队伍里有近战职业 且 进入副本30秒内
		if Player:IsRangeAttack() then
			PushDebugMessage("等待队友敲钟..." ,1)
			return nil , ""
		else
			return process_qiao_zhong , ""
		end
	elseif IsMsgCold("Quest_Map_Enter" , 3000 ) == false then
		--可能连npc 都还没刷出来
		return nil , ""
	end
	
	--不明确当前要做什么的情况下,查找当前区域的npc("潘菁菁"),通过对话来确定下一步
	local t_self_x , t_self_y = Player:GetPos()
	local t_area = GetLocalID( nFubenMapId , t_self_x , t_self_y )
	if t_area < 1 then
		t_area = 1
	end

	return process_npc_t , t_area
end

local function OnCommonMission()
	local ask_third = false 
	local task = nil 
	local task_param = nil

	while true do
		
		-- 判断是否接取任务
		local nComplete = GetMissionComplete() 

		if IsFuben() == false then
		--不在副本内

			DelMsg("Quest_Map_Enter")

			if nComplete == INVALID_ID then
				--未接任务
				--清空所有标志
				reset_sjz()
				-- 判断队伍人数
				if DataPool:GetTeamMemCount() < 3 and false then 
					ShowMessage("队伍人数不足 3 人！")
					return
				end

				MoveToNPC(194, 214, 1, "潘菁菁")
				if ask_third == true then
					QuestFrameOptionClicked("三闯四绝庄", CMP_FULL_MATCH)
					QuestFrameOptionClicked("确定", CMP_FULL_MATCH)
				else
					QuestFrameOptionClicked("四绝庄恩怨", CMP_FULL_MATCH)
				end

				QuestFrameAcceptClicked()

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

				if strType == "text" then
					if string.find(szText, "已经两次闯庄") then
						if l_param_third == 1 then
							ask_third = true 
						else
							ShowMessage("今日已经完成两次四绝！")
							break	-- 退出循环，结束脚本
						end
					elseif string.find(szText , "您身上好像没那么多钱") then
						ShowMessage("身上钱不够,无法接第3次四绝！")
						break	-- 退出循环，结束脚本
					elseif string.find(szText , "今日三次闯庄" ) then
						ShowMessage("今日已经完成三次四绝！")
						break	-- 退出循环，结束脚本
					end
				end

				System:Sleep(1000)

			elseif nComplete == 1 then
				MoveToNPC(194, 214, 1, "潘菁菁")
				QuestFrameOptionClicked("四绝庄恩怨", CMP_FULL_MATCH)
				QuestFrameMissionComplete()	-- 完成
			elseif nComplete == 0 then
				MoveToNPC(194, 214, 1, "潘菁菁")
				QuestFrameOptionClicked("四绝庄恩怨", CMP_FULL_MATCH)
				QuestFrameOptionClicked("前往四绝庄", CMP_FULL_MATCH)
			else
				reset_sjz()
				QuestFrameMissionAbnegate(g_idScript)
			end

		else
			UpdateMsg( "Quest_Map_Enter" , 1 )	--Quest_Map_Enter 消息不存在时更新一次

			if nComplete ~= 0 then
				process_exit()
			else
				local process , param = UpdateData()
				if process~= nil then
					task , task_param = process(param)
				elseif task ~= nil then
					task , task_param = task( task_param )
				else
					task , task_param = FindProcess()
				end
			end

		end

		System:Sleep(1000)
	end
end

local function OnXuKongMission()

	script_name = "挑战虚空四绝庄"
	local ask_third = false 
	local task = nil 
	local task_param = nil
	reset_sjz()

	while true do
		
		-- 判断是否接取任务
		local nComplete = GetMissionComplete() 

		if IsFuben() == false then
		--不在副本内

			DelMsg("Quest_Map_Enter")

			if nComplete == 0 then
				MoveToNPC(194, 214, 1, "潘菁菁")
				if DataPool:GetTeamMemCount() < 3 then 
					g_XukongRet = 1
					ShowMessage("队伍人数不足 3 人！")
					return
				end

				QuestFrameOptionClicked("挑战虚空四绝庄", CMP_FULL_MATCH)
				QuestFrameOptionClicked("前往四绝庄", CMP_FULL_MATCH)
			else
				break
			end

		else
			UpdateMsg( "Quest_Map_Enter" , 1 )	--Quest_Map_Enter 消息不存在时更新一次

			if nComplete ~= 0 then
				process_exit()
			else
				local process , param = UpdateData()
				if process~= nil then
					task , task_param = process(param)
				elseif task ~= nil then
					task , task_param = task( task_param )
				else
					task , task_param = FindProcess()
				end
			end

		end

		System:Sleep(1000)
	end
end

function _SMain(...)

	l_param_third = gscript_check( 0 , 0 , 1 )
	l_param_lzby = gscript_check( 1 , 0 , 1 )
	nSetNoFight = GetScriptParam( script_name , "attr" , SCRIPT_ATTR_NOFIGHT )

	LogChatMsg(0)
	LogChatMsg(2)

	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

_SMain()

