ShowMessage("ʮɱ-̨ű $Rev: 212 $")

--[[
[author]
class=1
attr=1
]]--


script_name="ɱ"
local nSetNoFight = 0

local g_MissionMap = 546
local g_nParam = {}

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_boss = {
	["ʢ"] = INVALID_ID ,
	["ʥA"] = INVALID_ID ,
	["ʥB"] = INVALID_ID ,
	[""] = INVALID_ID ,
	[""] = INVALID_ID ,
	[""] = INVALID_ID ,
	["ֳ"] = INVALID_ID ,
	["¬"] = INVALID_ID ,
	["³־"] = INVALID_ID ,
	["ڤ"] = INVALID_ID ,
	["ν"] = INVALID_ID ,
	[""] = INVALID_ID ,
	[""] = INVALID_ID ,
	[""] = INVALID_ID ,
	[""] = INVALID_ID ,
	["ɷ"] = INVALID_ID ,
	["ɫ"] = INVALID_ID , 
	["ɫ"] = INVALID_ID ,
	["ɫ"] = INVALID_ID ,
	["ѡNPC"] = INVALID_ID ,
	["ǷĿ"] = INVALID_ID ,
	[""] = INVALID_ID 
}

--[[
	[0] = {x=14,y=33},
	[1] = {x=20,y=34},
	[2] = {x=14,y=40},
	[3] = {x=14,y=27},
	[4] = {x=19,y=39},
	[5] = {x=20,y=27}
]]--

local g_TeamPos = 
{
	[0] = {x=20,y=33},
	[1] = {x=27,y=33},
	[2] = {x=20,y=40},
	[3] = {x=20,y=26},
	[4] = {x=27,y=40},
	[5] = {x=27,y=26}
}
--30,33

function UpdateBossInfo()

	g_boss["ʢ"] = INVALID_ID 
	g_boss["ʥA"] = INVALID_ID 
	g_boss["ʥB"] = 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
	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["ѡNPC"] = INVALID_ID
	g_boss["ǷĿ"] = INVALID_ID 
	g_boss[""] = INVALID_ID

	local nBossCount = 0

	local n = EnumObject(true)

	for i=0, n - 1 do
		local idObj, classObj, nameObj, xObj, yObj = EnumObject(false, i)
		if idObj == INVALID_ID then
			continue
		end
		if classObj == "MON" then
			if nameObj == "ʢ" then
				if Character:GetData(idObj, "DIE") == false then
					g_boss["ʢ"] = idObj
				end
			elseif nameObj == "ʥ" then 
				if Character:GetData(idObj, "DIE") == false then
					if g_boss["ʥA"] == INVALID_ID then
						g_boss["ʥA"] = idObj
					else
						g_boss["ʥB"] = idObj
					end
				end
			elseif nameObj == "" then 
				if Character:GetData(idObj, "DIE") == false then
					g_boss[""] = idObj
				end
			elseif nameObj == "" then
				if Character:GetData(idObj, "DIE") == false then
					g_boss[""] = idObj
				end
			elseif nameObj == "" then
				DbgPrintf("%s %s %s" , tostring(nameObj) , tostring(classObj) , tostring(Character:GetData(idObj, "DIE")) )
				if Character:GetData(idObj, "DIE") == false then
					g_boss[""] = idObj
				end
			elseif nameObj == "ֳ" then
				if Character:GetData(idObj, "DIE") == false then
					g_boss["ֳ"] = idObj
				end
			elseif nameObj == "¬" then
				if Character:GetData(idObj, "DIE") == false then
					g_boss["¬"] = idObj
				end
			elseif nameObj == "³־" then
				if Character:GetData(idObj, "DIE") == false then
					g_boss["³־"] = idObj
				end
			elseif nameObj == "ڤ" then
				if Character:GetData(idObj, "DIE") == false then
					g_boss["ڤ"] = idObj
				end
			elseif nameObj == "ν" then
				DbgPrintf("%s %s %s" , tostring(nameObj) , tostring(classObj) , tostring(Character:GetData(idObj, "DIE")) )
				if Character:GetData(idObj, "DIE") == false then
					g_boss["ν"] = idObj
				end
			elseif nameObj == "" then
				if Character:GetData(idObj, "DIE") == false then
					g_boss[""] = idObj
				end
			elseif nameObj == "" then
				if Character:GetData(idObj, "DIE") == false then
					g_boss[""] = idObj
				end
			elseif nameObj == "ɫ" then
				if Character:GetData(idObj, "DIE") == false then
					g_boss["ɫ"] = idObj
				end
			elseif nameObj == "ɫ" then
				if Character:GetData(idObj, "DIE") == false then
					g_boss["ɫ"] = idObj
				end
			elseif nameObj == "ɫ" then
				if Character:GetData(idObj, "DIE") == false then
					g_boss["ɫ"] = idObj
				end
			end
			if Character:GetData(idObj, "DIE") == false then
				nBossCount = nBossCount + 1
			end
		elseif classObj == "SPECIAL" then
			if nameObj == "" then
				g_boss[""] = idObj
				AI:SetAdv("в" , idObj , 5 , "")
				nBossCount = nBossCount + 1
			elseif nameObj == "" then
				g_boss[""] = idObj
				AI:SetAdv("в" , idObj , 5 , "")
				nBossCount = nBossCount + 1
			elseif nameObj == "ɷ" then
				g_boss["ɷ"] = idObj
				AI:SetAdv("в" , idObj , 5 , "ɷ")
				nBossCount = nBossCount + 1
			end
		elseif classObj == "NPC" then
			if nameObj == "ʢ" then
				g_boss["ѡNPC"] = idObj
			elseif nameObj == "ʥ" then 
				g_boss["ѡNPC"] = idObj
			elseif nameObj == "" then 
				g_boss["ѡNPC"] = idObj
			elseif nameObj == "" then
				g_boss["ѡNPC"] = idObj
			elseif nameObj == "" then
				g_boss["ѡNPC"] = idObj
			elseif nameObj == "ֳ" then
				g_boss["ѡNPC"] = idObj
			elseif nameObj == "¬" then
				g_boss["ѡNPC"] = idObj
			elseif nameObj == "³־" then
				g_boss["ѡNPC"] = idObj
			elseif nameObj == "ڤ" then
				g_boss["ѡNPC"] = idObj
			elseif nameObj == "ν" then
				g_boss["ѡNPC"] = idObj
			elseif nameObj == "" then
				g_boss["ѡNPC"] = idObj
			elseif nameObj == "" then
				g_boss["ѡNPC"] = idObj
			end
		elseif classObj == "PLAYER" or classObj == "PET" then
			local iPos,_,_ = CheckBufferByName( idObj, "ɱ")
			if INVALID_ID ~= iPos then
				g_boss[""] = idObj
				nBossCount = nBossCount + 1
			end
		end
	end

	return nBossCount
