--setMissionParam
-- 0采集任务阶段==0 采集 1 杀怪
-- 1杀怪计数;
-- 2,3杀怪，传说，宝图任务时间;
-- 4随机地点任务 场景id*1000*1000+坐标x*1000+坐标z
-- 5过期监听
-- 6失败监听，
-- 7失败达成
-- 8攻击次数
-- 9,10 任务接取时间

-- 脚本任务数据
-- 11 类型 用来确定寻路后的回调（100对话 101送信 102杀怪，103采集，104杀怪掉落） 
--12目标id
--13目标个数
--14完成目标id（-1自动完成）
--15额外数据1
--16额外数据2

--setMissionState
-- Mission_Invalid = 0,  
-- Mission_Accepted = 1,  任务进行中
-- Mission_Completed = 2, 任务可交
-- Mission_Failed = 3,    任务失败

--SetMissionCaredEvent
-- Event_Kill_Object  =0  杀怪
-- Event_EnterArea    =1  进入区域
-- Event_ItemChange   =2 
-- Event_LevelUp      =3 
-- Event_CollectItem  =4  采集
-- Event_UseItem      =5 
-- Event_Puzzle       =6 
-- Event_TeamKill     =7 
-- Event_TeamCollect  =8 
-- Event_CopyScene    =9 
-- Event_Guide        =10
-- Event_Attacks      =11
-- Event_Attribute    =12
-- Event_Round        =13

--捉鬼任务

--脚本号
x211103_g_scriptId=211103
--任务id
x211103_g_missionId=11103

x211103_g_RoundConfigId =11

x211103_g_SceneNpcLevels ={}
x211103_g_SceneNpcLevels[1]=50
x211103_g_SceneNpcLevels[2]=80
x211103_g_SceneNpcLevels[3]=140
x211103_g_SceneNpcLevels[4]=200
x211103_g_SceneNpcLevels[5]=250
x211103_g_SceneNpcLevels[6]=300
x211103_g_SceneNpcLevels[7]=350
x211103_g_SceneNpcLevels[8]=400
x211103_g_SceneNpcLevels[9]=450
x211103_g_SceneNpcLevels[10]=500
x211103_g_SceneNpcLevels[11]=550
x211103_g_SceneNpcLevels[12]=600
x211103_g_SceneNpcLevels[13]=650
x211103_g_SceneNpcLevels[14]=700
x211103_g_SceneNpcLevels[15]=750
x211103_g_SceneNpcLevels[16]=800
x211103_g_SceneNpcLevels[17]=850
x211103_g_SceneNpcLevels[18]=900
x211103_g_SceneNpcLevels[19]=950
x211103_g_SceneNpcLevels[20]=1000

--交任务的sceneNpc id
x211103_g_SceneNpcIds={}
x211103_g_SceneNpcIds[1]={-1}
x211103_g_SceneNpcIds[2]={-1}
x211103_g_SceneNpcIds[3]={-1}
x211103_g_SceneNpcIds[4]={-1}
x211103_g_SceneNpcIds[5]={-1}
x211103_g_SceneNpcIds[6]={-1}
x211103_g_SceneNpcIds[7]={-1}
x211103_g_SceneNpcIds[8]={-1}
x211103_g_SceneNpcIds[9]={-1}
x211103_g_SceneNpcIds[10]={-1}

