ShowMessage("四绝庄")

--[[
[author]
descript="闭关测试中..择日出山!"
]]--

script_name="四绝庄"

local isJinZhan = false
local SpecialCount=0
local SpecialX={}
local SpecialY={}
local SpecialRR=20
local SpecialRR4=20

local CalcXSpan={-3,3,-4,4,-5,5,-6,6,-7,7,-8,8}
local CalcYSpan={-3,3,-4,4,-5,5,-6,6,-7,7,-8,8}

local Area0X=97
local Area0Y=108

local BossAreaLeftTopX={87,15,84,16}
local BossAreaLeftTopY={77,86,17,16}
local BossAreaXLength={28,27,28,26}
local BossAreaYLength={28,29,30,24}

local PanJingJingPosX={96,35,84,23}
local PanJingJingPosY={80,87,22,17}

--金龙柱 木龙柱 水龙柱 火龙柱 土龙柱
local Area4LongZhuX={41,35,23,21,28}  --金龙柱
local Area4LongZhuY={27,36,16,24,18}	

local bfoundboss3somebuff=false
local bfoundxiaoguai=false
local bfightxiaoguai=false


local bfighting_or_ed	={false,false,false,false}
local bSureFinished		={false,false,false,false}

function InitfightSomevar()

	bfighting_or_ed[1]=false
	bfighting_or_ed[2]=false
	bfighting_or_ed[3]=false
	bfighting_or_ed[4]=false
	
	bSureFinished[1]=false
	bSureFinished[2]=false
	bSureFinished[3]=false
	bSureFinished[4]=false
	
end

function CheckMissionFinished()
		local strMission = "四绝庄恩怨"		
		local nIndex, idMission, idScript = DataPool:GetPlayerMission(strMission)
		if nIndex ~= -1 then
			ShowMessage("已经有 四绝庄恩怨 任务 ")
			DbgPrintf("%d %d %d", nIndex, idMission, idScript)			
			if DataPool:IsMissionCanCommit(idMission) then
				return true
			end
		end		
		return false
end

function GetNearTeamMemCount()

	local xNow, yNow = Player:GetPos()
	local n = DataPool:GetTeamMemCount()
	local count = 0

	for i = 0, n - 1 do
		local name = DataPool:GetTeamMemInfo(i)

		if name ~= "" then
			local sid, x, y = FindCharacterByName(name)

			 if GetDistSq(xNow, yNow, x, y) < 400 then
				count = count + 1
			 end
		end
	end
	return count

end

local nexttime = -1
function IsSteady()

	local now = System:GetTickCount()

	if (nexttime ~= -1 and nexttime < now) or (GetNearTeamMemCount() >= DataPool:GetTeamMemCount() - 3) then
	
		nexttime = -1
		return true
	else
		if nexttime == -1 then
			nexttime = now + 3000
		end

		PushDebugMessage(string.format("等待队员！%d秒后自动开始", (nexttime - now)/1000))
		System:Sleep(1000)
	end

	return false
end


function CheckDlgText(DlgText)
	local nDlgCount = DataPool:GetNPCEventList_Num()
	for i=1,nDlgCount do
		local Type ,State ,ScriptID ,Index ,Text = DataPool:GetNPCEventList_Item(i)
		--DbgPrintf("%d %s",Index,Text)
		if string.find(Text, DlgText) then
			return true
		end
	end
	return false
end


-- 查找玩家自己的增益状态
function Player_GetBuff(szBuffName)

	local nBuffCount = Player:GetBuffNumber()

	for i = 1, nBuffCount do
		local szBuffToolTips = Player:GetBuffToolTipsByIndex(i-1)
		--DbgPrintf("%s",szBuffToolTips)
		if string.find(szBuffToolTips, szBuffName) then
			return (i-1)
		end
	end
	return -1
end


function CheckObjBuff(sid, strBuffName)

	local nBuffCount = Character:GetBuffNumber(sid)

	for i = 1, nBuffCount do
		local _, strInfo = Character:GetBuffIconNameByIndex(sid, i-1)
		if string.find(strInfo, strBuffName) then
			return true
		end
	end

	return false
end


function ShowObjBuff(sid)
	local nBuffCount = Character:GetBuffNumber(sid)

	for i = 1, nBuffCount do
		local _, strInfo = Character:GetBuffIconNameByIndex(sid, i-1)
		DbgPrintf("%s", strInfo)
	end
end

function OnRelive(npc)	
	--Relive_Out_Ghost()
	System:Sleep(3000)
end

function OnPickup()

	for i=0, 3 do
		AI:SetAI("拾取")
		AI:Start(true)
		System:Sleep(100)
	end

end

function OnKillXiaoGuaiWhenBoss3(npc)

	ShowMessage("杀小怪")
	ShowMessage(npc)
	local x, y = Player:GetPos()
	
	AI:Stop()
	AI:SetAI("杀怪")
	AI:SetParameter("初始化")
	AI:SetParameter("目标", "添加", npc)
	AI:SetParameter("地图", -1)	
	AI:Start(false)
	
	local nowtick=System:GetTickCount()
	while true do
	
		-- 角色死亡后的处理
		if Player:GetData("HP_PERCENT") <= 0 then
			DbgPrintf("角色死亡")
			OnRelive(npc)
			break
		end		
		
		local nCoutn = FindCharacterFirstByName(npc)
		if nCoutn==0 then
			DbgPrintf("打完了")
			break
		end		
					
		if System:GetTickCount()-nowtick>=60*1000 then
			break
		end			
				
				
		local sid,npcx,npcy,die = FindCharacterByName("陶青")
		--青鹰的守护：对周围的目标造成每秒5%的伤害				
		if CheckObjBuff(sid,"青鹰的守护") then						
				
				bfoundboss3somebuff=true			
				
				if isJinZhan then
					AI:Stop()
				end
				
				local px,py=Player:GetPos()																
				if  ( (px-npcx)*(px-npcx)+(py-npcy)*(py-npcy) ) <=6*6 then
					DbgPrintf("在boss旁边 要闪开")				
					
					local newx=px
					local newy=py
					local findgoodxy=false						
							
					for m=1,10 do
						for n=1,10 do								
								
							newx=px+CalcXSpan[m]
							newy=py+CalcYSpan[n]					
									
							--考虑下边界	
							if  (newx<(BossAreaLeftTopX[3]+1)) or (newx>(BossAreaLeftTopX[3]+BossAreaXLength[3]-1)) then
								continue
							end
							if  (newy<(BossAreaLeftTopY[3]+1)) or (newy>(BossAreaLeftTopY[3]+BossAreaYLength[3]-1)) then
								continue
							end
								
									
							if  ( (newx-npcx)*(newx-npcx)+(newy-npcy)*(newy-npcy) ) >(6*6) then
								findgoodxy=true
								break
							end
									
						end
						if findgoodxy then
							break
						end																
					end	
				
					AI:Stop()
				
					MoveTo(newx,newy,-1, "", 1, false)											
					System:Sleep(1000)
				
					if not isJinZhan then
						AI:Start(false)
					end
				end							
		end							

		if isJinZhan and bfoundboss3somebuff then
				bfoundboss3somebuff=false
				AI:Start(false)
		end
				
				
		System:Sleep(200)
		
	end
