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 <= 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()