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

x020092_g_scriptId = 020092

--副本参数
x020092_g_CSParamIdxCurStage = 0			  --大阶段	
x020092_g_CSParamIdxCurSubStage = 1           --小阶段
x020092_g_KillMonsterNum = 2				  -- 记录杀了多少怪
x020092_g_chooseStage = 3   				  -- 选择时间记录
x020092_g_chooseFinish = 4 					  -- 副本选择完成记录

x020092_g_UserOneId = 1 						-- 记录用户 1 id
x020092_g_UserTwoId = 2 						-- 记录用户 2 id


x020092_g_AdapteLvl = 100  --   每个怪物的自适应等级套数  必须随适应等级的变化而改变

x020092_g_chooseTime = 30

x020092_g_MonstorList={}
x020092_g_MonstorList[1]={12000100, 12000100, 12000100, 12000100, 12000100, 12000100}  --小怪
x020092_g_MonstorList[2]={12000183, 12000183, 12000183, 12000183, 12000183, 12000183, 12000183, 12000183}  --小怪
x020092_g_MonstorList[3]={12000266}                                  --BOSS

x020092_g_MonstorPosList={}
x020092_g_MonstorPosList[1]={{37.13,69.93,-1.88},{37.61,73.9,-1.88},{33.15,69.74,-1.69},{30.78,71.44,-1.88},{34.65,72.62,-1.88},{32.09,75.06,-1.88}} --小怪坐标
x020092_g_MonstorPosList[2]={{78.58,61.39,-3.33},{79.06,65.36,-3.33},{74.6,61.2,-3.14},{72.23,62.9,-3.33},{76.1,64.08,-3.33},{74.01,65.9,-3.33},{73.3,58.34,-3.33},{76.87,58.03,-3.33}} --小怪坐标
x020092_g_MonstorPosList[3]={{124.83,122.72,4.11}}


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

function x020092_OnCopySceneOpen(scene)
	--print("情緣副本")
	local nUserCount = GetSceneUserCount(scene)
	for j=0,nUserCount-1 do
		local nUserObjId = GetUserObjIdByIdx(scene,j)
		SetHpFull(scene, nUserObjId)
	end	
	SetCopySceneParam(scene, x020092_g_chooseStage, 0)
	SetCopySceneParam(scene, x020092_g_chooseFinish, 0)
	SetCopySceneParam(scene, x020092_g_CSParamIdxCurStage, 0)

end


--死亡回调
function x020092_OnObjDie( scene,objId,rolebaseId,killerId )
	local nCurStage = GetCopySceneParam(scene,x020092_g_CSParamIdxCurStage)
	local MonCount = GetHostileMonsterCount(scene)
	local OldValue = GetCopySceneParam(scene, x020092_g_KillMonsterNum)
	--print("OldValue---------------------", OldValue)
	if OldValue < #x020092_g_MonstorList[nCurStage + 1] and rolebaseId > 100 then
    	SetCopySceneParam(scene, x020092_g_KillMonsterNum, OldValue + 1)
    end
    local nUserCount = GetSceneUserCount(scene)
    for i = 0, nUserCount - 1 do
        local nUserObjId = GetUserObjIdByIdx(scene, i)
        SynCsStageData(scene, nUserObjId)
    end
	if MonCount == 0 then
		SetCopySceneParam(scene,x020092_g_CSParamIdxCurStage,nCurStage+1)
		SetCopySceneParam(scene,x020092_g_CSParamIdxCurSubStage,0)	
		SetCopySceneParam(scene, x020092_g_KillMonsterNum, 0)
	end
end


