FF7/Battle/Battle Scenes/Battle Script

(Information on opcodes provided by Terence Fergusson. Almost all information comes from this post on the forums.)

There are four actions to be explained when dealing with any opcode in AI script: Arguments, Values to take from the stack, what to do with Arguments and Values, and what to put back on the stack. So to fully understand what is happening a brief explanation of the stack is necessary.

The stack is more-or-less a list of values with different lengths. Only the top of the stack (the most recently added value) can be accessed at any given time, but when that value is accessed it is removed from the top of the stack and the previously added value now becomes the top of the stack. Adding to the stack is called "Pushing" a value and taking a value off is called "Popping". When pushing a value to the stack, the value of the number is pushed followed by the value type. The value type can be one of three different things:

 1Xh

They are stored as DWords, but the X will determine how many bytes to use: 0 = bit, 1 = Byte, 2 = Word, 3 = Three bytes

Now that we've seen the stack, here are the opcodes:

GENERAL NOTES:

* Command 90h is overloaded.
* If a type 2X is popped and any type is accepted, only the first value will be considered.
* If specific type is expected and that type is not available, the game with either crash or ignore that entire line.
* TRUE and FALSE are stored as type 00 as '1' and '0' respectively.
* Commands 74h and A1h are never used, but have been documented as valid commands.
* Commands 75h and 96h only appear in the Character AI found in the KERNEL.BIN.
* Commands of group 2X, BX, CX, DX, EX, and FX are treated as NOPs. The script will ignore them and continue processing. Also, any 5X, 6X, 7X, 8X, and 9X value that isn't listed above appear to be treated the same way, but it's safest to use a value from the 2X group. An incorrect 0X, 1X, 3X, or 4X will have unintended side-effects.

0X & 1X CODES NOTES:

* Valid values for X are 0, 1, 2, and 3.
	+ These will store a bit, byte, word, and dword respectively.

3X CODES NOTES:

* The pushed value is type 2x if either of the pops are a 1X or 2X. In that case, the Masks of both pops are ANDed together. In \*all\* cases, the highest pop's size is used to determine the new pop's size.

4X CODES NOTES:

* The Pushed value will be type 00 if either component is a 1X or 2X type. It will only push TRUE or FALSE
* If both were 0X types, the result will be a type 02 which contains the Mask of all the objects in the pops that passed the comparison

86 CODE NOTES:

* This get the MP Cost of the ability referenced in the pop. The value of pop is referenced to the following areas:

```
 If Value >= 0xFFFF, return 0
 If Value <= 0x00FF, Addr = 00DAAC78 + Value*0x1C
 If Value >= 0x0100, match Value with wr[0099AAF4+(ID=[0..31])*2]
    First ID it matches, Addr = 0099A774 + ID*0x1C
 If no Addr is found, return 0
 Otherwise, return wr[Addr + 4]

```

The pushed value is, thus, the MP cost of the defined ability as a type 02. Note that 0x00 to 0xFF are standard magic and always loaded, while 0x100+ are the unique abilities loaded through scene.bin.

Command index in case of character AI
20h - For enemy attack
22h - Force execution of script (referenced by first pop)
24h - Pauses battle engine while string is displayed (in conjunction with code 93)