end
	
function OnKill(npc)

	ShowMessage("OnKill")
	ShowMessage(npc)

	local x, y = Player:GetPos()

	AI:SetAI("杀怪")
	AI:SetParameter("初始化")
	AI:SetParameter("目标", "添加", npc)
	AI:SetParameter("地图", -1)
	
	AI:Start(false)

	if npc == "秦韵" then
		Pet_Relax()
	end
	
	while true do

		-- 角色死亡后的处理
		if Player:GetData("HP_PERCENT") <= 0 then
			DbgPrintf("角色死亡")
			OnRelive(npc)
			break
		end
		if INVALID_ID == LockNearestTarget(npc, CHA_RELATION_ENEMY|CHA_TYPE_NPC|CHA_OWNER_ANY|CHA_OCCUPANT_ANY|CHA_STATE_ALIVE, "") then	
			AI:Stop()
			System:Sleep(2000)
			OnPickup()
			return true
		end

		local sid,npcx,npcy,die = FindCharacterByName(npc)

		if sid ~= INVALID_ID then
		
			if die == 1 then
				DbgPrintf("目标死亡")
				AI:Stop()
				System:Sleep(2000)
				OnPickup()
				return true			
			end		


			if npc == "闵墨" then
				
				--根据陷阱	
				System:Sleep(10)
			
				SpecialCount=0
				local nObjCount = EnumObject(true)
				for i=0, nObjCount - 1 do
					local idObj, class, nameObj, xObj, yObj, owner = EnumObject(false, i)
					if idObj ~= INVALID_ID then			
						-- 判断是否为陷阱
						if class == "Special" then
							-- 操作							
							DbgPrintf("陷阱 %s %d %d", nameObj,xObj,yObj)
							SpecialCount=SpecialCount+1
							SpecialX[SpecialCount]=xObj
							SpecialY[SpecialCount]=yObj	
						end
					end
				end

				if SpecialCount>0 then
					local px,py=Player:GetPos()
					local isInSpecial=false			
					for i=1, SpecialCount do
						if  ( (px-SpecialX[i])*(px-SpecialX[i])+(py-SpecialY[i])*(py-SpecialY[i]) ) <=SpecialRR then
							DbgPrintf("在陷阱中 %d %d", SpecialX[i],SpecialY[i])	
							isInSpecial=true
							break
						end
					end
					if isInSpecial then
						local newx=px
						local newy=py
						local findgoodxy=false
						for m=1,10 do
							for n=1,10 do
								
								newx=px+CalcXSpan[m]
								newy=py+CalcYSpan[n]								
							

								--考虑下边界

								if  newx<(BossAreaLeftTopX[1]+1) or newx>(BossAreaLeftTopX[1]+BossAreaXLength[1]-1) then
									continue
								end

								if  newy<(BossAreaLeftTopY[1]+1) or newy>(BossAreaLeftTopY[1]+BossAreaYLength[1]-1) then
									continue
								end

								

								local checkInSpecial=false
								
								for i=1, SpecialCount do
									if  ( (newx-SpecialX[i])*(newx-SpecialX[i])+(newy-SpecialY[i])*(newy-SpecialY[i]) ) <=SpecialRR then										
										checkInSpecial=true
										break
									end						
								end	
								if not checkInSpecial then
									DbgPrintf("避开陷阱，可以走到 %d %d", newx,newy)
									findgoodxy=true
								end					
								if findgoodxy then
									break
								end
							end
							if findgoodxy then
								break
							end
						end	
						ShowMessage("避开陷阱")
						AI:Stop()
						MoveTo(newx,newy,-1, "", 1, false)					
						System:Sleep(1000)
						AI:Start(false)
					end	
				end
				
			elseif npc == "秦韵" then
				--身上着火，收宠物？
				if Player_GetBuff("烛影摇红")>=0 then

				  ShowMessage("着火了，走远点")
					AI:Stop()
					
					AutoRunToTarget(BossAreaLeftTopX[2]+BossAreaXLength[2],BossAreaLeftTopY[2]+BossAreaYLength[2],-1, "")
					while Player_GetBuff("烛影摇红")>=0  do
			
						if Player:GetData("HP_PERCENT") <= 0 then
							DbgPrintf("角色死亡")
							OnRelive(npc)
							break
						end

						AutoRunToTarget(BossAreaLeftTopX[2]+BossAreaXLength[2],BossAreaLeftTopY[2]+BossAreaYLength[2],-1, "")
						System:Sleep(1000)
					end

					AutoRunToTarget(BossAreaLeftTopX[2]+BossAreaXLength[2]/2,BossAreaLeftTopY[2]+BossAreaYLength[2]/2,-1, "")	
					AI:Start(false)
				end

				--出现回音幡。判定回音幡的位置，所有角色往远离回音幡的角落走去。
				--boss应该在的位置 左上18,92 xy不应该超过6 
				--右上38，92  xy不应该超过6 
				--出现经幡后，往2个位置较远处引。
				

				local sid, x, y, die = FindCharacterByName("回音幡")

				if sid ~= INVALID_ID then
				 
				  ShowMessage("发现回音幡")
				  
					local px,py=Player:GetPos()

					if  ( (x-18)*(x-18)+(y-92)*(y-92) )< ( (x-38)*(x-38)+(y-92)*(y-92) ) then				
						ShowMessage("回音幡 靠近左上角")					
						if (BossAreaLeftTopX[2]+BossAreaXLength[2]-px)>10 or (py-BossAreaLeftTopY[2])>10 then
							ShowMessage("远离左上角，去右上角")	
							AI:Stop()
							MoveTo(BossAreaLeftTopX[2]+BossAreaXLength[2],BossAreaLeftTopY[2],-1, "", 1, false)				
							
							local now = System:GetTickCount()
							
							while true do
							
								local sid4, x4, y4, die4 = FindCharacterByName("秦韵")
								if GetDistSq(x4,y4,BossAreaLeftTopX[2]+BossAreaXLength[2],BossAreaLeftTopY[2])<=36 then
									break
								end
								
								if (System:GetTickCount()-now)>10000 then
									break
								end								
								System:Sleep(500)
							end
							
							AI:Start(false)
						end
					else
						ShowMessage("回音幡 靠近右上角")	
						if (px-BossAreaLeftTopX[2])>10 or (py-BossAreaLeftTopY[2])>10 then
							ShowMessage("远离右上角，去左上角")	
							AI:Stop()	
							MoveTo(BossAreaLeftTopX[2],BossAreaLeftTopY[2],-1, "", 1, false)		
							
							local now = System:GetTickCount()
							
							while true do
							
								local sid4, x4, y4, die4 = FindCharacterByName("秦韵")
								if GetDistSq(x4,y4,BossAreaLeftTopX[2],BossAreaLeftTopY[2])<=36 then
									break
								end
								
								if (System:GetTickCount()-now)>10000 then
									break
								end								
								System:Sleep(500)
							end
							
							
							AI:Start(false)		
						end						
					end									
				end			
						
			elseif npc == "陶青" then
				--出现黄圈，都进入


				--青鹰的守护：对周围的目标造成每秒5%的伤害				
				if CheckObjBuff(sid,"青鹰的守护") then						
				
						bfoundboss3somebuff=true																				
						
						if isJinZhan then
							AI:Stop()
						end
						
						local px,py=Player:GetPos()																
						if  ( (px-npcx)*(px-npcx)+(py-npcy)*(py-npcy) ) <=6*6 then
							DbgPrintf("在boss旁边 要闪开")												
							
							local newx=px
							local newy=py
							local findgoodxy=false						
							
							for m=1,10 do
								for n=1,10 do								
								
									newx=px+CalcXSpan[m]
									newy=py+CalcYSpan[n]					
									
									--考虑下边界	
									if  (newx<(BossAreaLeftTopX[3]+1)) or (newx>(BossAreaLeftTopX[3]+BossAreaXLength[3]-1)) then
										continue
									end
									if  (newy<(BossAreaLeftTopY[3]+1)) or (newy>(BossAreaLeftTopY[3]+BossAreaYLength[3]-1)) then
										continue
									end
								
									
									if  ( (newx-npcx)*(newx-npcx)+(newy-npcy)*(newy-npcy) ) >(6*6) then
										findgoodxy=true
										break
									end
									
								end
								if findgoodxy then
									break
								end																
							end	
						
							AI:Stop()
						
							MoveTo(newx,newy,-1, "", 1, false)											
							System:Sleep(1000)
						
							if not isJinZhan then
								AI:Start(false)
							end
						end							
				end							

				if isJinZhan and bfoundboss3somebuff then
						bfoundboss3somebuff=false
						AI:Start(false)
				end
				
				bfightxiaoguai=false
				bfoundxiaoguai=false
				
				--根据喊话 
				--潘菁菁：陶青的青鹰出现在盘龙阁的入口了，大家速去击退，不要让它们靠近陶青！
				for i=0, GetChatCount()-1 do
					local channel, time, name, data = GetChatMsg(i)			
							
					if channel == 2 then
					
						--DbgPrintf("%s", name)
						--DbgPrintf("%s", data)
						--DbgPrintf("channel=%d %d %d [%d]", channel,System:timeGetTime(), time, (System:timeGetTime() - time))
						--DbgPrintf(" ")
					
						if System:timeGetTime() < time + 3 then													
							if string.find(data, "陶青的青鹰出现在盘龙阁的入口了") then
								ShowMessage("陶青的青鹰出现在盘龙阁的入口了")									
								
								AI:Stop()
								MoveTo(BossAreaLeftTopX[3]+5,BossAreaLeftTopY[3]+BossAreaYLength[3],-1, "", 1, false)													
								
								--等青鹰
								local now = System:GetTickCount()							
								while true do							
									local sid4, x4, y4, die4 = FindCharacterByName("青鹰")
									if sid4~=INVALID_ID and die4==0 then									
										if GetDistSq(x4,y4,BossAreaLeftTopX[3]+5,BossAreaLeftTopY[3]+BossAreaYLength[3])<=16 then
											ShowMessage("青鹰来了")
											break
										end	
									end							
									
									if (System:GetTickCount()-now)>10000 then
										break
									end								
									System:Sleep(500)
								end
							
								OnKillXiaoGuaiWhenBoss3("青鹰")
								bfightxiaoguai=true													
								
							end
						end
					end
				end			
				
				
				
				bfoundxiaoguai=false
				
				while true do

					local sid4, x4, y4, die4 = FindCharacterByName("青鹰")
					if sid4~=INVALID_ID and die4==0 then
						OnKillXiaoGuaiWhenBoss3("青鹰")					
						bfightxiaoguai=true		
						bfoundxiaoguai=true
					end
					
					sid4, x4, y4, die4 = FindCharacterByName("冰墨虎")
					if sid4~=INVALID_ID and die4==0 then
						OnKillXiaoGuaiWhenBoss3("冰墨虎")	
						bfightxiaoguai=true						
						bfoundxiaoguai=true
					end
					
					sid4, x4, y4, die4 = FindCharacterByName("火墨虎")
					if sid4~=INVALID_ID and die4==0 then
						OnKillXiaoGuaiWhenBoss3("火墨虎")		
						bfightxiaoguai=true					
						bfoundxiaoguai=true
					end
					
					sid4, x4, y4, die4 = FindCharacterByName("玄墨虎")
					if sid4~=INVALID_ID and die4==0 then
						OnKillXiaoGuaiWhenBoss3("玄墨虎")	
						bfightxiaoguai=true						
						bfoundxiaoguai=true
					end
					
					sid4, x4, y4, die4 = FindCharacterByName("毒墨虎")
					if sid4~=INVALID_ID and die4==0 then
						OnKillXiaoGuaiWhenBoss3("毒墨虎")	
						bfightxiaoguai=true						
						bfoundxiaoguai=true
					end
				
					if not bfoundxiaoguai then
						DbgPrintf("没发现小怪")
						break
					else						
						DbgPrintf("发现了小怪，继续找")
						bfoundxiaoguai=false
					end
					System:Sleep(100)
				end						
				
				if bfightxiaoguai then	
									
					AI:Stop()
					AI:SetAI("杀怪")					
					AI:SetParameter("初始化")
					AI:SetParameter("目标", "添加", "陶青")
					AI:SetParameter("地图", -1)
					AI:Start(false)
				end
				
			elseif npc == "庞企" then

				local sid2, x2, y2, die2 = FindCharacterByName("星罗武士")

				if sid2 ~= INVALID_ID and die2==0 then
					local sid3, x3, y3, die3 = FindCharacterByName("庞企")
					if  (math.abs(x2-x3) +math.abs(y2-y3))<12 then
						
						ShowMessage("星罗武士来了")								
						--放宠物的猛招数
						UseSkill("五雷轰顶")
						UseSkill("天雷咒")
						UseSkill("咆哮")
					end
				end
				
				--根据喊话
				for i=0, GetChatCount()-1 do
					local channel, time, name, data = GetChatMsg(i)			
							
					if channel == 2 then
					
					--	DbgPrintf("%s", name)
						--DbgPrintf("%s", data)
						--DbgPrintf("channel=%d %d %d [%d]", channel,System:timeGetTime(), time, (System:timeGetTime() - time))
						--DbgPrintf(" ")
					
						if System:timeGetTime() < time + 3 then
													
							if string.find(data, "金龙柱旁边") then
								ShowMessage("走金龙柱旁边")								
								AI:Stop()
								MoveTo(Area4LongZhuX[1],Area4LongZhuY[1],-1, "", 1, false)					
								System:Sleep(2000)
								AI:Start(false)
							elseif string.find(data, "木龙柱旁边") then
								ShowMessage("走木龙柱旁边")								
								AI:Stop()
								MoveTo(Area4LongZhuX[2],Area4LongZhuY[2],-1, "", 1, false)					
								System:Sleep(2000)
								AI:Start(false)
							elseif string.find(data, "水龙柱旁边") then
								ShowMessage("走水龙柱旁边")								
								AI:Stop()
								MoveTo(Area4LongZhuX[3],Area4LongZhuY[3],-1, "", 1, false)					
								System:Sleep(2000)
								AI:Start(false)
							elseif string.find(data, "火龙柱旁边") then
								ShowMessage("走火龙柱旁边")		
								AI:Stop()
								MoveTo(Area4LongZhuX[4],Area4LongZhuY[4],-1, "", 1, false)					
								System:Sleep(2000)
								AI:Start(false)
							elseif string.find(data, "土龙柱旁边") then
								ShowMessage("走土龙柱旁边")		
								AI:Stop()
								MoveTo(Area4LongZhuX[5],Area4LongZhuY[5],-1, "", 1, false)					
								System:Sleep(2000)
								AI:Start(false)
							end
						end
					end
				end

				--根据陷阱	
			
				SpecialCount=0
				local nObjCount = EnumObject(true)
				for i=0, nObjCount - 1 do
					local idObj, class, nameObj, xObj, yObj, owner = EnumObject(false, i)
					if idObj ~= INVALID_ID then			
						-- 判断是否为陷阱
						if class == "Special" then
							-- 操作							
							DbgPrintf("陷阱 %s %d %d", nameObj,xObj,yObj)
							SpecialCount=SpecialCount+1
							SpecialX[SpecialCount]=xObj
							SpecialY[SpecialCount]=yObj	
						end
					end
				end

				if SpecialCount>0 then
					local px,py=Player:GetPos()
					local isInSpecial=false			
					for i=1, SpecialCount do
						if  ( (px-SpecialX[i])*(px-SpecialX[i])+(py-SpecialY[i])*(py-SpecialY[i]) ) <=SpecialRR4 then
							DbgPrintf("在陷阱中 %d %d", SpecialX[i],SpecialY[i])	
							isInSpecial=true
							break
						end
					end
					if isInSpecial then
						local newx=px
						local newy=py
						local findgoodxy=false
						
						for m=1,10 do
							for n=1,10 do
								
								newx=px+CalcXSpan[m]
								newy=py+CalcYSpan[n]								
							

								--考虑下边界

								if  newx<(BossAreaLeftTopX[4]+1) or newx>(BossAreaLeftTopX[4]+BossAreaXLength[4]-1) then
									continue
								end

								if  newy<(BossAreaLeftTopY[4]+1) or newy>(BossAreaLeftTopY[4]+BossAreaYLength[4]-1) then
									continue
								end

								

								local checkInSpecial=false
								
								for i=1, SpecialCount do
									if  ( (newx-SpecialX[i])*(newx-SpecialX[i])+(newy-SpecialY[i])*(newy-SpecialY[i]) ) <=SpecialRR4 then										
										checkInSpecial=true
										break
									end						
								end	
								if not checkInSpecial then
									DbgPrintf("避开陷阱可以走到 %d %d", newx,newy)
									findgoodxy=true
								end					
								if findgoodxy then
									break
								end
							end
							if findgoodxy then
								break
							end
						end	
						
						ShowMessage("避开陷阱")		
						AI:Stop()
						MoveTo(newx,newy,-1, "", 1, false)					
						System:Sleep(1000)
						AI:Start(false)
					end	
				end

			end
		end
		System:Sleep(300)
	end
	
	AI:Stop()
	return false