x211103_g_MonsterLevels ={}
x211103_g_MonsterLevels[1]=50
x211103_g_MonsterLevels[2]=55
x211103_g_MonsterLevels[3]=60
x211103_g_MonsterLevels[4]=65
x211103_g_MonsterLevels[5]=70
x211103_g_MonsterLevels[6]=75
x211103_g_MonsterLevels[7]=80
x211103_g_MonsterLevels[8]=85
x211103_g_MonsterLevels[9]=89
x211103_g_MonsterLevels[10]=95
x211103_g_MonsterLevels[11]=100
x211103_g_MonsterLevels[12]=105
x211103_g_MonsterLevels[13]=110
x211103_g_MonsterLevels[14]=115
x211103_g_MonsterLevels[15]=120
x211103_g_MonsterLevels[16]=125
x211103_g_MonsterLevels[17]=130
x211103_g_MonsterLevels[18]=135
x211103_g_MonsterLevels[19]=140
x211103_g_MonsterLevels[20]=145
x211103_g_MonsterLevels[21]=149
x211103_g_MonsterLevels[22]=155
x211103_g_MonsterLevels[23]=160
x211103_g_MonsterLevels[24]=165
x211103_g_MonsterLevels[25]=170
x211103_g_MonsterLevels[26]=175
x211103_g_MonsterLevels[27]=180
x211103_g_MonsterLevels[28]=185
x211103_g_MonsterLevels[29]=190
x211103_g_MonsterLevels[30]=195
x211103_g_MonsterLevels[31]=200
x211103_g_MonsterLevels[32]=209
x211103_g_MonsterLevels[33]=220
x211103_g_MonsterLevels[34]=230
x211103_g_MonsterLevels[35]=240
x211103_g_MonsterLevels[36]=250
x211103_g_MonsterLevels[37]=260
x211103_g_MonsterLevels[38]=269
x211103_g_MonsterLevels[39]=280
x211103_g_MonsterLevels[40]=290
x211103_g_MonsterLevels[41]=300
x211103_g_MonsterLevels[42]=330
x211103_g_MonsterLevels[43]=380
x211103_g_MonsterLevels[44]=440
x211103_g_MonsterLevels[45]=500
x211103_g_MonsterLevels[46]=550
x211103_g_MonsterLevels[46]=600
x211103_g_MonsterLevels[46]=650
x211103_g_MonsterLevels[46]=700
x211103_g_MonsterLevels[46]=750
x211103_g_MonsterLevels[46]=800
x211103_g_MonsterLevels[46]=850
x211103_g_MonsterLevels[46]=900
x211103_g_MonsterLevels[46]=950
x211103_g_MonsterLevels[46]=1000

--杀的怪的sceneNpc id
x211103_g_MonsterIds={}
x211103_g_MonsterIds[1]={300801}
x211103_g_MonsterIds[2]={300801}
x211103_g_MonsterIds[3]={300901}
x211103_g_MonsterIds[4]={400101}
x211103_g_MonsterIds[5]={400101}
x211103_g_MonsterIds[6]={400201}
x211103_g_MonsterIds[7]={400301}
x211103_g_MonsterIds[8]={400501}
x211103_g_MonsterIds[9]={400501}
x211103_g_MonsterIds[10]={500101}
x211103_g_MonsterIds[11]={500201}
x211103_g_MonsterIds[12]={500301}
x211103_g_MonsterIds[13]={500401}
x211103_g_MonsterIds[14]={500501}
x211103_g_MonsterIds[15]={500601}
x211103_g_MonsterIds[16]={500701}
x211103_g_MonsterIds[17]={500801}
x211103_g_MonsterIds[18]={500901}
x211103_g_MonsterIds[19]={501001}
x211103_g_MonsterIds[20]={501001,501101}
x211103_g_MonsterIds[21]={501101}
x211103_g_MonsterIds[22]={600101}
x211103_g_MonsterIds[23]={600101}
x211103_g_MonsterIds[24]={600101,600201}
x211103_g_MonsterIds[25]={600201}
x211103_g_MonsterIds[26]={600201,600301}
x211103_g_MonsterIds[27]={600301}
x211103_g_MonsterIds[28]={600301,600401}
x211103_g_MonsterIds[29]={600401}
x211103_g_MonsterIds[30]={600401,600501}
x211103_g_MonsterIds[31]={600501}
x211103_g_MonsterIds[32]={600501,600601}
x211103_g_MonsterIds[33]={700101,700201}
x211103_g_MonsterIds[34]={700201,700401}
x211103_g_MonsterIds[35]={700401,700701}
x211103_g_MonsterIds[36]={700701,700801}
x211103_g_MonsterIds[37]={700801,700901}
x211103_g_MonsterIds[38]={700901}
x211103_g_MonsterIds[39]={800101,800201}
x211103_g_MonsterIds[40]={800201,800301}
x211103_g_MonsterIds[41]={800301,800401}
x211103_g_MonsterIds[42]={800401,800501,800601}
x211103_g_MonsterIds[43]={900101,900201,900301,900401,900501}
x211103_g_MonsterIds[44]={1000101,1000201,1000301,1000401,1000501,1000601}
x211103_g_MonsterIds[45]={1100101,1100201,1100301,1100401,1100501,1100601}
x211103_g_MonsterIds[46]={1200101,1200201,1200301,1200401,1200501}
x211103_g_MonsterIds[47]={1300101,1300201,1300301,1300401,1300501}
x211103_g_MonsterIds[48]={1400101,1400201,1400301,1400401,1400501}
x211103_g_MonsterIds[49]={1500101,1500201,1500301,1500401,1500501}
x211103_g_MonsterIds[50]={1600101,1600201,1600301,1600401,1600501}
x211103_g_MonsterIds[51]={1700101,1700201,1700301,1700401,1700501}
x211103_g_MonsterIds[52]={1800101,1800201,1800301,1800401,1800501}
x211103_g_MonsterIds[53]={1900101,1900201,1900301,1900401,1900501}
x211103_g_MonsterIds[54]={2000101,2000201,2000301,2000401,2000501}
x211103_g_MonsterIds[55]={2100101,2100201,2100301,2100401,2100501}


