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

x060004_g_scriptId = 060004

--副本参数
x060004_g_CSParamIdxCurStage = 0			  --大阶段	
x060004_g_CSParamIdxCurSubStage = 1           --小阶段
x060004_g_CSParam1 = 2
x060004_g_CSParam2 = 3
x060004_g_CSParam3 = 4

x060004_g_CSParamIdxHasBox = 12 -- 该次副本是否有宝箱
x060004_g_CSParamIdxBoxHpAward = 13 -- 该次副本血量奖励索引
x060004_g_CSParamAnimationID = 5038           --mov动画ID

x060004_g_MonstorList={}
x060004_g_MonstorList[1]={600040,600040,600040,600040,600040,600040,600040,600040,600040}  --小怪
x060004_g_MonstorList[2]={600108,600041,600041,600108,600041,600041,600108,600041,600041}  --小怪、精英怪
x060004_g_MonstorList[3]={600208}  --Boss
x060004_g_MonstorList[4]={600042,600042,600042,600042,600042,600042,600042,600042,600042}  --小怪
x060004_g_MonstorList[5]={600109,600043,600043,600109,600043,600043,600109,600043,600043}  --小怪、精英怪
x060004_g_MonstorList[6]={600209}   
x060004_g_MonstorList[7]={600404}        			--  大书怪
                          --Boss

x060004_g_LevelAddBuff = {}
x060004_g_LevelAddBuff[1] = {-1, 30711, 30712, 30712, 30712}

x060004_g_FirstMissionId = 1813	-- 第一个任务
x060004_g_SecMissionId = 1814 -- 第2个任务

x060004_g_DropHp = {}   -- 宝箱掉落百分比血量，注意单调，由高到底，填写大于0的数 不包括0
x060004_g_DropHp[1] = {95,90,80,70,60,50,40,30,20}

x060004_g_BoxNumEnumId = 308
x060004_g_BoxNumCount = 2
x060004_g_BoxCountDown = 11  -- 刷宝箱之前的倒数时间

x060004_g_ClearBoxTime = 41 -- 清除宝箱怪物时间
x060004_g_BigBoxId = 600404  -- 大包厢ID

x060004_g_MonstorPosList={}
x060004_g_MonstorPosList[1]={{61.35,40.35,-1.57},{58.48,42.07,-1.57},{64.33,42.01,-1.57},{46.43,31.48,-0.28},{43.01,30.47,-0.31},{44.35,34.08,-0.31},{75.56,31.65,-2.78},{77.9,30.58,-2.78},{76.6,33.74,-2.78}} --小怪坐标
x060004_g_MonstorPosList[2]={{61.35,40.35,-1.57},{58.48,42.07,-1.57},{64.33,42.01,-1.57},{46.43,31.48,-0.28},{43.01,30.47,-0.31},{44.35,34.08,-0.31},{75.56,31.65,-2.78},{77.9,30.58,-2.78},{76.6,33.74,-2.78}} --小怪、精英坐标
x060004_g_MonstorPosList[3]={{61.64,44.02,-1.57}} --Boss坐标
x060004_g_MonstorPosList[4]={{61.35,40.35,-1.57},{58.48,42.07,-1.57},{64.33,42.01,-1.57},{46.43,31.48,-0.28},{43.01,30.47,-0.31},{44.35,34.08,-0.31},{75.56,31.65,-2.78},{77.9,30.58,-2.78},{76.6,33.74,-2.78}} --小怪坐标
x060004_g_MonstorPosList[5]={{61.35,40.35,-1.57},{58.48,42.07,-1.57},{64.33,42.01,-1.57},{46.43,31.48,-0.28},{43.01,30.47,-0.31},{44.35,34.08,-0.31},{75.56,31.65,-2.78},{77.9,30.58,-2.78},{76.6,33.74,-2.78}} --小怪、精英坐标
x060004_g_MonstorPosList[6]={{61.64,44.02,-1.57}} --Boss坐标
x060004_g_MonstorPosList[7]={{61.1,26.96,-2.86}} --仙书坐标

function x060004_OnCopySceneOpen(scene)
	--AddCSMission(scene,x060004_g_FirstMissionId)
	--AddCSMission(scene,x060004_g_SecMissionId)
