ShowMessage("十二杀星-生死擂台脚本 $Rev: 212 $")

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

script_name="杀星"
local nSetNoFight = 0

local nFubenMapId = 546
local l_Pre_GSS = INVALID_ID

function GetMissionComplete()

	local vIndex, vIdMission, vIdScript = DataPool:GetPlayerMission(script_name)
	local vParam = {}
	if vIndex ~= -1 then
		-- 取得任务参数
		for i=0,7 do
			vParam[i] = DataPool:GetPlayerMission_Variable(vIndex, i)
			DbgPrintf("[%d]%d", i, vParam[i])
		end
	
		return tonumber(vParam[0])
	end

	return INVALID_ID
end

function IsFuben()
	if GetActiveDataSceneID() ~= nFubenMapId then
		return false 
	end
	return true 
end

local l_Pre_Yaoxin = "非法目标" 
local l_Pre_BossDie = ""
local l_boss = {
	["关盛"] = 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 ,
	["红色妖星"] = INVALID_ID ,
	["待选NPC"] = INVALID_ID ,
	["瘟疫"] = INVALID_ID 
}

function ResetBoss()
	for i,v in pairs(l_boss) do
		l_boss[i] = INVALID_ID
	end
end

function UpdateData()

	ResetBoss()

	local vPreGssHPPercent = 100

	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
					if l_boss["公孙圣"] == INVALID_ID then
						l_boss["公孙圣"] = idObj
						vPreGssHPPercent = Character:GetData( idObj , "HP_PERCENT" )
					else
						--如果之前选择的是第1个,那么当第2个公孙圣 hp < 第1个 10% 时,选择第2个公孙圣
						--如果之前选择的是第2个,那么当第1个公孙圣 hp < 第2个 10% 时,选择第1个公孙圣
						local v2GssHPPercent = Character:GetData( idObj , "HP_PERCENT" )
						local v1GssHPPercent = Character:GetData( l_boss["公孙圣"] , "HP_PERCENT" )
						if l_Pre_GSS == l_boss["公孙圣"] then
							if v2GssHPPercent > v1GssHPPercent and math.abs( v2GssHPPercent - v1GssHPPercent ) > 10 then
								l_boss["公孙圣"] = idObj
							end
						elseif l_Pre_GSS == idObj then
							if v2GssHPPercent < v1GssHPPercent and math.abs( v1GssHPPercent - v2GssHPPercent ) > 10 then
								--选择第1个
							else
								l_boss["公孙圣"] = idObj
							end
						end
					end
				else
					l_Pre_BossDie = "公孙圣"
				end
			elseif l_boss[nameObj] ~= nil then
				if Character:GetData(idObj, "DIE") == false then
					l_boss[nameObj] = idObj
				else
					l_Pre_BossDie = nameObj
				end
			end
		elseif classObj == "SPECIAL" then
			if nameObj == "人亡阵" then
				l_boss["人亡阵"] = idObj
				AI:SetAdv("增加威胁对象" , idObj , 5 , "人亡阵")
			elseif nameObj == "天罡阵" then
				l_boss["天罡阵"] = idObj
				AI:SetAdv("增加威胁对象" , idObj , 5 , "天罡阵")
			elseif nameObj == "地煞阵" then
				l_boss["地煞阵"] = idObj
				AI:SetAdv("增加威胁对象" , idObj , 5 , "地煞阵")
			end
		elseif classObj == "NPC" then
			if nameObj == "关盛" then
				l_boss["待选NPC"] = idObj
			elseif nameObj == "公孙圣" then 
				l_boss["待选NPC"] = idObj
			elseif nameObj == "花容" then 
				l_boss["待选NPC"] = idObj
			elseif nameObj == "呼延啄" then
				l_boss["待选NPC"] = idObj
			elseif nameObj == "李魁" then
				l_boss["待选NPC"] = idObj
			elseif nameObj == "林充" then
				l_boss["待选NPC"] = idObj
			elseif nameObj == "卢君逸" then
				l_boss["待选NPC"] = idObj
			elseif nameObj == "鲁志生" then
				l_boss["待选NPC"] = idObj
			elseif nameObj == "秦冥" then
				l_boss["待选NPC"] = idObj
			elseif nameObj == "宋姜" then
				l_boss["待选NPC"] = idObj
			elseif nameObj == "武颂" then
				l_boss["待选NPC"] = idObj
			elseif nameObj == "吴永" then
				l_boss["待选NPC"] = idObj
			end
		elseif classObj == "PLAYER" or classObj == "PET" then
			local iPos,_,_ = CheckBufferByName( idObj, "杀气瘟疫")
			if INVALID_ID ~= iPos then
				l_boss["瘟疫"] = idObj
			end
		end
	end

	l_Pre_GSS = l_boss["公孙圣"]

	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
			-- 宋姜 需要在远距离攻击 你们激怒了我
			if string.find(data, "妖星聚义") then
				UpdateMsg("sx_songjiang")
			elseif string.find(data, "天罡霸气") then
				UpdateMsg("sx_lujunyi") 
			elseif string.find(data, "紫气东来") then
				l_Pre_Yaoxin = "紫色妖星" 
				ShowMessage("妖星 是紫色")
			elseif string.find(data, "黄粱美梦") then
				l_Pre_Yaoxin = "黄色妖星"
				ShowMessage("妖星 是黄色")
			elseif string.find(data, "红装素裹") then
				l_Pre_Yaoxin = "红色妖星"
				ShowMessage("妖星 是红色")
			end
		end
	end

	if l_boss["关盛"] ~= INVALID_ID then 
		return process_boss_kill,"关盛"
	elseif l_boss["公孙圣"] ~= INVALID_ID then
		return process_boss_kill,"公孙圣"
	elseif l_boss["花容"] ~= INVALID_ID or l_boss["人亡阵"] ~= INVALID_ID or l_boss["天罡阵"] ~= INVALID_ID or l_boss["地煞阵"] ~= INVALID_ID then
		return process_boss_kill,"花容"
	elseif l_boss["呼延啄"] ~= INVALID_ID then
		return process_boss_kill,"呼延啄"
	elseif l_boss["李魁"] ~= INVALID_ID or l_boss["瘟疫"] ~= INVALID_ID then
		return process_boss_kill,"李魁"
	elseif l_boss["林充"] ~= INVALID_ID then
		return process_boss_kill,"林充"
	elseif l_boss["卢君逸"] ~= INVALID_ID then
		return process_boss_kill,"卢君逸"
	elseif l_boss["鲁志生"] ~= INVALID_ID then
		return process_boss_kill,"鲁志生"
	elseif l_boss["秦冥"] ~= INVALID_ID then
		return process_boss_kill,"秦冥"
	elseif l_boss["宋姜"] ~= INVALID_ID then
		return process_boss_kill,"宋姜"
	elseif l_boss["武颂"] ~= INVALID_ID then
		return process_boss_kill,"武颂"
	elseif l_boss["吴永"] ~= INVALID_ID or l_boss["紫色妖星"]~= INVALID_ID or l_boss["黄色妖星"] ~= INVALID_ID or l_boss["红色妖星"] ~= INVALID_ID then
		return process_boss_kill,"吴永"
	end

	return nil,""	