end

function OnCopyScene()
	local pos , menpai = GetTeamStandPos()
	AI:SetAdv( "Ȧ" , 30 , 33 , 15 )
	AI:SetAI("߼ս") 
	AI:SetParameter("ͼ" , g_MissionMap)

	AI:StartEx(0)

	if nSetNoFight > 0 then
		AI:SetParameter("в" ,"", 0 )
		AI:SetParameter("в" ,"ɱ", 0 )
		AI:SetParameter("в" ,"Ӹ", 1)
		AI:SetParameter("в" ,"", 0)
		AI:SetParameter("в" ,"ֹͣ", 1)
	end

	AI:StartEx(1)

	local bRangeAttack = Player:IsRangeAttack()
	local pre_Yaoxin = "ǷĿ" 
	local pre_FingerAttack = INVALID_ID 
	local pre_GSS = 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 FingerAttack = -1
		local FingerAttackName = ""
		local FollowTarget = -1
		local mainguard_r = 6
		
		System:Sleep(300)

		local nBoss = UpdateBossInfo()

		if nBoss > 0 then
			UpdateMsg("Quest_SX_BossTick")
		end

		local nComplete = GetMissionComplete() 
		if nComplete == 0 then
			UpdateMsg("Quest_SX_Complete")
		elseif nComplete == 1 then
			if nBoss > 0 then
				UpdateMsg("Quest_SX_Complete")
			else
				if IsMsgCold("Quest_SX_Complete" , 5000) == true then
					break
				end
			end
		else
			break 
		end

		if GetActiveDataSceneID() ~= g_MissionMap then
			break 
		end

		-- жϷǷ
		if g_boss["ʥA"] ~= INVALID_ID and g_boss["ʥB"] ~= INVALID_ID then

			FingerAttack = g_boss["ʥA"]
			FingerAttackName = "ʥ"
			bYinshen =1 