end
function x060004_ClearAllMonster(scene)
	--print(count)
	for i = 1, 6 do
		local count = #x060004_g_MonstorList[i]
		for j = 1, count do
			DeleMonsterByRoleId(scene, x060004_g_MonstorList[i][j])
		end
	end
end
--死亡回调
function  x060004_StageControl(scene,nstage)
	SetCopySceneParam(scene, 2, 100)
	SetCopySceneParam(scene, 3, x060004_g_DropHp[1][1])
	SetCopySceneParam(scene, 4, 0)
	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 x060004_OnObjEnter(scene, objId)

	SynCsStageData(scene, objId, true)
	AutoCombat(scene,objId,true)
	x060004_SyncBoxInfo(scene, objId)
end

function x060004_OnDieCountDown1(scene)
		local OldValue = GetCopySceneParam(scene, x060004_g_CSParam1)
		SetCopySceneParam(scene, x060004_g_CSParam1, OldValue+1)
		local nUserCount = GetSceneUserCount(scene)
		for i=0,nUserCount-1 do   
			local nUserObjId = GetUserObjIdByIdx(scene,i)   
			SynCsStageData(scene, nUserObjId)
		end    
end
function x060004_OnObjDie( scene,objId,rolebaseId,killerId )
	local nCurStage = GetCopySceneParam(scene,x060004_g_CSParamIdxCurStage)
	local MonCount = GetHostileMonsterCount(scene)
	local oldStage = GetCopySceneParam(scene, 7)
	if nCurStage == 6 and rolebaseId ~= x060004_g_BigBoxId then
		return    -- 不是宝箱怪物进入宝箱阶段的死亡回调直接return
	end

	if rolebaseId == x060004_g_BigBoxId then
	--	print("最後一擊寶箱獎勵")
		x060004_OnBoxDrop(scene, objId, x060004_g_MonstorList[7][1], true)
		SetCopySceneParam(scene, x060004_g_CSParam1, 0)
		SetCopySceneParam(scene, x060004_g_CSParam2, 0)

				local nUserCount = GetSceneUserCount(scene)
				for i=0,nUserCount-1 do   
					local nUserObjId = GetUserObjIdByIdx(scene,i)   
					SynCsStageData(scene, nUserObjId)
				end    
	end
	if MonCount == 0 and rolebaseId ~= x060004_g_MonstorList[6][1] then
		--if(rolebaseId == x060004_g_MonstorList[2][1]) then
		--	x060004_StageControl(scene, 2)
	--	end
		SetCopySceneParam(scene,x060004_g_CSParamIdxCurStage,nCurStage+1)
		SetCopySceneParam(scene,x060004_g_CSParamIdxCurSubStage,0)
	end
	if rolebaseId == x060004_g_MonstorList[6][1] then
		if GetCSEvaluaion(scene) == 0 then
		 	local nUserCount = GetSceneUserCount(scene)
			for i=0,nUserCount-1 do		
				local nUserObjId = GetUserObjIdByIdx(scene,i)
		 		CSEvalueEnd(scene, nUserObjId)
		 	end
		 	SetCopySceneParam(scene, x060004_g_CSParamIdxHasBox, 1)     -- 激活寶箱階段
		 	DeleHostileMonster(scene)
		 	SetCopySceneParam(scene,x060004_g_CSParamIdxCurStage,nCurStage+1)
			SetCopySceneParam(scene,x060004_g_CSParamIdxCurSubStage,0)
			print("激活寶箱階段")
					--x050001_Stage_Box(scene, curStage, curSubStage)
		elseif GetCSEvaluaion(scene) ~= 0 then
			print("激活寶箱階段失败")
			SetCopySceneParam(scene,x060004_g_CSParamIdxCurStage,7)
			SetCopySceneParam(scene,x060004_g_CSParamIdxCurSubStage,0)
		end

	end
 	local Nowvalue = GetCopySceneParam(scene, x060004_g_CSParamIdxCurStage)
 	if Nowvalue >= 7 then
 		SetCopySceneParam(scene,x060004_g_CSParamIdxCurStage,7)
 	end
end


