x020028_g_scriptId=020028

x020028_g_IdPuTong={}
x020028_g_IdPuTong[1]={150001,150001,150001,150001,151001,150001,150001,150001,150001,151001,150001,150001,150001,150001,151001,150001,150001,150001,150001,151001}-- 小怪的编号   总的怪物的自适应等级   修改套数可以直接加或者减
x020028_g_IdPuTong[2]={150002,150002,150002,150002,151002,150002,150002,150002,150002,151002,150002,150002,150002,150002,151002,150002,150002,150002,150002,151002}--  总的怪物的第二套自适应等级
x020028_g_IdPuTong[3]={150003,150003,150003,150003,151003,150003,150003,150003,150003,151003,150003,150003,150003,150003,151003,150003,150003,150003,150003,151003}
x020028_g_IdPuTong[4]={150004,150004,150004,150004,151004,150004,150004,150004,150004,151004,150004,150004,150004,150004,151004,150004,150004,150004,150004,151004}
x020028_g_IdPuTong[5]={150005,150005,150005,150005,151005,150005,150005,150005,150005,151005,150005,150005,150005,150005,151005,150005,150005,150005,150005,151005}
x020028_g_IdPuTong[6]={150006,150006,150006,150006,151006,150006,150006,150006,150006,151006,150006,150006,150006,150006,151006,150006,150006,150006,150006,151006}
x020028_g_IdPuTong[7]={150007,150007,150007,150007,151007,150007,150007,150007,150007,151007,150007,150007,150007,150007,151007,150007,150007,150007,150007,151007}
x020028_g_IdPuTong[8]={150008,150008,150008,150008,151008,150008,150008,150008,150008,151008,150008,150008,150008,150008,151008,150008,150008,150008,150008,151008}
x020028_g_IdPuTong[9]={150009,150009,150009,150009,151009,150009,150009,150009,150009,151009,150009,150009,150009,150009,151009,150009,150009,150009,150009,151009}
x020028_g_IdPuTong[10]={150010,150010,150010,150010,151010,150010,150010,150010,150010,151010,150010,150010,150010,150010,151010,150010,150010,150010,150010,151010}
x020028_g_IdPuTong[11]={150011,150011,150011,150011,151011,150011,150011,150011,150011,151011,150011,150011,150011,150011,151011,150011,150011,150011,150011,151011}
x020028_g_IdPuTong[12]={150012,150012,150012,150012,151012,150012,150012,150012,150012,151012,150012,150012,150012,150012,151012,150012,150012,150012,150012,151012}
x020028_g_IdPuTong[13]={150013,150013,150013,150013,151013,150013,150013,150013,150013,151013,150013,150013,150013,150013,151013,150013,150013,150013,150013,151013}
x020028_g_IdPuTong[14]={150014,150014,150014,150014,151014,150014,150014,150014,150014,151014,150014,150014,150014,150014,151014,150014,150014,150014,150014,151014}
x020028_g_IdPuTong[15]={150015,150015,150015,150015,151015,150015,150015,150015,150015,151015,150015,150015,150015,150015,151015,150015,150015,150015,150015,151015}
x020028_g_IdPuTong[16]={150016,150016,150016,150016,151016,150016,150016,150016,150016,151016,150016,150016,150016,150016,151016,150016,150016,150016,150016,151016}
x020028_g_IdPuTong[17]={150017,150017,150017,150017,151017,150017,150017,150017,150017,151017,150017,150017,150017,150017,151017,150017,150017,150017,150017,151017}
x020028_g_IdPuTong[18]={150018,150018,150018,150018,151018,150018,150018,150018,150018,151018,150018,150018,150018,150018,151018,150018,150018,150018,150018,151018}
x020028_g_IdPuTong[19]={150019,150019,150019,150019,151019,150019,150019,150019,150019,151019,150019,150019,150019,150019,151019,150019,150019,150019,150019,151019}
x020028_g_IdPuTong[20]={150020,150020,150020,150020,151020,150020,150020,150020,150020,151020,150020,150020,150020,150020,151020,150020,150020,150020,150020,151020}



