--任务副本1
--进入副本→播放MOV→刷第一波小怪→击杀第一波小怪→刷BOSS→击杀BOSS→副本完成→传出副本

x020186_g_scriptId = 020186

--副本参数
x020186_g_CSParamIdxCurStage = 0			  --大阶段	
x020186_g_CSParamIdxCurSubStage = 1           --小阶段
x020186_g_KillMonsterNum = 2				  -- 记录杀了多少怪
x020186_g_BossOneMark = 4   -- Boss 1 标记 
x020186_g_BossTwoMark = 5   --  Boss 2 标记
x020186_g_Exp = 6 -- 获得的经验

x020186_g_EndMark = 11    		      -- 结束标识
x020186_g_AdapteLvl = 60  --   每个怪物的自适应等级套数  必须随适应等级的变化而改变

x020186_g_BossMonstorList={}       -- BOSS
x020186_g_BossMonstorList[1]={1400001}  -- 百分之百 boss
x020186_g_BossMonstorList[2]={1400102}  -- 百分之70 boss
x020186_g_BossMonstorList[3]={1400203}  -- 百分之40 boss

x020186_g_BossMonstorPosList={} -- BOSS位置 
x020186_g_BossMonstorPosList[1]={{54,90,-1.57}}

x020186_g_MonstorList={}
x020186_g_MonstorList[1]={1400304, 1400304, 1400304, 1400304}  --小怪
x020186_g_MonstorList[2]={1400405, 1400405, 1400405, 1400405, 1400405, 1400405,1400405, 1400405}  --小怪


x020186_g_MonstorPosList={}  -- 小怪位置 
x020186_g_MonstorPosList[1]={{49.5,90,-1.57},{52.5,90,-1.57},{55.5,90,-1.57},{58.5,90,-1.57}} --小怪坐标
x020186_g_MonstorPosList[2]={{49.5,90,-1.57},{52.5,90,-1.57},{55.5,90,-1.57},{58.5,90,-1.57},{49.5,93.5,-1.57},{52.5,93.5,-1.57},{55.5,93.5,-1.57},{58.5,93.5,-1.57}} --小怪坐标

x020186_g_BossDrop1 =-1   -- Boss 掉落

x020186_g_DropHp={}
x020186_g_DropHp[1] = {70, 40}  --- boss血量

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

function x020186_OnCopySceneOpen(scene)
	local nUserCount = GetSceneUserCount(scene)
	for j=0,nUserCount-1 do
		local nUserObjId = GetUserObjIdByIdx(scene,j)
		SetHpFull(scene, nUserObjId)
	end	
	SetCopySceneParam(scene, x020186_g_CSParamIdxCurStage, 0)
	SetCopySceneParam(scene, x020186_g_BossOneMark, 0)
	SetCopySceneParam(scene, x020186_g_BossTwoMark, 0)
	SetCopySceneParam(scene, x020186_g_Exp, 0)
	SetCopySceneParam(scene, x020186_g_EndMark, 0)
	SetXiLianFuBenParam(scene, 0)  -- 设置经验参数
end


--死亡回调
function x020186_OnObjDie( scene,objId,rolebaseId,killerId )
	local nCurStage = GetCopySceneParam(scene,x020186_g_CSParamIdxCurStage)
	local OldValue = GetCopySceneParam(scene, x020186_g_KillMonsterNum)
	local FlagOne = GetCopySceneParam(scene, x020186_g_BossOneMark)
	local nUser_ObjId = GetUserObjIdByIdx(scene, 0)
	if objId == nUser_ObjId then
		return 
	end
	local nAdaptedLvl = math.floor(GetAdaptLvl(scene)/10)
	if nAdaptedLvl > x020186_g_AdapteLvl then
		nAdaptedLvl = x020186_g_AdapteLvl
	end
	if rolebaseId == (nAdaptedLvl + x020186_g_BossMonstorList[1][1]) then
		SetCopySceneParam(scene, x020186_g_BossOneMark, 1)
	end
	if rolebaseId == (nAdaptedLvl + x020186_g_BossMonstorList[2][1]) then
		SetCopySceneParam(scene, x020186_g_BossTwoMark, 1)
	end
	local MonCount = GetHostileMonsterCountForXinLian(scene, nAdaptedLvl + x020186_g_BossMonstorList[1][1], nAdaptedLvl + x020186_g_BossMonstorList[2][1])
	
	if OldValue < #x020186_g_MonstorList[2] and rolebaseId > 100 then
    	SetCopySceneParam(scene, x020186_g_KillMonsterNum, OldValue + 1)
    end
    local nUserCount = GetSceneUserCount(scene)
    for i = 0, nUserCount - 1 do
        local nUserObjId = GetUserObjIdByIdx(scene, i)
        SynCsStageData(scene, nUserObjId)
    end
    XiLianFuBenSameExp(scene, rolebaseId)
    if rolebaseId == x020186_g_BossMonstorList[3][1] + nAdaptedLvl then
    	local ZhobjId = GetObjIdByRoleBase(scene, rolebaseId)
    	for k = 0,nUserCount-1 do
			local nUserObjId = GetUserObjIdByIdx(scene,k)
			CreatDropBox(scene, nUserObjId, x020186_g_BossDrop1, ZhobjId)
			print("boss3------------ 掉落")
		end
    end
    if(rolebaseId == (nAdaptedLvl + x020186_g_BossMonstorList[3][1])) then
    	SetCopySceneParam(scene, x020186_g_EndMark, 1)
    end
	if MonCount == 0 or rolebaseId == (nAdaptedLvl + x020186_g_BossMonstorList[1][1]) or rolebaseId == (x020186_g_BossMonstorList[2][1] + nAdaptedLvl) then 
		SetCopySceneParam(scene,x020186_g_CSParamIdxCurStage,nCurStage+1)
		SetCopySceneParam(scene,x020186_g_CSParamIdxCurSubStage,0)	
		SetCopySceneParam(scene, x020186_g_KillMonsterNum, 0)
	end
