ShowMessage("公会炼金任务脚本 $Rev: 209 $")

local NEED_ACTIVEPOINT = 50

local g_szMission = "炼金任务"
local g_nParam = {}
local g_nIndex = 0
local g_idMission = 0
local g_idScript = 0
local g_dwNextTime = 0

local g_NpcInfo = {
	[0] = {szNpcName="戴三金", fX=66, fY=135},
	[1] = {szNpcName="孔相如", fX=44, fY=46},
	[2] = {szNpcName="马应雄", fX=65, fY=55},
	[3] = {szNpcName="武大威", fX=134, fY=49},
	[4] = {szNpcName="万藏宝", fX=149, fY=44},
	[5] = {szNpcName="钱为一", fX=149, fY=56},
	[6] = {szNpcName="佟芙蓉", fX=147, fY=95},
	[7] = {szNpcName="朱世友", fX=129, fY=98},
	[8] = {szNpcName="苟写", fX=73, fY=93},
	[9] = {szNpcName="卢黄连", fX=50, fY=97},
	[10] = {szNpcName="穆易", fX=46, fY=90},
	[11] = {szNpcName="杜子庞", fX=45, fY=73},
	[12] = {szNpcName="乐善", fX=44, fY=58},
	[13] = {szNpcName="武强盛", fX=131, fY=126}
}

function GetBuff(szBuffName)

	local nBuffCount = Player:GetBuffNumber()

	for i = 0, nBuffCount - 1 do
		local szBuffToolTips = Player:GetBuffToolTipsByIndex(i)

		if string.find(szBuffToolTips, szBuffName) then
			return i
		end
	end
	return -1
end

function LJ_OnMission_600061()
	
	local index = g_nParam[5]

	if g_NpcInfo[index] == nil then
		ShowMessage("炼金任务：未知的NPC")
		return
	end

	if GetBuff("变身") ~= -1 then
		MoveToNPC(g_NpcInfo[0].fX, g_NpcInfo[0].fY, -1, g_NpcInfo[0].szNpcName)
	else
		MoveToNPC(g_NpcInfo[index].fX, g_NpcInfo[index].fY, -1, g_NpcInfo[index].szNpcName)
		Dismount()
		QuestFrameOptionClicked("开始搬运金矿", CMP_PART_MATCH)
	end

end

function LJ_OnMission_600062()
	if GetBuff("变身") == -1 then
		MoveToNPC(g_NpcInfo[0].fX, g_NpcInfo[0].fY, -1, g_NpcInfo[0].szNpcName)
		Dismount()
		QuestFrameOptionClicked("获取变身能力", CMP_FULL_MATCH)
	else
		if Get_XParam_INT(600055, 0) == 0 then
			UseSkill("查找资源")
		else
			local x = Get_XParam_INT(600055, 1)
			local y = Get_XParam_INT(600055, 2)

			MoveTo(x, y, -1, "")
			UseSkill("采集资源")
		end
	end
end

function LJ_OnMission_600063()

	if GetBuff("变身") == -1 then
		MoveToNPC(g_NpcInfo[0].fX, g_NpcInfo[0].fY, -1, g_NpcInfo[0].szNpcName)
		Dismount()
		QuestFrameOptionClicked("获取变身能力", CMP_FULL_MATCH)
	else
		if Get_XParam_INT(600055, 0) == 0 then
			UseSkill("查找资源")
		else
			local x = Get_XParam_INT(600055, 1)
			local y = Get_XParam_INT(600055, 2)

			MoveTo(x, y, -1, "")
			UseSkill("收集资源")
		end
	end
end

function LJ_OnMission_600064()

	if GetBuff("变身") == -1 then
		MoveToNPC(g_NpcInfo[0].fX, g_NpcInfo[0].fY, -1, g_NpcInfo[0].szNpcName)
		Dismount()
		QuestFrameOptionClicked("获取变身能力", CMP_FULL_MATCH)
	else
	--[[
		while TripperObj_Active("金矿") do
			System:Sleep(1000)
		end
		]]--
		
		OnPickup()
		
		if Get_XParam_INT(600055, 0) == 0 then
			UseSkill("查找资源")
		else
			local x = Get_XParam_INT(600055, 1)
			local y = Get_XParam_INT(600055, 2)

			MoveTo(x, y, -1, "")
			UseSkill("挖掘资源")
		end
	end

