--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

--捉鬼任务

--脚本号
x211202_g_scriptId=211202
--任务id
x211202_g_missionId=11202

x211202_g_RoundConfigId =12

x211202_g_MinLevel=1
x211202_g_AdapteLevel=10

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

x211202_g_MinMonsterLevel=1
x211202_g_AdapteMonsterLevel=10

--杀的怪的sceneNpc id
x211202_g_MonsterIds={}
x211202_g_MonsterIds[1]={2716}
x211202_g_MonsterIds[2]={2716,4079}
x211202_g_MonsterIds[3]={2716,4079}
x211202_g_MonsterIds[4]={2716,4079,533,543,573,607,635,654}
x211202_g_MonsterIds[5]={4079,533,543,573,607,635,654}
x211202_g_MonsterIds[6]={607,635,654,1303,1320,1335,1368,1381,1395,1419,1433,1447}
x211202_g_MonsterIds[7]={1368,1381,1395,1419,1433,1447,421,405,445,460,471}
x211202_g_MonsterIds[8]={421,405,445,460,471,1546,1560,1570,1585,1608,1618,1631}
x211202_g_MonsterIds[9]={1647,1673,1807,1825,1846,1858,1882,1898,1917,1945,1969}
x211202_g_MonsterIds[10]={1858,1882,1898,1917,1945,1969,2101,2110,2122,2139,2181,2208}

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

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

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

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

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

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


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

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

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


function x211202_GetRandomSceneNpcId(level)
			
	local levelIndex = math.floor((level - x211202_g_MinLevel) / x211202_g_AdapteLevel) + 1
	if levelIndex > #x211202_g_SceneNpcIds then
		levelIndex = #x211202_g_SceneNpcIds
	end
	local sceneNpcIndex=math.random(#x211202_g_SceneNpcIds[levelIndex])
	
	print(levelIndex, sceneNpcIndex,x211202_g_SceneNpcIds[levelIndex][sceneNpcIndex])
	return x211202_g_SceneNpcIds[levelIndex][sceneNpcIndex]
end	

function x211202_GetRandomMonsterId(level)
	local levelIndex = math.floor((level - x211202_g_MinMonsterLevel) / x211202_g_AdapteMonsterLevel) + 1
	if levelIndex > #x211202_g_MonsterIds then
		levelIndex = #x211202_g_MonsterIds
	end
	local monsterIndex=math.random(#x211202_g_MonsterIds[levelIndex])
	
	print(levelIndex, monsterIndex,x211202_g_MonsterIds[levelIndex][monsterIndex])
	return x211202_g_MonsterIds[levelIndex][monsterIndex]
end	