end


--副本Tick
function x020186_OnCopySceneTick(scene,tickCount)
	local curStage = GetCopySceneParam(scene,x020186_g_CSParamIdxCurStage)
	local curSubStage = GetCopySceneParam(scene,x020186_g_CSParamIdxCurSubStage)
	SetCopySceneParam(scene, x020186_g_CSParamIdxCurSubStage, curSubStage+1)
	local nUserCount = GetSceneUserCount(scene)
	local nEndMark = GetCopySceneParam(scene,x020186_g_EndMark)
	local nAdaptedLvl = math.floor(GetAdaptLvl(scene)/10)
	if nAdaptedLvl > x020186_g_AdapteLvl then
		nAdaptedLvl = x020186_g_AdapteLvl
	end
	if curStage == 0 then
		x020186_Stage_1(scene, curStage, curSubStage, nAdaptedLvl)
		x020186_StageControl(scene, curStage)
	elseif curStage == 1 then
		x020186_Stage_2(scene, curStage, curSubStage, nAdaptedLvl)
		x020186_StageControl(scene, curStage)
	elseif curStage == 2 then		
		x020186_Stage_3(scene, curStage, curSubStage, nAdaptedLvl)
		x020186_StageControl(scene, curStage)	
	elseif curStage == 3 then
		x020186_Stage_4(scene, curStage, curSubStage, nAdaptedLvl)
		x020186_StageControl(scene, curStage)
	elseif curStage == 4 then
		x020186_Stage_5(scene, curStage, curSubStage, nAdaptedLvl)
		x020186_StageControl(scene, curStage)
	elseif curStage == 5 and nEndMark == 1 then
		XiLianFuBenSecondEnd(scene, true)
		SetCopySceneSuccess(scene)
	end
end

--第1阶段 刷BOSS
function x020186_Stage_1(scene ,stage,substage, nlv)
	if substage == 0 then
		TellPlayerAutoCombat(scene)
		local count = #x020186_g_BossMonstorList[1]
		for i = 1, count do
			CreateMonster(scene, x020186_g_BossMonstorList[1][1] + nlv, x020186_g_BossMonstorPosList[1][1][3], x020186_g_BossMonstorPosList[1][1][1], x020186_g_BossMonstorPosList[1][1][2])
		end
	end
end

-- 第2阶段 刷小怪
function x020186_Stage_2(scene ,stage,substage, nlv)
	if substage == 1 then
		TellPlayerAutoCombat(scene)
		local count = #x020186_g_MonstorList[1]
		for i = 1, count do
			CreateMonster(scene, x020186_g_MonstorList[1][i] + nlv, x020186_g_MonstorPosList[1][i][3], x020186_g_MonstorPosList[1][i][1], x020186_g_MonstorPosList[1][i][2])
		end
	end
end

--第3阶段 刷BOSS
function x020186_Stage_3(scene ,stage,substage, nlv)
	if substage == 2 then
		local count = #x020186_g_BossMonstorList[2]
		for i = 1, count do
			CreateMonster(scene, x020186_g_BossMonstorList[2][1] + nlv, x020186_g_BossMonstorPosList[1][1][3], x020186_g_BossMonstorPosList[1][1][1], x020186_g_BossMonstorPosList[1][1][2])
		end
	end
end

