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

--捉鬼任务

--脚本号
x211104_g_scriptId=211104
--任务id
x211104_g_missionId=11104

x211104_g_RoundConfigId =11

x211104_g_SceneNpcLevels ={}
x211104_g_SceneNpcLevels[1]=30
x211104_g_SceneNpcLevels[2]=60
x211104_g_SceneNpcLevels[3]=80
x211104_g_SceneNpcLevels[4]=100
x211104_g_SceneNpcLevels[5]=200
x211104_g_SceneNpcLevels[6]=300
x211104_g_SceneNpcLevels[7]=350
x211104_g_SceneNpcLevels[8]=380
x211104_g_SceneNpcLevels[9]=390
x211104_g_SceneNpcLevels[10]=600

x211104_g_SceneNpcIds={}
x211104_g_SceneNpcIds[1]={-1}
x211104_g_SceneNpcIds[2]={-1}
x211104_g_SceneNpcIds[3]={-1}
x211104_g_SceneNpcIds[4]={-1}
x211104_g_SceneNpcIds[5]={-1}
x211104_g_SceneNpcIds[6]={-1}
x211104_g_SceneNpcIds[7]={-1}
x211104_g_SceneNpcIds[8]={-1}
x211104_g_SceneNpcIds[9]={-1}
x211104_g_SceneNpcIds[10]={-1}

x211104_g_SGPLevels ={}
x211104_g_SGPLevels[1]=30
x211104_g_SGPLevels[2]=60
x211104_g_SGPLevels[3]=80
x211104_g_SGPLevels[4]=100
x211104_g_SGPLevels[5]=200
x211104_g_SGPLevels[6]=300
x211104_g_SGPLevels[7]=350
x211104_g_SGPLevels[8]=380
x211104_g_SGPLevels[9]=390
x211104_g_SGPLevels[10]=600

--sceneNpc id
x211104_g_SceneGroupPoints={}
x211104_g_SceneGroupPoints[1]={{id=85,dict=28000,itemid=-1},{id=3011,dict=28001,itemid=-1},{id=3012,dict=28002,itemid=-1}}--（采集物ID；字典表ID；道具ID、不获得道具填-1）
x211104_g_SceneGroupPoints[2]={{id=85,dict=28000,itemid=-1},{id=3011,dict=28001,itemid=-1},{id=3012,dict=28002,itemid=-1},{id=3014,dict=28003,itemid=-1},{id=3015,dict=28004,itemid=-1},{id=3016,dict=28005,itemid=-1}}
x211104_g_SceneGroupPoints[3]={{id=3014,dict=28003,itemid=-1},{id=3015,dict=28004,itemid=-1},{id=3016,dict=28005,itemid=-1},{id=3017,dict=28006,itemid=-1},{id=94,dict=28007,itemid=-1},{id=3018,dict=28008,itemid=-1}}
x211104_g_SceneGroupPoints[4]={{id=3014,dict=28003,itemid=-1},{id=3015,dict=28004,itemid=-1},{id=3016,dict=28005,itemid=-1},{id=3017,dict=28006,itemid=-1},{id=94,dict=28007,itemid=-1},{id=3018,dict=28008,itemid=-1},{id=600,dict=28009,itemid=-1},{id=601,dict=28010,itemid=-1},{id=602,dict=28011,itemid=-1},{id=603,dict=28012,itemid=-1}}
x211104_g_SceneGroupPoints[5]={{id=3014,dict=28003,itemid=-1},{id=3015,dict=28004,itemid=-1},{id=3016,dict=28005,itemid=-1},{id=3017,dict=28006,itemid=-1},{id=94,dict=28007,itemid=-1},{id=3018,dict=28008,itemid=-1},{id=600,dict=28009,itemid=-1},{id=601,dict=28010,itemid=-1},{id=602,dict=28011,itemid=-1},{id=603,dict=28012,itemid=-1},{id=604,dict=28013,itemid=-1},{id=605,dict=28014,itemid=-1},{id=606,dict=28015,itemid=-1},{id=607,dict=28016,itemid=-1}}
x211104_g_SceneGroupPoints[6]={{id=600,dict=28009,itemid=-1},{id=601,dict=28010,itemid=-1},{id=602,dict=28011,itemid=-1},{id=603,dict=28012,itemid=-1},{id=604,dict=28013,itemid=-1},{id=605,dict=28014,itemid=-1},{id=606,dict=28015,itemid=-1},{id=607,dict=28016,itemid=-1},{id=608,dict=28017,itemid=-1},{id=609,dict=28018,itemid=-1},{id=610,dict=28019,itemid=-1}}
x211104_g_SceneGroupPoints[7]={{id=604,dict=28013,itemid=-1},{id=605,dict=28014,itemid=-1},{id=606,dict=28015,itemid=-1},{id=607,dict=28016,itemid=-1},{id=608,dict=28017,itemid=-1},{id=609,dict=28018,itemid=-1},{id=610,dict=28019,itemid=-1},{id=611,dict=28020,itemid=-1},{id=612,dict=28021,itemid=-1},{id=613,dict=28022,itemid=-1}}
x211104_g_SceneGroupPoints[8]={{id=606,dict=28015,itemid=-1},{id=607,dict=28016,itemid=-1},{id=608,dict=28017,itemid=-1},{id=609,dict=28018,itemid=-1},{id=610,dict=28019,itemid=-1},{id=611,dict=28020,itemid=-1},{id=612,dict=28021,itemid=-1},{id=613,dict=28022,itemid=-1},{id=615,dict=28023,itemid=-1},{id=616,dict=28024,itemid=-1}}
x211104_g_SceneGroupPoints[9]={{id=608,dict=28017,itemid=-1},{id=609,dict=28018,itemid=-1},{id=610,dict=28019,itemid=-1},{id=611,dict=28020,itemid=-1},{id=612,dict=28021,itemid=-1},{id=613,dict=28022,itemid=-1},{id=615,dict=28023,itemid=-1},{id=616,dict=28024,itemid=-1},{id=617,dict=28025,itemid=-1},{id=618,dict=28026,itemid=-1}}
x211104_g_SceneGroupPoints[10]={{id=611,dict=28020,itemid=-1},{id=612,dict=28021,itemid=-1},{id=613,dict=28022,itemid=-1},{id=615,dict=28023,itemid=-1},{id=616,dict=28024,itemid=-1},{id=617,dict=28025,itemid=-1},{id=618,dict=28026,itemid=-1},{id=619,dict=28027,itemid=-1},{id=620,dict=28028,itemid=-1},{id=621,dict=28029,itemid=-1}}

