ShowMessage(" Ver:1.00")

--[[
[author]
descript=Զץ걦޵ԣ֧ЯȼΪ75
sCmdEx=_PETLIST_WX0
cmd0str=ѡ޵ 1
cmd0=1
cmd1str=ѡ޵ 2
cmd1=2
cmd2str=ѡ޵ 3
cmd2=3
cmd3str=ѡ޵ 4
cmd3=4
cmd4str=ѡ޵ 5
cmd4=5
cmd5str=ѡ޵ 6
cmd5=6
cmd6str=ѡ޵ 7
cmd6=7
cmd7str=ѡ޵ 8
cmd7=8
cmd8str=ѡ޵ 9
cmd8=9
cmd9str=ѡ޵ 10
cmd9=10
cmd_default=7
]]--

script_name=""


local thisMode = 0 
local thisExStr = ""

local moneyCosts = {
	[0] = 10000,
	[1] = 11000,
	[2] = 12100,
	[3] = 13310,
	[4] = 14641,
	[5] = 16105,
	[6] = 17716,
	[7] = 19487,
	[8] = 21436,
	[9] = 23579,
}


function select_pet( main_pet_wx )
	local ass_pet_index = INVALID_ID
	local ass_basic = 0 
	local drop_pet_index = INVALID_ID
	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)
			DbgPrintf(" %d , Ŀ %d" , basic , thisMode )
			if basic >= main_pet_wx then
				if ass_pet_index == INVALID_ID or basic < ass_basic then
					ass_pet_index = i 
					ass_basic = basic
				end
			else
				drop_pet_index = i 
			end	
		end
	end
	
	return ass_pet_index,drop_pet_index,max_slot-pet_count
end

function select_main_pet()
	
	local idx = INVALID_ID
	local wx = 0 
	local lv = 0 
	local count = 0

	for i = 0 , PET_MAX_NUMBER - 1 do 
		if Pet:IsPresent(i) then
			local name = Pet:GetName(i)
			if name == thisExStr then
				wx = Pet:GetSavvy(i)
				lv = Pet:GetTakeLevel(i)
				idx = i 
				count = count + 1
			end
		end
	end

	-- ,ǰ,Яȼ
	return count , idx , wx , lv
end

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

	if main_pet_take <= 5 then
		AI:SetParameter("Ŀ", "", "깷")
		AI:SetParameter("", "", "깷")
		AI:SetParameter("ͼ", 112)
		AI:SetParameter("", "", 237 , 62 )
		AI:SetParameter("", "", 236 , 45 )
	elseif main_pet_take <= 45 then
		AI:SetParameter("Ŀ", "", "èͷӥ")
		AI:SetParameter("", "", "èͷӥ")
		AI:SetParameter("ͼ", 112)
		AI:SetParameter("", "", 50 , 213 )
		AI:SetParameter("", "", 69 , 221 )
	elseif main_pet_take <= 55 then
		AI:SetParameter("Ŀ", "", "")
		AI:SetParameter("", "", "")
		AI:SetParameter("ͼ", 112)
		AI:SetParameter("", "", 186 , 225 )
	elseif main_pet_take <= 65 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 <= 75 then
		AI:SetParameter("Ŀ", "", "")
		AI:SetParameter("", "", "")
		AI:SetParameter("ͼ", 201)
		AI:SetParameter("", "", 223 , 162 )
		AI:SetParameter("", "", 214 , 146 )
		AI:SetParameter("", "", 217 , 132 )
	else
		ShowMessage("ЯȼΪ 75 ϵޣű֧")
		return
	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 , drop_pet_index , pet_slot = select_pet( main_pet_wx )
		if drop_pet_index ~= INVALID_ID then
			if IsMsgCold("Update_Pet_Drop" , 5000 ) == true then
				local dropCmd = string.format("Pet:Go_Free(%d)" , drop_pet_index ) 
				DoLuaString("" ,tostring(dropCmd))
				UpdateMsg("Update_Pet_Drop")
			end
		elseif pet_slot <= 0 then
			break 
		end

		System:Sleep(1000)
	end

	AI:Stop()
end

function _SMain()

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

	if _CommandExStr ~= nil then
		thisExStr = tostring(_CommandExStr)
	end

	ShowMessage(" %s  %d" , thisExStr , thisMode )

	while true do 

		System:Sleep(1000)

		local main_s,main_pet_index,main_pet_wx,main_pet_take = select_main_pet()
		if main_s > 1 then
			ShowMessage("ָ %s ظ,ʹΨһ!" ,  tostring(thisExStr) )
			return
		end
		
		if main_pet_index == INVALID_ID then
			ShowMessage("Ҳָ %s" , tostring(thisExStr))
			return 
		end

		if main_pet_take > 75 then
			ShowMessage("ЯȼΪ 75 ϵޣű֧")
			return	
		end	

		if main_pet_wx >= thisMode then
			ShowMessage("%s Ѿﵽ %d,˳ű" , tostring(thisExStr) , tonumber(thisMode))
			return
		end

		if main_pet_wx <= 9 then
			local jz = Player:GetData("MONEY_JZ")
			if jz < moneyCosts[main_pet_wx] then
				ShowMessage("ϵĽӲ,Լ,˳ű")
				return
			end
		end

		local ass_pet_index , drop_pet_index , pet_slot = select_pet( main_pet_wx )


		if drop_pet_index ~= INVALID_ID then
			if IsMsgCold("Update_Pet_Drop" , 5000 ) == true then
				local dropCmd = string.format("Pet:Go_Free(%d)" , drop_pet_index ) 
				DoLuaString("" ,tostring(dropCmd))
				UpdateMsg("Update_Pet_Drop")
			end
			continue
		end

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

		DbgPrintf("select pet %d,%d,%d" , ass_pet_index , drop_pet_index , pet_slot )
		if GetActiveDataSceneID() == 1 and ass_pet_index ~= INVALID_ID then
			--ǰ
			Pet_Relax()

			if ass_pet_index ~= INVALID_ID then
				if UI:IsWindowShow("PetSavvy") == true then
					if IsMsgCold("Update_Pet_WX",12000) == true then
						local sCmd = string.format("PetSavvy_Choose_Clicked(\"main\");PetSavvy_SelectPet(%d)" , main_pet_index )
						DoLuaString("PetSavvy_Env",sCmd)
						local sCmd_2 = string.format("PetSavvy_Choose_Clicked(\"assis\");PetSavvy_SelectPet(%d)" , ass_pet_index )
						DoLuaString("PetSavvy_Env",sCmd_2 )
						System:Sleep(1000)
						DoLuaString("PetSavvy_Env","PetSavvy_OK_Clicked()" )
						UpdateMsg("Update_Pet_WX")
						System:Sleep(2000)
					else
						ShowMessage("Ϣһ..")
					end
				else
					MoveToNPC( 173 , 238, 1 , "" )
					QuestFrameOptionClicked("ó", CMP_FULL_MATCH)
				end
				continue
			else
				if UI:IsWindowShow("PetSavvy") == true then
					UI:WindowHide()
				end
			end
		else
			if pet_slot > 0 then
				if GetActiveDataSceneID() == 1 then
					LuoYangRestore()
				end

				OnCatchPet( main_pet_wx , main_pet_take )
			else
				MoveToNPC( 173 , 238, 1 , "" )
				QuestFrameOptionClicked("ó", CMP_FULL_MATCH)
			end
		end
	end
end

_SMain()