ShowMessage("-· Rev: 212 ")

--[[
[author]
cmd0str=<-> 
cmd0=0
cmd1str= => 
cmd1=1
cmd_default=0
]]--
script_name=""


local NEED_ACTIVEPOINT = 20

local g_NpcInfo = {
	[0] = {szNpcName="", fX=326, fY=318, nSceneID = 0},
	[1] = {szNpcName="½ʿ", fX=331, fY=169, nSceneID = 1},
	[2] = {szNpcName="", fX=55, fY=176, nSceneID = 2},
}

-- 
local SALT_BUY_VALUE = 1	-- 
local IRON_BUY_VALUE = 2	-- 
local RICE_BUY_VALUE = 3	-- 

local SALT_SELL_VALUE = 10
local IRON_SELL_VALUE = 20
local RICE_SELL_VALUE = 30


-- ӵеĻ
local g_salt = false
local g_iron = false
local g_rice = false

-- ߼
local g_bLuoyang = false
local g_bDali = false
local g_bTime_Sell = false
local g_bTime_Buy = false

local mission_index = -1
local mission_count = 0
local Sell_Price = -1

local thisMode = 0 

function Carriage_Quest_Shown()

	local UI_ID = Get_XParam_INT(0,0);
	DbgPrintf("Carriage_Quest_Shown() UI_ID:%d" , tonumber(UI_ID))

	if UI_ID == 5 then

		local UI_Detail = Get_XParam_INT(0,1)

		if UI_Detail == 12 then

			Carriage_Quest_GuanPiao_Show();

		end
	end

end

function Carriage_Quest_GuanPiao_Show()

	DbgPrintf("Carriage_Quest_GuanPiao_Show()")
	local CD_Time_Sell = Get_XParam_INT(0,2)
	local CD_Time_Buy = Get_XParam_INT(0,3)
	local circle = Get_XParam_INT(0,4)
	mission_index = Get_XParam_INT(0,5)
	mission_count = Get_XParam_INT(0,6)
	Sell_Price = Get_XParam_INT(0,7)

	Refresh_My_Freight()


end

function Refresh_My_Freight()

	if mission_index == -1 then return end

	local Cargo = DataPool:GetPlayerMission_Variable(mission_index,2)
	local Balance = DataPool:GetPlayerMission_Variable(mission_index,5)
	local Price_Display = DataPool:GetPlayerMission_Variable(mission_index,0)

	--DbgPrintf("Cargo=%d Balance=%d Price_Display=%d", Cargo, Balance, Price_Display)

	local Cargo_Standard = 100
	if Cargo < Cargo_Standard then
		g_salt = false
	else
		g_salt = true
	end

	Cargo_Standard = 10
	if math.mod(Cargo,100) < Cargo_Standard then
		g_iron = false
	else
		g_iron = true
	end

	Cargo_Standard = 1
	if math.mod(Cargo,10) < Cargo_Standard then
		g_rice = false
	else
		g_rice = true
	end

	--DbgPrintf("Cargo = %d, Balance = %d, Price_Display = %d", Cargo, Balance, Price_Display)

end

function HaveWrit()
	
	if DataPool:UserBag_CountItemByName("˻") == 1 then
		DbgPrintf("")
		return true
	end

	DbgPrintf("û")
	return false

end

function GetWrit()

	local Npc = nil

	if thisMode == 0 then
		if GetActiveDataSceneID() == 2 then	--жǷڴ
			Npc = g_NpcInfo[2]
		else	-- ڴʹʼ
			Npc = g_NpcInfo[0]
		end
	else
		Npc = g_NpcInfo[0]
	end

	MoveToNPC(Npc.fX, Npc.fY, Npc.nSceneID, Npc.szNpcName)
	QuestFrameOptionClicked("ʼ", CMP_FULL_MATCH)	-- ѡѡ

	DoLuaString("Carriage_Env", "Get_Writ()")	-- ȡùƱ

	System:Sleep(1000)

	local strType,_,_,_,szText = DataPool:GetNPCEventList_Item(0);

	if strType == "text" then
		if string.find(szText, "20") then
			return false
		end
	end

	return true

end