end

function OnHandleNpc(npc)
	
 	OnPickup()
	-- 击杀BOSS
	OnKill(npc)		

end

function OnDialogArea0()
		
		local px,py=Player:GetPos()
	
		--如果收到了潘菁菁的潜伏buff，走进打鼓的地方。	
		if Player_GetBuff("隐身")>=0 then
			--走到打鼓的地方。			
			AutoRunToTarget(103,80,-1, "四绝钟")
			return
			
		end 			
		
		
		--如果附近没有 潘菁菁，就都进去。
		local sid, x, y, die = FindCharacterByName("潘菁菁")
		if sid == INVALID_ID then
			ShowMessage("潘菁菁不在门口 走")
			AutoRunToTarget(PanJingJingPosX[1],PanJingJingPosY[1],-1,"")
			return
		end	
		
		if sid ~= INVALID_ID and GetDistSq(x, y, 99,112)>36 then
			ShowMessage("潘菁菁在，但是不在门口附近 走")
			AutoRunToTarget(PanJingJingPosX[1],PanJingJingPosY[1],-1,"")
			return
		
		else
				--发现潘菁菁在门口 ，收宠物，对话		
				ShowMessage("潘菁菁就在门口")
				if isJinZhan then
					Pet_Relax()
					MoveToNPC(100,114,-1, "潘菁菁")		
					if CheckDlgText("确定") then		
						QuestFrameOptionClicked("确定", 1)
						System:Sleep(1000)		
						return			
					end					
				end
		end
		
		--如果收到了潘菁菁的潜伏buff，走进打鼓的地方。	
		if Player_GetBuff("隐身")>=0 then
			--走到打鼓的地方。
			AutoRunToTarget(103,80,-1, "四绝钟")
			return
		end 		
		
