Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug due to variable overflow when AI chooses new Pokemon to send out #3059

Closed
Pawkkie opened this issue Jun 17, 2023 · 0 comments · Fixed by #3068
Closed

Bug due to variable overflow when AI chooses new Pokemon to send out #3059

Pawkkie opened this issue Jun 17, 2023 · 0 comments · Fixed by #3068
Labels
bug Bug category: battle-ai Pertains to Battle Engine Upgrade's AI status: unconfirmed This bug has not been reproduced yet

Comments

@Pawkkie
Copy link

Pawkkie commented Jun 17, 2023

Description

In the GetBestMonTypeMatchup function in battle_ai_switch_items.c (highlighted here ), we calculate the most suitable defense mon to use in the AI's decision of what mon to send out after the previous one faints. Was having some issues (you can see my Discord post and bug discovery here and here), and found this.

The typeEffectiveness variable seems to be overflowing, and that overflow is causing the AI to make incorrect decisions.

I looked into GetTypeModifier, which returns an effectiveness value from sTypeEffectivenessTable, which stores type effectiveness in UQ_4_12 format, seen here. The conversion to UQ_4_12 is here. This format scales input numbers by 4096.

The typeEffectiveness variable I linked in my previous post is u32, which has a max value of just under 4.3 x 10^9. typeEffectiveness is initialized to UQ_4_12(1.0), which is 4096 in decimal. Any neutral result from GetTypeModifier multiplies by 4096, any resisted value multiplies by 2048, and any effective value multiplies by 8192.

This means typeEffectiveness overflows as soon as you hit a case where either mon has two typings unless there's an immunity, because even the smallest multiplier in a two typing case is 4096 * 2048 * 2048, about 1.7 * 10 ^10, and much bigger than typeEffectiveness can store. As far as I can tell, the default behaviour is functionally random for cases where mons have multiple types because it sort of is, depending on how the compiler is handling the overflow.

Version

1.5.0 (Default)

Upcoming Version

No response

Discord contact info

@Pawkkie

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug category: battle-ai Pertains to Battle Engine Upgrade's AI status: unconfirmed This bug has not been reproduced yet
Projects
None yet
1 participant