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