ShowMessage("端午驱瘴 $Rev: 100 $")


--[[
[author]
class=3
perday_max=1
descript="请保持材料背包空格大于2，否者脚本会自动跳过。"
]]--
script_name="端午驱瘴"

local g_NpcInfo = {
	[0] = {szNpcName="端庆" , mapId = 1 , fX=221 , fY=205 , CheckID1=0x013bdb8c , CheckID2=0x2626c2c },
}

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 DataPool:IsMissionCanCommit(idMission) then

				-- 完成任务
				DbgPrintf("任务已完成")
				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()	-- 完成
				
			else
				local nCheck1 = DataPool:UserBag_CountItemByIDTable( g_NpcInfo[0].CheckID1 ) 
				local nCheck2 = DataPool:UserBag_CountItemByIDTable( g_NpcInfo[0].CheckID2 ) 

				if nParam[1] < 1 then
 
					if nCheck1 < 5 then

						AI:SetAI("采集")
						AI:SetParameter("初始化")
						AI:SetParameter("目标", "添加", "艾蒿")
						AI:SetParameter("地图", 4 )
						AI:SetParameter("坐标", "添加",272, 187)

						AI:Start(false)

						while true do
							nCheck1 = DataPool:UserBag_CountItemByIDTable( g_NpcInfo[0].CheckID1 ) 
							if nCheck1 > 4 then
								break
							end
	
							System:Sleep(500)
						end
		
						AI:Stop()
					else

						--交艾叶
						MoveToNPC(  g_NpcInfo[0].fX, g_NpcInfo[0].fY , g_NpcInfo[0].mapId , g_NpcInfo[0].szNpcName )
						QuestFrameOptionClicked("制作艾人", CMP_FULL_MATCH)	-- 选择选项
						System:Sleep(3000)
					end

				else

					if nCheck2 < 1 then
						ShowMessage("未发现道具 艾人")
						MissionBlockProcess(idScript)
					else
						math.randomseed(os.time())
						local nX = math.random(221, 231)
						local nY = math.random(188, 212)
						MoveTo(  nX, nY , g_NpcInfo[0].mapId )
						Dismount()
						UseItem("艾人", "")
						System:Sleep(2000)
					end

				end

			end

		else
			-- 领取任务

			local nEmptySlot = PlayerPackage:GetEmptySlot(1)
			if  nEmptySlot < 2 then 
				ShowMessage("材料栏需要至少2个空格才能接取任务。")
				return 
			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()