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

--[[
[author]
descript="չز..ճɽ!"
]]--

script_name="ɱ"


------------------------------------------------------------------------------------------------------------------
function FindGongSunSheng()

	local idNpc_A = INVALID_ID
	local idNpc_B = INVALID_ID

	local n = EnumObject(true)

	for i=0, n - 1 do
		local idObj, classObj, nameObj, xObj, yObj = EnumObject(false, i)
		if idObj ~= INVALID_ID then
			if classObj == "NPC" then
				if nameObj == "ʥ" then
					if Character:GetData(idObj, "DIE") == false then

						if idNpc_A ~= INVALID_ID then
							idNpc_B = idObj
							break
						end
						idNpc_A = idObj
					end
				end
			end
		end
	end

	return idNpc_A, idNpc_B
end


local g_boss = {
	[0] = {name="ʢ", fun=OnGuanSheng},
	[1] = {name="ʥ", fun=OnGongSongShen},
	[2] = {name="", fun=OnHuaRong},
	[3] = {name="", fun=OnOther},
	[4] = {name="", fun=OnLiKui},
	[5] = {name="ֳ", fun=OnOther},
	[6] = {name="¬", fun=OnLuJunYi},
	[7] = {name="³־", fun=OnLuZhiSheng},
	[8] = {name="ڤ", fun=OnOther},
	[9] = {name="ν", fun=OnSongJiang},
	[10] = {name="", fun=OnOther},
	[11] = {name="", fun=OnWuYong}
}


function FindBoss()
	local nFlags = CHA_RELATION_ENEMY|CHA_TYPE_NPC|CHA_OWNER_ANY|CHA_OCCUPANT_ANY|CHA_STATE_ALIVE

	return -1
end