end



------------------------------------------------------------------------------------------------
function _SMain(...)

	local nQuestTimes = 0 
	local nWaitTimes = 0 

	while true do

		g_nIndex, g_idMission, g_idScript = DataPool:GetPlayerMission("炼金任务")
		if g_nIndex == -1 then
			g_szMission = "快捷炼金任务"
			g_nIndex, g_idMission, g_idScript = DataPool:GetPlayerMission(g_szMission)
		else
			g_szMission = "炼金任务"
		end

		if g_nIndex ~= -1 then

			DbgPrintf("%s Index:%d MissionID:%d ScriptID:%d", g_szMission, g_nIndex, g_idMission, g_idScript)

			-- 取得任务参数
			for i=0,7 do
				g_nParam[i] = DataPool:GetPlayerMission_Variable(g_nIndex, i)
				DbgPrintf("[%d]%d", i, g_nParam[i])
			end

			CityToGuild()	-- 进入帮派城市

			if DataPool:IsMissionCanCommit(g_idMission) then

				-- 完成任务
				DbgPrintf("任务已完成")
				MoveToNPC(g_NpcInfo[0].fX, g_NpcInfo[0].fY, -1, g_NpcInfo[0].szNpcName)
				QuestFrameOptionClicked(g_szMission, CMP_FULL_MATCH)	-- 选择选项
				QuestFrameMissionContinue()	-- 继续
				QuestFrameMissionComplete()	-- 完成
				QuestFrameMissionClose()
				
			else
				-- 已接任务，未完成
				DbgPrintf("任务进行中")

				if g_idScript == 600061 or g_idScript == 891013 then		-- 从指定NPC背金子
					LJ_OnMission_600061()
				elseif g_idScript == 600062 or g_idScript == 891014 then	-- 在一个地区采集资源，金子直接进入背包
					LJ_OnMission_600062()
				elseif g_idScript == 600063 or g_idScript == 891015 then	-- 在一个地区收集资源，金子直接进入背包
					LJ_OnMission_600063()
				elseif g_idScript == 600064 or g_idScript == 891016 then	-- 在一个地区挖掘资源，金子掉落在地上
					LJ_OnMission_600064()
				else
					ShowMessage("未知的任务脚本结束")
					DbgPrintf("未知的炼金任务 %d" , tonumber(g_idScript))
					break
				end

				local bUseSkill = false 

				local isNext,szMsg = GetDebugMessage()
				while isNext == 1 do
					if string.find(szMsg, "还没有发现合适的地方来收集任务物品") then
						bUseSkill = true 
					end
					isNext,szMsg = GetDebugMessage()
				end

				if bUseSkill == true then 
					UseSkill("查找资源")
				end
			end
		else
			-------------------------------------------------------------------------------------
			-- 领取任务

			-- 判断活跃值
			local nActivePoint = Player:GetData("ACTIVEPOINT")

			if nActivePoint < 50 then
				if nQuestTimes == 0 then
					ShowMessage("炼金任务：活跃值不足！")
					break
				else
					if nWaitTimes > 3 then 
						ShowMessage("炼金任务：活跃值不足！")
						break
					else
						nWaitTimes = nWaitTimes + 1
						System:Sleep(1000)
						continue
					end
				end 
			end

			if nActivePoint >= 250 then
				g_szMission = "快捷炼金任务"
			else
				g_szMission = "炼金任务"
			end

			-- 进入帮派城市
			if CityToGuild() == false then
				ShowMessage("炼金任务：没有帮派城市！")
				break 
			end

			MoveToNPC(g_NpcInfo[0].fX, g_NpcInfo[0].fY, -1, g_NpcInfo[0].szNpcName)
			Dismount()
			QuestFrameOptionClicked(g_szMission, CMP_FULL_MATCH)	-- 选择选项
			QuestFrameAcceptClicked()

			nWaitTimes = 0
			nQuestTimes = nQuestTimes + 1

		end

		System:Sleep(500)
	end

	GuildToCity(0)
end

_SMain()