--x020028_g_IdPuTong[1] = {110001}
--x020028_g_IdPuTong[2] = {196}
x020028_g_AdapteLvl = 20		--   自适应等级的数量  十套   修改套数之后记得更改
--x020028_g_IdPuTong[1] = {110001}
x020028_g_IdJingying ={}
x020028_g_IdJingying[1] = {152001,152002,152003,152004,152005,152006,152007,152008,152009,152010,152011,152012,152013,152014,152015,152016,152017,152018,152019,152020}		--  第一个精英怪的自适应等级  修改套可以直接加减数量		
x020028_g_IdJingying[2] = {153001,153002,153003,153004,153005,153006,153007,153008,153009,153010,153011,153012,153013,153014,153015,153016,153017,153018,153019,153020}   --  第二个精英怪的自适应等级  修改套可以直接加减数量

x020028_g_JingyingNum = 2   --   精英怪物的数量

x020028_g_JingyingPosList = {}
x020028_g_JingyingPosList[1] = {{126,176,3.14},{136,180,3.14}}     -- 精英怪坐标
--x020028_g_JingyingPosList[1] = {{104,89,-1.53}, {110, 89, -1.53}}
x020028_g_JingyingSkillId = 300201

x020028_g_IdBoss = {}
x020028_g_IdBoss[1] = {154001,154002,154003,154004,154005,154006,154007,154008,154009,154010,154011,154012,154013,154014,154015,154016,154017,154018,154019,154020} -- Boss的自适应等级  修改套可以直接加减数量		
x020028_g_Iddaneng=700002
x020028_g_BossBingFengBuffId = {}
x020028_g_BossBingFengBuffId[1] = {10019, 10020}
x020028_g_BossPosList={}
x020028_g_BossPosList[1] = {{127,191,0}}                   --BOSS刷新坐标
--x020028_g_BossPosList[1] = {{100,82,0}} 
x020028_g_BossPosList[2] = {{127,110,0}}                         --大能对话后移动的坐标

x020028_g_MonsterPosList={}     		    --   Boss坐标
x020028_g_MonsterPosList[1]={{127,110,0},{114,117,0},{122,115,0},{139,113,0},{140,121,0},{133,122,0},{128,120,0},{119,120,0},{118,127,0},{115,133,0},{121,133,0},{140,132,0},{127,110,0},{114,117,0},{122,115,0},{139,113,0},{140,121,0},{133,122,0},{128,120,0},{119,120,0},{118,127,0},{115,133,0},{121,133,0},{140,132,0}}			-- 	 召唤小怪坐标

x020028_g_ResurrectionPosList = {}
x020028_g_ResurrectionPosList[1]={{100,82,3.14}}                      -- 复活点坐标

x020028_g_NpcPosList={}     
x020028_g_NpcPosList[1]={{106,89,-1.53}}	                       --大能NPC的出生坐标
				
x020028_g_TpTime = 2				-- 传送点特效播中的刷怪时间
x020028_g_CreateMonTime = 5       --  生成怪物间隔时间
x020028_g_PozExcursion = {}       --  传送点周围怪物坐标偏移量
x020028_g_PozExcursion[1] = {{-3, 2}, {-2, 0}, {-1, 1}, {0, -3}}



x020028_g_BossTips = 2432       --  Boss对话	
x020028_g_BossFailure =  2505    --  失败提示

x020028_g_CSParamIdxCurStage=0                     -- 场景切换波数
x020028_g_CSParamIdxCurSubStage=1				   -- 同一场景中的时间点		
x020028_g_CSParamIdxrandtime=2					   -- 是否触发大能对话	
x020028_g_CSParamIdxcurNum = 3					   -- 触发对话后的时间戳	  
x020028_g_CSParamIdBoss = 4                        -- 精英传输能量的总时间
x020028_g_CSParamTpRandId = 5 					   --  随机数
x020028_g_CSParamBuffobjId = 6						-- 大能是否走到目标地点并说话
x020028_g_CSParamIdstage = 7						-- 出现的波数记录


