Trainer AI

Post Reply
entrpntr
Pokémon Trainer
Posts: 141
Joined: Wed Feb 04, 2015 11:22 pm

Trainer AI

Post by entrpntr » Mon Aug 31, 2015 1:56 am

I finally put in the work to figure out precisely how trainer AI works in Gen 2 and I've written up enough for now that should elucidate most of the inner workings. This includes analyzing the pokecrystal disassembly as well as setting breakpoints in BGB and testing several battles to see how moves get evaluated.

Pastebin of the writeup can be found here: http://pastebin.com/EjAW76cx. I haven't finished writing up Smart AI yet, which admittedly includes a good number of the more important battles. I haven't thoroughly tested those battles to confirm behavior and the sheer amount of stuff going on is making it rather tedious to write up, so I may not get to it for a few days.

I found the relevant RAM/ROM locations in Gold; from what I've seen up to this point, there is no difference from the AI in Crystal (as far as I can tell). The probabilities and the degrees to which moves are influenced in each AI layer all appear to be the same and none of my tests so far have turned up anything unexpected in the way moves are scored. There may very well be some more latent discrepancies, but until I investigate further, I'm leaning towards perceived differences between games to be mistaken.

If there appear to be any mistakes, or if anything is confusing in the pastebin, please let me know. I'll update the thread once I've finished investigating/testing things in-game. Eventually, I'll aim to do another write-up that will give more thorough breakdowns to how the AI affects each fight in the game (at least the ones where there is a meaningful impact).

Some useful files in the pokecrystal disassembly in case anyone wants to verify things independently:

https://github.com/kanzure/pokecrystal/ ... coring.asm (core scoring functions)
https://github.com/kanzure/pokecrystal/ ... ibutes.asm (trainer information)
https://github.com/kanzure/pokecrystal/ ... /items.asm (item evaluation functions)
https://raw.githubusercontent.com/kanzu ... r/main.asm (Ctrl+F for AIChooseMove; also AI_Redundant that's referenced in scoring.asm)

entrpntr
Pokémon Trainer
Posts: 141
Joined: Wed Feb 04, 2015 11:22 pm

Re: Trainer AI

Post by entrpntr » Mon Sep 07, 2015 5:51 am

Wrote up the Smart AI section, which now consumes ~70% of the document; I edited the pastebin that's linked in the OP, but it may be easier to read the raw text: http://pastebin.com/raw.php?i=EjAW76cx.

I did not thoroughly test everything in-game yet to verify since the writeup took so much damn time; I took more care with the stuff that is relevant to GSC speedruns, so this should hopefully be error-free for those who want to use it for routing. For the stuff that isn't speedrun-relevant, there are more likely to be errors, especially where I was reasoning about undocumented functions from the disassembly. I'll aim to do more thorough sanity testing the next chance I get.

Definitely looking for any suggestions on how I could make things clearer (wording/formatting/organization/etc). Also, please point out any errors or omissions you happen to find.

If anyone has questions about any specific fights, feel free to ask and I'll be glad to walk through the AI computations.

Ryziken
Preschooler
Posts: 9
Joined: Tue Sep 23, 2014 5:09 pm

Re: Trainer AI

Post by Ryziken » Fri Sep 18, 2015 12:54 am

Wow, this was a highly interesting read. There were some key takeaways that can make Cyndaquil route less of a headache (like manipping only one Icy Wind for the Pryce fight). Great job compiling this!

The 50-50 item heal chance when between 25% and 50% HP surprised me a little. I had thought from my own observations that it was a SMALL chance to heal at 50%, then became gradually more likely at smaller and smaller amounts of health. Maybe I've just been lucky. :p

entrpntr
Pokémon Trainer
Posts: 141
Joined: Wed Feb 04, 2015 11:22 pm

Re: Trainer AI

Post by entrpntr » Sun Sep 27, 2015 2:44 am

I've done one more big update to the pastebin; updates from here on out should be minor and I won't update the thread unless I find something major.
Ryziken wrote:The 50-50 item heal chance when between 25% and 50% HP surprised me a little. I had thought from my own observations that it was a SMALL chance to heal at 50%, then became gradually more likely at smaller and smaller amounts of health. Maybe I've just been lucky. :p
I am pretty sure you are right; looks like it is actually ~20% (50/256). There is some unusued logic that would heal 50% of the time. Before doing research I had the same thought as you (gradually more likely as HP goes down), but barring some huge failing on my part, this is not the case.

---

