
--[[
[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 = LV9N
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 = "N" }

	}

	gNameFish[ 10 ] = {
--			 { szName = "" } ,
--			 { szName = "" } ,
			 { szName = "֬" } ,
			 { szName = "N" }

	}


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

	


















