x020089_g_scriptId = 020089


--副本参数
x020089_g_AdapteLvl = 60  --   每个怪物的自适应等级套数  必须随适应等级的变化而改变
--以上为波次刷怪的波次适应等级  编号1代表第一波怪的自适应等级    编号1里第一个中括号表示第一波第一个怪物的自适应等级套数  依次类推

x020089_g_BossId = {}
x020089_g_BossId[1] = {488000,488000,488000,488000,488000,488000,488000,488000,488000,488000,488000,488000,488000,488000,488000,488000,488000,488000,488000,488000}	-- Boss编号   表示Boss的自适应等级套数
x020089_g_BossId[2] = {487000} -- buff怪

x020089_g_BossPosList={}
x020089_g_BossPosList[1]={{58.53,44.56,0.53},{67.54,43.52,-3.79},{61.1,63.6,-1.57},{55.36,32.28,1.41},{62.91,55.82,-1.34},{62.49,37.13,1.57},{76.64,37,-4},{80.08,48.71,-3.16},{73.85,52.93,-2.37},{49.8,58.05,-0.41},{55.16,57.7,-1.22},{46.02,49.16,-0.15},{49.69,44.31,0.69},{73.6,62.18,-1.92},{49.02,36.96,0.96},{54.61,38.4,1.15},{54.96,51.04,-0.28},{68.5,51.6,-2.53},{66.11,31.49,1.57},{73.38,41.35,-3.99}}    --  Boss 坐标
x020089_g_BossPosList[2]={{62.04,45.74,-1.57}}    --  buff怪 坐标

x020089_g_MoveListId = {}
x020089_g_MoveListId[1] = { 40001,40002,40003,40004,40005 }  -- buff怪 移动路径

x020089_g_Movparm = 10

x020089_g_CSParamIdxCurStage=0                     -- 场景切换波数
x020089_g_CSParamIdxCurSubStage=1				   -- 同一场景中的时间点
x020089_g_CSParamIdxrandtime=2					   -- 记录击杀了几个第一只怪

x020089_kill_monster_num = 20  					   --  击杀怪物的数量

x020089_g_BossTime=4  -- boss记录间隔时间
x020089_g_BossFlag=5  -- boss标识

x020089_g_sSeedAddBuff = 75401--角色BUFF

function x020089_OnObjEnter(scene, objId)
	--CopySceneNPC(scene,1001101,51001,6,201,1,"XXX")--场景、角色ID、CharModel表ID、势力、出现的场景SceneID、等级、名称
	SynCsStageData(scene, objId, true)
	TestFullHp(scene,objId)
	--AutoCombat(scene,objId,true)
end

function x020089_OnCopySceneQuit(scene)
    RemoveAllPlayerImpact(scene, x020089_g_sSeedAddBuff)
end

function x020089_OnCopySceneTimeOut(scene)
    RemoveAllPlayerImpact(scene, x020089_g_sSeedAddBuff)
end

function x020089_OnCopySceneOpen(scene, objId)
	--SynCsStageData(scene, objId, true)
	--AutoCombat(scene,objId,true)
	StoryAnimation(scene, 2206)
