ShowMessage("寻宝乐翻天脚本 $Rev: 100 $")


--[[
[author]
class=3
perday_max=1
descript="请留意背包空格，保证有足够位置可以让脚本顺利执行。"
]]--
script_name="寻宝乐翻天"

local g_NpcName = "猪小胖"			-- NPC名称
local g_Npc_X = 289
local g_Npc_Y = 222
local g_Npc_Map = 1


function _SMain(...)

	local nIndex = 0
	local idMission = -1 
	local idScript = 0 

	while true do

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

		if nIndex ~= -1 then

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

			if DataPool:IsMissionCanCommit(idMission) then

				-- 完成任务
				DbgPrintf("任务已完成")
				MoveToNPC( g_Npc_X, g_Npc_Y, g_Npc_Map , g_NpcName )
				QuestFrameOptionClicked(script_name, CMP_FULL_MATCH)	-- 选择选项
				QuestFrameMissionContinue()	-- 继续
				QuestFrameMissionComplete()	-- 完成
				
			else
				local nParam 
				-- 取得任务参数
				for i=0,7 do
					nParam[i] = DataPool:GetPlayerMission_Variable(nIndex, i)
					DbgPrintf("[%d]%d", i, nParam[i])
				end

				Dismount()
				UseItem("寻宝仙镜")
				System:Sleep(1000)
				local isNext,szMsg = GetDebugMessage()

				local tX = 0 
				local tY = 0 
				local tMap = -1
				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
					if nParam[1] == 0 then 
						tMap = 1
					elseif nParam[2] == 0 then 
						tMap = 0
					elseif nParam[0] == 0 then						
						tMap = 2
					end
				else
					ShowMessage("尝试再次获取坐标!")
					System:Sleep(1000)
					continue				
				end

				if tMap ~= -1 and tX > 0 and tY > 0 then
					MoveTo( tX , tY , tMap )
					Dismount()
					UseItem("寻宝仙镜")
				end
			end
		else
			-------------------------------------------------------------------------------------
			-- 领取任务

			local nLevel = Player:GetData("LEVEL")

			if nLevel < 30 then 
				ShowMessage("等级不够30级，不能接寻宝乐翻天任务。")
				return
			end

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

			MoveToNPC( g_Npc_X, g_Npc_Y, g_Npc_Map , g_NpcName )
			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

		System:Sleep(500)
	end

end

_SMain()