wx020030_g_scriptId=020030

x020030_g_IdPuTong={}
x020030_g_IdPuTong[1]={{140001,140002,140003,140004,140005,140006,140007,140008,140009,140010,140011,140012,140013,140014,140015,140016,140017,140018,140019,140020},
{140001,140002,140003,140004,140005,140006,140007,140008,140009,140010,140011,140012,140013,140014,140015,140016,140017,140018,140019,140020}}
x020030_g_IdPuTong[2]={{141001,141002,141003,141004,141005,141006,141007,141008,141009,141010,141011,141012,141013,141014,141015,141016,141017,141018,141019,141020},
{141001,141002,141003,141004,141005,141006,141007,141008,141009,141010,141011,141012,141013,141014,141015,141016,141017,141018,141019,141020}}
x020030_g_IdPuTong[3]={{141001,141002,141003,141004,141005,141006,141007,141008,141009,141010,141011,141012,141013,141014,141015,141016,141017,141018,141019,141020},
{141001,141002,141003,141004,141005,141006,141007,141008,141009,141010,141011,141012,141013,141014,141015,141016,141017,141018,141019,141020}}
x020030_g_IdPuTong[4]={{141001,141002,141003,141004,141005,141006,141007,141008,141009,141010,141011,141012,141013,141014,141015,141016,141017,141018,141019,141020},
{141001,141002,141003,141004,141005,141006,141007,141008,141009,141010,141011,141012,141013,141014,141015,141016,141017,141018,141019,141020}}
x020030_g_IdPuTong[6]={{142001,142002,142003,142004,142005,142006,142007,142008,142009,142010,142011,142012,142013,142014,142015,142016,142017,142018,142019,142020},
{142001,142002,142003,142004,142005,142006,142007,142008,142009,142010,142011,142012,142013,142014,142015,142016,142017,142018,142019,142020}}
x020030_g_AdapteLvl = 20  --   每个怪物的自适应等级套数  必须随适应等级的变化而改变
--以上为波次刷怪的波次适应等级  编号1代表第一波怪的自适应等级    编号1里第一个中括号表示第一波第一个怪物的自适应等级套数  依次类推

x020030_g_BossId = {}
x020030_g_BossId[1] = {144001,144002,144003,144004,144005,144006,144007,144008,144009,144010,144011,144012,144013,144014,144015,144016,144017,144018,144019,144020}	-- Boss编号   表示Boss的自适应等级套数   
x020030_g_BossPosList={}
x020030_g_BossPosList[1]={{125,187.1,4.71}}    --  Boss 坐标

x020030_g_MonsterPosList={}     
x020030_g_MonsterPosList[1]={{106,85,3.21},{106,80,3.21}}		
x020030_g_MonsterPosList[2]={{176.3,103.2,-0.58},{181.3,95.6,0.89}}		--  小怪坐标
x020030_g_MonsterPosList[3]={{180.8,71.3,4.71},{176.1,64.2,1.57}}
x020030_g_MonsterPosList[4]={{198.6,87.7,4.71},{198.2,78.7,1.57}}

x020030_g_MonsterPosList[6]={{122.6,154,4.71},{127,154,4.71}}	   

x020030_g_TrapPosList={} 
x020030_g_TrapPosList[1]= {{363.5, 199.3}, {360.5, 195,3},{363.5, 199.3}, {360.5, 195,3},{363.5, 199.3}, {360.5, 195,3},{363.5, 199.3}, {360.5, 195,3}, {365.2,208.2}, {373, 200}, {361.36,192}}  -- 陷阱随机坐标

x020030_g_ResurrectionPosList = {}
x020030_g_ResurrectionPosList[1]={{88,74,0}}                      -- 复活点坐标


x020030_g_CSParamIdxCurStage=0                     -- 场景切换波数
x020030_g_CSParamIdxCurSubStage=1				   -- 同一场景中的时间点		
x020030_g_CSParamIdxrandtime=2					   -- 20血以下是否释放		
x020030_g_CSParamIdxcurNum = 3					   -- 40血以下是否释放
x020030_g_CSParamIdxBossId = 4 					   -- 60血	
x020030_g_CSParamTPobjId = 5                       -- 80血
x020030_g_CSParamTipsTime = 6  					   -- 系统提示时间戳


