ShowMessage("ս翷 $Rev: 20 $")

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

script_name="翷"

local state = ""
local nexttime = -1
local checktime = -1
local count = 0

-- 븱 >>  >> ɣ >> ϴ >> ƽ&׿ >> ƽ >> ˮ >> 뿪

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) < 80 then
				count = count + 1
			 end
		end
	end
	return count

end

function IsSteady()

	local now = System:GetTickCount()

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

		PushDebugMessage(string.format("ȴԱ%dԶʼ", (nexttime - now)/1000))
		System:Sleep(1000)
	end

	return false
end

function OnRelive(npc)
	
	Relive_Out_Ghost()
	System:Sleep(3000)
	--local x, y = Player:GetPos()
	--MoveTo(x, y, -1, "", 1)

end

function OnCheck()

	local now = System:GetTickCount()

	if checktime ~= -1 then

		if checktime < now then

			MoveTo(125, 40, -1, "", 1)

			local sid, _, _, die = FindCharacterByName("ˮ")

			if sid == INVALID_ID then
				
				MoveToNPC(158, 54 , -1, "")
				System:Sleep(1500)

				if FindQuestFrameOption("ս", CMP_PART_MATCH) == false then

					MoveToNPC(96, 41 , -1, "ϴ")
					System:Sleep(1500)

					if FindQuestFrameOption("սˮ", CMP_PART_MATCH) == false then

						return true
					end
					
				end

			end
			
			MoveTo(125, 35, -1, "", 1)
			checktime = -1
		else
			PushDebugMessage(string.format("%dǷ", (checktime - now)/1000))
		end
	else
		checktime = now + 37000
	end

	return false
end

function OnPickup()

	AI:SetParameter("")
	AI:SetAI("ɱ")
	AI:SetParameter("ʼ")
	AI:SetParameter("ʰȡ" , 1 )
	AI:SetParameter("ͼ" , -1)
	AI:Start(true)
		
	AI:SetParameter("в" , "ɱ" , 0 )

	for i=0, 3 do
		if LockNearestTarget( "" , CHA_TYPE_BAG ) == -1 then
			break 
		end

		System:Sleep(1000)
	end
	
	AI:Stop()
	AI:SetParameter("ָ")

end

function OnKill(npc)

	local x, y = Player:GetPos()

	AI:SetAI("ɱ")
	AI:SetParameter("ʼ")
	AI:SetParameter("Ŀ", "", npc)
	AI:SetParameter("ͼ", -1)
	
	AI:Start(false)

	while true do

		-- ɫĴ
		if Player:GetData("HP_PERCENT") <= 0 then
			DbgPrintf("ɫ")
			OnRelive(npc)
			break
		end

		local sid, _, _, die = FindCharacterByName(npc)

		if sid == INVALID_ID then

			if npc == "ɣ" then

				sid, x, y, die = FindCharacterByName("׽ʬ")

				if sid == INVALID_ID then
					DbgPrintf("ûҵ׽ʬ")
					--break
				else
					if Player_GetBuff("") == -1 then
						UseSkill("")
					end
				end
			else
				DbgPrintf("ûҵBOSS")
				break
			end

		else
			if die == 1 then
				DbgPrintf("Ŀ")
				AI:Stop()
				System:Sleep(2000)
				OnPickup()
				return true
			end

			while npc == "ˮ" do
				local channel, time, name, data = GetChatMsg(i)
				if channel == INVALID_ID then
					break 
				end
					
				if channel == 0 then
					if System:timeGetTime() < time + 3 then
								
						DbgPrintf("%s", name)
						DbgPrintf("%s", data)
						DbgPrintf("%d %d [%d]", System:timeGetTime(), time, (System:timeGetTime() - time))
						DbgPrintf(" ")

						if string.find(data, "") then
							AI:Stop()
							MoveTo(113, 29, -1, "", 1, false)
							System:Sleep(1000)
							MoveTo(125, 40, -1, "", 1, false)
							AI:Start(false)
						end

						if string.find(data, "̹ǵĺ") then
							AI:Stop()
							MoveTo(135, 29, -1, "", 1, false)
							System:Sleep(1000)
							MoveTo(125, 40, -1, "", 1, false)
							AI:Start(false)
						end

						if string.find(data, "֮") then
							AI:Stop()
							MoveTo(146, 48, -1, "", 1, false)
							System:Sleep(1000)
							MoveTo(125, 40, -1, "", 1, false)
							AI:Start(false)
						end

						if string.find(data, "") then
							AI:Stop()
							MoveTo(136, 67, -1, "", 1, false)
							System:Sleep(1000)
							MoveTo(125, 40, -1, "", 1, false)
							AI:Start(false)
						end

						if string.find(data, "׺䶥") then
							AI:Stop()
							MoveTo(114, 67, -1, "", 1, false)
							System:Sleep(1000)
							MoveTo(125, 40, -1, "", 1, false)
							AI:Start(false)
						end

						if string.find(data, "") then
							AI:Stop()
							MoveTo(102, 48, -1, "", 1, false)
							System:Sleep(1000)
							MoveTo(125, 40, -1, "", 1, false)
							AI:Start(false)
						end
					end
				end
			end
		end
		System:Sleep(500)
	end
	
	AI:Stop()
	return false
