x020018_g_scriptId=020018

x020018_g_IdPuTong={}
x020018_g_IdPuTong[1]={120001,120001,120001,120001,121001,120001,120001,120001,120001,121001,120001,120001,120001,120001,121001,120001,120001,120001,120001,121001}-- 小怪的编号   总的怪物的自适应等级   修改套数可以直接加或者减
x020018_g_IdPuTong[2]={120002,120002,120002,120002,121002,120002,120002,120002,120002,121002,120002,120002,120002,120002,121002,120002,120002,120002,120002,121002}--  总的怪物的第二套自适应等级
x020018_g_IdPuTong[3]={120003,120003,120003,120003,121003,120003,120003,120003,120003,121003,120003,120003,120003,120003,121003,120003,120003,120003,120003,121003}
x020018_g_IdPuTong[4]={120004,120004,120004,120004,121004,120004,120004,120004,120004,121004,120004,120004,120004,120004,121004,120004,120004,120004,120004,121004}
x020018_g_IdPuTong[5]={120005,120005,120005,120005,121005,120005,120005,120005,120005,121005,120005,120005,120005,120005,121005,120005,120005,120005,120005,121005}
x020018_g_IdPuTong[6]={120006,120006,120006,120006,121006,120006,120006,120006,120006,121006,120006,120006,120006,120006,121006,120006,120006,120006,120006,121006}
x020018_g_IdPuTong[7]={120007,120007,120007,120007,121007,120007,120007,120007,120007,121007,120007,120007,120007,120007,121007,120007,120007,120007,120007,121007}
x020018_g_IdPuTong[8]={120008,120008,120008,120008,121008,120008,120008,120008,120008,121008,120008,120008,120008,120008,121008,120008,120008,120008,120008,121008}
x020018_g_IdPuTong[9]={120009,120009,120009,120009,121009,120009,120009,120009,120009,121009,120009,120009,120009,120009,121009,120009,120009,120009,120009,121009}
x020018_g_IdPuTong[10]={120010,120010,120010,120010,121010,120010,120010,120010,120010,121010,120010,120010,120010,120010,121010,120010,120010,120010,120010,121010}
x020018_g_IdPuTong[11]={120011,120011,120011,120011,121011,120011,120011,120011,120011,121011,120011,120011,120011,120011,121011,120011,120011,120011,120011,121011}
x020018_g_IdPuTong[12]={120012,120012,120012,120012,121012,120012,120012,120012,120012,121012,120012,120012,120012,120012,121012,120012,120012,120012,120012,121012}
x020018_g_IdPuTong[13]={120013,120013,120013,120013,121013,120013,120013,120013,120013,121013,120013,120013,120013,120013,121013,120013,120013,120013,120013,121013}
x020018_g_IdPuTong[14]={120014,120014,120014,120014,121014,120014,120014,120014,120014,121014,120014,120014,120014,120014,121014,120014,120014,120014,120014,121014}
x020018_g_IdPuTong[15]={120015,120015,120015,120015,121015,120015,120015,120015,120015,121015,120015,120015,120015,120015,121015,120015,120015,120015,120015,121015}
x020018_g_IdPuTong[16]={120016,120016,120016,120016,121016,120016,120016,120016,120016,121016,120016,120016,120016,120016,121016,120016,120016,120016,120016,121016}
x020018_g_IdPuTong[17]={120017,120017,120017,120017,121017,120017,120017,120017,120017,121017,120017,120017,120017,120017,121017,120017,120017,120017,120017,121017}
x020018_g_IdPuTong[18]={120018,120018,120018,120018,121018,120018,120018,120018,120018,121018,120018,120018,120018,120018,121018,120018,120018,120018,120018,121018}
x020018_g_IdPuTong[19]={120019,120019,120019,120019,121019,120019,120019,120019,120019,121019,120019,120019,120019,120019,121019,120019,120019,120019,120019,121019}
x020018_g_IdPuTong[20]={120020,120020,120020,120020,121020,120020,120020,120020,120020,121020,120020,120020,120020,120020,121020,120020,120020,120020,120020,121020}



