x020079_g_scriptId = 020079

x020079_g_BossId = {} -- BossId   生成怪物规则   每组生成一个 对应下面的位置    每组对应一个buff 
x020079_g_BossId[1] = {{552000,552100}}--带有召唤类的怪物 跟程序确认
x020079_g_BossId[2] = {{552300}}
x020079_g_BossId[3] = {{552400}}
x020079_g_BossId[4] = {{552600},{552700},{552800},{552900}}--带有召唤类的怪物 跟程序确认
x020079_g_BossId[5] = {{553400}}
x020079_g_BossId[6] = {{553500}}
x020079_g_BossId[7] = {{553600}}

x020079_g_BossPos = {} -- Boss位置
x020079_g_BossPos[1] = {{{96.19,37.26,1.69},{96.19,37.26,1.69}}}
x020079_g_BossPos[2] = {{{129.06,124.75,3.95}}}
x020079_g_BossPos[3] = {{{143.08,86.04,3.11}}}
x020079_g_BossPos[4] = {{{89.28,137.02,-1.42}},{{85.6,141.53,-1.42}},{{96.47,143.23,-1.42}},{{93.58,137.84,-1.42}}}
x020079_g_BossPos[5] = {{{40.4,89.2,-0.001}}}
x020079_g_BossPos[6] = {{{55.41,50.81,0.74}}}
x020079_g_BossPos[7] = {{{91.83,88.61,-0.59}}}

x020079_g_BossBuff_NoMonster_1 = 88018 --顶掉管理员身上buff 的buff
x020079_g_BossBuff_NoMonster_2 = 88019
x020079_g_BossBuff = {} --Boss的buff  -1 表示不上任何buff
x020079_g_BossBuff[1] = {{82001,-1}}
x020079_g_BossBuff[2] = {{83001}}
x020079_g_BossBuff[3] = {{-1}}
x020079_g_BossBuff[4] = {{84001},{85001},{86001},{87001}}
x020079_g_BossBuff[5] = {{-1}}
x020079_g_BossBuff[6] = {{-1}}
x020079_g_BossBuff[7] = {{88003}}

x020079_g_HaveAdaptLvl = {} --是否自适应
x020079_g_HaveAdaptLvl[1] = 1
x020079_g_HaveAdaptLvl[2] = 0
x020079_g_HaveAdaptLvl[3] = 1
x020079_g_HaveAdaptLvl[4] = 1
x020079_g_HaveAdaptLvl[5] = 1
x020079_g_HaveAdaptLvl[6] = 1
x020079_g_HaveAdaptLvl[7] = 0



x020079_g_MonsterId = {} -- 广场舞大妈Id
x020079_g_MonsterId[1] = 553700
x020079_g_MonsterId[2] = 553700
x020079_g_MonsterId[3] = 553700

x020079_g_MonsterPatrol = {}
x020079_g_MonsterPatrol[1] = 3300
x020079_g_MonsterPatrol[2] = 3400
x020079_g_MonsterPatrol[3] = 3500

x020079_g_MonsterPos = {} -- 广场舞大妈的出生位置
x020079_g_MonsterPos[1] = {93.83,62.54,-1.42}
x020079_g_MonsterPos[2] = {107.21,88.89,3.16}
x020079_g_MonsterPos[3] = {103.22,100.21,0.74}
x020079_g_MonsterBuff = 88001 --广场舞大妈的buff

x020079_g_IsAdaptDropBox = {}
x020079_g_IsAdaptDropBox[1] = -1
x020079_g_IsAdaptDropBox[2] = -1
x020079_g_IsAdaptDropBox[3] = -1
x020079_g_IsAdaptDropBox[4] = -1
x020079_g_IsAdaptDropBox[5] = -1
x020079_g_IsAdaptDropBox[6] = -1
x020079_g_IsAdaptDropBox[7] = -1
x020079_g_IsAdaptDropBox[8] = -1
x020079_g_IsAdaptDropBox[9] = -1
x020079_g_IsAdaptDropBox[10] = 1
x020079_g_IsAdaptDropBox[11] = 1

