wx020190_g_scriptId=020190

x020190_g_SpecialInitSlowTime = 10	--特殊怪延迟初始化时间
x020190_g_LittleMonsterMax = 25		--普通怪最大数量
x020190_g_SpecialAliveTime = 15		--特殊怪存活时间
x020190_g_SpecialRefreshTime = 30	--特殊怪刷新时间
x020190_g_AdapteLvl = 46
x20190_g_BeginLvl = 55				--开始的等级，功能开放等级/10
x020190_g_JieJieBuffId = 36000002	--禁咒结界buff
x020190_g_ActivityBaseId = 83

x020190_g_MonsterAIPtrolPath = {}
x020190_g_MonsterAIPtrolPath[1] = 50001
x020190_g_MonsterAIPtrolPath[2] = 50008
x020190_g_MonsterAIPtrolPath[3] = 50015

--小怪,精英怪,特殊怪,召唤的精英怪
x020190_g_BossroleId = {}
x020190_g_BossroleId[1] = {500201,500202,500203,500204,500205,500206,500207,500208,500209,500210,500211,500212,500213,
							500214,500215,500216,500217,500218,500219,500220,500221,500222,500223,500224,500225,500226,
							500227,500228,500229,500230,500231,500232,500233,500234,500235,500236,500237,500238,500239,
							500240,500241,500242,500243,500244,500245,500246}

x020190_g_BossroleId[2] = {500251,500252,500253,500254,500255,500256,500257,500258,500259,500260,500261,500262,500263,
							500264,500265,500266,500267,500268,500269,500270,500271,500272,500273,500274,500275,500276,
							500277,500278,500279,500280,500281,500282,500283,500284,500285,500286,500287,500288,500289,
							500290,500291,500292,500293,500294,500295,500296}

x020190_g_BossroleId[3] = {500301,500302,500303,500304,500305,500306,500307,500308,500309,500310,500311,500312,500313,
							500314,500315,500316,500317,500318,500319,500320,500321,500322,500323,500324,500325,500326,
							500327,500328,500329,500330,500331,500332,500333,500334,500335,500336,500337,500338,500339,
							500340,500341,500342,500343,500344,500345,500346}

x020190_g_BossroleId[4] = {500351,500352,500353,500354,500355,500356,500357,500358,500359,500360,500361,500362,500363,
							500364,500365,500366,500367,500368,500369,500370,500371,500372,500373,500374,500375,500376,
							500377,500378,500379,500380,500381,500382,500383,500384,500385,500386,500387,500388,500389,
							500390,500391,500392,500393,500394,500395,500396}

x020190_g_MonsterBornPosList={}
x020190_g_MonsterBornPosList[1]={{54.56,54.48,90},{53.7,43.59,90},{53.7,65.44,90},{52.2,49.78,90},{52.2,59.24,90},{55.64,46.99,90},
							{55.64,61.93,90},{59.54,41.95,90},{59.54,66.98,90},{59.77,47.28,90},{59.77,61.52,90},{62.86,50.59,90},
							{62.86,58.31,90},{64.33,42.47066,90},{64.33,66.75283,90},{65.94,47.97,90},{65.94,60.99,90},{66.9,51.8,90},
							{66.9,57.02,90},{68.87,43.91827,90},{68.87,65.27164,90},{70.43,51.89808,90},{70.43,56.98,90},{71.4,48.13,90},{71.4,60.78,90}}
x020190_g_MonsterBornPosList[2]={{59.18,54.48,90}}
x020190_g_MonsterBornPosList[3]={{48.75,54.48,90}}

x020190_g_DropId = {}
x020190_g_DropId[1] = {222041, 222042, 0, 222043}

x020067_g_BuffPos = {} -- Buff生成点
x020067_g_BuffPos[1] = {{61,54.5,46.66},{61,54.5,46.66},{61,54.5,46.66}}
x020067_g_BuffId = {} -- GroupPointId
x020067_g_BuffId[1] = {1102,1103,1104}

