--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买道具,106传说道具类型，107传说道具id） 
--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

--传说任务--根据道具的classid subclassid完成

--脚本号
x211306_g_scriptId=211306
--任务id
x211306_g_missionId=11306

x211306_g_RoundConfigId =13

x211306_g_MinLevel=1
x211306_g_AdapteLevel=10

x211306_g_SceneNpcIds={}
x211306_g_SceneNpcIds[1]={2700,2701,2702,2703,2704,2705,2706,2707,2708,2709,2710}
x211306_g_SceneNpcIds[2]={2700,2701,2702,2703,2704,2705,2706,2709,2710,4001,4002,4003,4006,4013,4014,4020,4026}
x211306_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}
x211306_g_SceneNpcIds[4]={4033,4035,4036,4037,4038,4070,4074,4086,4096,4097,4101,4105,4115,4118,4119,500,508,510,511,513,601}
x211306_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}
x211306_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}
x211306_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}
x211306_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}
x211306_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}
x211306_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}

x211306_g_MinSceneLevel=1
x211306_g_AdapteSceneLevel=10
--按当前等级计算掉落道具的场景
x211306_g_SceneIds={33,33,33,33,33,33,33,33,33,33}


x211306_g_MinItemLevel=1
x211306_g_AdapteItemLevel=10
x211306_g_ItemClass={}
--shoptype 商店类型  classid subclassid 道具分类 dropid 掉落 strId 道具名称描述 jumpItemId 跳转到商店的道具id链接
x211306_g_ItemClass[1]={{shopType=1,classId=4,subClassId=0,dropId=111111511,strId=8804,jumpItemId=106000007}}
x211306_g_ItemClass[2]={{shopType=1,classId=4,subClassId=0,dropId=111111511,strId=8804,jumpItemId=106000007}}
x211306_g_ItemClass[3]={{shopType=1,classId=4,subClassId=0,dropId=111111511,strId=8804,jumpItemId=106000007}}
x211306_g_ItemClass[4]={{shopType=1,classId=4,subClassId=0,dropId=111111511,strId=8804,jumpItemId=106000007}}
x211306_g_ItemClass[5]={{shopType=1,classId=4,subClassId=0,dropId=111111511,strId=8804,jumpItemId=106000007}}
x211306_g_ItemClass[6]={{shopType=1,classId=4,subClassId=0,dropId=111111511,strId=8804,jumpItemId=106000007}}
x211306_g_ItemClass[7]={{shopType=1,classId=4,subClassId=0,dropId=111111511,strId=8804,jumpItemId=106000007}}
x211306_g_ItemClass[8]={{shopType=1,classId=4,subClassId=0,dropId=111111511,strId=8804,jumpItemId=106000007}}
x211306_g_ItemClass[9]={{shopType=1,classId=4,subClassId=0,dropId=111111511,strId=8804,jumpItemId=106000007}}
x211306_g_ItemClass[10]={{shopType=1,classId=4,subClassId=0,dropId=111111511,strId=8804,jumpItemId=106000007}}
--掉率
x211306_g_RandomItemDrop=99
--传说杀怪有效时间
x211306_g_time=20*60*1000

--- 任务函数 
--任务接受，已接受到任务id请求，通过接受验证，未执行添加逻辑
function x211306_OnMissionAccept( scene,selfId )
	print(GetServerTime())
	print(os.time())
	-- body
	if AddMissionNN(scene,selfId,x211306_g_missionId) then  --添加任务，不同步
		local level = GetLevel(scene,selfId)
		local npcId=x211306_GetRandomSceneNpcId(level)
		local sceneId = x211306_GetSceneId(level)
		local itemClass =x211306_GetRandomItemClass(level)
		local classData = itemClass.classId*10000+itemClass.subClassId*10+itemClass.shopType
		local params = {{0,itemClass.jumpItemId},{1,0},{4,itemClass.strId},{8,x211306_g_time},{11,106}, {12,sceneId},{13,itemClass.dropId},{14,npcId},{15,classData}}
		
		SetMissionParams(scene,selfId,x211306_g_missionId,params) --设置客户端需要的脚本任务参数，不同步

		DispatchMission(scene, selfId, x211306_g_missionId, x211306_g_RoundConfigId)      --同步任务

		SetMissionTime(scene, selfId, x211306_g_missionId)
		
		SetMissionCaredEvent(scene, selfId, x211306_g_missionId, 0, true)
		SetMissionCaredEvent(scene, selfId, x211306_g_missionId, 2, true)
		
		local  count =GetItemCountByClass(scene,selfId,itemClass.classId,itemClass.subClassId)
		SetMissionParam(scene, selfId, x211306_g_missionId, 1, count, true) 
		if count >=1 then
			SetMissionState(scene, selfId, x211306_g_missionId, 2)
		else 	
			SetMissionState(scene, selfId, x211306_g_missionId, 1)	  --设置任务状态为进行中
		end
	end
