Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate constants for script-command maps #289

Open
lhearachel opened this issue Nov 3, 2024 · 0 comments
Open

Generate constants for script-command maps #289

lhearachel opened this issue Nov 3, 2024 · 0 comments

Comments

@lhearachel
Copy link
Collaborator

Problem Statement

The assembler-macros used for compilation of field, battle, and trainer AI scripting use a a magic number-sequence to translate macro inputs into the byte-code understood by the game's internal script-processing engines. These numbers are processed by the engines as indices into a respective function table. This is a bit brittle, as it's easy for a modder to create a new scripting command for any engine, create an assembler-macro for the next element in the sequence, and accidentally inject their new function at the wrong place in the respective function table.

Proposal

We should generate a set of constants for use by each class of scripting's associated assembler-macros and function-tables. To illustrate, such constants would be made use of like so:

In asm/macros/scrcmd.inc:

    .macro Noop
-   .short 0
+   .short SCRCMD_NOOP
    .endm

    .macro Dummy
-   .short 1
+   .short SCRCMD_DUMMY
    .endm

    .macro End
-   .short 2
+   .short SCRCMD_END
    .endm

    .macro WaitTime frames, countdownVarID
-   .short 3
+   .short SCRCMD_WAITTIME
    .short \frames
    .short \countdownVarID
    .endm

In src/scrcmd.c:

const ScrCmdFunc Unk_020EAC58[] = {
    [SCRCMD_NOOP] = ScrCmd_Noop,
    [SCRCMD_DUMMY] = ScrCmd_Dummy,
    [SCRCMD_END] = ScrCmd_End,
    [SCRCMD_WAIT_TIME] = ScrCmd_WaitTime,
    // ommitted for brevity
};

One clear negative to this approach is that it adds some additional boilerplate for a modder when it comes to creating a new scripting command. However, there is also a clear authoritative association between macros and function-table, allowing a user to quickly jump between the two concepts without needing to sift through potentially many scripts making use of their command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant