ShowMessage(" $Rev: 100 $")

--[[
[author]
class=3
perday_max=1
cmd0str=Ѻͭڳ
cmd0={XChe=1}
cmd1str=Ѻ˰ڳ
cmd1={XChe=2}
cmd2str=Ѻ˻ƽڳ
cmd2={XChe=3}
cmd_default=0
]]--
script_name=""

local g_ConfirmId = 890901
local g_ConfirmData = {}
local g_XChe = 0

function GetMissionComplete()

	for i=0,9 do
		g_ConfirmData[i] = GetConfimDataBySid(g_ConfirmId,i,"data")
		--DbgPrintf("[%d]%d", i, g_ConfirmData[i])
	end

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 CheckPlayer()

	local wYear,wMonth,wDay,wHour,wMin,wSec = System:GetLocalTime()
	--DbgPrintf("%d-%d-%d %d:%d:%d" , wYear,wMonth,wDay,wHour,wMin,wSec )
	if wHour < 8 or wHour > 22 then 
		ShowMessage(" 23:00  8:00 ֮䲻ɽѺ")
		return false
	end

	if Player:GetData("LEVEL") < 85 then 
		ShowMessage("ȼ85ɽѺ")
		return false
	end

	local nFreeSlot = PlayerPackage:GetEmptySlot(0)
	DbgPrintf("ʣո:%d" , nFreeSlot)
	if nFreeSlot < 1 then 
		ShowMessage("ûпո,޷ִнű")
		return false
	end

	local PlayerMoney = Player:GetData("MONEY")
	local PlayerYuanbao = Player:GetData("YUANBAO")
	if g_XChe == 1 then 
		if PlayerMoney < 100000 then
			ShowMessage("ϽҲ,ɽѺ")
			return false
		end
	elseif g_XChe == 2 then 
		if PlayerMoney < 200000 then
			ShowMessage("ϽҲ,ɽѺ")
			return false
		end
		if PlayerYuanbao < 200 then
			ShowMessage("Ԫ,ɽѺ")
			return false
		end
	elseif g_XChe == 3 then
		if PlayerMoney < 500000 then
			ShowMessage("ϽҲ,ɽѺ")
			return false
		end
		if PlayerYuanbao < 500 then
			ShowMessage("Ԫ,ɽѺ")
			return false
		end
	else
		ShowMessage("޴˲")
		return false					
	end

	return true
end

function _SMain(...)

	if _CommandLine ~= nil then 
		if _CommandLine.XChe ~= nil then
			g_XChe = tonumber(_CommandLine.XChe)
		end
	end

	while true do 

		local WaitTime = 1000
		GetMissionComplete()

		if g_ConfirmData[0] ~= 1 or g_ConfirmData[6] < 2 then
		
			if CheckPlayer() == false then 
				break
			end

			--
			if UI:IsWindowShow("Escort_JieBiao") then
				DoMount(false)
				local XClick = string.format("Escort_JieBiao_OnClick(%d)" , tonumber(g_XChe))
				DoLuaString("Escort_JieBiao_Env" , XClick )
				System:Sleep(500)

				if UI:IsWindowShow("MessageBox_Self2") then
					DoLuaString("MessageBox_Self2_Env" , "MessageBox_Self2_Ok_Clicked()")
					WaitTime = 2000
				end
			else
				DoMount(true)
				MoveToNPC( 100 , 128 , 580 , "" , true)
				QuestFrameOptionClicked(script_name , CMP_FULL_MATCH)

				System:Sleep(1000)
				local strType,_,_,_,szText = DataPool:GetNPCEventList_Item(0)
				if strType == "text" then
					if string.find(szText, "нѺڳ") then
						ShowMessage("Ѻڣ")
						System:UpdateRecord( script_name )
						break
					end
				end
			end

		elseif g_ConfirmData[2] == 4 then 
			
			if g_ConfirmData[1] == 2 then 
				MoveToNPC( 32 , 89 , 569 , "ӱ" , 2 )
				QuestFrameOptionClicked("ڳ" , CMP_FULL_MATCH)
			elseif g_ConfirmData[1] == 3 then 
				MoveToNPC( 234 , 46 , 569 , "" , 2 )
				QuestFrameOptionClicked("ڳ" , CMP_FULL_MATCH)
			elseif g_ConfirmData[1] == 1 then 
				MoveToNPC( 107 , 232 , 569 , "" , 2 )
				QuestFrameOptionClicked("ڳ" , CMP_FULL_MATCH)
			else
				ShowMessage("Ѻڱ,!")
			end

			WaitTime = 2000	
		else
			local MapID = GetActiveDataSceneID()

			if MapID == g_ConfirmData[3] then

				local NpcTitle = "ר"
				if MapID == 569 then
					NpcTitle = ""
				end
				
				local sCheName = Player:GetName()
				sCheName = string.format("%s%s" , tostring(sCheName) , "Ѻ˵ڳ")
				local nCheID,_,nCheX,nCheY = FindSpecialNpc( sCheName )
				local nNpcID,NpcName,nX,nY = FindSpecialNpc( NpcTitle )
				local dist = math.sqrt((nX-g_ConfirmData[4])*(nX-g_ConfirmData[4])+(nY-g_ConfirmData[5])*(nY-g_ConfirmData[5]))

				if nNpcID ~= INVALID_ID and dist < 6 then
					MoveToNPC( nX , nY , MapID , NpcName )

					if MapID == 580 then
						if g_ConfirmData[1] == 1 then 
							if UI:IsWindowShow("AcceptBox") then 
								DoLuaString("AcceptBox_Env" , "AcceptBox_OK_Clicked()")
							else
								QuestFrameOptionClicked("Įԭ" , CMP_PART_MATCH)
								System:Sleep(1000)
							end
						elseif g_ConfirmData[1] == 2 then 
							if UI:IsWindowShow("AcceptBox") then 
								DoLuaString("AcceptBox_Env" , "AcceptBox_OK_Clicked()")
							else
								QuestFrameOptionClicked("ϻ" , CMP_PART_MATCH)
								System:Sleep(1000)
							end
						else
							if UI:IsWindowShow("AcceptBox") then 
								DoLuaString("AcceptBox_Env" , "AcceptBox_OK_Clicked()")
							else
								QuestFrameOptionClicked("" , CMP_PART_MATCH)
								System:Sleep(1000)
							end
						end
					elseif MapID == 569 then
						QuestFrameOptionClicked("ڳ" , CMP_FULL_MATCH)
						WaitTime = 2000	
					else
						if UI:IsWindowShow("AcceptBox") then 
							DoLuaString("AcceptBox_Env" , "AcceptBox_OK_Clicked()")
						else
							QuestFrameOptionClicked("ֺϪ" , CMP_PART_MATCH)
							System:Sleep(1000)
						end
					end
					
				else
					if nCheID ~= INVALID_ID then
						AutoRunToTarget( nCheX , nCheY , g_ConfirmData[3] , "")
						WaitTime = 700
					else
						AutoRunToTarget( g_ConfirmData[4] , g_ConfirmData[5] , g_ConfirmData[3] , "")
					end
				end
			else
				MoveTo( g_ConfirmData[4] , g_ConfirmData[5] , g_ConfirmData[3] , "" , 2 )
			end
		end
		
		System:Sleep(WaitTime)			
	end
end

_SMain()