function ConvertWrit()

	local Npc = nil

	if GetActiveDataSceneID() == 2 then	--жǷڴ
		Npc = g_NpcInfo[2]
	else	-- ڴʹʼ
		Npc = g_NpcInfo[0]
	end

	MoveToNPC(Npc.fX, Npc.fY, Npc.nSceneID, Npc.szNpcName, false)
	QuestFrameOptionClicked("ʼ", CMP_FULL_MATCH)	-- ѡѡ

	local dwTimeOut = System:GetTickCount() + 3000
	while System:GetTickCount() < dwTimeOut do
		if UI:IsWindowShow("Carriage") then 
			break 
		end
		System:Sleep(100)
	end

	if g_salt == true then
		Carriage_Action_Clicked(SALT_SELL_VALUE)
		System:Sleep(1000)
	end

	if g_iron == true then
		Carriage_Action_Clicked(IRON_SELL_VALUE)
		System:Sleep(1000)
	end

	DoLuaString("Carriage_Env", "Convert_Writ()")
	System:Sleep(1000)
	QuestFrameOptionClicked("̰̰", CMP_PART_MATCH)
	mission_index = -1
end


function Carriage_Action_Clicked(nIndex)

	local Npc = nil
	local Item = nil

	if GetActiveDataSceneID() == 2 then	--жǷڴ
		Npc = g_NpcInfo[2]
	else					-- ڴʹʼ
		Npc = g_NpcInfo[0]
	end

	MoveToNPC(Npc.fX, Npc.fY, -1, Npc.szNpcName, false)
	QuestFrameOptionClicked("ʼ", CMP_FULL_MATCH)	-- ѡѡ


	local CD_Time_Sell = Get_XParam_INT(1,6);
	local CD_Time_Buy = Get_XParam_INT(1,7);

	if  nIndex < 10 then

		if CD_Time_Buy <= 0 then
			DoLuaString("Carriage_Env", "Carriage_Button1_Clicked()")	--ѹг
			System:Sleep(1000)
		elseif g_bTime_Buy == true then
			ShowMessage("ȴѹгʱ")
			System:Sleep(100)
			return
		end

	elseif nIndex > 9 then

		if CD_Time_Sell <= 0 then
			DoLuaString("Carriage_Env", "Carriage_Button2_Clicked()")	--̧г
			System:Sleep(1000)
		elseif g_bTime_Sell == true then
			ShowMessage("ȴ̧гʱ")
			System:Sleep(100)
			return
		end
	end
--[[
	if CD_Time_Buy <= 0 and nIndex < 10 then
		DoLuaString("Carriage_Env", "Carriage_Button1_Clicked()")	--ѹг
		System:Sleep(1000)
	end

	if CD_Time_Sell <= 0 and nIndex > 9 then
		DoLuaString("Carriage_Env", "Carriage_Button2_Clicked()")	--̧г
		System:Sleep(1000)
	end
]]--
	DoLuaString("Carriage_Env", string.format("Carriage_Action_Clicked(%d)", nIndex))

end

function _callback(fX, fY, nScene)

	local nMP = Player:GetData("MP")

	if nMP < 500 then
		return true
	end

	local nMenPai = Player:GetData("MENPAI")

	--
	if nMenPai == 0 then
		if Player_GetBuff("һέɽ") == -1 then
			UseSkill("һέɽ")
		end
	--
	elseif nMenPai == 1 then
		if Player_GetBuff("") == -1 then
			UseSkill("")
		end
	--ؤ
	elseif nMenPai == 2 then
		if Player_GetBuff("˲") == -1 then
			UseSkill("˲")
		end
	--
	elseif nMenPai == 6 then
		if Player_GetBuff("ö") == -1 then
			UseSkill("ö")
		end
	--ɽ
	elseif nMenPai == 7 then
		if Player_GetBuff("׾Թ϶") == -1 then
			UseSkill("׾Թ϶")
		end
	--Ī
	elseif nMenPai == 10 then
		if Player_GetBuff("İ") == -1 then
			UseSkill("İ")
		end
	--
	elseif nMenPai == 11 then
		if Player_GetBuff("") == -1 then
			UseSkill("")
		end

	end

	return true

end

function DaliHuixue()

	while true do

		local nHPPercent = Player:GetData("HP_PERCENT")
		local nMPPercent = Player:GetData("MP_PERCENT")

		DbgPrintf("LuoYangRestore() %d %d", nHPPercent, nMPPercent)

		if (nHPPercent > 75 and nMPPercent > 75) then return end
		
		MoveToNPC(44,149,-1,"",false)
		QuestFrameOptionClicked("", CMP_FULL_MATCH)
		QuestFrameOptionClicked("", CMP_FULL_MATCH)

		QuestFrameSpeak(0, "")
		QuestFrameOptionClicked("޻ָѪ", CMP_FULL_MATCH)
		QuestFrameOptionClicked("õ", CMP_FULL_MATCH)

		System:Sleep(200)
	end
end