--第4阶段 刷小怪
function x020186_Stage_4(scene ,stage,substage, nlv)
	if substage == 2 then
		local count = #x020186_g_MonstorList[2]
		for i = 1, count do
			CreateMonster(scene, x020186_g_MonstorList[2][i] + nlv, x020186_g_MonstorPosList[2][i][3], x020186_g_MonstorPosList[2][i][1], x020186_g_MonstorPosList[2][i][2])
		end
	end
end

--第5阶段 刷Boss
function x020186_Stage_5(scene ,stage,substage, nlv)
	if substage == 2 then
		local count = #x020186_g_BossMonstorList[3]
		for i = 1, count do
			CreateMonster(scene, x020186_g_BossMonstorList[3][1] + nlv, x020186_g_BossMonstorPosList[1][1][3], x020186_g_BossMonstorPosList[1][1][1], x020186_g_BossMonstorPosList[1][1][2])
		end
	end
end

function x020186_HpCountDown1(scene)
	local nUserCount = GetSceneUserCount(scene)
	local nCurStage = GetCopySceneParam(scene,x020186_g_CSParamIdxCurStage)
	for i=0,nUserCount-1 do   
		local nUserObjId = GetUserObjIdByIdx(scene,i)   
		SynCsStageData(scene, nUserObjId)
	end   
	local nAdaptedLvl = math.floor(GetAdaptLvl(scene)/10)
	if nAdaptedLvl > x020186_g_AdapteLvl then
		nAdaptedLvl = x020186_g_AdapteLvl
	end
	local nUserCount = GetSceneUserCount(scene)
	local ZhobjId1 = GetObjIdByRoleBase(scene, x020186_g_BossMonstorList[1][1] + (nAdaptedLvl))
    if math.ceil(CalculatePercenHp(scene, ZhobjId1)) <= x020186_g_DropHp[1][1] and CalculatePercenHp(scene, ZhobjId1) >= 0 then
        local FlagOne = GetCopySceneParam(scene, x020186_g_BossOneMark)
		if FlagOne == 0 then
			SetCopySceneParam(scene, x020186_g_BossOneMark, 1)
			for k = 0,nUserCount-1 do
				local nUserObjId = GetUserObjIdByIdx(scene,k)
				CreatDropBox(scene, nUserObjId, x020186_g_BossDrop1, ZhobjId1)
			end
			SetCopySceneParam(scene,x020186_g_CSParamIdxCurStage,nCurStage+1)
			SetCopySceneParam(scene,x020186_g_CSParamIdxCurSubStage,0)	
			SetCopySceneParam(scene, x020186_g_KillMonsterNum, 0)
		end	
	end
end


function x020186_HpCountDown2(scene)
	local nUserCount = GetSceneUserCount(scene)
	local nCurStage = GetCopySceneParam(scene,x020186_g_CSParamIdxCurStage)
	for i=0,nUserCount-1 do   
		local nUserObjId = GetUserObjIdByIdx(scene,i)   
		SynCsStageData(scene, nUserObjId)
	end   
	local nAdaptedLvl = math.floor(GetAdaptLvl(scene)/10) 
	if nAdaptedLvl > x020186_g_AdapteLvl then
		nAdaptedLvl = x020186_g_AdapteLvl
	end
	local nUserCount = GetSceneUserCount(scene)
	local ZhobjId2 = GetObjIdByRoleBase(scene, x020186_g_BossMonstorList[2][1] + (nAdaptedLvl))
	if math.ceil(CalculatePercenHp(scene, ZhobjId2)) <= x020186_g_DropHp[1][2] and CalculatePercenHp(scene, ZhobjId2) >= 0 then
        local FlagTwo = GetCopySceneParam(scene, x020186_g_BossTwoMark)
		if FlagTwo == 0 then
			SetCopySceneParam(scene, x020186_g_BossTwoMark, 1)
			for k = 0,nUserCount-1 do
				local nUserObjId = GetUserObjIdByIdx(scene,k)
				CreatDropBox(scene, nUserObjId, x020186_g_BossDrop1, ZhobjId2)
			end
			SetCopySceneParam(scene,x020186_g_CSParamIdxCurStage,nCurStage+1)
			SetCopySceneParam(scene,x020186_g_CSParamIdxCurSubStage,0)	
			SetCopySceneParam(scene, x020186_g_KillMonsterNum, 0)
		end	
	end
end

function  x020186_StageControl(scene,nstage)
	SetCopySceneParam(scene, 7, nstage)
	local nUserCount = GetSceneUserCount(scene)
	for j=0,nUserCount-1 do
		local nUserObjId = GetUserObjIdByIdx(scene,j)
		SynCsStageData(scene, nUserObjId)
	end
end

function x020186_OnCopySceneTimeOut(scene)
   	SetCopySceneFail(scene)
   	XiLianFuBenSecondEnd(scene, false)
   	DeleHostileMonster(scene)
end
