ShowMessage("Ӻιδ $Rev: 100 $")


--[[
[author]
class=3
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" or type == "MON" then
			if title == npc_title then
				return id,name,nX,nY
			end
		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 
							local tMap = INVALID_ID
							while isNext == 1 do
								DbgPrintf("  =>%s" , tostring(szMsg))
								--local _,_,sX,sY = string.find( szMsg , "%*.(%d+)%*.(%d+)" )
								local _,_,sMap,sX,sY = string.find( szMsg , "#{.-\042.-\042.(.-)\042.(%d+)\042.(%d+)" )
								if sMap ~= nil and sX ~= nil and sY ~= nil then 
									DbgPrintf("Ŀ %s , (%s,%s)" , tostring(sMap) , tostring(sX) , tostring(sY) )
									tMap = DataBase:GetSceneID(tostring(sMap))
									tX = tonumber(sX)
									tY = tonumber(sY)
								end

								isNext,szMsg = GetDebugMessage()
							end

							if tMap ~= INVALID_ID and tX > 0 and tY > 0  then
								MoveTo(tX, tY, tMap , "", 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()