From d2a0d9299f5c97b3cbfa8f1e6ed843ac07d9c928 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Wed, 3 Jan 2024 16:34:55 -0500 Subject: [PATCH] Add some constants to the API --- CHANGELOG.md | 3 +++ docsrc/manual/scripting-capabilities.rst | 12 +++++++++++ src/scriptapi/scripting.cpp | 26 ++++++++++++++++-------- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6eea8f05..ed489d72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project somewhat adheres to [Semantic Versioning](https://semver.org/sp The **"Breaking Changes"** listed below are changes that have been made in the decompilation projects (e.g. pokeemerald), which porymap requires in order to work properly. It also includes changes to the scripting API that may change the behavior of existing porymap scripts. If porymap is used with a project or API script that is not up-to-date with the breaking changes, then porymap will likely break or behave improperly. ## [Unreleased] +### Added +- Add `metatile_behaviors`, `num_primary_palettes`, and `num_secondary_palettes` to `constants` in the API. + ### Changed - The API functions `addImage` and `createImage` now support project-relative paths. - Metatile ID strings are now padded to their current max, not the overall max. diff --git a/docsrc/manual/scripting-capabilities.rst b/docsrc/manual/scripting-capabilities.rst index 62044464..9507b048 100644 --- a/docsrc/manual/scripting-capabilities.rst +++ b/docsrc/manual/scripting-capabilities.rst @@ -2082,6 +2082,14 @@ All constants are accessible via the global ``constants`` object. The maximum number of metatiles in a secondary tileset. +.. js:attribute:: constants.num_primary_palettes + + The number of palettes in a primary tileset. + +.. js:attribute:: constants.num_secondary_palettes + + The number of palettes in a secondary tileset. + .. js:attribute:: constants.layers_per_metatile The number of tile layers used in each metatile. This will either be ``2`` or ``3``, depending on the config setting ``enable_triple_layer_metatiles``. @@ -2090,6 +2098,10 @@ All constants are accessible via the global ``constants`` object. The number of tiles in each metatile. This will either be ``8`` or ``12``, depending on the config setting ``enable_triple_layer_metatiles``. +.. js:attribute:: constants.metatile_behaviors + + An object mapping metatile behavior names to their values. For example, ``constants.metatile_behaviors["MB_TALL_GRASS"]`` would normally be ``2``. + .. js:attribute:: constants.base_game_version The string value of the config setting ``base_game_version``. This will either be ``pokeruby``, ``pokefirered``, or ``pokeemerald``. diff --git a/src/scriptapi/scripting.cpp b/src/scriptapi/scripting.cpp index 5f56008f..19086df0 100644 --- a/src/scriptapi/scripting.cpp +++ b/src/scriptapi/scripting.cpp @@ -76,12 +76,6 @@ void Scripting::populateGlobalObject(MainWindow *mainWindow) { QJSValue constants = instance->engine->newObject(); - // Get basic tile/metatile information - int numTilesPrimary = Project::getNumTilesPrimary(); - int numTilesTotal = Project::getNumTilesTotal(); - int numMetatilesPrimary = Project::getNumMetatilesPrimary(); - int numMetatilesTotal = Project::getNumMetatilesTotal(); - // Invisibly create an "About" window to read Porymap version AboutPorymap *about = new AboutPorymap(mainWindow); if (about) { @@ -91,17 +85,33 @@ void Scripting::populateGlobalObject(MainWindow *mainWindow) { } else { logError("Failed to read Porymap version for API"); } + + // Get basic tileset information + int numTilesPrimary = Project::getNumTilesPrimary(); + int numMetatilesPrimary = Project::getNumMetatilesPrimary(); + int numPalettesPrimary = Project::getNumPalettesPrimary(); constants.setProperty("max_primary_tiles", numTilesPrimary); - constants.setProperty("max_secondary_tiles", numTilesTotal - numTilesPrimary); + constants.setProperty("max_secondary_tiles", Project::getNumTilesTotal() - numTilesPrimary); constants.setProperty("max_primary_metatiles", numMetatilesPrimary); - constants.setProperty("max_secondary_metatiles", numMetatilesTotal - numMetatilesPrimary); + constants.setProperty("max_secondary_metatiles", Project::getNumMetatilesTotal() - numMetatilesPrimary); + constants.setProperty("num_primary_palettes", numPalettesPrimary); + constants.setProperty("num_secondary_palettes", Project::getNumPalettesTotal() - numPalettesPrimary); constants.setProperty("layers_per_metatile", projectConfig.getNumLayersInMetatile()); constants.setProperty("tiles_per_metatile", projectConfig.getNumTilesInMetatile()); + constants.setProperty("base_game_version", projectConfig.getBaseGameVersionString()); + // Read out behavior values into constants object + QJSValue behaviorsArray = instance->engine->newObject(); + const QMap * map = &mainWindow->editor->project->metatileBehaviorMap; + for (auto i = map->cbegin(), end = map->cend(); i != end; i++) + behaviorsArray.setProperty(i.key(), i.value()); + constants.setProperty("metatile_behaviors", behaviorsArray); + instance->engine->globalObject().setProperty("constants", constants); // Prevent changes to the constants object + instance->engine->evaluate("Object.freeze(constants.metatile_behaviors);"); instance->engine->evaluate("Object.freeze(constants.version);"); instance->engine->evaluate("Object.freeze(constants);"); }