--x020018_g_IdPuTong[1] = {110001}
--x020018_g_IdPuTong[2] = {196}
x020018_g_AdapteLvl = 20		--   自适应等级的数量  十套   修改套数之后记得更改
--x020018_g_IdPuTong[1] = {110001}
x020018_g_IdJingying ={}
x020018_g_IdJingying[1] = {122001,122002,122003,122004,122005,122006,122007,122008,122009,122010,122011,122012,122013,122014,122015,122016,122017,122018,122019,122020}		--  第一个精英怪的自适应等级  修改套可以直接加减数量		
x020018_g_IdJingying[2] = {123001,123002,123003,123004,123005,123006,123007,123008,123009,123010,123011,123012,123013,123014,123015,123016,123017,123018,123019,123020}   --  第二个精英怪的自适应等级  修改套可以直接加减数量

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

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

x020018_g_IdBoss = {}
x020018_g_IdBoss[1] = {124001,124002,124003,124004,124005,124006,124007,124008,124009,124010,124011,124012,124013,124014,124015,124016,124017,124018,124019,124020} -- Boss的自适应等级  修改套可以直接加减数量		
x020018_g_Iddaneng=700001
x020018_g_BossBingFengBuffId = {}
x020018_g_BossBingFengBuffId[1] = {10019, 10020}
x020018_g_BossPosList={}
x020018_g_BossPosList[1] = {{127,191,0}}                   --BOSS刷新坐标
--x020018_g_BossPosList[1] = {{100,82,0}} 
x020018_g_BossPosList[2] = {{127,110,0}}                         --大能对话后移动的坐标

x020018_g_MonsterPosList={}     		    --   Boss坐标
x020018_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}}			-- 	 召唤小怪坐标

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

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



x020018_g_BossTips = 2432       --  Boss对话	
x020018_g_BossFailure =  2505    --  失败提示

x020018_g_CSParamIdxCurStage=0                     -- 场景切换波数
x020018_g_CSParamIdxCurSubStage=1				   -- 同一场景中的时间点		
x020018_g_CSParamIdxrandtime=2					   -- 是否触发大能对话	
x020018_g_CSParamIdxcurNum = 3					   -- 触发对话后的时间戳	  
x020018_g_CSParamIdBoss = 4                        -- 精英传输能量的总时间
x020018_g_CSParamTpRandId = 5 					   --  随机数
x020018_g_CSParamBuffobjId = 6						-- 大能是否走到目标地点并说话
x020018_g_CSParamIdstage = 7						-- 出现的波数记录


x020018_g_User_ParamIdOfOne = 1                   -- 三环本第一环钥匙
x020018_g_User_ParamIdOfTwo = 2 				  -- 三环本第二换钥匙

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

x020018_g_CounterId = 8504
x020018_g_IdCSxiayiBox= 10110			--侠义值宝箱的ID
x020018_g_IdCSItemBox = 10100             --宝箱的ID

x020018_g_BuffId = 10021
x020018_g_BuffTime = 50

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

x020018_g_BossNumEnumId = 60
x020018_g_luckyBossNumEnumId = 56


function x020018_OnCopySceneOpen(scene)
	AddCSMission(scene,63001)

	AddCSMission(scene,63002)
	AddCSMission(scene,63002)
	AddCSMission(scene,63002)
	AddCSMission(scene,63002)
	AddCSMission(scene,63002)
	AddCSMission(scene,63002)
	AddCSMission(scene,63002)
	AddCSMission(scene,63002)
	AddCSMission(scene,63002)
	AddCSMission(scene,63002)
	AddCSMission(scene,63002)
	AddCSMission(scene,63002)
	AddCSMission(scene,63002)
	AddCSMission(scene,63002)
	AddCSMission(scene,63002)
	AddCSMission(scene,63002)
	AddCSMission(scene,63002)
	AddCSMission(scene,63002)
	AddCSMission(scene,63002)

	AddCSMission(scene,63003)
	AddCSMission(scene,63003)
	
end

--任务完成回调
function x020018_OnMissionComplete(scene, nObjId, nMissionId)
	local nCurStage = GetCopySceneParam(scene,x020018_g_CSParamIdxCurStage)
	NpcPaoPaoByRBId(scene, 700001, 50000)
	SetCopySceneParam(scene,2,1)	
	
	OnCSMissionStage(scene, 1)