x020028_g_User_ParamIdOfOne = 1                   -- 三环本第一环钥匙
x020028_g_User_ParamIdOfTwo = 2 				  -- 三环本第二换钥匙

x020028_g_User_ParamHp = 50    --  精英怪物中断能量血量百分比

x020028_g_CounterId = 8504
x020028_g_IdCSxiayiBox= 10210			--侠义值宝箱的ID
x020028_g_IdCSItemBox = 10200            --宝箱的ID

x020028_g_BuffId = 200331
x020028_g_BuffTime = 50

x020028_g_Tips={}
x020028_g_Tips[1]="#{50004}*"				-- 打死JY怪物提示

x020028_g_BossNumEnumId = 60
x020028_g_luckyBossNumEnumId = 56

function x020028_OnCopySceneOpen(scene)
	AddCSMission(scene,65001)

	AddCSMission(scene,65002)
	AddCSMission(scene,65002)
	AddCSMission(scene,65002)
	AddCSMission(scene,65002)
	AddCSMission(scene,65002)
	AddCSMission(scene,65002)
	AddCSMission(scene,65002)
	AddCSMission(scene,65002)
	AddCSMission(scene,65002)
	AddCSMission(scene,65002)
	AddCSMission(scene,65002)
	AddCSMission(scene,65002)
	AddCSMission(scene,65002)
	AddCSMission(scene,65002)
	AddCSMission(scene,65002)
	AddCSMission(scene,65002)
	AddCSMission(scene,65002)
	AddCSMission(scene,65002)
	AddCSMission(scene,65002)

	AddCSMission(scene,65003)
	AddCSMission(scene,65003)
	
end

--任务完成回调
function x020028_OnMissionComplete(scene, nObjId, nMissionId)
	local nCurStage = GetCopySceneParam(scene,x020028_g_CSParamIdxCurStage)
	NpcPaoPaoByRBId(scene, 700002, 50000)
	SetCopySceneParam(scene,2,1)	

	OnCSMissionStage(scene, 1)
end