function OnCopyScene()
	AI:SetAI("߼ս") 
	AI:SetParameter("ͼ" , 546)
	AI:Start()
	local nMoveTarget = -1

	local bRangeAttack = Player:IsRangeAttack()

	while true do

		System:Sleep(300)
		
		local nBossCount = 0
		local nBadBuffCount = 0 
		local bAttack = 1 
		local bDefend = 0
		local nLockCount = 0 
		local AttackMode = AI:GetAdv("")

		local id = FindObjectByName( "ʥ" , "")
		if INVALID_ID ~= id then
			local idNpc_A, idNpc_B = FindGongSunSheng()

			DbgPrintf("ҷ %X, %X", idNpc_A, idNpc_B)

			-- жϷǷ
			if idNpc_A ~= INVALID_ID and idNpc_B ~= INVALID_ID then
		
				local hpNpc_A = Character:GetData(idNpc_A, "HP_PERCENT")
				local hpNpc_B = Character:GetData(idNpc_B, "HP_PERCENT")

				DbgPrintf("ȽѪ %d, %d", hpNpc_A, hpNpc_B)
		
				-- ѡѪȽ϶һ
				if math.abs(hpNpc_A - hpNpc_B) > 10 then
					if hpNpc_A > hpNpc_B then
						idNpc_A = idNpc_A
					else
						idNpc_A = idNpc_B
					end
				end

				if idNpc_A ~= INVALID_ID then 
					nLockCount = nLockCount + 1
					AI:SetAdv("ָɱ" , idNpc_A )
				end
			end
		end	

		local id = FindObjectByName( "ν" , "")
		if INVALID_ID ~= id then
			if AttackMode == 1 then
				AI:SetAdv("в" , id , 7 )
			else
				AI:SetAdv("ɾв" , id )
			end
		end

		local id = FindObjectByName( "ʢ" , "")
		if INVALID_ID ~= id then
			local iPos,_,_ = CheckBufferByName( id, "")
			if INVALID_ID ~= iPos then
				--DbgPrintf("ͷ֮") 
				nMoveTarget = id 
			else 
				if AI:GetAdv("") == 2 then
					nMoveTarget = id  
				end
			end
		end

		local id = FindObjectByName( "¬" , "")
		if INVALID_ID ~= id then
			local iPos,_,_ = CheckBufferByName( id, "")
			if INVALID_ID ~= iPos then
				AI:SetAdv("в" , id , 3)
				bAttack = 0 
			else 
				AI:SetAdv("ɾв" , id )
			end
		end

		
		--"" or name == "" or name == "ɷ" 
		local nSpecialCount = 0
		local idSpecial = FindObjectByName("" , "")
		if INVALID_ID ~= idSpecial then
			AI:SetAdv("в" , idSpecial , 3 )
			nSpecialCount = nSpecialCount + 1
		end
		
		idSpecial = FindObjectByName("" , "")
		if INVALID_ID ~= idSpecial then
			AI:SetAdv("в" , idSpecial , 3 )
			nSpecialCount = nSpecialCount + 1
		end

		idSpecial = FindObjectByName("ɷ" , "")
		if INVALID_ID ~= idSpecial then
			AI:SetAdv("в" , idSpecial , 3 )
			nSpecialCount = nSpecialCount + 1
		end

		
		--ʢ   ֮ һ
		while true do 
			local channel, time, name, data = GetChatMsg()

			if channel == INVALID_ID then 
				break 
			end

			local now = System:timeGetTime()

			if channel == 0 and now < time + 2 then

				--DbgPrintf("%s", name)
				--DbgPrintf("%s", data)
				--DbgPrintf("%d %d [%d]", System:timeGetTime(), time, (System:timeGetTime() - time))
				--DbgPrintf(" ")
				-- ν ҪԶ빥 Ǽŭ

				if string.find(data, "֮") then
					DbgPrintf("⵽֮󡿣")
					--£
					local id,tx,ty = FindObjectByName("ʢ" , "")
					if INVALID_ID ~= id then
						nMoveTarget = id
					end
				end


				if string.find(data, "Ǿ") then
					--ɢ
					local id = FindObjectByName("ν" , "")
					if INVALID_ID ~= id then
						bDefend = 1
						AI:SetAdv("" , "" , 30000 ) 
					end
				end

				if string.find(data, "") then
					id,_,_,die,_,_,_ = FindObjectByName("ɫ" , "")
					if INVALID_ID ~= id and die == 0 then
						nLockCount = nLockCount + 1
						AI:SetAdv("ָɱ" , id)
					end
				end

				if string.find(data, "") then
					id,_,_,die,_,_,_ = FindObjectByName("ɫ" , "")
					if INVALID_ID ~= id and die == 0 then
						nLockCount = nLockCount + 1
						AI:SetAdv("ָɱ" , id)
					end
				end

				if string.find(data, "ױع") then
					id,_,_,die,_,_,_ = FindObjectByName("ɫ" , "")
					if INVALID_ID ~= id and die == 0 then
						nLockCount = nLockCount + 1
						AI:SetAdv("ָɱ" , id)
					end
				end
			end
		end


		if INVALID_ID ~= nMoveTarget then
			local idMove,moveX,moveY,die,_,_,_ = FindObjectByID( nMoveTarget , "" )
			if INVALID_ID ~= idMove and die == 0 then
				if IsDestination( moveX , moveY , -1 , 3 ) == false then
					AI:SetAdv("" , "ƶ" , moveX , moveY , 5000 ) 
					continue
				else
					nMoveTarget = -1
				end
			else
				nMoveTarget = -1
			end
		end


		if bAttack == 0 then
			AI:SetAdv("" , "" )
			continue 
		end 


		--жԼǷ
		local nPetID = Player:GetFightingPetID()
		if nPetID ~= -1 then
			local iPos,_,_ = CheckBufferByName( nPetID, "")
			if INVALID_ID ~= iPos then
				Pet_Relax()
			end
		end


		if bRangeAttack == false then
			local nHP = Player:GetData("HP")
			local nMaxHP = Player:GetData("MAXHP")
			local nHPPercent = (nHP * 100) / nMaxHP
			if nHPPercent < 15 then 
				AI:SetAdv("" , "" , 5000 )
			end
		end
		
		--DbgPrintf(" - ")
		AI:SetAdv("" , "")

	end
	AI:Stop()
end

--0x222
function _SMain(...)

	ShowMessage("ɱǣչز..ճɽ!")
	if true then 
		return
	end

	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 )
	AI:SetAdv( "Ȧ" , 30 , 33 , 5 )
	
	while true do

		-- жǷȡ
		local nIndex, idMission = DataPool:GetPlayerMission("ɱ")

		if nIndex == -1 then

			DbgPrintf("δȡ")

			MoveToNPC(131, 79, 2, "ٴʦ")
			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)

		else

			DbgPrintf("Ѿȡ")
			local sceneName = GetActiveSceneName()

			if DataPool:IsMissionCanCommit(idMission) then
				DbgPrintf("")
				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

			else
				if sceneName == "" then

					--if DataPool:IsTeamLeader() or g_die == true then
						MoveToNPC(131, 79, 2, "ٴʦ")
						QuestFrameOptionClicked("ɱ", CMP_FULL_MATCH)
						QuestFrameOptionClicked("ǰ̨", CMP_FULL_MATCH)
						System:Sleep(1500)
						local strType,_,_,_,szText = DataPool:GetNPCEventList_Item(0)
						if strType == "text" then
							if string.find(szText, "3") then
								ShowMessage("ˣű")
								break
							end
						end
					--end


				elseif sceneName == "̨" then
					DbgPrintf("Ѿ븱")
					OnCopyScene()
				else
					MoveToNPC(131, 79, 2, "ٴʦ")
				end
			end
		end

		System:Sleep(1000)
	end


	LogChatMsg(-1)
end

_SMain()
