x020188_g_scriptId=020188


x020188_g_CSUserBuffId = 77301

x020188_g_CSParamIdxCurFloor=0
x020188_g_CSParamIdxCurSubStage=1
x020188_g_CSEndFlag = 2
x020188_g_CSSettlementFlag = 4 -- 结算标识
x020188_g_CSShopMark = 5 -- 商店记录标识

function x020188_OnCopySceneOpen(scene)
	SetCopySceneParam(scene, x020188_g_CSEndFlag, 0)
	SetCopySceneParam(scene, x020188_g_CSSettlementFlag, 0)
	SetCopySceneParam(scene, x020188_g_CSShopMark, 0)
	local nUserCount = GetSceneUserCount(scene)
	for j=0,nUserCount-1 do
		local nUserObjId = GetUserObjIdByIdx(scene,j)
		if IsAlive(scene,nUserObjId) == false then
			TestFullHp(scene, nUserObjId)
		end
		SetHpFull(scene, nUserObjId)
	end
end

function x020188_OnCopySceneTick(scene,tickCount)
	--副本阶段
	local nUserCount = GetSceneUserCount(scene)
	local nEndMark = GetCopySceneParam(scene, x020188_g_CSEndFlag)
	local nSettlementFlag = GetCopySceneParam(scene, x020188_g_CSSettlementFlag)
	if tickCount == 0 then
		x020188_StageControl(scene, 0)
		for i=0,nUserCount-1 do
			local nUserObjId = GetUserObjIdByIdx(scene,i)
			CreateMonByZhanJiaScene(scene, 0, nUserObjId)
		end
	end
	if tickCount == 5 then
		for i=0,nUserCount-1 do
			local nUserObjId = GetUserObjIdByIdx(scene,i)
			PushZhanJiaShop(scene, nUserObjId)
		end
	end
	if nEndMark == 1 and nSettlementFlag == 0 then
		SetCopySceneParam(scene, x020188_g_CSSettlementFlag, 1)
		for i=0,nUserCount-1 do
				local nUserObjId = GetUserObjIdByIdx(scene,i)
			if GetCSEvaluaion(scene) ~= 0 then		
				x020188_OnSuccess(scene, nUserObjId, false)
			elseif GetCSEvaluaion(scene) == 0 then
				x020188_OnSuccess(scene, nUserObjId, true)
			end
		end
	end
	for j=0,nUserCount-1 do
		local nUserObjId = GetUserObjIdByIdx(scene,j)
		local FlushTime = GetFlushTime(scene, nUserObjId)
		if(FlushTime > 0) then
			x020188_CreateMonster(scene, FlushTime, tickCount)
		end		
	end

end

function x020188_OnObjEnter(scene,objId)
	TellPlayerAutoCombat(scene)
	SynCsStageData(scene, objId, true)
	SendImpactToUnit(scene, objId , x020188_g_CSUserBuffId)
end

function  x020188_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 x020188_OnObjDie( scene,objId,rolebaseId,killerId )
	local nUserCount = GetSceneUserCount(scene)
	local CurStage = GetCopySceneParam(scene, x020188_g_CSParamIdxCurSubStage)
	if GetHostileMonsterCount(scene) == 0 then
		for i=0,nUserCount-1 do
			local nUserObjId = GetUserObjIdByIdx(scene,i)
			local bEnd = CheckZhanJiaCSEnd(scene, CurStage + 1, nUserObjId)
			if (bEnd == false) then
				x020188_StageControl(scene, CurStage+1)
				SetCopySceneParam(scene, x020188_g_CSParamIdxCurSubStage, CurStage+1)
				CreateMonByZhanJiaScene(scene, CurStage+1, nUserObjId)
				return
			end
		end
		SetCopySceneParam(scene, x020188_g_CSEndFlag, 1)
	end
end

function x020188_OnSuccess(scene, objId, IsPec)
	ZhanJiaSuccess(scene, objId, IsPec)
	SetCopySceneParam(scene, x020188_g_CSEndFlag, 1)
end


function x020188_CreateMonster(scene, FlushTime, tickCount)
	local CurStage = GetCopySceneParam(scene, x020188_g_CSParamIdxCurSubStage)
	local nUserCount = GetSceneUserCount(scene)
	if (tickCount % (FlushTime / 1000) == 0)  and (tickCount > 0) then
		for i=0,nUserCount-1 do
			local nUserObjId = GetUserObjIdByIdx(scene,i)
			local bEnd = CheckZhanJiaCSEnd(scene, CurStage + 1, nUserObjId)
			if bEnd == false then
				x020188_StageControl(scene, CurStage+1)
				SetCopySceneParam(scene, x020188_g_CSParamIdxCurSubStage, CurStage+1)
				CreateMonByZhanJiaScene(scene, CurStage+1, nUserObjId)
				return
			elseif bEnd == true and (GetHostileMonsterCount(scene) == 0) then
				for j=0,nUserCount-1 do
					local nUserObjId = GetUserObjIdByIdx(scene,j)
					if GetCSEvaluaion(scene) ~= 0 then
						x020188_OnSuccess(scene, nUserObjId, true)
					elseif GetCSEvaluaion(scene) == 0 then
						x020188_OnSuccess(scene, nUserObjId, true)
					SetCopySceneSuccess(scene)
					end
				end
			end
		end
	end
end


function x020188_OnCopySceneTimeOut(scene)
	local nUserCount = GetSceneUserCount(scene)
	SetCopySceneParam(scene, x020188_g_CSSettlementFlag, 1)
	for j=0,nUserCount-1 do
		local nUserObjId = GetUserObjIdByIdx(scene,j)
		x020188_OnSuccess(scene, nUserObjId, false)
	end
end

function x020188_OnPrepareLeaveCopyScene(scene)
	local nUserCount = GetSceneUserCount(scene)
	for j=0,nUserCount-1 do
		local nUserObjId = GetUserObjIdByIdx(scene,j)
		AutoReChallengeJuLing(scene, nUserObjId)
	end
end

