
--[[
[author]

cmd0str = LV1：草鱼
cmd0 = 0

cmd1str = LV1：青鱼
cmd1 = 1


cmd2str = LV2：鲫鱼
cmd2 = 2

cmd3str = LV2：箭鱼
cmd3 = 3

cmd4str = LV3：鲢鱼
cmd4 = 4

cmd5str = LV3：鲈鱼
cmd5 = 5


cmd6str = LV4：平鱼
cmd6 = 6

cmd7str = LV4：扁头鱼
cmd7 = 7

cmd8str = LV5：鳝鱼
cmd8 = 8

cmd9str = LV5：油力鱼
cmd9 = 9


cmd10str = LV6：黑鱼
cmd10 = 10

cmd11str = LV6：秋刀鱼
cmd11 = 11


cmd12str = LV7：斗鱼
cmd12 = 12

cmd13str = LV7：梭鱼
cmd13 = 13


cmd14str = LV8：鲳鱼
cmd14 = 14

cmd15str = LV8：光背鱼
cmd15 = 15


cmd16str = LV9：银鱼
cmd16 = 16

cmd17str = LV9：钩鱼
cmd17 = 17

cmd18str = LV9：胭脂鱼
cmd18 = 18

cmd19str = LV9：翘嘴红鮊
cmd19 = 19


cmd20str = LV10：鲑鱼
cmd20 = 20

cmd21str = LV10：摆鳍鱼
cmd21 = 21

cmd_default = 0

]]--

script_name = "钓鱼"

local gIndexName = 0 

local gPos = {}

	
	gPos[ 0x06 ] = { 
			{ x = 175 , y = 208 } ,
			{ x = 161 , y = 221 } 
}
	gPos[ 0x07 ] = { 
			{ x = 60 , y = 60 } , 
			{ x = 88 , y = 220 } 
}
	gPos[ 0x08 ] = { 
			{ x = 223 , y = 166 } , 
			{ x = 251 , y = 203 } 
}
	gPos[ 0x05 ] = { 
			{ x = 52 , y = 120 } , 
			{ x = 146 , y = 188 } 
}
	gPos[ 0x03 ] = { -- loi tai day
			{ x = 146 , y = 196 } , 
			{ x = 172 , y = 173 } 
}
	gPos[ 0x04 ] = { 
			{ x = 94 , y = 147 } , 
			{ x = 96 , y = 158 } 
}



	gPos[ 0x1e ] = { 
			{ x = 128 , y = 93 } , 
			{ x = 132 , y = 95 }
}



	gPos[ 0x18 ] = { 
			{ x = 243 , y = 199 } , 
			{ x = 252 , y = 199 } 
}



	gPos[ 0x12 ] = { 
			{ x = 170 , y = 186 } , 
			{ x = 193 , y = 183 } 
}



	gPos[ 0x1f ] = { 
			{ x = 45 , y = 218 } , 
			{ x = 60 , y = 283 } 
}



	gPos[ 0x19 ] = { 
			{ x = 238 , y = 75 } , 
			{ x = 229 , y = 75 } 
}



	gPos[ 0x13 ] = { 
			{ x = 65 , y = 221 } , 
			{ x = 41 , y = 47 } 
}
	gPos[ 0x20 ] = { 
			{ x = 102 , y = 237 } , 
			{ x = 102 , y = 238 }
}




	gPos[ 0x1a ] = { 
			{ x = 212 , y = 105 } , 
			{ x = 258 , y = 169 } 	
}
	gPos[ 0xf5 ] = { 
			{ x = 107 , y = 69 } , 	
			{ x = 98 , y = 47 }  	
}
	gPos[ 0xf7 ] = { 
			{ x = 47 , y = 193 } , 	
			{ x = 50 , y = 190 } 	
}
	gPos[ 0xf4 ] = { 
			{ x = 166 , y = 119 } , 
			{ x = 170 , y = 119 } 	
}



	gPos[ 0x14 ] = { 
			{ x = 218 , y = 168 } , 
			{ x = 220 , y = 172 } 
}