end

function x020018_OnCopySceneTick(scene,tickCount)					--副本阶段
	local curStage = GetCopySceneParam(scene,x020018_g_CSParamIdxCurStage) +1
	local curSubStage = GetCopySceneParam(scene,x020018_g_CSParamIdxCurSubStage) +1
	local MonStage =  #x020018_g_IdPuTong[1]
	--print(curStage)
	SetCopySceneParam(scene,x020018_g_CSParamIdxCurSubStage,curSubStage)

	local nAdaptedLvl = math.floor(GetAdaptLvl(scene)/5)
	if nAdaptedLvl > x020018_g_AdapteLvl then
		nAdaptedLvl = x020018_g_AdapteLvl
	end
	if(tickCount == 0)  then
		CreateMonster(scene, x020018_g_Iddaneng,x020018_g_NpcPosList[1][1][3],x020018_g_NpcPosList[1][1][1],x020018_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 = x020018_g_JingyingNum
		for i = 1, count do
			local objid = GetObjIdByRoleBase(scene, x020018_g_IdJingying[i][nAdaptedLvl])
			if CalculatePercenHp(scene, objid) ~= 100 and CalculatePercenHp(scene, objid) ~= -1 and CalculatePercenHp(scene, objid) ~=0 and GetCopySceneParam(scene,x020018_g_CSParamIdstage) == 0 then
					local nUserCount = GetSceneUserCount(scene)
					for i=0,nUserCount-1 do
						local nUserObjId = GetUserObjIdByIdx(scene,i)
					end
					SendAllPlayerServerTips(scene, x020018_g_Tips[1])
					SetCopySceneParam(scene, x020018_g_CSParamIdstage, 1)
			end
			if CalculatePercenHp(scene, objid) > x020018_g_User_ParamHp  then
				local CurTime = GetCopySceneParam(scene, x020018_g_CSParamIdBoss)
				SetCopySceneParam(scene, x020018_g_CSParamIdBoss, CurTime+1)

				local Bossid = GetObjIdByRoleBase(scene, x020018_g_IdBoss[1][nAdaptedLvl])
				
				ProcessSkillRequest(scene, objid,Bossid, x020018_g_JingyingSkillId)
			elseif CalculatePercenHp(scene, objid) < x020018_g_User_ParamHp and CalculatePercenHp(scene, objid) ~= -1 then
				SkillInterrupt(scene, objid)
				DeleteTmpSkill(scene, objid, x020018_g_JingyingSkillId)
			end
		end
	end	
		
	if  1 == curStage and GetCopySceneParam(scene, x020018_g_CSParamIdxrandtime) ~= 0 then
		if GetCopySceneParam(scene, x020018_g_CSParamIdxcurNum) == 0   then
			SetCopySceneParam(scene, x020018_g_CSParamIdxcurNum, curStage)
			SetCopySceneParam(scene,x020018_g_CSParamIdxCurSubStage,0)
		end
		x020018_Stage_1(scene,curStage,curSubStage, nAdaptedLvl, tickCount)
	elseif MonStage >= curStage and  GetCopySceneParam(scene, x020018_g_CSParamIdxrandtime) ~= 0  then
		x020018_Stage_4(scene,curStage,curSubStage, nAdaptedLvl)
	elseif MonStage+1 == curStage and  GetCopySceneParam(scene, x020018_g_CSParamIdxrandtime) ~= 0 then
		x020018_Stage_2(scene,curStage,curSubStage, nAdaptedLvl)
	elseif MonStage+2 == curStage and  GetCopySceneParam(scene, x020018_g_CSParamIdxrandtime) ~= 0 then
		x020018_Stage_3(scene,curStage,curSubStage, nAdaptedLvl)
	elseif MonStage+3 == curStage and  GetCopySceneParam(scene, x020018_g_CSParamIdxrandtime) ~= 0 then
		SetCopySceneSuccess(scene)
	end		
	
end

function x020018_Success(scene)
	AccoutByCopyScene(scene)	
	local nUserCount = GetSceneUserCount(scene)
	for i=0,nUserCount-1 do
		local nUserObjId = GetUserObjIdByIdx(scene,i)
	--	AwardCSItemBox(scene,nUserObjId,x020018_g_IdCSxiayiBox,x020018_g_IdCSItemBox)
	end
end

function  x020018_OnObjDie(scene,objId,rolebaseId,killerId)		
	local MonStage =  #x020018_g_IdPuTong[1]	
	local MonCount = GetHostileMonsterCount(scene)
	local nCurStage = GetCopySceneParam(scene, x020018_g_CSParamIdxCurStage)
	if (MonCount == 0) then
		SetCopySceneParam(scene,x020018_g_CSParamIdxCurStage,nCurStage+1)
		SetCopySceneParam(scene,x020018_g_CSParamIdxCurSubStage,0)
		SetCopySceneParam(scene, x020018_g_CSParamTpRandId, 0)

		if(nCurStage == MonStage+1)    then															-- BOSS奖励次数限制
			x020018_Success(scene)
		end
	end
	--print("OnDie MonStage",MonStage)
	--print("OnDie nCurStage",nCurStage)
	if MonStage >= nCurStage+1 and  GetCopySceneParam(scene, x020018_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, x020018_g_CSParamIdxrandtime, 0)
			SetCopySceneParam(scene,x020018_g_CSParamIdxCurSubStage,0)
			SetCopySceneParam(scene, x020018_g_CSParamIdxCurStage, 0)
			SetCopySceneParam(scene, x020018_g_CSParamIdxcurNum, 0)
			SetCopySceneParam(scene, x020018_g_CSParamIdBoss, 0)
			SetCopySceneParam(scene, x020018_g_CSParamBuffobjId, 0)
			SetCopySceneParam(scene, x020018_g_CSParamIdstage, 0)
			SetCopySceneParam(scene, x020018_g_CSParamTpRandId, 0)
			DeleHostileMonster(scene)
			for i=0,nUserCount-1 do                                                                                        --  帮助奖励（当所有BOSS奖励触发后开始触发）
				local nUserObjId = GetUserObjIdByIdx(scene,i)
 				AbandonMissionById(scene, nUserObjId, 63002)
			end
		end
	end
end

function x020018_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 x020018_Stage_1(scene ,stage,substage, idx, tick)  
	   
	local nCount = #x020018_g_MonsterPosList[1]/2
	local eqs = 2
	local item = RandomMon(1, nCount)     
	if GetCopySceneParam(scene, x020018_g_CSParamTpRandId) ~= 0 then
		item = GetCopySceneParam(scene, x020018_g_CSParamTpRandId)
	end
	SetCopySceneParam(scene, x020018_g_CSParamTpRandId, item)
	if stage == 1 and GetCopySceneParam(scene, x020018_g_CSParamBuffobjId) == 0 then
		local objId = GetObjIdByRoleBase(scene, x020018_g_Iddaneng)
		local CurPoz = GetObjPosition(scene, objId)
		if((math.abs(CurPoz[0]-x020018_g_BossPosList[2][1][1]) < eqs) and (math.abs(CurPoz[1] - x020018_g_BossPosList[2][1][2]) < eqs))  then
			SetCopySceneParam(scene, x020018_g_CSParamBuffobjId, tick + 2)
			NpcPaoPaoByRBId(scene, 700001, 50001)
		end
	end
	
	if substage == 1 and GetCopySceneParam(scene, x020018_g_CSParamBuffobjId) == 0 then
		OnCSMissionStage(scene,stage)
		local objId = GetObjIdByRoleBase(scene, x020018_g_Iddaneng)
		CommandMoveTo(scene, objId, x020018_g_BossPosList[2][1][1], x020018_g_BossPosList[2][1][2])
	elseif tick == GetCopySceneParam(scene, x020018_g_CSParamBuffobjId) and GetCopySceneParam(scene, x020018_g_CSParamBuffobjId) ~= 0 then

		local buffObjId = SetSceneBuff(scene,1003,x020018_g_MonsterPosList[1][item][1],x020018_g_MonsterPosList[1][item][2],0)
	elseif tick == GetCopySceneParam(scene, x020018_g_CSParamBuffobjId)+x020018_g_TpTime and GetCopySceneParam(scene, x020018_g_CSParamBuffobjId) ~= 0 then
		local count = #x020018_g_PozExcursion[1]
		for i = 1, count do
			CreateMonster(scene, x020018_g_IdPuTong[idx][stage], 0, x020018_g_MonsterPosList[1][item][1] + x020018_g_PozExcursion[1][i][1], x020018_g_MonsterPosList[1][item][2] + x020018_g_PozExcursion[1][i][2])
		end
	end
end

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

	if substage == 2 then
		OnCSMissionStage(scene,stage -1)
		local buffObjId = SetSceneBuff(scene,1003,x020018_g_MonsterPosList[1][item][1],x020018_g_MonsterPosList[1][item][2],0)
	elseif substage ==2+x020018_g_TpTime then
		local count = #x020018_g_PozExcursion[1]
		for i = 1, count do
			CreateMonster(scene, x020018_g_IdPuTong[idx][stage], 0, x020018_g_MonsterPosList[1][item][1] + x020018_g_PozExcursion[1][i][1], x020018_g_MonsterPosList[1][item][2] + x020018_g_PozExcursion[1][i][2])
		end
	end
end

function x020018_Stage_2(scene ,stage,substage, idx)  
	local count = #x020018_g_JingyingPosList[1]
	if substage == 1 then	
		OnCSMissionStage(scene,stage -1)
		local obj = CreateMonster(scene, x020018_g_IdBoss[1][idx], x020018_g_BossPosList[1][1][3], x020018_g_BossPosList[1][1][1], x020018_g_BossPosList[1][1][2])
		SetNpcCampByObjId(scene, obj, 1)
		local objid = CreateMonster(scene, x020018_g_IdJingying[1][idx] ,x020018_g_JingyingPosList[1][1][3],x020018_g_JingyingPosList[1][1][1],x020018_g_JingyingPosList[1][1][2])
		DeleteTmpSkill(scene, objid, x020018_g_JingyingSkillId)
		objid = CreateMonster(scene, x020018_g_IdJingying[2][idx] ,x020018_g_JingyingPosList[1][2][3],x020018_g_JingyingPosList[1][2][1],x020018_g_JingyingPosList[1][2][2])
		DeleteTmpSkill(scene, objid, x020018_g_JingyingSkillId)

	elseif substage == 2 then
		local obj = GetObjIdByRoleBase(scene, x020018_g_IdBoss[1][idx])
		local count = #x020018_g_BossBingFengBuffId[1]
		for i = 1, count do
			SendImpactToUnit(scene, obj, x020018_g_BossBingFengBuffId[1][i])
		end
	elseif substage >= 3 then
		local obj = GetObjIdByRoleBase(scene, x020018_g_IdBoss[1][idx])
		local count = x020018_g_JingyingNum
		for i = 1, count do 
			local objid = GetObjIdByRoleBase(scene, x020018_g_IdJingying[i][idx])
			if CalculatePercenHp(scene, objid) >= x020018_g_User_ParamHp then
				ProcessSkillRequest(scene, objid,obj, x020018_g_JingyingSkillId)
			end
		end
	end
end

function x020018_Stage_3(scene ,stage,substage, idx)  
	                  
	if substage == 1 then
		OnCSMissionStage(scene,stage -1)
		local objid = GetObjIdByRoleBase(scene, x020018_g_IdBoss[1][idx])
		local count = #x020018_g_BossBingFengBuffId[1]
		for i = 1, count do
			RemoveImpact(scene, objid, x020018_g_BossBingFengBuffId[1][i])
		end
		SetNpcCampByObjId(scene,objid,3)
		local Num = math.ceil(GetCopySceneParam(scene, x020018_g_CSParamIdBoss)/x020018_g_BuffTime)
		for i = 0, GetCopySceneParam(scene, x020018_g_CSParamIdBoss)/Num do
			SendImpactToUnit(scene, objid, x020018_g_BuffId)
		end

	end

end

function x020018_OnObjresurrectionEnd(scene ,objId)
	SetPos(scene,objId,x020018_g_ResurrectionPosList[1][1],x020018_g_ResurrectionPosList[1][2], 0)
end