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

-- 脚本任务数据
-- 11 类型 用来确定寻路后的回调（杀怪，采集，找npc） 
-- 12 目标id（rolebaseid，采集点id）
-- 13 目标个数

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

--捉鬼任务

--脚本号
x220001_g_scriptId=220001
--任务id
x220001_g_missionId=20001

x220001_g_RoundConfigId = 1

--怪物RoleBaseId，需要更多怪物的话，直接在后面加就好了(一行配16个)
x220001_g_MonsterRolebaseId = {}
x220001_g_MonsterRolebaseId[1]={440001,440006,440011,440016,440021,440026,440031,440036,440041,440046,440051,440056,440061,440066,440071,440076}
x220001_g_MonsterRolebaseId[2]={440002,440007,440012,440017,440022,440027,440032,440037,440042,440047,440052,440057,440062,440067,440072,440077}
x220001_g_MonsterRolebaseId[3]={440003,440008,440013,440018,440023,440028,440033,440038,440043,440048,440053,440058,440063,440068,440073,440078}
x220001_g_MonsterRolebaseId[4]={440004,440009,440014,440019,440024,440029,440034,440039,440044,440049,440054,440059,440064,440069,440074,440079}
x220001_g_MonsterRolebaseId[5]={440005,440010,440015,440020,440025,440030,440035,440040,440045,440050,440055,440060,440065,440070,440075,440080}

--随机位置(列表示等级，行(场景1，场景2，场景3...配几个就几个场景))
x220001_g_RandomScene = {}
x220001_g_RandomScene[1]={2001,2002}
x220001_g_RandomScene[2]={2001,2002}
x220001_g_RandomScene[3]={2003,2004}
x220001_g_RandomScene[4]={2003,2004}
x220001_g_RandomScene[5]={2003,2004,2005}
x220001_g_RandomScene[6]={2003,2004,2005,2006}
x220001_g_RandomScene[7]={2004,2005,2006,2007}
x220001_g_RandomScene[8]={2005,2006,2007,2008}
x220001_g_RandomScene[9]={2006,2007,2008,2009}
x220001_g_RandomScene[10]={2007,2008,2009,2010}
x220001_g_RandomScene[11]={2008,2009,2010,2011}
x220001_g_RandomScene[12]={2009,2010,2011,2012}
x220001_g_RandomScene[13]={2010,2011,2012,2013}
x220001_g_RandomScene[14]={2011,2012,2013,2014}
x220001_g_RandomScene[15]={2012,2013,2014,2015}
x220001_g_RandomScene[16]={2013,2014,2015,2016}

x220001_g_AdapteLevel = 5
x220001_g_MinLevel = 25

x220001_g_Duration = 10
x220001_g_KillMonsterCount = 1
x220001_g_MissionEventType = {0, 7}

--- 任务函数 
--任务接受，已接受到任务id请求，通过接受验证，未执行添加逻辑
function x220001_OnMissionAccept( scene,selfId )
	-- body
	if AddMissionNN(scene,selfId,x220001_g_missionId) then  --添加任务，不同步
		local level = MissionGetTeamAdaptLvl(scene, selfId)
		local pos = x220001_RandomMonsterPosition(level)
		local monsterId = x220001_GetMonsterRolebaseId(level)
		local params = {{4,pos}, {11,1}, {12, monsterId}, {13, x220001_g_KillMonsterCount}}
		
		SetMissionParams(scene,selfId,x220001_g_missionId,params) --设置客户端需要的脚本任务参数，不同步
		SetTeamMissionRoundLevel(scene, selfId, level)
		-- SetMissionParam(scene,selfId,x220001_g_missionId,4,nPos,false)
		-- SetMissionParam(scene,selfId,x220001_g_missionId,11,0,false)
		-- SetMissionParam(scene,selfId,x220001_g_missionId,12,x220001_g_MonsterRolebaseId,false)
		-- SetMissionParam(scene,selfId,x220001_g_missionId,13,1,false)
		SetMissionTime(scene, selfId, x220001_g_missionId)
		
		for i = 1, #x220001_g_MissionEventType do
			SetMissionCaredEvent(scene, selfId, x220001_g_missionId, x220001_g_MissionEventType[i], true)
		end

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

		SceneCreateMonster(scene, selfId, monsterId, math.floor(pos / 1000000), math.floor((pos % 1000000) / 1000), math.floor(pos % 1000), 0, x220001_g_Duration)
	end
end

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

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

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


--事件 激活事件监听需要调用SetMissionCareEvent
--杀怪事件监听
function x220001_OnKillMonster( scene,selfId,monsterId)
	-- body
	if GetMissionState(scene,selfId,x220001_g_missionId)~=2 then
	
	if IsTeamLeader(scene, selfId) then	
		local monsterRolebaseId = GetMissionParam(scene, selfId, x220001_g_missionId, 12)
		if monsterId == monsterRolebaseId then

			local count = GetMissionParam(scene, selfId, x220001_g_missionId, 1) + 1      --(param[1] 保存杀怪数)
		
			SetMissionParam(scene, selfId, x220001_g_missionId, 1, count, true)   --任务计数+1
			
			if count >= x220001_g_KillMonsterCount then
				SetMissionState(scene, selfId, x220001_g_missionId, 2)         --设置任务状态为可交 Mission_Completed	
			end
		end
	end
	end
end

function x220001_OnTeamMemberKillMonster(scene, selfId, monsterId)
	-- body
	if GetMissionState(scene,selfId,x220001_g_missionId)~=2 then
	
	if IsTeamLeader(scene, selfId) then	
		local monsterRolebaseId = GetMissionParam(scene, selfId, x220001_g_missionId, 12)
		print(monsterId, monsterRolebaseId)
		if monsterId == monsterRolebaseId then

			print(2)

			local count = GetMissionParam(scene, selfId, x220001_g_missionId, 1) + 1      --(param[1] 保存杀怪数)
		
			SetMissionParam(scene, selfId, x220001_g_missionId, 1, count, true)   --任务计数+1
			print(count, x220001_g_KillMonsterCount)
			if count >= x220001_g_KillMonsterCount then
				SetMissionState(scene, selfId, x220001_g_missionId, 2)         --设置任务状态为可交 Mission_Completed
			end
		end
	end
end
end


--进入区域监听
function x220001_OnOnEnterArea(scene, selfId, areaId)
	-- body

end
--采集事件监听
function x220001_OnCollectItem(scene, selfId, groupPointId, sceneGroupPointid)
	-- body

end

function x220001_GetMonsterRolebaseId(level)
	-- body
	local index = RandomMon(1, #x220001_g_MonsterRolebaseId + 1)
	local monsterIndex = math.floor((level - x220001_g_MinLevel) / x220001_g_AdapteLevel) + 1
	if monsterIndex > #x220001_g_MonsterRolebaseId[index] then
		monsterIndex = #x220001_g_MonsterRolebaseId[index]
	end
	if monsterIndex < 1 then
		monsterIndex = 1
	end
	--print(index, monsterIndex, x220001_g_MonsterRolebaseId[index][monsterIndex])
	return x220001_g_MonsterRolebaseId[index][monsterIndex]
end

function x220001_RandomMonsterPosition(level)
	-- body
	local posIndex = math.floor(math.max(0, level - x220001_g_MinLevel) / x220001_g_AdapteLevel) + 1
	local index = RandomMon(1, #x220001_g_RandomScene[posIndex] + 1)
	--print(posIndex, index)
	return GetRandomScenePos(x220001_g_RandomScene[posIndex][index])
end