--副本Tick
function x020092_OnCopySceneTick(scene,tickCount)
	local curStage = GetCopySceneParam(scene,x020092_g_CSParamIdxCurStage)
	local curSubStage = GetCopySceneParam(scene,x020092_g_CSParamIdxCurSubStage)
	SetCopySceneParam(scene, x020092_g_CSParamIdxCurSubStage, curSubStage+1)
	local choosetime = GetCopySceneParam(scene,x020092_g_chooseStage)
	local is_finish =  GetCopySceneParam(scene,x020092_g_chooseFinish)
	local nUserCount = GetSceneUserCount(scene)
	local nAdaptedLvl = math.floor(GetAdaptLvl(scene)/10)
	if nAdaptedLvl > x020092_g_AdapteLvl then
		nAdaptedLvl = x020092_g_AdapteLvl
	end
	if curStage == 0 then
		x020092_Stage_1(scene, curStage, curSubStage, nAdaptedLvl)
		x020092_StageControl(scene, curStage)
		SetCopySceneParam(scene,x020092_g_chooseStage, tickCount)
	elseif curStage == 1 then
		x020092_Stage_2(scene, curStage, curSubStage, nAdaptedLvl)
		x020092_StageControl(scene, curStage)
		SetCopySceneParam(scene, x020092_g_chooseStage, tickCount)
	elseif curStage == 2 then		
		x020092_Stage_3(scene, curStage, curSubStage, nAdaptedLvl)
		x020092_StageControl(scene, curStage)	
		SetCopySceneParam(scene, x020092_g_chooseStage, tickCount)
	elseif curStage == 3 and tickCount - choosetime == 2 then
		local high = math_randomEx(1, 6, 3)   -- 获取随机牌  6取 3 
		SetCopySceneGuidParam(scene)
		for i=0,nUserCount-1 do
			local nUserObjId = GetUserObjIdByIdx(scene,i)
			SetChooseQingYuan(scene, nUserObjId, high[1], high[2], high[3])
		end
	elseif curStage == 3 and tickCount - choosetime == 18 and is_finish == 0 and nUserCount == 1 then   -- 组队人退出掉线 情况 强制结束
		local nUserCount = GetSceneUserCount(scene)
		for j=0,nUserCount-1 do
		   local nUserObjId = GetUserObjIdByIdx(scene,j)
		   SetMarryFubenStatus(scene, nUserObjId, nUserCount)
   		end

   	elseif curStage == 3 and tickCount - choosetime == 20 and is_finish == 0 then   -- 退出掉线 选择中又上线 强制结束
		local nUserCount = GetSceneUserCount(scene)
		for j=0,nUserCount-1 do
		   local nUserObjId = GetUserObjIdByIdx(scene,j)
		   SetMarryFubenStatus(scene, nUserObjId)
   		end

	elseif curStage == 3 and is_finish == 1 then
		-- ShowSJCSEnd(scene, true)
		SetCopySceneSuccess(scene)
	end
end

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

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

--第3阶段 刷BOSS
function x020092_Stage_3(scene ,stage,substage, nlv)
	if substage == 2 then
		CreateMonster(scene, x020092_g_MonstorList[3][1] + nlv - 18, x020092_g_MonstorPosList[3][1][3], x020092_g_MonstorPosList[3][1][1], x020092_g_MonstorPosList[3][1][2])
	end
end


function  x020092_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 x020092_OnCopySceneTimeOut(scene)
   local nUserCount = GetSceneUserCount(scene)
   for j=0,nUserCount-1 do
   		local nUserObjId = GetUserObjIdByIdx(scene,j)
   		SetMarryFubenStatus(scene, nUserObjId)
   end
end


function table_fillNum(m,n)
    local j,k
    if n then
       j=m
       k=n
    else
        j=1
        k=m
    end
    local t={}
    for i=j,k do
        table.insert(t,i)
    end
    return t
end

function math_randomEx(m,n,cnt) 
    local tmp=table_fillNum(m,n)
    if cnt>n-m+1 then
        return tmp
    end
    local x=0
    local t={}math.randomseed(os.time())
    while  cnt>0 do
       x=math.random(1,n-m+1) 
       table.insert(t,tmp[x])
       table.remove(tmp,x)
       cnt=cnt-1
       m=m+1
      
    end
    return t
end
