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

--[[
[author]
]]--

script_name="杀星"

local g_MissionMap = 546
local g_nParam = {}

local g_nIndex, g_idMission, g_idScript = DataPool:GetPlayerMission(script_name)

function GetMissionComplete()

	g_nIndex, g_idMission, g_idScript = DataPool:GetPlayerMission(script_name)

	if g_nIndex ~= -1 then

		DbgPrintf("%s Index:%d MissionID:%d ScriptID:%d", script_name, g_nIndex, g_idMission, g_idScript)

		-- 取得任务参数
		for i=0,7 do
			g_nParam[i] = DataPool:GetPlayerMission_Variable(g_nIndex, i)
			DbgPrintf("[%d]%d", i, g_nParam[i])
		end
		
		return tonumber(g_nParam[0])
	end

	return -1
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 
}

--[[
	[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}
}


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 

	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 nameObj == "关盛" then
				if classObj == "MON" and Character:GetData(idObj, "DIE") == false then
					g_boss["关盛"] = idObj
				elseif classObj == "NPC" then
					g_boss["待选NPC"] = idObj
				end
			elseif nameObj == "公孙圣" then 
				if classObj == "MON" and Character:GetData(idObj, "DIE") == false then
					if g_boss["公孙圣A"] == INVALID_ID then
						g_boss["公孙圣A"] = idObj
					else
						g_boss["公孙圣B"] = idObj
					end
				elseif classObj == "NPC" then
					g_boss["待选NPC"] = idObj
				end
			elseif nameObj == "花容" then 
				if classObj == "MON" and Character:GetData(idObj, "DIE") == false then
					g_boss["花容"] = idObj
				elseif classObj == "NPC" then
					g_boss["待选NPC"] = idObj
				end
			elseif nameObj == "呼延啄" then
				if classObj == "MON" and Character:GetData(idObj, "DIE") == false then
					g_boss["呼延啄"] = idObj
				elseif classObj == "NPC" then
					g_boss["待选NPC"] = idObj
				end
			elseif nameObj == "李魁" then
				DbgPrintf("检测%s %s %s" , tostring(nameObj) , tostring(classObj) , tostring(Character:GetData(idObj, "DIE")) )
				if classObj == "MON" and Character:GetData(idObj, "DIE") == false then
					g_boss["李魁"] = idObj
				elseif classObj == "NPC" then
					g_boss["待选NPC"] = idObj
				end
			elseif nameObj == "林充" then
				if classObj == "MON" and Character:GetData(idObj, "DIE") == false then
					g_boss["林充"] = idObj
				elseif classObj == "NPC" then
					g_boss["待选NPC"] = idObj
				end
			elseif nameObj == "卢君逸" then
				if classObj == "MON" and Character:GetData(idObj, "DIE") == false then
					g_boss["卢君逸"] = idObj
				elseif classObj == "NPC" then
					g_boss["待选NPC"] = idObj
				end
			elseif nameObj == "鲁志生" then
				if classObj == "MON" and Character:GetData(idObj, "DIE") == false then
					g_boss["鲁志生"] = idObj
				elseif classObj == "NPC" then
					g_boss["待选NPC"] = idObj
				end
			elseif nameObj == "秦冥" then
				if classObj == "MON" and Character:GetData(idObj, "DIE") == false then
					g_boss["秦冥"] = idObj
				elseif classObj == "NPC" then
					g_boss["待选NPC"] = idObj
				end
			elseif nameObj == "宋姜" then
				DbgPrintf("检测%s %s %s" , tostring(nameObj) , tostring(classObj) , tostring(Character:GetData(idObj, "DIE")) )
				if classObj == "MON" and Character:GetData(idObj, "DIE") == false then
					g_boss["宋姜"] = idObj
				elseif classObj == "NPC" then
					g_boss["待选NPC"] = idObj
				end
			elseif nameObj == "武颂" then
				if classObj == "MON" and Character:GetData(idObj, "DIE") == false then
					g_boss["武颂"] = idObj
				elseif classObj == "NPC" then
					g_boss["待选NPC"] = idObj
				end
			elseif nameObj == "吴永" then
				if classObj == "MON" and Character:GetData(idObj, "DIE") == false then
					g_boss["吴永"] = idObj
				elseif classObj == "NPC" then
					g_boss["待选NPC"] = idObj
				end
			elseif nameObj == "紫色妖星" then
				if classObj == "MON" and Character:GetData(idObj, "DIE") == false then
					g_boss["紫色妖星"] = idObj
				end
			elseif nameObj == "黄色妖星" then
				if classObj == "MON" and Character:GetData(idObj, "DIE") == false then
					g_boss["黄色妖星"] = idObj
				end
			elseif nameObj == "红色妖星" then
				if classObj == "MON" and Character:GetData(idObj, "DIE") == false then
					g_boss["红色妖星"] = idObj
				end
			elseif nameObj == "人亡阵" then
				if classObj == "SPECIAL" then
					g_boss["人亡阵"] = idObj
					AI:SetAdv("增加威胁对象" , idObj , 4 , "人亡阵")
				end
			elseif nameObj == "天罡阵" then
				if classObj == "SPECIAL"then
					g_boss["天罡阵"] = idObj
					AI:SetAdv("增加威胁对象" , idObj , 4 , "天罡阵")
				end
			elseif nameObj == "地煞阵" then
				if classObj == "SPECIAL" then
					g_boss["地煞阵"] = idObj
					AI:SetAdv("增加威胁对象" , idObj , 4 , "地煞阵")
				end
			end
		end
	end
end

function OnCopyScene()
	local pos , menpai = GetTeamStandPos()
	AI:SetAdv( "主防御圈" , g_TeamPos[pos].x , g_TeamPos[pos].y , 6 )
	AI:SetAI("高级战斗") 
	AI:SetParameter("地图" , g_MissionMap)
	AI:Start()

	local bRangeAttack = Player:IsRangeAttack()
	local pre_Yaoxin = "非法目标" 
	local pre_FingerAttack = INVALID_ID 
	local pre_GSS = INVALID_ID
	local next_boss_time = os.time()
	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 bFixPlace = 0

		local FingerAttack = -1
		local FingerAttackName = ""
		local FollowTarget = -1
		local mainguard_r = 6
		
		System:Sleep(300)

		UpdateBossInfo()

		local nBoss = 0 
		for i , v in pairs(g_boss) do
			if v ~= INVALID_ID then
				nBoss = nBoss + 1 
			end
		end

		if nBoss > 0 and g_boss["待选NPC"] ~= INVALID_ID then
			nBoss = nBoss - 1
		end

		if nBoss > 0 then
			UpdateMsg("Quest_SX_BossTick")
		end

		local new_pos,_ = GetTeamStandPos()
		if new_pos ~= pos then
			pos = new_pos 
			AI:SetAdv( "主防御圈" , g_TeamPos[pos].x , g_TeamPos[pos].y , 6 )
		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" , 15000) == 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 = "公孙圣"
			bCallPet = 0
			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 , "宋姜" )
			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
			bCallPet = 0 
			bFixPlace = 1
		end

		id = g_boss["卢君逸"]
		if INVALID_ID ~= id then
			local iPos,_,_ = CheckBufferByName( id, "天罡霸气")
			if INVALID_ID ~= iPos then
				bKillMonster = 0
				bCallPet = 0
				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("删除威胁对象" , "人亡阵" )
		else
			bPickUp = 0
			bCallPet = 0 
		end

		if INVALID_ID == g_boss["天罡阵"] then
			AI:SetAdv("删除威胁对象" , "天罡阵" )
		else
			bPickUp = 0 
			bCallPet = 0 
		end

		if INVALID_ID == g_boss["地煞阵"] then
			AI:SetAdv("删除威胁对象" , "地煞阵" )
		else
			bPickUp = 0
			bCallPet = 0
		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		

		--判断自己宠物是否中瘟疫
		local nPetID = Player:GetFightingPetID()
		if nPetID ~= INVALID_ID then
			local iPos,_,_ = CheckBufferByName( nPetID, "瘟疫")
			if INVALID_ID ~= iPos then
				bCallPet = 0
			end
		end
		
		--自己中了瘟疫且被攻击时,停止攻击
		if INVALID_ID ~= Player:GetBuffByName("瘟疫") then
			bCallPet = 0 
			bPickUp = 0 
			if pos > 2 then
				bYinshen = 1
			elseif pos == 0 and menpai == 4 then
				bYinshen = 1
				bKillMonster = 0
			end
		end

		--自己是主加血,且被攻击时,停止攻击
		if pos == 0 and menpai == 4 then
			bYinshen = 1

			if nHPPercent < 60 or nMPPercent < 15 then
				bKillMonster = 0
			end
		end


		if nComplete == 0 and nBoss <= 0 and DataPool:IsTeamLeader() == true then
			local isNext,szMsg = GetDebugMessage()
			while isNext == 1 do
				DbgPrintf("dbg msg %s" , tostring(szMsg))
				if szMsg ~= nil then
					--local _,_,uWaitTime = string.find(szMsg ,"SXGWTZ_150415_07")
					if string.find(szMsg ,"SXGWTZ_150415_07") then
						next_boss_time = os.time() + 60
					end
				end
				isNext,szMsg = GetDebugMessage()
			end
		end

		--and nHPPercent > 70 and nMPPercent > 50
		--and bCallPet == 1
		if nComplete == 0 and nBoss <= 0 and DataPool:IsTeamLeader() == true and bKillMonster == 1  and os.time() < next_boss_time and IsMsgCold("Quest_SX_BossTick" , 10000) then
			MoveTo( 36 , 34 , g_MissionMap )
			System:Sleep(1000)
			UpdateBossInfo()
			if 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)
					next_boss_time = os.time()
					AI:Start()
				end
			end
		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("不允许出防御圈" , bFixPlace )

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

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

		if nComplete == INVALID_ID then

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

			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)

		elseif nComplete == 1 then
			
			local sceneName = GetActiveSceneName()	
			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

		elseif nComplete == 0 then

			local sceneName = GetActiveSceneName()
			if sceneName == "生死擂台" then
				DbgPrintf("已经进入副本")
				OnCopyScene()
			else
				MoveToNPC(131, 79, 2, "枯荣大师")
				QuestFrameOptionClicked("杀星", CMP_FULL_MATCH)
				QuestFrameOptionClicked("前往生死擂台", CMP_FULL_MATCH)
			end
		else
			QuestFrameMissionAbnegate(g_idScript)
		end

		System:Sleep(1000)
	end


	LogChatMsg(-1)
end

_SMain()
