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

--[[
[author]
descript="闭关测试中..择日出山!"
]]--

script_name="大理杀星"


------------------------------------------------------------------------------------------------------------------
function FindGongSunSheng()

	local idNpc_A = INVALID_ID
	local idNpc_B = 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 classObj == "NPC" then
				if nameObj == "公孙圣" then
					if Character:GetData(idObj, "DIE") == false then

						if idNpc_A ~= INVALID_ID then
							idNpc_B = idObj
							break
						end
						idNpc_A = idObj
					end
				end
			end
		end
	end

	return idNpc_A, idNpc_B
end


local g_boss = {
	[0] = {name="关盛", fun=OnGuanSheng},
	[1] = {name="公孙圣", fun=OnGongSongShen},
	[2] = {name="花容", fun=OnHuaRong},
	[3] = {name="呼延啄", fun=OnOther},
	[4] = {name="李魁", fun=OnLiKui},
	[5] = {name="林充", fun=OnOther},
	[6] = {name="卢君逸", fun=OnLuJunYi},
	[7] = {name="鲁志生", fun=OnLuZhiSheng},
	[8] = {name="秦冥", fun=OnOther},
	[9] = {name="宋姜", fun=OnSongJiang},
	[10] = {name="武颂", fun=OnOther},
	[11] = {name="吴永", fun=OnWuYong}
}


function FindBoss()
	local nFlags = CHA_RELATION_ENEMY|CHA_TYPE_NPC|CHA_OWNER_ANY|CHA_OCCUPANT_ANY|CHA_STATE_ALIVE

	return -1
end