x020190_g_CSParamIdInitFlag = 1				   		   --初始化记录
x020190_g_CSParamIdLittleKillNum = 2				   --普通怪击杀数量
x020190_g_CSParamIdMiddleKillNum = 3				   --精英怪击杀数量
x020190_g_CSParamIdSpecialKillNum = 4				   --特殊怪击杀数量

x020190_g_CSParamIdSpecialRefreshTick = 5			   --特殊怪刷新开始时间
x020190_g_CSParamIdSpecialBornTick = 6				   --特殊怪出生时间
x020190_g_CSParamIdLittleDieTick = 10				   --普通怪死亡时刻

function x020072_OnObjEnter(scene,objId)
	--AutoCombat(scene,objId,true)
end

function x020190_OnCopySceneOpen(scene)
	local nlv = x020190_GetAdaptIndex(scene)
	
	SetCopySceneParam(scene, x020190_g_CSParamIdInitFlag, 0)
	SetCopySceneParam(scene, x020190_g_CSParamIdSpecialRefreshTick, 0)
	SetCopySceneParam(scene, x020190_g_CSParamIdLittleKillNum, 0)
	SetCopySceneParam(scene, x020190_g_CSParamIdMiddleKillNum, 0)
	SetCopySceneParam(scene, x020190_g_CSParamIdSpecialKillNum, 0)
	SetCopySceneParam(scene, x020190_g_CSParamIdSpecialBornTick, -1)
	SetCopySceneParam(scene, 0, 0)
	x020190_InitMonster(scene,nlv)
end

function x020190_GetAdaptIndex(scene)
	local nlv = math.floor(GetAdaptLvl(scene)/10)
	nlv = nlv - x20190_g_BeginLvl + 1

	if nlv > x020190_g_AdapteLvl then
		nlv = x020190_g_AdapteLvl
	end

	if nlv < 0 then
		nlv = 0
	end
	return nlv
end

