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