x020030_g_User_ParamIdOfOne = 1                   -- 三环本第一环钥匙
x020030_g_User_ParamIdOfTwo = 2 				  -- 三环本第二换钥匙

 x020030_g_PuppetId = {}
 x020030_g_PuppetId[1] = {143001,143002,143003,143004,143005,143006,143007,143008,143009,143010,143011,143012,143013,143014,143015,143016,143017,143018,143019,143020}	--  傀儡Id
x020030_g_PuppetNum = 3     --   每侧几个   不是自适应等级数量不用更改		  
x020030_g_PuppetPosList={}    --  随机坐标   注意写两遍  前面和后面是一样的  延伸一次方便随机
x020030_g_PuppetPosList[1] = {{89, 189}, {85, 190}, {81,190}, {89, 189}, {85, 190}, {81,190}}     --   傀儡左侧随机坐标  
x020030_g_PuppetPosList[2] =  {{160, 190}, {164, 189}, {168,169}, {160, 190}, {164, 189}, {168,169}}    --   右侧随机坐标

x020030_g_TargetPosList = {}
x020030_g_TargetPosList[1] = {{124, 188, 3,14}}   --  傀儡目标坐标

x020030_g_BuffId = 20105

x020030_g_MissionId = 63031  --  起始任务Id
x020030_g_Tarpid = 1008    -- 陷阱ID
x020030_g_BossTipId = 50003    --  Boss喊话提示
x020030_g_BossFakeObjId  = 801  --  喊话的BOSS形象

x020030_g_TpId = 1031  -- 传送点ID
x020030_g_TpPos={}
x020030_g_TpPos[1]={182.9,83.6}   -- 传送点坐标

x020030_g_Tips={}
x020030_g_Tips[1]="#{50006}*"       --  召唤傀儡提示

x020030_g_IdCSItemBox = 10102   -- 奖品箱子ID
x020030_g_IdCSxiayiBox = 10112
x020030_g_TimeForReply = 30

function x020030_OnCopySceneOpen(scene)
	AddCSMission(scene,65031)
	AddCSMission(scene,65032)
	AddCSMission(scene,65033)
	AddCSMission(scene,65034)
	AddCSMission(scene,65035)
	AddCSMission(scene,65036)
	AddCSMission(scene,65037)
end

