View Single Post
  #195    
Old January 1st, 2012 (10:40 PM).
JPAN JPAN is offline
pokemon rom researcher
 
Join Date: Dec 2008
Posts: 102
Unfortunately, time to hack is growing shorter by the semester, and research subjects seem to pile up when you have little time. I was hoping to release some examples with this, but...

The subject is Battle AI.
Battles are pretty much a mystery when compared to other fields of pokemon study. We can pretty much change the Overworld to do whatever we want, but to get the code to run anything else (other than that for which it was intended) in the battle screen is hard. Even strings, that outside we can display with a simple call, need to be encoded into the battle string displayer and accessed by an index, requiring repointing tables, changing limiter bytes and much more.

When we look at a battle, we expect the opposing team to do something back. They supposedly have some strategy, but digging into the trainer and wild pokemon code revealed something suprising: Trainers and wild pokemon seem to share the same code.
Functions used by the wild pokemon have trainer only functions, disabled because of the battle flag.
And I have yet to find where they select what they do. The trainer "AI byte" only function seem to be setting the IV's for each party member (0xff being 31 IV's for everything).

So, what I found was the attack selection byte. In the case the pokemon will attack, this is the byte that is used to see what he selected.
At 0x0801488E is the last location the selected attack is stored. It first goes through a series of temporary addresses that change a lot, so pinpointing the origin is hard. Replacing that location with a hook and creating a routine to handle it can create diverse AI.

At 0x02000090 is the location of your attack. 0x02000091 keeps the enemy attack, 92 and 93 the same for partner pokemon. That attack is stored as the Attack slot number. The attack it corresponds can be found by going into the battle data for each pokemon (0x02023be4 for yours, 0x02023be4 + 0x58 for opponent) + 0xc + (attackSlot*2).

So, what can we use this for? We can hook this routine to simulate lots of different scenarios.
For instance, using it to create smarter pokemon (like Elite Four members that know the type chart and use the most effective attacks, and learn information about your team during battle). Or change attacks on your own pokemon(like simulating the battle palace on Fire Red). Or create double battles with a secondary trainer (AI for the second trainer).

The main reason I don't have any simple examples for this one is because other than the really dumb "random attack" model, there are no simple examples. Programming an AI is time consuming (specially in assembly), but for anyone out there that wants to try, I give you a way to start.

As you may notice, the function refered is not the AI function. It's merely the last in a series of memory variables being copied, that end at that location when the attack is selected. This findings do not allow you to change Item usage for trainers, and if the game decides to use an item, or change the pokemon on the field, your attack changes at this level will have no effect. But for wild pokemon, it works perfectly.
__________________
Here are the links for my work


Currently working on:
Battle Script Documentation
Another large project
Reply With Quote