ShowMessage("ץ걦Ǯ Ver:1.00")

--[[
[author]
descript=Զץ걦䵺ɳ(84,38)Ǯ
cmd0str=(䵺)ץȸ,ݻ,
cmd0=0
cmd1str=(ʥɽ)ץѩ
cmd1=1
cmd2str=(ʥɽ)ץϬţ
cmd2=2
cmd3str=(ʥɽ)ץ
cmd3=3
cmd4str=(ʥɽ)ץ
cmd4=4
cmd_default=1
]]--

script_name="ץ걦Ǯ"

function select_pet()
	local ass_pet_index = INVALID_ID
	local ass_basic = 0 
	local pet_count = 0 
	local max_slot = GetMyCurMaxPetCount()

	for i = 0 , max_slot - 1 do 
		if Pet:IsPresent(i) ~= true then
			continue 
		end

		pet_count = pet_count + 1
		local name = Pet:GetName(i)
		if string.find( name , "") == 1 then
			local basic = Pet:GetBasic(i)
			local wx = Pet:GetSavvy(i)
			local lx = Pet:GetLixing(i)
			--DbgPrintf("select %d,%d" , wx , lx )
			if wx == 0 and lx == 0 then
				ass_pet_index = i 
				ass_basic = basic
			end	
		end
	end
	
	return ass_pet_index,max_slot-pet_count
end

function OnCatchPet( main_pet_take )
	--ЯȼȡҪɱֵбб
	AI:SetAI("ɱ")

	if main_pet_take <= 0 then
		AI:SetParameter("Ŀ", "", "ȸ")
		AI:SetParameter("Ŀ", "", "꽣ݻ")
		AI:SetParameter("Ŀ", "", "")
		AI:SetParameter("", "", "ȸ")
		AI:SetParameter("", "", "꽣ݻ")
		AI:SetParameter("", "", "")
		AI:SetParameter("ͼ", 112)
		AI:SetParameter("", "", 275 , 173 )
		AI:SetParameter("", "", 272 , 145 )
	elseif main_pet_take <= 1 then
		AI:SetParameter("Ŀ", "", "ѩ")
		AI:SetParameter("", "", "ѩ")
		AI:SetParameter("ͼ", 201)
		AI:SetParameter("", "", 175 , 224 )
		AI:SetParameter("", "", 163 , 223 )
	elseif main_pet_take <= 2 then
		AI:SetParameter("Ŀ", "", "Ϭţ")
		AI:SetParameter("", "", "Ϭţ")
		AI:SetParameter("ͼ", 201)
		AI:SetParameter("", "", 161 , 166 )
		AI:SetParameter("", "", 167 , 177 )
	elseif main_pet_take <= 3 then
		AI:SetParameter("Ŀ", "", "꽣")
		AI:SetParameter("", "", "꽣")
		AI:SetParameter("ͼ", 201)
		AI:SetParameter("", "", 209 , 71 )
		AI:SetParameter("", "", 210 , 94 )
		AI:SetParameter("", "", 186 , 100 )
	else
		AI:SetParameter("Ŀ", "", "")
		AI:SetParameter("", "", "")
		AI:SetParameter("ͼ", 201)
		AI:SetParameter("", "", 32 , 66 )
		AI:SetParameter("", "", 34 , 37 )
	end

	AI:SetParameter("ʰȡ", PM_NOPICKUP)
	AI:Start(false)

	while true do 

		local sid, x, y, die = FindCharacterByName("")
		if sid ~= INVALID_ID and die == 0 then
			local xNow, yNow = Player:GetPos()
			if GetDistSq(xNow, yNow, x, y) < 10 then

				System:OutputDebugString("")

				AI:Stop()
				while Player_GetBuff("") == -1 do
					Pet_Relax()
					UseSkill("")
					System:Sleep(200)
				end

				while Player_GetBuff("") ~= -1 do
					System:Sleep(200)
				end
				break
			end
		end

		local ass_pet_index , pet_slot = select_pet()
		if pet_slot <= 0 then
			break 
		end

		System:Sleep(1000)
	end

	AI:Stop()
end

function _SMain()

	local main_pet_take = 1

	if _CommandLine ~= nil then
		main_pet_take = tonumber(_CommandLine)
	end

	local ass_pet_index , pet_slot = select_pet()
	DbgPrintf("select pet %d,%d" , ass_pet_index , pet_slot )

	if ass_pet_index == INVALID_ID and pet_slot <= 0 then
		ShowMessage("Я,˳ű!")
		return
	end

	while true do 

		System:Sleep(500)

		ass_pet_index , pet_slot = select_pet()

		if ass_pet_index == INVALID_ID and pet_slot <= 0 then
			MoveTo( 252 , 126 , 2 )
			ShowMessage("޷Я,شֹͣһ")
			break 
		end

		DbgPrintf("select pet %d,%d" , ass_pet_index , pet_slot )
		if GetActiveDataSceneID() == 112 and ass_pet_index ~= INVALID_ID then
			--ǰ
			if ass_pet_index ~= INVALID_ID then
				if UI:IsWindowShow("MissionReply") == true then
					local sCmd = string.format("Exchange:AddPet(%d)" , ass_pet_index )
					DoLuaString("",sCmd )
					System:Sleep(1000)
					DoLuaString("MissionReply_Env","MissionReply_Accept_Clicked()" )
					System:Sleep(2000)
				else
					MoveToNPC( 84 , 38, 112 , "ɳ" )
					QuestFrameOptionClicked("Ҫ", CMP_FULL_MATCH)

					System:Sleep(500)
					local bContinue = true
					local isNext,szMsg = GetDebugMessage()
					while isNext == 1 do

						if string.find(szMsg, "ճ޴Ѵ") then
							bContinue = false 
							ShowMessage("޴Ѵޣ")
						end
						isNext,szMsg = GetDebugMessage()
					end

					if bContinue == false then
						break 
					end
				end
				continue
			else
				if UI:IsWindowShow("MissionReply") == true then
					UI:WindowHide()
				end
			end
		else
			if pet_slot > 0 then
				if GetActiveDataSceneID() ~= 201 then
					LuoYangRestore()
				end

				OnCatchPet( main_pet_take )
			elseif ass_pet_index ~= INVALID_ID then
				MoveToNPC( 84 , 38, 112 , "ɳ" )
			end
		end
	end
end

_SMain()