end
function x020089_OnCopySceneTick(scene,tickCount)					--副本阶段

		if tickCount == 0 then

		CaluCSRound(scene)
	end
	local curStage = GetCopySceneParam(scene,x020089_g_CSParamIdxCurStage)
	local curSubStage = GetCopySceneParam(scene,x020089_g_CSParamIdxCurSubStage) +1
	local OldValue = GetCopySceneParam(scene, x020089_g_CSParamIdxrandtime)
	local nBossFlag = GetCopySceneParam(scene, x020089_g_BossFlag)
	local nBossTime = GetCopySceneParam(scene, x020089_g_BossTime)
	SetCopySceneParam(scene,x020089_g_CSParamIdxCurSubStage,curSubStage)
	if GetCopySceneStatus(scene) == 4 and curStage == 1 then
		curStage = 3
		x020089_ClearAllMonster(scene)
		SetCopySceneParam(scene, x020089_g_CSParamIdxCurStage, 3)
		ShowSJCSEnd(scene, false)
	end
	local nAdaptedLvl = math.floor(GetAdaptLvl(scene)/10)
	if nAdaptedLvl > x020089_g_AdapteLvl then
		nAdaptedLvl = x020089_g_AdapteLvl
	end
	if(tickCount == 20) then
		ChangeAllMonsterCamp(scene,3)
	end
	if curStage == 0 then                                           -- MOV
		x020089_Stage_0(scene,curStage,curSubStage,nAdaptedLvl)
	elseif curStage == 1 then										-- 打怪阶段
		x020089_Stage_1(scene,curStage,curSubStage,nAdaptedLvl-1)
	elseif curStage == 2 then										-- 结算阶段
		x020089_Stage_2(scene, curStage, curSubStage)
	end
	if curStage == 1 and OldValue < x020089_kill_monster_num and nBossFlag == 1 and nBossTime == 0 then  -- 延时boss复活
		SetCopySceneParam(scene, x020089_g_BossTime, tickCount)	
	end
	if (tickCount - nBossTime) == 3 and curStage == 1 and OldValue < x020089_kill_monster_num and nBossFlag == 1 then
		local MonId = CreateMonster(scene, x020089_g_BossId[2][1] + (nAdaptedLvl - 1) * 10, x020089_g_BossPosList[2][1][3], x020089_g_BossPosList[2][1][1], x020089_g_BossPosList[2][1][2])
		SendAllPlayerImpact(scene, x020089_g_sSeedAddBuff)
		NpcStartPatrol(scene, MonId, x020089_g_MoveListId[1][1])--怪物寻路
		SetCopySceneParam(scene, x020089_g_BossFlag, 0)	-- 重置boss 标识
		SetCopySceneParam(scene, x020089_g_BossTime, 0)	
	end
end

function x020089_Stage_0(scene ,stage,substage, nlv)
	if ((substage == x020089_g_Movparm and GetCopySceneParam(scene,13) == 0) or GetCopySceneParam(scene,13) == 1) then
		SetCopySceneParam(scene, 13, 2)
		local nCurStage = GetCopySceneParam(scene, x020089_g_CSParamIdxCurStage)
		SetCopySceneParam(scene,x020089_g_CSParamIdxCurStage,1)
		SetCopySceneParam(scene,x020089_g_CSParamIdxCurSubStage,0)
	end
end

function x020089_OnStoryEventEnd(scene,nStoryId,nAnimationId)
	local OldValue =  GetCopySceneParam(scene,13)
	--print("播放完MOV")
	--print(OldValue)
	if OldValue == 0 then

		SetCopySceneParam(scene, 13,1)
	end
end
function x020089_ClearAllMonster(scene)
	--    for i = 1, 3 do
    --	DeleMonsterByRoleId(scene, x020089_g_YuansuId[i][nAdaptedLvl])
    --end
    DeleHostileMonster(scene)

end
function x020089_Stage_2(scene,curStage,curSubStage)
	-- body
	local nCurStage = GetCopySceneParam(scene, x020089_g_CSParamIdxCurStage)
	SetCopySceneParam(scene,x020089_g_CSParamIdxCurStage,nCurStage+1)
	SetCopySceneParam(scene,x020089_g_CSParamIdxCurSubStage,0)
	x020089_ClearAllMonster(scene)
	ShowSJCSEnd(scene, true)
	x020089_Success(scene)

end
function x020089_Success(scene)
	AccoutByCopyScene(scene)
	local nUserCount = GetSceneUserCount(scene)
	for i=0,nUserCount-1 do
		local nUserObjId = GetUserObjIdByIdx(scene,i)
	--	AwardCSItemBox(scene,nUserObjId,x020089_g_IdCSxiayiBox, x020089_g_IdCSItemBox)
	end
	SetCopySceneSuccess(scene)
end