function x020030_OnCopySceneTick(scene,tickCount)					--副本阶段
	local curStage = GetCopySceneParam(scene,x020030_g_CSParamIdxCurStage) +1
	local curSubStage = GetCopySceneParam(scene,x020030_g_CSParamIdxCurSubStage) +1
	SetCopySceneParam(scene,x020030_g_CSParamIdxCurSubStage,curSubStage)

	local nAdaptedLvl = math.floor(GetAdaptLvl(scene)/5)
	if nAdaptedLvl > x020030_g_AdapteLvl then
		nAdaptedLvl = x020030_g_AdapteLvl
	end

	if tickCount%30 == 0   then
		local count = #x020030_g_TrapPosList[1]
		for i = 1, count do
			SetSceneBuff(scene, x020030_g_Tarpid, x020030_g_TrapPosList[1][i][1], x020030_g_TrapPosList[1][i][2], 0)
		end
	end
	if(curStage == 7) then
		x020030_CheckMonPos(scene, x020030_g_PuppetId[1][nAdaptedLvl], nAdaptedLvl)
	end
	local objid = GetObjIdByRoleBase(scene, x020030_g_BossId[1][nAdaptedLvl])
	local PercenHp = CalculatePercenHp(scene, objid)
	if(not MonIsBattle(scene, x020030_g_BossId[1][nAdaptedLvl]) and PercenHp > 0) then 
		for i = 2, 5 do
			SetCopySceneParam(scene, i, 0)
		end
		DeleMonsterByRoleId(scene, x020030_g_PuppetId[1][nAdaptedLvl])
		CancleImpact(scene, objid, x020030_g_BuffId)
	end
	if(GetCopySceneParam(scene, x020030_g_CSParamTipsTime) == tickCount and tickCount ~= 0 ) then
		local nUserCount = GetSceneUserCount(scene)
		for i=0,nUserCount-1 do
			local nUserObjId = GetUserObjIdByIdx(scene,i)
			SetCopySceneParam(scene, x020030_g_CSParamTipsTime, 0)
		end
		SendAllPlayerServerTips(scene, x020030_g_Tips[1])
	end
 	if PercenHp <= 80 and  PercenHp ~= 0 and  PercenHp ~= -1 then
		if GetCopySceneParam(scene, math.ceil(PercenHp/20)+1) ~= 1 then
			local nUserCount = GetSceneUserCount(scene)
			for i=0,nUserCount-1 do                                                                                        --  神傀儡刷新
				local nUserObjId = GetUserObjIdByIdx(scene,i)
				ShowBossTips(scene, nUserObjId, x020030_g_BossFakeObjId, x020030_g_BossTipId, x020030_g_BossId[1][nAdaptedLvl])
				SetCopySceneParam(scene, x020030_g_CSParamTipsTime, tickCount+6)
			end
			SetCopySceneParam(scene, math.ceil(PercenHp/20)+1, 1)
			local Posnum  = #x020030_g_PuppetPosList[1]
			local rand = RandomMon(1, Posnum/2)
			for i = rand+1, rand+x020030_g_PuppetNum  do
				if(rand % 2 == 0) then
					local PuppetId = CreateMonster(scene, x020030_g_PuppetId[1][nAdaptedLvl], 3.14, x020030_g_PuppetPosList[1][i][1], x020030_g_PuppetPosList[1][i][2])
			
					SetNpcCampByObjId(scene, PuppetId, 3)
					CommandMoveTo(scene, PuppetId, x020030_g_TargetPosList[1][1][1], x020030_g_TargetPosList[1][1][2])

				elseif(rand % 2 == 1) then
					local PuppetId2 = CreateMonster(scene, x020030_g_PuppetId[1][nAdaptedLvl], 3.14, x020030_g_PuppetPosList[2][i][1], x020030_g_PuppetPosList[2][i][2])
					SetNpcCampByObjId(scene, PuppetId2, 3)
					CommandMoveTo(scene, PuppetId2, x020030_g_TargetPosList[1][1][1], x020030_g_TargetPosList[1][1][2])
				end
			end
		end
	end

	if curStage == 1 then
		x020030_Stage_1(scene,curStage,curSubStage,nAdaptedLvl)
	elseif curStage == 2 then
		x020030_Stage_2(scene,curStage,curSubStage, nAdaptedLvl)
	elseif curStage == 3 then
		x020030_Stage_2(scene,curStage,curSubStage, nAdaptedLvl)
	elseif curStage == 4 then
		x020030_Stage_2(scene,curStage,curSubStage, nAdaptedLvl)
	elseif curStage == 5 then
		x020030_Stage_5(scene,curStage,curSubStage, nAdaptedLvl)
	elseif curStage == 6 then
		x020030_Stage_6(scene,curStage,curSubStage, nAdaptedLvl)
	elseif curStage == 7 then
		x020030_Stage_4(scene,curStage,curSubStage, nAdaptedLvl)	
	elseif curStage == 8 then

	end			
end

function x020030_OnTransmit(nScene, nObjId, nGroupId)
	local nCurStage = GetCopySceneParam(nScene,x020030_g_CSParamIdxCurStage)
	if nCurStage == 4 then
		SetCopySceneParam(nScene,x020030_g_CSParamIdxCurStage,nCurStage+1)
		SetCopySceneParam(nScene,x020030_g_CSParamIdxCurSubStage,0)
	end
end

function x020030_IsAce(scene, nlv)
	local flag = 0
	local nUserCount = GetSceneUserCount(scene)
	for i=0,nUserCount-1 do                                                                                        --  帮助奖励（当所有BOSS奖励触发后开始触发）
		local nUserObjId = GetUserObjIdByIdx(scene,i)
		if(IsAlive(scene, nUserObjId)) then
			flag = 1
		end
	end
	if(flag == 0)  then
		DeleMonsterByRoleId(scene, x020030_g_PuppetId[1][nlv])
	end
end

function x020030_AddMission(scene)
	local nUserCount = GetSceneUserCount(scene)
	for i=0,nUserCount-1 do
		local nUserObjId = GetUserObjIdByIdx(scene,i)
			AcceptMission(scene,nUserObjId,x020030_g_MissionId)
	end
end


