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

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

--陷阱和物品同时存在时,不会拾取物品
--近战敲钟时,最后一步会召唤宠物导致敲钟失败
--回音幡出现时,找到的目标点不是最优点
--攻击中停止攻击需求时,角色不会移动到设定位置,还会继续追着目标
--28,27  28,29  31,30  32,26

script_name="四绝庄恩怨"

local g_MissionMap = 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

local g_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 g_boss = {
	["潘菁菁NPC"] = INVALID_ID ,
	["闵墨"] = INVALID_ID ,
	["闵墨NPC"] = INVALID_ID ,
	["将进酒"] = INVALID_ID ,
	["秦韵"] = INVALID_ID ,
	["秦韵NPC"] = INVALID_ID ,
	["回音幡"] = INVALID_ID ,
	["陶青"] = INVALID_ID ,
	["陶青NPC"] = INVALID_ID ,
	["青鹰"] = INVALID_ID ,
	["墨虎"] = INVALID_ID ,
	["白云"] = INVALID_ID ,
	["黑云"] = INVALID_ID ,
	["庞企"] = INVALID_ID ,
	["庞企NPC"] = INVALID_ID ,
	["星罗武士"] = INVALID_ID ,
	["十步一杀"] = INVALID_ID , 
	["四绝庄教头"] = INVALID_ID ,
	["四绝庄护卫"] = INVALID_ID ,
	["钟NPC"] = INVALID_ID ,
	["烛影摇红"] = INVALID_ID
}

