- 2
- Posts
- 1
- Years
- Any pronouns
- Seen Aug 8, 2023
Apologies if my usage of coding terminology here is incorrect. I have a very rudimentary understanding of coding outside of VBA.
Recently I've been trying to gain a better technical understanding of the Gen 3 trainer AI by looking at the pokefirered disassembly. More specifically, right now I'm focusing on the opening rival fight; I wanted to know the technical logic behind why the rival seems to prioritize attacking over Growl or Tail Whip. For now, I'll focus on Growl (for my testing I had the rival choose Charmander).
From what I can tell, the rival is controlled by three AI scripts: AI_CheckBadMove, AI_CheckViability, and AI_TryToFaint, found under battle_ai_scripts.s in the disassembly. The last is relevant on turns where the rival can knock the player out, but I'd like to focus on the second script, AI_CheckViability.
The "priority score" for Growl appears to be affected by the script, AI_CBM_AttackDown, copied below:
To better understand it, I tried replicating the logic of this code visually (apologies for poor resolution, originally it was just for personal use):
https://imgur.com/a/7iz7SGr
In the diagram, the "blue" lines represent "yes" to the above question, "red" represents no, and a slash through the line means RNG is used to determine if the entire script just ends there.
Based on my interpretation of the code, the AI shouldn't prioritize attacking over lowering Attack if 1) the opponent's attack stat isn't altered, 2) the opponent has over 70% health, and 3) the opponent has a type that's STAB moves would be physical. To test that, I used PKHex to change my starter to a Level 100 Surskit, and ran the fight while spamming Haze the entire time; when the opponent ran out of a move, I'd restart the fight. From this test, I found that Charmander used Scratch and Growl at a ratio of 53:30, which confuses me. Shouldn't the ratio be 50:50, if my above understanding of the code is correct?
Tl;dr is my understanding of the attack modification AI code accurate, and if so, why aren't Scratch and Growl equally weighted in my simulation?
There's a ton of questions I could ask about the trainer AI code, but I felt focusing on this small instance first would be a good start. Any help would be appreciated.
Recently I've been trying to gain a better technical understanding of the Gen 3 trainer AI by looking at the pokefirered disassembly. More specifically, right now I'm focusing on the opening rival fight; I wanted to know the technical logic behind why the rival seems to prioritize attacking over Growl or Tail Whip. For now, I'll focus on Growl (for my testing I had the rival choose Charmander).
From what I can tell, the rival is controlled by three AI scripts: AI_CheckBadMove, AI_CheckViability, and AI_TryToFaint, found under battle_ai_scripts.s in the disassembly. The last is relevant on turns where the rival can knock the player out, but I'd like to focus on the second script, AI_CheckViability.
The "priority score" for Growl appears to be affected by the script, AI_CBM_AttackDown, copied below:
Code:
AI_CV_AttackDown::
if_stat_level_equal AI_TARGET, STAT_ATK, 6, AI_CV_AttackDown3
score -1
if_hp_more_than AI_USER, 90, AI_CV_AttackDown2
score -1
AI_CV_AttackDown2::
if_stat_level_more_than AI_TARGET, STAT_ATK, 3, AI_CV_AttackDown3
if_random_less_than 50, AI_CV_AttackDown3
score -2
AI_CV_AttackDown3::
if_hp_more_than AI_TARGET, 70, AI_CV_AttackDown4
score -2
AI_CV_AttackDown4::
get_target_type1
if_in_bytes AI_CV_AttackDown_PhysicalTypeList, AI_CV_AttackDown_End
get_target_type2
if_in_bytes AI_CV_AttackDown_PhysicalTypeList, AI_CV_AttackDown_End
if_random_less_than 50, AI_CV_AttackDown_End
score -2
AI_CV_AttackDown_End::
end
@ Missing Poison, Flying, and Ghost for unknown reason
AI_CV_AttackDown_PhysicalTypeList::
.byte TYPE_NORMAL
.byte TYPE_FIGHTING
.byte TYPE_GROUND
.byte TYPE_ROCK
.byte TYPE_BUG
.byte TYPE_STEEL
.byte -1
To better understand it, I tried replicating the logic of this code visually (apologies for poor resolution, originally it was just for personal use):
https://imgur.com/a/7iz7SGr
In the diagram, the "blue" lines represent "yes" to the above question, "red" represents no, and a slash through the line means RNG is used to determine if the entire script just ends there.
Based on my interpretation of the code, the AI shouldn't prioritize attacking over lowering Attack if 1) the opponent's attack stat isn't altered, 2) the opponent has over 70% health, and 3) the opponent has a type that's STAB moves would be physical. To test that, I used PKHex to change my starter to a Level 100 Surskit, and ran the fight while spamming Haze the entire time; when the opponent ran out of a move, I'd restart the fight. From this test, I found that Charmander used Scratch and Growl at a ratio of 53:30, which confuses me. Shouldn't the ratio be 50:50, if my above understanding of the code is correct?
Tl;dr is my understanding of the attack modification AI code accurate, and if so, why aren't Scratch and Growl equally weighted in my simulation?
There's a ton of questions I could ask about the trainer AI code, but I felt focusing on this small instance first would be a good start. Any help would be appreciated.