end

--------------------------------------------------------------
-- 
function OnHaDaBa()

	MoveTo(124, 92, -1, "", 1)

	for i = 1, 5 do

		OnPickup()

		local sid, x, y, die = FindCharacterByName("")

		if sid ~= INVALID_ID then
			
			if die == 0 then
				DbgPrintf("֡ԡ")

				-- жϹǷ񼤻
				if INVALID_ID == LockNearestTarget("", CHA_RELATION_ENEMY|CHA_TYPE_NPC|CHA_OWNER_ANY|CHA_OCCUPANT_ANY|CHA_STATE_ALIVE, "") then	
					
					-- BOSSδ
					if DataPool:IsTeamLeader() then
						
						if IsSteady() then
							-- NPC
							MoveToNPC(124, 87, -1, "")
							QuestFrameOptionClicked("ս", CMP_PART_MATCH)
							System:Sleep(1000)
						end

					else
						-- Ա
						DbgPrintf("ȴӳԡ")
					end

				else	
					-- ɱBOSS
					if OnKill("") then
						state = "ɣ"
					end
				end

			else
				state = "ɣ"
			end

			return
		end

		System:Sleep(1000)
	end

	-- ִһ
	state = "ɣ"

end

--------------------------------------------------------------
-- ɣ
function OnSangTuGong()

	MoveTo(40, 106, -1, "", 1)

	for i = 1, 5 do

		OnPickup()

		local sid, x, y, die = FindCharacterByName("ɣ")

		if sid ~= INVALID_ID then
			
			if die == 0 then
				DbgPrintf("֡ɣ")

				-- жɣǷ񼤻
				if INVALID_ID == LockNearestTarget("ɣ", CHA_RELATION_ENEMY|CHA_TYPE_NPC|CHA_OWNER_ANY|CHA_OCCUPANT_ANY|CHA_STATE_ALIVE, "") then		
					
					-- BOSSδ
					if DataPool:IsTeamLeader() then

						if IsSteady() then

							MoveToNPC(124, 87, -1, "ɣ")
							QuestFrameOptionClicked("ս", CMP_PART_MATCH)
							System:Sleep(1500)
							local strType,_,_,_,szText = DataPool:GetNPCEventList_Item(0)

							if strType == "text" then
								if string.find(szText, "㻹Ҷ") then
									state = ""
									return
								end
							end
							System:Sleep(1000)
						end

					else
						-- Ա
						DbgPrintf("ȴӳɣ")
					end

				else	
					-- ɱBOSS
					if OnKill("ɣ") then
						state = "ϴ"
					end
				end

			else
				state = "ϴ"
			end

			return
		end

		sid, x, y, die = FindCharacterByName("׽ʬ")

		if sid ~= INVALID_ID then

			-- ɱBOSS
			if OnKill("ɣ") then
				state = "ϴ"
			end
			return
		end

		System:Sleep(1000)
	end

	-- ִһ
	state = "ϴ"

end

