diff --git a/.github/workflows/cpp-ci-serial-programs-base.yml b/.github/workflows/cpp-ci-serial-programs-base.yml index 021cca3550..1dc36f98a4 100644 --- a/.github/workflows/cpp-ci-serial-programs-base.yml +++ b/.github/workflows/cpp-ci-serial-programs-base.yml @@ -88,6 +88,20 @@ jobs: ref: 'master' path: 'Arduino-Source/Packages' + # Fork only, until upstream Packages has them: the FRLG player sprite sheets + # AutoStory reads (PokemonFRLG_PlayerFacingReader.cpp; red is the boy, green + # the girl) are on dayfine/Packages branch lab/autostory-resources. Without + # them the AutoStoryResources test fails. + - name: Fetch fork-only AutoStory resources + if: github.repository == 'dayfine/Arduino-Source' + shell: bash + run: | + for FILE in Resources/PokemonFRLG/PlayerSprites/red_normal.png Resources/PokemonFRLG/PlayerSprites/green_normal.png; do + mkdir -p "Arduino-Source/Packages/$(dirname "$FILE")" + curl -fsSL --retry 3 -o "Arduino-Source/Packages/$FILE" \ + "https://raw.githubusercontent.com/dayfine/Packages/lab/autostory-resources/$FILE" + done + - name: Install Python for pinned map preparation uses: actions/setup-python@v5 with: @@ -95,7 +109,7 @@ jobs: - name: Prepare approved FRLG map resources id: map-resources - uses: dayfine/PokemonSimulation/integrations/arduino-source/verify-map-packs@906956e94fc912ae9adb0e44845da780f3bc7e4c + uses: dayfine/PokemonSimulation/integrations/arduino-source/verify-map-packs@d3eea1b5c04180f8b60eb98b0d65f70aca6b1abb with: layout: resources operation: prepare @@ -106,7 +120,7 @@ jobs: uses: actions/checkout@v7 with: repository: dayfine/PokemonSimulation - ref: 9cb12c52f5f055299e32c2a7cf43e7103c101e3e + ref: d3eea1b5c04180f8b60eb98b0d65f70aca6b1abb path: StoryReplaySource - name: Verify replay preview bytes and independently regenerate its route input @@ -127,7 +141,7 @@ jobs: assert pin['status'] == 'preview-unmerged-PR-73' and revision == pin['revision'] for name, entry in pin['files'].items(): published = (source / entry['path']).read_bytes() - copied = (native / 'StoryData/frlg-parcel-00.05.json' if name == 'capture' else folder / (name + '.json')).read_bytes() + copied = (folder / (name + '.json')).read_bytes() assert hashlib.sha256(published).hexdigest() == entry['sha256'], name assert copied == published, name binding = load_binding(source / pin['files']['binding']['path'], source) @@ -267,22 +281,24 @@ jobs: cd Arduino-Source/SerialPrograms mkdir bin cd bin - cmake .. -DQT_MAJOR:STRING=6 -DPA_SDL=ON -DPA_FRLG_STORY_EXPORT=ON -DPA_FRLG_STORY_REPLAY=ON ${{env.CMAKE_ADDITIONAL_FLAGS}} + cmake .. -DQT_MAJOR:STRING=6 -DPA_SDL=ON -DPA_FRLG_STORY_REPLAY=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) + # Since #260 the checkpoint is authored as JSON and the C++ writer is gone + # (StoryCheckpointExport no longer builds): check and upload the plan itself. + - name: Check authored 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 + cp ../Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-02.01.plan.json 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 data['game'] == 'frlg' and data['checkpoint']['id'] == '02.01' assert len(data['checkpoint']['events']) == 3 PYTHON diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h index b19c74a333..f58692d503 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -20,6 +20,7 @@ #include "Pokemon/Options/Pokemon_StoryRangeOptions_Tests.h" #include "Pokemon_AutoStorySelect_Tests.h" #include "Pokemon_CheckpointRetry_Tests.h" +#include "Pokemon_FrameReplay_Tests.h" #include "Pokemon_Interrupts_Tests.h" #include "Pokemon_SceneJudges_Tests.h" #include "Pokemon_StoryActions_Tests.h" @@ -61,6 +62,7 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_SceneJudges(database); add_tests_Interrupts(database); add_tests_Transitions(database); + add_tests_FrameReplay(database); add_tests_StoryActions(database); add_tests_StoryInterrupts(database); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_FrameReplay.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_FrameReplay.cpp new file mode 100644 index 0000000000..e1b9df7eac --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_FrameReplay.cpp @@ -0,0 +1,121 @@ +/* Frame Replay + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "Pokemon_FrameReplay.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +// The time a frame file's name gives ("1250.png" -> 1250), or -1 when the +// name is not all digits or the file not a .png. +long long frame_time_of(const Filesystem::Path& file){ + if (file.extension().string() != ".png"){ + return -1; + } + const std::string stem = file.stem().string(); + if (stem.empty() || stem.size() > 9){ + return -1; + } + for (char c : stem){ + if (!std::isdigit((unsigned char)c)){ + return -1; + } + } + return std::stoll(stem); +} + +} // namespace + + +ReplayFrameSource::ReplayFrameSource(std::vector frames) + : m_frames(std::move(frames)) +{ + std::stable_sort(m_frames.begin(), m_frames.end(), [](const ReplayFrame& a, const ReplayFrame& b){ + return a.at < b.at; + }); +} + +ReplayFrameSource ReplayFrameSource::from_folder(const std::string& folder){ + std::vector frames; + if (Filesystem::is_directory(folder)){ + for (const Filesystem::Path& file : Filesystem::list_directory(folder)){ + const long long ms = frame_time_of(file); + if (ms >= 0){ + frames.push_back(ReplayFrame{.at = Milliseconds(ms), .file = file.string()}); + } + } + } + if (frames.empty()){ + throw FileException(nullptr, PA_CURRENT_FUNCTION, "No .png frames to replay.", folder); + } + return ReplayFrameSource(std::move(frames)); +} + +Milliseconds ReplayFrameSource::last_frame() const{ + return m_frames.empty() ? Milliseconds(0) : m_frames.back().at; +} + +VideoSnapshot ReplayFrameSource::frame_at(Milliseconds at, WallClock origin) const{ + auto after = std::upper_bound(m_frames.begin(), m_frames.end(), at, [](Milliseconds time, const ReplayFrame& frame){ + return time < frame.at; + }); + if (after == m_frames.begin()){ + return VideoSnapshot(); + } + ReplayFrame& frame = *(after - 1); + if (!frame.image){ + frame.image = std::make_shared(frame.file); + } + VideoSnapshot out; + out.frame = frame.image; + out.timestamp = origin + at; + return out; +} + + +ReplayHit replay_until( + const ReplayFrameSource& source, ReplayWindow window, + const std::vector& callbacks, + WallClock origin, Milliseconds period +){ + for (Milliseconds now = window.start; now < window.end; now += period){ + const VideoSnapshot frame = source.frame_at(now, origin); + if (!frame){ + continue; + } + for (size_t c = 0; c < callbacks.size(); c++){ + if (callbacks[c]->process_frame(frame)){ + return ReplayHit{.index = (int)c, .at = now}; + } + } + } + return ReplayHit{}; +} + +ScreenEventLog replay_events( + const ReplayFrameSource& source, ReplayWindow window, + ScreenEventObserver& observer, + WallClock origin, Milliseconds period +){ + ScreenEventLog log; + for (Milliseconds now = window.start; now < window.end; now += period){ + const VideoSnapshot frame = source.frame_at(now, origin); + log.record(observer.observe(frame, origin + now), origin + now); + } + return log; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_FrameReplay.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_FrameReplay.h new file mode 100644 index 0000000000..f2ea8473fc --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_FrameReplay.h @@ -0,0 +1,125 @@ +/* Frame Replay + * + * From: https://github.com/PokemonAutomation/ + * + * Recorded console frames, replayed through the real detectors on a + * virtual clock, so a console failure becomes a deterministic test. The + * story sims script what a screen shows (a label per frame); a replay + * shows what the console showed and lets the game's own watchers and + * ScreenEventObserver judge it. + * + * A recording is a folder of frames named by their time in ms from its + * start ("0.png", "1250.png"), as the captured transitions + * (Pokemon_Transitions_Tests.cpp). A frame shows until the next one. + * Recordings are too big for this repo; tests read them from + * CommandLineTests//FrameReplay// and skip when it is missing. + * + * From the stream history (an error report's Video.mp4, kept when the + * "Stream History" setting is on), cut the seconds around the failure at + * 20 frames a second, then rename frame N to its time, (N - 1) * 50 ms: + * ffmpeg -ss -t -i Video.mp4 -vf fps=20 %d.png + * + * A failed AutoStory event saves its own (Pokemon_AutoStoryFailureCapture.h) + * in USER_FILE_PATH()//AutoStoryFailures/