end

function process_boss_kill( vBoss )

	AI:SetAdv( "主防御圈" , 30 , 33 , 15 )
	AI:SetAI("高级战斗") 
	AI:SetParameter("地图" , nFubenMapId)

	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_teamfollow = false

	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 FollowTarget = -1
		
		System:Sleep(300)

		if IsFuben() == false then
			break
		end

		local vResult , vParam = UpdateData()
		if vResult == process_boss_kill then
			if vParam ~= vBoss then
				break
			end
			UpdateMsg("FB_DLSX_KILLBOSS")
		else
			local nComplete = GetMissionComplete() 
			if nComplete ~= 0 then
				break
			elseif vBoss == "战前准备" then
				--wait
			elseif l_Pre_BossDie == vBoss then
				break
			else
				--丢失目标,等待5秒后退出
				if IsMsgCold("FB_DLSX_KILLBOSS" , 5000) == true then
					break
				end
			end
		end

		-- 判断分身是否存在
		if l_boss["公孙圣"] ~= INVALID_ID then	
			local iPos,_,_ = CheckBufferByName( l_boss["公孙圣"], "天罡护体")
			if iPos ~= INVALID_ID then
				bStopSpecialAttack = 1
			end
			
			AI:SetAdv("指定击杀" , -1 , "" )
			AI:SetAdv("指定击杀" , l_boss["公孙圣"] , "公孙圣" )
		elseif l_boss[l_Pre_Yaoxin] ~= nil and INVALID_ID ~= l_boss[l_Pre_Yaoxin] then
			AI:SetAdv("指定击杀" , -1 , "" )
			AI:SetAdv("指定击杀" , l_boss[l_Pre_Yaoxin] , l_Pre_Yaoxin )
		elseif l_boss["黄色妖星"] ~= INVALID_ID or l_boss["红色妖星"] ~= INVALID_ID or l_boss["紫色妖星"] ~= INVALID_ID then
			if l_boss["吴永"] ~= INVALID_ID then
				AI:SetAdv("指定击杀" , -1 , "" )
				AI:SetAdv("指定击杀" , l_boss["吴永"] , "吴永" )
			end
		else
			AI:SetAdv("指定击杀" , -1 , "" )
		end

		if INVALID_ID ~= l_boss["宋姜"] then
