diff --git a/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEventTypes.h b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEventTypes.h index e9d05d5052..4b7f543a70 100644 --- a/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEventTypes.h +++ b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEventTypes.h @@ -33,10 +33,12 @@ enum class ScreenEvent : uint8_t{ PLAYER_CAN_MOVE, // The overworld with the player in control. SCREEN_STILL, // The screen barely changed since the frame before. SWITCH_HOME_MENU, // The Switch's HOME menu: the game is not on the screen. + PLAYER_STILL, // A box around the player barely changed since the + // frame before: people walking elsewhere do not count. }; // How many ScreenEvent values there are. -constexpr size_t SCREEN_EVENT_COUNT = 11; -static_assert((size_t)ScreenEvent::SWITCH_HOME_MENU + 1 == SCREEN_EVENT_COUNT, "SWITCH_HOME_MENU is the last ScreenEvent"); +constexpr size_t SCREEN_EVENT_COUNT = 12; +static_assert((size_t)ScreenEvent::PLAYER_STILL + 1 == SCREEN_EVENT_COUNT, "PLAYER_STILL is the last ScreenEvent"); // "dialog box", for the log. std::string screen_event_name(ScreenEvent event); diff --git a/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents.cpp b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents.cpp index 8269377758..770736e4d1 100644 --- a/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents.cpp +++ b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents.cpp @@ -29,6 +29,7 @@ std::string screen_event_name(ScreenEvent event){ case ScreenEvent::PLAYER_CAN_MOVE: return "player can move"; case ScreenEvent::SCREEN_STILL: return "screen still"; case ScreenEvent::SWITCH_HOME_MENU: return "Switch HOME menu"; + case ScreenEvent::PLAYER_STILL: return "player still"; } return "?"; } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.cpp index 26d7c2b2a8..e29d0265c7 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.cpp @@ -92,6 +92,12 @@ class LaunchDriver{ // One look and its answer; the launch's end when this look ended it. std::optional step(){ const Observation observation = m_backend.observe(); + if (m_looks_since_title_press){ + ++*m_looks_since_title_press; + if (*m_looks_since_title_press <= m_policy.load_black_looks && observation.events.has(ScreenEvent::FADE_BLACK)){ + m_load_seen = true; + } + } const LaunchLook look = read_launch_look(m_policy, observation, m_launched || m_loading); if (look.screen != LaunchScreen::OVERWORLD){ m_settled = 0; @@ -175,6 +181,9 @@ class LaunchDriver{ m_backend.press(each.button, each.hold, each.release); } } + // A black before this title was not the load. + m_load_seen = false; + m_looks_since_title_press = 0; return press("title", m_policy.title_press, m_policy.presses_per_screen); } @@ -182,6 +191,14 @@ class LaunchDriver{ if (needs_load()){ return press("overworld", {.button = BUTTON_HOME}, m_policy.presses_per_screen, "HOME, to load the save"); } + if (m_policy.overworld_after_load && m_loading && !m_load_seen){ + // Not the load's press: on the movie, it goes back to the + // title through a black. + return press( + "title", m_policy.title_press, m_policy.presses_per_screen, + std::format("no load yet, so the title's attract movie: {}", button_to_string(m_policy.title_press.button)) + ); + } if (++m_settled >= m_policy.settled_looks){ log("overworld", std::format("settled for {} looks", m_settled)); return outcome_ok(); @@ -237,6 +254,13 @@ class LaunchDriver{ bool m_launch_pressed = false; Milliseconds m_home_waited = Milliseconds(0); bool m_backup_sent = false; + // Looks since the title press was sent on a look `policy.title` read + // as the title; nothing before one. A press on the attract movie + // (`overworld_after_load`) is not a title press. + std::optional m_looks_since_title_press; + // A black screen showed within `policy.load_black_looks` looks of the + // title press, and the title has not shown since: the save loading. + bool m_load_seen = false; }; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.h index eb90245801..1ede81e218 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.h @@ -116,6 +116,18 @@ struct StoryLaunchPolicy{ std::vector prompts = {}; // The player in control. Unset: the look shows PLAYER_CAN_MOVE. std::function overworld = {}; + // Once the launch closed the game or saw its title, the overworld + // counts only after a `title_press` on a look `title` matched and + // then the load's black (FADE_BLACK), with no title since; until then + // an overworld look takes `title_press` too, which does not start the + // load. BDSP's title plays an attract movie, a town with people + // walking, that reads as the overworld, and ZL on it goes back to + // the title through a black. + bool overworld_after_load = false; + // For `overworld_after_load`: the load's black comes within this many + // looks of the title press. A later black is the title, left idle + // after a dropped press, going to its attract movie. + size_t load_black_looks = 10; // Looks in a row the overworld must show before the launch is done. size_t settled_looks = 3; // The wait between looks at a screen with nothing to press. diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch_Tests.cpp index 1853b06ac0..9c6b5af290 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch_Tests.cpp @@ -5,8 +5,9 @@ * The launch on a scripted console: each case is the screens the console * shows, look by look (the last repeats), and the presses the launch must * make, or where it must fail. The test game's screens are readings its - * policy reads; HOME is the shared SWITCH_HOME_MENU event and the - * overworld the shared PLAYER_CAN_MOVE. + * policy reads; HOME is the shared SWITCH_HOME_MENU event, a boot's or + * load's black the shared FADE_BLACK and the overworld the shared + * PLAYER_CAN_MOVE. * */ @@ -38,6 +39,7 @@ enum class Screen{ LICENSE_CHECK, // the online license check BLACK, // a boot or a load TITLE, + TITLE_UNREAD, // the title, on a look its check misses NEW_GAME, // the title's menu without a save BACKUP_CONFIRM, // "Load the backup save?" LOAD_MENU, // Continue / New Game @@ -60,6 +62,9 @@ Observation look(Screen screen){ case Screen::OVERWORLD: observation.events = ScreenEvents{ScreenEvent::PLAYER_CAN_MOVE}; return observation; + case Screen::BLACK: + observation.events = ScreenEvents{ScreenEvent::FADE_BLACK}; + return observation; default: observation.readings[std::string(SCREEN)] = (int)screen; return observation; @@ -91,10 +96,16 @@ enum class GameScreens{ AFTER_LAUNCH, // read only once the launch launched the game }; +// When the test game's overworld counts after the launch closed it. +enum class OverworldCheck{ + ANY_TIME, + AFTER_LOAD, // only after a title press and the load's black +}; + // The test game: A on the title, `backup` for the backup save, A on each // prompt (Up, Up, A on the update prompt; B on the game's dialog), and // small bounds so a failure takes few looks. -StoryLaunchPolicy test_policy(Backup backup, GameScreens game_screens, TileCheck tile_check){ +StoryLaunchPolicy test_policy(Backup backup, GameScreens game_screens, TileCheck tile_check, OverworldCheck overworld_check){ StoryLaunchPolicy policy; policy.game = "test"; policy.tile_selected = [](const ScreenObservation& observation){ @@ -120,7 +131,9 @@ StoryLaunchPolicy test_policy(Backup backup, GameScreens game_screens, TileCheck .after_launch = game_screens == GameScreens::AFTER_LAUNCH}, }; policy.title_after_launch = game_screens == GameScreens::AFTER_LAUNCH; + policy.overworld_after_load = overworld_check == OverworldCheck::AFTER_LOAD; policy.presses_per_screen = 3; + policy.load_black_looks = 3; policy.launch_wait = Milliseconds(2000); policy.max_looks = 12; return policy; @@ -143,6 +156,7 @@ struct LaunchCase{ Backup backup = Backup::PLZA; GameScreens game_screens = GameScreens::STRICT; TileCheck tile_check = TileCheck::READINGS; + OverworldCheck overworld_check = OverworldCheck::ANY_TIME; }; struct Expectation{ // Empty: the launch succeeds; else a part of its failure's detail. @@ -225,6 +239,45 @@ const std::vector LAUNCH_CASES = { Screen::BLACK, Screen::GAME_DIALOG, Screen::TITLE}), .game_screens = GameScreens::AFTER_LAUNCH}, .expectation = {.inputs = "HOME HOME X A wait B A wait wait"}}, + // Console B, 2026-11-15 03:42 (BDSP): after the launch the title's + // attract movie, a town with people walking, read as the overworld + // and settled before the title took a press. + {.name = "attract movie never counts as the overworld", + .input = {.screens = {Screen::HOME_ON_GAME, Screen::HOME_ON_GAME, Screen::BLACK, Screen::OVERWORLD}, + .game_screens = GameScreens::AFTER_LAUNCH, .overworld_check = OverworldCheck::AFTER_LOAD}, + .expectation = {.failure = "stuck on title", .inputs = "X A wait A A A"}}, + {.name = "attract movie takes the title press", + .input = {.screens = then_settled({Screen::HOME_ON_GAME, Screen::HOME_ON_GAME, Screen::BLACK, Screen::OVERWORLD, + Screen::TITLE, Screen::BLACK}), + .game_screens = GameScreens::AFTER_LAUNCH, .overworld_check = OverworldCheck::AFTER_LOAD}, + .expectation = {.inputs = "X A wait A A wait wait wait"}}, + // Console B, 2026-11-15 04:21 (BDSP, Japanese): the title press on the + // attract movie went back to the title through a black, and that + // black counted as the load; the title, read as the overworld, + // settled. Only a press on a recognised title starts the load. + {.name = "a black after the attract movie's press is not the load", + .input = {.screens = {Screen::HOME_ON_GAME, Screen::HOME_ON_GAME, Screen::BLACK, Screen::OVERWORLD, Screen::BLACK, + Screen::OVERWORLD}, + .game_screens = GameScreens::AFTER_LAUNCH, .overworld_check = OverworldCheck::AFTER_LOAD}, + .expectation = {.failure = "stuck on title", .inputs = "X A wait A wait A A"}}, + // A title that shows again after a black: that black was not the load. + {.name = "the title again after a black is not loaded", + .input = {.screens = {Screen::HOME_ON_GAME, Screen::HOME_ON_GAME, Screen::BLACK, Screen::TITLE, Screen::BLACK, + Screen::TITLE, Screen::OVERWORLD}, + .game_screens = GameScreens::AFTER_LAUNCH, .overworld_check = OverworldCheck::AFTER_LOAD}, + .expectation = {.failure = "stuck on title", .inputs = "X A wait A wait A A"}}, + // A title press dropped, and the title, its check missing it, left + // idle until the attract movie came up through a black: that black + // is too long after the press to be the load. + {.name = "a black long after a dropped title press is not the load", + .input = {.screens = {Screen::HOME_ON_GAME, Screen::HOME_ON_GAME, Screen::BLACK, Screen::TITLE, Screen::TITLE_UNREAD, + Screen::TITLE_UNREAD, Screen::TITLE_UNREAD, Screen::BLACK, Screen::OVERWORLD}, + .game_screens = GameScreens::AFTER_LAUNCH, .overworld_check = OverworldCheck::AFTER_LOAD}, + .expectation = {.failure = "stuck on title", .inputs = "X A wait A wait wait wait wait A A"}}, + {.name = "kept overworld needs no load", + .input = {.screens = then_settled({}), .request = {.running = Running::KEEP}, + .game_screens = GameScreens::AFTER_LAUNCH, .overworld_check = OverworldCheck::AFTER_LOAD}, + .expectation = {.inputs = "wait wait"}}, }; // The fake backend's record of `input` ("A" -> "press A"). @@ -265,7 +318,7 @@ std::string check_launch(const LaunchCase& test, CancellableScope& scope){ looks.push_back(look(screen)); } FakeStoryBackend backend({.looks = std::move(looks)}, scope); - const Outcome outcome = launch_story_game(backend, test_policy(test.input.backup, test.input.game_screens, test.input.tile_check), test.input.request); + const Outcome outcome = launch_story_game(backend, test_policy(test.input.backup, test.input.game_screens, test.input.tile_check, test.input.overworld_check), test.input.request); if (test.expectation.failure.empty() && !outcome.succeeded()){ return std::format("expected success, got {}", to_string(outcome)); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h index d84361dded..577603f577 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -24,9 +24,11 @@ #include "Engine/Pokemon_StoryEngine_Tests.h" #include "Pokemon/Options/Pokemon_StoryRangeOptions_Tests.h" #include "Pokemon_AutoStorySelect_Tests.h" +#include "Pokemon_CenterVisit_Tests.h" #include "Pokemon_CheckpointRetry_Tests.h" #include "Pokemon_Interrupts_Tests.h" #include "Pokemon_SceneJudges_Tests.h" +#include "Pokemon_Settle_Tests.h" #include "Pokemon_StoryActions_Tests.h" #include "Pokemon_StoryEvents_Tests.h" #include "Pokemon_StoryFailureClasses_Tests.h" @@ -73,6 +75,8 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_FrameCoverage(database); add_tests_CoveredWatch(database); add_tests_SceneJudges(database); + add_tests_Settle(database); + add_tests_CenterVisit(database); add_tests_Interrupts(database); add_tests_Transitions(database); add_tests_StoryActions(database); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CenterVisit.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CenterVisit.cpp new file mode 100644 index 0000000000..962440ce67 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CenterVisit.cpp @@ -0,0 +1,102 @@ +/* Center Visit + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "Pokemon_CenterVisit.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +std::string_view place_name(CenterPlace place){ + switch (place){ + case CenterPlace::INSIDE: return "inside"; + case CenterPlace::OUTSIDE: return "outside"; + case CenterPlace::UNKNOWN: return "unknown"; + } + return "?"; +} + +// The settle after a door, as the visit's `failure` unless it ended +// quiet. +std::optional settle_door(SettleKind kind, std::string_view label, std::string_view failure, CenterVisitIO& io){ + const SettleEnd end = io.settle(kind, label); + if (end.result == SettleEnd::Result::QUIET){ + return std::nullopt; + } + return outcome_failed(std::string(failure), end.text); +} + +} // namespace + + +Outcome enter_center(std::string_view label, CenterVisitIO& io){ + // Every way in settles before the walk to the nurse: the inside can + // still be fading in whether or not the walk saw the fade. With no + // fade, the walk can have gone on in as far as the counter (BDSP lab-B + // 2026-11-08, Oreburgh). + // A lineup that saw no fade either settles AFTER_WARP: it can return + // on the mat before the fade starts. + const WarpSeen seen = io.walk_in(); + std::string how = describe(seen); + SettleKind kind = SettleKind::DOOR; + if (!seen.any()){ + if (io.place() == CenterPlace::INSIDE){ + how = "no fade, but inside"; + }else{ + const WarpSeen lined_up = io.line_up_and_walk_in(); + how = std::format("no fade; lined up by sight: {}", describe(lined_up)); + if (!lined_up.any()){ + kind = SettleKind::AFTER_WARP; + } + } + } + if (std::optional failed = settle_door(kind, std::format("{}: into the Center", label), CENTER_NOT_ENTERED, io)){ + return std::move(*failed); + } + const CenterPlace place = io.place(); + io.log(std::format("[Center] {}: in: {}; reads {}.", label, how, place_name(place))); + if (place == CenterPlace::OUTSIDE){ + return outcome_failed(std::string(CENTER_NOT_ENTERED), "not in a Pokemon Center after its door"); + } + return outcome_ok(); +} + +Outcome leave_center(std::string_view label, CenterVisitIO& io){ + // With no fade seen yet, the fade can still be to come: the settle + // waits for it, and fails when none shows. + const WarpSeen seen = io.walk_out(); + const SettleKind kind = seen.any() ? SettleKind::DOOR : SettleKind::AFTER_WARP; + if (std::optional failed = settle_door(kind, std::format("{}: out of the Center", label), CENTER_NOT_LEFT, io)){ + return std::move(*failed); + } + const CenterPlace place = io.place(); + io.log(std::format("[Center] {}: out: {}; reads {}.", label, describe(seen), place_name(place))); + if (place == CenterPlace::INSIDE){ + return outcome_failed(std::string(CENTER_NOT_LEFT), "still inside the Pokemon Center after its door"); + } + return outcome_ok(); +} + +Outcome run_center_visit(std::string_view label, CenterVisitIO& io){ + const Outcome in = enter_center(label, io); + if (!in.succeeded()){ + return in; + } + io.heal(); + io.log(std::format("[Center] {}: healed.", label)); + return leave_center(label, io); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CenterVisit.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CenterVisit.h new file mode 100644 index 0000000000..841e64cf20 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CenterVisit.h @@ -0,0 +1,78 @@ +/* Center Visit + * + * From: https://github.com/PokemonAutomation/ + * + * A Pokemon Center visit for every game: in through the door, wait out + * the fade-in, heal, out through the door, wait out the fade, and the two + * checks that the doors took us where they should. A game gives the + * walks, the reads and the nurse through CenterVisitIO; the order, the + * settles and the checks are here. + * + */ + +#ifndef PokemonAutomation_Pokemon_CenterVisit_H +#define PokemonAutomation_Pokemon_CenterVisit_H + +#include +#include "Pokemon_SceneJudges.h" +#include "Pokemon_Settle.h" +#include "Pokemon_StoryOutcome.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Where a game's reads say the player is. +enum class CenterPlace{ + INSIDE, // a Center's floor or counter shows + OUTSIDE, // something else shows + UNKNOWN, // the game cannot tell from this screen +}; + +// The failure of a visit that is not in the Center after walking in. +inline constexpr std::string_view CENTER_NOT_ENTERED = "heal_enter"; +// The failure of a visit still in the Center after walking out. +inline constexpr std::string_view CENTER_NOT_LEFT = "heal_exit"; + + +// What a visit needs of a game. Its walks and the nurse fail by throwing, +// as the game's own steps do. +class CenterVisitIO{ +public: + virtual ~CenterVisitIO() = default; + + // From below the door, walks in. Returns what it saw of the warp. + virtual WarpSeen walk_in() = 0; + // Lines up with the door by sight and walks in: the walk in saw no + // warp and the inside does not show. Returns what it saw of the warp. + virtual WarpSeen line_up_and_walk_in() = 0; + virtual CenterPlace place() = 0; + // From inside the door, walks to the nurse, heals, and clears her + // last line. + virtual void heal() = 0; + // From the nurse, walks out. Returns what it saw of the warp. + virtual WarpSeen walk_out() = 0; + // Waits until the game settles (run_settle() or the sim's own). A + // visit fails on any end but QUIET, with the end's text. + virtual SettleEnd settle(SettleKind kind, std::string_view label) = 0; + virtual void log(std::string_view line) = 0; +}; + +// Walks in and waits out the fade-in. Fails CENTER_NOT_ENTERED when the +// settle does not end quiet or the reads then say outside: the nurse's +// presses would land elsewhere (FRLG console B 2026-11-13, 12.03). +// UNKNOWN passes. +Outcome enter_center(std::string_view label, CenterVisitIO& io); +// Walks out and waits out the fade. Fails CENTER_NOT_LEFT when the +// settle does not end quiet or the reads then say inside (BDSP r143a). +// UNKNOWN passes. +Outcome leave_center(std::string_view label, CenterVisitIO& io); +// enter_center(), the nurse, leave_center(): from below the door back to +// below it. +Outcome run_center_visit(std::string_view label, CenterVisitIO& io); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CenterVisit_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CenterVisit_Tests.cpp new file mode 100644 index 0000000000..2f11f00474 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CenterVisit_Tests.cpp @@ -0,0 +1,203 @@ +/* Center Visit Tests + * + * From: https://github.com/PokemonAutomation/ + * + * run_center_visit() on a fake game: the order of its calls, which + * settle follows each door, and the two guards that fail a visit. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon_CenterVisit.h" +#include "Pokemon_CenterVisit_Tests.h" +#include "Pokemon_TestExpect.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + +namespace{ + + +const WarpSeen FADE{.fade = WarpFade::BLACK}; +const WarpSeen NO_FADE{}; + +using SettleResult = SettleEnd::Result; + +struct VisitTestCase{ + // What the fake game does, call by call. + struct Input{ + WarpSeen in; + // What the lineup after a walk in without a fade sees. + WarpSeen lined_up = FADE; + // What place() reads, call by call; UNKNOWN after the last. + std::vector places; + WarpSeen out; + // How each settle ends, call by call; QUIET after the last. + std::vector settles = {}; + }; + + struct Expectation{ + std::string calls; + // The Outcome's failure; nothing for a visit that succeeds. + std::optional failure = std::nullopt; + }; + + std::string name; + Input input; + Expectation expectation; +}; + +using VisitScript = VisitTestCase::Input; + +// A game whose doors and reads are scripted. It writes each call down +// in `calls`, in order: "in settle_door inside heal ...". +class FakeCenterVisitIO : public CenterVisitIO{ +public: + FakeCenterVisitIO(VisitScript script, std::string& calls) + : m_script(std::move(script)) + , m_calls(calls) + {} + + virtual WarpSeen walk_in() override{ + note("in"); + return m_script.in; + } + virtual WarpSeen line_up_and_walk_in() override{ + note("line_up"); + return m_script.lined_up; + } + virtual CenterPlace place() override{ + const CenterPlace place = m_next_place < m_script.places.size() ? m_script.places[m_next_place] : CenterPlace::UNKNOWN; + m_next_place++; + note(place == CenterPlace::INSIDE ? "inside" : place == CenterPlace::OUTSIDE ? "outside" : "unknown"); + return place; + } + virtual void heal() override{ + note("heal"); + } + virtual WarpSeen walk_out() override{ + note("out"); + return m_script.out; + } + virtual SettleEnd settle(SettleKind kind, std::string_view) override{ + note(kind == SettleKind::DOOR ? "settle_door" : kind == SettleKind::AFTER_WARP ? "settle_warp" : "settle_talk"); + const SettleResult result = m_next_settle < m_script.settles.size() ? m_script.settles[m_next_settle] : SettleResult::QUIET; + m_next_settle++; + return {result, result == SettleResult::QUIET ? "settled" : "not settled"}; + } + virtual void log(std::string_view) override{} + +private: + void note(std::string_view call){ + if (!m_calls.empty()){ + m_calls += " "; + } + m_calls += call; + } + + const VisitScript m_script; + size_t m_next_place = 0; + size_t m_next_settle = 0; + std::string& m_calls; +}; + + +class Test_Visit : public UnitTest{ +public: + explicit Test_Visit(VisitTestCase test_case) + : UnitTest(std::format("Pokemon::CenterVisit - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + std::string calls; + FakeCenterVisitIO io(m_case.input, calls); + const Outcome outcome = run_center_visit("Heal", io); + const std::string got = outcome.succeeded() ? "succeeded" : outcome.failure; + const UnitTestResult result = expect_equal("outcome", got, m_case.expectation.failure.value_or("succeeded")); + if (result.result != UnitTestResult::PASSED){ + return result; + } + return expect_equal("calls", calls, m_case.expectation.calls); + } + +private: + const VisitTestCase m_case; +}; + +using P = CenterPlace; + +const VisitTestCase VISIT_CASES[] = { + { + .name = "in by the fade, healed, out by the fade", + .input = {.in = FADE, .places = {P::INSIDE, P::OUTSIDE}, .out = FADE}, + .expectation = {.calls = "in settle_door inside heal out settle_door outside"}, + }, + { + .name = "no fade in, but the inside shows: no lineup", + .input = {.in = NO_FADE, .places = {P::INSIDE, P::INSIDE, P::OUTSIDE}, .out = FADE}, + .expectation = {.calls = "in inside settle_door inside heal out settle_door outside"}, + }, + { + .name = "no fade in and no inside: lines up with the door", + .input = {.in = NO_FADE, .places = {P::OUTSIDE, P::INSIDE, P::OUTSIDE}, .out = FADE}, + .expectation = {.calls = "in outside line_up settle_door inside heal out settle_door outside"}, + }, + { + .name = "no fade in, and none in the lineup: the settle waits for the warp", + .input = {.in = NO_FADE, .lined_up = NO_FADE, .places = {P::OUTSIDE, P::INSIDE, P::OUTSIDE}, .out = FADE}, + .expectation = {.calls = "in outside line_up settle_warp inside heal out settle_door outside"}, + }, + { + .name = "settle into the Center times out: fails before the nurse", + .input = {.in = FADE, .places = {P::INSIDE}, .out = FADE, .settles = {SettleResult::TIMED_OUT}}, + .expectation = {.calls = "in settle_door", .failure = std::string(CENTER_NOT_ENTERED)}, + }, + { + .name = "settle out of the Center times out: fails", + .input = {.in = FADE, .places = {P::INSIDE, P::OUTSIDE}, .out = NO_FADE, + .settles = {SettleResult::QUIET, SettleResult::TIMED_OUT}}, + .expectation = {.calls = "in settle_door inside heal out settle_warp", .failure = std::string(CENTER_NOT_LEFT)}, + }, + { + .name = "outside after the way in: fails before the nurse", + .input = {.in = FADE, .places = {P::OUTSIDE}, .out = FADE}, + .expectation = {.calls = "in settle_door outside", .failure = std::string(CENTER_NOT_ENTERED)}, + }, + { + .name = "no fade out yet: the settle waits for the warp", + .input = {.in = FADE, .places = {P::INSIDE, P::OUTSIDE}, .out = NO_FADE}, + .expectation = {.calls = "in settle_door inside heal out settle_warp outside"}, + }, + { + .name = "still inside after the way out: fails", + .input = {.in = FADE, .places = {P::INSIDE, P::INSIDE}, .out = FADE}, + .expectation = {.calls = "in settle_door inside heal out settle_door inside", .failure = std::string(CENTER_NOT_LEFT)}, + }, + { + .name = "a game that cannot read the place: no guard fails", + .input = {.in = FADE, .places = {}, .out = FADE}, + .expectation = {.calls = "in settle_door unknown heal out settle_door unknown"}, + }, +}; + +} // namespace + + +void add_tests_CenterVisit(UnitTestDatabase& database){ + for (const VisitTestCase& item : VISIT_CASES){ + database.add(item); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CenterVisit_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CenterVisit_Tests.h new file mode 100644 index 0000000000..18d07b63f4 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CenterVisit_Tests.h @@ -0,0 +1,23 @@ +/* Center Visit Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_CenterVisit_Tests_H +#define PokemonAutomation_Pokemon_CenterVisit_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for a Pokemon Center visit: in, heal, out. +void add_tests_CenterVisit(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp index 907ec813c1..1741c4317c 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp @@ -51,7 +51,7 @@ SceneMove TalkOver::look(const ScreenEvents& events, WallClock now){ // ended the scene as it went down. const bool calm = !box && m_fade_showing == WarpFade::NONE && events.has(ScreenEvent::PLAYER_CAN_MOVE) && warped(); - const bool quiet = calm && events.has(ScreenEvent::SCREEN_STILL); + const bool quiet = calm && events.has(m_rule.still); if (!calm){ m_calm_since.reset(); }else if (!m_calm_since.has_value()){ @@ -63,7 +63,7 @@ SceneMove TalkOver::look(const ScreenEvents& events, WallClock now){ m_quiet_since = now; } const bool over = - (quiet && now - *m_quiet_since >= TALK_OVER_QUIET) || + (quiet && now - *m_quiet_since >= m_rule.quiet) || (calm && now - *m_calm_since >= m_rule.calm); if (over && m_rule.end_on_quiet){ return SceneMove::DONE; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h index 6e924a5c33..8f6eb340e9 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h @@ -71,10 +71,16 @@ struct TalkOverRule{ // lines: PLZA's party bar, held 1 s, is the overworld; its city is // rarely still. Milliseconds calm = TALK_OVER_CALM; + // How long the overworld still and with no box ends it. + Milliseconds quiet = TALK_OVER_QUIET; + // What still means: the whole screen, or only the box around the + // player (PLAYER_STILL), so people walking about neither hold the + // quiet up nor end it early. + ScreenEvent still = ScreenEvent::SCREEN_STILL; }; // When a talk or a scene is over, from looks alone. A look is a frame's -// events: DIALOG_BOX, PLAYER_CAN_MOVE, SCREEN_STILL, a fade, +// events: DIALOG_BOX, PLAYER_CAN_MOVE, SCREEN_STILL (or the rule's `still`), a fade, // LOCATION_BANNER (the expected map's) and BATTLE_STARTED are read. // It is over only once every fade has come back up, no box shows, and // the overworld has been quiet (or calm) since: a look that shows a fade diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle.cpp new file mode 100644 index 0000000000..3038c6bbe3 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle.cpp @@ -0,0 +1,106 @@ +/* Settle + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Pokemon_Settle.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + +namespace{ + +// A door's settle: the player still this long after the fade is back up +// ends it; a walk any earlier could still go nowhere in the fade-in +// (BDSP lab-B 2026-11-10, 00.04b's nurse). +constexpr Milliseconds DOOR_QUIET = Milliseconds(1000); +// A door's settle: the overworld back up this long ends it, still or not +// (someone walking past the player the whole time). +constexpr Milliseconds DOOR_CALM = Milliseconds(2500); + +} // namespace + + +SettleRule settle_rule(SettleKind kind){ + switch (kind){ + case SettleKind::TALK: + return {}; + case SettleKind::AFTER_WARP: + return {.judge = {.warp = true}}; + case SettleKind::DOOR: + return { + .judge = {.calm = DOOR_CALM, .quiet = DOOR_QUIET, .still = ScreenEvent::PLAYER_STILL}, + .look_every = Milliseconds(200), + }; + } + return {}; +} + + +namespace{ + +// Why a settle that hit its cap failed. +std::string timed_out(const TalkOver& judge, const SettleRule& rule, std::string_view label){ + const long long seconds = std::chrono::duration_cast(rule.cap).count(); + if (!judge.warped()){ + return std::format("{}: no fade or banner within {} s, so we are not where the step goes (map mismatch).", label, seconds); + } + if (!rule.judge.end_on_quiet){ + return std::format("{}: no battle within {} s of the talk.", label, seconds); + } + return std::format("{}: not back on a quiet overworld within {} s.", label, seconds); +} + +} // namespace + + +SettleEnd run_settle(const SettleRule& rule, std::string_view label, SettleIO& io){ + TalkOver judge(rule.judge); + std::optional deadline; + while (true){ + const SettleLook look = io.look(); + if (!deadline.has_value()){ + deadline = look.when + rule.cap; + } + if (look.when >= *deadline){ + break; + } + if (look.since.has_value() && *look.since < look.when){ + // The screen showed these events from `since` on: the judge + // sees them then too, so their quiet counts from there. + judge.look(look.events, *look.since); + } + const SceneMove move = judge.look(look.events, look.when); + switch (move){ + case SceneMove::DONE: + io.log(std::format("[Settle] {}: over; {}.", label, judge.describe())); + return {SettleEnd::Result::QUIET, judge.describe()}; + case SceneMove::BATTLE: + if (rule.watch_battles){ + continue; + } + if (!io.flee_battle()){ + io.log(std::format("[Settle] {}: a battle started.", label)); + return {SettleEnd::Result::BATTLE, judge.describe()}; + } + io.log(std::format("[Settle] {}: fled a wild battle.", label)); + continue; + case SceneMove::NEXT: + case SceneMove::B: + io.press(move, rule.look_every); + continue; + } + } + const std::string failure = timed_out(judge, rule, label); + io.log(std::format("[Settle] {}", failure)); + return {SettleEnd::Result::TIMED_OUT, failure}; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle.h new file mode 100644 index 0000000000..108ff59ecb --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle.h @@ -0,0 +1,107 @@ +/* Settle + * + * From: https://github.com/PokemonAutomation/ + * + * How a story step waits for the game to settle after a talk, a scene, a + * warp or a door: one loop over TalkOver (Pokemon_SceneJudges.h) for + * every game, the console and the sims alike. A game gives it the looks + * and the presses through SettleIO; the rule for each kind of settle is + * one table here. + * + */ + +#ifndef PokemonAutomation_Pokemon_Settle_H +#define PokemonAutomation_Pokemon_Settle_H + +#include +#include +#include +#include "Common/Cpp/Time.h" +#include "CommonTools/ScreenEvents/ScreenEventTypes.h" +#include "Pokemon_SceneJudges.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// What ends a settle. +enum class SettleKind{ + // Text and scenes pressed through, then the overworld quiet. + TALK, + // As TALK, but a warp (a map change) must come first: a fade, or the + // far map's banner. + AFTER_WARP, + // After a door or a way through with no text to come: the fade back + // up and a short still spell around the player (PLAYER_STILL), people + // walking about ignored. + DOOR, +}; + +// How a settle looks and how long it may take. +struct SettleRule{ + // What ends it. + TalkOverRule judge = {}; + // The wait after a press before the next look: a line's text takes + // about a second to run out; a door has no text and looks often to + // catch its short still spell. + Milliseconds look_every = Milliseconds(1200); + // How long it may take before it fails. + Milliseconds cap = Milliseconds(120000); + // For a game that only watches: a battle on screen is watched like any + // other unsettled screen, until the overworld is quiet after it. No + // flee_battle(), no press; its look() is the wait. + bool watch_battles = false; +}; + +// The rule of `kind`. +SettleRule settle_rule(SettleKind kind); + + +// One look at the screen: its events, and when it was taken. +struct SettleLook{ + ScreenEvents events; + WallClock when; + // Since when the screen has shown `events` unchanged, for a look that + // watches a span: a quiet that began early in it counts from then, not + // from `when`. None: from `when`. + std::optional since = std::nullopt; +}; + +// What run_settle() needs of a game: its looks and its presses. +class SettleIO{ +public: + virtual ~SettleIO() = default; + + // The screen now. + virtual SettleLook look() = 0; + // Answers a look (SceneMove::NEXT: the talk's button; SceneMove::B: + // B), then waits `wait`. A game that only watches waits. + virtual void press(SceneMove move, Milliseconds wait) = 0; + // A battle started: flees a wild one and returns true, or returns + // false for one that ends the settle (a trainer's after their talk). + virtual bool flee_battle() = 0; + virtual void log(std::string_view line) = 0; +}; + +// How a settle ended. +struct SettleEnd{ + enum class Result{ + QUIET, // The rule's end: the overworld quiet (or calm). + BATTLE, // A battle the IO did not flee. + TIMED_OUT, // The cap: the step fails. + }; + + Result result; + // What it saw ("black fade back up"); for TIMED_OUT, the failure. + std::string text; +}; + +// Looks and presses until `rule` says the game has settled, a battle +// ends it, or the cap. `label` names the step in the log and the failure. +SettleEnd run_settle(const SettleRule& rule, std::string_view label, SettleIO& io); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle_Tests.cpp new file mode 100644 index 0000000000..31589378a6 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle_Tests.cpp @@ -0,0 +1,410 @@ +/* Settle Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The rule table for each SettleKind, and run_settle() on a scripted + * screen: what it presses, when it ends and how it fails. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon_Settle.h" +#include "Pokemon_Settle_Tests.h" +#include "Pokemon_TestExpect.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + +namespace{ + +using namespace std::chrono_literals; + + +// A rule's fields, for comparing and for messages. +std::string describe(const SettleRule& rule){ + return std::format( + "warp={} quiet={}ms calm={}ms still={} look={}ms cap={}ms", + rule.judge.warp, rule.judge.quiet.count(), rule.judge.calm.count(), + screen_event_name(rule.judge.still), rule.look_every.count(), rule.cap.count() + ); +} + + +// The rule a kind of settle runs by. +struct RuleTestCase{ + struct Input{ + SettleKind kind; + }; + + struct Expectation{ + std::string rule; + }; + + std::string name; + Input input; + Expectation expectation; +}; + +class Test_Rule : public UnitTest{ +public: + explicit Test_Rule(RuleTestCase test_case) + : UnitTest(std::format("Pokemon::Settle - rule: {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return expect_equal("settle_rule()", describe(settle_rule(m_case.input.kind)), m_case.expectation.rule); + } + +private: + const RuleTestCase m_case; +}; + +const RuleTestCase RULE_CASES[] = { + { + .name = "a talk waits out 5 s of quiet", + .input = {SettleKind::TALK}, + .expectation = {"warp=false quiet=5000ms calm=15000ms still=screen still look=1200ms cap=120000ms"}, + }, + { + .name = "after a warp, the fade or banner comes first", + .input = {SettleKind::AFTER_WARP}, + .expectation = {"warp=true quiet=5000ms calm=15000ms still=screen still look=1200ms cap=120000ms"}, + }, + { + .name = "a door waits 1 s of the player still, looking often", + .input = {SettleKind::DOOR}, + .expectation = {"warp=false quiet=1000ms calm=2500ms still=player still look=200ms cap=120000ms"}, + }, +}; + + +// The screen a settle sees, a look per press: the script's looks in +// order, then its last one for good. Each press and each fled battle +// moves the clock on. +class ScriptedSettleIO : public SettleIO{ +public: + ScriptedSettleIO(std::vector looks, bool flee) + : m_looks(std::move(looks)) + , m_flee(flee) + {} + + virtual SettleLook look() override{ + const size_t index = m_next < m_looks.size() ? m_next : m_looks.size() - 1; + m_next++; + return {m_looks[index], m_now}; + } + virtual void press(SceneMove move, Milliseconds wait) override{ + m_presses += move == SceneMove::NEXT ? "N" : "B"; + m_now += wait; + } + virtual bool flee_battle() override{ + m_presses += "F"; + m_now += 5000ms; + return m_flee; + } + virtual void log(std::string_view) override{} + + // What it pressed: N for the talk's button, B for B, F for a flee. + const std::string& presses() const{ + return m_presses; + } + +private: + const std::vector m_looks; + const bool m_flee; + size_t m_next = 0; + WallClock m_now = WallClock(); + std::string m_presses; +}; + +const ScreenEvents QUIET{ScreenEvent::PLAYER_CAN_MOVE, ScreenEvent::SCREEN_STILL, ScreenEvent::PLAYER_STILL}; +const ScreenEvents BOX{ScreenEvent::DIALOG_BOX, ScreenEvent::SCREEN_STILL}; +// The overworld with someone walking past: the screen moves, the player does not. +const ScreenEvents PEOPLE_WALKING{ScreenEvent::PLAYER_CAN_MOVE, ScreenEvent::PLAYER_STILL}; +// A black frame as BDSP's eyes see it: no box, so "overworld", and still. +const ScreenEvents FADED{ScreenEvent::FADE_BLACK, ScreenEvent::PLAYER_CAN_MOVE, ScreenEvent::SCREEN_STILL, ScreenEvent::PLAYER_STILL}; +const ScreenEvents BATTLE{ScreenEvent::BATTLE_STARTED}; + +std::vector repeat(const ScreenEvents& look, size_t count){ + return std::vector(count, look); +} +std::vector concat(std::vector a, const std::vector& b){ + a.insert(a.end(), b.begin(), b.end()); + return a; +} + +std::string_view result_name(SettleEnd::Result result){ + switch (result){ + case SettleEnd::Result::QUIET: return "quiet"; + case SettleEnd::Result::BATTLE: return "battle"; + case SettleEnd::Result::TIMED_OUT: return "timed out"; + } + return "?"; +} + + +// run_settle() over a scripted screen. +struct RunTestCase{ + struct Input{ + SettleRule rule; + std::vector looks; + // Whether a battle that starts is fled (a wild one). + bool flee = true; + }; + + struct Expectation{ + SettleEnd::Result result; + // The presses (ScriptedSettleIO::presses()). + std::string presses; + // The end's text, checked for a timeout only: its failure. + std::string failure = {}; + }; + + std::string name; + Input input; + Expectation expectation; +}; + +class Test_Run : public UnitTest{ +public: + explicit Test_Run(RunTestCase test_case) + : UnitTest(std::format("Pokemon::Settle - run: {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + ScriptedSettleIO io(m_case.input.looks, m_case.input.flee); + const SettleEnd end = run_settle(m_case.input.rule, "Step", io); + if (end.result != m_case.expectation.result){ + return std::format("expected {}, got {} ({})", result_name(m_case.expectation.result), result_name(end.result), end.text); + } + if (end.result == SettleEnd::Result::TIMED_OUT){ + const UnitTestResult text = expect_equal("failure", end.text, m_case.expectation.failure); + if (text.result != UnitTestResult::PASSED){ + return text; + } + } + return expect_equal("presses", io.presses(), m_case.expectation.presses); + } + +private: + const RunTestCase m_case; +}; + +SettleRule talk_before_battle(){ + SettleRule rule = settle_rule(SettleKind::TALK); + rule.judge.end_on_quiet = false; + return rule; +} + +const RunTestCase RUN_CASES[] = { + { + .name = "a talk presses B while quiet, until 5 s of it", + .input = {.rule = settle_rule(SettleKind::TALK), .looks = {QUIET}}, + .expectation = {.result = SettleEnd::Result::QUIET, .presses = "BBBBB"}, + }, + { + .name = "a box is answered with the talk's button", + .input = {.rule = settle_rule(SettleKind::TALK), .looks = concat(repeat(BOX, 2), {QUIET})}, + .expectation = {.result = SettleEnd::Result::QUIET, .presses = "NNBBBBB"}, + }, + { + .name = "a door's settle ends 1 s after the fade is back up", + .input = {.rule = settle_rule(SettleKind::DOOR), .looks = concat(repeat(FADED, 3), {QUIET})}, + .expectation = {.result = SettleEnd::Result::QUIET, .presses = "BBBBBBBB"}, + }, + { + .name = "people walking about do not hold a door's settle up", + .input = {.rule = settle_rule(SettleKind::DOOR), .looks = {PEOPLE_WALKING}}, + .expectation = {.result = SettleEnd::Result::QUIET, .presses = "BBBBB"}, + }, + { + .name = "a wild battle is fled and the settle goes on", + .input = {.rule = settle_rule(SettleKind::DOOR), .looks = concat({QUIET, BATTLE}, {QUIET})}, + .expectation = {.result = SettleEnd::Result::QUIET, .presses = "BFBBBBB"}, + }, + { + .name = "a battle not fled ends it", + .input = {.rule = talk_before_battle(), .looks = concat(repeat(BOX, 2), {BATTLE}), .flee = false}, + .expectation = {.result = SettleEnd::Result::BATTLE, .presses = "NNF"}, + }, + { + .name = "no fade or banner by the cap is a map mismatch", + .input = {.rule = {.judge = {.warp = true}, .look_every = 1000ms, .cap = 3000ms}, .looks = {QUIET}}, + .expectation = { + .result = SettleEnd::Result::TIMED_OUT, .presses = "BBB", + .failure = "Step: no fade or banner within 3 s, so we are not where the step goes (map mismatch).", + }, + }, + { + .name = "no quiet overworld by the cap", + .input = {.rule = {.look_every = 1000ms, .cap = 3000ms}, .looks = {BOX}}, + .expectation = { + .result = SettleEnd::Result::TIMED_OUT, .presses = "NNN", + .failure = "Step: not back on a quiet overworld within 3 s.", + }, + }, + { + .name = "no battle by the cap after a trainer's talk", + .input = {.rule = {.judge = {.end_on_quiet = false}, .look_every = 1000ms, .cap = 3000ms}, .looks = {QUIET}}, + .expectation = { + .result = SettleEnd::Result::TIMED_OUT, .presses = "BBB", + .failure = "Step: no battle within 3 s of the talk.", + }, + }, +}; + + + +// A look of a game that only watches: its events, and how long they had +// shown unchanged when the look ended (none: not known). +struct WatchedLook{ + ScreenEvents events; + std::optional unchanged_for = std::nullopt; +}; + +// The screen a watching game sees: each look watches `look_every` and +// moves the clock on by it; the script's looks in order, then its last +// one for good. Presses do nothing. +class WatchingSettleIO : public SettleIO{ +public: + WatchingSettleIO(std::vector looks, Milliseconds look_every) + : m_looks(std::move(looks)) + , m_look_every(look_every) + {} + + virtual SettleLook look() override{ + const size_t index = m_count < m_looks.size() ? m_count : m_looks.size() - 1; + m_count++; + m_now += m_look_every; + const WatchedLook& seen = m_looks[index]; + std::optional since; + if (seen.unchanged_for.has_value()){ + since = m_now - *seen.unchanged_for; + } + return {.events = seen.events, .when = m_now, .since = since}; + } + virtual void press(SceneMove, Milliseconds) override{} + virtual bool flee_battle() override{ + m_flees++; + return true; + } + virtual void log(std::string_view) override{} + + size_t looks() const{ + return m_count; + } + size_t flees() const{ + return m_flees; + } + +private: + const std::vector m_looks; + const Milliseconds m_look_every; + size_t m_count = 0; + size_t m_flees = 0; + WallClock m_now = WallClock(); +}; + +// run_settle() for a game that only watches (FRLG). +struct WatchTestCase{ + struct Input{ + SettleRule rule; + std::vector looks; + }; + + struct Expectation{ + SettleEnd::Result result; + // The looks it took before it ended. + size_t looks; + // The flee_battle() calls. + size_t flees = 0; + }; + + std::string name; + Input input; + Expectation expectation; +}; + +class Test_Watch : public UnitTest{ +public: + explicit Test_Watch(WatchTestCase test_case) + : UnitTest(std::format("Pokemon::Settle - watch: {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + WatchingSettleIO io(m_case.input.looks, m_case.input.rule.look_every); + const SettleEnd end = run_settle(m_case.input.rule, "Step", io); + if (end.result != m_case.expectation.result){ + return std::format("expected {}, got {} ({})", result_name(m_case.expectation.result), result_name(end.result), end.text); + } + const UnitTestResult looks = expect_equal("looks", std::to_string(io.looks()), std::to_string(m_case.expectation.looks)); + if (looks.result != UnitTestResult::PASSED){ + return looks; + } + return expect_equal("flees", std::to_string(io.flees()), std::to_string(m_case.expectation.flees)); + } + +private: + const WatchTestCase m_case; +}; + +// A door's settle looked at every 400 ms, as FRLG watches. +SettleRule watched_door(bool watch_battles){ + SettleRule rule = settle_rule(SettleKind::DOOR); + rule.look_every = 400ms; + rule.watch_battles = watch_battles; + return rule; +} + +const WatchTestCase WATCH_CASES[] = { + { + // Quiet from 50 ms into the first look: 1 s of it by the third + // look's end (1200 ms), not the fourth (1600 ms, 1 s from the + // first look's end). + .name = "a quiet that began early in a look counts from then", + .input = {.rule = watched_door(false), .looks = {{QUIET, 350ms}, {QUIET, 750ms}, {QUIET, 1150ms}, {QUIET, 1550ms}}}, + .expectation = {.result = SettleEnd::Result::QUIET, .looks = 3}, + }, + { + .name = "with no since, the quiet counts from the look", + .input = {.rule = watched_door(false), .looks = {{QUIET}}}, + .expectation = {.result = SettleEnd::Result::QUIET, .looks = 4}, + }, + { + // 1 s of quiet after the battle: from the third look (1200 ms) + // to the sixth (2400 ms). + .name = "a battle on screen is watched, not fled", + .input = {.rule = watched_door(true), .looks = {{QUIET}, {BATTLE}, {QUIET}}}, + .expectation = {.result = SettleEnd::Result::QUIET, .looks = 6, .flees = 0}, + }, +}; + +} // namespace + + +void add_tests_Settle(UnitTestDatabase& database){ + for (const RuleTestCase& item : RULE_CASES){ + database.add(item); + } + for (const RunTestCase& item : RUN_CASES){ + database.add(item); + } + for (const WatchTestCase& item : WATCH_CASES){ + database.add(item); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle_Tests.h new file mode 100644 index 0000000000..2ab1b64a03 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle_Tests.h @@ -0,0 +1,23 @@ +/* Settle Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_Settle_Tests_H +#define PokemonAutomation_Pokemon_Settle_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for the settle rules and the settle loop. +void add_tests_Settle(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp index 0eac06d758..bfe4a86819 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp @@ -105,8 +105,8 @@ std::vector walk_rows(){ { C::POSE_LOST, "pose_lost", "where the player is or faces is unknown or not what the map says", - "the localizer: no pose, another map, another facing", - {}, {}, R::RELOAD, + "the localizer: no pose, another map, another facing; no Center after its door", + {}, {"heal_enter"}, R::RELOAD, }, }; } @@ -118,7 +118,7 @@ std::vector warp_rows(){ C::WARP_NO_OP, "warp_no_op", "a step onto a warp shows the fade's black but does not warp", "the map or tile after the fade is the one before it", - {FaultClass::WARP_NO_OP}, {}, R::RETRY_STEP, + {FaultClass::WARP_NO_OP}, {"heal_exit"}, R::RETRY_STEP, }, { C::LATE_FADE, "late_fade", diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.cpp index 07fbd66ff3..72319a15f3 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.cpp @@ -105,6 +105,8 @@ const OutcomeClassTestCase OUTCOME_CASES[] = { {.name = "checkpoint end", .input = {"end_failed"}, .expectation = {C::STATE_CHECK_FAILED}}, {.name = "postconditions", .input = {"postconditions_failed"}, .expectation = {C::STATE_CHECK_FAILED}}, {.name = "operation", .input = {"operation_failed"}, .expectation = {C::OPERATION_FAILED}}, + {.name = "no Center after its door", .input = {"heal_enter"}, .expectation = {C::POSE_LOST}}, + {.name = "still in the Center after its door", .input = {"heal_exit"}, .expectation = {C::WARP_NO_OP}}, {.name = "a game's own name", .input = {"no_banner"}, .expectation = {std::nullopt}}, {.name = "empty", .input = {""}, .expectation = {std::nullopt}}, }; diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 40b2bd9985..fec01bbfd8 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1631,6 +1631,10 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h Source/Pokemon/AutoStory/Pokemon_BuiltinStoryActions.cpp Source/Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h + Source/Pokemon/AutoStory/Pokemon_CenterVisit.cpp + Source/Pokemon/AutoStory/Pokemon_CenterVisit.h + Source/Pokemon/AutoStory/Pokemon_CenterVisit_Tests.cpp + Source/Pokemon/AutoStory/Pokemon_CenterVisit_Tests.h Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.cpp Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.cpp @@ -1648,6 +1652,10 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_SceneJudges.h Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.h + Source/Pokemon/AutoStory/Pokemon_Settle.cpp + Source/Pokemon/AutoStory/Pokemon_Settle.h + Source/Pokemon/AutoStory/Pokemon_Settle_Tests.cpp + Source/Pokemon/AutoStory/Pokemon_Settle_Tests.h Source/Pokemon/AutoStory/Pokemon_StoryActions.cpp Source/Pokemon/AutoStory/Pokemon_StoryActions.h Source/Pokemon/AutoStory/Pokemon_StoryActions_Tests.cpp