function x020028_OnCopySceneTick(scene,tickCount)					--副本阶段
	local curStage = GetCopySceneParam(scene,x020028_g_CSParamIdxCurStage) +1
	local curSubStage = GetCopySceneParam(scene,x020028_g_CSParamIdxCurSubStage) +1
	local MonStage =  #x020028_g_IdPuTong[1]
	--print(curStage)
	SetCopySceneParam(scene,x020028_g_CSParamIdxCurSubStage,curSubStage)

	local nAdaptedLvl = math.floor(GetAdaptLvl(scene)/5)
	if nAdaptedLvl > x020028_g_AdapteLvl then
		nAdaptedLvl = x020028_g_AdapteLvl
	end
	if(tickCount == 0)  then
		CreateMonster(scene, x020028_g_Iddaneng,x020028_g_NpcPosList[1][1][3],x020028_g_NpcPosList[1][1][1],x020028_g_NpcPosList[1][1][2])
		local nUserCount = GetSceneUserCount(scene)
		for i=0,nUserCount-1 do
			local nUserObjId = GetUserObjIdByIdx(scene,i)
		end
		OnCSMissionStage(scene,0)
	end	

	if MonStage+1 >= curStage  then
		local count = x020028_g_JingyingNum
		for i = 1, count do
			local objid = GetObjIdByRoleBase(scene, x020028_g_IdJingying[i][nAdaptedLvl])
			if CalculatePercenHp(scene, objid) ~= 100 and CalculatePercenHp(scene, objid) ~= -1 and CalculatePercenHp(scene, objid) ~=0 and GetCopySceneParam(scene,x020028_g_CSParamIdstage) == 0 then
					local nUserCount = GetSceneUserCount(scene)
					for i=0,nUserCount-1 do
						local nUserObjId = GetUserObjIdByIdx(scene,i)
					end
					SendAllPlayerServerTips(scene, x020028_g_Tips[1])
					SetCopySceneParam(scene, x020028_g_CSParamIdstage, 1)
			end
			if CalculatePercenHp(scene, objid) > x020028_g_User_ParamHp  then
				local CurTime = GetCopySceneParam(scene, x020028_g_CSParamIdBoss)
				SetCopySceneParam(scene, x020028_g_CSParamIdBoss, CurTime+1)

				local Bossid = GetObjIdByRoleBase(scene, x020028_g_IdBoss[1][nAdaptedLvl])
				
				ProcessSkillRequest(scene, objid,Bossid, x020028_g_JingyingSkillId)
			elseif CalculatePercenHp(scene, objid) < x020028_g_User_ParamHp and CalculatePercenHp(scene, objid) ~= -1 then
				SkillInterrupt(scene, objid)
				DeleteTmpSkill(scene, objid, x020028_g_JingyingSkillId)
			end
		end
	end	
		
	if  1 == curStage and GetCopySceneParam(scene, x020028_g_CSParamIdxrandtime) ~= 0 then
		if GetCopySceneParam(scene, x020028_g_CSParamIdxcurNum) == 0   then
			SetCopySceneParam(scene, x020028_g_CSParamIdxcurNum, curStage)
			SetCopySceneParam(scene,x020028_g_CSParamIdxCurSubStage,0)
		end
		x020028_Stage_1(scene,curStage,curSubStage, nAdaptedLvl, tickCount)
	elseif MonStage >= curStage and  GetCopySceneParam(scene, x020028_g_CSParamIdxrandtime) ~= 0  then
		x020028_Stage_4(scene,curStage,curSubStage, nAdaptedLvl)
	elseif MonStage+1 == curStage and  GetCopySceneParam(scene, x020028_g_CSParamIdxrandtime) ~= 0 then
		x020028_Stage_2(scene,curStage,curSubStage, nAdaptedLvl)
	elseif MonStage+2 == curStage and  GetCopySceneParam(scene, x020028_g_CSParamIdxrandtime) ~= 0 then
		x020028_Stage_3(scene,curStage,curSubStage, nAdaptedLvl)
	elseif MonStage+3 == curStage and  GetCopySceneParam(scene, x020028_g_CSParamIdxrandtime) ~= 0 then
		SetCopySceneSuccess(scene)
	end		
	
end

function x020028_Success(scene)
	AccoutByCopyScene(scene)	
	local nUserCount = GetSceneUserCount(scene)
	for i=0,nUserCount-1 do
		local nUserObjId = GetUserObjIdByIdx(scene,i)
	--	AwardCSItemBox(scene,nUserObjId,x020028_g_IdCSxiayiBox,x020028_g_IdCSItemBox)
	end
end

function  x020028_OnObjDie(scene,objId,rolebaseId,killerId)		
	local MonStage =  #x020028_g_IdPuTong[1]	
	local MonCount = GetHostileMonsterCount(scene)
	local nCurStage = GetCopySceneParam(scene, x020028_g_CSParamIdxCurStage)
	if (MonCount == 0) then
		SetCopySceneParam(scene,x020028_g_CSParamIdxCurStage,nCurStage+1)
		SetCopySceneParam(scene,x020028_g_CSParamIdxCurSubStage,0)
		SetCopySceneParam(scene, x020028_g_CSParamTpRandId, 0)

		if(nCurStage == MonStage+1)    then															-- BOSS奖励次数限制

				x020028_Success(scene)
			
		end
	end
	--print("OnDie MonStage",MonStage)
	--print("OnDie nCurStage",nCurStage)
	if MonStage >= nCurStage+1 and  GetCopySceneParam(scene, x020028_g_CSParamIdxrandtime) ~= 0 then
		local nUserCount = GetSceneUserCount(scene)
		local flag = 0
		for i=0,nUserCount-1 do                                                                                        --  帮助奖励（当所有BOSS奖励触发后开始触发）
			local nUserObjId = GetUserObjIdByIdx(scene,i)
			if(IsAlive(scene, nUserObjId)) then
				flag = 1
			end
		end
		if(flag == 0)  then
			SetCopySceneParam(scene, x020028_g_CSParamIdxrandtime, 0)
			SetCopySceneParam(scene,x020028_g_CSParamIdxCurSubStage,0)
			SetCopySceneParam(scene, x020028_g_CSParamIdxCurStage, 0)
			SetCopySceneParam(scene, x020028_g_CSParamIdxcurNum, 0)
			SetCopySceneParam(scene, x020028_g_CSParamIdBoss, 0)
			SetCopySceneParam(scene, x020028_g_CSParamBuffobjId, 0)
			SetCopySceneParam(scene, x020028_g_CSParamIdstage, 0)
			SetCopySceneParam(scene, x020028_g_CSParamTpRandId, 0)
			DeleHostileMonster(scene)
			for i=0,nUserCount-1 do                                                                                        --  帮助奖励（当所有BOSS奖励触发后开始触发）
				local nUserObjId = GetUserObjIdByIdx(scene,i)
 				AbandonMissionById(scene, nUserObjId, 65002)
			end
		end
	end