end

function OnDialogArea1()
		
			local px,py=Player:GetPos()
			
			--如果收到了潘菁菁的潜伏buff，过去打鼓					
			if Player_GetBuff("隐身")>=0 then
			
				if px>=103-1 and px<=103+1 and py>=80-1 and py<=80+1 then
					local sid2, x2, y2, die2 = FindCharacterByName("四绝钟")
					if sid2~=INVALID_ID then
						UseSkill_Target("普通攻击","四绝钟")
						return
					end
				end
			
				--走到打鼓的地方。
				AutoRunToTarget(103,80,-1, "四绝钟")
				return
			end 
			
			--任务完成
			if CheckMissionFinished() then
				--任务已经完成 交任务
				ShowMessage("已经完成 四绝庄恩怨 任务 ")
				if px>=PanJingJingPosX[1]-2 and px<=PanJingJingPosX[1]+2	and py>=PanJingJingPosY[1]-2 and py<=PanJingJingPosY[1]+2 then				
					MoveToNPC(PanJingJingPosX[1],PanJingJingPosY[1],-1, "潘菁菁")		
					QuestFrameOptionClicked("离开副本", 1)
					return
				else
					AutoRunToTarget(PanJingJingPosX[1],PanJingJingPosY[1],-1,"")		
					return
				end
			end
			
			if bSureFinished[1] then
				DbgPrintf("战斗结束")
				if px>=PanJingJingPosX[1]-2 and px<=PanJingJingPosX[1]+2 and py>=PanJingJingPosY[1]-2 and py<=PanJingJingPosY[1]+2 then									
					MoveToNPC(PanJingJingPosX[1],PanJingJingPosY[1],-1, "潘菁菁")						
					if CheckDlgText("传送到星罗坛") then		
						QuestFrameOptionClicked("传送到星罗坛", 1)
						System:Sleep(1000)							
						return
					end
					if CheckDlgText("传送到盘龙阁") then		
						QuestFrameOptionClicked("传送到盘龙阁", 1)
						System:Sleep(1000)							
						return
					end
				
					if CheckDlgText("传送到西韵阁") then		
						QuestFrameOptionClicked("传送到西韵阁", 1)
						System:Sleep(1000)		
						return					
					end
				else		
					AutoRunToTarget(PanJingJingPosX[1],PanJingJingPosY[1],-1,"")		
					return
				end
			end
			
			if not bfighting_or_ed[1] then							
				--还没开始战斗，要启动战斗
				
				if px>=PanJingJingPosX[1]-2 and px<=PanJingJingPosX[1]+2 and py>=PanJingJingPosY[1]-2 and py<=PanJingJingPosY[1]+2 then				
									
					MoveToNPC(PanJingJingPosX[1],PanJingJingPosY[1],-1, "潘菁菁")						
					--如果有对话选项 开始战斗，就查看队友是否在周围。				
					if CheckDlgText("开始战斗") then	
						if IsSteady() then
							bfighting_or_ed[1]=true
							QuestFrameOptionClicked("开始战斗", 1)
							System:Sleep(1000)		
							return
						end
					end				
					
					--没有出现开始战斗，但是传送按钮，可能是死亡后再进来时战斗已经结束
					if CheckDlgText("传送到星罗坛") then		
						QuestFrameOptionClicked("传送到星罗坛", 1)
						System:Sleep(1000)							
						return
					end
					if CheckDlgText("传送到盘龙阁") then		
						QuestFrameOptionClicked("传送到盘龙阁", 1)
						System:Sleep(1000)							
						return
					end
				
					if CheckDlgText("传送到西韵阁") then		
						QuestFrameOptionClicked("传送到西韵阁", 1)
						System:Sleep(1000)		
						return					
					end
				else		
					AutoRunToTarget(PanJingJingPosX[1],PanJingJingPosY[1],-1,"")		
					return
				end
			else	--bfighting_or_ed[1]==true 正在战斗或者战斗刚结束
				if INVALID_ID ~= LockNearestTarget("闵墨", CHA_RELATION_FRIEND|CHA_TYPE_NPC|CHA_OWNER_ANY|CHA_OCCUPANT_ANY|CHA_STATE_ALIVE, "") then						
					--变友好了					
					DbgPrintf("看到boss 变友好了1")
					bSureFinished[1]=true
				else
					if     px>=BossAreaLeftTopX[1]+BossAreaXLength[1]/2-3
						 and px<=BossAreaLeftTopX[1]+BossAreaXLength[1]/2+3 
						 and py>=BossAreaLeftTopY[1]+BossAreaYLength[1]/2-3
						 and py>=BossAreaLeftTopY[1]+BossAreaYLength[1]/2+3 then
						
						--在中间							
						
						DbgPrintf("到中间观望")
						local bfindbossisfriend=false
						local bfindbossisenemy=false
						local now = System:GetTickCount()
						while true do
						 	if INVALID_ID ~= LockNearestTarget("闵墨", CHA_RELATION_FRIEND|CHA_TYPE_NPC|CHA_OWNER_ANY|CHA_OCCUPANT_ANY|CHA_STATE_ALIVE, "") then
						 		DbgPrintf("看到boss 变友好了2")
						 		bfindbossisfriend=true
						 		break
						 	end
						 	if INVALID_ID ~= LockNearestTarget("闵墨", CHA_RELATION_ENEMY|CHA_TYPE_NPC|CHA_OWNER_ANY|CHA_OCCUPANT_ANY|CHA_STATE_ALIVE, "") then
						 		DbgPrintf("看到boss 红名")
						 		bfindbossisenemy=true
						 		break
						 	end						 	
							if System:GetTickCount()-now>5000 then
						 		break
						 	end
						 	System:Sleep(300)
						end
						
						if bfindbossisfriend then
							bSureFinished[1]=true						
						else
							if not bfindbossisenemy then
								DbgPrintf("没看到")
								bSureFinished[1]=true
							end
						end
						 
					else		
						--走到中间		
						DbgPrintf("没看到boss 走到中间")
						AutoRunToTarget(BossAreaLeftTopX[1]+BossAreaXLength[1]/2,BossAreaLeftTopY[1]+BossAreaYLength[1]/2,-1,"")		
						return
					end
				end																	
			end
