ShowMessage("ÿվ۱ű $Rev: 1 $")

--[[
[author]
class=3
]]--

local g_szJuBaoPen = ""
local g_Position = {
{52,89},{100,147},{147,90}
}

--ɱʯ
--Ͷ뱦ʯ

function GetJuBaoPenName()
	local Id,szName = LockNearestTarget("۱",CHA_TYPE_NPC|CHA_RELATION_FRIEND ,CMP_PART_MATCH)
	
	if Id == -1 then
		return ""
	end
	return szName
end

function OnProcedure()
	
	-- жǷڵ
	if Player_GetBuff("ԭʯ") ~= -1 then
		MoveTo(100, 79, -1, "", 3, false)

		if g_szJuBaoPen == "" then
			MoveTo(100, 79, -1)
			g_szJuBaoPen = GetJuBaoPenName()
			return true
		end

		local Id = FindCharacterByName(g_szJuBaoPen)
		Player_UseSkill_Collect(3107, Id)
		return true
	end

	-- жǷ
	if Player_GetBuff("") == -1 then

		if g_szJuBaoPen == "" then

			MoveTo(100, 79, -1)
			g_szJuBaoPen = GetJuBaoPenName()

		else

			Dismount()
			MoveToNPC(100, 79, -1, g_szJuBaoPen)

			if Player_GetBuff("ԭʯ") ~= -1 or Player_GetBuff("") ~= -1 then
				return true
			end

			if FindQuestFrameOption("Ҫ", CMP_FULL_MATCH) then
				
				if Guild:GetGuildContri() < 50 then
					ShowMessage("ṱ׶50")
					return false
				end

				QuestFrameOptionClicked("Ҫ", CMP_FULL_MATCH)
				QuestFrameOptionClicked("״α", CMP_FULL_MATCH)
				--System:Sleep(1000)
				return ture

			elseif FindQuestFrameOption("ٴα", CMP_FULL_MATCH) == false then

				QuestFrameOptionClicked("ȡľ", CMP_FULL_MATCH)
				QuestFrameOptionClicked("ֱȡľ", CMP_FULL_MATCH)
				System:Sleep(1500)

				local isNext,szMsg = GetDebugMessage()
				while isNext == 1 do
					if string.find(szMsg, "ȡľ") then
						return false
					end
					isNext,szMsg = GetDebugMessage()
				end

				return ture
			end

			QuestFrameOptionClicked("ٴα", CMP_FULL_MATCH)
			--System:Sleep(1000)
		end

		return ture
	end

	-- ԭʯ
	--[[
	math.randomseed(os.time())
	local idx = math.random(1, 3)

	if g_Position[idx] == nil then
		ShowMessage("ݴ")
		return false
	end
	]]--
	
	MoveTo(g_Position[2][1], g_Position[2][2], -1, "", 4, false)
	Player_UseSkill_Collect(3106)

end

function _SMain(...)

	while true do

		-- ɳ
		if CityToGuild() == false then
			ShowMessage("ûаɳУ")
			break 
		end

		if OnProcedure() == false then
			break
		end

		System:Sleep(500)
	end

	GuildToCity(0)
end

_SMain()