local gSceneLV = {}

	gSceneLV[ 0 ] = { 
			{ nScenceID = 0x06 } ,
			{ nScenceID = 0x07 } ,
			{ nScenceID = 0x08 } ,
			{ nScenceID = 0x05 } ,
			{ nScenceID = 0x03 } ,
			{ nScenceID = 0x04 }

	}

	gSceneLV[ 1 ] = { 
			{ nScenceID = 0x1e }
	}

	gSceneLV[ 2 ] = { 
			{ nScenceID = 0x18 }
	}

	gSceneLV[ 3 ] = { 
			{ nScenceID = 0x12 }
	}

	gSceneLV[ 4 ] = { 
			{ nScenceID = 0x1f }
	}

	gSceneLV[ 5 ] = { 
			{ nScenceID = 0x19 }
	}

	gSceneLV[ 6 ] = { 
			{ nScenceID = 0x13 }
	}

	gSceneLV[ 7 ] = { 
			{ nScenceID = 0x20 }
	}

	gSceneLV[ 8 ] = { 
			{ nScenceID = 0x1a } ,

	}
	gSceneLV[ 10 ] = { 

			{ nScenceID = 0xf5 } ,
			{ nScenceID = 0xf7 } ,
			{ nScenceID = 0xf4 }
	}



	gSceneLV[ 9 ] = { 
			 { nScenceID = 0x14 }
	}

local gNameFish = {}

	gNameFish[ 0 ] = {
			 { szName = "草鱼" } ,
			 { szName = "青鱼" }
	}

	gNameFish[ 1 ] = {
			 { szName = "鲫鱼" } ,
			 { szName = "箭鱼" }

	} 

	gNameFish[ 2 ] = {
			 { szName = "鲢鱼" } ,
			 { szName = "鲈鱼" }
	}

	gNameFish[ 3 ] = {
			 { szName = "平鱼" } ,
			 { szName = "扁头鱼" }
	}

	gNameFish[ 4 ] = {
			 { szName = "鳝鱼" } ,
			 { szName = "油力鱼" } 
	}

	gNameFish[ 5 ] = {
			 { szName = "黑鱼" } ,
			 { szName = "秋刀鱼" }
	}

	gNameFish[ 6 ] = {
			 { szName = "斗鱼" } ,
			 { szName = "梭鱼" }
	}

	gNameFish[ 7 ] = {
			 { szName = "鲳鱼" } ,
			 { szName = "光背鱼" }
	}

	gNameFish[ 8 ] = {
			 { szName = "银鱼" } ,
			 { szName = "钩鱼" } ,
--			 { szName = "胭脂鱼" } ,
--			 { szName = "翘嘴红鮊" }

	}

	gNameFish[ 10 ] = {
--			 { szName = "银鱼" } ,
--			 { szName = "钩鱼" } ,
			 { szName = "胭脂鱼" } ,
			 { szName = "翘嘴红鮊" }

	}


	gNameFish[ 9 ] = {
			 { szName = "鲑鱼" } ,
			 { szName = "摆鳍鱼" } 
	}



function isFullBag( nID )
	
	local nResult = PlayerPackage:GetEmptySlot( nID )

	if nResult == 0 then
		ShowMessage( "包已满" )
		return true
	end

	return false
end




local nTime = System:GetTickCount()


function getFishID( nLevel , indexNameFish )

	local fishID = -1
	local pos = {}

	pos = gNameFish[ nLevel ]	

	if math.mod( indexNameFish , 2 ) ~= 0 then
		indexNameFish = 1
	else
		indexNameFish = 2
	end

	local xNow, yNow = Player:GetPos()


	local infoObj = { { id , x , y , dist } }

	local n = EnumObject(true)

	for i = 0 , n - 1 do

		local idObj, classObj, nameObj, xObj, yObj = EnumObject( false, i )

		if classObj == "GROW" and nameObj == pos[ indexNameFish ].szName then

			infoObj[ i + 1 ].id = idObj
			infoObj[ i + 1 ].x = xObj
			infoObj[ i + 1 ].y = yObj
			infoObj[ i + 1 ].dist = GetDist( xNow , yNow , xObj , yObj )
			
		end
	end

	local nDist = 0 ;	
	local nIndex = 0 ;

	for i = 1 , table.getn( infoObj ) - 1 do

		nDist = infoObj[ i ].dist

		for j = i + 1 , table.getn( infoObj ) do
			if( pos.dist < nDist ) then
				nDist = dist
				nIndex = j
			end
		end
	end


	return infoObj[ nIndex ].id , infoObj[ nIndex ].x , infoObj[ nIndex ].y

end