end

function OnDialogArea2()
		
			local px,py=Player:GetPos()
			
			--任务完成
			if CheckMissionFinished() then
				--任务已经完成 交任务
				ShowMessage("已经完成 四绝庄恩怨 任务 ")
				if px>=PanJingJingPosX[2]-2 and px<=PanJingJingPosX[2]+2	and py>=PanJingJingPosY[2]-2 and py<=PanJingJingPosY[2]+2 then				
					MoveToNPC(PanJingJingPosX[2],PanJingJingPosY[2],-1, "潘菁菁")		
					QuestFrameOptionClicked("离开副本", 1)
					return
				else
					AutoRunToTarget(PanJingJingPosX[2],PanJingJingPosY[2],-1,"")		
					return
				end
			end
			
			if bSureFinished[2] then
				DbgPrintf("战斗结束")
				if px>=PanJingJingPosX[2]-2 and px<=PanJingJingPosX[2]+2 and py>=PanJingJingPosY[2]-2 and py<=PanJingJingPosY[2]+2 then									
					MoveToNPC(PanJingJingPosX[2],PanJingJingPosY[2],-1, "潘菁菁")						
					if CheckDlgText("传送到星罗坛") then		
						QuestFrameOptionClicked("传送到星罗坛", 1)
						System:Sleep(1000)							
						return
					end
					if CheckDlgText("传送到盘龙阁") then		
						QuestFrameOptionClicked("传送到盘龙阁", 1)
						System:Sleep(1000)							
						return
					end				
				else		
					AutoRunToTarget(PanJingJingPosX[2],PanJingJingPosY[2],-1,"")		
					return
				end
			end
			
			if not bfighting_or_ed[2] then							
				--还没开始战斗，要启动战斗
				
				if px>=PanJingJingPosX[2]-2 and px<=PanJingJingPosX[2]+2 and py>=PanJingJingPosY[2]-2 and py<=PanJingJingPosY[2]+2 then				
									
					MoveToNPC(PanJingJingPosX[2],PanJingJingPosY[2],-1, "潘菁菁")						
					--如果有对话选项 开始战斗，就查看队友是否在周围。				
					if CheckDlgText("开始战斗") then	
						if IsSteady() then
							bfighting_or_ed[2]=true
							QuestFrameOptionClicked("开始战斗", 1)
							System:Sleep(1000)		
							return
						end
					end				
					
					--没有出现开始战斗，但是传送按钮，可能是死亡后再进来时战斗已经结束
					if CheckDlgText("传送到星罗坛") then		
						QuestFrameOptionClicked("传送到星罗坛", 1)
						System:Sleep(1000)							
						return
					end
					if CheckDlgText("传送到盘龙阁") then		
						QuestFrameOptionClicked("传送到盘龙阁", 1)
						System:Sleep(1000)							
						return
					end
				else		
					AutoRunToTarget(PanJingJingPosX[2],PanJingJingPosY[2],-1,"")		
					return
				end
			else	--bfighting_or_ed[2]==true 正在战斗或者战斗刚结束
				if INVALID_ID ~= LockNearestTarget("秦韵", CHA_RELATION_FRIEND|CHA_TYPE_NPC|CHA_OWNER_ANY|CHA_OCCUPANT_ANY|CHA_STATE_ALIVE, "") then						
					--变友好了					
					DbgPrintf("看到boss 变友好了1")
					bSureFinished[2]=true
				else
					if     px>=BossAreaLeftTopX[2]+BossAreaXLength[2]/2-3
						 and px<=BossAreaLeftTopX[2]+BossAreaXLength[2]/2+3 
						 and py>=BossAreaLeftTopY[2]+BossAreaYLength[2]/2-3
						 and py>=BossAreaLeftTopY[2]+BossAreaYLength[2]/2+3 then
						
						--在中间							
						
						DbgPrintf("到中间观望")
						local bfindbossisfriend=false
						local bfindbossisenemy=false
						local now = System:GetTickCount()
						while true do
						 	if INVALID_ID ~= LockNearestTarget("秦韵", CHA_RELATION_FRIEND|CHA_TYPE_NPC|CHA_OWNER_ANY|CHA_OCCUPANT_ANY|CHA_STATE_ALIVE, "") then
						 		DbgPrintf("看到boss 变友好了2")
						 		bfindbossisfriend=true
						 		break
						 	end
						 	if INVALID_ID ~= LockNearestTarget("秦韵", CHA_RELATION_ENEMY|CHA_TYPE_NPC|CHA_OWNER_ANY|CHA_OCCUPANT_ANY|CHA_STATE_ALIVE, "") then
						 		DbgPrintf("看到boss 红名")
						 		bfindbossisenemy=true
						 		break
						 	end						 	
							if System:GetTickCount()-now>5000 then
						 		break
						 	end
						 	System:Sleep(300)
						end
						
						if bfindbossisfriend then
							bSureFinished[2]=true						
						else
							if not bfindbossisenemy then
								DbgPrintf("没看到")
								bSureFinished[2]=true
							end
						end
						 
					else		
						--走到中间		
						DbgPrintf("没看到boss 走到中间")
						AutoRunToTarget(BossAreaLeftTopX[2]+BossAreaXLength[2]/2,BossAreaLeftTopY[2]+BossAreaYLength[2]/2,-1,"")		
						return
					end
				end																	
			end