end

function x020028_CheckMissionMD(scene ,objId, type)
	local nLastData = GetMissionMD(scene, objId, 3, type)
	local nNewData = CheckRefreshTime(scene, nLastData, objId, 0)       -- 参数为0代表每天更新
	if math.floor(nLastData/100) ~= nNewData then
		SetMissionMD(scene, objId, 3, type, nNewData*100)
	end
end

function x020028_Stage_1(scene ,stage,substage, idx, tick)  
	   
	local nCount = #x020028_g_MonsterPosList[1]/2
	local eqs = 2
	local item = RandomMon(1, nCount)     
	if GetCopySceneParam(scene, x020028_g_CSParamTpRandId) ~= 0 then
		item = GetCopySceneParam(scene, x020028_g_CSParamTpRandId)
	end
	SetCopySceneParam(scene, x020028_g_CSParamTpRandId, item)
	if stage == 1 and GetCopySceneParam(scene, x020028_g_CSParamBuffobjId) == 0 then
		local objId = GetObjIdByRoleBase(scene, x020028_g_Iddaneng)
		local CurPoz = GetObjPosition(scene, objId)
		
		
		if((math.abs(CurPoz[0]-x020028_g_BossPosList[2][1][1]) < eqs) and (math.abs(CurPoz[1] - x020028_g_BossPosList[2][1][2]) < eqs))  then
			SetCopySceneParam(scene, x020028_g_CSParamBuffobjId, tick + 2)
			NpcPaoPaoByRBId(scene, 700002, 50001)
		end
	end
	
	if substage == 1 and GetCopySceneParam(scene, x020028_g_CSParamBuffobjId) == 0 then
		OnCSMissionStage(scene,stage)
		local objId = GetObjIdByRoleBase(scene, x020028_g_Iddaneng)
		CommandMoveTo(scene, objId, x020028_g_BossPosList[2][1][1], x020028_g_BossPosList[2][1][2])
	elseif tick == GetCopySceneParam(scene, x020028_g_CSParamBuffobjId) and GetCopySceneParam(scene, x020028_g_CSParamBuffobjId) ~= 0 then

		local buffObjId = SetSceneBuff(scene,1003,x020028_g_MonsterPosList[1][item][1],x020028_g_MonsterPosList[1][item][2],0)
	elseif tick == GetCopySceneParam(scene, x020028_g_CSParamBuffobjId)+x020028_g_TpTime and GetCopySceneParam(scene, x020028_g_CSParamBuffobjId) ~= 0 then
		local count = #x020028_g_PozExcursion[1]
		for i = 1, count do
			CreateMonster(scene, x020028_g_IdPuTong[idx][stage], 0, x020028_g_MonsterPosList[1][item][1] + x020028_g_PozExcursion[1][i][1], x020028_g_MonsterPosList[1][item][2] + x020028_g_PozExcursion[1][i][2])
		end
	end
end

