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)	-- 选择选项

	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)
--[[
	if GetActiveDataSceneID() ~= nScene then
		return false
	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

]]--
--[[
	local sid = LockNearestTarget("", CHA_RELATION_ENEMY|CHA_TYPE_NPC|CHA_OWNER_FREE|CHA_OWNER_MYSELF|CHA_OCCUPANT_FREE|CHA_OCCUPANT_MYSELF|CHA_OCCUPANT_TEAM|CHA_STATE_ALIVE, "漕运强盗")

	if sid == INVALID_ID then return end
	
	-- 判断死亡
	if Character:GetData(sid, "DIE") == false then 

		local szName = Character:GetName(sid)

		DbgPrintf("目标 = %s", szName)

		AI:SetAI("杀怪")
		AI:SetParameter("初始化")
		AI:SetParameter("目标", "添加", szName)
		AI:Start(true)

		System:Sleep(1500)

		AI:SetAI("拾取")
		AI:Start(true)
	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 )
					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 )
						end
					else
						if g_salt == true then
							Carriage_Action_Clicked(SALT_SELL_VALUE)
							System:Sleep(1000)
						end

						ConvertWrit()
					end
				else

					ShowMessage("地图错误，请在洛阳或大理开始脚本！")
					return

				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)

