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

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