wx020039_g_scriptId=020039																																																										
																																																										
x020039_g_IdPuTong={}																																																										
--  x020039_g_IdPuTong [1] = {{怪物1的自适应ID}， ｛怪物2的自适应ID｝ ， ｛怪物3的自适应ID｝}    有几个怪物加几组   后面对应																																																										
x020039_g_IdPuTong[1] ={{370001,	370002,	370003,	370004,	370005,	370006,	370007,	370008,	370009,	370010,	370011,	370012,	370013,	370014,	370015,	370016,	370017,	370018,	370019,	370020},{371001,	371002,	371003,	371004,	371005,	371006,	371007,	371008,	371009,	371010,	371011,	371012,	371013,	371014,	371015,	371016,	371017,	371018,	371019,	371020},{370001,	370002,	370003,	370004,	370005,	370006,	370007,	370008,	370009,	370010,	370011,	370012,	370013,	370014,	370015,	370016,	370017,	370018,	370019,	370020}}    --  位置2	第一波一个精英 2个小怪   小怪的Debuff AI配置在怪物身上
x020039_g_IdPuTong[2] ={{372001,	372002,	372003,	372004,	372005,	372006,	372007,	372008,	372009,	372010,	372011,	372012,	372013,	372014,	372015,	372016,	372017,	372018,	372019,	372020},{372001,	372002,	372003,	372004,	372005,	372006,	372007,	372008,	372009,	372010,	372011,	372012,	372013,	372014,	372015,	372016,	372017,	372018,	372019,	372020}}			 --  位置3	第二波五个小怪																
x020039_g_IdPuTong[3] ={{372001,	372002,	372003,	372004,	372005,	372006,	372007,	372008,	372009,	372010,	372011,	372012,	372013,	372014,	372015,	372016,	372017,	372018,	372019,	372020},{372001,	372002,	372003,	372004,	372005,	372006,	372007,	372008,	372009,	372010,	372011,	372012,	372013,	372014,	372015,	372016,	372017,	372018,	372019,	372020}}  		 --  位置4  击杀五个巡逻小怪																		
x020039_g_IdPuTong[4] ={{373001,	373002,	373003,	373004,	373005,	373006,	373007,	373008,	373009,	373010,	373011,	373012,	373013,	373014,	373015,	373016,	373017,	373018,	373019,	373020},{374001,	374002,	374003,	374004,	374005,	374006,	374007,	374008,	374009,	374010,	374011,	374012,	374013,	374014,	374015,	374016,	374017,	374018,	374019,	374020},{373001,	373002,	373003,	373004,	373005,	373006,	373007,	373008,	373009,	373010,	373011,	373012,	373013,	373014,	373015,	373016,	373017,	373018,	373019,	373020}}    --  位置6  第二波精英怪  + 两个小怪  位置5  AI配置在怪物身上	
x020039_g_IdPuTong[5] ={{372001,	372002,	372003,	372004,	372005,	372006,	372007,	372008,	372009,	372010,	372011,	372012,	372013,	372014,	372015,	372016,	372017,	372018,	372019,	372020},{372001,	372002,	372003,	372004,	372005,	372006,	372007,	372008,	372009,	372010,	372011,	372012,	372013,	372014,	372015,	372016,	372017,	372018,	372019,	372020}}           --  位置7 一波巡逻小怪  																				
x020039_g_IdPuTong[6] ={{375001,	375002,	375003,	375004,	375005,	375006,	375007,	375008,	375009,	375010,	375011,	375012,	375013,	375014,	375015,	375016,	375017,	375018,	375019,	375020},{376001,	376002,	376003,	376004,	376005,	376006,	376007,	376008,	376009,	376010,	376011,	376012,	376013,	376014,	376015,	376016,	376017,	376018,	376019,	376020},{375001,	375002,	375003,	375004,	375005,	375006,	375007,	375008,	375009,	375010,	375011,	375012,	375013,	375014,	375015,	375016,	375017,	375018,	375019,	375020}}    --  位置8 第三波精英怪  同上 	
x020039_g_IdPuTong[7] ={{372001,	372002,	372003,	372004,	372005,	372006,	372007,	372008,	372009,	372010,	372011,	372012,	372013,	372014,	372015,	372016,	372017,	372018,	372019,	372020}}     		 --  位置9 一波巡逻小怪																																					
x020039_g_IdPuTong[8] ={{377001,	377002,	377003,	377004,	377005,	377006,	377007,	377008,	377009,	377010,	377011,	377012,	377013,	377014,	377015,	377016,	377017,	377018,	377019,	377020}}   		 --  位置11   Boss ID																																					
x020039_g_IdPuTong[9] ={{370101,	370102,	370103,	370104,	370105,	370106,	370107,	370108,	370109,	370110,	370111,	370112,	370113,	370114,	370115,	370116,	370117,	370118,	370119,	370120},{373101,	373102,	373103,	373104,	373105,	373106,	373107,	373108,	373109,	373110,	373111,	373112,	373113,	373114,	373115,	373116,	373117,	373118,	373119,	373120}, {375101,	375102,	375103,	375104,	375105,	375106,	375107,	375108,	375109,	375110,	375111,	375112,	375113,	375114,	375115,	375116,	375117,	375118,	375119,	375120}}      -- 40血的元素怪Id   AI不用配置技能  手动释放	
x020039_g_IdPuTong[10]={{371101,	371102,	371103,	371104,	371105,	371106,	371107,	371108,	371109,	371110,	371111,	371112,	371113,	371114,	371115,	371116,	371117,	371118,	371119,	371120},{374101,	374102,	374103,	374104,	374105,	374106,	374107,	374108,	374109,	374110,	374111,	374112,	374113,	374114,	374115,	374116,	374117,	374118,	374119,	374120}, {376101,	376102,	376103,	376104,	376105,	376106,	376107,	376108,	376109,	376110,	376111,	376112,	376113,	376114,	376115,	376116,	376117,	376118,	376119,	376120}}     -- 80血的精英怪Id       	
																																																									