--- 任务函数 
--任务接受，已接受到任务id请求，通过接受验证，未执行添加逻辑
function x211103_OnMissionAccept( scene,selfId )
	-- body
	if AddMissionNN(scene,selfId,x211103_g_missionId) then  --添加任务，不同步
		local level = GetLevel(scene,selfId)
		local npcId=x211103_GetRandomSceneNpcId(level)
		local monsterId =x211103_GetRandomMonsterId(level)
		local count = 24
		local params = {{1,0},{11,102}, {12,monsterId},{13,count},{14, npcId}}
		
		SetMissionParams(scene,selfId,x211103_g_missionId,params) --设置客户端需要的脚本任务参数，不同步

		DispatchMission(scene, selfId, x211103_g_missionId, x211103_g_RoundConfigId)      --同步任务
		SetMissionState(scene, selfId, x211103_g_missionId, 1)	  --设置任务状态为任务进行中

		SetMissionCaredEvent(scene, selfId, x211103_g_missionId, 0, true)
		SetMissionCaredEvent(scene, selfId, x211103_g_missionId, 7, true)
	end
end

--检查任务是否可以完成
function x211103_CheckCompleted( scene,selfId )
	-- body
	return true
end

--任务完成，执行在删除任务之前，执行一些清理操作，比如清楚任务相关的道具
function x211103_OnMissionCompleted( scene,selfId )
	-- body
end

--放弃任务，同任务完成
function x211103_OnMissionAbandon( scene,selfId )
	-- body
end


--事件 激活事件监听需要调用SetMissionCareEvent
--杀怪事件监听
function x211103_OnKillMonster( scene,selfId,monsterId)
	-- body
	x211103_OnKill(scene, selfId, monsterId)
end

function x220001_OnTeamMemberKillMonster(scene, selfId, monsterId)
	-- body
	x211103_OnKill(scene, selfId, monsterId)
end

function  x211103_OnKill(scene,selfId,monsterId)
	-- body
	if GetMissionState(scene,selfId,x211103_g_missionId)~=2 then
	
		local sceneMonsterId = GetMissionParam(scene, selfId, x211103_g_missionId, 12)
		local countNeed = GetMissionParam(scene, selfId, x211103_g_missionId, 13)
		local monsterRolebaseId=GetRoleBaseIdBySceneNpcId(sceneMonsterId)
		if monsterId == monsterRolebaseId then
			local count = GetMissionParam(scene, selfId, x211103_g_missionId, 1) + 1      --(param[1] 保存杀怪数)
			SetMissionParam(scene, selfId, x211103_g_missionId, 1, count, true)   --任务计数+1
			
			if count >= countNeed then
				SetMissionState(scene, selfId, x211103_g_missionId, 2)         --设置任务状态为可交 Mission_Completed	
			end
		end
	end	
end



function x211103_GetRandomSceneNpcId(level)
			
	local levelIndex =1
	for i=1,#x211103_g_SceneNpcLevels-1 do
		if level>x211103_g_SceneNpcLevels[i] and level<=x211103_g_SceneNpcLevels[i+1] then
			levelIndex=i+1
		end
	end
	if levelIndex>#x211103_g_SceneNpcIds then 
		levelIndex=#x211103_g_SceneNpcIds
	end
	local sceneNpcIndex=math.random(#x211103_g_SceneNpcIds[levelIndex])
	
	--print(levelIndex, sceneNpcIndex,x211103_g_SceneNpcIds[levelIndex][sceneNpcIndex])
	return x211103_g_SceneNpcIds[levelIndex][sceneNpcIndex]
end	

function x211103_GetRandomMonsterId(level)
	local levelIndex =1
	for i=1,#x211103_g_MonsterLevels-1 do
		if level>x211103_g_MonsterLevels[i] and level<=x211103_g_MonsterLevels[i+1] then
			levelIndex=i+1
		end
	end
	if levelIndex>#x211103_g_MonsterIds then 
		levelIndex=#x211103_g_MonsterIds
	end
	local monsterIndex=math.random(#x211103_g_MonsterIds[levelIndex])
	
	--print(levelIndex, monsterIndex,x211103_g_MonsterIds[levelIndex][monsterIndex])
	return x211103_g_MonsterIds[levelIndex][monsterIndex]
end	
