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