--副本Tick
function x060004_OnCopySceneTick(scene,tickCount)
	local curStage = GetCopySceneParam(scene,x060004_g_CSParamIdxCurStage)
	local curSubStage = GetCopySceneParam(scene,x060004_g_CSParamIdxCurSubStage)
	SetCopySceneParam(scene, x060004_g_CSParamIdxCurSubStage, curSubStage+1)
	local a = GetCopySceneStatus(scene)
	--print(a)
	if GetCopySceneStatus(scene) == 4 and curStage < 6 then
		curStage = 8
	--	print("失敗了")
		SetCopySceneParam(scene, x060004_g_CSParamIdxCurStage, 8)
		ShowCardEnd(scene, false, 5)
		DeleHostileMonster(scene)
		--ShowSJCSEnd(scene, false)
	end
	x060004_BoxHpTick(scene)
	if curStage <= 5 then
		x060004_Stage_1(scene, curStage, curSubStage)
	elseif curStage == 6 then			-- 进入宝箱阶段
		
		x060004_Stage_Box(scene, curStage, curSubStage)
	elseif curStage == 7 then
		x060004_Stage_2(scene, curStage, curSubStage)
		
	end
end
function x060004_SyncBoxInfo( scene, objId )
	local HasBox = GetCopySceneParam(scene,x060004_g_CSParamIdxHasBox)

	local curStage = GetCopySceneParam(scene,x060004_g_CSParamIdxCurStage)

	local curSubStage = GetCopySceneParam(scene,x060004_g_CSParamIdxCurSubStage)

	if HasBox == 1 and curStage == 6 and curSubStage > x060004_g_BoxCountDown and curSubStage <= x060004_g_ClearBoxTime then
		SyncSLBossBoxTime(scene, objId, x060004_g_ClearBoxTime-curSubStage, x060004_g_ClearBoxTime-x060004_g_BoxCountDown-1)
	end
end

function x060004_HpCountDown1(scene)
--	print("階段是")
--	print(stage)
	local OldHp = math.ceil(GetCopySceneParam(scene, x060004_g_CSParam1))
	local ZhobjId2 = GetObjIdByRoleBase(scene, x060004_g_MonstorList[7][1])
	local NewHp = math.ceil(CalculatePercenHp(scene, ZhobjId2))

	if NewHp < 0 or NewHp == OldHp then
		return
	end
	local TargetHp = 0
	local HpCount = #x060004_g_DropHp[1]

	for i = 1, HpCount do
		if NewHp > x060004_g_DropHp[1][i] then
			TargetHp = x060004_g_DropHp[1][i]
			break
		end
	end

	SetCopySceneParam(scene, x060004_g_CSParam1, NewHp)
	SetCopySceneParam(scene, x060004_g_CSParam2, TargetHp)

				local nUserCount = GetSceneUserCount(scene)
				for i=0,nUserCount-1 do   
					local nUserObjId = GetUserObjIdByIdx(scene,i)   
					SynCsStageData(scene, nUserObjId)
				end    

end
function x060004_Stage_Box(scene ,stage,substage)
	local HasBox = GetCopySceneParam(scene,x060004_g_CSParamIdxHasBox)

	x060004_HpCountDown1(scene)
	local nUserCount = GetSceneUserCount(scene)
	if substage == 0 then
		if HasBox == 0 then 
			return
		end
		x060004_StageControl(scene, stage)
		for i=0,nUserCount-1 do		
			local nUserObjId = GetUserObjIdByIdx(scene,i)
			CSEvalueEnd(scene, nUserObjId,false)	
			x060004_CheckMissionMD(scene,nUserObjId,x060004_g_BoxNumEnumId)
			local oldTime = GetMissionMD(scene, nUserObjId, 4, x060004_g_BoxNumEnumId)
			SetMissionMD(scene, nUserObjId, 4, x060004_g_BoxNumEnumId, oldTime+1)
		end
	elseif substage == x060004_g_BoxCountDown then
		if HasBox == 0 then 
			return
		end
		for i=0,nUserCount-1 do		
			local nUserObjId = GetUserObjIdByIdx(scene,i)
			--AddActivtyNum(scene, nUserObjId, 40)
			--CSEvalueEnd(scene, nUserObjId)
			x060004_SyncBoxInfo(scene, nUserObjId)

		end

		local count = #x060004_g_MonstorList[stage+1]
		local MonId
		local nUserCount = GetSceneUserCount(scene)
		if nUserCount < 1 then
			nUserCount = 1
		end
		for i = 1, count do
			MonId = CreateMonster(scene, x060004_g_MonstorList[stage+1][i], x060004_g_MonstorPosList[stage+1][i][3], x060004_g_MonstorPosList[stage+1][i][1], x060004_g_MonstorPosList[stage+1][i][2])
		--	print("上Buff了")
		--	print(nUserCount)
			SendImpactToUnit(scene, MonId, x060004_g_LevelAddBuff[1][nUserCount])
			SetHpFull(scene, MonId)
			UpdateObjHpMax(scene,MonId)
		end
	elseif substage >= x060004_g_ClearBoxTime then
		DeleHostileMonster(scene)
		SetCopySceneParam(scene,x060004_g_CSParamIdxCurStage,stage+1)
		SetCopySceneParam(scene,x060004_g_CSParamIdxCurSubStage,0)
	end