I did a somewhat thorough look to try to understand major implications; unfortunately, I don't think I've found much that will change standard strats in-battle (at least the strats I'm familiar with); most of the implications I've found have to do more with HP/risk management from having a better underlying idea of the likelihoods of each event (particularly in Slowpoke Well, Bugsy, Whitney, Morty, Chuck, Elite 4).

Some assorted things that are related to previous speculation or may be otherwise interesting:

(1) I don't think the "Opportunist" or "Cautious" layers were well-understood before. They mainly have implications on early game fights: e.g. The Koffing Grunt in the Slowpoke Well is only likely to use Poison Gas turn 1 (pretty sure it is 33.3% turn 1, 3.33% after), which has some impact on whether to heal beforehand; also in Gold, Ralph's Goldeen will start discouraging Tail Whip when it is below 50% HP, which probably makes Leer + Scratch x3 the correct play in all situations (to minimize chances of seeing Supersonic).

(2) The AI damage calcs for Fury Cutter are always based on Fury Cutter 1. This explains why Bugsy's Scyther opts for Quick Attack when Toto's HP is in the 6-10 range or so, even if it is at Fury Cutter 2 or 3. The Smart_FuryCutter layer is the main driver for encouraging Fury Cutter's usage.

(3) Unless I have missed something, Rival 2 AI is indeed unchanged between Gold/Crystal, against perceptions of several runners (haven't 100% verified every "Smart" sublayer is unchanged, but all the sublayers relevant to this fight appear the same, as do each of the 9 non-smart layers). Regarding the fight, something I hadn't considered was setting options to Switch mode and switching in a slave (unless it is Bellsprout) before Bayleef. When Croconaw comes back in, Bayleef will be 12.5% to use Razor Leaf turn 1, rather than ~32.4%. This is because Growl will be encouraged 50% of the time, rather than discouraged 88% of the time. Probably not worth it timewise, but possibly a safe strat for marathon settings (or for people learning the game). A related idea, if you have Bellsprout, is to switch him in if Croc got a turn 1 Razor Leaf crit; even L3 Bellsprout survives non-crit Razor Leafs. Having a free turn to Super Potion Croc greatly improve survival chances, as Croc's turn counter is reset on top of the heal.

(4) Smart AI encourages Fury Cutter moreso than it does Rage. Haven't quantified it, but this is relevant to whether you want to use Fury Cutter or Rage on Whitney (Clefairy will choose Mimic 33.3% of the time starting turn 2, until its HP is 50% or below).

(5) In the 25-50% HP range, Lance's Gyarados will lock into Hyper Beam (25% and below he might Flail). A possible strat is turn 1 Strength, and if Gyarados used Surf/Flail on turn 1, set up an X Special + X Speed. This would ensure that you won't take damage the rest of the fight (Ice Punch guaranteed on Dragonite; outspeed Aerodactyl) and you get a decent timesave avoiding move animations during the recharging text. I think this should work well in Crystal compared to giving Dragonite/Aerodactyl turns. I don't know how much sense this would make in Gold, but it may be safer and/or more efficient timewise (esp. if it saves a Full Restore usage) for certain HP values.

(6) Red's Pikachu indeed uses Charm 75% / Thunder 25% for all turns while setting up. You should also use the X Speed before X Attacks if you enter the fight at an HP where Thunder + Quick Attack can kill (to manip Quick Attack, which is only encouraged in kill range if Pikachu is slower).


I think that's everything I have to add; the paste should hopefully explain things well enough. I can still provide insight on some fights if anyone has any specific questions.

***EDIT: One other thing that might be better understood is the RIval 3 fight in Crystal, as explained by Exarion a while back here:
Exarion wrote:Interesting that Curse is favored on turn 1. This doesn't really apply to the Morty fight, since you have to set up on the Gastly and then 1-shot it, but it does apply to Rival 3 in Crystal. When his Haunter leads with Lick (not that uncommon), you're more likely to get Curse on the next turn if you used an X Special, and you're more likely to get Lick/Spite if you used Surf/Bite. In most cases, you'd prefer Lick/Spite because Curse damage + crit Razor Leaf can kill. Also, the Curse damage wastes time and must be healed off before Morty.

This is relevant when considering whether to get Abra after dodging Ivan+Irwin. If you get Abra, you've fought Donald/Teru and are Lv. 25 for Bayleef, meaning an X Special is unnecessary. If you don't get Abra and then dodge 2/3 spinners before Ecruteak, you are Lv. 24 for Bayleef, meaning you need to X Special and have lower HP/Sp. Def for Razor Leaf.

Keizaron
Site Admin
Posts: 272
Joined: Wed Jul 16, 2014 6:13 pm
Location: Spokane, WA
Contact:

Re: Trainer AI

Post by Keizaron » Sun Sep 27, 2015 9:38 am

entrpntr wrote:(3) Unless I have missed something, Rival 2 AI is indeed unchanged between Gold/Crystal, against perceptions of several runners (haven't 100% verified every "Smart" sublayer is unchanged, but all the sublayers relevant to this fight appear the same, as do each of the 9 non-smart layers). Regarding the fight, something I hadn't considered was setting options to Switch mode and switching in a slave (unless it is Bellsprout) before Bayleef. When Croconaw comes back in, Bayleef will be 12.5% to use Razor Leaf turn 1, rather than ~32.4%. This is because Growl will be encouraged 50% of the time, rather than discouraged 88% of the time. Probably not worth it timewise, but possibly a safe strat for marathon settings (or for people learning the game). A related idea, if you have Bellsprout, is to switch him in if Croc got a turn 1 Razor Leaf crit; even L3 Bellsprout survives non-crit Razor Leafs. Having a free turn to Super Potion Croc greatly improve survival chances, as Croc's turn counter is reset on top of the heal.
Does the reduction of Razor Leaf apply if you keep Set mode but switch the slave in on turn 1? Or is Shift mode absolutely required for that?

entrpntr
Pokémon Trainer
Posts: 141
Joined: Wed Feb 04, 2015 11:22 pm

Re: Trainer AI

Post by entrpntr » Sun Sep 27, 2015 12:16 pm

It would still apply, since Growl would still be boosted, but it struck me as less effective. If your slave is in on Bayleef's first turn, Bayleef will lock in to Razor Leaf if it kills (which should be every slave except Bellsprout). If Croconaw is still in and you manually switch, the possibility of Reflect (~32.4%) gives me pause, since Fury Cutter may not 3-hit (especially given that Growl becomes more likely when you switch back in).

I am dumb and messed up the math. Growl should be ~66.2% with the strat vs ~2.9% normally. The switch mode strat gives Razor Leaf = ~16.2%, Reflect = ~16.2%, Poisonpowder = ~1.3% on Croc's first turn back in, so it's less of an impact than I stated. These are ~32.4% each on Bayleef's first turn if you leave Croc in. I forgot Poisonpowder was discouraged most of the time after Bayleef's first turn. (For the sake of completion, Reflect becomes discouraged most of the time once Bayleef takes damage, and obviously won't be chosen if a Reflect is already up.)

Exarion
Cooltrainer
Posts: 249
Joined: Thu Jul 17, 2014 10:07 am

Re: Trainer AI

Post by Exarion » Sun Nov 15, 2015 4:22 pm

Are we sure that Lance has the Aggressive AI clause? His Aerodactyl seems to choose attacking moves at random, and Gyarados' AI patterns don't seem consistent with your write-up either.
Twitch channel: http://www.twitch.tv/exarionu
Pokemon Red speedrunning guide: http://pastebin.com/CkVA5yvJ

entrpntr
Pokémon Trainer
Posts: 141
Joined: Wed Feb 04, 2015 11:22 pm

Re: Trainer AI

Post by entrpntr » Mon Nov 16, 2015 6:13 pm

Yes, he does, but Hyper Beam throws things for a loop.

Hyper Beam is the most damaging move for Gyarados and Aerodactyl, so their other moves get discouraged (actually, Flail doesn't get discouraged in the Aggressive layer since it is variable power, but does in the Smart layer when above 25% health). Then Hyper Beam has a chance to be discouraged 1 stage (~42.2%) or 2 stages (~42.2%) in the Smart layer above 50% health.

So on turn 1, there is a chance that Hyper Beam ends up with the same score as the other moves, and possibly even a lower score. It comes out to Surf/Flail ~35.2% each, Hyper Beam ~29.7% for Gyarados || Hyper Beam ~26.2%, Wing Attack/Rock Slide/Ancientpower ~24.6% each for Aerodactyl, which explains why things look so random.

Then on turn 2 (when between 25% and 50% HP), Gyarados locks on to Hyper Beam (as I mentioned in an earlier post), since all other moves are discouraged in the Aggressive layer. (That's why the X Speed + X Special strat on turns 2 & 3 struck me as a good idea in Crystal when you're L52 for the L50 Dragonite, but I didn't explore it much further.)

Exarion
Cooltrainer
Posts: 249
Joined: Thu Jul 17, 2014 10:07 am

Re: Trainer AI

Post by Exarion » Mon Nov 16, 2015 7:33 pm

Ah, that makes sense. Thanks.
Twitch channel: http://www.twitch.tv/exarionu
Pokemon Red speedrunning guide: http://pastebin.com/CkVA5yvJ

Post Reply

Return to “Generation 2”

Who is online

Users browsing this forum: No registered users and 1 guest