ShowMessage("虚空杀星脚本 $Rev: 212 $")

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


script_name="挑战虚空杀星"
local nSetNoFight = 0

local g_MissionMap = 546
local g_XukongMission = 
{
	[0] = {name = "挑战虚空杀星" , vin = "挑战虚空杀星" } ,
	[1] = {name = "挑战虚空四绝庄" , vin = "挑战虚空四绝庄" } ,
	[2] = {name = "挑战虚空缥缈峰" , vin = "挑战虚空缥缈峰" } ,
	[3] = {name = "挑战虚空燕子坞" , vin = "挑战虚空燕子坞" }
}

function GetMissionComplete()
	
	for i = 0 , 3 do
		local nIndex, idMission, idScript = DataPool:GetPlayerMission( g_XukongMission[i].name )

		if nIndex == -1 then
			continue 
		end

		DbgPrintf("%s Index:%d MissionID:%d ScriptID:%d", script_name, nIndex, idMission, idScript)

		local nParam = {}
		-- 取得任务参数
		for i=0,7 do
			nParam[i] = DataPool:GetPlayerMission_Variable( nIndex , i)
			DbgPrintf("[%d]%d", i, nParam[i])
		end
		
		return tonumber(nParam[0]) , idMission, idScript , g_XukongMission[i].name , g_XukongMission[i].vin
	end

	return INVALID_ID , 0, 0 , "" , ""
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 , "宋姜" )
			end
--[[
			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 INVALID_ID ~= g_boss["紫色妖星"] or INVALID_ID ~= g_boss["黄色妖星"] or INVALID_ID ~= g_boss["红色妖星"] then
			bStopSpecialAttack = 1 
		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["瘟疫"] , 5 , "瘟疫")
			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 , idMission, idScript , sMissionName , sIn = GetMissionComplete() 
		if nComplete == INVALID_ID then

			-- 判断队伍人数
			if DataPool:GetTeamMemCount() < 3 then 
				ShowMessage("队伍人数不足 3 人！")
				return
			end

			DbgPrintf("未接取任务")

			MoveToNPC(200, 333, 1, "独孤悔")
			QuestFrameOptionClicked("挑战虚空杀星", CMP_FULL_MATCH)
			QuestFrameAcceptClicked()
			System:Sleep(1000)

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

			if strType == "text" then
				if string.find(szText, "明天再来") then
					ShowMessage("今日已经完成虚空杀星！")
					break	-- 退出循环，结束脚本
				elseif string.find(szText , "空位不足") then
					ShowMessage("背包空格不足,暂停脚本") 
					break
				end
			end

		elseif nComplete == 1 then

			local sceneName = GetActiveSceneName()	
			if sceneName == "生死擂台" then
				MoveToNPC(12, 34, -1, "枯荣大师", MAX_TALK_RANGE, false)
				QuestFrameOptionClicked("确定", CMP_FULL_MATCH)

			else
				MoveToNPC(200, 333, 1, "独孤悔")
				QuestFrameOptionClicked( sMissionName , CMP_FULL_MATCH)
				QuestFrameMissionComplete()	-- 完成

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

				if strType == "text" then
					if string.find(szText, "明天再来") then
						ShowMessage("今日已经完成虚空杀星！")
						break	-- 退出循环，结束脚本
					elseif string.find(szText , "空位不足") then
						ShowMessage("背包空格不足,暂停脚本") 
						break
					end
				end
			end

		elseif nComplete == 0 then

			if sMissionName == script_name then
				local sceneName = GetActiveSceneName()
				if sceneName == "生死擂台" then
					DbgPrintf("已经进入副本")
					OnCopyScene()
				else
					MoveToNPC(131, 79, 2, "枯荣大师")
					QuestFrameOptionClicked( sIn , CMP_FULL_MATCH)
					QuestFrameOptionClicked("前往生死擂台", CMP_FULL_MATCH)
				end
			else
				MoveToNPC(200, 333, 1, "独孤悔")
				System:Sleep(1000)
				local sOpt = GetQuestFrameOption(1)
				if sOpt == script_name then
					--身上有虚空任务,且npc提供需要执行的任务,但是两者不一致时,放弃身上的任务
					QuestFrameMissionAbnegate(idScript)
				elseif sOpt ~= "" then
					ShowMessage("今日虚空任务是 %s , 跳过执行脚本" , sOpt)
					break
				end
					
			end
		else
			QuestFrameMissionAbnegate(idScript)
		end

		System:Sleep(1000)
	end


	LogChatMsg(-1)
end

_SMain()
