--setMissionParam
-- 0采集任务阶段==0 采集 1 杀怪
-- 1杀怪计数;
-- 2,3杀怪，传说，宝图任务时间;
-- 4随机地点任务 场景id*1000*1000+坐标x*1000+坐标z
-- 5过期监听
-- 6失败监听，
-- 7失败达成
-- 8攻击次数
-- 9,10 任务接取时间

-- 脚本任务数据
-- 11 类型 用来确定寻路后的回调（1抓鬼 100对话 101送信 102杀怪，103采集，104杀怪掉落,105买道具,106传说道具类型，107传说道具id,108答题,109宝图） 
--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

--捉鬼任务

--脚本号
x210201_g_scriptId=210201
--任务id
x210201_g_missionId=10201

x210201_g_RoundConfigId =2

x210201_g_MinSceneGroupPointLevel=1
x210201_g_AdapteSceneGroupPointLevel=10
--随机位置
x210201_g_ScenePos={}
x210201_g_ScenePos[1]={{sceneId=30,posX=204,posZ=63},{sceneId=30,posX=203,posZ=35},{sceneId=30,posX=200,posZ=41},{sceneId=30,posX=153,posZ=50},{sceneId=30,posX=131,posZ=50},{sceneId=30,posX=218,posZ=163}}
x210201_g_ScenePos[2]={{sceneId=30,posX=204,posZ=63},{sceneId=30,posX=203,posZ=35},{sceneId=30,posX=200,posZ=41},{sceneId=30,posX=153,posZ=50},{sceneId=30,posX=131,posZ=50},{sceneId=30,posX=218,posZ=163},{sceneId=33,posX=127,posZ=217},{sceneId=33,posX=107,posZ=231},{sceneId=33,posX=119,posZ=254},{sceneId=33,posX=121,posZ=273},{sceneId=33,posX=121,posZ=302},{sceneId=33,posX=124,posZ=374}}
x210201_g_ScenePos[3]={{sceneId=30,posX=204,posZ=63},{sceneId=30,posX=203,posZ=35},{sceneId=30,posX=200,posZ=41},{sceneId=30,posX=153,posZ=50},{sceneId=30,posX=131,posZ=50},{sceneId=30,posX=218,posZ=163},{sceneId=33,posX=127,posZ=217},{sceneId=33,posX=107,posZ=231},{sceneId=33,posX=119,posZ=254},{sceneId=33,posX=121,posZ=273},{sceneId=33,posX=121,posZ=302},{sceneId=33,posX=124,posZ=374}}
x210201_g_ScenePos[4]={{sceneId=33,posX=127,posZ=217},{sceneId=33,posX=107,posZ=231},{sceneId=33,posX=119,posZ=254},{sceneId=33,posX=121,posZ=273},{sceneId=33,posX=121,posZ=302},{sceneId=33,posX=124,posZ=374},{sceneId=34,posX=337,posZ=172},{sceneId=34,posX=389,posZ=130},{sceneId=34,posX=394,posZ=134},{sceneId=34,posX=402,posZ=134},{sceneId=34,posX=403,posZ=140},{sceneId=34,posX=410,posZ=149},{sceneId=34,posX=431,posZ=229},{sceneId=34,posX=432,posZ=211},{sceneId=34,posX=240,posZ=230},{sceneId=34,posX=189,posZ=242},{sceneId=34,posX=350,posZ=206},{sceneId=34,posX=346,posZ=190}}
x210201_g_ScenePos[5]={{sceneId=33,posX=127,posZ=217},{sceneId=33,posX=107,posZ=231},{sceneId=33,posX=119,posZ=254},{sceneId=33,posX=121,posZ=273},{sceneId=33,posX=121,posZ=302},{sceneId=33,posX=124,posZ=374},{sceneId=34,posX=431,posZ=229},{sceneId=34,posX=432,posZ=211},{sceneId=34,posX=240,posZ=230},{sceneId=34,posX=189,posZ=242},{sceneId=34,posX=350,posZ=206},{sceneId=34,posX=346,posZ=190},{sceneId=10,posX=119,posZ=257},{sceneId=10,posX=71,posZ=257},{sceneId=10,posX=61,posZ=297},{sceneId=10,posX=93,posZ=311},{sceneId=10,posX=127,posZ=306},{sceneId=10,posX=167,posZ=301}}
x210201_g_ScenePos[6]={{sceneId=34,posX=431,posZ=229},{sceneId=34,posX=432,posZ=211},{sceneId=34,posX=240,posZ=230},{sceneId=34,posX=189,posZ=242},{sceneId=34,posX=350,posZ=206},{sceneId=34,posX=346,posZ=190},{sceneId=10,posX=119,posZ=257},{sceneId=10,posX=71,posZ=257},{sceneId=10,posX=61,posZ=297},{sceneId=10,posX=93,posZ=311},{sceneId=10,posX=127,posZ=306},{sceneId=10,posX=167,posZ=301},{sceneId=12,posX=72,posZ=75},{sceneId=12,posX=81,posZ=72},{sceneId=12,posX=81,posZ=83},{sceneId=12,posX=88,posZ=75},{sceneId=12,posX=128,posZ=171},{sceneId=12,posX=137,posZ=180}}
x210201_g_ScenePos[7]={{sceneId=10,posX=219,posZ=73},{sceneId=10,posX=170,posZ=85},{sceneId=10,posX=133,posZ=102},{sceneId=10,posX=82,posZ=102},{sceneId=10,posX=117,posZ=39},{sceneId=10,posX=169,posZ=17},{sceneId=12,posX=312,posZ=166},{sceneId=12,posX=306,posZ=169},{sceneId=12,posX=309,posZ=158},{sceneId=12,posX=368,posZ=142},{sceneId=12,posX=397,posZ=146},{sceneId=12,posX=408,posZ=140},{sceneId=32,posX=89,posZ=44},{sceneId=32,posX=95,posZ=48},{sceneId=32,posX=95,posZ=41},{sceneId=32,posX=95,posZ=30},{sceneId=32,posX=106,posZ=29},{sceneId=32,posX=111,posZ=36}}
x210201_g_ScenePos[8]={{sceneId=12,posX=312,posZ=166},{sceneId=12,posX=306,posZ=169},{sceneId=12,posX=309,posZ=158},{sceneId=12,posX=368,posZ=142},{sceneId=12,posX=397,posZ=146},{sceneId=12,posX=408,posZ=140},{sceneId=32,posX=123,posZ=109},{sceneId=32,posX=122,posZ=131},{sceneId=32,posX=108,posZ=168},{sceneId=32,posX=113,posZ=172},{sceneId=32,posX=104,posZ=174},{sceneId=32,posX=110,posZ=180},{sceneId=11,posX=70,posZ=142},{sceneId=11,posX=80,posZ=150},{sceneId=11,posX=89,posZ=158},{sceneId=11,posX=99,posZ=162},{sceneId=11,posX=113,posZ=175},{sceneId=11,posX=114,posZ=184}}
x210201_g_ScenePos[9]={{sceneId=32,posX=123,posZ=109},{sceneId=32,posX=122,posZ=131},{sceneId=32,posX=108,posZ=168},{sceneId=32,posX=113,posZ=172},{sceneId=32,posX=104,posZ=174},{sceneId=32,posX=110,posZ=180},{sceneId=11,posX=296,posZ=162},{sceneId=11,posX=296,posZ=174},{sceneId=11,posX=308,posZ=167},{sceneId=11,posX=323,posZ=167},{sceneId=11,posX=338,posZ=175},{sceneId=11,posX=338,posZ=156},{sceneId=18,posX=140,posZ=72},{sceneId=18,posX=144,posZ=90},{sceneId=18,posX=144,posZ=100},{sceneId=18,posX=190,posZ=124},{sceneId=18,posX=287,posZ=165},{sceneId=18,posX=281,posZ=174}}
x210201_g_ScenePos[10]={{sceneId=18,posX=388,posZ=286},{sceneId=18,posX=276,posZ=306},{sceneId=18,posX=253,posZ=328},{sceneId=18,posX=207,posZ=281},{sceneId=18,posX=201,posZ=282},{sceneId=18,posX=203,posZ=277},{sceneId=15,posX=391,posZ=110},{sceneId=15,posX=400,posZ=120},{sceneId=15,posX=387,posZ=129},{sceneId=15,posX=382,posZ=142},{sceneId=15,posX=305,posZ=147},{sceneId=15,posX=298,posZ=153},{sceneId=15,posX=328,posZ=328},{sceneId=15,posX=301,posZ=342},{sceneId=15,posX=295,posZ=361},{sceneId=15,posX=294,posZ=406},{sceneId=15,posX=298,posZ=398},{sceneId=15,posX=291,posZ=421}}