end
function OnDialogArea3()
		
			local px,py=Player:GetPos()
					
			--任务完成
			if CheckMissionFinished() then
				--任务已经完成 交任务
				ShowMessage("已经完成 四绝庄恩怨 任务 ")
				if px>=PanJingJingPosX[3]-2 and px<=PanJingJingPosX[3]+2	and py>=PanJingJingPosY[3]-2 and py<=PanJingJingPosY[3]+2 then				
					MoveToNPC(PanJingJingPosX[3],PanJingJingPosY[3],-1, "潘菁菁")		
					QuestFrameOptionClicked("离开副本", 1)
					return
				else
					AutoRunToTarget(PanJingJingPosX[3],PanJingJingPosY[3],-1,"")		
					return
				end
			end
			
			if bSureFinished[3] then
				DbgPrintf("战斗结束")
				if px>=PanJingJingPosX[3]-2 and px<=PanJingJingPosX[3]+2 and py>=PanJingJingPosY[3]-2 and py<=PanJingJingPosY[3]+2 then									
					MoveToNPC(PanJingJingPosX[3],PanJingJingPosY[3],-1, "潘菁菁")						
					if CheckDlgText("传送到星罗坛") then		
						QuestFrameOptionClicked("传送到星罗坛", 1)
						System:Sleep(1000)							
						return
					end					
				else		
					AutoRunToTarget(PanJingJingPosX[3],PanJingJingPosY[3],-1,"")		
					return
				end
			end
			
			if not bfighting_or_ed[3] then							
				--还没开始战斗，要启动战斗
				
				if px>=PanJingJingPosX[3]-2 and px<=PanJingJingPosX[3]+2 and py>=PanJingJingPosY[3]-2 and py<=PanJingJingPosY[3]+2 then				
									
					MoveToNPC(PanJingJingPosX[3],PanJingJingPosY[3],-1, "潘菁菁")						
					--如果有对话选项 开始战斗，就查看队友是否在周围。				
					if CheckDlgText("开始战斗") then	
						if IsSteady() then
							bfighting_or_ed[3]=true
							QuestFrameOptionClicked("开始战斗", 1)
							System:Sleep(1000)		
							return
						end
					end				
					
					--没有出现开始战斗，但是传送按钮，可能是死亡后再进来时战斗已经结束				
					if CheckDlgText("传送到星罗坛") then		
						QuestFrameOptionClicked("传送到星罗坛", 1)
						System:Sleep(1000)							
						return
					end		
				else		
					AutoRunToTarget(PanJingJingPosX[3],PanJingJingPosY[3],-1,"")		
					return
				end
			else	--bfighting_or_ed[3]==true 正在战斗或者战斗刚结束
				if INVALID_ID ~= LockNearestTarget("陶青", CHA_RELATION_FRIEND|CHA_TYPE_NPC|CHA_OWNER_ANY|CHA_OCCUPANT_ANY|CHA_STATE_ALIVE, "") then						
					--变友好了					
					DbgPrintf("看到boss 变友好了1")
					bSureFinished[3]=true
				else
					if     px>=BossAreaLeftTopX[3]+BossAreaXLength[3]/2-3
						 and px<=BossAreaLeftTopX[3]+BossAreaXLength[3]/2+3 
						 and py>=BossAreaLeftTopY[3]+BossAreaYLength[3]/2-3
						 and py>=BossAreaLeftTopY[3]+BossAreaYLength[3]/2+3 then
						
						--在中间							
						
						DbgPrintf("到中间观望")
						local bfindbossisfriend=false
						local bfindbossisenemy=false
						local now = System:GetTickCount()
						while true do
						 	if INVALID_ID ~= LockNearestTarget("陶青", CHA_RELATION_FRIEND|CHA_TYPE_NPC|CHA_OWNER_ANY|CHA_OCCUPANT_ANY|CHA_STATE_ALIVE, "") then
						 		DbgPrintf("看到boss 变友好了2")
						 		bfindbossisfriend=true
						 		break
						 	end
						 	if INVALID_ID ~= LockNearestTarget("陶青", CHA_RELATION_ENEMY|CHA_TYPE_NPC|CHA_OWNER_ANY|CHA_OCCUPANT_ANY|CHA_STATE_ALIVE, "") then
						 		DbgPrintf("看到boss 红名")
						 		bfindbossisenemy=true
						 		break
						 	end						 	
							if System:GetTickCount()-now>5000 then
						 		break
						 	end
						 	System:Sleep(300)
						end
						
						if bfindbossisfriend then
							bSureFinished[3]=true						
						else
							if not bfindbossisenemy then	
								DbgPrintf("没看到")
								bSureFinished[3]=true
							end
						end
						 
					else		
						--走到中间		
						DbgPrintf("没看到boss 走到中间")
						AutoRunToTarget(BossAreaLeftTopX[3]+BossAreaXLength[3]/2,BossAreaLeftTopY[3]+BossAreaYLength[3]/2,-1,"")		
						return
					end
				end																	
			end

