Mother 3/Game logic

From Data Crystal
Jump to navigation Jump to search
Main scripts
Game MOTHER 3
Start Address 0x1198C10
End Address 0x136A6F3
# of Entries 2002 (0x7D2)
Entry Length Variable
Total Length 1907428 bytes (0x1D1AE4)
Back to the ROM map

Overview

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.

Layout

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.

Scripts

A script consists of a list of 4-byte codes. The first of these 4 bytes denotes the code type. It must be between 0 and 0xE, inclusive. The remaining three bytes, ordered as [XX YY ZZ], are usually formatted as either a sign-extended 32-bit value 0xZZYYXX, or as an unsigned 16-bit value 0xZZYY (the XX byte is ignored).

The script system has an internal stack of 4-byte values. This stack is used to pass arguments to, and return-values from, the script codes that get called.

Code values

The following table summarizes known code values:

00 XX YY ZZ Unknown
01 XX YY ZZ Push 0xZZYYXX to the stack (sign-extended)
02 XX YY ZZ Unknown
03 XX YY ZZ Unknown
04 00 YY ZZ Call extended control code 0xZZYY (must be between 0 and 0xFF, inclusive)
05 XX YY ZZ Call a special sequence. Observed in the save frog's script for loading both the text and the file select screen. XX may possibly be unused
06 XX YY ZZ Unknown
07 00 YY ZZ Unconditionally jump to offset 0xZZYY and return
08 XX YY ZZ Unknown
09 XX YY ZZ End of script
0A XX YY ZZ Unknown; affects the stack pointer
0B XX YY ZZ Increment the current stack pointer by 0xZZYYXX
0C XX YY ZZ Unknown
0D 00 YY ZZ If the value at the top of the stack is zero, go to offset 0xZZYY (and don't return)
0E XX YY ZZ Pop two values off the stack, compare them, and push the result

Example

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

Indeed, there are 0x1B offsets. The first five offsets are reserved for some unknown reason: hotspots and map objects cannot access them (the game adds 5 to whatever script index it tries to load).

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