function  x020089_OnObjDie(scene,objId,rolebaseId,killerId)
	local MonCount = GetHostileMonsterCount(scene)    -- 获取活着的怪物数量
	local nCurStage = GetCopySceneParam(scene, x020089_g_CSParamIdxCurStage)
	local nAdaptedLvl = math.floor(GetAdaptLvl(scene)/10)
	local nBossFlag = GetCopySceneParam(scene, x020089_g_BossFlag)
	if nAdaptedLvl > x020089_g_AdapteLvl then
		nAdaptedLvl = x020089_g_AdapteLvl
	end

	local Monindex = 2
 	if x020089_g_BossId[1][1] + (nAdaptedLvl - 1) * 10 == rolebaseId then
    	Monindex = 1
 	end
	local count = #x020089_g_BossPosList[Monindex]
    local nRand = math.random(1, count)
	x020089_OnDieCountDown1(scene, rolebaseId, nAdaptedLvl)
	local OldValue = GetCopySceneParam(scene, x020089_g_CSParamIdxrandtime)

	if OldValue == x020089_kill_monster_num and nCurStage < 2 then
		SetCopySceneParam(scene,x020089_g_CSParamIdxCurStage,nCurStage+1)
		SetCopySceneParam(scene,x020089_g_CSParamIdxCurSubStage,0)
		DeleHostileMonster(scene)
		return
	end
	
	if OldValue < x020089_kill_monster_num and Monindex == 2 and nBossFlag == 0 then
		SetCopySceneParam(scene,x020089_g_BossFlag, 1)
	end
	if OldValue < x020089_kill_monster_num and Monindex == 1 then
		local MonId = CreateMonster(scene, x020089_g_BossId[1][1] + (nAdaptedLvl - 1) * 10, x020089_g_BossPosList[1][nRand][3], x020089_g_BossPosList[1][nRand][1], x020089_g_BossPosList[1][nRand][2])
		-- if rolebaseId - (nAdaptedLvl-1) * 10 == x020089_g_BossId[2][1] then
		-- 	SendAllPlayerImpact(scene, x020089_g_sSeedAddBuff)
		-- 	NpcStartPatrol(scene, MonId, x020089_g_MoveListId[1][1])--怪物寻路
		-- end
	end
end

function x020089_OnDieCountDown1(scene, rolebaseId, nlv)
	local OldValue = GetCopySceneParam(scene, x020089_g_CSParamIdxrandtime)
	if OldValue < x020089_kill_monster_num then
    	SetCopySceneParam(scene, x020089_g_CSParamIdxrandtime, OldValue + 1)
    end
    local nUserCount = GetSceneUserCount(scene)
    for i = 0, nUserCount - 1 do
        local nUserObjId = GetUserObjIdByIdx(scene, i)
        SynCsStageData(scene, nUserObjId)
    end
end

function x020089_CreateMonster( scene,idx,camp, nlv)					--刷怪（
	local count = #x020089_g_MonsterPosList[idx]
	for i = 1, count  do
		CreateMonster(scene,  x020089_g_IdPuTong[idx][i][nlv], x020089_g_MonsterPosList[idx][i][3], x020089_g_MonsterPosList[idx][i][1],x020089_g_MonsterPosList[idx][i][2])
	end
end

function x020089_Stage_1(scene ,stage,substage, nlv)
	if substage == 1 then
        local nUserCount = GetSceneUserCount(scene)
        for i = 0, nUserCount - 1 do
            local nUserObjId = GetUserObjIdByIdx(scene, i)
            AutoCombat(scene, nUserObjId, true)
        end
        for i = 1, #x020089_g_BossId do
            for j = 1, #x020089_g_BossId[i] do
                local MonId = CreateMonster(scene, x020089_g_BossId[i][j] + nlv * 10, x020089_g_BossPosList[i][j][3], x020089_g_BossPosList[i][j][1], x020089_g_BossPosList[i][j][2])
				if x020089_g_BossId[2][1] == x020089_g_BossId[i][j] then
					NpcStartPatrol(scene, MonId, x020089_g_MoveListId[1][1])--怪物寻路
				end
            end
        end
    end
end