--------------------------------------------------------------
-- ϴ
function OnWuLaoDa()

	MoveTo(116, 50, -1, "", 1)

	for i = 1, 5 do

		OnPickup()

		local sid, x, y, die = FindCharacterByName("ϴ")

		if sid ~= INVALID_ID then
			
			if die == 0 then
				DbgPrintf("֡ϴ")

				-- жϴǷ񼤻
				if INVALID_ID == LockNearestTarget("ϴ", CHA_RELATION_ENEMY|CHA_TYPE_NPC|CHA_OWNER_ANY|CHA_OCCUPANT_ANY|CHA_STATE_ALIVE, "") then	
				
					-- BOSSδ

					if DataPool:IsTeamLeader() then

						-- ӳNPC
						if IsSteady() then

							MoveToNPC(124, 87, -1, "ϴ")
							QuestFrameOptionClicked("ս", CMP_PART_MATCH)
							System:Sleep(1500)
							local strType,_,_,_,szText = DataPool:GetNPCEventList_Item(0)

							if strType == "text" then
								if string.find(szText, "㻹Ҷ") then
									state = ""
									return
								end
							end
							System:Sleep(1000)
						end
					else
						-- Ա
						DbgPrintf("ȴӳϴ")
					end

				else	
					-- ɱBOSS
					if OnKill("ϴ") then
						state = "ƽ"
					end
				end

			else
				state = "ƽ"
			end

			return
		end

		System:Sleep(1000)
	end

	-- ִһ
	state = "ƽ"

end

--------------------------------------------------------------
-- ƽ
function OnBuPingDaoRen()

	MoveTo(125, 35, -1, "", 1)

	for i = 1, 3 do

		OnPickup()

		local sid, x, y, die = FindCharacterByName("ƽ")

		if sid ~= INVALID_ID then
			
			if die == 0 then
				DbgPrintf("֡ƽˡ")

				-- ɱBOSS
				if OnKill("ƽ") then
					state = "׿"
				end

			else
				state = "׿"
			end

			return
		end

		System:Sleep(1000)
	end

	if DataPool:IsTeamLeader() then

		if IsSteady() then
			MoveToNPC(158, 54 , -1, "")
			System:Sleep(1500)

			if FindQuestFrameOption("ս", CMP_PART_MATCH) == true then
				QuestFrameOptionClicked("ս", CMP_PART_MATCH)
				System:Sleep(7000)
			else
				state = "ˮ"
			end
		end

		return
	end

	if OnCheck() then
		state = "END"
		return
	end

	-- Ա״̬
	state = GetState()
end

--------------------------------------------------------------
-- ׿
function OnZhuoBuFan()

	MoveTo(125, 35, -1, "", 1)

	for i = 1, 3 do

		local sid, x, y, die = FindCharacterByName("׿")

		if sid ~= INVALID_ID then
			
			if die == 0 then
				DbgPrintf("֡׿")

				-- ɱBOSS
				if OnKill("׿") then
					state = "ƽ"
				end


			else
				state = "ƽ"
			end

			return
		end

		System:Sleep(1000)
	end

	if DataPool:IsTeamLeader() then

		if IsSteady() then
			MoveToNPC(158, 54 , -1, "")
			System:Sleep(1500)

			if FindQuestFrameOption("ս", CMP_PART_MATCH) == true then
				DbgPrintf("ҵѡսƽ˺׿")
				QuestFrameOptionClicked("ս", CMP_PART_MATCH)
				System:Sleep(7000)
			else
				state = "ˮ"
			end
		end

		return
	end

	-- Ա״̬
	state = GetState()
end

--------------------------------------------------------------
-- ƽ
function OnRenShengPing()

	MoveTo(125, 35, -1, "", 1)

	for i = 1, 3 do

		local sid, x, y, die = FindCharacterByName("ƽ")

		if sid ~= INVALID_ID then
			
			if die == 0 then
				DbgPrintf("֡ƽ")

				-- ɱBOSS
				if OnKill("ƽ") then
					state = "ˮ"
				end


			else
				state = "ˮ"
			end

			return
		end

		System:Sleep(1000)
	end

	-- Ա״̬
	state = GetState()
end