--[[		
			local hpNpc_A = Character:GetData(g_boss["ʥA"], "HP_PERCENT")
			local hpNpc_B = Character:GetData(g_boss["ʥB"], "HP_PERCENT")

			DbgPrintf("ȽѪ %d, %d", hpNpc_A, hpNpc_B)
			if INVALID_ID == pre_GSS then
				pre_GSS = g_boss["ʥB"]
			end

			-- ѡѪȽ϶һ
			if (hpNpc_A > hpNpc_B)  then
				if math.abs(hpNpc_A - hpNpc_B) > 10 then
					pre_GSS = g_boss["ʥA"]
				end
			else
				if math.abs(hpNpc_B - hpNpc_A) > 10 then
					pre_GSS = g_boss["ʥB"]
				end	
			end

			if pre_GSS ~= INVALID_ID then 
				FingerAttack = pre_GSS
				FingerAttackName = "ʥ"
			end
]]--
		end

		local id = g_boss["ν"]
		if INVALID_ID ~= id then
			if bRangeAttack == true then
				AI:SetAdv("в" , id , 6 , "ν" )
			else
				if IsMsgCold("sx_songjiang", 15000) then
					AI:SetAdv("ɾв" , id )
				else
					AI:SetAdv("в" , id , 6 , "ν")
					bKillMonster = 0
				end
			end
		else
			AI:SetAdv( "ɾв" , "ν" )
		end

		id = g_boss["ʢ"]
		if INVALID_ID ~= id then
			FollowTarget = id
		end

		id = g_boss["³־"]
		if INVALID_ID ~= id then
			local iPos,_,_ = CheckBufferByName( id , "·")

			if INVALID_ID ~= iPos then
				bStopSpecialAttack = 1
			end

			--·,ֹͣ⼼ܹ
			--ձ,ֹbossѪ
			bCallPet = 0
		end

		--id = g_boss[""]
		--if INVALID_ID ~= id then
		--	--    5׾		
		--	mainguard_r = 2
		--end

		id = g_boss["¬"]
		local bForceNoCallPet = 0 
		if INVALID_ID ~= id then
			local iPos,_,_ = CheckBufferByName( id, "")
			if INVALID_ID ~= iPos then
				bKillMonster = 0
				bForceNoCallPet = 1 
				UpdateMsg("sx_lujunyi")
			else 
				if IsMsgCold("sx_lujunyi" , 5000) then
					bKillMonster = 1
				else
					bKillMonster = 0 
				end					
			end

			local iPos,_,_ = CheckBufferByName( id, "")
			if INVALID_ID ~= iPos then
				bStopSpecialAttack = 1
			end
		end

		
		if INVALID_ID == g_boss[""] then
			AI:SetAdv("ɾв" , "" )
		end

		if INVALID_ID == g_boss[""] then
			AI:SetAdv("ɾв" , "" )
		end

		if INVALID_ID == g_boss["ɷ"] then
			AI:SetAdv("ɾв" , "ɷ" )
		end

		if IsMsgCold("sx_yaoxin" , 10000) == true then
			if INVALID_ID == g_boss["ɫ"] and INVALID_ID == g_boss["ɫ"] and INVALID_ID == g_boss["ɫ"] then
				pre_Yaoxin = "ǷĿ"
			end
		end
		
		if INVALID_ID ~= g_boss[pre_Yaoxin] then
			FingerAttack = g_boss[pre_Yaoxin]
			FingerAttackName = pre_Yaoxin
		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("%s", data)
				--DbgPrintf("%d %d [%d]", System:timeGetTime(), time, (System:timeGetTime() - time))
				--DbgPrintf(" ")
				-- ν ҪԶ빥 Ǽŭ

				if string.find(data, "Ǿ") then
					if INVALID_ID ~= g_boss["ν"] then
						UpdateMsg("sx_songjiang")
					end
				end

				if string.find(data, "") then
					if INVALID_ID ~= g_boss["¬"] then
						UpdateMsg("sx_lujunyi") 
					end
				end

				if string.find(data, "") then
					pre_Yaoxin = "ɫ" 
					ShowMessage(" ɫ")
					UpdateMsg("sx_yaoxin")
				end
				if string.find(data, "") then
					pre_Yaoxin = "ɫ"
					ShowMessage(" ǻɫ")
					UpdateMsg("sx_yaoxin")
				end

				if string.find(data, "װع") then
					pre_Yaoxin = "ɫ"
					ShowMessage(" Ǻɫ")
					UpdateMsg("sx_yaoxin")
				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
			local nPetID = Player:GetFightingPetID()
			if nPetID == g_boss[""] then
				bForceNoCallPet = 1 
			elseif g_boss[""] == Player:GetData("ID") then
				bForceNoCallPet = 1
			else
				--Σն
				AI:SetAdv("в" , g_boss[""] , 6 , "")
			end
		else
			AI:SetAdv("ɾв" , "" )
		end

		--ԼѪ,ұʱ,ֹͣ
		if pos == 0 and menpai == 4 then
			bYinshen = 1

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

		if nBoss <= 0 and DataPool:IsTeamLeader() == true then
			MoveTo( 36 , 34 , g_MissionMap )
			System:Sleep(1000)
			nBoss = UpdateBossInfo()
			if nBoss <= 0 and INVALID_ID ~= g_boss["ѡNPC"] then
				local npc_id , npc_x , npc_y , _ , _ , _ , _ , npc_name = FindObjectByID( g_boss["ѡNPC"] , "NPC" )
				if INVLIAD_ID ~= npc_id then
					MoveToNPC( npc_x , npc_y , g_MissionMap , npc_name )
					QuestFrameOptionClicked("ս", CMP_PART_MATCH)
					System:Sleep(2000)
					AI:Start()
				end
			end
		end

		if bForceNoCallPet == 1 then
			bCallPet = 0 
		end

		if nSetNoFight > 0 then
			bKillMonster = 0 
			bYinGuai = 0 
			bTeamAttack = 0 
			bYinshen = 1
			bStopAssist = 1
		end

		AI:SetParameter("в" , "" , 1 )
		AI:SetParameter("ʰȡ" , bPickUp )
		AI:SetParameter("в" , "ɱ" , bKillMonster )
		AI:SetParameter("в" , "" , bYinGuai )
		AI:SetParameter("в" , "Ӹ" , bTeamFollow )
		AI:SetParameter("в" , "" , bTeamAttack )
		AI:SetParameter("в" , "ֹܸͣ" , bStopAssistSelf )
		AI:SetParameter("в" , "ֹͣ" , bStopAssist )
		AI:SetParameter("в" , "ֹͣ⹥" , bStopSpecialAttack )
		AI:SetParameter("в" , "ٻ" , bCallPet )

		AI:SetAdv("" , FollowTarget , 3 )
		AI:SetAdv("Ȧ뾶" , mainguard_r )
		AI:SetAdv("" , bYinshen )
		AI:SetAdv("Ȧ" , 0 )

		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