x210201_g_MinMonsterLevel=1
x210201_g_AdapteMonsterLevel=10
--刷怪rolebaseid
x210201_g_MonsterRoleBaseIds={}
x210201_g_MonsterRoleBaseIds[1]={{id=50001,strId=500003},{id=50002,strId=500003},{id=50003,strId=500003},{id=50004,strId=500002},{id=50005,strId=500002},{id=50006,strId=500002}}
x210201_g_MonsterRoleBaseIds[2]={{id=50007,strId=500003},{id=50008,strId=500003},{id=50009,strId=500003},{id=50010,strId=500002},{id=50011,strId=500002},{id=50012,strId=500002}}
x210201_g_MonsterRoleBaseIds[3]={{id=50007,strId=500003},{id=50008,strId=500003},{id=50009,strId=500003},{id=50010,strId=500002},{id=50011,strId=500002},{id=50012,strId=500002}}
x210201_g_MonsterRoleBaseIds[4]={{id=50013,strId=500003},{id=50014,strId=500003},{id=50015,strId=500003},{id=50016,strId=500002},{id=50017,strId=500002},{id=50018,strId=500002}}
x210201_g_MonsterRoleBaseIds[5]={{id=50019,strId=500003},{id=50020,strId=500003},{id=50021,strId=500003},{id=50022,strId=500002},{id=50023,strId=500002},{id=50024,strId=500002}}
x210201_g_MonsterRoleBaseIds[6]={{id=50025,strId=500003},{id=50026,strId=500003},{id=50027,strId=500003},{id=50028,strId=500002},{id=50029,strId=500002},{id=50030,strId=500002}}
x210201_g_MonsterRoleBaseIds[7]={{id=50031,strId=500003},{id=50032,strId=500003},{id=50033,strId=500003},{id=50034,strId=500002},{id=50035,strId=500002},{id=50036,strId=500002}}
x210201_g_MonsterRoleBaseIds[8]={{id=50037,strId=500003},{id=50038,strId=500003},{id=50039,strId=500003},{id=50040,strId=500002},{id=50041,strId=500002},{id=50042,strId=500002}}
x210201_g_MonsterRoleBaseIds[9]={{id=50043,strId=500003},{id=50044,strId=500003},{id=50045,strId=500003},{id=50046,strId=500002},{id=50047,strId=500002},{id=50048,strId=500002}}
x210201_g_MonsterRoleBaseIds[10]={{id=50049,strId=500003},{id=50050,strId=500003},{id=50051,strId=500003},{id=50052,strId=500002},{id=50053,strId=500002},{id=50054,strId=500002}}