--[[
			if bRangeAttack == true then
				AI:SetAdv("增加威胁对象" , l_boss["宋姜"] , 6 , "宋姜" )
			else
]]--
				if IsMsgCold("sx_songjiang", 5000) == true then
					AI:SetAdv("删除威胁对象" , l_boss["宋姜"] )
				else
					AI:SetAdv("增加威胁对象" , l_boss["宋姜"] , 6 , "宋姜")
					bKillMonster = 0
				end
--[[
			end
]]--
		else
			AI:SetAdv( "删除威胁对象" , "宋姜" )
		end

		if INVALID_ID ~= l_boss["关盛"] then
			AI:SetAdv("跟随对象" , l_boss["关盛"] , 3 )
		else
			AI:SetAdv("跟随对象" , -1 , 3 )
		end

		if INVALID_ID ~= l_boss["鲁志生"] then
			local iPos,_,_ = CheckBufferByName( l_boss["鲁志生"] , "孤芳自赏")

			if INVALID_ID ~= iPos then
				bStopSpecialAttack = 1
			end

			--孤芳自赏,停止特殊技能攻击
			--收宝宝,防止boss回血
			bCallPet = 0
		end

		local teamfollow = false

		if INVALID_ID ~= l_boss["卢君逸"] then
			local iPos,_,_ = CheckBufferByName( l_boss["卢君逸"], "天罡霸气")
			if INVALID_ID ~= iPos then
				bKillMonster = 0
				teamfollow = true
				bCallPet = 0 
				UpdateMsg("sx_lujunyi")
			else 
				if IsMsgCold("sx_lujunyi" , 5000) then
					bKillMonster = 1
				else
					bKillMonster = 0 
					teamfollow = true
				end					
			end

			local iPos,_,_ = CheckBufferByName( l_boss["卢君逸"], "天罡皇气")
			if INVALID_ID ~= iPos then
				bStopSpecialAttack = 1
			end
		end

		--组队跟随超过3秒,自动退出组队跟随
		if teamfollow ~= pre_teamfollow then
			pre_teamfollow = teamfollow
			if teamfollow == true then
				Player:EnableTeamFollow(true)
				UpdateMsg("SX_TEAMFOLLOW")
			else
				Player:EnableTeamFollow(false)
				DelMsg("SX_TEAMFOLLOW")
			end
		else
			if IsMsgCold("SX_TEAMFOLLOW" , 3000) == true then
				Player:EnableTeamFollow(false)
			end
		end
		
		if INVALID_ID == l_boss["人亡阵"] then
			AI:SetAdv("删除威胁对象" , "人亡阵" )
		end

		if INVALID_ID == l_boss["天罡阵"] then
			AI:SetAdv("删除威胁对象" , "天罡阵" )
		end

		if INVALID_ID == l_boss["地煞阵"] then
			AI:SetAdv("删除威胁对象" , "地煞阵" )
		end

		--判断自己宠物是否中毒
		if l_boss["瘟疫"] ~= INVALID_ID then
			local nPetID = Player:GetFightingPetID()
			if nPetID == l_boss["瘟疫"] then
				bCallPet = 0
			elseif l_boss["瘟疫"] == Player:GetData("ID") then
				bCallPet = 0
			else
				--设置危险对象
				AI:SetAdv("增加威胁对象" , l_boss["瘟疫"] , 6 , "瘟疫")
			end
		else
			AI:SetAdv("删除威胁对象" , "瘟疫" )
		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("允许隐身" , bYinshen )
		AI:SetAdv("不允许出防御圈" , 0 )
	end
	AI:Stop()

	OnSweepBattleField()
end

function _breakmove( fX , fY , nScene )
	if IsFuben() == true then
		return false 
	end
	return true