--------------------------------------------------------------
-- ˮ
function OnLiQiuShui()

	MoveTo(125, 40, -1, "", 1)

	for i = 1, 3 do

		OnPickup()

		local sid, x, y, die = FindCharacterByName("ˮ")

		if sid ~= INVALID_ID then
			
			if die == 0 then
				DbgPrintf("֡ˮ")

				-- ɱBOSS
				if OnKill("ˮ") then
					state = "END"
				end

			else
				state = "END"
			end

			return
		end

		System:Sleep(1000)
	end

	if DataPool:IsTeamLeader() then

		if IsSteady() then

			MoveToNPC(96, 41 , -1, "ϴ")
			System:Sleep(1500)

			if FindQuestFrameOption("սˮ", CMP_PART_MATCH) == true then
				QuestFrameOptionClicked("սˮ", CMP_PART_MATCH)
				System:Sleep(3000)
			else
				state = "END"	
			end
		end
		return
	end

end

function ExitScene()

	MoveTo(124, 171, -1)
	System:Sleep(1500)
	DoLuaString("MessageBox_Self_Env", "MessageBox_Self_OK_Clicked()")

end

-- ȡ״̬
function GetState()

	----------------------------------------------
	local sid, _, _, die = FindCharacterByName("ɣ")

	if sid ~= INVALID_ID and die == 0 then
		return "ɣ"
	end

	sid, _, _, die = FindCharacterByName("׽ʬ")

	if sid ~= INVALID_ID then
		return "ɣ"
	end

	----------------------------------------------
	sid, _, _, die = FindCharacterByName("ϴ")

	if sid ~= INVALID_ID then
		
		if x == 95 and y == 39 then
			
			return "ƽ"
			
		else
			if die == 0 then
				return "ϴ"
			end	
		end
	end

	----------------------------------------------
	sid, _, _, die = FindCharacterByName("ƽ")

	if sid ~= INVALID_ID and die == 0 then
		return "ƽ"
	end


	----------------------------------------------
	sid, _, _, die = FindCharacterByName("׿")

	if sid ~= INVALID_ID and die == 0 then
		return "׿"
	end


	----------------------------------------------
	sid, _, _, die = FindCharacterByName("ƽ")

	if sid ~= INVALID_ID and die == 0 then
		return "ƽ"
	end


	----------------------------------------------
	local sid, _, _, die = FindCharacterByName("ˮ")

	if sid ~= INVALID_ID and die == 0 then
		return "ˮ"
	end


	local xNow, yNow = Player:GetPos()
	------------------------------------------------------
	if 60 > GetDistSq(xNow, yNow, 40, 106) then
		return "ɣ"
	end

	------------------------------------------------------
	if 40 > GetDistSq(xNow, yNow, 116, 50) then
		return "ϴ"
	end

	------------------------------------------------------
	if 40 > GetDistSq(xNow, yNow, 125, 35) then
		return "ƽ"
	end

	------------------------------------------------------
	return ""
end

function OnFuben()

	state = "BEGIN"

	while GetActiveSceneName() == "翷" do

		DbgPrintf("state = %s", state)
		
		if state == "BEGIN" then
			state = GetState()
		elseif state == "" then
			OnHaDaBa()
		elseif state == "ɣ" then
			OnSangTuGong()
		elseif state == "ϴ" then
			OnWuLaoDa()
		elseif state == "ƽ" then
			OnBuPingDaoRen()
		elseif state == "׿" then
			OnZhuoBuFan()
		elseif state == "ƽ" then
			OnRenShengPing()
		elseif state == "ˮ" then
			OnLiQiuShui()
		elseif state == "END" then
			ExitScene()
		end
		
		System:Sleep(100)
	end

end

function Process()

	local sceneName = GetActiveSceneName()

	if sceneName == "¥" then

		if DataPool:IsTeamLeader() then
			-- 븱
			MoveToNPC(193, 224, 246, "˪")
			QuestFrameOptionClicked("ս", CMP_PART_MATCH)
			System:Sleep(1500)
			return
		end

	elseif sceneName == "翷" then

		-- 
		OnFuben()
		count = count + 1
		return
	end

	MoveToNPC(193, 224, 246, "˪")

end

function _SMain(...)
	
	ShowMessage("Ʈűչز..ճɽ!")
	if true then 
		return
	end


	LogChatMsg(0)

	while count < 2 do

		Process()

	end
		
	LogChatMsg(-1)
end

_SMain()