function x020028_Stage_4(scene ,stage,substage, idx)  
	   
	local nCount = #x020028_g_MonsterPosList[1]/2
	local item = RandomMon(1, nCount)     
	if GetCopySceneParam(scene, x020028_g_CSParamTpRandId) ~= 0 then
		item = GetCopySceneParam(scene, x020028_g_CSParamTpRandId)
	end
	SetCopySceneParam(scene, x020028_g_CSParamTpRandId, item)

	if substage == 2 then
		OnCSMissionStage(scene,stage -1)
		local buffObjId = SetSceneBuff(scene,1003,x020028_g_MonsterPosList[1][item][1],x020028_g_MonsterPosList[1][item][2],0)
	elseif substage ==2+x020028_g_TpTime then
		local count = #x020028_g_PozExcursion[1]
		for i = 1, count do
			CreateMonster(scene, x020028_g_IdPuTong[idx][stage], 0, x020028_g_MonsterPosList[1][item][1] + x020028_g_PozExcursion[1][i][1], x020028_g_MonsterPosList[1][item][2] + x020028_g_PozExcursion[1][i][2])
		end
	end
end

function x020028_Stage_2(scene ,stage,substage, idx)  
	local count = #x020028_g_JingyingPosList[1]
	if substage == 1 then	
		OnCSMissionStage(scene,stage -1)
		local obj = CreateMonster(scene, x020028_g_IdBoss[1][idx], x020028_g_BossPosList[1][1][3], x020028_g_BossPosList[1][1][1], x020028_g_BossPosList[1][1][2])
		SetNpcCampByObjId(scene, obj, 1)
		local objid = CreateMonster(scene, x020028_g_IdJingying[1][idx] ,x020028_g_JingyingPosList[1][1][3],x020028_g_JingyingPosList[1][1][1],x020028_g_JingyingPosList[1][1][2])
		DeleteTmpSkill(scene, objid, x020028_g_JingyingSkillId)
		objid = CreateMonster(scene, x020028_g_IdJingying[2][idx] ,x020028_g_JingyingPosList[1][2][3],x020028_g_JingyingPosList[1][2][1],x020028_g_JingyingPosList[1][2][2])
		DeleteTmpSkill(scene, objid, x020028_g_JingyingSkillId)

	elseif substage == 2 then
		local obj = GetObjIdByRoleBase(scene, x020028_g_IdBoss[1][idx])
		local count = #x020028_g_BossBingFengBuffId[1]
		for i = 1, count do
			SendImpactToUnit(scene, obj, x020028_g_BossBingFengBuffId[1][i])
		end
	elseif substage >= 3 then
		local obj = GetObjIdByRoleBase(scene, x020028_g_IdBoss[1][idx])
		local count = x020028_g_JingyingNum
		for i = 1, count do
			local objid = GetObjIdByRoleBase(scene, x020028_g_IdJingying[i][idx])
			if CalculatePercenHp(scene, objid) >= x020028_g_User_ParamHp then
				ProcessSkillRequest(scene, objid,obj, x020028_g_JingyingSkillId)
			end
		end
	end
end

function x020028_Stage_3(scene ,stage,substage, idx)  
	                  
	if substage == 1 then
		OnCSMissionStage(scene,stage -1)
		local objid = GetObjIdByRoleBase(scene, x020028_g_IdBoss[1][idx])
		local count = #x020028_g_BossBingFengBuffId[1]
		for i = 1, count do
			RemoveImpact(scene, objid, x020028_g_BossBingFengBuffId[1][i])
		end
		SetNpcCampByObjId(scene,objid,3)
		local Num = math.ceil(GetCopySceneParam(scene, x020028_g_CSParamIdBoss)/x020028_g_BuffTime)
		for i = 0, GetCopySceneParam(scene, x020028_g_CSParamIdBoss)/Num do
			SendImpactToUnit(scene, objid, x020028_g_BuffId)
		end

	end

end

function x020028_OnObjresurrectionEnd(scene ,objId)
	SetPos(scene,objId,x020028_g_ResurrectionPosList[1][1],x020028_g_ResurrectionPosList[1][2], 0)
end