end
function x060004_BoxHpTick(scene)
	local ZhobjId2 = GetObjIdByRoleBase(scene, x060004_g_MonstorList[7][1])
	local HpCount = #x060004_g_DropHp[1]

	for i=1, HpCount do
		--print("寶箱當前血量百分比")
		--print(CalculatePercenHp(scene, ZhobjId2))
		if math.ceil(CalculatePercenHp(scene, ZhobjId2)) <= x060004_g_DropHp[1][i] and CalculatePercenHp(scene, ZhobjId2) >= 0 then 
			local Flag = GetCopySceneParam(scene,x060004_g_CSParamIdxBoxHpAward - 1 + i)
			if Flag == 0 then
				SetCopySceneParam(scene,x060004_g_CSParamIdxBoxHpAward - 1 + i,1)
			--	print("非最後一擊寶箱獎勵")
			--	print(x060004_g_DropHp[1][i])
				x060004_OnBoxDrop(scene, ZhobjId2, x060004_g_MonstorList[7][1], false)
			end
		end
	end
end
function x060004_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 x060004_OnBoxDrop(scene, MonId, rolebaseId, IsKill)
	local  IsEff = false
	local nUserCount = GetSceneUserCount(scene)
		for i=0,nUserCount-1 do		
			local nUserObjId = GetUserObjIdByIdx(scene,i)
				--x060004_CheckMissionMD(scene,nUserObjId,x060004_g_BossNumEnumId)
			local oldTime = GetMissionMD(scene, nUserObjId, 4, x060004_g_BoxNumEnumId)
			--	SetMissionMD(scene, nUserObjId, 4, x060004_g_BossNumEnumId, oldTime+1)
		--	print("獲得獎勵次數")
		--	print(oldTime)
			local Flag = false
			if(GetMissionMD(scene, nUserObjId, 4, x060004_g_BoxNumEnumId)%100 <= x060004_g_BoxNumCount) then
				CreateCardBox(scene, nUserObjId,IsKill, MonId)
			end
		end
end

--第1阶段 刷小怪
function x060004_Stage_1(scene ,stage,substage)
	if substage == 1 then
			x060004_OnDieCountDown1(scene)
		local count = #x060004_g_MonstorList[stage+1]
		local MonId
		local nUserCount = GetSceneUserCount(scene)
		if nUserCount < 1 then
			nUserCount = 1
		end
		for i = 1, count do
			MonId = CreateMonster(scene, x060004_g_MonstorList[stage+1][i], x060004_g_MonstorPosList[stage+1][i][3], x060004_g_MonstorPosList[stage+1][i][1], x060004_g_MonstorPosList[stage+1][i][2])
		--	print("上Buff了")
		--	print(nUserCount)
			SendImpactToUnit(scene, MonId, x060004_g_LevelAddBuff[1][nUserCount])
			SetHpFull(scene, MonId)
			UpdateObjHpMax(scene,MonId)
		end
	end
end
function x060004_Stage_2(scene ,stage,substage)
	if substage == 1 then
		--x060004_OnDieCountDown1(scene)
		DeleHostileMonster(scene)
		AccoutByCopyScene(scene)
		ShowCardEnd(scene, true, 5)
		
		SetCopySceneSuccess(scene, true)
	
	end
end