--0x222
function _SMain(...)

	nSetNoFight = GetScriptParam( script_name , "attr" , SCRIPT_ATTR_NOFIGHT )

	LogChatMsg(0)

	--սʼ
	AI:SetAdv( "" )
	AI:SetAdv( "ս" ,  14 , 17 , 46 , 17 )
	AI:SetAdv( "ս" ,  46 , 17 , 46 , 47 )
	AI:SetAdv( "ս" ,  14 , 17 , 14 , 47 )
	AI:SetAdv( "ս" ,  14 , 47 , 46 , 47 )

	while true do

		-- жǷȡ
		local nComplete = GetMissionComplete() 

		if nComplete == INVALID_ID then

			if GetQuestTimesInfo("ɱ") == 0 then
				ShowMessage("մɱ!")
				return
			end

			MoveToNPC(131, 79, 2, "ٴʦ")
			-- ж϶
			if DataPool:GetTeamMemCount() < 3 then 
				ShowMessage(" 3 ˣ")
				return
			end

			QuestFrameOptionClicked("ɱ", CMP_FULL_MATCH)

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

			if strType == "text" then
				if string.find(szText, "Ѿ") then
					ShowMessage("Ѿɱǣ")
					break	-- ˳ѭű
				end
			end

			QuestFrameAcceptClicked()
			System:Sleep(1000)

		elseif nComplete == 1 then
			
			local sceneName = GetActiveSceneName()	
			if sceneName == "̨" then
					
				MoveToNPC(12, 34, -1, "ٴʦ", MAX_TALK_RANGE, false)
				QuestFrameOptionClicked("ȷ", CMP_FULL_MATCH)

			else
				MoveToNPC(131, 79, 2, "ٴʦ")
				QuestFrameOptionClicked("ɱ", CMP_FULL_MATCH)
				QuestFrameMissionComplete()	-- 
			end

		elseif nComplete == 0 then

			local sceneName = GetActiveSceneName()
			if sceneName == "̨" then
				DbgPrintf("Ѿ븱")
				OnCopyScene()
			else
				MoveToNPC(131, 79, 2, "ٴʦ")
				QuestFrameOptionClicked("ɱ", CMP_FULL_MATCH)
				QuestFrameOptionClicked("ǰ̨", CMP_FULL_MATCH)
			end
		else
			QuestFrameMissionAbnegate(g_idScript)
		end

		System:Sleep(1000)
	end


	LogChatMsg(-1)
end

_SMain()