x020079_g_MonsterRoleBaseId = {}
x020079_g_MonsterRoleBaseId[1] = 552100
x020079_g_MonsterRoleBaseId[2] = 552200
x020079_g_MonsterRoleBaseId[3] = 552300
x020079_g_MonsterRoleBaseId[4] = 552400
x020079_g_MonsterRoleBaseId[5] = 553000
x020079_g_MonsterRoleBaseId[6] = 553100
x020079_g_MonsterRoleBaseId[7] = 553200
x020079_g_MonsterRoleBaseId[8] = 553300
x020079_g_MonsterRoleBaseId[9] = 553400
x020079_g_MonsterRoleBaseId[10] = 553500
x020079_g_MonsterRoleBaseId[11] = 553600
x020079_g_MonsterDropBoxId = {}
x020079_g_MonsterDropBoxId[1] = 277401
x020079_g_MonsterDropBoxId[2] = 277402
x020079_g_MonsterDropBoxId[3] = 277411
x020079_g_MonsterDropBoxId[4] = 277421
x020079_g_MonsterDropBoxId[5] = 277431
x020079_g_MonsterDropBoxId[6] = 277431
x020079_g_MonsterDropBoxId[7] = 277431
x020079_g_MonsterDropBoxId[8] = 277431
x020079_g_MonsterDropBoxId[9] = 277441
x020079_g_MonsterDropBoxId[10] = 277501
x020079_g_MonsterDropBoxId[11] = 277601



x020079_g_StageMonsterId = {}
x020079_g_StageMonsterId[1] = {552000,552100,552200}
x020079_g_StageMonsterId[2] = {552300}
x020079_g_StageMonsterId[3] = {552400}
x020079_g_StageMonsterId[4] = {552600,552700,552800,552900,553000,553100,553200,553300}
x020079_g_StageMonsterId[5] = {553400}
x020079_g_StageMonsterId[6] = {553500}
x020079_g_StageMonsterId[7] = {553600}


function x020079_OnCopySceneOpen(scene)
    x020079_g_Init(scene)
end
function x020079_g_Init(scene)
	--初始化玩家左侧面板
	local nUserCount = GetSceneUserCount(scene)
	for j=0,nUserCount-1 do   
		local nUserObjId = GetUserObjIdByIdx(scene,j)   
		SynCsStageData(scene, nUserObjId)
	end
	
    local MonsterLevelId = math.ceil(math.floor(GetAdaptLvl(scene)/20)) --自适应等级
    math.randomseed(os.time())--随机数种子
	--刷Boss  并加buff
	local nCountBossCount = #x020079_g_BossId
	for i=1,nCountBossCount do
		local nCountBossId = #x020079_g_BossId[i]
		for j=1,nCountBossId do
			local nIndex = math.random()
			nIndex = nIndex % #x020079_g_BossId[i][j]
			nIndex = math.floor( nIndex + 1)
			CreateMonster(scene,x020079_g_BossId[i][j][nIndex]+MonsterLevelId,x020079_g_BossPos[i][j][nIndex][3],x020079_g_BossPos[i][j][nIndex][1],x020079_g_BossPos[i][j][nIndex][2])
			if x020079_g_BossBuff[i][j][nIndex] ~= -1 then
				if(x020079_g_HaveAdaptLvl[i] == 1) then
					SendImpactToAllByRBID(scene,x020079_g_BossId[i][j][nIndex]+MonsterLevelId,x020079_g_BossBuff[i][j][nIndex] - 10 + MonsterLevelId * 2)
				end
				if(x020079_g_HaveAdaptLvl[i] == 0) then
					SendImpactToAllByRBID(scene,x020079_g_BossId[i][j][nIndex]+MonsterLevelId,x020079_g_BossBuff[i][j][nIndex])
				end
			end
		end
	end
	--刷广场舞大妈   并加Buff

	local nCountMonsterId = #x020079_g_MonsterId
	for i = 1,nCountMonsterId do
		local ThisObjId = CreateMonster(scene,x020079_g_MonsterId[i],x020079_g_MonsterPos[i][3],x020079_g_MonsterPos[i][1],x020079_g_MonsterPos[i][2])
		NpcStartPatrol(scene,ThisObjId,x020079_g_MonsterPatrol[i])
		SendImpactToAllByRBID(scene,x020079_g_MonsterId[i],x020079_g_MonsterBuff)
	end
