Battle Initialization

󰃭 2025-03-28

At the beginning of battle an initiative order is calculated for the current number of characters in the party + 8 enemies (regardless of how many their actually are, this is likely because of the fights where additional enemies can join after initialization).

This order is stored at 7EB163-7EB16D.

The random order is calculated with a very naive algorithm. A table of entity IDs is populated. The player characters (PCs) are populated first with ids from 0-2. If there are fewer than 3 PCs in the party then fewer IDs are populated. The enemies are populated next with ids from 3-B in reverse order, enemy IDs are populated regardless of the number of enemies actually in the battle. If < 3 PCs in the party pad out the end with FFs.

So in the case of a party of 3 PCs the table would be:

[0x0, 0x1, 0x2, 0xB, 0xA, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3]

and in the case of a party with just 1 PC the table would be:

[0x0, 0xB, 0xA, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0xFF, 0xFF]

The code then proceeds to call RollRNG for a number between 0 and B, it loads the value from the entity table. If that value is FF then throw it away and call RollRNG again until it finds a valid entity ID. Once a valid entity ID is found store it in slot 0 of the initiative table and repeat until the iniative table is completely filled.1

You probably noticed how terribly inefficient this method is, assuming a full party filling the first entry in the initiative table will take 1 RNG roll, the second entry has a 1/12 chance of needing to be rerolled, the third entry has a 2/12 chance of needing to be rerolled, and so on until the last entry has an 11/12 chance of needing to be rerolled which could easily fail many times in a row.

However, we’re working with a set RNG Table that loops every 256 times we roll, Square likely determined the actual worst case for this RNG method and in practice the longest it will actually take is within acceptable limits. The whole process takes less than a frame, so while it seems odd on the surface, it works for what the developers needed it to.


  1. This same algorithm is used for battle initiative in Final Fantasy V. ↩︎