diff --git a/.github/workflows/cpp-ci-serial-programs-base.yml b/.github/workflows/cpp-ci-serial-programs-base.yml index 58f1a9d890..9595aa79f0 100644 --- a/.github/workflows/cpp-ci-serial-programs-base.yml +++ b/.github/workflows/cpp-ci-serial-programs-base.yml @@ -208,12 +208,32 @@ jobs: cd Arduino-Source/SerialPrograms mkdir bin cd bin - cmake .. -DQT_MAJOR:STRING=6 ${{env.CMAKE_ADDITIONAL_FLAGS}} + cmake .. -DQT_MAJOR:STRING=6 -DPA_FRLG_STORY_EXPORT=ON ${{env.CMAKE_ADDITIONAL_FLAGS}} # By default Ninja reads each compile's .d dependency file into its own database, then deletes it. # To keep them for the downstream clang-query cache (see .github/scripts/clang-query-cached.py), add # -d keepdepfile cmake --build . --config RelWithDebInfo --parallel ${{env.BUILD_PARALLEL}} -- -d keepdepfile + - name: Export evaluated FRLG Parcel checkpoint (Ubuntu) + if: startsWith(inputs.os, 'ubuntu') && inputs.run-tests + run: | + cd Arduino-Source/SerialPrograms/bin + ./StoryCheckpointExport 00.05 > frlg-parcel.native.json + python3 - <<'PYTHON' + import json + from pathlib import Path + data = json.loads(Path('frlg-parcel.native.json').read_text()) + assert data['game'] == 'frlg' and data['checkpoint']['id'] == '00.05' + assert len(data['checkpoint']['events']) == 3 + PYTHON + + - name: Upload evaluated FRLG checkpoint + if: startsWith(inputs.os, 'ubuntu') && inputs.run-tests + uses: actions/upload-artifact@v7 + with: + name: frlg-checkpoint-${{inputs.os}}-${{inputs.compiler}} + path: Arduino-Source/SerialPrograms/bin/frlg-parcel.native.json + - name: Prepare upload build if: inputs.upload-build shell: bash diff --git a/SerialPrograms/CMakeLists.txt b/SerialPrograms/CMakeLists.txt index d43d52d625..73729ebde9 100644 --- a/SerialPrograms/CMakeLists.txt +++ b/SerialPrograms/CMakeLists.txt @@ -921,6 +921,7 @@ if(COMMAND_LINE_EXE) include(Source/CommandLine/CommandLineExecutable.cmake) endif() +include(../devtools/story-export/FRLG.cmake) if(PA_PYTHON_BINDINGS) # Python bindings (GUI-free) from subdirectory include(Source/PythonBindings/PythonBindings.cmake) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp index 1973dbe3e4..d8cd48d681 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp @@ -40,6 +40,7 @@ #include "PokemonFRLG_Navigation.h" #include "Programs/AutoStory/PokemonFRLG_AutoStoryBattle.h" #include "Programs/AutoStory/PokemonFRLG_AutoStoryTable.h" +#include "Programs/AutoStory/PokemonFRLG_AutoStoryExport_Tests.h" #include "Tests/TestUtils.h" #include "PokemonFRLG_Tests.h" @@ -317,6 +318,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_OverworldRecovery(database); add_tests_PlayerFacingReader(database); add_tests_AutoStory(database); + add_tests_AutoStoryExport(database); add_tests_AutoStorySimulation(database); add_tests_AutoStoryItems(database); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport.cpp new file mode 100644 index 0000000000..a0b85c21ea --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport.cpp @@ -0,0 +1,103 @@ +/* FRLG Authored Checkpoint Export + * From: https://github.com/PokemonAutomation/ + */ +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonArray.h" +#include "Common/Cpp/Json/JsonObject.h" +#include "Pokemon/AutoStory/Pokemon_StoryStepJson.h" +#include "PokemonFRLG_AutoStoryExport.h" +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ +namespace{ +const char* menu_name(StartMenu menu){ + switch (menu){ + case StartMenu::BEFORE_POKEMON: return "before_pokemon"; + case StartMenu::BEFORE_POKEDEX: return "before_pokedex"; + case StartMenu::FULL: return "full"; + } + throw ParseException("Unsupported FRLG START menu."); +} +JsonArray strings(const std::vector& values){ + JsonArray result; + for (const std::string& value : values){ + result.push_back(value); + } + return result; +} +int64_t count(size_t value){ + if (value > (size_t)std::numeric_limits::max()){ + throw ParseException("FRLG checkpoint count exceeds JSON integer range."); + } + return (int64_t)value; +} +JsonObject stats_json(const Stats& stats){ + JsonObject result; + result["hp"] = stats.hp; + result["attack"] = stats.attack; + result["defense"] = stats.defense; + result["sp_attack"] = stats.sp_attack; + result["sp_defense"] = stats.sp_defense; + result["speed"] = stats.speed; + return result; +} +JsonObject member_json(const PartyMember& member){ + JsonObject result; + result["species"] = member.species; + JsonArray moves; + for (const KnownMove& move : member.moves){ + JsonObject entry; + entry["move"] = move.move; + entry["pp"] = move.pp; + moves.push_back(std::move(entry)); + } + result["moves"] = std::move(moves); + result["moves_through_level"] = member.moves_through_level; + result["moves_read"] = member.moves_read; + result["level"] = member.level; + result["stats"] = member.stats ? JsonValue(stats_json(*member.stats)) : JsonValue(); + return result; +} +JsonObject supplies_json(const Supplies& supplies){ + JsonObject result; + result["potions"] = supplies.potions; + result["antidotes"] = supplies.antidotes; + result["parlyz_heals"] = supplies.parlyz_heals; + result["pocket"] = strings(supplies.pocket); + result["status"] = status_name(supplies.status); + result["hurt"] = supplies.hurt; + result["hp_red"] = supplies.hp_red; + result["hp_fill"] = supplies.hp_fill; + result["lead"] = member_json(supplies.lead); + JsonArray protected_slots; + for (size_t slot : supplies.protected_slots){ + protected_slots.push_back(count(slot)); + } + result["protected_slots"] = std::move(protected_slots); + result["foes"] = strings(supplies.foes); + result["trainer"] = supplies.trainer; + result["risk"] = battle_risk_name(supplies.risk); + result["foe_out"] = supplies.foe_out ? JsonValue(count(*supplies.foe_out)) : JsonValue(); + result["wild_foes"] = strings(supplies.wild_foes); + return result; +} +} // namespace +JsonValue story_checkpoint_export(const StoryCheckpoint& checkpoint){ + JsonObject result; + result["format"] = "pokesim-native-checkpoint-export/1"; + result["game"] = "frlg"; + result["status"] = "native-table-export"; + result["checkpoint"] = Pokemon::story_checkpoint_json(checkpoint); + JsonObject native; + native["menu"] = menu_name(checkpoint.menu); + native["supplies"] = supplies_json(checkpoint.supplies); + result["native_fields"] = std::move(native); + result["state_basis"] = "native-table-bookkeeping"; + result["unbound"] = strings({"runtime_configuration", "map_pins", "executor_bindings", "entry_evidence", "effective_save_retry_policy"}); + return result; +} +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport.h new file mode 100644 index 0000000000..f2cc73c6b2 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport.h @@ -0,0 +1,18 @@ +/* FRLG Authored Checkpoint Export + * From: https://github.com/PokemonAutomation/ + */ +#ifndef PokemonAutomation_PokemonFRLG_AutoStoryExport_H +#define PokemonAutomation_PokemonFRLG_AutoStoryExport_H +#include "Common/Cpp/Json/JsonValue.h" +#include "PokemonFRLG_AutoStoryEvents.h" +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ +// Capture evaluated shared fields and every FRLG checkpoint extension. +// Native supplies are bookkeeping, not observed state or entry requirements. +// Runtime options, map pins, effective policies and execution remain unbound. +JsonValue story_checkpoint_export(const StoryCheckpoint& checkpoint); +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport_Tests.cpp new file mode 100644 index 0000000000..bc4e0f3f12 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport_Tests.cpp @@ -0,0 +1,102 @@ +/* FRLG Checkpoint Export Tests + * From: https://github.com/PokemonAutomation/ + */ +#include "Common/Cpp/Json/JsonArray.h" +#include "Common/Cpp/Json/JsonObject.h" +#include "Pokemon/AutoStory/Pokemon_StoryStepJson.h" +#include "PokemonFRLG_AutoStoryExport.h" +#include "PokemonFRLG_AutoStoryTable.h" +#include "PokemonFRLG_AutoStoryExport_Tests.h" +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ +namespace{ +struct ExportInput{ + std::string_view id; + bool custom = false; +}; +struct ExportExpectation{ + std::vector events; + std::string_view menu; + std::string_view native_json = {}; +}; +struct ExportCase{ + ExportInput input; + ExportExpectation expectation; +}; +const ExportCase CASES[] = { + {.input = {.id = "00.05"}, .expectation = {.events = {"below_mart_door", "mart_door", "parcel_from_clerk"}, .menu = "before_pokedex"}}, + {.input = {.id = "00.05", .custom = true}, .expectation = { + .events = {"below_mart_door", "mart_door", "parcel_from_clerk"}, .menu = "full", + .native_json = R"({"menu":"full","supplies":{"potions":2,"antidotes":3,"parlyz_heals":4,"pocket":["antidote",""],"status":"PSN","hurt":true,"hp_red":true,"hp_fill":0.25,"lead":{"species":"wartortle","moves":[{"move":"bite","pp":0}],"moves_through_level":18,"moves_read":true,"level":19,"stats":{"hp":50,"attack":20,"defense":30,"sp_attack":40,"sp_defense":35,"speed":25}},"protected_slots":[0,2],"foes":["staryu"],"trainer":"misty","risk":"high","foe_out":0,"wild_foes":["pidgey"]}})" + }}, +}; +void customize(StoryCheckpoint& row){ + row.menu = StartMenu::FULL; + Supplies& state = row.supplies; + state.potions = 2; + state.antidotes = 3; + state.parlyz_heals = 4; + state.pocket = {"antidote", ""}; + state.status = StatusCondition::POISON; + state.hurt = true; + state.hp_red = true; + state.hp_fill = 0.25; + state.lead = PartyMember{.species = "wartortle", .moves = {{"bite", 0}}, + .moves_through_level = 18, .moves_read = true, .level = 19, + .stats = Stats{.hp = 50, .attack = 20, .defense = 30, .sp_attack = 40, .sp_defense = 35, .speed = 25}}; + state.protected_slots = {0, 2}; + state.foes = {"staryu"}; + state.trainer = "misty"; + state.risk = BattleRisk::HIGH; + state.foe_out = 0; + state.wild_foes = {"pidgey"}; +} +std::string check_export(StoryCheckpoint row, const ExportCase& test){ + if (test.input.custom){ + customize(row); + } + const JsonValue encoded = story_checkpoint_export(row); + const JsonObject& root = encoded.to_object_throw(); + const JsonObject& native = root.get_object_throw("native_fields"); + if (native.get_string_throw("menu") != test.expectation.menu){ + return "Expected menu " + std::string(test.expectation.menu) + ", read " + native.get_string_throw("menu"); + } + if (!test.expectation.native_json.empty() && !(JsonValue(native.clone()) == parse_json(std::string(test.expectation.native_json)))){ + return "Expected native fields " + std::string(test.expectation.native_json) + ", read " + native.dump(); + } + const auto decoded = Pokemon::read_story_checkpoint(root.get_value_throw("checkpoint"), "export test"); + if (!(Pokemon::story_checkpoint_json(decoded) == Pokemon::story_checkpoint_json(row))){ + return "Expected every authored common field to survive export/import."; + } + std::vector names; + for (const auto& step : decoded.steps){ + names.push_back(step.name); + } + return names == test.expectation.events ? "" : "Expected Parcel's three authored events in order."; +} +class ExportTest : public UnitTest{ +public: + explicit ExportTest(ExportCase test) + : UnitTest("PokemonFRLG::CheckpointExport - " + std::string(test.input.id) + (test.input.custom ? " custom" : " authored")), m_test(test){} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + for (const auto& row : story_checkpoints()){ + if (row.id == m_test.input.id){ + const std::string error = check_export(row, m_test); + return error.empty() ? UnitTestResult(true) : UnitTestResult(error); + } + } + return UnitTestResult("Expected checkpoint " + std::string(m_test.input.id) + ", read no matching row."); + } +private: + ExportCase m_test; +}; +} // namespace +void add_tests_AutoStoryExport(UnitTestDatabase& database){ + for (const ExportCase& test : CASES){ + database.add(test); + } +} +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport_Tests.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport_Tests.h new file mode 100644 index 0000000000..90eabc74a9 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport_Tests.h @@ -0,0 +1,15 @@ +/* FRLG Checkpoint Export Tests + * From: https://github.com/PokemonAutomation/ + */ +#ifndef PokemonAutomation_PokemonFRLG_AutoStoryExport_Tests_H +#define PokemonAutomation_PokemonFRLG_AutoStoryExport_Tests_H +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ +// Register evaluated-table and non-default native-extension preservation tests. +void add_tests_AutoStoryExport(UnitTestDatabase& database); +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index a9052417a4..9f742cd8d4 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1870,6 +1870,10 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryParty_Tests.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySummary.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySummary.h + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport.cpp + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport.h + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport_Tests.cpp + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport_Tests.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryWinChance.cpp diff --git a/devtools/story-export/FRLG.cmake b/devtools/story-export/FRLG.cmake new file mode 100644 index 0000000000..2c217b0480 --- /dev/null +++ b/devtools/story-export/FRLG.cmake @@ -0,0 +1,17 @@ +# Evaluates the real game table without constructing a console executor. +# Full application libraries are build dependencies; this is not PA_CORE_ONLY. +option(PA_FRLG_STORY_EXPORT "Build the offline evaluated FRLG checkpoint exporter" OFF) +if(PA_FRLG_STORY_EXPORT) + execute_process(COMMAND git rev-parse HEAD WORKING_DIRECTORY "${REPO_ROOT_DIR}" + OUTPUT_VARIABLE STORY_EXPORT_REVISION OUTPUT_STRIP_TRAILING_WHITESPACE COMMAND_ERROR_IS_FATAL ANY) + execute_process(COMMAND git diff --quiet HEAD WORKING_DIRECTORY "${REPO_ROOT_DIR}" + RESULT_VARIABLE STORY_EXPORT_DIRTY) + if(NOT STORY_EXPORT_DIRTY EQUAL 0) + message(FATAL_ERROR "Commit tracked changes before configuring a provenance-bearing story export.") + endif() + add_executable(StoryCheckpointExport + ../devtools/story-export/frlg_main.cpp) + target_link_libraries(StoryCheckpointExport PRIVATE SerialProgramsLib) + apply_common_target_properties(StoryCheckpointExport) + target_compile_definitions(StoryCheckpointExport PRIVATE PA_STORY_EXPORT_REVISION="${STORY_EXPORT_REVISION}") +endif() diff --git a/devtools/story-export/README.md b/devtools/story-export/README.md new file mode 100644 index 0000000000..8195c7b3fe --- /dev/null +++ b/devtools/story-export/README.md @@ -0,0 +1,38 @@ +# Evaluated native story checkpoint capture + +This first adapter exports FRLG stage 1's actual `story_checkpoints()` rows. +It does not parse C++ text, construct a story executor, load a save, open a +profile or send controller inputs. It is a full-library build target, not the +Qt-free core target; linking Qt libraries does not create an application/window. + +From a clean committed checkout with the usual native build dependencies: + +```sh +cmake -S SerialPrograms -B build -DPA_FRLG_STORY_EXPORT=ON +cmake --build build --target StoryCheckpointExport -j4 +build/StoryCheckpointExport 00.05 > frlg-parcel.native.json +``` + +On multi-configuration generators add `--config RelWithDebInfo`, then run +`build/RelWithDebInfo/StoryCheckpointExport.exe`. The current full-library CI +builds the target on every platform and captures Parcel as a Linux artifact. +Reconfigure after switching source revisions: `source_revision` names the clean +revision used at configure time. Canonical captures should come from clean CI. + +The `pokesim-native-checkpoint-export/1` capture contains the shared checkpoint +codec plus all fields of this source revision's FRLG checkpoint extension: +menu and supplies (including party moves/PP, optional stats, slot zero, and +foe bookkeeping). Defaults remain native bookkeeping, not measured state or +requirements. No inference of full HP, actual inventory or battle probability +is permitted from this capture alone. + +Runtime configuration, map pins, executor bindings, entry evidence and effective +save/retry policy are explicitly unbound. This is not the proposed executable +story-plan envelope, and must not be passed directly to a console executor. +Other native revisions with more checkpoint fields need an updated adapter. +BDSP `01.03`, policy binding and native/Python execution parity remain next. + +Stdout is machine JSON; import/format it through PokemonSimulation's native +export CLI and compact contract formatter before checking in review data. +Tests next to the adapter check the real Parcel row and every native extension +field using synthetic non-default state. They do not validate console timings. diff --git a/devtools/story-export/frlg_main.cpp b/devtools/story-export/frlg_main.cpp new file mode 100644 index 0000000000..72d3a0bbcf --- /dev/null +++ b/devtools/story-export/frlg_main.cpp @@ -0,0 +1,34 @@ +/* Offline evaluated FRLG table export. No console or profile initialization. + * cmake -S SerialPrograms -B build -DPA_FRLG_STORY_EXPORT=ON + * cmake --build build --target StoryCheckpointExport -j4 + * build/StoryCheckpointExport 00.05 > frlg-parcel.native.json + * Format the captured JSON with PokemonSimulation's contract formatter. + */ +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonObject.h" +#include "VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryExport.h" +#include "VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.h" + +int main(int argc, char** argv){ + if (argc != 2){ + std::cerr << "Usage: StoryCheckpointExport CHECKPOINT_ID\n"; + return 2; + } + try{ + for (const auto& row : PokemonAutomation::NintendoSwitch::PokemonFRLG::story_checkpoints()){ + if (row.id != std::string_view(argv[1])){ + continue; + } + auto result = PokemonAutomation::NintendoSwitch::PokemonFRLG::story_checkpoint_export(row); + result.to_object_throw()["source_revision"] = PA_STORY_EXPORT_REVISION; + std::cout << result.dump(-1) << '\n'; + return 0; + } + std::cerr << "Unknown checkpoint: " << argv[1] << '\n'; + }catch (const PokemonAutomation::Exception& error){ + std::cerr << error.to_str() << '\n'; + } + return 1; +}