Module:CalcEncounters

--How to use it: --Paste the following function into any page: -- --example: --There is some page (e.g. Deep Viridian Forest) and pokemon that you can encounter there, for e.g. 4 extremly rare, 6 rare, 7 uncommon and 4 common --If you want to calculate the encounter ratio of one of the rare pokemon for normal member (not gold) you use: -- --why?: --The first number is 3 - it is a ID of RARE pokemon, this is a full list of IDs: --1 - common --2 - uncommon --3 - rare --4 - very rare --5 - extremly rare --6 - legendary

--Next number: 6 - this is the number of possible RARE pokemon (in the table is 6 rare pokemon that can encouter in Deep Viridian Forest) --Next number: 0 - we want to get ratio for normal member, if you want to get ratio for gold member put here 1 --The last is: 1,1,1,0,1,0 (6 number split by ,) --This is a possibility of encounter at least one pokemon from each encounter type in order: common,uncommon,rare,very rare, extremly rare, legendary --I said at the beggining that there was 4 common pokemon: so, at the first place you put 1 --Then 7 uncommon (at least one uncommon), so put 1 --6 rare, so next put 1 --there isn't a possibility to encounter very rare pokemon, so put there 0 --then 4 extremly rare: so next number is 1 --no legendary pokemon, so put then 0

--Another example: --There is a table for land where you can encounter: 5 common, 3 very rare and 1 legendary pokemon --We want to get an encounter ratio for one of the very rare pokemon for gold member --This is a function you should use (check yourself): -- --If you still do not understand how this is works, write to me, here by this page (username: Marcin7848) or by pokemon planet forum - my name Warmix.

--The result of using this function will be e.g.: 98,121% --The algorithm bases on pokemon chances got from pokemon planet forum.

local p = {}

function p.main(frame) local pokemonRarityID = tonumber(frame.args[1]); local numberOfGivenPokemonRarity = tonumber(frame.args[2]); local isGoldMemberRarity = tonumber(frame.args[3]); local pokemonEncountersString = frame.args[4]; local pokemonChances = {1, 12, 60, 600, 5000, 125000} -- 1 / X   local possiblePokemonToEncounter={} for str in string.gmatch(pokemonEncountersString, "([^,]+)") do       table.insert(possiblePokemonToEncounter, str) end local pokemonEncounterChances={} local maxPossiblePokemonEncounterID = 0 for i=1,6 do       if tonumber(possiblePokemonToEncounter[i]) == 1 then maxPossiblePokemonEncounterID = i       end local pokemonChancesTable = {} for j=i,1,-1 do           local isPossibleToEncounter = possiblePokemonToEncounter[j] local chance = 0 if j==i then chance = tonumber(isPossibleToEncounter) * 100 / pokemonChances[i] else chance = pokemonEncounterChances[i-1][j] - (pokemonEncounterChances[i-1][j] * pokemonChancesTable[1] / 100) end table.insert(pokemonChancesTable, chance) end p.reverseArray(pokemonChancesTable) table.insert(pokemonEncounterChances, pokemonChancesTable) end if isGoldMemberRarity == 1 then if pokemonRarityID == 1 then local rarityCommonSum = 100 for i=maxPossiblePokemonEncounterID,2,-1 do               local goldMemberBonus = pokemonEncounterChances[maxPossiblePokemonEncounterID][i] + pokemonEncounterChances[maxPossiblePokemonEncounterID][i] * 50 / 100; rarityCommonSum = rarityCommonSum - goldMemberBonus; end pokemonEncounterChances[maxPossiblePokemonEncounterID][1] = rarityCommonSum else pokemonEncounterChances[maxPossiblePokemonEncounterID][pokemonRarityID] = pokemonEncounterChances[maxPossiblePokemonEncounterID][pokemonRarityID] + pokemonEncounterChances[maxPossiblePokemonEncounterID][pokemonRarityID] * 50 / 100; end end local result = pokemonEncounterChances[maxPossiblePokemonEncounterID][pokemonRarityID] / numberOfGivenPokemonRarity local mult = 10^(3 or 0) result = math.floor(result * mult + 0.5) / mult return result .. "%" end

function p.reverseArray(arr) local i, j = 1, #arr

while i < j do		arr[i], arr[j] = arr[j], arr[i]

i = i + 1 j = j - 1 end end

return p