MOTHER 3:Game logic
|# of Entries||2002 (0x7D2)|
|Total Length||1907428 bytes (0x1D1AE4)|
|Back to the ROM map|
This large table controls most of the game's scripted logic. It's responsible for displaying text from the main text table, starting battles, setting event flags, on-screen animations, and more.
There are 2002 offsets at the start of this table; the offsets occur in pairs, so there are 1001 logical entries overall. The entries correspond to the main text table. Similar to the main text table, the first offset in each pair points to a 16-bit offset table, and the second offset in each pair points to the actual script data. The 16-bit offset table has a 16-bit header indicating how many offsets are in that table.
Say we wanted to get the scripts for the area with Alec's house. This area is map 21. We need to add 1 to this value because the first entry in this table (and in the main text table, too) correspond to a special null-area with shared text blocks. So, we're looking for entry 22 in this table. Skipping the 4-byte header, and considering that each entry has two offsets, we get to $1198CC4:
1198CC4: 40 AE 03 00 78 AE 03 00
So the 16-bit offset table is located at $11D3A50 (1198C10 + 3AE40), and the script data itself starts at $11D3A88 (1198C10 + 3AE78). The offset table looks like this:
11D3A50: 1B 00 07 02 0A 02 1B 02 84 02 C2 02 D4 02 1F 03 3F 03 54 03 5B 03 68 03 75 03 82 03 8F 03 9C 03 A4 03 AB 04 AE 04 14 05 3C 05 46 05 B4 05 B9 05 BF 05 D0 05 D4 05 DB 05
Each offset is in units of 32 bits; that is, to get the absolute address of an actual script, you need to multiply the offset by 4, and then add it to the script pointer (in this case, add it to $11D3A88).
Now, say we want to see the save frog's script. His object has a script value of 2, but since the game adds 5, we actually want the 7th offset: 0x33F. Multiplying by 4 and adding to the script pointer,
frog script address = $11D3A88 + (0x33F * 4) = $11D4784
This finally brings us to his script:
0B 0D 00 00 = Add 5 to the current stack pointer 0A 0D 00 00 = Affects the stack in a similar way (These two instructions have an unknown motivation; many scripts do not have them, and those that do often function perfectly fine without them)
01 62 01 00 = Push 0x162 to the stack 04 00 0D 00 = Check event flag 0x162; overwrite the current stack value with the result (In this case, the 0x162 on the stack gets overwritten with a 0 or 1, depending on whether or not the flag is set)
01 00 00 00 = Push 0 to the stack 0E 0A 00 00 = Compare the value of the flag with 0, and store the result (true or false) on the stack 0D 00 4F 03 = If it's false (that is, if flag != 0), jump to script number 0x34F (labelled here as already_talked)
01 FF FF FF = Push 0xFFFFFFFF (-1) to the stack 01 FF FF FF = Push -1, again 01 A5 00 00 = Push 0xA5 to the stack 04 00 33 00 = Display string 0xA5 from entry 0 in the main text table, using the default speaker (first -1), the frog itself (The second -1 does absolutely nothing, but it needs to be there)
01 20 00 00 = Push 0x20 to the stack 04 00 00 00 = Unknown
01 62 01 00 = Push 0x162 to the stack 01 01 00 00 = Push 1 to the stack 04 00 0A 00 = Set flag 0x162 to 1
already_talked: 01 FF FF FF = Push 0xFFFFFFFF (-1) to the stack 01 FF FF FF = Push -1, again 05 00 A7 10 = Call the save frog sequence (not sure about the details on this)
09 00 00 00 = End