--采集id
x210201_g_GroupPointId=5001
--怪物存活时间
x210201_g_MosnterLifeTime=30*60*1000

--- 任务函数 
--任务接受，已接受到任务id请求，通过接受验证，未执行添加逻辑
function x210201_OnMissionAccept( scene,selfId )
	-- body
	if AddMissionNN(scene,selfId,x210201_g_missionId) then  --添加任务，不同步
		local level = GetLevel(scene,selfId)
		local randomPos =x210201_GetRandomPos(level)
		local  pos = randomPos.sceneId*1000000+randomPos.posX*1000+randomPos.posZ
		local monsterRoleBaseId=x210201_GetRandomMonsterId(level)
		print(monsterRoleBaseId.id)
		local params = {{0,0},{1,0},{4,pos},{11,109},{12,x210201_g_GroupPointId},{13,monsterRoleBaseId.id},{15,monsterRoleBaseId.strId}}
		
		SetMissionParams(scene,selfId,x210201_g_missionId,params) --设置客户端需要的脚本任务参数，不同步

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

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

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

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

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


--事件 激活事件监听需要调用SetMissionCareEvent
--采集事件监听
function x210201_OnCollectItem( scene,selfId,groupPointId,SceneGroupPointId)
	-- body
	if groupPointId == GetMissionParam(scene, selfId, x210201_g_missionId, 12) then
		local count = GetMissionParam(scene, selfId, x210201_g_missionId, 1) + 1      --(param[1] 保存杀怪数)
		SetMissionParam(scene, selfId, x210201_g_missionId, 1, count, true)   --任务计数+1
		if count>=1 then
			SetMissionParam(scene, selfId, x210201_g_missionId, 0, 1, true)  
			SetMissionParam(scene, selfId, x210201_g_missionId, 1, 0, true)
			SetMissionTime(scene,selfId,x210201_g_missionId)
			SetMissionCaredEvent(scene, selfId, x210201_g_missionId, 4, false)
			SetMissionCaredEvent(scene, selfId, x210201_g_missionId, 0, true)
			--创建怪
			local monsterId = GetMissionParam(scene, selfId, x210201_g_missionId, 14)
			local  strId = GetMissionParam(scene, selfId, x210201_g_missionId, 15)
			TreasureMonster(scene,selfId,x210201_g_missionId,monsterId,x210201_g_MosnterLifeTime,strId)
		end
	end
end

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


function x210201_GetRandomPos(level)
	local levelIndex = math.floor((level - x210201_g_MinSceneGroupPointLevel) / x210201_g_AdapteSceneGroupPointLevel) + 1
	if levelIndex > #x210201_g_ScenePos then
		levelIndex = #x210201_g_ScenePos
	end
	local groupIndex=math.random(#x210201_g_ScenePos[levelIndex])
	
	print(levelIndex, groupIndex,x210201_g_ScenePos[levelIndex][groupIndex])
	return x210201_g_ScenePos[levelIndex][groupIndex]
end	

function x210201_GetRandomMonsterId(level)
	local levelIndex = math.floor((level - x210201_g_MinMonsterLevel) / x210201_g_AdapteMonsterLevel) + 1
	if levelIndex > #x210201_g_MonsterRoleBaseIds then
		levelIndex = #x210201_g_MonsterRoleBaseIds
	end
	local monsterIndex=math.random(#x210201_g_MonsterRoleBaseIds[levelIndex])
	
	print(levelIndex, monsterIndex,x210201_g_MonsterRoleBaseIds[levelIndex][monsterIndex])
	return x210201_g_MonsterRoleBaseIds[levelIndex][monsterIndex]
end	