x020039_g_AdaptLvl = 20																																																										

x020039_g_MonsterPosList={}     
x020039_g_MonsterPosList[1]={{95.12,45.82,3.21}, {91.50,50.96,3.21}, {94.07,56.85,3.21}}		
x020039_g_MonsterPosList[2]={{81.96,65.75,-1.06}, {85.19,67.44,-1.06}}		--  小怪坐	```标
x020039_g_MonsterPosList[3]={{61.89,145.32,4.1},{65.1,143.55,4.1}}	  
x020039_g_MonsterPosList[4]={{71.13,164.87,4.1},{75.58,162.30,4.1},{80.45,159.25,4.1}}	   
x020039_g_MonsterPosList[5]={{131.14,167.41,2.10},{129.87,163.11,2.10}}		
x020039_g_MonsterPosList[6]={{154.09,161.41,2.10},{153.23,156.74,2.10},{148.83,153.83,2.10}}		-- 对应上方怪物ID  坐标
x020039_g_MonsterPosList[7]={{162.24,142.70,2.10}}          -- 这波没用了
x020039_g_MonsterPosList[8]={{115.49,107.59,1.61}}        
x020039_g_MonsterPosList[9]={{129.65,106.96,3.12}, {115.58,92.96,1.61},{100.25,107.44,-0.08}}
x020039_g_MonsterPosList[10]={{129.65,106.96,3.12}, {115.58,92.96,1.61},{100.25,107.44,-0.08}}




x020039_g_CSParamIdxCurStage=0                     -- 场景切换波数
x020039_g_CSParamIdxCurSubStage=1				   -- 同一场景中的时间点		
x020039_g_CSParamIdxrandtime=2					   -- 80血以下是否释放		
x020039_g_CSParamIdxcurNum = 3					   -- 40血以下是否释放
x020039_g_CSParamNumJYone = 4   				   -- 场面上的第1种类精英怪物数量
x020039_g_CSParamNumJYtwo = 5	  				   -- 场面上的第2种类精英怪物数量
x020039_g_CSParamNumJYthree = 6   				   -- 场面上的第3种类精英怪物数量

x020039_g_User_ParamIdOfOne = 1                   -- 三环本第一环钥匙
x020039_g_User_ParamIdOfTwo = 2 				  -- 三环本第二换钥匙


x020039_g_BuffId = 200331						-- Boss释放的加防御的buff
x020039_g_BuffJyNum = 1				  --   同时释放1个增益buff
x020039_g_BuffJyId = {}
x020039_g_BuffJyId[1] = {200331, 200331, 200331}   -- 精英怪物的三个Debuff   对应顺序为出场顺序

x020039_g_Hp2 = 60  --  第二次大招血量
x020039_g_Hp1 = 95	--  第一次

x020039_g_ShoutBossId1 = 21    -- 第一次释放召唤的时候的FakeobjId
x020039_g_ShoutBossId2 = 21	   -- 第二次释放召唤的时候的FakeobjId

x020039_g_ShoutStrId1 = 50015	-- 第一次释放召唤的时候的字典Id
x020039_g_ShoutStrId2 = 50016

x020039_g_BuffZyId = {}
x020039_g_BuffZyId[1] = {100, 100, 100}  	-- 三个临时技能的生成点grouppointID

x020039_g_BuffZyPos = {}						--  三个临时技能生成点的坐标
x020039_g_BuffZyPos[1]={{106,85,3.21}, {106,85,3.21},{88,74,0}}

x020039_g_TpId = {}
x020039_g_TpId[1] = {1034, 1035, 1036}	  -- 传送点ID
x020039_g_TpPos={}
x020039_g_TpPos[1]={75.9,80.02}   
x020039_g_TpPos[2]={89.44,183.38}-- 传送点生成坐标
x020039_g_TpPos[3]={161.66,143.38}

x020039_g_IdCSItemBoxxiayi = 10601   -- 侠义值箱子ID
x020039_g_IdCSItemBox = 10501     --  基础奖励箱子

function x020039_OnCopySceneOpen( scene )
	AddCSMission(scene,66021)
	AddCSMission(scene,66022)
	AddCSMission(scene,66023)
	AddCSMission(scene,66024)
	AddCSMission(scene,66025)
	AddCSMission(scene,66026)
	AddCSMission(scene,66027)
	AddCSMission(scene,66028)
	AddCSMission(scene,66029)
	AddCSMission(scene,66030)
end
function x020039_OnMissionComplete(scene, nObjId, nMissionId)
	--print(nMissionId)
	if nMissionId == 66023 then
		OnCSMissionStage(scene,3)
	end
	if nMissionId == 66026 then
		OnCSMissionStage(scene, 6)
	end
	if nMissionId == 66029 then
		OnCSMissionStage(scene, 9)
	end
end
function x020039_OnCopySceneTick(scene,tickCount)					--副本阶段
	local curStage = GetCopySceneParam(scene,x020039_g_CSParamIdxCurStage) +1
	local curSubStage = GetCopySceneParam(scene,x020039_g_CSParamIdxCurSubStage) +1
	SetCopySceneParam(scene,x020039_g_CSParamIdxCurSubStage,curSubStage)
	--for i =1,3 do
	--math.randomseed(os.time())
	--	SetSceneBuff(scene,x020039_g_BuffIdLst[math.random(3)],x020039_g_BuffPos[2][1],x020039_g_BuffPos[2][2],0)
	--	SetSceneBuff(scene,x020039_g_BuffIdLst[math.random(3)],x020039_g_BuffPos[3][1],x020039_g_BuffPos[3][2],0)
	--end
	--local MonCount = GetMonsterCount(scene)
	
	
	
--		local nCount = #x020039_g_MonsterPosList[curStage]
--		x020039_g_CurNum = (curStage-1)*5 + (nCount - MonCount+25)

--	end	  

	local nAdaptedLvl = math.floor(GetAdaptLvl(scene)/5)	
	if(nAdaptedLvl > x020039_g_AdaptLvl)  then
		nAdaptedLvl = x020039_g_AdaptLvl
	end	
	if curStage == 1 then
		x020039_Stage_1(scene,curStage,curSubStage,nAdaptedLvl)
	--	x020039_Success(scene)
	elseif curStage == 2 then
		x020039_Stage_2(scene,curStage,curSubStage,nAdaptedLvl)
	elseif curStage == 3 then
		x020039_Stage_3(scene,curStage,curSubStage,nAdaptedLvl)
	elseif curStage == 4 then
		x020039_Stage_4(scene,curStage,curSubStage,nAdaptedLvl)
	elseif curStage == 5 then
		x020039_Stage_5(scene,curStage,curSubStage,nAdaptedLvl)
	elseif curStage == 6 then
		x020039_Stage_6(scene,curStage,curSubStage,nAdaptedLvl)
	elseif curStage == 7 then
		x020039_Stage_7(scene,curStage,curSubStage,nAdaptedLvl)
	--elseif curStage == 8 then
	--	x020039_Stage_8(scene,curStage,curSubStage,nAdaptedLvl)
	end		
	local Hp = CalculatePercenHp(scene, GetObjIdByRoleBase(scene, x020039_g_IdPuTong[8][1][nAdaptedLvl]))
--	if(CalculatePercenHp(scene, GetObjIdByRoleBase(scene, x020039_g_IdPuTong[8][1][nAdaptedLvl])) == 100) then
	if(not MonIsBattle(scene, x020039_g_IdPuTong[8][1][nAdaptedLvl]) and Hp > 0) then 
		SetCopySceneParam(scene, x020039_g_CSParamIdxrandtime, 0)
		SetCopySceneParam(scene, x020039_g_CSParamIdxcurNum, 0)
		DeleMonsterByRoleId(scene, x020039_g_IdPuTong[9][1][nAdaptedLvl])
		DeleMonsterByRoleId(scene, x020039_g_IdPuTong[9][2][nAdaptedLvl])
		DeleMonsterByRoleId(scene, x020039_g_IdPuTong[9][3][nAdaptedLvl])
		DeleMonsterByRoleId(scene, x020039_g_IdPuTong[10][1][nAdaptedLvl])
		DeleMonsterByRoleId(scene, x020039_g_IdPuTong[10][2][nAdaptedLvl])
		DeleMonsterByRoleId(scene, x020039_g_IdPuTong[10][3][nAdaptedLvl])
		SetCopySceneParam(scene, x020039_g_CSParamNumJYone, 0)
		SetCopySceneParam(scene, x020039_g_CSParamNumJYtwo, 0)
		SetCopySceneParam(scene, x020039_g_CSParamNumJYthree, 0)
	end
	if ((CalculatePercenHp(scene, GetObjIdByRoleBase(scene, x020039_g_IdPuTong[8][1][nAdaptedLvl])) <= x020039_g_Hp1) and (CalculatePercenHp(scene, GetObjIdByRoleBase(scene, x020039_g_IdPuTong[8][1][nAdaptedLvl])) ~= -1) and (CalculatePercenHp(scene, GetObjIdByRoleBase(scene, x020039_g_IdPuTong[8][1][nAdaptedLvl])) ~= 0) and GetCopySceneParam(scene, x020039_g_CSParamIdxrandtime) == 0)then
		SetCopySceneParam(scene, x020039_g_CSParamIdxrandtime, 1)
		local MonobjId = GetObjIdByRoleBase(scene, x020039_g_IdPuTong[8][1][nAdaptedLvl])
		SendImpactToUnit(scene, MonobjId, x020039_g_BuffId)
		local nUserCount = GetSceneUserCount(scene)
		for i=0,nUserCount-1 do
			local nUserObjId = GetUserObjIdByIdx(scene,i)
			ShowBossTips(scene, nUserObjId, x020039_g_ShoutBossId1, x020039_g_ShoutStrId1,  x020039_g_IdPuTong[8][1][nAdaptedLvl])
		end
		x020039_CreateMonster(scene, 10, 3, nAdaptedLvl)
	end
	if ((CalculatePercenHp(scene, GetObjIdByRoleBase(scene, x020039_g_IdPuTong[8][1][nAdaptedLvl])) <= x020039_g_Hp2) and (CalculatePercenHp(scene, GetObjIdByRoleBase(scene, x020039_g_IdPuTong[8][1][nAdaptedLvl])) ~= -1) and (CalculatePercenHp(scene, GetObjIdByRoleBase(scene, x020039_g_IdPuTong[8][1][nAdaptedLvl])) ~= 0) and GetCopySceneParam(scene, x020039_g_CSParamIdxcurNum) == 0)then
		SetCopySceneParam(scene, x020039_g_CSParamIdxcurNum, 1)
		SetCopySceneParam(scene, x020039_g_CSParamNumJYone, 1)
		SetCopySceneParam(scene, x020039_g_CSParamNumJYtwo, 1)
		SetCopySceneParam(scene, x020039_g_CSParamNumJYthree, 1)
		local nUserCount = GetSceneUserCount(scene)
		for i=0,nUserCount-1 do
			local nUserObjId = GetUserObjIdByIdx(scene,i)
			ShowBossTips(scene, nUserObjId, x020039_g_ShoutBossId2, x020039_g_ShoutStrId2,  x020039_g_IdPuTong[8][1][nAdaptedLvl])
		end
		x020039_CreateMonster(scene, 9, 3, nAdaptedLvl)
	end
	if(curStage == 7 and tickCount%10 == 0 and (CalculatePercenHp(scene, GetObjIdByRoleBase(scene, x020039_g_IdPuTong[8][1][nAdaptedLvl])) <= x020039_g_Hp2) and (CalculatePercenHp(scene, GetObjIdByRoleBase(scene, x020039_g_IdPuTong[8][1][nAdaptedLvl])) ~= 0)) then
		local count  = 0
		--print("!!")
		local oldNumJYone = GetCopySceneParam(scene, x020039_g_CSParamNumJYone)
		local oldnumjytow = GetCopySceneParam(scene, x020039_g_CSParamNumJYtwo)
		local oldnumjythree = GetCopySceneParam(scene, x020039_g_CSParamNumJYthree)
		--print(oldNumJYone, "+" , oldnumjytow, "+ ", oldnumjythree)
		
		for i = 1, 100 do
			local rand = RandomMon(4, 6)
			if(GetCopySceneParam(scene, rand) >= 1 and count < x020039_g_BuffJyNum) then
				count = count + 1
				local rand2 = 0
				local nUserCount = GetSceneUserCount(scene)
				if nUserCount ~= 1 then
					 rand2 = RandomMon(0, nUserCount-1)
				end
				local nUserObjId = GetUserObjIdByIdx(scene,rand2)
				--print(rand, "+", rand2)
				SendImpactToUnit(scene, nUserObjId, x020039_g_BuffJyId[1][rand-3])
				SetCopySceneParam(scene, rand, 0)
			end
		end
		SetCopySceneParam(scene, x020039_g_CSParamNumJYone, oldNumJYone)
		SetCopySceneParam(scene, x020039_g_CSParamNumJYtwo, oldnumjytow)
		SetCopySceneParam(scene, x020039_g_CSParamNumJYthree, oldnumjythree)
	end
end

function x020039_Success(scene)
	local nUserCount = GetSceneUserCount(scene)
	for i=0,nUserCount-1 do
		local nUserObjId = GetUserObjIdByIdx(scene,i)
	--	AwardCSItemBox(scene,nUserObjId,x020039_g_IdCSItemBoxxiayi,x020039_g_IdCSItemBox)
	end

	SetCopySceneSuccess(scene)
end

function  x020039_OnObjDie(scene,objId,rolebaseId,killerId)			
	local MonCount = GetHostileMonsterCount(scene)
	local nAdaptedLvl = math.floor(GetAdaptLvl(scene)/5)	
	if(nAdaptedLvl > x020039_g_AdaptLvl)  then
		nAdaptedLvl = x020039_g_AdaptLvl
	end	
	local nCurStage = GetCopySceneParam(scene, x020039_g_CSParamIdxCurStage)
	if(rolebaseId == x020039_g_IdPuTong[10][1][nAdaptedLvl]) then
			SetSceneBuff(scene, x020039_g_BuffZyId[1][1], x020039_g_BuffZyPos[1][1][1], x020039_g_BuffZyPos[1][1][2], 0)
	end
	if(rolebaseId == x020039_g_IdPuTong[10][2][nAdaptedLvl]) then
			SetSceneBuff(scene, x020039_g_BuffZyId[1][2], x020039_g_BuffZyPos[1][2][1], x020039_g_BuffZyPos[1][2][2], 0)
	end
	if(rolebaseId == x020039_g_IdPuTong[10][3][nAdaptedLvl]) then
			SetSceneBuff(scene, x020039_g_BuffZyId[1][3], x020039_g_BuffZyPos[1][3][1], x020039_g_BuffZyPos[1][3][2], 0)
	end
		--print(rolebaseId ,"!!" , x020039_g_IdPuTong[9][1][nAdaptedLvl])
	if(rolebaseId == x020039_g_IdPuTong[9][1][nAdaptedLvl]) then
			SetCopySceneParam(scene, x020039_g_CSParamNumJYone, 0)
	end
	if(rolebaseId == x020039_g_IdPuTong[9][2][nAdaptedLvl]) then
			SetCopySceneParam(scene, x020039_g_CSParamNumJYtwo, 0)
	end
	if(rolebaseId == x020039_g_IdPuTong[9][3][nAdaptedLvl]) then
			SetCopySceneParam(scene, x020039_g_CSParamNumJYthree, 0)
	end
	if(MonCount == 0) then
			SetCopySceneParam(scene,x020039_g_CSParamIdxCurStage,nCurStage+1)	
			SetCopySceneParam(scene,x020039_g_CSParamIdxCurSubStage,0)
			if(nCurStage == 6)    then															-- BOSS奖励次数限制
				x020039_Success(scene)
			end
	end
end

function x020039_CreateMonster( scene,idx,camp, nlv)					--刷怪（
	local count = #x020039_g_MonsterPosList[idx]
	for i = 1, count  do
		local nMonObjId = CreateMonster(scene,  x020039_g_IdPuTong[idx][i][nlv], x020039_g_MonsterPosList[idx][i][3], x020039_g_MonsterPosList[idx][i][1],x020039_g_MonsterPosList[idx][i][2])
		SetNpcCampByObjId(scene, nMonObjId, camp)
	end
end

function x020039_CheckMissionMD(scene ,objId, type)
	local nLastData = GetMissionMD(scene, objId, 4, type)
	local nNewData = CheckRefreshTime(scene, nLastData, objId, 0)
	if math.floor(nLastData/100) ~= nNewData then
		SetMissionMD(scene, objId, 4, type, nNewData*100)
	end
end

function x020039_Stage_1(scene ,stage,substage, nlv)        -- 第一波精英                
	if substage == 1 then
		OnCSMissionStage(scene,0)
		x020039_CreateMonster(scene,stage,3,nlv)
	end
end

function x020039_Stage_2(scene ,stage,substage, nlv)                        
	if substage == 1 then
		OnCSMissionStage(scene,1)
		x020039_CreateMonster(scene,stage,3,nlv)
	end
end

function x020039_Stage_3(scene ,stage,substage,nlv)                      
	if substage == 1 then
		OnCSMissionStage(scene,2)
		SetSceneBuff(scene, x020039_g_TpId[1][1], x020039_g_TpPos[1][1], x020039_g_TpPos[1][2], 0)
	elseif substage == 2 then
		x020039_CreateMonster(scene,stage,3,nlv)
	end
end

function x020039_Stage_4(scene ,stage,substage,nlv)			-- 第二波精英			
	if substage == 1 then
		OnCSMissionStage(scene,4)
		x020039_CreateMonster(scene,stage,3,nlv)
	end
end

function x020039_Stage_5(scene ,stage,substage,nlv)		
	if substage == 1 then
		OnCSMissionStage(scene,5)
		SetSceneBuff(scene, x020039_g_TpId[1][2], x020039_g_TpPos[2][1], x020039_g_TpPos[2][2], 0)
	elseif substage == 2 then
		x020039_CreateMonster(scene,stage,3,nlv)
	end
end

function x020039_Stage_6(scene ,stage,substage,nlv)		 -- 第三波精英				
	if substage == 1 then
		OnCSMissionStage(scene,7)
		x020039_CreateMonster(scene,stage,3,nlv)
	end
end

function x020039_Stage_7(scene ,stage,substage,nlv)		-- Boss  		
	if substage == 1 then
		OnCSMissionStage(scene,8)
		SetSceneBuff(scene, x020039_g_TpId[1][3], x020039_g_TpPos[3][1], x020039_g_TpPos[3][2], 0)
	elseif substage == 2 then
		x020039_CreateMonster(scene,8,3,nlv)
	end
end