end
function OnDialogArea4()
		
			local px,py=Player:GetPos()
						
			--任务完成
			if CheckMissionFinished() then
				--任务已经完成 交任务
				ShowMessage("已经完成 四绝庄恩怨 任务 ")
				if px>=PanJingJingPosX[4]-2 and px<=PanJingJingPosX[4]+2	and py>=PanJingJingPosY[4]-2 and py<=PanJingJingPosY[4]+2 then				
					MoveToNPC(PanJingJingPosX[4],PanJingJingPosY[4],-1, "潘菁菁")		
					QuestFrameOptionClicked("离开副本", 1)
					return
				else
					AutoRunToTarget(PanJingJingPosX[4],PanJingJingPosY[4],-1,"")		
					return
				end
			end			
			
			
			if not bfighting_or_ed[4] then							
				
				--还没开始战斗，要启动战斗					
				if px>=PanJingJingPosX[4]-2 and px<=PanJingJingPosX[4]+2 and py>=PanJingJingPosY[4]-2 and py<=PanJingJingPosY[4]+2 then													
					MoveToNPC(PanJingJingPosX[4],PanJingJingPosY[4],-1, "潘菁菁")						
					--如果有对话选项 开始战斗，就查看队友是否在周围。				
					if CheckDlgText("上去取宝") then						
						QuestFrameOptionClicked("上去取宝", 1)
						System:Sleep(1000)					
					end
								
					if CheckDlgText("将功补过，协助四绝庄抵御外敌") then	
						if IsSteady() then
							bfighting_or_ed[4]=true
							QuestFrameOptionClicked("将功补过，协助四绝庄抵御外敌", 1)
							System:Sleep(1000)		
						end
					end
					return						
				else		
					AutoRunToTarget(PanJingJingPosX[4],PanJingJingPosY[4],-1,"")		
					return
				end
				
			else	
					--走到中间		
					
					DbgPrintf("没看到boss 走到中间")
					AutoRunToTarget(BossAreaLeftTopX[4]+BossAreaXLength[4]/2,BossAreaLeftTopY[4]+BossAreaYLength[4]/2,-1,"")		
					return					
			end