end

function _breakmove_infuben()
	if IsFuben() == false then
		return false 
	end

	local vResult , vParam = UpdateData()
	if vResult ~= nil then
		return false
	end

	return true
end 

function process_npc_t()

	local vResult , vParam = UpdateData()
	if vResult ~= nil then
		return 
	end

	if INVALID_ID == l_boss["待选NPC"] then
		MoveInMap( 36 , 34 , nFubenMapId , 2 , _breakmove_infuben )
		if UpdateData() ~= nil then
			return
		end
	end

	if INVALID_ID ~= l_boss["待选NPC"] then
		local npc_id , npc_x , npc_y , _ , _ , _ , _ , npc_name = FindObjectByID( l_boss["待选NPC"] , "NPC" )
		if INVLIAD_ID ~= npc_id then
			MoveToNPC( npc_x , npc_y , nFubenMapId , npc_name , false , false , _breakmove_infuben )
			if UpdateData() == nil then
				QuestFrameOptionClicked("开启挑战", CMP_PART_MATCH)
				System:Sleep(2000)
			end
		end
	end
end

local function OnCommonMission()

	while true do

		System:Sleep(500)
		
		local nComplete = GetMissionComplete() 

		if IsFuben() == true then
			local vResult , vParam = UpdateData()
			if vResult == process_boss_kill then
				process_boss_kill( vParam )
			elseif nComplete ~= 0 then
				--退出副本
				MoveToNPC(12, 34, nFubenMapId , "枯荣大师" , false )
				QuestFrameOptionClicked("确定", CMP_FULL_MATCH)
			else
				--开怪
				if DataPool:GetTeamMemCount() == 0 or DataPool:IsTeamLeader() == true then
					process_npc_t()
				else
					process_boss_kill( "战前准备" )
				end
			end
		else
			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
				MoveToNPC(131, 79, 2, "枯荣大师")
				QuestFrameOptionClicked("杀星", CMP_FULL_MATCH)
				QuestFrameMissionComplete()	-- 完成
			elseif nComplete == 0 then
				MoveToNPC(131, 79, 2, "枯荣大师" , true , true , _breakmove )
				QuestFrameOptionClicked("杀星", CMP_FULL_MATCH)
				QuestFrameOptionClicked("前往生死擂台", CMP_FULL_MATCH)
			else
				QuestFrameMissionAbnegate(g_idScript)
			end
		end		
	end
end

local function OnXuKongMission()

	script_name = "挑战虚空杀星"

	while true do

		System:Sleep(500)

		-- 判断是否接取任务
		if IsFuben() == true then
			local nComplete = GetMissionComplete() 

			local vResult , vParam = UpdateData()
			if vResult == process_boss_kill then
				process_boss_kill( vParam )
			elseif nComplete ~= 0 then
				--退出副本
				MoveToNPC(12, 34, nFubenMapId , "枯荣大师" , false )
				QuestFrameOptionClicked("确定", CMP_FULL_MATCH)
			else
				--开怪
				if DataPool:GetTeamMemCount() == 0 or DataPool:IsTeamLeader() == true then
					process_npc_t()
				else
					process_boss_kill( "战前准备" )
				end
			end
		else
			local nComplete = GetMissionComplete() 
			if nComplete == 0 then
				MoveToNPC(131, 79, 2, "枯荣大师" , true , true , _breakmove )
				if DataPool:GetTeamMemCount() < 3 and IsFuben() == false then 
					g_XukongRet = 1
					ShowMessage("队伍人数不足 3 人！")
					return
				end

				QuestFrameOptionClicked("挑战虚空杀星", CMP_FULL_MATCH)
				QuestFrameOptionClicked("前往生死擂台", CMP_FULL_MATCH)
			else
				break
			end
		end
	end
end

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 )

	local isXK = false
	if _CommandLoad ~= nil then
		local vCmd = tostring(_CommandLoad)
		if vCmd == "虚空任务" then
			isXK = true
		end
	end

	if isXK == true then
		OnXuKongMission()
	else
		OnCommonMission()
	end

	LogChatMsg(-1)
end

AI:SetMode("暂停复活地图" , nFubenMapId)
_SMain()
AI:SetMode("暂停复活地图" , -1)


--[[
"吴永" 几个妖星出现时,能正确喊出对象的颜色,但自动攻击两下后就停止了攻击,有时候直接不攻击
公孙圣 不会根据血量切换目标
]]--