• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Explaining the fishing.lua chance algorithm - TFS 1.2

William Sezo

Rookie but passionate
Joined
May 6, 2016
Messages
38
Reaction score
6
Location
Brazil
Hi!

I'm trying to make it possible for the player to catch other kinds of fish (as well as trash) from the fishing on regular water areas.

I managed to make it possible to fish other stuff, in this case i'm testing with worn boots, fish remains, fish tail, etc.

This is my fishing.lua:

Code:
local waterIds = {493, 4608, 4609, 4610, 4611, 4612, 4613, 4614, 4615, 4616, 4617, 4618, 4619, 4620, 4621, 4622, 4623, 4624, 4625, 7236, 10499, 15401, 15402}
local lootTrash = {2234, 2238, 2376, 2509, 2667}
local lootCommon = {2152, 2167, 2168, 2669, 7588, 7589}
local lootRare = {2143, 2146, 2149, 7158, 7159}
local lootVeryRare = {7632, 7633, 10220}
local useWorms = false

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local targetId = target.itemid
    if not isInArray(waterIds, target.itemid) then
        return false
    end

    if targetId == 10499 then
        local owner = target:getAttribute(ITEM_ATTRIBUTE_CORPSEOWNER)
        if owner ~= 0 and owner ~= player:getId() then
            player:sendTextMessage(MESSAGE_STATUS_SMALL, "You are not the owner.")
            return true
        end

        toPosition:sendMagicEffect(CONST_ME_WATERSPLASH)
        target:remove()

        local rareChance = math.random(1, 100)
        if rareChance == 1 then
            player:addItem(lootVeryRare[math.random(#lootVeryRare)], 1)
            player:say("I'm the luckiest! WOW!!!", TALKTYPE_MONSTER_SAY)
        elseif rareChance <= 3 then
            player:addItem(lootRare[math.random(#lootRare)], 1)
            player:say("Feeling lucky today! Iiiha!!!", TALKTYPE_MONSTER_SAY)
        elseif rareChance <= 10 then
            player:addItem(lootCommon[math.random(#lootCommon)], 1)
            player:say("I Guess it's Ok!", TALKTYPE_MONSTER_SAY)
        else
            player:addItem(lootTrash[math.random(#lootTrash)], 1)
            player:say("Well... Better than nothing...", TALKTYPE_MONSTER_SAY)
        end
        return true
    end

    if targetId ~= 7236 then
        toPosition:sendMagicEffect(CONST_ME_LOSEENERGY)
    end

    if targetId == 493 or targetId == 15402 then
        return true
    end

    player:addSkillTries(SKILL_FISHING, 1)
    if math.random(1, 100) <= math.min(math.max(10 + (player:getEffectiveSkillLevel(SKILL_FISHING) - 10) * 0.597, 10), 50) then
        if useWorms and not player:removeItem("worm", 1) then
            return true
        end

        if targetId == 15401 then
            target:transform(targetId + 1)
            target:decay()

            if math.random(1, 100) >= 97 then
                player:addItem(15405, 1)
                return true
            end
        elseif targetId == 7236 then
            target:transform(targetId + 1)
            target:decay()

            local rareChance = math.random(1, 100)
            if rareChance <= 2 then
                player:addItem(7158, 1)
                player:say("I caught a Rainbow Trout! WoooW!!!", TALKTYPE_MONSTER_SAY)
                return true
            elseif rareChance <= 5 then
                player:addItem(2669, 1)
                player:say("I caught a Northern Pike!!!", TALKTYPE_MONSTER_SAY)
                return true
            elseif rareChance == 1 then
                player:addItem(7963, 1)
                player:say("I caught a Legendary Marlin! Holy shit!!!", TALKTYPE_MONSTER_SAY)
                return true
            elseif rareChance <= 15 then
                player:addItem(7159, 1)
                player:say("I caught a Green Perch!", TALKTYPE_MONSTER_SAY)
                return true
            end
        end
       
        player:addItem("fish", 1)
        player:say("Hooked!", TALKTYPE_MONSTER_SAY)
       
        elseif math.random(1, 100) <= math.min(math.max(5 + (player:getEffectiveSkillLevel(SKILL_FISHING) - 170) * 0.597, 10), 50) then
        if useWorms and not player:removeItem("worm", 1) then
            return true
        end
        player:addItem(2238, 1)
        player:say("Stinky!", TALKTYPE_MONSTER_SAY)
       
        elseif math.random(1, 100) <= math.min(math.max(2 + (player:getEffectiveSkillLevel(SKILL_FISHING) - 150) * 0.597, 10), 50) then
        if useWorms and not player:removeItem("worm", 1) then
            return true
        end
        player:addItem(5951, 1)
        player:say("It will have to go on without a tail...", TALKTYPE_MONSTER_SAY)
       
        elseif math.random(1, 100) <= math.min(math.max(1 + (player:getEffectiveSkillLevel(SKILL_FISHING) - 250) * 0.597, 10), 50) then
        if useWorms and not player:removeItem("worm", 1) then
            return true
        end
        player:addItem(8713, 1)
        player:say("Wow! A glowing fish, must be worth something...", TALKTYPE_MONSTER_SAY)
       
        elseif math.random(1, 100) <= math.min(math.max(1 + (player:getEffectiveSkillLevel(SKILL_FISHING) - 170) * 0.597, 10), 50) then
        if useWorms and not player:removeItem("worm", 1) then
            return true
        end
        player:addItem(2240, 1)
        player:say("Guess I shouldn't have pulled this hard...", TALKTYPE_MONSTER_SAY)
    end
    return true
end

Everything works fine on TFS 1.2, but I can't quite understand the formula

if math.random(1, 100) <= math.min(math.max(1 + (player:getEffectiveSkillLevel(SKILL_FISHING) - 10) * 0.597, 10), 50)

Without understanding of this formula, I'm having trouble setting up the right chance for those new drops, If an expert lua coder could give me a hand with this.

Thank you.
 
Hi!

I'm trying to make it possible for the player to catch other kinds of fish (as well as trash) from the fishing on regular water areas.

I managed to make it possible to fish other stuff, in this case i'm testing with worn boots, fish remains, fish tail, etc.

This is my fishing.lua:

Code:
local waterIds = {493, 4608, 4609, 4610, 4611, 4612, 4613, 4614, 4615, 4616, 4617, 4618, 4619, 4620, 4621, 4622, 4623, 4624, 4625, 7236, 10499, 15401, 15402}
local lootTrash = {2234, 2238, 2376, 2509, 2667}
local lootCommon = {2152, 2167, 2168, 2669, 7588, 7589}
local lootRare = {2143, 2146, 2149, 7158, 7159}
local lootVeryRare = {7632, 7633, 10220}
local useWorms = false

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local targetId = target.itemid
    if not isInArray(waterIds, target.itemid) then
        return false
    end

    if targetId == 10499 then
        local owner = target:getAttribute(ITEM_ATTRIBUTE_CORPSEOWNER)
        if owner ~= 0 and owner ~= player:getId() then
            player:sendTextMessage(MESSAGE_STATUS_SMALL, "You are not the owner.")
            return true
        end

        toPosition:sendMagicEffect(CONST_ME_WATERSPLASH)
        target:remove()

        local rareChance = math.random(1, 100)
        if rareChance == 1 then
            player:addItem(lootVeryRare[math.random(#lootVeryRare)], 1)
            player:say("I'm the luckiest! WOW!!!", TALKTYPE_MONSTER_SAY)
        elseif rareChance <= 3 then
            player:addItem(lootRare[math.random(#lootRare)], 1)
            player:say("Feeling lucky today! Iiiha!!!", TALKTYPE_MONSTER_SAY)
        elseif rareChance <= 10 then
            player:addItem(lootCommon[math.random(#lootCommon)], 1)
            player:say("I Guess it's Ok!", TALKTYPE_MONSTER_SAY)
        else
            player:addItem(lootTrash[math.random(#lootTrash)], 1)
            player:say("Well... Better than nothing...", TALKTYPE_MONSTER_SAY)
        end
        return true
    end

    if targetId ~= 7236 then
        toPosition:sendMagicEffect(CONST_ME_LOSEENERGY)
    end

    if targetId == 493 or targetId == 15402 then
        return true
    end

    player:addSkillTries(SKILL_FISHING, 1)
    if math.random(1, 100) <= math.min(math.max(10 + (player:getEffectiveSkillLevel(SKILL_FISHING) - 10) * 0.597, 10), 50) then
        if useWorms and not player:removeItem("worm", 1) then
            return true
        end

        if targetId == 15401 then
            target:transform(targetId + 1)
            target:decay()

            if math.random(1, 100) >= 97 then
                player:addItem(15405, 1)
                return true
            end
        elseif targetId == 7236 then
            target:transform(targetId + 1)
            target:decay()

            local rareChance = math.random(1, 100)
            if rareChance <= 2 then
                player:addItem(7158, 1)
                player:say("I caught a Rainbow Trout! WoooW!!!", TALKTYPE_MONSTER_SAY)
                return true
            elseif rareChance <= 5 then
                player:addItem(2669, 1)
                player:say("I caught a Northern Pike!!!", TALKTYPE_MONSTER_SAY)
                return true
            elseif rareChance == 1 then
                player:addItem(7963, 1)
                player:say("I caught a Legendary Marlin! Holy shit!!!", TALKTYPE_MONSTER_SAY)
                return true
            elseif rareChance <= 15 then
                player:addItem(7159, 1)
                player:say("I caught a Green Perch!", TALKTYPE_MONSTER_SAY)
                return true
            end
        end
  
        player:addItem("fish", 1)
        player:say("Hooked!", TALKTYPE_MONSTER_SAY)
  
        elseif math.random(1, 100) <= math.min(math.max(5 + (player:getEffectiveSkillLevel(SKILL_FISHING) - 170) * 0.597, 10), 50) then
        if useWorms and not player:removeItem("worm", 1) then
            return true
        end
        player:addItem(2238, 1)
        player:say("Stinky!", TALKTYPE_MONSTER_SAY)
  
        elseif math.random(1, 100) <= math.min(math.max(2 + (player:getEffectiveSkillLevel(SKILL_FISHING) - 150) * 0.597, 10), 50) then
        if useWorms and not player:removeItem("worm", 1) then
            return true
        end
        player:addItem(5951, 1)
        player:say("It will have to go on without a tail...", TALKTYPE_MONSTER_SAY)
  
        elseif math.random(1, 100) <= math.min(math.max(1 + (player:getEffectiveSkillLevel(SKILL_FISHING) - 250) * 0.597, 10), 50) then
        if useWorms and not player:removeItem("worm", 1) then
            return true
        end
        player:addItem(8713, 1)
        player:say("Wow! A glowing fish, must be worth something...", TALKTYPE_MONSTER_SAY)
  
        elseif math.random(1, 100) <= math.min(math.max(1 + (player:getEffectiveSkillLevel(SKILL_FISHING) - 170) * 0.597, 10), 50) then
        if useWorms and not player:removeItem("worm", 1) then
            return true
        end
        player:addItem(2240, 1)
        player:say("Guess I shouldn't have pulled this hard...", TALKTYPE_MONSTER_SAY)
    end
    return true
end

Everything works fine on TFS 1.2, but I can't quite understand the formula

if math.random(1, 100) <= math.min(math.max(1 + (player:getEffectiveSkillLevel(SKILL_FISHING) - 10) * 0.597, 10), 50)

Without understanding of this formula, I'm having trouble setting up the right chance for those new drops, If an expert lua coder could give me a hand with this.

Thank you.
You don't have to be an expert lua coder to understand that, you just gotta understand what the functions do.

math.random(1, 100) -- Let's assume this is a uniform random from 1 to 100 so every value from 1 to 100 has the same chance of being picked.
math.min(a, b) -- Returns the value of the smallest between those 2 (ex: math.min(10, 20) returns 10)
math.max(a, b) -- Returns the value of the biggest between those 2 (ex: math.max(10, 20) returns 20)
getEffectiveSkillLevel(skillId) it will return the effective fishing skill of the player (playerskill + equip bonuses + spell bonuses)

So lets say a player has 50 fishing skill level, you have to replace the getEffectiveSkillLevel for 50
math.min(math.max(1 + (50 - 10) * 0.597, 10), 50) = 24.88

So if the random is uniform between 1 and 100 the chances of this condition being true is 24%, simple math:
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ,16, 17, 18, 19, 20, 21, 22, 23, 24) (24 values) out of 1 - 100 (100 values) = 24 / 100 = 0,24 = 24%

You can then see that the with this math.min(a, 50) the maximum chance is 50% no matter what the player skill.

OcFQD3J.png


f(x) = chance
x = skill,
 
Last edited:
Thanks MatheusMkalo so much for the explanation! But i'm really bad at this, could you give me an example on changing the chance in: math.min(math.max(1 + (X - 10) * 0.597, 10), 50) to lower or higher chances?
 
Thanks MatheusMkalo so much for the explanation! But i'm really bad at this, could you give me an example on changing the chance in: math.min(math.max(1 + (X - 10) * 0.597, 10), 50) to lower or higher chances?
It's math, you just have to know what you want and make a formula that is close enough to what you want. I cannot teach you math :x
 
Back
Top