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

--捉鬼任务

--脚本号
x211401_g_scriptId=211401

--环ID
x211401_g_RoundConfigId = 14

--任务ID(策划配置)
x211401_g_MissionId = 60550

--采集点ID(策划配置)
x211401_g_SceneGroupPoints={}
x211401_g_SceneGroupPoints[1]={{id=300,dict=10259,itemid=-1}}--（采集物ID；字典表ID；道具ID、不获得道具填-1）
x211401_g_SceneGroupPoints[2]={{id=301,dict=10259,itemid=-1}}
x211401_g_SceneGroupPoints[3]={{id=302,dict=10259,itemid=-1}}
x211401_g_SceneGroupPoints[4]={{id=303,dict=10259,itemid=-1}}
x211401_g_SceneGroupPoints[5]={{id=304,dict=10259,itemid=-1}}
x211401_g_SceneGroupPoints[6]={{id=500,dict=10259,itemid=-1}}
x211401_g_SceneGroupPoints[7]={{id=501,dict=10259,itemid=-1}}
x211401_g_SceneGroupPoints[8]={{id=502,dict=10259,itemid=-1}}
x211401_g_SceneGroupPoints[9]={{id=503,dict=10259,itemid=-1}}
x211401_g_SceneGroupPoints[10]={{id=504,dict=10259,itemid=-1}}

--- 任务函数 
--任务接受，已接受到任务id请求，通过接受验证，未执行添加逻辑
function x211401_OnMissionAccept(scene,selfId)

	if AddMissionNN(scene,selfId,x211401_g_missionId) then  --添加任务，不同步
		local sceneGroupPoint = x211401_GetRandomSceneGroupPoint(scene,selfId)

		local params = {{1,0},{11,103},{12,sceneGroupPoint.id},{13,1},{14,-1},{15,sceneGroupPoint.itemid},{4,sceneGroupPoint.dict}}
		
		SetMissionParams(scene,selfId,x211401_g_missionId,params)	--设置客户端需要的脚本任务参数，不同步

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

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

--检查任务是否可以完成
function x211401_CheckCompleted(scene,selfId)

	return true
end

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

	local itemId = GetMissionParam(scene,selfId,x211401_g_missionId,15)
	if itemId > 0 then
		local count = GetMissionParam(scene,selfId,x211401_g_missionId,1)
		DelItem(scene,selfId,itemId,count)	
	end
end

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

	local itemId = GetMissionParam(scene,selfId,x211401_g_missionId,15)
	if itemId > 0 then
		local count = GetMissionParam(scene,selfId,x211401_g_missionId,1)
		DelItem(scene,selfId,itemId,count)
	end
end


--事件 激活事件监听需要调用SetMissionCareEvent
--采集事件监听
function x211401_OnCollectItem( scene,selfId,groupPointId,SceneGroupPointId)

	print("x211401_OnCollectItem")
	if SceneGroupPointId == GetMissionParam(scene,selfId,x211401_g_missionId,12) then
		if GetAvailableBagSize(scene,selfId) < 1 then
			SendOperateResult(scene,selfId,104) --背包已满 无法采集
		else
			local itemId = GetMissionParam(scene,selfId,x211401_g_missionId,15)
			if itemId > 0 then
				CreateItemToBag(scene,selfId,itemId,1)
			end
			local count = GetMissionParam(scene,selfId,x211401_g_missionId,1) + 1	--(param[1] 保存杀怪数)
			local countNeed = GetMissionParam(scene,selfId,x211401_g_missionId,13)
			SetMissionParam(scene,selfId,x211401_g_missionId,1,count,true)	--任务计数+1
			if count >= countNeed then
				SetMissionState(scene,selfId,x211401_g_missionId,2)	
			end
		end	
	end
end

function x211401_GetRandomSceneGroupPoint(scene,selfId)

	local round = GetMissionRoundInSection(scene,selfId,x211401_g_RoundConfigId)
	local index = GetGuildDinnerGroupPointIndex(scene,selfId,round,x211401_g_RoundConfigId,#x211401_g_SceneGroupPoints)
	
	print(index,x211401_g_SceneGroupPoints[index])
	return x211401_g_SceneGroupPoints[index]
end	