end

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

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


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

end


--事件 激活事件监听需要调用SetMissionCareEvent
--杀怪事件监听
function x211306_OnKillMonster( scene,selfId,monsterId)
	-- body
	local sceneId = GetMissionParam(scene, selfId, x211306_g_missionId, 12)
	local currentSceneId = GetSceneClassId(scene)
	print(sceneId==currentSceneId)
	if sceneId==currentSceneId then

		local nowTime=	GetServerTime()
		local  missionTime = GetMissionTime(scene,selfId,x211306_g_missionId)
		print(nowTime)
		if nowTime-missionTime < x211306_g_time then
			print("drop")
			if math.random(100)<x211306_g_RandomItemDrop then
				local dropId = GetMissionParam(scene, selfId, x211306_g_missionId, 13)
				if GetAvailableBagSize(scene,selfId)>=1 then
					CreateItemToUserByDropBox(scene,selfId,dropId)
					SetMissionCaredEvent(scene, selfId, x211306_g_missionId, 0, false)
					ClearMissionTime(scene,selfId,x211306_g_missionId)
				else
		 			SendOperateResult(scene,selfId,104) --背包已满 无法掉落
		 		end
			end
		else
		 	SetMissionCaredEvent(scene, selfId, x211306_g_missionId, 0, false)
			ClearMissionTime(scene,selfId,x211306_g_missionId)
		end	
	end
end




function x211306_OnItemChange( scene,selfId )
	-- body
	local  classData = GetMissionParam(scene, selfId, x211306_g_missionId, 15)
	local  classId=classData/10000
	local  subClassId =classData%10000/10
	local count=GetItemCountByClass(scene,selfId,classId,subClassId)
	SetMissionParam(scene, selfId, x211306_g_missionId, 1, count, true) 
	if count>=1 then
		SetMissionState(scene, selfId, x211306_g_missionId, 2)	
	else
		SetMissionState(scene, selfId, x211306_g_missionId, 1)	
	end

end



function x211306_GetRandomSceneNpcId(level)

	local levelIndex = math.floor((level - x211306_g_MinLevel) / x211306_g_AdapteLevel) + 1
	if levelIndex > #x211306_g_SceneNpcIds then
		levelIndex = #x211306_g_SceneNpcIds
	end
	local sceneNpcIndex=math.random(#x211306_g_SceneNpcIds[levelIndex])
	
	print(levelIndex, sceneNpcIndex,x211306_g_SceneNpcIds[levelIndex][sceneNpcIndex])
	return x211306_g_SceneNpcIds[levelIndex][sceneNpcIndex]
end	

function x211306_GetSceneId(level)
	local levelIndex = math.floor((level - x211306_g_MinSceneLevel) / x211306_g_AdapteSceneLevel) + 1
	if levelIndex > #x211306_g_SceneIds then
		levelIndex = #x211306_g_SceneIds
	end
	return x211306_g_SceneIds[levelIndex]
end	
function x211306_GetRandomItemClass(level)

	local levelIndex = math.floor((level - x211306_g_MinItemLevel) / x211306_g_AdapteItemLevel) + 1
	if levelIndex > #x211306_g_ItemClass then
		levelIndex = #x211306_g_ItemClass
	end
	local itemIndex=math.random(#x211306_g_ItemClass[levelIndex])
	
	print(levelIndex, itemIndex,x211306_g_ItemClass[levelIndex][itemIndex])
	return x211306_g_ItemClass[levelIndex][itemIndex]
end	