--副本Tick
function x020190_OnCopySceneTick(scene,tickCount)
	local nlv = x020190_GetAdaptIndex(scene)
	local numlittle = GetAliveMonsterCount(scene, x020190_g_BossroleId[1][nlv])
	local numSpecial = GetAliveMonsterCount(scene, x020190_g_BossroleId[3][nlv])
	local numCallMiddle = GetAliveMonsterCount(scene, x020190_g_BossroleId[4][nlv])
	local nInitFlag = GetCopySceneParam(scene,x020190_g_CSParamIdInitFlag)

	if nInitFlag == 2 then
		--print("=================end=================")
		return
	end

	--普通怪
	local nLittleDieTick = GetCopySceneParam(scene,x020190_g_CSParamIdLittleDieTick)
	if numlittle < x020190_g_LittleMonsterMax and nLittleDieTick + 1 == tickCount then
		for k=numlittle+1,x020190_g_LittleMonsterMax do 
			local randomnum = RandomMon(1,#x020190_g_MonsterBornPosList[1])
			local nMonObjId = CreateMonster(scene,  x020190_g_BossroleId[1][nlv], x020190_g_MonsterBornPosList[1][randomnum][3], x020190_g_MonsterBornPosList[1][randomnum][1],x020190_g_MonsterBornPosList[1][randomnum][2])
			SetNpcCampByObjId(scene, nMonObjId, 3)
			--print("createMonster普通怪刷新" .. nMonObjId)
		end
	end

	--特殊怪初始化
	if nInitFlag == 0 then
		if x020190_g_SpecialInitSlowTime == tickCount and numSpecial < 1 then
			local MonsterObjId = CreateMonster(scene,  x020190_g_BossroleId[3][nlv], x020190_g_MonsterBornPosList[3][1][3], x020190_g_MonsterBornPosList[3][1][1],x020190_g_MonsterBornPosList[3][1][2])
			SetNpcCampByObjId(scene, MonsterObjId, 3)
			--print("createMonster特殊怪初始化" .. MonsterObjId .. "tickCount:"..tickCount)
			SetCopySceneParam(scene,x020190_g_CSParamIdSpecialBornTick,tickCount)
			SetCopySceneParam(scene,x020190_g_CSParamIdSpecialRefreshTick,tickCount)
			NpcStartPatrol(scene,MonsterObjId,x020190_g_MonsterAIPtrolPath[1])
			SetCopySceneParam(scene,x020190_g_CSParamIdInitFlag,1)
			local nUserCount = GetSceneUserCount(scene)
			for i=0,nUserCount-1 do
				local nUserObjId = GetUserObjIdByIdx(scene,i)
				XuLingXianZhenBossEnter(scene, nUserObjId,x020190_g_SpecialAliveTime)
				--print("XuLingXianZhenBossEnter發送" .. nUserObjId)
			end
		end
	end

	--特殊怪刷新
	local nSpKillNum = GetCopySceneParam(scene,x020190_g_CSParamIdSpecialKillNum)
	local nSpRefreshTick = GetCopySceneParam(scene,x020190_g_CSParamIdSpecialRefreshTick)
	numSpecial = GetAliveMonsterCount(scene, x020190_g_BossroleId[3][nlv])
	if nInitFlag == 1 and numCallMiddle < 1 and numSpecial < 1 and nSpRefreshTick + x020190_g_SpecialRefreshTime == tickCount then
		local MonsterObjId = CreateMonster(scene,  x020190_g_BossroleId[3][nlv], x020190_g_MonsterBornPosList[3][1][3], x020190_g_MonsterBornPosList[3][1][1],x020190_g_MonsterBornPosList[3][1][2])
		SetNpcCampByObjId(scene, MonsterObjId, 3)
		SetCopySceneParam(scene,x020190_g_CSParamIdSpecialRefreshTick,tickCount)
		--print("createMonster特殊怪刷新" .. MonsterObjId .. "tickCount:"..tickCount)
		SetCopySceneParam(scene,x020190_g_CSParamIdSpecialBornTick,tickCount)
		NpcStartPatrol(scene,MonsterObjId,x020190_g_MonsterAIPtrolPath[nSpKillNum%3+1])
		local nUserCount = GetSceneUserCount(scene)
		for i=0,nUserCount-1 do
			local nUserObjId = GetUserObjIdByIdx(scene,i)
			XuLingXianZhenBossEnter(scene, nUserObjId,x020190_g_SpecialAliveTime)
			--print("XuLingXianZhenBossEnter發送" .. nUserObjId)
		end
	end

	--特殊怪生命周期判断
	local nBornTick = GetCopySceneParam(scene,x020190_g_CSParamIdSpecialBornTick)
	if nBornTick > 0 and numSpecial > 0 and nBornTick + x020190_g_SpecialAliveTime == tickCount then
		DeleMonsterByRoleId(scene, x020190_g_BossroleId[3][nlv])
		--print("DeleMonsterByRoleId特殊怪自動消失" .. x020190_g_BossroleId[3][nlv] .. "tickCount:" .. tickCount)
		local nUserCount = GetSceneUserCount(scene)
		for i=0,nUserCount-1 do
			local nUserObjId = GetUserObjIdByIdx(scene,i)
			SendImpactToUnit(scene,nUserObjId,x020190_g_JieJieBuffId)
		end
	end
end

function x020190_InitMonster(scene, nlv)
	for i = 1,x020190_g_LittleMonsterMax do
		local randomnum = RandomMon(1,#x020190_g_MonsterBornPosList[1])
		local nMonObjId = CreateMonster(scene,  x020190_g_BossroleId[1][nlv], x020190_g_MonsterBornPosList[1][randomnum][3], x020190_g_MonsterBornPosList[1][randomnum][1],x020190_g_MonsterBornPosList[1][randomnum][2])
		SetNpcCampByObjId(scene, nMonObjId, 3)
		--print("createMonster普通怪初始化" .. nMonObjId .. ",i=" .. i)
	end

	local ObjId = CreateMonster(scene,  x020190_g_BossroleId[2][nlv], x020190_g_MonsterBornPosList[2][1][3], x020190_g_MonsterBornPosList[2][1][1],x020190_g_MonsterBornPosList[2][1][2])
	SetNpcCampByObjId(scene, ObjId, 3)
	--print("createMonster精英怪初始化" .. ObjId)


end

function x020190_OnCopySceneTimeOut(scene)
	SetCopySceneParam(scene,x020190_g_CSParamIdInitFlag,2)
	x020190_Success(scene)
end

--结算
function x020190_Success(scene)
	AccoutByCopyScene(scene)
	DeleHostileMonster(scene)
	DeleAliveNpcFriendlyMonster(scene)
	XuLingXianZhenEnd(scene,true)
	SetCopySceneSuccess(scene)
	--print("x020190_Success結束")
end

function  x020190_OnCopySceneQuit(scene)
	local nInitFlag = GetCopySceneParam(scene,x020190_g_CSParamIdInitFlag)
	if nInitFlag == 2 then
		return
	end

	SetCopySceneParam(scene,x020190_g_CSParamIdInitFlag,2)
	AccoutByCopyScene(scene)
	DeleHostileMonster(scene)
	DeleAliveNpcFriendlyMonster(scene)
	XuLingXianZhenEnd(scene,false)
	SetCopySceneFail(scene)
	--print("OnCopySceneQuit失敗結束")
end

--死亡回调
function  x020190_OnObjDie(scene,objId,rolebaseId,killerId)			
	local nlv = x020190_GetAdaptIndex(scene)

	local tickCount = GetCopySceneParam(scene,9)

	if rolebaseId == x020190_g_BossroleId[1][nlv] then
		local littleCur = GetCopySceneParam(scene, x020190_g_CSParamIdLittleKillNum)
		SetCopySceneParam(scene, x020190_g_CSParamIdLittleKillNum, littleCur+1)	
		SetCopySceneParam(scene,x020190_g_CSParamIdLittleDieTick,tickCount)
	elseif rolebaseId == x020190_g_BossroleId[2][nlv] then
		local MiddleCur = GetCopySceneParam(scene, x020190_g_CSParamIdMiddleKillNum)
		SetCopySceneParam(scene, x020190_g_CSParamIdMiddleKillNum, MiddleCur+1)	
	elseif rolebaseId == x020190_g_BossroleId[3][nlv] then
		x020190_WhenSpecialMonsterDead(scene,rolebaseId,nlv)
	elseif rolebaseId == x020190_g_BossroleId[4][nlv] then
		SetCopySceneParam(scene, x020190_g_CSParamIdSpecialRefreshTick, tickCount)
		local MiddleCur = GetCopySceneParam(scene, x020190_g_CSParamIdMiddleKillNum)
		SetCopySceneParam(scene, x020190_g_CSParamIdMiddleKillNum, MiddleCur+1)	
	end

	XiLianFuBenSameExp(scene, rolebaseId)
	local nUserCount = GetSceneUserCount(scene)
	for i=0,nUserCount-1 do
		local nUserObjId = GetUserObjIdByIdx(scene,i)
		SendXuLingXianZhenInfo(scene, nUserObjId)
		--print("SendXuLingXianZhenInfo發送統計信息" .. "little:"..GetCopySceneParam(scene,2)..",midd:"..GetCopySceneParam(scene,3))
	end
end



function x020190_WhenSpecialMonsterDead(scene,rolebaseId,nlv)
	--print("WhenSpecialMonsterDead特殊怪死亡")
	x020190_GiveMonsterDieAward(scene)
	x020190_ClearAllMonster(scene)

	local nKillSpecialNum = GetCopySceneParam(scene, x020190_g_CSParamIdSpecialKillNum)
	SetCopySceneParam(scene, x020190_g_CSParamIdSpecialKillNum, nKillSpecialNum+1)	

	-- 生成Buff点
	local countBuffPos = #x020067_g_BuffPos[1]
	if countBuffPos > 0 then
		local idx = nKillSpecialNum%countBuffPos + 1
		--print("CreateGroupPoint生成采集點" .. idx)
		CreateGroupPoint(scene,x020067_g_BuffId[1][idx],x020067_g_BuffPos[1][idx][1],x020067_g_BuffPos[1][idx][2],x020067_g_BuffPos[1][idx][3])
	end

	local nUserCount = GetSceneUserCount(scene)
	for i=0,nUserCount-1 do
		local nUserObjId = GetUserObjIdByIdx(scene,i)
		XuLingXianZhenBossDead(scene, nUserObjId, rolebaseId)
		--print("XuLingXianZhenBossDead給客戶端發送特殊怪死亡")
	end
	
	--召唤精英怪
	local tbDeadPos = GetObjPositionByRBID(scene,rolebaseId)
	local numCallMiddle = GetAliveMonsterCount(scene, x020190_g_BossroleId[4][nlv])
	if numCallMiddle < 1 then
		local ObjId = CreateMonster(scene,  x020190_g_BossroleId[4][nlv], x020190_g_MonsterBornPosList[2][1][3], tbDeadPos[0],tbDeadPos[1])
		--print("createMonster召喚精英怪" .. ObjId)
	end

end

function x020190_ClearAllMonster(scene)
	--print("x020190_ClearAllMonster")
	local nlv = x020190_GetAdaptIndex(scene)

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

	for monsterType = 1, 4 do
    	local nNum = DeleMonsterByRoleId(scene, x020190_g_BossroleId[monsterType][nlv])
		if nNum > 0 then
			if monsterType==1 then
				local littleCur = GetCopySceneParam(scene, x020190_g_CSParamIdLittleKillNum)
				SetCopySceneParam(scene, x020190_g_CSParamIdLittleKillNum, littleCur+nNum)	
				SetCopySceneParam(scene,x020190_g_CSParamIdLittleDieTick,GetCopySceneParam(scene,9))
			elseif monsterType==2 then
				local MiddleCur = GetCopySceneParam(scene, x020190_g_CSParamIdMiddleKillNum)
				SetCopySceneParam(scene, x020190_g_CSParamIdMiddleKillNum, MiddleCur+nNum)	
			elseif monsterType==4 then
				SetCopySceneParam(scene, x020190_g_CSParamIdSpecialRefreshTick, GetCopySceneParam(scene,9))
				local MiddleCur = GetCopySceneParam(scene, x020190_g_CSParamIdMiddleKillNum)
				SetCopySceneParam(scene, x020190_g_CSParamIdMiddleKillNum, MiddleCur+nNum)	
			end

			XuLingXianZhenRecordExp(scene, nUserObjId, x020190_g_BossroleId[monsterType][nlv], nNum)
		end
		--print("DeleMonsterByRoleId刪除怪物" .. x020190_g_BossroleId[monsterType][nlv] .. "	个数:" .. nNum)
    end
end

function x020190_GiveMonsterDieAward(scene)
	local nlv = x020190_GetAdaptIndex(scene)

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

	for k = 1, 4 do
		local objId = GetObjIdByRoleBase(scene, x020190_g_BossroleId[k][nlv])
		local nMonsterNum = GetAliveMonsterCount(scene, x020190_g_BossroleId[k][nlv])
		if x020190_g_DropId[1][k] > 0 and nMonsterNum > 0 and nUserObjId~=nil and objId > 0 then
			for j=1,nMonsterNum do
				CreatDropBoxNear(scene, nUserObjId, x020190_g_DropId[1][k], objId)
			end
			--print("CreatDropBox給獎勵" .. x020190_g_DropId[1][k] .. "	个数:" .. nMonsterNum)
		end
	end
	
end