tModLoader [Snippet] Fishing catch quality calculator

Flashkirby99

Skeletron
If you're doing anything fishing related and you want, rather than a flat random check, a more faithfhul check using the fishing power of the player, here's a little bit of code I wrote based on the in-game code and the description from the wiki, this goes in your ModPlayer:

Code:
public override void CatchFish(Item fishingRod, Item bait, int power, int liquidType, int poolSize, int worldLayer, int questFish, ref int caughtType, ref bool junk)
{
    if (junk) return; // Don't do stuff if the catch is a junk catch

    bool common, uncommon, rare, veryrare, superrare, isCrate;
    calculateCatchRates(power, out common, out uncommon, out rare, out veryrare, out superrare, out isCrate);

    if (liquidType != 0) return; // Uness it's special, all fish are caught in water only

    // Do catch stuff here
    if (uncommon)
    {
        caughtType = ItemID.Worm;
    }
}


/// <summary>
/// Calculate the base catch rates for different tiers of fish. Parameter chances are shown at 50% fishing power. Examples of fish at each tier, plus individual catch rates:
/// <para> Common: Neon Tetra, Crimson Tigerfish, Atlantic Cod, Red Snapper (1/2)</para>
/// <para> Uncommon: Damselfish, Frost Minnow, Ebonkoi</para>
/// <para> Rare: Honeyfin, Prismite, Purple Clubberfish</para>
/// <para> Very Rare: Sawtooth Shark, Flarefin Koi, Golden Crate</para>
/// <para> Extremely Rare: Obsidian Swordfish, Toxikarp (1/2),  Bladetongue (1/2), Balloon Pufferfish (1/5), Zephyr Fish (1/10)</para>
/// If all else fails, Terraria rewards the player with a Bass (or Trout in the ocean).
/// </summary>/
/// <param name="power">The fishing skill. </param>
/// <param name="common">33.3% = power:150 (capped 1:2). /</param>
/// <param name="uncommon">16.7% = power:300 (capped 1:3). </param>
/// <param name="rare">4.8% = power:1050 (capped 1:4). </param>
/// <param name="veryrare">2.2% = power:2250 (capped 1:5). </param>
/// <param name="superrare">1.1% = power:4500 (capped 1:6). </param>
/// <param name="isCrate">1:10, 1:5 with crate potion. </param>
public void calculateCatchRates(int power, out bool common, out bool uncommon, out bool rare, out bool veryrare, out bool superrare, out bool isCrate)
{
    common = false;
    uncommon = false;
    rare = false;
    veryrare = false;
    superrare = false;
    isCrate = false;

    if (power <= 0) return;

    if (Main.rand.Next(Math.Max(2, 150 * 1 / power)) == 0)
    { common = true; }
    if (Main.rand.Next(Math.Max(3, 150 * 2 / power)) == 0)
    { uncommon = true; }
    if (Main.rand.Next(Math.Max(4, 150 * 7 / power)) == 0)
    { rare = true; }
    if (Main.rand.Next(Math.Max(5, 150 * 15 / power)) == 0)
    { veryrare = true; }
    if (Main.rand.Next(Math.Max(6, 150 * 30 / power)) == 0)
    { superrare = true; }
    if (Main.rand.Next(100) < (10 + (player.cratePotion ? 10 : 0)))
    { isCrate = true; }
}

It is worth noting that crates are rolled before and take prioriy over most fish. For better behaviour I would suggest making a check for if the "caughtType" is a crate item id, to avoid messing too much with vanilla catch chances.

Catches that are rolled before crates (and thus override chances for a crate):
  • Any fish caught in lava
    • 2315 - Obsidifish
    • 2312 - Flarefin Koi
    • 2331 - Obsidian Swordfish
  • Any fish caught in honey
    • 2451 - Bumblebee Tuna (quest fish)
    • 2314 - Honeyfin
  • Junk
    • 2337 - Old Shoe
    • 2338 - Fishing Seaweed
    • 2339 - Tin Can

Other useful links:
Catch mechanics
ExampleMod quest fish
ExampleMod mod player
tModloader documentation on CatchFish method
Method output on rextester

PS: Should I post more of snippets of random mechanics (and if so what)?
 
Last edited:
Back
Top Bottom