--- 任务函数 
--任务接受，已接受到任务id请求，通过接受验证，未执行添加逻辑
function x211104_OnMissionAccept( scene,selfId )
	-- body
	if AddMissionNN(scene,selfId,x211104_g_missionId) then  --添加任务，不同步
		local level = GetLevel(scene,selfId)
		local npcId=x211104_GetRandomSceneNpcId(level)
		local sceneGroupPoint =x211104_GetRandomSceneGroupPoint(level)

		local params = {{1,0},{11,103}, {12,sceneGroupPoint.id},{13,1},{14, npcId},{15,sceneGroupPoint.itemid},{4,sceneGroupPoint.dict}}
		
		SetMissionParams(scene,selfId,x211104_g_missionId,params) --设置客户端需要的脚本任务参数，不同步

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

		SetMissionCaredEvent(scene, selfId, x211104_g_missionId, 4, true)
	end
end

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

--任务完成，执行在删除任务之前，执行一些清理操作，比如清楚任务相关的道具
function x211104_OnMissionCompleted( scene,selfId )
	-- body
	local  itemId = GetMissionParam(scene, selfId, x211104_g_missionId, 15)
	if itemId>0 then
	local  count =GetMissionParam(scene, selfId, x211104_g_missionId, 1)
	DelItem(scene,selfId,itemId,count)
	end
end

--放弃任务，同任务完成
function x211104_OnMissionAbandon( scene,selfId )
	-- body
	local  itemId = GetMissionParam(scene, selfId, x211104_g_missionId, 15)
	if itemId>0 then
	local  count =GetMissionParam(scene, selfId, x211104_g_missionId, 1)
	DelItem(scene,selfId,itemId,count)
	end
end


--事件 激活事件监听需要调用SetMissionCareEvent
--采集事件监听
function x211104_OnCollectItem( scene,selfId,groupPointId,SceneGroupPointId)
	-- body
	if SceneGroupPointId== GetMissionParam(scene, selfId, x211104_g_missionId, 12) then
	--print("collect")
	if GetAvailableBagSize(scene,selfId) <1 then
		SendOperateResult(scene,selfId,104) --背包已满 无法采集
	else
		local  itemId = GetMissionParam(scene, selfId, x211104_g_missionId, 15)
		if itemId>0 then
		CreateItemToBag(scene,selfId,itemId,1)
		end
		local count = GetMissionParam(scene, selfId, x211104_g_missionId, 1) + 1      --(param[1] 保存杀怪数)
		local countNeed =GetMissionParam(scene, selfId, x211104_g_missionId, 13)
		SetMissionParam(scene, selfId, x211104_g_missionId, 1, count, true)   --任务计数+1
		if count>=countNeed then
			SetMissionState(scene, selfId, x211104_g_missionId, 2)	
		end
	end	
	end
end



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

function x211104_GetRandomSceneGroupPoint(level)
	local levelIndex =1
	for i=1,#x211104_g_SGPLevels-1 do
		if level>x211104_g_SGPLevels[i] and level<=x211104_g_SGPLevels[i+1] then
			levelIndex=i+1
		end
	end
	if levelIndex>#x211104_g_SceneGroupPoints then 
		levelIndex=#x211104_g_SceneGroupPoints
	end
	local groupIndex=math.random(#x211104_g_SceneGroupPoints[levelIndex])
	
	--print(levelIndex, groupIndex,x211104_g_SceneGroupPoints[levelIndex][groupIndex])
	return x211104_g_SceneGroupPoints[levelIndex][groupIndex]
end	