function OnCopyScene()
	AI:SetAI("高级战斗") 
	AI:SetParameter("地图" , 546)
	AI:Start()
	local nMoveTarget = -1

	local bRangeAttack = Player:IsRangeAttack()

	while true do

		System:Sleep(300)
		
		local nBossCount = 0
		local nBadBuffCount = 0 
		local bAttack = 1 
		local bDefend = 0
		local nLockCount = 0 
		local AttackMode = AI:GetAdv("阵型")

		local id = FindObjectByName( "公孙圣" , "怪物")
		if INVALID_ID ~= id then
			local idNpc_A, idNpc_B = FindGongSunSheng()

			DbgPrintf("查找分身 %X, %X", idNpc_A, idNpc_B)

			-- 判断分身是否存在
			if idNpc_A ~= INVALID_ID and idNpc_B ~= INVALID_ID then
		
				local hpNpc_A = Character:GetData(idNpc_A, "HP_PERCENT")
				local hpNpc_B = Character:GetData(idNpc_B, "HP_PERCENT")

				DbgPrintf("比较血量 %d, %d", hpNpc_A, hpNpc_B)
		
				-- 选择血比较多的一个
				if math.abs(hpNpc_A - hpNpc_B) > 10 then
					if hpNpc_A > hpNpc_B then
						idNpc_A = idNpc_A
					else
						idNpc_A = idNpc_B
					end
				end

				if idNpc_A ~= INVALID_ID then 
					nLockCount = nLockCount + 1
					AI:SetAdv("指定击杀" , idNpc_A )
				end
			end
		end	

		local id = FindObjectByName( "宋姜" , "怪物")
		if INVALID_ID ~= id then
			if AttackMode == 1 then
				AI:SetAdv("增加威胁对象" , id , 7 )
			else
				AI:SetAdv("删除威胁对象" , id )
			end
		end

		local id = FindObjectByName( "关盛" , "怪物")
		if INVALID_ID ~= id then
			local iPos,_,_ = CheckBufferByName( id, "天罡护体")
			if INVALID_ID ~= iPos then
				--DbgPrintf("即将释放灭世之阵") 
				nMoveTarget = id 
			else 
				if AI:GetAdv("阵型") == 2 then
					nMoveTarget = id  
				end
			end
		end

		local id = FindObjectByName( "卢君逸" , "怪物")
		if INVALID_ID ~= id then
			local iPos,_,_ = CheckBufferByName( id, "天罡霸气")
			if INVALID_ID ~= iPos then
				AI:SetAdv("增加威胁对象" , id , 3)
				bAttack = 0 
			else 
				AI:SetAdv("删除威胁对象" , id )
			end
		end

		
		--"人亡阵" or name == "天罡阵" or name == "地煞阵" 
		local nSpecialCount = 0
		local idSpecial = FindObjectByName("人亡阵" , "特殊对象")
		if INVALID_ID ~= idSpecial then
			AI:SetAdv("增加威胁对象" , idSpecial , 3 )
			nSpecialCount = nSpecialCount + 1
		end
		
		idSpecial = FindObjectByName("天罡阵" , "特殊对象")
		if INVALID_ID ~= idSpecial then
			AI:SetAdv("增加威胁对象" , idSpecial , 3 )
			nSpecialCount = nSpecialCount + 1
		end

		idSpecial = FindObjectByName("地煞阵" , "特殊对象")
		if INVALID_ID ~= idSpecial then
			AI:SetAdv("增加威胁对象" , idSpecial , 3 )
			nSpecialCount = nSpecialCount + 1
		end

		
		--关盛 灭世刀阵  灭世之阵 九死一生
		while true do 
			local channel, time, name, data = GetChatMsg()

			if channel == INVALID_ID then 
				break 
			end

			local now = System:timeGetTime()

			if channel == 0 and now < time + 2 then

				--DbgPrintf("%s", name)
				--DbgPrintf("%s", data)
				--DbgPrintf("%d %d [%d]", System:timeGetTime(), time, (System:timeGetTime() - time))
				--DbgPrintf(" ")
				-- 宋姜 需要在远距离攻击 你们激怒了我

				if string.find(data, "灭世之阵") then
					DbgPrintf("检测到【灭世之阵】！！！！！")
					--聚拢
					local id,tx,ty = FindObjectByName("关盛" , "怪物")
					if INVALID_ID ~= id then
						nMoveTarget = id
					end
				end


				if string.find(data, "妖星聚义") then
					--分散
					local id = FindObjectByName("宋姜" , "怪物")
					if INVALID_ID ~= id then
						bDefend = 1
						AI:SetAdv("阵型" , "规避" , 30000 ) 
					end
				end

				if string.find(data, "紫气东来") then
					id,_,_,die,_,_,_ = FindObjectByName("紫色妖星" , "怪物")
					if INVALID_ID ~= id and die == 0 then
						nLockCount = nLockCount + 1
						AI:SetAdv("指定击杀" , id)
					end
				end

				if string.find(data, "黄粱美梦") then
					id,_,_,die,_,_,_ = FindObjectByName("黄色妖星" , "怪物")
					if INVALID_ID ~= id and die == 0 then
						nLockCount = nLockCount + 1
						AI:SetAdv("指定击杀" , id)
					end
				end

				if string.find(data, "红妆素裹") then
					id,_,_,die,_,_,_ = FindObjectByName("红色妖星" , "怪物")
					if INVALID_ID ~= id and die == 0 then
						nLockCount = nLockCount + 1
						AI:SetAdv("指定击杀" , id)
					end
				end
			end
		end


		if INVALID_ID ~= nMoveTarget then
			local idMove,moveX,moveY,die,_,_,_ = FindObjectByID( nMoveTarget , "怪物" )
			if INVALID_ID ~= idMove and die == 0 then
				if IsDestination( moveX , moveY , -1 , 3 ) == false then
					AI:SetAdv("阵型" , "移动" , moveX , moveY , 5000 ) 
					continue
				else
					nMoveTarget = -1
				end
			else
				nMoveTarget = -1
			end
		end


		if bAttack == 0 then
			AI:SetAdv("阵型" , "规避" )
			continue 
		end 


		--判断自己宠物是否中瘟疫
		local nPetID = Player:GetFightingPetID()
		if nPetID ~= -1 then
			local iPos,_,_ = CheckBufferByName( nPetID, "瘟疫")
			if INVALID_ID ~= iPos then
				Pet_Relax()
			end
		end


		if bRangeAttack == false then
			local nHP = Player:GetData("HP")
			local nMaxHP = Player:GetData("MAXHP")
			local nHPPercent = (nHP * 100) / nMaxHP
			if nHPPercent < 15 then 
				AI:SetAdv("阵型" , "规避" , 5000 )
			end
		end
		
		--DbgPrintf("设置阵型 - 进攻")
		AI:SetAdv("阵型" , "进攻")

	end
	AI:Stop()
end

--0x222
function _SMain(...)

	ShowMessage("大理杀星：闭关测试中..择日出山!")
	if true then 
		return
	end

	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 )
	AI:SetAdv( "主防御圈" , 30 , 33 , 5 )
	
	while true do

		-- 判断是否接取任务
		local nIndex, idMission = DataPool:GetPlayerMission("杀星")

		if nIndex == -1 then

			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)

		else

			DbgPrintf("已经接取任务")
			local sceneName = GetActiveSceneName()

			if DataPool:IsMissionCanCommit(idMission) then
				DbgPrintf("完成任务")
				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

			else
				if sceneName == "大理" then

					--if DataPool:IsTeamLeader() or g_die == true then
						MoveToNPC(131, 79, 2, "枯荣大师")
						QuestFrameOptionClicked("杀星", CMP_FULL_MATCH)
						QuestFrameOptionClicked("前往生死擂台", CMP_FULL_MATCH)
						System:Sleep(1500)
						local strType,_,_,_,szText = DataPool:GetNPCEventList_Item(0)
						if strType == "text" then
							if string.find(szText, "必须是3人") then
								ShowMessage("队伍少于三人，脚本结束！")
								break
							end
						end
					--end


				elseif sceneName == "生死擂台" then
					DbgPrintf("已经进入副本")
					OnCopyScene()
				else
					MoveToNPC(131, 79, 2, "枯荣大师")
				end
			end
		end

		System:Sleep(1000)
	end


	LogChatMsg(-1)
end

_SMain()