function x020030_CheckMonPos(scene, rolebaseId, nlv)
	local CurMonId = GetAllObjByRBId(scene, rolebaseId)
	local eqs = 2
	local objid = GetObjIdByRoleBase(scene, x020030_g_BossId[1][nlv])
	local Count = GetCountByRBId(scene, rolebaseId)
	for i = 1, Count do
		if(CurMonId[i] ~= nil) then
			local CurPoz = GetObjPosition(scene, CurMonId[i])
			if((math.abs(CurPoz[0] - x020030_g_TargetPosList[1][1][1]) < eqs) and (math.abs(CurPoz[1] -  x020030_g_TargetPosList[1][1][2]) < eqs) and (not GetMonFlag(scene, CurMonId[i])))  then
				SetMonFlag(scene, CurMonId[i])
				SendImpactToUnit(scene, objid, x020030_g_BuffId)
				SendImpactToUnit(scene, CurMonId[i], x020030_g_BuffId)
			end
		end
	end 
end


function x020030_Success(scene)
	AccoutByCopyScene(scene)	
	local nUserCount = GetSceneUserCount(scene)
	for i=0,nUserCount-1 do
		local nUserObjId = GetUserObjIdByIdx(scene,i)
	--	AwardCSItemBox(scene,nUserObjId,x020030_g_IdCSxiayiBox, x020030_g_IdCSItemBox)
	end

	SetCopySceneSuccess(scene)
end


function  x020030_OnObjDie(scene,objId,rolebaseId,killerId)			
	local MonCount = GetHostileMonsterCount(scene)
	local nCurStage = GetCopySceneParam(scene, x020030_g_CSParamIdxCurStage)
	local nAdaptedLvl = math.floor(GetAdaptLvl(scene)/5)
	if nAdaptedLvl > x020030_g_AdapteLvl then
		nAdaptedLvl = x020030_g_AdapteLvl
	end
	x020030_IsAce(scene, nAdaptedLvl)
	if(MonCount == 0) then

		SetCopySceneParam(scene,x020030_g_CSParamIdxCurStage,nCurStage+1)
		SetCopySceneParam(scene,x020030_g_CSParamIdxCurSubStage,0)
		if(nCurStage == 6)    then															-- BOSS奖励次数限制
			local nUserCount = GetSceneUserCount(scene)
			for i=0,nUserCount-1 do                                                                                        --  帮助奖励（当所有BOSS奖励触发后开始触发）
					local nUserObjId = GetUserObjIdByIdx(scene,i)
					local Key2 = GetUserCsParm(scene, x020030_g_User_ParamIdOfTwo, nUserObjId)
					if Key2 ~= 0   then

						SetUserCsParm(scene, nUserObjId, x020030_g_User_ParamIdOfOne, Key2-1)
					end
			end
			x020030_Success(scene)
		end
	end
end


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


function x020030_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 x020030_Stage_1(scene ,stage,substage, nlv)                      
	if substage == 1 then
		OnCSMissionStage(scene,stage-1)
		x020030_CreateMonster(scene,stage,3,nlv)
	end
end


function x020030_Stage_2(scene ,stage,substage, nlv)                        
	if(substage == 1) then
		OnCSMissionStage(scene,stage-1)
		x020030_CreateMonster(scene,stage,3,nlv)
	end
end

function x020030_Stage_5(scene ,stage,substage,nlv)   
	if substage == 1 then
		OnCSMissionStage(scene,stage-1)
		SetSceneBuff(scene, x020030_g_TpId, x020030_g_TpPos[1][1], x020030_g_TpPos[1][2], 0)	
	end
end

function x020030_Stage_6(scene ,stage,substage,nlv)   
	if substage == 1 then
		OnCSMissionStage(scene,stage-1)
		x020030_CreateMonster(scene,stage,3,nlv)	
	end
end

--传送门
function x020030_Stage_3(scene ,stage,substage,nlv)                      
	if substage == 1 then
		OnCSMissionStage(scene,stage-1)
		x020030_CreateMonster(scene,stage,3,nlv)	
	elseif substage == 2 then 
		SetSceneBuff(scene, x020030_g_TpId, x020030_g_TpPos[1][1], x020030_g_TpPos[1][2], 0)	
	end
end

--BOSS
function x020030_Stage_4(scene ,stage,substage,nlv)						
	if substage == 1 then
		OnCSMissionStage(scene,stage-1)
		CreateMonster(scene, x020030_g_BossId[1][nlv], x020030_g_BossPosList[1][1][3], x020030_g_BossPosList[1][1][1],x020030_g_BossPosList[1][1][2])
	end
end


function x020030_OnObjresurrectionEnd(scene ,objId)
		for i = 2, 5 do
			SetCopySceneParam(scene, i, 0)
		end
end