ShowMessage("子何归兮梦未央 $Rev: 100 $")


--[[
[author]
perday_max=1
descript="请保持背包空格大于2，否者脚本会自动跳过。"
]]--
script_name="子何归兮梦未央"

local g_NpcInfo = {
	[0] = {szNpcName="包宗之" , mapId = 1 , fX=346 , fY=199 , CheckID1=0x0243da63 , CheckID2=0x0243da64 },
}


local PreRide = false 

function _callback(fX, fY, nScene)

	if GetMount() ~= INVALID_ID then
		if PreRide == false then
			 Player:EnableTeamFollow(true)
			UpdateMsg("CheckTeamFollow")
		else
			if IsMsgCold("CheckTeamFollow" , 10000) == true then
				Player:EnableTeamFollow(true)
				UpdateMsg("CheckTeamFollow")
			end
		end

		PreRide = true 
	else
		if PreRide == true then 
			Player:EnableTeamFollow(false)
			UpdateMsg("CheckTeamFollow_False")
		else
			if IsMsgCold("CheckTeamFollow_False" , 10000) == true then
				Player:EnableTeamFollow(false)
				UpdateMsg("CheckTeamFollow_False")
			end
		end
		PreRide = false
	end
 
	return true
end

function FindSpecialNpc( npc_title )
	
	local nMax = EnumObject(true)

	for i = 0 , nMax do 
		local id,type,name,nX,nY,title = EnumObject(false,i)
		--DbgPrintf("%s %s" , name , title)
		if type == "NPC" and title == npc_title then
			return id,name,nX,nY
		end
	end

	return INVALID_ID,"",0,0
end

function _SMain(...)

	local nIndex = 0
	local idMission = -1 
	local idScript = 0 
	local nParam = {}

	while true do

		nIndex, idMission, idScript = DataPool:GetPlayerMission( script_name )
		

		-- 取得任务参数
		for i=0,7 do
			nParam[i] = DataPool:GetPlayerMission_Variable(nIndex, i)
			DbgPrintf("[%d]%d", i, nParam[i])
		end

		if nIndex ~= -1 then

			--DbgPrintf("%s Index:%d MissionID:%d ScriptID:%d", script_name, nIndex, idMission, idScript)

			if nParam[0] == 1 then

				-- 完成任务
				DbgPrintf("任务已完成")
				Player:EnableTeamFollow(false)
				MoveToNPC(  g_NpcInfo[0].fX, g_NpcInfo[0].fY , g_NpcInfo[0].mapId , g_NpcInfo[0].szNpcName )
				QuestFrameOptionClicked(script_name, CMP_FULL_MATCH)	-- 选择选项
				QuestFrameMissionContinue()	-- 继续
				QuestFrameMissionComplete()	-- 完成

			elseif nParam[0] == 0 then
				
				local nCheck1 = DataPool:UserBag_CountItemByIDTable( g_NpcInfo[0].CheckID1 ) 
				local nCheck2 = DataPool:UserBag_CountItemByIDTable( g_NpcInfo[0].CheckID2 )

				if nParam[1] < 1 then
 
					local sNpcName = DataPool:GetTeamMemInfo(0)
					sNpcName = string.format("%s%s" , tostring(sNpcName) , "召唤的魂魄")
					local nNpcID,_,NpcX,nNpcY = FindSpecialNpc( sNpcName )

					if nNpcID ~= INVALID_ID then
						Player:EnableTeamFollow(false)
						Dismount()
						UseItemById_Target(g_NpcInfo[0].CheckID2 , nNpcID , 1)
						System:Sleep(1000)
						continue
					else
						if DataPool:IsTeamLeader() == true then

							if nCheck1 < 1 then
								ShowMessage("未发现道具 芦苇幡")
								MissionBlockProcess(idScript)
								System:Sleep(1000)
								continue
							end

							UseItemById(g_NpcInfo[0].CheckID1)
							System:Sleep(1000)
							local isNext,szMsg = GetDebugMessage()

							local tX = 0 
							local tY = 0 
							while isNext == 1 do
								DbgPrintf("  =>%s" , tostring(szMsg))
								local _,_,sX,sY = string.find( szMsg , "%*.(%d+)%*.(%d+)" )
								if sX ~= nil and sY ~= nil then 
									tX = tonumber(sX)
									tY = tonumber(sY)
								end

								isNext,szMsg = GetDebugMessage()
							end

							if tX > 0 and tY > 0  then
								MoveTo(tX, tY, 4 , "", 3, true, _callback , true )
								System:Sleep(1000)
								Player:EnableTeamFollow(false)
							else
								ShowMessage("尝试再次获取坐标!")
								System:Sleep(1000)
								Player:EnableTeamFollow(false)
								Dismount()
								continue				
							end

						else
							--队友 跟随队长
							AI:SetAI("杀怪")
							AI:SetParameter("初始化")
							AI:SetParameter("地图", -1 )
							AI:SetParameter("拾取", 0)
							AI:SetParameter("运行参数" ,"杀怪", 0)
							AI:SetParameter("运行参数" ,"组队跟随", 1)
							AI:SetParameter("运行参数" ,"辅助攻击", 0)
							AI:SetParameter("运行参数" ,"停止辅助技能", 1)

							AI:Start()
							while true do
								if AI:IsStop() == true  then
									break 
								end

								nNpcID,_,NpcX,nNpcY = FindSpecialNpc( sNpcName )
								if nNpcID ~= INVALID_ID then
									break
								end

								if DataPool:IsTeamLeader() == true then
									break
								end

								System:Sleep(1000)
							end
							AI:Stop()
						end
					end
				elseif nParam[2] < 1 then
					
					local tNpcID , tX , tY = FindObjectByName("黄金芦苇" , "NPC")

					if tNpcID ~= INVALID_ID then
						Player_UseSkill_Collect(0x0cb1,"黄金芦苇")
					else
						ShowMessage("周围未找到 黄金芦苇")
					end

					System:Sleep(1000)
					continue
				end
			else
				
				ShowMessage("任务失败")
				MissionBlockProcess(idScript)
				System:Sleep(1000)

			end

		else
			-- 领取任务

			if DataPool:IsTeamLeader() == true then
				local nEmptySlot = PlayerPackage:GetEmptySlot(0)
				if  nEmptySlot < 2 then 
					ShowMessage("背包需要至少2个空格才能接取任务。")
					return 
				end

				if DataPool:GetTeamMemCount() < 3 then
					ShowMessage("队伍人数少于3人,无法接任务!")
					return
				end
			end

			MoveToNPC(  g_NpcInfo[0].fX, g_NpcInfo[0].fY , g_NpcInfo[0].mapId , g_NpcInfo[0].szNpcName )
			QuestFrameOptionClicked(script_name, CMP_FULL_MATCH)	-- 选择选项
			QuestFrameAcceptClicked()

			System:Sleep(1000)

			local isNext,szMsg = GetDebugMessage()
			while isNext == 1 do
				if string.find(szMsg, "请明天再来") then
					ShowMessage("完成端午 子何归兮梦未央 脚本。") 
					System:UpdateRecord( script_name )
					return
				end
				isNext,szMsg = GetDebugMessage()
			end

		end
	end
end

_SMain()