end

function OnDialog()

	local px,py=Player:GetPos()

	--刚进来的时候
	if px>=Area0X and py>=Area0Y then
	
		ShowMessage("刚进来")			
		OnDialogArea0()
		return
	end
	
	
	--在boss1区域
	if px>=BossAreaLeftTopX[1] and px<=BossAreaLeftTopX[1]+BossAreaXLength[1] 
		and py>=BossAreaLeftTopY[1] and py<= BossAreaLeftTopY[1]+BossAreaYLength[1] then		
			
			ShowMessage("在四绝殿")			
			OnDialogArea1()	
			return
	end
	
	if px>=73-2 and px<=73+2 and py>=92-2 and py<=92+2 then
		
		ShowMessage("在四绝殿到西韵阁路上")
		AutoRunToTarget(PanJingJingPosX[2],PanJingJingPosY[2],-1,"")		
		return			
	end
	
	
	
	if px>=BossAreaLeftTopX[2] and px<=BossAreaLeftTopX[2]+BossAreaXLength[2] 
		and py>=BossAreaLeftTopY[2] and py<= BossAreaLeftTopY[2]+BossAreaYLength[2] then		
			
			ShowMessage("在 西韵阁")
			OnDialogArea2()	
			return
	end
	
	if px>=40-2 and px<=40+2 and py>=76-2 and py<=76+2 then
		
		ShowMessage("在西韵阁到盘龙阁 路上")
		AutoRunToTarget(PanJingJingPosX[3],PanJingJingPosY[3],-1,"")		
		return			
	end
	
	if px>=BossAreaLeftTopX[3] and px<=BossAreaLeftTopX[3]+BossAreaXLength[3] 
		and py>=BossAreaLeftTopY[3] and py<= BossAreaLeftTopY[3]+BossAreaYLength[3] then		
			
			ShowMessage("在 盘龙阁")
			OnDialogArea3()	
			return
	end
	
	if px>=70-2 and px<=70+2 and py>=29-2 and py<=29+2 then
		
		ShowMessage("在盘龙阁  到  星罗坛 路上")
		AutoRunToTarget(PanJingJingPosX[4],PanJingJingPosY[4],-1,"")		
		return			
	end
	
	if px>=BossAreaLeftTopX[4] and px<=BossAreaLeftTopX[4]+BossAreaXLength[4] 
		and py>=BossAreaLeftTopY[4] and py<= BossAreaLeftTopY[4]+BossAreaYLength[4] then		
			
			ShowMessage("在 星罗坛")		
			OnDialogArea4()	
			return
	end		
	
end
function Process()
local sceneName = GetActiveSceneName()

	if sceneName == "四绝庄" then		
		
		
		--发现潜伏失败后出现的小怪，也得打
		
				
		if INVALID_ID ~= LockNearestTarget("闵墨", CHA_RELATION_ENEMY|CHA_TYPE_NPC|CHA_OWNER_ANY|CHA_OCCUPANT_ANY|CHA_STATE_ALIVE, "") then	
			bfighting_or_ed[1]=true
			OnHandleNpc("闵墨")			
			return
		end

		if INVALID_ID ~= LockNearestTarget("秦韵", CHA_RELATION_ENEMY|CHA_TYPE_NPC|CHA_OWNER_ANY|CHA_OCCUPANT_ANY|CHA_STATE_ALIVE, "") then	
			bfighting_or_ed[2]=true
			OnHandleNpc("秦韵")
			return
		end

		if INVALID_ID ~= LockNearestTarget("陶青", CHA_RELATION_ENEMY|CHA_TYPE_NPC|CHA_OWNER_ANY|CHA_OCCUPANT_ANY|CHA_STATE_ALIVE, "") then	
			OnHandleNpc("陶青")
			bfighting_or_ed[3]=true
			return
		end

		if INVALID_ID ~= LockNearestTarget("庞企", CHA_RELATION_ENEMY|CHA_TYPE_NPC|CHA_OWNER_ANY|CHA_OCCUPANT_ANY|CHA_STATE_ALIVE, "") then	
			bfighting_or_ed[4]=true
			OnHandleNpc("庞企")		
			return
		end
		
		OnDialog()
		
		return
	elseif sceneName=="苏州" then		
		
		InitfightSomevar()
				
		if CheckMissionFinished() then
			--任务已经完成 交任务
				ShowMessage("已经完成 四绝庄恩怨 任务 ")
				MoveToNPC(195,214,DataBase:GetSceneID("苏州"), "潘菁菁")	
				System:Sleep(1500)
				QuestFrameOptionClicked("四绝庄恩怨", 1)
				System:Sleep(1500)
				QuestFrameMissionComplete()	-- 完成				
				System:Sleep(1500)
				return
		end		

		--任务没有或者没完成时
		MoveToNPC(195,214,DataBase:GetSceneID("苏州"), "潘菁菁")	
		System:Sleep(1500)
		QuestFrameOptionClicked("四绝庄恩怨", 1)
		System:Sleep(1500)

		local strMission = "四绝庄恩怨"		
		local nIndex, idMission, idScript = DataPool:GetPlayerMission(strMission)
		if nIndex == -1 then
			QuestFrameAcceptClicked()
			System:Sleep(1500)		
		end

		QuestFrameOptionClicked("前往四绝庄", 1)
		
		return
	end
		
	MoveToNPC(195,214,DataBase:GetSceneID("苏州"), "潘菁菁")
end

function _SMain(...)     
 	
	ShowMessage("四绝庄脚本 闭关测试中..择日出山!")
	if true then 
		return
	end
	
	local menpai=Player:GetData("MENPAI")	
	if menpai==0 or menpai==7 or menpai==1 or menpai==10 or menpai==2 then
		isJinZhan=true
	end
	
	while true do	 				
		Process()
		System:Sleep(1000)		
	end
end

_SMain()