local g_main_pos =
{
	[0] = {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 g_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 g_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

function UpdateBossInfo()

	g_boss["潘菁菁NPC"] = INVALID_ID 
	g_boss["闵墨"] = INVALID_ID 
	g_boss["将进酒"] = INVALID_ID 
	g_boss["秦韵"] = INVALID_ID 
	g_boss["回音幡"] = INVALID_ID 
	g_boss["陶青"] = INVALID_ID 
	-- g_boss["陶青NPC"] = INVALID_ID 
	-- g_boss["庞企NPC"] = INVALID_ID 
	-- g_boss["秦韵NPC"] = INVALID_ID 
	-- g_boss["闵墨NPC"] = INVALID_ID
	g_boss["青鹰"] = INVALID_ID 
	g_boss["墨虎"] = INVALID_ID 
	g_boss["白云"] = INVALID_ID 
	g_boss["黑云"] = INVALID_ID 
	g_boss["庞企"] = INVALID_ID 
	g_boss["星罗武士"] = INVALID_ID
	g_boss["十步一杀"] = INVALID_ID 
	g_boss["四绝庄教头"] = INVALID_ID 
	g_boss["四绝庄护卫"] = INVALID_ID 
	g_boss["烛影摇红"] = INVALID_ID

	local t_hyf_near_dis = 99999

	local n = EnumObject(true)

	local nBossCount = 0

	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
					g_boss["闵墨"] = idObj
					nBossCount = nBossCount + 1
					g_boss["钟NPC"] = 99999
				end
			elseif nameObj == "秦韵" then 
				if Character:GetData(idObj, "DIE") == false then
					g_boss["秦韵"] = idObj
					nBossCount = nBossCount + 1
				end
			elseif nameObj == "陶青" then 
				if Character:GetData(idObj, "DIE") == false then
					g_boss["陶青"] = idObj
					nBossCount = nBossCount + 1
				end
			elseif nameObj == "青鹰" then
				if Character:GetData(idObj, "DIE") == false then
					g_boss["青鹰"] = idObj
					nBossCount = nBossCount + 1
				end
			elseif string.find(nameObj , "墨虎") then
				if Character:GetData(idObj, "DIE") == false then
					g_boss["墨虎"] = idObj
					nBossCount = nBossCount + 1
				end
			elseif string.find(nameObj , "四绝庄教头") then
				if Character:GetData(idObj, "DIE") == false then
					g_boss["四绝庄教头"] = idObj
					nBossCount = nBossCount + 1
				end
			elseif string.find(nameObj , "四绝庄护卫") then
				if Character:GetData(idObj, "DIE") == false then
					g_boss["四绝庄护卫"] = idObj
					nBossCount = nBossCount + 1
				end
			elseif nameObj == "星罗武士" then
				if Character:GetData(idObj, "DIE") == false then
					g_boss["星罗武士"] = idObj
					nBossCount = nBossCount + 1
				end
			elseif nameObj == "庞企" then
				if Character:GetData(idObj, "DIE") == false then
					g_boss["庞企"] = idObj
					nBossCount = nBossCount + 1
				end
			end
		elseif classObj == "NPC" then
			if nameObj == "闵墨" then
				g_boss["闵墨NPC"] = idObj
				g_boss["钟NPC"] = 99999
			elseif nameObj == "秦韵" then 
				g_boss["秦韵NPC"] = idObj
			elseif nameObj == "陶青" then 
				g_boss["陶青NPC"] = idObj
			elseif nameObj == "潘菁菁" then
				g_boss["潘菁菁NPC"] = 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 , g_step2_pos[i].x , g_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 g_boss["回音幡"] == INVALID_ID or t_this_near_dis < t_hyf_near_dis then
					g_boss["回音幡"] = idObj
					nBossCount = nBossCount + 1
					t_hyf_near_dis = t_this_near_dis
					DbgPrintf("回音幡 %d  %d,%d,距离 %d 坐标最近" , g_boss["回音幡"] , xObj , yObj , t_this_step )
				end

				AI:SetAdv("增加威胁对象" , idObj , 4 , "回音幡")
			elseif nameObj == "庞企" then
				g_boss["庞企NPC"] = idObj
			end			
		elseif classObj == "SPECIAL" then
			if string.find( nameObj , "十步一杀") then
				g_boss["十步一杀"] = idObj
				nBossCount = nBossCount + 1
				AI:SetAdv("增加威胁对象" , idObj , 4 , "十步一杀")
			elseif nameObj == "白云" then
				g_boss["白云"] = idObj
				nBossCount = nBossCount + 1
			elseif nameObj == "黑云" then
				g_boss["黑云"] = idObj
				nBossCount = nBossCount + 1
				if l_param_lzby == 1 then
					AI:SetAdv("增加威胁对象" , idObj , 4 , "黑云")
				end
			elseif string.find( nameObj , "将进酒") then 
				g_boss["将进酒"] = idObj
				nBossCount = nBossCount + 1
				AI:SetAdv("增加威胁对象" , idObj , 4 , "将进酒")
			end
		elseif classObj == "PLAYER" or classObj == "PET" then
			local iPos,_,_ = CheckBufferByName( idObj, "烛影摇红")
			if INVALID_ID ~= iPos then
				g_boss["烛影摇红"] = idObj
			end
		end
	end

	return nBossCount
end


-- 0,跳过  1,战斗  2,处理相关事务
-- 如何确定当前时间应该做什么
-- 如果地图内有对应npc,则可以明确任务
-- 查询队友所在区域(编号1,2,3,4),以最大区域编号为当前任务
-- 与传送npc对话,可以确定当前任务
function CheckProcess()

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

--对话潘菁菁
function OnNpcEvent( nProcess_chk )

	local _,nCheckResult = CheckProcess()
	if nCheckResult ~= INVALID_ID then
		return nCheckResult
	end

	MoveTo(g_pqq_pos[nProcess_chk].x , g_pqq_pos[nProcess_chk].y , g_MissionMap , "潘菁菁" , 4 , false , CheckProcess , true )
	local _,t_chk = CheckProcess()
	if t_chk ~= INVALID_ID then
		return t_chk
	end

	MoveToNPC( g_pqq_pos[nProcess_chk].x , g_pqq_pos[nProcess_chk].y , g_MissionMap , "潘菁菁" )

	while true do

		local list_count = DataPool:GetNPCEventList_Num()

		if list_count < 1 then
			if nProcess_chk == 1 then
				if g_boss["闵墨NPC"] ~= INVALID_ID then
					return INVALID_ID
				end
			elseif nProcess_chk == 2 then
				if g_boss["秦韵NPC"] ~= INVALID_ID then
					return INVALID_ID
				end
			elseif nProcess_chk == 3 then
				if g_boss["陶青NPC"] ~= INVALID_ID then
					return INVALID_ID
				end
			end

			local idNpc = FindObjectByName("潘菁菁" , "NPC")
			if idNpc ~= INVALID_ID then
				return nProcess_chk
			end

			return INVALID_ID 
		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
						if t_click < 4 then
							t_click = 4
						end
					elseif string.find(szText , "传送到盘龙阁") then
						if t_click < 3 then
							t_click = 3
						end
					elseif string.find(szText , "传送到西韵阁") then
						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 nProcess_chk
			elseif t_click == 6 then
				QuestFrameOptionClicked("将攻补过，协助四绝庄抵御外敌", CMP_PART_MATCH)
				return 4
			elseif t_click == 5 then
				QuestFrameOptionClicked("上去取宝", CMP_PART_MATCH)
				MoveToNPC( g_pqq_pos[nProcess_chk].x , g_pqq_pos[nProcess_chk].y , g_MissionMap , "潘菁菁" )
				--继续等待
			elseif t_click == 4 then
				QuestFrameOptionClicked("传送到星罗坛", CMP_PART_MATCH)
				return INVALID_ID
			elseif t_click == 3 then
				QuestFrameOptionClicked("传送到盘龙阁", CMP_PART_MATCH)
				return INVALID_ID
			elseif t_click == 2 then
				QuestFrameOptionClicked("传送到西韵阁", CMP_PART_MATCH)
				return INVALID_ID
			elseif t_click == 1 then
				QuestFrameOptionClicked("确定", CMP_PART_MATCH)
				return 0
			else
				--该对话中无合适选项
				ShowMessage("潘菁菁 的对话中没有发现合适选项")
				return INVALID_ID
			end
		end
	
		local _,nCheckResult = CheckProcess()

		if nCheckResult ~= INVALID_ID then
			return nCheckResult
		end
	end

	return INVALID_ID
end



function OnFightBoss( nBossStep )

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

	if pos < 0 then
		pos = 0
	end

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

	SetEvent( "EVENT_PICKUP" , 0 )
	
	AI:SetAdv( "清除参数" )
	if nBossStep == 1 or nBossStep == 0 then
		--战斗界限:117,78 - 117,108 - 85,107 - 85,78
		--42,85 - 42,115 - 13,115 - 13,85
		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( "主防御圈" , g_main_pos[nBossStep].x , g_main_pos[nBossStep].y , g_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( "主防御圈" , g_main_pos[nBossStep].x , g_main_pos[nBossStep].y , g_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( "主防御圈" , g_main_pos[nBossStep].x , g_main_pos[nBossStep].y , g_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( "主防御圈" , g_main_pos[nBossStep].x  , g_main_pos[nBossStep].y  , g_main_pos[nBossStep].r )
		end
	end

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

	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)

		local nBoss = UpdateBossInfo()
		if nBoss > 0 then
			default_wait = 10000
			UpdateMsg("Quest_SJZ_Complete")
		end


		if LockNearestTarget( "" , CHA_TYPE_BAG , 0 , 0 , 0 ) ~= -1 then
			UpdateMsg("Quest_SJZ_Complete")
		end

		local t_my_mapid = GetActiveDataSceneID()
		if t_my_mapid ~= g_MissionMap then
			break
		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
			--如果进入副本超过30秒或者附近没有潘或者发现任何怪物或者队伍中无近战职业则设置中心区域
			--否者设置等待区域,等待并刷新时间

			if nBoss > 0 then
				flag_step0 = 1
				AI:SetAdv( "主防御圈" , g_main_pos[1].x , g_main_pos[1].y , g_main_pos[1].r )
			end

			if flag_step0 == 0 then
				local _,t_pqq_x , t_pqq_y = FindObjectByID( g_boss["潘菁菁NPC"] , "NPC")
				local t_pqq_dis = GetDist( t_pqq_x , t_pqq_y , g_pqq_pos[0].x ,g_pqq_pos[0].y )
				if TestMenpai() == false or t_pqq_dis > 5 or IsMsgCold("Quest_SJZ_Begin" , 30000 ) then
					DbgPrintf("潘菁菁远离中心 %d,%d 距离 %d" , t_pqq_x , t_pqq_y , t_pqq_dis )
					--设置新的中心
					flag_step0 = 1
					AI:SetAdv( "主防御圈" , g_main_pos[1].x , g_main_pos[1].y , g_main_pos[1].r )
				else
					--等待
					UpdateMsg("Quest_SJZ_Complete")
				end
			else
				--杀怪
			end
			
		elseif nBossStep == 1 then
			--1 :杀 闵墨
			--无需跟随队长,战斗中心无需改变
		elseif nBossStep == 2 then
			--2 :杀 秦韵
			if g_boss["回音幡"] ~= INVALID_ID then
				if flag_step2 == 0 then
					flag_step2 = 1 
				end
			end

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

			if flag_step2 == 0 and t_BossHPPercent > 50 then
				-- 各自按照既定位置站好,近战停止进攻,所有人收宠物,缩小战斗中心
				-- AI:SetAdv( "主防御圈" , g_boss2_pos[pos].x , g_boss2_pos[pos].y , 3 )
				-- if bRangeAttack == false then
				-- 	bKillMonster = 0
				--	bStopAssist = 1
				-- end
				bCallPet = 0 
				-- bFixPlace = 1
			else
				if flag_step2 == 0 then
					flag_step2 = 1 
				end

				if t_followleader == true then
					--可跟随队长时,队员跟随队长
					FollowTarget = LeaderId
					bTeamFollow = 1
				else
					--不可跟随时,需要自己抉择,修改战斗中心且必须在战斗中心内
					if g_boss["回音幡"] ~= INVALID_ID and IsMsgCold("Quest_SJZ_SETHYF" , 10000) then
					
						local _,t_hyf_x,t_hyf_y = FindObjectByID( g_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 , g_step2_pos[i].x , g_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
--[[
							DbgPrintf("当前人物离 %d 中心最近" , t_this_step)
							--找出当前人物离哪一个安全中心最近
							if GetDist( t_hyf_x , t_hyf_y , g_step2_pos[t_this_step].x ,g_step2_pos[t_this_step].y ) < 12 then
								flag_step2 = t_this_step + 1
								if flag_step2 > 4 then
									flag_step2 = 1
								end
							else
								flag_step2 = t_this_step 
							end
]]--
							UpdateMsg("Quest_SJZ_SETHYF")
							AI:SetAdv( "主防御圈" , g_step2_pos[flag_step2].x , g_step2_pos[flag_step2].y , 4 )
							DbgPrintf("回音幡,调整当前防御圈是 %d , %d,%d" , flag_step2 , g_step2_pos[flag_step2].x , g_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 g_boss["白云"] ~= INVALID_ID then
						
							local _,t_by_x,t_by_y = FindObjectByID( g_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 g_boss["白云"] ~= INVALID_ID or g_boss["黑云"] ~= INVALID_ID then
							bTeamFollow = 1
							FollowTarget = LeaderId
						end
					end
				end
			end

			--bFixPlace = 1

		elseif nBossStep == 4 then
			-- 4 :杀 庞企
			if flag_step4 == 1 then
				if l_param_lzby == 0 then
					AI:SetAdv( "主防御圈" , g_main_pos[nBossStep].x , g_main_pos[nBossStep].y , g_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( "主防御圈" , g_main_pos[nBossStep].x , g_main_pos[nBossStep].y , g_main_pos[nBossStep].r )
			end
		end					
			
		local t_iPos,_,_ = CheckBufferByName(g_boss["陶青"] , "天罡护体" ) 
		if t_iPos ~= INVALID_ID then
			bStopSpecialAttack = 1 
			flag_step3 = 1
			bFixPlace = 1
			bKillMonster = 0
		end

		local t_iPos,_,_ = CheckBufferByName(g_boss["庞企"] , "天罡护体" ) 
		if t_iPos ~= INVALID_ID then
			bStopSpecialAttack = 1 
			bKillMonster = 0
		end
				
		if g_boss["回音幡"] ~= INVALID_ID then
			local t_self_x , t_self_y = Player:GetPos()
			if GetDist( t_self_x , t_self_y , g_step2_pos[flag_step2].x ,g_step2_pos[flag_step2].y ) > 4 then
				bKillMonster = 0
				bStopAssistSelf = 1
				bStopAssist = 1
			end
		elseif g_boss["青鹰"] ~= INVALID_ID then
			AI:SetAdv("指定击杀" , 0 , "青鹰" )
			flag_step3 = 1
			bStopSpecialAttack = 0
			bYinGuai = 1
			bKillMonster = 1
		elseif g_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 g_boss["星罗武士"] ~= INVALID_ID then
			AI:SetAdv("指定击杀" , 0 , "星罗武士" )
			bStopSpecialAttack = 0
			bYinGuai = 1
			bKillMonster = 1
		elseif g_boss["四绝庄护卫"] ~= INVALID_ID then
			bYinGuai = 1
		elseif g_boss["四绝庄教头"] ~= INVALID_ID then
			bYinGuai = 1
		else
			AI:SetAdv("指定击杀" , -1 , "" )
			bTeamFollow = 1
			bTeamAttack = 0
		end

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

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

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

		if g_boss["黑云"] == INVALID_ID then
			AI:SetAdv("删除威胁对象" , "黑云")
		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

				--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
					--回音幡出现
					UpdateMsg("Quest_SJZ_Complete")
					if flag_step2 == 0 then
						flag_step2 = 1 
					end
				elseif string.find(data, "SJZ_100129_60") then
					if g_boss["秦韵NPC"] == INVALID_ID then
						g_boss["秦韵NPC"] = 99999
					end
				elseif string.find(data, "SJZ_100129_46") then
					if g_boss["闵墨NPC"] == INVALID_ID then
						g_boss["闵墨NPC"] = 99999
					end
				elseif string.find(data, "SJZ_100129_71") then
					if g_boss["陶青NPC"] == INVALID_ID then
						g_boss["陶青NPC"] = 99999
					end
				elseif string.find(data, "SJZ_100129_105") then
					if g_boss["庞企NPC"] == INVALID_ID then
						g_boss["庞企NPC"] = 99999
					end
				elseif string.find(data, "SJZ_100129_41") then
					--教头被杀死,或被引开
					--boss已经出现
					if g_boss["钟NPC"] == INVALID_ID then
						g_boss["钟NPC"] = 99999
					end
				elseif string.find(data,"护卫和教头听到钟声，纷纷离开了四绝殿") then
					if g_boss["钟NPC"] == INVALID_ID then
						g_boss["钟NPC"] = 99999
					end	
				end
			end
		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 g_boss["烛影摇红"] ~= INVALID_ID then
			UpdateMsg("Quest_SJZ_Complete")

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

		if g_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 g_boss["闵墨NPC"] ~= INVALID_ID or g_boss["闵墨"] ~= INVALID_ID then
			g_boss["钟NPC"] = 99999
		end

		local boss_done = false 
		if nBossStep == 0 then
			if g_boss["钟NPC"] ~= INVALID_ID then
				boss_done = true
			end
		elseif nBossStep == 1 then
			if g_boss["闵墨NPC"] ~= INVALID_ID then
				boss_done = true
			end
		elseif nBossStep == 2 then
			if g_boss["秦韵NPC"] ~= INVALID_ID then
				boss_done = true
			end
		elseif nBossStep == 3 then
			if  g_boss["陶青NPC"] ~= INVALID_ID then
				boss_done = true
			end
		elseif nBossStep == 4 then
			if g_boss["庞企NPC"] ~= INVALID_ID then
				boss_done = true
			end
		end

		local nComplete = GetMissionComplete() 
		if boss_done == true or nComplete > 0 then
			default_wait = 3000
		end

		if IsMsgCold("Quest_SJZ_Complete" , default_wait ) == true then

			if nComplete > 0 or boss_done == true then
				break
			else
				local t_self_x , t_self_y = Player:GetPos()
				local t_set_x = g_main_pos[nBossStep].x
				local t_set_y = g_main_pos[nBossStep].y
				local t_set_r = g_main_pos[nBossStep].r

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

				if GetDist( t_self_x , t_self_y , t_set_x , t_set_y ) > 4 then
					ShowMessage("超时调整位置 %d" , tonumber(default_wait))
					AI:SetAdv( "主防御圈" , t_set_x , t_set_y , t_set_r )
					UpdateMsg("Quest_SJZ_Complete")
					default_wait = 5000
				else
					break 
				end
			end
		else
			if boss_done == true and GetEvent( "EVENT_PICKUP" , 0 ) > 0 then
				break 
			end
		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()
end


function _SMain(...)

	l_param_third = gscript_check( 0 , 0 , 1 )
	l_param_lzby = gscript_check( 1 , 0 , 1 )
	local ask_third = false 

	LogChatMsg(0)
	LogChatMsg(2)

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

		if GetActiveDataSceneID() ~= g_MissionMap then
		--不在副本内
			if nComplete == INVALID_ID then

				g_boss["陶青NPC"] = INVALID_ID 
				g_boss["庞企NPC"] = INVALID_ID 
				g_boss["秦韵NPC"] = INVALID_ID 
				g_boss["闵墨NPC"] = INVALID_ID
				g_boss["钟NPC"] = INVALID_ID

				-- 判断队伍人数
				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
				QuestFrameMissionAbnegate(g_idScript)
			end

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

				-- 获取当前所在区域的潘青青
				local t_self_x , t_self_y = Player:GetPos()
				local t_area = GetLocalID( g_MissionMap , t_self_x , t_self_y )
				if t_area < 1 then
					t_area = 1
				end

				local _,t_pqq_x , t_pqq_y = FindObjectByName( "潘菁菁" , "NPC")
				local t_pqq_dis = GetDist( t_pqq_x , t_pqq_y , g_pqq_pos[0].x ,g_pqq_pos[0].y )
				if t_pqq_dis < 6 then
					if Player:IsRangeAttack() == false then
						Pet_Relax()
						if INVALID_ID ~= Player:GetBuffByName("隐身") then
							--移动到钟旁边,并对话钟
							Dismount()
							UpdateMsg("Stop_UseItem")
							MoveTo(101,91,g_MissionMap,"" ,2,false,nil,false,1)
							UpdateMsg("Stop_UseItem")
							MoveTo(103,80,g_MissionMap,"" ,2,false,nil,false,1)
							UpdateMsg("Stop_UseItem")
							AI:SetAI("杀怪")
							AI:SetParameter("初始化")
							AI:SetParameter("地图", g_MissionMap)
							AI:SetParameter("坐标", "添加" , 103,80 )
							AI:SetParameter("目标" , "添加" , "四绝钟")
							AI:StartEx(0)
							AI:SetParameter("运行参数", "召唤珍兽" , 0 )
							AI:SetParameter("运行参数", "停止辅助技能" , 1 )
							AI:SetParameter("运行参数", "停止自身技能辅助" , 1 )
							AI:SetParameter("运行参数", "停止特殊攻击技能" , 1 )
							AI:StartEx(1)
							while true do 
								UpdateMsg("Stop_UseItem")
								local channel, time, name, data = GetChatMsg()
								local now = System:timeGetTime()
								if channel ~= INVALID_ID and now < time + 10 then 
									if string.find(data, "SJZ_100129_41") then
										if g_boss["钟NPC"] == INVALID_ID then
											g_boss["钟NPC"] = 99999
										end
										break
									elseif string.find(data,"护卫和教头听到钟声，纷纷离开了四绝殿") then
										if g_boss["钟NPC"] == INVALID_ID then
											g_boss["钟NPC"] = 99999
										end	
										break
									end
								end

								if INVALID_ID == Player:GetBuffByName("隐身") then
									break
								end
								System:Sleep(1000)
							end
							AI:Stop()
							DelMsg("Stop_UseItem")
						else
							Dismount()
							MoveToNPC(g_pqq_pos[0].x , g_pqq_pos[0].y , g_MissionMap , "潘菁菁" )
							QuestFrameOptionClicked("确定", CMP_PART_MATCH)
						end
					else
						OnFightBoss( 0 )
					end
				else
					local t_fight_pos = OnNpcEvent( t_area )
					DbgPrintf("当前战斗为 %d" , t_fight_pos)
					if t_fight_pos ~= INVALID_ID then
						OnFightBoss( t_fight_pos )
					end
				end
			end

		-- 判断是否在副本中
		end

		System:Sleep(1000)
	end

	LogChatMsg(-1)
end

_SMain()
