diff --git a/data/events/magikarp_lengths.asm b/data/events/magikarp_lengths.asm index 1a1040d6c68..3a6b9e1ea21 100644 --- a/data/events/magikarp_lengths.asm +++ b/data/events/magikarp_lengths.asm @@ -2,9 +2,7 @@ MagikarpLengths: ; [wMagikarpLength] = z * 100 + (bc - x) / y ; First argument is the bc threshold as well as x. ; Second argument is y. -; In reality, due to the bug at .BCLessThanDE, -; the threshold is determined by only register b. - dwb 110, 1 ; not used unless the bug is fixed + dwb 110, 1 dwb 310, 2 dwb 710, 4 dwb 2710, 20 @@ -17,4 +15,4 @@ MagikarpLengths: dwb 64710, 20 dwb 65210, 5 dwb 65410, 2 - dwb 65510, 1 ; not used + dwb 65510, 1 diff --git a/docs/bugs_and_glitches.md b/docs/bugs_and_glitches.md index 77152efb5c0..25fcc14fdd7 100644 --- a/docs/bugs_and_glitches.md +++ b/docs/bugs_and_glitches.md @@ -2407,6 +2407,8 @@ CopyPokemonName_Buffer1_Buffer3: ### Magikarp lengths can be miscalculated +`CalcMagikarpLength.BCLessThanDE` only compares the high bytes of `bc` and `de`. This causes the first entry of the `MagikarpLengths` table to go unused, and `bc` values between 65,280 and 65,509 to use the wrong formula. As a side effect, the highest possible length is capped at 1,625mm (before being converted to feet and inches). + **Fix:** Edit `CalcMagikarpLength.BCLessThanDE` in [engine/events/magikarp.asm](https://github.com/pret/pokecrystal/blob/master/engine/events/magikarp.asm): ```diff @@ -2414,8 +2416,9 @@ CopyPokemonName_Buffer1_Buffer3: -; BUG: Magikarp lengths can be miscalculated (see docs/bugs_and_glitches.md) ld a, b cp d - ret c +- ret c - ret nc ++ ret nz ld a, c cp e ret diff --git a/engine/events/magikarp.asm b/engine/events/magikarp.asm index 09d75967e5e..d5d2c5ecb54 100644 --- a/engine/events/magikarp.asm +++ b/engine/events/magikarp.asm @@ -108,36 +108,18 @@ CalcMagikarpLength: ; de: wEnemyMonDVs ; bc: wPlayerID -; This function is poorly commented. - -; In short, it generates a value between 190 and 1786 using -; a Magikarp's DVs and its trainer ID. This value is further -; filtered in LoadEnemyMon to make longer Magikarp even rarer. +; It generates a value between 190 and 1755 using a Magikarp's DVs +; and its trainer ID. This value is further filtered in LoadEnemyMon +; to make longer Magikarp even rarer. ; The value is generated from a lookup table. -; The index is determined by the dv xored with the player's trainer id. - -; bc = rrc(dv[0]) ++ rrc(dv[1]) ^ rrc(id) +; The index is determined by the DV xored with the player's trainer ID +; and then stored in bc. -; if bc < 10: [wMagikarpLength] = c + 190 -; if bc ≥ $ff00: [wMagikarpLength] = c + 1370 +; if bc < 10: [wMagikarpLength] = bc + 190 +; if bc ≥ 65510: [wMagikarpLength] = bc - 65510 + 1600 ; else: [wMagikarpLength] = z * 100 + (bc - x) / y -; X, Y, and Z depend on the value of b as follows: - -; if b = 0: x = 310, y = 2, z = 3 -; if b = 1: x = 710, y = 4, z = 4 -; if b = 2-9: x = 2710, y = 20, z = 5 -; if b = 10-29: x = 7710, y = 50, z = 6 -; if b = 30-68: x = 17710, y = 100, z = 7 -; if b = 69-126: x = 32710, y = 150, z = 8 -; if b = 127-185: x = 47710, y = 150, z = 9 -; if b = 186-224: x = 57710, y = 100, z = 10 -; if b = 225-243: x = 62710, y = 50, z = 11 -; if b = 244-251: x = 64710, y = 20, z = 12 -; if b = 252-253: x = 65210, y = 5, z = 13 -; if b = 254: x = 65410, y = 2, z = 14 - ; bc = rrc(dv[0]) ++ rrc(dv[1]) ^ rrc(id) ; id