By today’s standards, the Artificial Intelligence in Street Fighter 2 World Warrior isn’t very sophisticated. These days, when most people talk about AI they’re talking about machine learning. There’s not any of that in SF2. Anyone looking for some insight into how to write an AI engine for a game today will be disappointed.
Moves made by computer opponents are not made independently but are instead grouped into small scripts, written in a bytecode similar to machine language. A computer avatar has a repertoire of different scripts for each opponent they could face in the game*, and set of circumstances, such as a nearby fireball.
Instructions in the scripts can command the avatar to execute an attack (punch/kick/special/throw), walk or jump somewhere, and wait – either for a timer, or some condition such as the ability to throw another fireball.
Other instructions can directly manipulate variables in the AI state machine, test for certain conditions, and form primitive IF…END blocks. Here’s one of Ryu’s typical ‘easy’ attack routines: Throw three fireballs at you, and if you’re somehow silly enough to catch all three and get dizzy, run up to you and throw you.
ORG 0x99c88 sf2ua.bin 0x02, ; script header, type 2 0x10, 0x50, 0x04, 0x00, ; throw a hi str fireball 0x00, 0x80, ; wait until I have no fireball 0x10, 0x50, 0x04, 0x00, ; another hi str fireball 0x00, 0x80, ; wait again 0x10, 0x50, 0x04, 0x00, ; another hi str fireball 0x92, ; are they dizzy? 0x04, 0x00, 0x18, ; walk until we're within 24 pixels 0x00, 0x82, ; wait if they're still getting up 0x10, 0x84, 0x00, 0x00, ; throw(4) 0x94, ; end if 0x00, 0x00, 0x00, 0x00, ; wait four frames 0x00, 0x00, 0x00, 0x00, 0x86, ; chain to another randomly chosen script
Apart from the header byte, the first byte of each row is the instruction byte. Instructions are grouped into avatar commands (0x0-0x7f) and control flow / variable access (0x80 – 0xff). Each of them mostly have a fixed number of parameters, for example the Attack instruction (0x10) always take three:
- The attack type (special moves are 0x50, 0x52, 0x54…, throws are 0x80, 0x82, 0x84…).
- The strength of the attack (in this case it’s a strong / fast fireball)
- A repetition count for attacks involving holds and multiple hits. (unused here)
The Wait (0x00) instruction usually takes one parameter, decoded as:
- 0x0 – 0x7f: Wait for N frames
- 0x80: Wait until I am able to throw a fireball
- 0x81: Wait until opponent is within M pixels (additional second parameter)
- 0x82: Wait until my opponent is attackable
- 0xc0: Wait until opponent’s jump reaches height M (second param)
All of the instructions and many of the parameters are even multiples of two (there is no 0x01 instruction, for instance) so that they can be used directly against 16-bit jump tables. Low/mid/high strength translate to 0, 2 and 4, as are most internals in the game. Any odd numbers would cause a CPU bus error exception, which would result in the SF2 ROM restarting.
The AI engine has three main modes of operation:
- Waiting for an attack. Simple scripts are chosen at random which consist mainly of walking backwards/forwards small amounts.
- Actively attacking. Scripts such as the one above are selected.
- Reacting to an attack. Scripts suitable for countering the attack are selected. Sometimes. Depending on the AI difficulty setting the computer lets plenty of attacks through unguarded, of course.
For the first two modes, there are 8 levels of scripts, which are chosen based on how much time is left in the round. When reacting to an attack the scripts are chosen based on something called a yoke.
Each frame of animation for both avatars and projectiles contains a value for the yoke in the metadata, which the AI peeks at to select a script suitable for responding to that attack. The computer sees the yoke of your move as soon as you have input it, before the first animation frame has even displayed. As such it gets one more frame of advantage on top of your reaction time.
A question addressed in an earlier post in this blog was whether the AI “cheats”, and it certainly does. Charge moves such as blade kicks are simply executed as instructions, so they cannot fail. Guile can do a bladekick from a standing position simply because that’s what’s in the script. It’s probably possible for the AI to command special moves in the air. One instruction is available to disable collisions against the computer player for a certain number of frames. Using it, you could write a script to simply walk through an approaching fireball.
One of the (more hidden) test screens in the game performs a sanity check on the AI bytecode. It’s not immediately obvious what it’s for as it just displays “OK” and nothing else, but the disassembly reveals the developers were perhaps testing and developing the AI code on CPS machines. The error messages in the test code reveal names for some of the instructions as MOSI, KON, KYON, TIGA, TAN, GTAN and END IF, but apart from the obvious latter name I haven’t figured what they might mean.
Viewers at home can find Ryu’s AI bytecode starting at ROM address 0x9966e on sf2ua. Set a breakpoint at 0x2ad2a to stop at the main entry to the AI code. Avatar AI state variables start at 0x200 from the player struct (0x5c6 and 0x8c6 for P1/2 respectively on sf2ua).
[* WW contains no scripts for battling the four bosses since that shouldn’t happen, and in any case the formulae for each of the opponents are all the same anyway! Maybe the developers ran out of ROM space or time, but the WW computer players use the same formula no matter who they’re fighting]