end

function x020079_OnObjEnter(scene,objId)
	--AutoCombat(scene,objId,true)
end

function x020079_OnCopySceneTimeOut(scene)
	local nUserCount = GetSceneUserCount(scene)
	for i=0,nUserCount-1 do
		local nUserObjId = GetUserObjIdByIdx(scene,i)
		--YouLeChangEnd(scene,nUserObjId,false)
	end
	SetCopySceneFail(scene)
end

function x020079_OnObjDie(scene,objId,rolebaseId,killerId)
	local MonsterLevelId = math.floor(GetAdaptLvl(scene)/20) --自适应等级
	local DropBoxId = math.floor(GetAdaptLvl(scene)/50)
	local DropId = 0
	for i = 1, #x020079_g_MonsterRoleBaseId do
		if (rolebaseId == (x020079_g_MonsterRoleBaseId[i] +MonsterLevelId)) then
			DropId = x020079_g_MonsterDropBoxId[i]
			if(x020079_g_IsAdaptDropBox[i] ~= 1) then
				DropBoxId = 0
			end
		end
	end
	if DropId ~= 0 then
		local nUserCount = GetSceneUserCount(scene)
		for k = 0,nUserCount-1 do		
			local nUserObjId = GetUserObjIdByIdx(scene,k)

			CreatDropBox(scene, nUserObjId, DropId + DropBoxId, objId)
		end
	end
	



	local MonCount = GetHostileMonsterCount(scene)

		MonCount = MonCount - GetAliveMonsterCount(scene,553700)
	
	if( rolebaseId ~= 552000 + MonsterLevelId and rolebaseId ~= 552600 + MonsterLevelId and rolebaseId ~= 552700 + MonsterLevelId and rolebaseId ~= 552800+ MonsterLevelId and rolebaseId ~= 552900+ MonsterLevelId and MonCount == 1) then
		
		if GetAliveMonsterCount(scene,553600+MonsterLevelId) ~= 0 then
			SendImpactToAllByRBID(scene,553600+MonsterLevelId,x020079_g_BossBuff_NoMonster_1)
			SendImpactToAllByRBID(scene,553600+MonsterLevelId,x020079_g_BossBuff_NoMonster_2)
			local nCountMonsterId = #x020079_g_MonsterId
			for i = 1,nCountMonsterId do
				DeleMonsterByRoleId(scene,x020079_g_MonsterId[i])
			end
		end
	end
	if( rolebaseId ~= 552000 + MonsterLevelId and rolebaseId ~= 552600 + MonsterLevelId and rolebaseId ~= 552700 + MonsterLevelId and rolebaseId ~= 552800+ MonsterLevelId and rolebaseId ~= 552900+ MonsterLevelId and MonCount == 0) then

		local nUserCount = GetSceneUserCount(scene)
		for i=0,nUserCount-1 do
			local nUserObjId = GetUserObjIdByIdx(scene,i)
			YouLeChangEnd(scene,nUserObjId,true)
		end
		SetCopySceneSuccess(scene)
	end
	if (552000 + MonsterLevelId~= rolebaseId and rolebaseId ~= 552600 + MonsterLevelId and rolebaseId ~= 552700 + MonsterLevelId and rolebaseId ~= 552800 + MonsterLevelId and rolebaseId ~= 552900+ MonsterLevelId) then
		local nMark = -1
		for i = 1, #x020079_g_StageMonsterId do 
			for j = 1,#x020079_g_StageMonsterId[i] do
				if(rolebaseId == (x020079_g_StageMonsterId[i][j]+MonsterLevelId))then
					nMark = i
				end 
			end
		end
		--print(nMark)
		if nMark ~= -1 then
			local nMarkMonsterCount = 0
			for i = 1,#x020079_g_StageMonsterId[nMark] do
				nMarkMonsterCount = nMarkMonsterCount + GetAliveMonsterCount(scene,x020079_g_StageMonsterId[nMark][i]+MonsterLevelId)
			end
			if nMarkMonsterCount == 0 then
				UpdateYouLeChangStage(scene,nMark)
			end
		end
	end
end