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

--捉鬼任务

--脚本号
x211105_g_scriptId=211105
--任务id
x211105_g_missionId=11105

x211105_g_RoundConfigId =11

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

x211105_g_SceneNpcIds={}
x211105_g_SceneNpcIds[1]={2700,2701,2702,2703,2704,2705,2706,2707,2708,2709,2710}
x211105_g_SceneNpcIds[2]={2700,2701,2702,2703,2704,2705,2706,2709,2710,4001,4002,4003,4006,4013,4014,4020,4026}
x211105_g_SceneNpcIds[3]={4001,4002,4003,4006,4013,4014,4020,4026,4033,4035,4036,4037,4038,4070,4074,4086,4096,4097,4101,4105,4115,4118,4119}
x211105_g_SceneNpcIds[4]={4033,4035,4036,4037,4038,4070,4074,4086,4096,4097,4101,4105,4115,4118,4119,500,508,510,511,513,601}
x211105_g_SceneNpcIds[5]={500,508,510,511,513,601,1100,1102,1106,1107,1108,1110,1111,1118,1119,1124,1130,1131,1133,1138,1139,1140,1143,1147,1153,1155,1157,1158,1159,1160,1161}
x211105_g_SceneNpcIds[6]={1329,1330,1351,1353,1355,1358,1359,1459,1460,1461,1130,1131,1133,1138,1139,1140,1143,1147,1152,1153,1155,1157,1158,1159,1160,1161}
x211105_g_SceneNpcIds[7]={401,412,413,414,440,441,442,469,1329,1330,1351,1353,1355,1358,1359,1459,1460,1461,1140,1143,1147,1152,1153,1155,1157,1158,1159}
x211105_g_SceneNpcIds[8]={401,412,413,414,440,441,442,469,1329,1330,1351,1353,1355,1358,1359,1509,1510,1513,1514,1515,1516,1517,1519,1520,1521,1522,1525,1527,1528}
x211105_g_SceneNpcIds[9]={1802,1803,1804,1805,1852,1856,1936,1937,1940,1941,2004,1509,1510,1513,1514,1515,1516,1517,1519,1520,1521,1522,1525,1527,1528}
x211105_g_SceneNpcIds[10]={1804,1805,1852,1856,1936,1937,1940,1941,2004,2117,2118,2119,2120,2137,2158,2159,2160,2161,2162,2163,2164,2165,2178,2179,2206,2229,2230,2231}

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


x211105_g_RandomItemDrop=99
--sceneNpc id
x211105_g_MonsterIds={}
x211105_g_MonsterIds[1]={{id=2713,itemId=105000038}}--怪物ID；掉落物品ID
x211105_g_MonsterIds[2]={{id=2713,itemId=105000038},{id=4076,itemId=105000040}}
x211105_g_MonsterIds[3]={{id=2713,itemId=105000038},{id=4076,itemId=105000040}}
x211105_g_MonsterIds[4]={{id=4076,itemId=105000040},{id=532,itemId=105000041},{id=605,itemId=105000042},{id=634,itemId=105000043},{id=577,itemId=105000044}}
x211105_g_MonsterIds[5]={{id=4076,itemId=105000040},{id=532,itemId=105000041},{id=605,itemId=105000042},{id=634,itemId=105000043},{id=577,itemId=105000044}}
x211105_g_MonsterIds[6]={{id=532,itemId=105000041},{id=605,itemId=105000042},{id=634,itemId=105000043},{id=577,itemId=105000044},{id=1435,itemId=105000045},{id=1384,itemId=105000046},{id=1420,itemId=105000047}}
x211105_g_MonsterIds[7]={{id=577,itemId=105000044},{id=1435,itemId=105000045},{id=1384,itemId=105000046},{id=1420,itemId=105000047},{id=410,itemId=105000048},{id=471,itemId=105000049}}
x211105_g_MonsterIds[8]={{id=1420,itemId=105000047},{id=410,itemId=105000048},{id=471,itemId=105000049},{id=1649,itemId=105000050},{id=1584,itemId=105000051},{id=1560,itemId=105000052}}
x211105_g_MonsterIds[9]={{id=1649,itemId=105000050},{id=1584,itemId=105000051},{id=1560,itemId=105000052},{id=1898,itemId=105000053},{id=1917,itemId=105000054},{id=1969,itemId=105000055}}
x211105_g_MonsterIds[10]={{id=1560,itemId=105000052},{id=1898,itemId=105000053},{id=1917,itemId=105000054},{id=1969,itemId=105000055},{id=2181,itemId=105000056},{id=2208,itemId=105000057}}

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

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

		SetMissionCaredEvent(scene, selfId, x211105_g_missionId, 0, true)
		SetMissionCaredEvent(scene, selfId, x211105_g_missionId, 2, true)
	end
end

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

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

--放弃任务，同任务完成
function x211105_OnMissionAbandon( scene,selfId )
	-- body
	local  itemId = GetMissionParam(scene, selfId, x211105_g_missionId, 15)
	local  count =GetMissionParam(scene, selfId, x211105_g_missionId, 1)
	DelItem(scene,selfId,itemId,count)
end


--事件 激活事件监听需要调用SetMissionCareEvent
--杀怪事件监听
function x211105_OnKillMonster( scene,selfId,monsterId)
	-- body
	if GetMissionState(scene,selfId,x211105_g_missionId)~=2 then
	
	local sceneMonsterId = GetMissionParam(scene, selfId, x211105_g_missionId, 12)
	local  itemId = GetMissionParam(scene, selfId, x211105_g_missionId, 15)
	local monsterRolebaseId=GetRoleBaseIdBySceneNpcId(sceneMonsterId)
	if monsterId == monsterRolebaseId then
			
			if math.random(100)<x211105_g_RandomItemDrop then
				if GetAvailableBagSize(scene,selfId)>=1 then
					 CreateItemToBag(scene,selfId,itemId,1) 
				else
					SendOperateResult(scene,selfId,104) --背包已满 无法掉落
				end
			end
			
		end
	end
end


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

end




function x211105_GetRandomSceneNpcId(level)

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

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

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