FF7/Battle/Battle Scenes

## Introduction

FF7 keeps each enemy battle configuration is a file called "scene.bin" This file is located in the following directories.

This file is exactly the same in both versions. This holds all the battle configurations for all enemies encountered in the game.

## Scene.Bin file format

### Overview

The scene.bin file contains 256 gziped files which give us information for all the FF7 monsters. In order to find these files in scene.bin, you have to know that the file is structured with blocks exactly 0x2000 bytes in length. In the first table (scene.bin block), you will see what contains a block. Blocks are concatenated with each other to form the scene.bin file. So if you want to extract data from scene.bin, you'll need to find the correct blocks and to extract the gziped files from it. After that you simply ungzip those files and you'll find 256 files, with a length is 7808 bytes. Known information about those files can be found in the second table (The Data File specification). Because extracting file manually would be a pain, several tools was developed in order to help you. You can use Scene Reader for example, it's a win32 tool to extract and repack scene.bin archive.

Also note, that in kernel.bin there is a look-up table for scene.bin, which tells how many files there are in each section of scene.bin. You need to update it every time you repack the file and something changes. The table is at offset 0x0F1C of the third section of the kernel.bin file. You can use SceneFix program, which'll update the table for you.

We have 1024 possible battle numbers: 0 - 1023. Each group of \*4\* Battle Numbers refers to a particular Scene file: for instance, Battles 0-3 refer to File 0 in Scene.bin, Battles 4-7 refer to File 1 in Scene.bin, and so forth.

#### Japanese format

In the japanese scene.bin, ennemies names and attacks names have a size of 16 bytes, instead of 32 bytes.

### General file format

 0x0004
 0x003C
 0x0040
 lastpointer \* 4

### Data file format

#### Battle Formation Data

4 Possible battle formations per scene, maximum of 6 enemies per battle. Each enemy entry contains the following data:

#### Formation ID

Formation ID is an index to a formation within a given scene. It is the scene index bit shifted 2 to the left plus formation index within the scene.

```
0 1 2 3 4 5 6 7 8 9 A B C D E F
[ - - - - - - - - - - - - ] [ ]
        scene index         formation index

```

For this reason, the Formation ID will not exceed 03FFh.

example: Formation 028Dh bit shift two to the right to get scene

```
028D >> 2 = 00A3 (right-most bits are truncated)

```

This is Scene 163 Formation Index is just the ID ANDed with 3.

```
028D AND 3 = 01

```

Formation 1 So this is Formation 1 in scene 163. (SOLDIER:3rd x2)

#### AI Data

This section contains Battle Script that is executed before or/and during every fight. Every enemy has it's own script, and this script is divided to a number of sections. Each script starts with 32 bytes of header that are divided into 16 parts representing 16 script types. The 2-byte number in any section is an offset relative to the beginning of this 32 byte block that tells the script reader where the script starts for that script type.

Its structure and opcodes are described here.

NOTES:

* A monster's total AI size will always be an even number of bytes. If the actual scripts are an odd number, a single NULL (FFh) will be placed before the next monster's AI header (may not be required).
* Battle begins after all characters' Initialize scripts have been run (Players first, then enemies, then formation).
* The only character with "Battle End" is in Cloud's AI. It's meant to lower the character's Love Points with him if he lets them die or he dies with them in the party (not sure which).
* Pre-Action Event occurs on all battle participants prior to any actions performed by any participant regardless of actor or target. This includes all executed 92 commands that have a command index of less than 21h. If any 92 commands are called in this section, the command that caused this script to run has priority.
* The Custom Event sections are not called by any event. They only occur if they are called with the 92 command.

```
60 22 <- command index "Run script"
60 0X <- where X is the script section in hex (eg. X = 8 would call Custom Event 1 since it is script id 08
         [not to be confused with offset])
92

```

* Custom Event 8 is only used on Mystery Ninja (all), Ultimate Weapons in location other than above Cosmo Canyon, Safer Sephiroth, and the final "showdown" between Cloud and Sephiroth. These characters have scripts on them that do not remove them from battle when they are defeated.
* Custom Events 1-7 may not work. (not thoroughly tested)
* The order of scripts executed:

:\* Beginning of battle
Pre-Battle (all participants)
* Once a "main-script enabled" enemy's time gauge is full:

Main (Enemy performs action)
* Pre-Attack (If enemy script uses a 92 command with a command index of 20h or less)

Pre-Action Setup (occurs on all participants)
* Post-Attack
	1. Death Counter (If script owner died, execution stops here)
	2. General Counter (Executed by all targets)
	3. Physical Counter/Magical Counter (Executed by all targets depending on damage type)
* Battle ends

Battle End (all participants)
#### Binary "Cover Flags"

These flags are used in conjunction with row to determine if a target can be selected as the target of a **short-range attack**. The determination of this is worked out in this way: An enemy exists in row 1 and another in row 2. If the enemy in row 1 shares a cover flag with the enemy in row 2 then the enemy in row 2 cannot be targeted until all enemies in row 1 that share a cover flag with the row 2 enemy is defeated. It works like this. Two active enemies exist, A and B.

```
If ((B's row > A's row) and (B's cover flags AND A's cover flags) > 0) then enemy B cannot be targeted by short-range attacks.

```

for any enemies A and B.
 Example:
 Consider the Battery Cap x6 battle in the forest between Nibelheim and Rocket Town. Their cover flags (in binary) are:

```
Row 1:       00100
Row 2:    00110 01100
Row 3: 00011 00100 11000

```

The battery caps in row 2 cannot be targeted by a short-range attack until the one in row 1 has been defeated because they share the 0x4 cover flag. Once row 1 has been cleared:

```
Row 2:    00110 01100
Row 3: 00011 00100 11000

```

The battery cap on left in row 2 covers the left two in row 3 because it shares flag 0x4 with the one in the middle and flag 0x2 with the one on the far left. As long as it is active these in row 3 cannot be targeted. Similarly, the battery cap on the right in row 2 shares the 0x4 flag with the middle of row 3 and the 0x8 flag with the far right of row 3 so these cannot be targeted until the right side of row 2 is defeated.
 It is also necessary to note that because row 1 does not share any flags with the extreme right and left of row 3, they can be targeted if the corresponding enemy in row 2 is defeated even if the row 1 enemy is still active.
 Also of note is that enemies in the same row that share cover flags are not considered.
 Only the first five bits may be considered even though the value is stored as a word.

## Useful downloads

There are few programs written that will help you edit scene.bin file:

* Scene Reader
* SceneEdit
* Scenester
* Proud Clod