--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杀怪掉落,105买道具） 
--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

--捉鬼任务

--脚本号
x211106_g_scriptId=211106
--任务id
x211106_g_missionId=11106

x211106_g_RoundConfigId =11

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

x211106_g_ItemIds={}
x211106_g_ItemIds[1]={{id=105000034,type=0},{id=105000035,type=0},{id=105000036,type=0},{id=105000037,type=0}}--等级区间内送的道具ID（买道具送给NPC;0商店、1拍卖行）
x211106_g_ItemIds[2]={{id=105000034,type=0},{id=105000035,type=0},{id=105000036,type=0},{id=105000037,type=0}}
x211106_g_ItemIds[3]={{id=105000034,type=0},{id=105000035,type=0},{id=105000036,type=0},{id=105000037,type=0}}
x211106_g_ItemIds[4]={{id=105000034,type=0},{id=105000035,type=0},{id=105000036,type=0},{id=105000037,type=0}}
x211106_g_ItemIds[5]={{id=105000034,type=0},{id=105000035,type=0},{id=105000036,type=0},{id=105000037,type=0}}
x211106_g_ItemIds[6]={{id=105000034,type=0},{id=105000035,type=0},{id=105000036,type=0},{id=105000037,type=0}}
x211106_g_ItemIds[7]={{id=105000034,type=0},{id=105000035,type=0},{id=105000036,type=0},{id=105000037,type=0}}
x211106_g_ItemIds[8]={{id=105000034,type=0},{id=105000035,type=0},{id=105000036,type=0},{id=105000037,type=0}}
x211106_g_ItemIds[9]={{id=105000034,type=0},{id=105000035,type=0},{id=105000036,type=0},{id=105000037,type=0}}
x211106_g_ItemIds[10]={{id=105000034,type=0},{id=105000035,type=0},{id=105000036,type=0},{id=105000037,type=0}}



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

x211106_g_SceneNpcIds={}
x211106_g_SceneNpcIds[1]={2700,2701,2702,2704}--等级区间内收道具NPCID（NPC1，NPC2)(如果不需要找NPC交任务，填-1)
x211106_g_SceneNpcIds[2]={3001,3003,3005,3013,3023}
x211106_g_SceneNpcIds[3]={5001,5005,5006,5007,5009,5010}
x211106_g_SceneNpcIds[4]={3024,3027,3028,3029,3030,3031}
x211106_g_SceneNpcIds[5]={6001,6004,6005,6006,6007,6008}
x211106_g_SceneNpcIds[6]={7003,7004,7005,7006,7007,7008}
x211106_g_SceneNpcIds[7]={6018,6019,6021,6022,6025}
x211106_g_SceneNpcIds[8]={1102,1130,1131,1139,1140}
x211106_g_SceneNpcIds[9]={8000,1102,1130,1131,1139,1140}
x211106_g_SceneNpcIds[10]={4500,4532,4557,4558,4714}
x211106_g_SceneNpcIds[11]={1300,1301,1302,1303,1307,1310,1312,1313,1321,1322,1323}
x211106_g_SceneNpcIds[12]={1300,1301,1302,1303,1307,1310,1312,1313,1321,1322,1323}
x211106_g_SceneNpcIds[13]={13084,13127,13156,13164,13403,13404}
x211106_g_SceneNpcIds[14]={13084,13127,13156,13164,13403,13404}
--- 任务函数 
--任务接受，已接受到任务id请求，通过接受验证，未执行添加逻辑
function x211106_OnMissionAccept( scene,selfId )
		-- body
		local level = GetLevel(scene,selfId)
		local itemId = x211106_GetRandomItemId(level)

		if AddMissionNN(scene,selfId,x211106_g_missionId) then  --添加任务，不同步
			
			local npcId=x211106_GetRandomSceneNpcId(level)
			local params = {{11,105}, {12,itemId.id},{13,1},{14, npcId},{15,itemId.type}}
			
			SetMissionParams(scene,selfId,x211106_g_missionId,params) --设置客户端需要的脚本任务参数，不同步

			DispatchMission(scene, selfId, x211106_g_missionId, x211106_g_RoundConfigId)      --同步任务

			SetMissionCaredEvent(scene, selfId, x211106_g_missionId, 2, true)

			local  count = GetItemCount(scene,selfId,itemId.id)
			SetMissionParam(scene, selfId, x211106_g_missionId, 1, count, true) 
			if count>=1 then
			SetMissionState(scene, selfId, x211106_g_missionId, 2)
			else 	
			SetMissionState(scene, selfId, x211106_g_missionId, 1)	  --设置任务状态为进行中
			end
		end
end



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

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

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


function x211106_OnItemChange( scene,selfId )
	-- body
	local  itemId = GetMissionParam(scene, selfId, x211106_g_missionId, 12)
	local  countNeed = GetMissionParam(scene, selfId, x211106_g_missionId, 13)
	local count=GetItemCount(scene,selfId,itemId)
	SetMissionParam(scene, selfId, x211106_g_missionId, 1, count, true) 
	if count>=countNeed then
		SetMissionState(scene, selfId, x211106_g_missionId, 2)	
	else
		SetMissionState(scene, selfId, x211106_g_missionId, 1)	
	end

end

function x211106_GetRandomSceneNpcId(level)

	local levelIndex =1
	for i=1,#x211106_g_SceneNpcLevels-1 do
		if level>x211106_g_SceneNpcLevels[i] and level<=x211106_g_SceneNpcLevels[i+1] then
			levelIndex=i+1
		end
	end
	if levelIndex>#x211106_g_SceneNpcIds then 
		levelIndex=#x211106_g_SceneNpcIds
	end

	local sceneNpcIndex=math.random(#x211106_g_SceneNpcIds[levelIndex])
	
	--print(levelIndex, sceneNpcIndex,x211106_g_SceneNpcIds[levelIndex][sceneNpcIndex])
	return x211106_g_SceneNpcIds[levelIndex][sceneNpcIndex]
end	

function x211106_GetRandomItemId(level)

	local levelIndex =1
	for i=1,#x211106_g_ItemLevels-1 do
		if level>x211106_g_ItemLevels[i] and level<=x211106_g_ItemLevels[i+1] then
			levelIndex=i+1
		end
	end
	if levelIndex>#x211106_g_ItemIds then 
		levelIndex=#x211106_g_ItemIds
	end
	
	local itemIndex=math.random(#x211106_g_ItemIds[levelIndex])
	
	print(levelIndex, itemIndex,x211106_g_ItemIds[levelIndex][itemIndex])
	return x211106_g_ItemIds[levelIndex][itemIndex]
end