-- Ѫ
function LuoyangHuixue()
	
	while true do

		local nHPPercent = Player:GetData("HP_PERCENT")
		local nMPPercent = Player:GetData("MP_PERCENT")

		DbgPrintf("LuoYangRestore() %d %d", nHPPercent, nMPPercent)

		if (nHPPercent > 75 and nMPPercent > 75) then return end
		
		MoveToNPC(185, 336, 0, "",false)
		QuestFrameOptionClicked("ŭ", CMP_FULL_MATCH)
		QuestFrameOptionClicked("", CMP_FULL_MATCH)

		QuestFrameSpeak(0, "")
		QuestFrameOptionClicked("޻ָѪ", CMP_FULL_MATCH)
		QuestFrameOptionClicked("õ", CMP_FULL_MATCH)

		System:Sleep(200)
	end
end

function OnCarriage()

	while true do

		if HaveWrit() == true then

			if mission_index == -1 then
				--or mission_index == 0
				DbgPrintf("ʹû")
				UseItem("˻")
				UI:WindowHide("Carriage")
				Carriage_Quest_Shown()
			else

				Refresh_My_Freight()
				
				if g_iron then
					DbgPrintf("g_iron = true")
				else
					DbgPrintf("g_iron = false")
				end

				if g_rice then
					DbgPrintf("g_rice = true")
				else
					DbgPrintf("g_rice = false")
				end

				if g_salt then
					DbgPrintf("g_salt = true")
				else
					DbgPrintf("g_salt = false")
				end

				local nCurSceneID = GetActiveDataSceneID()

				if nCurSceneID == 0 then

					if g_iron == true then

						Carriage_Action_Clicked(IRON_SELL_VALUE)
						System:Sleep(1000)
						ConvertWrit()

					elseif g_salt == false then

						if mission_count == 20 then
							ConvertWrit()
						else
							Carriage_Action_Clicked(SALT_BUY_VALUE)
						end
						System:Sleep(1000)

					elseif g_salt == true then

						if g_bLuoyang == true then
							LuoyangHuixue()
						end

						--MoveToNPC(g_NpcInfo[2].fX, g_NpcInfo[2].fY, g_NpcInfo[2].nSceneID, g_NpcInfo[2].szNpcName, false)
						MoveTo(g_NpcInfo[2].fX, g_NpcInfo[2].fY, g_NpcInfo[2].nSceneID, g_NpcInfo[2].szNpcName, 3 , false , _callback , false , true )
					end

				elseif nCurSceneID == 2 then

					if thisMode == 0 then 
						if g_salt == true then
							Carriage_Action_Clicked(SALT_SELL_VALUE)
							System:Sleep(1000)	
							ConvertWrit()	
						elseif g_iron == false then
							if mission_count == 20 then
								ConvertWrit()
							else
								Carriage_Action_Clicked(IRON_BUY_VALUE)
							end
							System:Sleep(1000)
						elseif g_iron == true then
							MoveTo(g_NpcInfo[0].fX, g_NpcInfo[0].fY, g_NpcInfo[0].nSceneID, g_NpcInfo[0].szNpcName, 3 , false , _callback , false , true )
						end
					else
						if g_salt == true then
							Carriage_Action_Clicked(SALT_SELL_VALUE)
							System:Sleep(1000)
						end

						ConvertWrit()
					end
				else
					--ǴӴʼ
					if g_iron == true then
						--
						MoveTo(g_NpcInfo[0].fX, g_NpcInfo[0].fY, g_NpcInfo[0].nSceneID, g_NpcInfo[0].szNpcName, 3 , false , _callback , false , true )
					else
						--ʼ
						MoveTo(g_NpcInfo[2].fX, g_NpcInfo[2].fY, g_NpcInfo[2].nSceneID, g_NpcInfo[2].szNpcName, 3 , false , _callback , false , true )
					end
				end

			end
		else
			if GetWrit() == false then
				ShowMessage("Ѿ20")
				System:UpdateRecord( script_name )
				return
			end
		end

		System:Sleep(200)
	end

end

function _SMain(...)

	if arg.n ~= 4 then 
		ShowMessage("")
		return
	end

	if _CommandLine ~= nil then
		thisMode = tonumber(_CommandLine)
	end
	
	g_bTime_Sell = arg[1]
	g_bTime_Buy = arg[2]
	g_bLuoyang = arg[3]
	g_bDali = arg[4]

	if Player:GetData("LEVEL") < 40 then
		ShowMessage("ȼ40")
		return
	end

	if Player:GetData("MONEY") < 20000 and Player:GetData("MONEY_JZ") < 20000 then
		ShowMessage("Ҳ㣡")
		return
	end

	OnCarriage()

end

-- һʾǷȴ̧۸ trueʾȴfalseʾõȴ
-- ڶʾǷȴѹ۸ trueʾȴ
-- ʾǷѪ
-- ĸʾǷڴѪ
_SMain(false,false,true,false)