function gotoSceneId( nSceneID , nLevel )
	
	if gPos[ nSceneID ] == nil then 
		return false
	end

	local posMap = gPos[ nSceneID ]

	local pos = {}
	if gIndexName == 1 or gIndexName == 3 then
		pos = posMap[ 1 ]
	elseif gIndexName == 2 or gIndexName == 4 then
		pos = posMap[ 2 ]
	end

		MoveTo( pos.x , pos.y , nSceneID )

		local nFishID , xFish , yFish
		for i = 1 , 3 do

			nFishID , xFish , yFish = getFishID( nLevel ,  gIndexName )

			if nFishID ~= -1 then
				break
			else
				System:Sleep( 3000 )
			end

		end

		if nFishID == -1 then
			ShowMessage( "无法获取任何鱼的资源信息" )
			return
		end

		local nStop = false
		while nStop == false do

			MoveTo( xFish , yFish , nSceneID , "" , 2 , true )

			OpenPackage( nFishID )

			if isFullBag( 1 ) == true then	
			
				ShowMessage( "包满,回城并停止挂机" )
				MoveTo( 253 , 125, 2 )
				nStop = true
				return false
			end

			System:Sleep( 2000 )
		end

	return true

	
end


function gotoFish( nLevel )


	if gSceneLV[ nLevel ] == nil then 
		return
	end
	
	for i , pos in pairs( gSceneLV[ nLevel ] ) do
	
		if gotoSceneId( pos.nScenceID , nLevel ) == false then
			return
		end
		if nLevel == 8 then
			gIndexName = gIndexName + 2 
		end
	end

end

function _SMain( ... )

	local nLevel = 0
	local nIndex = 0
	
	if _CommandLine ~= nil then 
		nIndex = tonumber( _CommandLine )
	end

	if nIndex == 0 or nIndex == 1 then
		nLevel = 0
	elseif nIndex == 2 or nIndex == 3 then
		nLevel = 1
	elseif nIndex == 4 or nIndex == 5 then
		nLevel = 2
	elseif nIndex == 6 or nIndex == 7 then
		nLevel = 3
	elseif nIndex == 8 or nIndex == 9 then
		nLevel = 4
	elseif nIndex == 10 or nIndex == 11 then
		nLevel = 5
	elseif nIndex == 12 or nIndex == 13 then
		nLevel = 6
	elseif nIndex == 14 or nIndex == 15 then
		nLevel = 7
	elseif nIndex == 16 or nIndex == 17 then -- or nIndex == 18 or nIndex == 19 then
		nLevel = 8
	elseif nIndex == 18 or nIndex == 19 then
		nLevel = 10
	elseif nIndex == 20 or nIndex == 21 then
		nLevel = 9
	end

	local nSkillLV = Player:GetAbilityInfo( 9 , "level" )	
	if nSkillLV == nil then
		ShowMessage( "您还没有学钓鱼技能!" )
		return
	end

	if Player:GetData( "LEVEL" ) < 10 then
		ShowMessage( "您的等级小于10级,还不能够钓鱼!" )
		return 
	end

	if isFullBag( 1 ) == true then
		return
	end

	if nLevel == 10 then
		if nSkillLV < 9 then
			ShowMessage( "你的钓鱼等级不足以钓该种类的鱼" )
			return
		end

	else		
		if nSkillLV < nLevel + 1 then
			ShowMessage( "你的钓鱼等级 %d, 不足以钓该种类的鱼" , nSkillLV )
			return
		end
	end

	if nIndex == 18 then
		gIndexName = 3
	elseif nIndex == 19 then
		gIndexName = 4
	else
		if math.mod( nIndex , 2 ) == 0 then
			gIndexName = 1
		else
			gIndexName = 2
		end
	end
	
	local bTool = false
			
	if DataPool:UserBag_CountItemByName("钓鱼竿") < 1 then
		if DataPool:UserBag_CountItemByName("太公钓") < 1 then

			ShowMessage( "Go buy itiem: 钓鱼竿" )
			MoveToNPC( 306 , 324 ,  0x00 , "姜渔" )	
			
			while isFullBag( 0 ) do
				System:Sleep( 3000 )
			end			

			for i = 1 , 3 do

				QuestFrameOptionClicked( "购买工具" , CMP_PART_MATCH )
				ShopBuy( 10100031 , 1 )

				if DataPool:UserBag_CountItemByName("钓鱼竿") > 0 or DataPool:UserBag_CountItemByName("太公钓") > 0 then
					UI:WindowHide()
					bTool = true
					break
				else
					System:Sleep( 3000 )
				end
			end
		else
			bTool = true
		end
	else
		bTool = true
	end
	

	if bTool == true then

		gotoFish( nLevel )
	else
		ShowMessage( "包内无鱼竿物品,停止挂机!" )
		return
	end

end

_SMain()

	


















