diff --git a/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp index afac33d987..c5e1c86862 100644 --- a/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp @@ -8,13 +8,15 @@ #include "Inference/PokemonBDSP_DialogDetector.h" #include "Inference/PokemonBDSP_MenuDetector.h" #include "Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions_Tests.h" +#include "Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.h" +#include "Programs/AutoStory/PokemonBDSP_AutoStory_RecoveryWalk_Tests.h" #include "Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.h" #include "Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.h" #include "Programs/AutoStory/PokemonBDSP_AutoStory_RoutePlanner_Tests.h" #include "Programs/AutoStory/PokemonBDSP_AutoStory_Segment00_Tests.h" #include "Programs/AutoStory/PokemonBDSP_AutoStory_StoryMaps_Tests.h" #include "Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.h" -#include "Programs/AutoStory/PokemonBDSP_AutoStory_StorySimulation_Tests.h" +#include "Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySimulation_Tests.h" #include "Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.h" #include "Programs/AutoStory/PokemonBDSP_AutoStory_StoryData_Tests.h" #include "Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.h" @@ -40,6 +42,8 @@ void add_tests(UnitTestDatabase& database){ add_tests_AutoStory_StoryData(database); add_tests_AutoStory_Walk(database); add_tests_AutoStory_PlannedRoute(database); + add_tests_AutoStory_GridSim(database); + add_tests_AutoStory_RecoveryWalk(database); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_CoordEvents.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_CoordEvents.cpp new file mode 100644 index 0000000000..c3fe3fea36 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_CoordEvents.cpp @@ -0,0 +1,109 @@ +/* BDSP AutoStory - Coord events + * + * 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 "Common/Cpp/Json/JsonValue.h" +#include "PokemonBDSP_AutoStory_Maps.h" +#include "PokemonBDSP_AutoStory_CoordEvents.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using Pokemon::TilePos; + + +namespace{ + +int read_int(const JsonObject& object, const std::string& key, const std::string& where){ + const JsonValue* value = object.get_value(key); + if (value == nullptr || !value->is_integer()){ + throw ParseException(where + key + " is missing or not an integer."); + } + return (int)value->to_integer_default(); +} + +std::string read_string(const JsonObject& object, const std::string& key, const std::string& where){ + const JsonValue* value = object.get_value(key); + const std::string* text = value == nullptr ? nullptr : value->to_string(); + if (text == nullptr){ + throw ParseException(where + key + " is missing or not a string."); + } + return *text; +} + +CoordEvent read_event(const JsonObject& object, const std::string& where){ + CoordEvent event{ + .name = read_string(object, "name", where), + .tiles = {}, + .var = read_string(object, "var", where), + .value = read_int(object, "value", where), + .source = read_string(object, "source", where), + }; + const int x = read_int(object, "x", where); + const int y = read_int(object, "y", where); + const int width = read_int(object, "width", where); + const int length = read_int(object, "length", where); + if (width < 1 || length < 1){ + throw ParseException(where + "width and length must be at least 1."); + } + for (int row = y; row < y + length; row++){ + for (int col = x; col < x + width; col++){ + event.tiles.push_back({col, row}); + } + } + return event; +} + +std::vector read_events(const std::string& name, const std::string& json){ + std::vector events; + const JsonValue root = parse_json(json); + const JsonObject* object = root.to_object(); + const JsonValue* list = object == nullptr ? nullptr : object->get_value("coord_events"); + if (list == nullptr){ + return events; + } + const JsonArray* array = list->to_array(); + if (array == nullptr){ + throw ParseException(name + ".json: coord_events is not an array."); + } + for (size_t c = 0; c < array->size(); c++){ + const std::string where = std::format("{}.json: coord_events[{}].", name, c); + const JsonObject* item = (*array)[c].to_object(); + if (item == nullptr){ + throw ParseException(where + " is not an object."); + } + events.push_back(read_event(*item, where)); + } + return events; +} + +} // namespace + + +const std::vector& story_coord_events(std::string_view map){ + static const std::map, std::less<>> by_map = []{ + std::map, std::less<>> out; + for (const auto& [name, json] : bdsp_map_json()){ + out.emplace(name, read_events(name, json)); + } + return out; + }(); + if (auto iter = by_map.find(map); iter != by_map.end()){ + return iter->second; + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("No BDSP story map named \"{}\".", map)); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_CoordEvents.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_CoordEvents.h new file mode 100644 index 0000000000..51f2d8514a --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_CoordEvents.h @@ -0,0 +1,51 @@ +/* BDSP AutoStory - Coord events + * + * From: https://github.com/PokemonAutomation/ + * + * The scene triggers of the story maps: stepping onto one of an event's + * tiles while its story variable holds `value` starts its script. They are + * the decomp's coord events (zone_event), read from a map's + * "coord_events", which the shared map schema leaves to the game: + * + * "coord_events": [{"name": "catch_tutorial", "x": 21, "y": 25, "width": 1, "length": 5, + * "var": "VAR_ROUTE_202_STATE", "value": 0, "source": "..."}, ...] + * + * A trigger seen on the console that the decomp does not have names the run + * in `source`. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_CoordEvents_H +#define PokemonAutomation_PokemonBDSP_AutoStory_CoordEvents_H + +#include +#include +#include +#include "Pokemon/Navigation/Pokemon_Grid.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Coordinate-triggered event parsed from BDSP map data. +struct CoordEvent{ + std::string name; + // The map's own tiles: `width` columns from x, `length` rows from y. + std::vector tiles; + std::string var; + int value = 0; + std::string source; +}; + +// The coord events of the map `map` (a JSON map's name), none if it lists +// none. Throws InternalProgramError for an unknown map, and ParseException +// for an event that does not parse. +const std::vector& story_coord_events(std::string_view map); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RecoveryWalk_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RecoveryWalk_Tests.cpp new file mode 100644 index 0000000000..6f288e1b38 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RecoveryWalk_Tests.cpp @@ -0,0 +1,168 @@ +/* BDSP Recovery Walk Tests + * + * From: https://github.com/PokemonAutomation/ + * + * November 5 mine report: EAST 14 moved 4 beside Machop on row 15. + * The exact deployed revision/run ID is unavailable. BEFORE/AFTER events + * and two interrupted advances are explicit diagnostics, not an inferred + * physical cause. Run the real story step and route walker on GridSim. + */ +#include +#include "Pokemon/AutoStory/Pokemon_TilePose.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" +#include "Simulation/PokemonBDSP_AutoStory_GridSim.h" +#include "PokemonBDSP_AutoStory_StoryPlaces.h" +#include "PokemonBDSP_AutoStory_StoryStepIO.h" +#include "PokemonBDSP_AutoStory_StoryTable.h" +#include "PokemonBDSP_AutoStory_StoryWalkSteps.h" +#include "PokemonBDSP_AutoStory_RecoveryWalk_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ +namespace{ +using namespace std::chrono_literals; +using Pokemon::Direction; +using Pokemon::TilePos; + +class InterruptingRouteIO : public GridSimRouteIO{ +public: + InterruptingRouteIO(GridSim& sim, std::vector& notes, + std::vector taps, size_t interrupted_holds) + : GridSimRouteIO(sim, notes, std::move(taps)), m_left(interrupted_holds) + {} + virtual WalkResult walk_to_wall(const RouteWallWalk& walk) override{ + if (m_left > 0){ + m_left--; + return {.stop = WalkStop::LIMIT, .walked = 400ms, .wild_battles = 1}; + } + return GridSimRouteIO::walk_to_wall(walk); + } +private: + size_t m_left; +}; + +// Only walking is needed for this real LEGS step. Unexpected story actions +// fail the test. The actual RouteWalker executes through the GridSim RouteIO. +class RecoveryStepIO : public StoryStepIO{ +public: + explicit RecoveryStepIO(RouteIO& route) : m_route(route){} + virtual RouteIO& route() override{ return m_route; } + virtual void fight(const std::string&) override{ throw GridSimFailure{"unexpected trainer"}; } + virtual void walk_until_stopped(Direction, size_t, const std::string&) override{ throw GridSimFailure{"unexpected blind hold"}; } + virtual bool run_until_map(Direction, Milliseconds, const std::string&, const std::string&) override{ throw GridSimFailure{"unexpected banner walk"}; } + virtual bool run_into_door(Direction, bool, Milliseconds = Milliseconds(12000)) override{ throw GridSimFailure{"unexpected door"}; } + virtual std::optional tap_to_banner(Direction, const std::string&, size_t) override{ throw GridSimFailure{"unexpected banner"}; } + virtual void settle(Button, const std::string&, bool) override{ throw GridSimFailure{"unexpected scene"}; } +private: + RouteIO& m_route; +}; + +const Pokemon::StoryStep& mine_step(){ + static const Pokemon::StoryStep step = []{ + Pokemon::StoryStep step; + step.kind = Pokemon::StoryStepKind::LEGS; + step.name = "below_roark"; + step.action = "open"; + step.args["legs"] = std::vector{ + {.direction = Direction::EAST, .tiles = 1}, + {.direction = Direction::SOUTH, .tiles = 2}, + {.direction = Direction::WEST, .tiles = 1}, + {.direction = Direction::SOUTH, .tiles = 5}, + {.direction = Direction::EAST, .tiles = 14}, + {.direction = Direction::SOUTH, .tiles = 10}, + {.direction = Direction::WEST, .tiles = 2}, + {.direction = Direction::SOUTH, .tiles = 3}, + {.direction = Direction::WEST, .tiles = 9}, + }; + return step; + }(); + return step; +} + +struct RecoveryInput{ + std::string name; + std::vector taps; + Direction direction = Direction::SOUTH; + size_t tiles = 5; + SimEncounterPhase phase = SimEncounterPhase::BEFORE_MOVEMENT; + WalkOrigin origin = WalkOrigin::CURRENT_LEG; + size_t interrupted_holds = 0; +}; +struct RecoveryExpectation{ + std::string failure = {}; + TilePos end = {.x = 18, .y = 29}; + Direction facing = Direction::WEST; +}; +struct RecoveryCase{ + RecoveryInput input; + RecoveryExpectation expectation; +}; + +class Test_RecoveryWalk : public UnitTest{ +public: + explicit Test_RecoveryWalk(RecoveryCase data) + : UnitTest("PokemonBDSP::AutoStory - Recovery walk: " + data.input.name), m_case(std::move(data)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const StoryWorld* world = story_plan_world("OreburghMine", "OreburghMine"); + if (world == nullptr){ + return "Missing native mine world."; + } + GridSim sim({.world = world}, world->to_world("OreburghMine", {15, 9}), Direction::SOUTH, true); + std::vector taps; + for (size_t tap : m_case.input.taps){ + taps.push_back({.label = "below_roark", .direction = m_case.input.direction, + .requested_tiles = m_case.input.tiles, .tap = tap, .phase = m_case.input.phase, .origin = m_case.input.origin}); + } + std::vector notes; + InterruptingRouteIO route(sim, notes, std::move(taps), m_case.input.interrupted_holds); + RecoveryStepIO io(route); + StoryWalkSteps walks(io); + walks.begin_attempt("01.03"); + Pokemon::RunState state{.pose = Pokemon::tile_pose("OreburghMine", {{15, 9}}, Direction::SOUTH), .flags = {}}; + std::string failure; + std::optional declared; + try{ + const auto outcome = walks.legs(state, mine_step()); + failure = outcome.failure; + declared = outcome.pose; + }catch (const GridSimFailure& error){ + failure = error.message; + } + if (!m_case.expectation.failure.empty()){ + return failure.find(m_case.expectation.failure) != std::string::npos ? UnitTestResult(true) + : UnitTestResult("Expected " + m_case.expectation.failure + ", got " + failure); + } + const auto end = story_to_local(*world, "OreburghMine", nearest_tile(sim.player().pos)); + if (!failure.empty() || !end || *end != m_case.expectation.end || !declared + || Pokemon::facing_of(*declared) != sim.player().facing || sim.player().facing != m_case.expectation.facing){ + return "Expected below Roark (18,29), got " + to_string(sim.player().pos) + ": " + failure; + } + return true; + } +private: + RecoveryCase m_case; +}; +} // namespace + +void add_tests_AutoStory_RecoveryWalk(UnitTestDatabase& database){ + const RecoveryCase CASES[] = { + {.input = {.name = "uninterrupted native mine step", .taps = {}}, .expectation = {.failure = ""}}, + {.input = {.name = "last tap interrupted before movement", .taps = {5}}, .expectation = {.failure = ""}}, + {.input = {.name = "last tap completed before battle", .taps = {5}, .phase = SimEncounterPhase::AFTER_MOVEMENT}, .expectation = {.failure = ""}}, + {.input = {.name = "two interrupted advances, row 15", .taps = {4, 5}}, .expectation = {.failure = ""}}, + {.input = {.name = "last west leg recovers position and facing", .taps = {9}, .direction = Direction::WEST, .tiles = 9}, .expectation = {.failure = ""}}, + {.input = {.name = "repeat the interrupted recovery hold", .taps = {5}, .interrupted_holds = 1}, .expectation = {.failure = ""}}, + {.input = {.name = "recovery hold budget exhausted", .taps = {5}, .interrupted_holds = 10}, .expectation = {.failure = "recovery budget"}}, + {.input = {.name = "first input may belong to previous leg", .taps = {1}}, .expectation = {.failure = "encounter_origin_unknown"}}, + {.input = {.name = "late encounter origin unknown", .taps = {5}, .origin = WalkOrigin::MAY_PRECEDE_LEG}, .expectation = {.failure = "encounter_origin_unknown"}}, + }; + for (const RecoveryCase& scenario : CASES){ + database.add(scenario); + } +} +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RecoveryWalk_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RecoveryWalk_Tests.h new file mode 100644 index 0000000000..9ee770013b --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RecoveryWalk_Tests.h @@ -0,0 +1,15 @@ +/* BDSP Recovery Walk Tests + * From: https://github.com/PokemonAutomation/ + */ +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_RecoveryWalk_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_RecoveryWalk_Tests_H +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ +// Register mine interruption, localization and bounded recovery scenarios. +void add_tests_AutoStory_RecoveryWalk(UnitTestDatabase& database); +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RoutePlanner_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RoutePlanner_Tests.cpp index 6c055847d0..8b2c777f1b 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RoutePlanner_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RoutePlanner_Tests.cpp @@ -20,6 +20,7 @@ #include "Data/PokemonBDSP_AutoStory_WalkRules.h" #include "Data/PokemonBDSP_AutoStory_WallRecovery.h" #include "Data/PokemonBDSP_AutoStory_Maps.h" +#include "Simulation/PokemonBDSP_AutoStory_GridSim.h" #include "PokemonBDSP_AutoStory_RoutePlanner_Tests.h" namespace PokemonAutomation{ @@ -140,6 +141,47 @@ UnitTestResult test_plan_robust_route(Logger&){ } +// November 5 mine report: credited taps need not establish physical pose. +// The two phases are explicit diagnostics, not inferred console timing. +struct EncounterInput{ + std::string name; + SimEncounterPhase phase; +}; +struct EncounterExpectation{ + TilePos end; + size_t battles = 1; + size_t credited = 5; +}; +struct EncounterCase{ + EncounterInput input; + EncounterExpectation expectation; +}; +class Test_InterruptedMineTaps : public UnitTest{ +public: + explicit Test_InterruptedMineTaps(EncounterCase scenario) + : UnitTest("PokemonBDSP::AutoStory - Encounter credits: " + scenario.input.name), m_case(std::move(scenario)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + StoryWorld mine; + mine.add("OreburghMine", story_map("OreburghMine"), {0, 0}); + GridSim sim({.world = &mine}, {15, 12}, Direction::SOUTH, /*can_run=*/false); + std::vector notes; + GridSimRouteIO io(sim, notes, {{.label = "south", .direction = Direction::SOUTH, + .requested_tiles = 5, .tap = 5, .phase = m_case.input.phase}}); + const auto result = io.walk_taps(Direction::SOUTH, 5, "south"); + const auto& expected = m_case.expectation; + const TilePos actual = nearest_tile(sim.player().pos); + if (result.wild_battles != expected.battles || result.moved_chunks != expected.credited || actual != expected.end){ + return std::format("Expected {} battles, {} credits, tile {}; got {}, {}, {}.", + expected.battles, expected.credited, Pokemon::to_string(expected.end), + result.wild_battles, result.moved_chunks, Pokemon::to_string(actual)); + } + return true; + } +private: + EncounterCase m_case; +}; + // Empty map name selects a synthetic corridor: ####### / #.....# / #######. // Both possible starts (1,1)/(5,1) must share a safe wall ray. NPCs and // warps on that ray or its wall invalidate it; ordinary terrain is unchanged. @@ -271,6 +313,27 @@ class Test_RoutePlanner : public UnitTest{ RoutePlannerCase m_case; }; +const EncounterCase ENCOUNTER_CASES[] = { + {.input = {.name = "before movement", .phase = SimEncounterPhase::BEFORE_MOVEMENT}, + .expectation = {.end = {.x = 15, .y = 16}}}, + {.input = {.name = "after movement", .phase = SimEncounterPhase::AFTER_MOVEMENT}, + .expectation = {.end = {.x = 15, .y = 17}}}, +}; + +const RecoveryGeometryCase GEOMETRY_CASES[] = { + {.input = {.name = "mine rows converge south", .map = "OreburghMine", .from = {{15, 15}, {15, 16}, {15, 17}}}, + .expectation = {.anchor = TilePos{.x = 15, .y = 17}}}, + {.input = {.name = "r55 tiles converge east", .map = "Route202", .from = {{27, 5}, {26, 5}}}, + .expectation = {.anchor = TilePos{.x = 30, .y = 5}, .direction = Direction::EAST}}, + {.input = {.name = "wandering area is unsafe", .from = {{1, 1}, {5, 1}}, + .wander = Pokemon::TileBox{.x0 = 2, .y0 = 1, .x1 = 4, .y1 = 1}}, + .expectation = {.anchor = std::nullopt, .free_tile = TilePos{.x = 3, .y = 1}}}, + {.input = {.name = "warp crossings are unsafe", .from = {{1, 1}, {5, 1}}, .warps = {{3, 1}}}, + .expectation = {.anchor = std::nullopt}}, + {.input = {.name = "warps on blocked glyphs are unsafe", .from = {{1, 1}, {5, 1}}, .warps = {{0, 1}, {6, 1}}}, + .expectation = {.anchor = std::nullopt}}, +}; + } // namespace @@ -293,24 +356,13 @@ const RoutePlannerCase ROUTE_PLANNER_CASES[] = { }, }; -const RecoveryGeometryCase GEOMETRY_CASES[] = { - {.input = {.name = "mine rows converge south", .map = "OreburghMine", .from = {{15, 15}, {15, 16}, {15, 17}}}, - .expectation = {.anchor = TilePos{.x = 15, .y = 17}}}, - {.input = {.name = "r55 tiles converge east", .map = "Route202", .from = {{27, 5}, {26, 5}}}, - .expectation = {.anchor = TilePos{.x = 30, .y = 5}, .direction = Direction::EAST}}, - {.input = {.name = "wandering area is unsafe", .from = {{1, 1}, {5, 1}}, - .wander = Pokemon::TileBox{.x0 = 2, .y0 = 1, .x1 = 4, .y1 = 1}}, - .expectation = {.anchor = std::nullopt, .free_tile = TilePos{.x = 3, .y = 1}}}, - {.input = {.name = "warp crossings are unsafe", .from = {{1, 1}, {5, 1}}, .warps = {{3, 1}}}, - .expectation = {.anchor = std::nullopt}}, - {.input = {.name = "warps on blocked glyphs are unsafe", .from = {{1, 1}, {5, 1}}, .warps = {{0, 1}, {6, 1}}}, - .expectation = {.anchor = std::nullopt}}, -}; - void add_tests_AutoStory_RoutePlanner(UnitTestDatabase& database){ for (const auto& item : ROUTE_PLANNER_CASES){ database.add(item); } + for (const auto& scenario : ENCOUNTER_CASES){ + database.add(scenario); + } for (const auto& scenario : GEOMETRY_CASES){ database.add(scenario); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.cpp index 5d45ed3912..919ab90698 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.cpp @@ -103,38 +103,24 @@ void save_failure_screenshot(VideoStream& stream, size_t attempt){ stream.log("Failure screenshot: " + path); } -// A trainer who saw us on a route: Route 204's Twins battle as a pair -// (01.10), everyone else alone. -void fight_story_trainer(VideoStream& stream, ProControllerContext& context, const std::string& trainer){ - if (trainer.find("Twins") != std::string::npos){ - talk_or_fight(stream, context, trainer, TalkExpect::BATTLE, BattleFormat::DOUBLES); - return; - } - fight_route_trainer(stream, context, trainer); } -} // namespace - StoryExecutor::StoryExecutor(StoryEnv& env) : m_env(env) + , m_io(env.env.console, env.context) + , m_walks(m_io) {} void StoryExecutor::begin_attempt(const Pokemon::StoryCheckpoint& data){ m_checkpoint = data.id; - // A row starts from its save: after the shoes' row, the save has them. - m_can_run = data.id > std::string(RUNNING_SHOES_ROW); - m_trainers.clear(); + m_walks.begin_attempt(data.id); } Outcome StoryExecutor::run(const StoryStep& step, RunState& state){ stream().log(std::format("[{}] {} {}: {}", m_checkpoint, Pokemon::story_step_kind_name(step.kind), step.name, step.why), COLOR_BLUE); try{ - Outcome outcome = run_guarded(state, step); - if (outcome.succeeded() && m_checkpoint == RUNNING_SHOES_ROW && step.name == RUNNING_SHOES_STEP){ - m_can_run = true; - } - return outcome; + return run_guarded(state, step); }catch (OperationFailedException& e){ if (m_stop_rows.contains(m_checkpoint)){ throw; @@ -185,45 +171,6 @@ Outcome StoryExecutor::run_guarded(RunState& state, const StoryStep& step){ } } -Pose StoryExecutor::walk_to(const Pose& from, const Pose& goal, const StoryStep& step){ - const StoryWorld* maps = story_plan_world(from.map, goal.map); - if (maps == nullptr){ - fail_step(stream(), std::format("{}: no maps join {} and {}.", step.name, from.map, goal.map)); - } - const std::optional costs = story_route_costs(step.action); - if (!costs){ - throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Unknown route costs: " + step.action); - } - std::vector start; - for (const TilePos& pos : Pokemon::tiles_of(from)){ - start.push_back(story_to_world(*maps, from.map, pos)); - } - std::vector targets; - for (const TilePos& pos : Pokemon::tiles_of(goal)){ - targets.push_back(story_to_world(*maps, goal.map, pos)); - } - // The ones fought stay off m_trainers until the next attempt. - std::vector& sights = m_trainers.try_emplace(maps, story_trainers(*maps)).first->second; - RouteTrainers trainers{ - sights, - [this](const std::string& trainer){ fight_story_trainer(stream(), context(), trainer); } - }; - const RouteWalkPlan plan{ - [&](const TilePos& pos){ return std::find(targets.begin(), targets.end(), pos) != targets.end(); }, - label_of(step), {}, *costs, m_can_run ? MoveSpeed::RUN : MoveSpeed::WALK - }; - const std::vector at = walk_route(stream(), context(), run_from_wild_battle, *maps, start, trainers, plan); - sights = std::move(trainers.sights); - std::vector local; - for (const TilePos& pos : at){ - const std::optional tile = story_to_local(*maps, goal.map, pos); - if (!tile){ - fail_step(stream(), std::format("{}: the walk ended off {}.", step.name, goal.map)); - } - local.push_back(*tile); - } - return Pokemon::tile_pose(goal.map, local, Pokemon::facing_of(goal)); -} Outcome StoryExecutor::new_game(RunState&, const StoryStep&){ play_new_game_intro(m_env.env, context(), m_env.player_name); @@ -237,293 +184,19 @@ Outcome StoryExecutor::options(RunState&, const StoryStep&){ } Outcome StoryExecutor::route(RunState& state, const StoryStep& step){ - if (Pokemon::tiles_of(state.pose).empty()){ - return Pokemon::outcome_failed("no_pose", Pokemon::to_string(state.pose)); - } - const GridPose& target = story_arg(step.args, "target"); - return Pokemon::outcome_ok(walk_to(state.pose, Pokemon::pose_of(target), step)); + return m_walks.route(state, step); } -namespace{ - -// A LEGS step "to_dialogue": taps its one leg until a dialogue starts on the -// way, wherever it starts (Dawn's at Sandgem's edge, r35/r36: an exact tile -// and banner raced her scene). The scene's lines are the next step's. -Outcome leg_to_dialogue(VideoStream& stream, ProControllerContext& context, const StoryStep& step, const std::string& checkpoint){ - const auto& legs_list = story_arg>(step.args, "legs"); - const Pokemon::StoryLeg& leg = legs_list.front(); - route_turn(stream, context, leg.direction); - const auto [x, y] = dpad_of(leg.direction); - const WalkResult walk = walk_tiles_dpad(stream, context, run_from_wild_battle, {x, y}, (size_t)leg.tiles, step.name); - stream.log(std::format("[{}] {}: taps {} to a dialogue: {} tiles, {}.", checkpoint, step.name, direction_name(leg.direction), walk.moved_chunks, walk_stop_name(walk.stop))); - if (walk.stop == WalkStop::DIALOG){ - return Pokemon::outcome_ok(); - } - // A scene can lock us before its first line: Dawn walks over first (r37). - ShortDialogWatcher dialog(COLOR_CYAN); - if (walk.stop == WalkStop::BLOCKED && wait_until(stream, context, 10000ms, {{dialog}}) == 0){ - return Pokemon::outcome_ok(); - } - return Pokemon::outcome_failed("no_dialogue", step.name); -} - -// A LEGS step "to_map": runs its one leg until the location banner of -// pose_after's map shows, wherever it shows. A straight run along an open -// row needs no tile count (r41, r42: Sandgem's row 11 took us into the lab). -Outcome leg_to_map(VideoStream& stream, ProControllerContext& context, const StoryStep& step, const std::string& checkpoint){ - const auto& legs_list = story_arg>(step.args, "legs"); - const Pokemon::StoryLeg& leg = legs_list.front(); - const std::string map = step.pose_after ? Pokemon::grid_pose_of(*step.pose_after).map : ""; - route_turn(stream, context, leg.direction); - const auto [x, y] = dpad_of(leg.direction); - const WalkResult walk = walk_until_dpad(stream, context, run_from_wild_battle, {x, y}, - Milliseconds(250) * leg.tiles + 2000ms, step.name, { - .arrived = [&](const ImageViewRGB32& screen){ return location_banner_matches(read_location_banner(screen), map); }, - .pace = DpadPace::RUN, - }); - stream.log(std::format("[{}] {}: ran {} to {}'s banner: {}.", checkpoint, step.name, direction_name(leg.direction), map, walk_stop_name(walk.stop))); - return walk.stop == WalkStop::ARRIVED ? Pokemon::outcome_ok() : Pokemon::outcome_failed("no_banner", map); -} - -} // namespace - Outcome StoryExecutor::legs(RunState& state, const StoryStep& step){ - const auto& legs_list = story_arg>(step.args, "legs"); - if (step.action == "to_wall"){ - for (const Pokemon::StoryLeg& leg : legs_list){ - walk_until_stopped(stream(), context(), leg.direction, (size_t)leg.tiles, label_of(step)); - } - return Pokemon::outcome_ok(); - } - if (step.action == "to_dialogue"){ - return leg_to_dialogue(stream(), context(), step, m_checkpoint); - } - if (step.action == "to_map"){ - return leg_to_map(stream(), context(), step, m_checkpoint); - } - const bool one_short_ok = step.action == "one_short_ok"; - if (!one_short_ok && !step.action.empty() && step.action != "legs"){ - return Pokemon::outcome_failed("unknown_action", step.action); - } - std::vector at = Pokemon::tiles_of(state.pose); - for (const Pokemon::StoryLeg& leg : legs_list){ - // Facing another way, a tap only turns (r256): turn first. - route_turn(stream(), context(), leg.direction); - const auto [x, y] = dpad_of(leg.direction); - const size_t tiles = (size_t)leg.tiles; - const WalkResult walk = walk_tiles_dpad(stream(), context(), run_from_wild_battle, {x, y}, tiles, step.name); - stream().log(std::format("[{}] {}: taps {} moved {} of {} tiles.", m_checkpoint, step.name, direction_name(leg.direction), walk.moved_chunks, tiles)); - if (walk.moved_chunks + (one_short_ok ? 1 : 0) < tiles){ - return Pokemon::outcome_failed("short_leg", std::format("{} {} moved {}", direction_name(leg.direction), tiles, walk.moved_chunks)); - } - // Where we may be: the end, or a tile short of it. - std::vector next; - for (const TilePos& start : at){ - TilePos end = start; - for (size_t i = 0; i < tiles; i++){ - if (one_short_ok && i + 1 == tiles){ - next.push_back(end); - } - end = end.next(leg.direction); - } - next.push_back(end); - } - at = std::move(next); - } - return Pokemon::outcome_ok(Pokemon::tile_pose(state.pose.map, at, legs_list.back().direction)); -} - -namespace{ - -// One rule for scenes and map changes: v2's long B mashes, with an end -// check. Press through the text, and keep pressing B while the screen is -// quiet: a scene can pause while someone walks off (Dawn and Barry after -// Rowan leaves, r23), fade dimly, not to black, to another map (r25), or -// wait on a box the detector misses (r26). B never starts a talk on the -// overworld. Done once the overworld has shown, with no text and a still -// screen, for 5 s of presses: after r31's fade home a scripted walk-in ran -// 5 s with no text before Mom spoke. -// -// A step or scene that ends on another map (`fade`) must fade on the way, -// and only the quiet after the fade counts: r30's scene went quiet for 5 s -// before the fade home, and r28 "went out" a tile beside the door and -// walked a whole route in the house. No fade is a map mismatch. -constexpr double FADE_DARKNESS = 40; // brightness 0 to 255; r24's dim fade home goes darker -void settle(VideoStream& stream, ProControllerContext& context, Button button, const std::string& label, bool fade){ - const WallClock none = WallClock::max(); - const WallClock deadline = current_time() + 120s; - ShortDialogDetector dialog; - OverworldDetector overworld; - WallClock quiet_since = none; - WallClock calm_since = none; // overworld, no text, still or not - double darkest = 255; - VideoSnapshot previous; - while (current_time() < deadline){ - context.wait_for_all_requests(); - const auto frame = stream.video().snapshot(); - const WallClock now = current_time(); - if (frame){ - darkest = std::min(darkest, image_average(*frame.frame).sum() / 3); - } - const bool talking = frame && dialog.detect(frame); - const bool still = frame && previous && walk_screen_change(*previous.frame, *frame.frame) < WALK_TAP_BLOCKED_CHANGE; - previous = frame; - // Cloud shadows drift over Floaroma, so its screen is never still: r83 - // got Mew and then waited out the 120 s on a calm overworld. 15 s of - // overworld with no text is three times r31's silent walk-in. - const bool calm = !talking && overworld.detect(frame) && (!fade || darkest < FADE_DARKNESS); - calm_since = calm ? (calm_since == none ? now : calm_since) : none; - if (calm && now - calm_since >= 15000ms){ - stream.log(std::format("[Story] {}: settled on 15 s of overworld without text (the screen kept moving); darkest frame {:.0f}.", label, darkest)); - return; - } - if (!talking && still && overworld.detect(frame) && (!fade || darkest < FADE_DARKNESS)){ - quiet_since = quiet_since == none ? now : quiet_since; - if (now - quiet_since >= 5000ms){ - stream.log(std::format("[Story] {}: settled; darkest frame {:.0f}.", label, darkest)); - return; - } - }else{ - quiet_since = none; - } - pbf_press_button(context, talking ? button : BUTTON_B, 80ms, 1200ms); - } - OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, - fade && darkest >= FADE_DARKNESS - ? label + ": no fade within 120 s, so we are not where the step goes (map mismatch)." - : label + ": not back on a quiet overworld within 120 s.", - stream); -} - -} // namespace - -namespace{ - -// Holds the D-pad toward a door (with B after the shoes) until the screen goes -// dark or a line shows, up to 12 s. Returns whether it went dark. A line -// stops it: holding on under Mom's "Oh, Kosmos!" wasted 12 s (r45). The walker's black-screen watch -// missed the lab's fade into Sandgem (r44, "dialog"), and settle's samples -// 1.3 s apart can fall either side of it, so watch at settle's own darkness. -DpadPosition dpad_toward(Direction direction){ - return - direction == Direction::NORTH ? DPAD_UP : - direction == Direction::SOUTH ? DPAD_DOWN : - direction == Direction::WEST ? DPAD_LEFT : DPAD_RIGHT; -} - -bool run_into_door(VideoStream& stream, ProControllerContext& context, Direction direction, bool run){ - const DpadPosition dpad = dpad_toward(direction); - BlackScreenWatcher dark(COLOR_RED, {0.1, 0.1, 0.8, 0.8}, 3 * FADE_DARKNESS, 60); - ShortDialogWatcher dialog(COLOR_YELLOW); - const int ret = run_until(stream, context, [&](ProControllerContext& context){ - if (run){ - ssf_press_button(context, BUTTON_B, 0ms, 12000ms, 0ms); - } - pbf_press_dpad(context, dpad, 12000ms, 0ms); - }, {{dark}, {dialog}}); - return ret == 0; -} - -// What the screen showed after a tap toward a door or a cave mouth. -enum class TapLook{ - NOTHING, - DARK, - BANNER, -}; - -// Looks for `map`'s location banner (and, if `dark_too`, the fade) for up -// to `wait`. -TapLook look_after_tap(VideoStream& stream, ProControllerContext& context, const std::string& map, Milliseconds wait, bool dark_too){ - const WallClock deadline = current_time() + wait; - while (true){ - context.wait_for_all_requests(); - const VideoSnapshot screen = stream.video().snapshot(); - if (screen && location_banner_matches(read_location_banner(*screen.frame), map)){ - return TapLook::BANNER; - } - if (dark_too && screen && image_average(*screen.frame).sum() / 3 < FADE_DARKNESS){ - return TapLook::DARK; - } - if (current_time() >= deadline){ - return TapLook::NOTHING; - } - context.wait_for(100ms); - } + return m_walks.legs(state, step); } -// A STEP "to_banner": D-pad taps one at a time toward a door or cave mouth, -// each followed by a look, until the fade (then no more taps) and -// pose_after's map's location banner. A held walk can carry on past a -// fade that goes unseen: out of the Gym it walked on south (r219g), and -// through the Oreburgh Gate's west mouth on west into a pocket by Route -// 203's pond (r75). Taps during the fade are eaten. No banner fails the -// step right there. -Outcome tap_to_banner(VideoStream& stream, ProControllerContext& context, const StoryStep& step, const std::string& checkpoint){ - const std::string map = step.pose_after ? story_banner_name(Pokemon::grid_pose_of(*step.pose_after).map) : ""; - const Direction direction = story_arg(step.args, "direction"); - for (size_t tap = 1; tap <= 30; tap++){ - pbf_press_dpad(context, dpad_toward(direction), 160ms, 0ms); - TapLook look = look_after_tap(stream, context, map, 1500ms, true); - const bool faded = look == TapLook::DARK; - if (faded){ - look = look_after_tap(stream, context, map, 8000ms, false); - } - if (look == TapLook::BANNER){ - stream.log(std::format("[{}] {}: {}'s banner after {} taps {}.", checkpoint, step.name, map, tap, direction_name(direction))); - context.wait_for(1000ms); - return Pokemon::outcome_ok(); - } - if (faded){ - break; - } - } - return Pokemon::outcome_failed("no_banner", map); -} - -} // namespace - Outcome StoryExecutor::step(RunState& state, const StoryStep& step){ - const Direction direction = story_arg(step.args, "direction"); - route_turn(stream(), context(), direction); - if (step.action == "to_banner"){ - return tap_to_banner(stream(), context(), step, m_checkpoint); - } - // A step through a door: walk (or run) on until the screen goes dark, - // from next to the door or from anywhere in its line (r43: tapping 8 tiles - // down the lab took 7 s), then settle for any scene on arrival (Mom's, - // 00.01; Dawn's tour, 00.04). Without the fade, settle must see it itself - // (r33 was outside, "no fade"). - if (step.pose_after && Pokemon::grid_pose_of(*step.pose_after).map != state.pose.map){ - const bool faded = run_into_door(stream(), context(), direction, m_can_run); - stream().log(std::format("[{}] {}: {} through the door: {}.", m_checkpoint, step.name, direction_name(direction), faded ? "faded" : "no fade yet")); - settle(stream(), context(), BUTTON_B, label_of(step), !faded); - return Pokemon::outcome_ok(); - } - const auto [x, y] = dpad_of(direction); - const WalkResult walk = walk_tiles_dpad(stream(), context(), run_from_wild_battle, {x, y}, 1, step.name); - stream().log(std::format("[{}] {}: a step {}, {}.", m_checkpoint, step.name, direction_name(direction), walk_stop_name(walk.stop))); - std::vector at; - for (const TilePos& pos : Pokemon::tiles_of(state.pose)){ - at.push_back(pos.next(direction)); - } - return Pokemon::outcome_ok(Pokemon::tile_pose(state.pose.map, at, direction)); + return m_walks.step(state, step); } Outcome StoryExecutor::talk(RunState& state, const StoryStep& step){ - // Face whom we talk to: a route ends facing its last leg, not its goal - // (r40 reached Mom facing north). They block the tile, so the tap only turns. - if (const std::optional facing = Pokemon::facing_of(state.pose)){ - route_turn(stream(), context(), *facing); - } - pbf_press_button(context(), BUTTON_A, 80ms, 200ms); - context().wait_for_all_requests(); - // Roark's first line in the mine comes after the camera zooms in on - // us, which can take some seconds (r98; the old event waited 8 s). - ShortDialogWatcher dialog(COLOR_YELLOW); - if (wait_until(stream(), context(), 8000ms, {{dialog}}) != 0){ - fail_step(stream(), label_of(step) + ": nobody answered."); - } - return Pokemon::outcome_ok(); + return m_walks.talk(state, step); } Outcome StoryExecutor::answer(RunState&, const StoryStep& step){ @@ -535,15 +208,7 @@ Outcome StoryExecutor::answer(RunState&, const StoryStep& step){ } Outcome StoryExecutor::mash(RunState& state, const StoryStep& step){ - // The step before may have played the scene already (a door step settles - // its arrival scene), so its first line is not required. - ShortDialogWatcher dialog(COLOR_CYAN); - if (wait_until(stream(), context(), 15000ms, {{dialog}}) != 0){ - stream().log(label_of(step) + ": no line within 15 s; the step before played it.", COLOR_ORANGE); - } - settle(stream(), context(), step.action == "a" ? BUTTON_A : BUTTON_B, label_of(step), - step.pose_after && Pokemon::grid_pose_of(*step.pose_after).map != state.pose.map); - return Pokemon::outcome_ok(); + return m_walks.mash(state, step); } Outcome StoryExecutor::battle(RunState&, const StoryStep& step){ @@ -588,7 +253,7 @@ Outcome StoryExecutor::heal(RunState&, const StoryStep& step){ // way can leave the route unsure of the column (r59, Jubilife). Below // Sandgem's door the lineup read 0.7 tile off and stepped us off it (r48). route_turn(stream(), context(), Direction::NORTH); - const bool in = run_into_door(stream(), context(), Direction::NORTH, m_can_run); + const bool in = m_io.run_into_door(Direction::NORTH, m_walks.can_run()); stream().log(std::format("[{}] {}: north into the Center: {}.", m_checkpoint, step.name, in ? "faded" : "no door; lining up by sight")); if (in){ pbf_wait(context(), 1500ms); @@ -610,9 +275,9 @@ Outcome StoryExecutor::heal(RunState&, const StoryStep& step){ // Out the same way: south until the fade. The walk out's indoor check // read "not a room" a step from the counter and stopped inside (r50, r59). route_turn(stream(), context(), Direction::SOUTH); - const bool out = run_into_door(stream(), context(), Direction::SOUTH, m_can_run); + const bool out = m_io.run_into_door(Direction::SOUTH, m_walks.can_run()); stream().log(std::format("[{}] {}: south out of the Center: {}.", m_checkpoint, step.name, out ? "faded" : "no fade yet")); - settle(stream(), context(), BUTTON_B, label_of(step), !out); + m_io.settle(BUTTON_B, label_of(step), !out); // r143a's heal said it had walked out while still inside. context().wait_for_all_requests(); VideoSnapshot screen = stream().video().snapshot(); diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h index bc8e9b535e..46a5fd7f91 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h @@ -38,7 +38,8 @@ #include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" #include "Pokemon/AutoStory/Types/Pokemon_StoryStep.h" #include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RoutePlanner.h" -#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_TrainerSight.h" +#include "PokemonBDSP_AutoStory_StoryStepIO.h" +#include "PokemonBDSP_AutoStory_StoryWalkSteps.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -116,11 +117,6 @@ class StoryExecutor : public Pokemon::StoryStepExecutor{ const std::string& checkpoint() const{ return m_checkpoint; } - // Walks from the tiles of `from` on its map to any of `goal`'s tiles, - // fighting the trainers who see us this attempt. Returns where it left - // us, as a pose on `goal`'s map. - Pokemon::Pose walk_to(const Pokemon::Pose& from, const Pokemon::Pose& goal, const Pokemon::StoryStep& step); - // The intro of a new game, from the title screen to the bedroom. Pokemon::Outcome new_game(Pokemon::RunState& state, const Pokemon::StoryStep& step); // verify_tutorial_settings(): fast text, Autosave off. @@ -161,13 +157,10 @@ class StoryExecutor : public Pokemon::StoryStepExecutor{ Pokemon::Outcome run_guarded(Pokemon::RunState& state, const Pokemon::StoryStep& step); StoryEnv& m_env; + ConsoleStoryStepIO m_io; + // The steps that walk and talk (ROUTE, LEGS, STEP, TALK, MASH). + StoryWalkSteps m_walks; std::string m_checkpoint; - // Whether the player has the Running Shoes: the pace routes are timed at. - bool m_can_run = false; - // The trainers who can still see us on each story_plan_world() world, - // in its tiles, from its first walk this attempt; all of them again at - // each attempt. - std::map> m_trainers; std::map m_failures; std::map m_run_counts; std::set m_stop_rows; diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.cpp new file mode 100644 index 0000000000..f172d91bef --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.cpp @@ -0,0 +1,229 @@ +/* BDSP AutoStory - Story step IO + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/VisualDetectors/BlackScreenDetector.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h" +#include "PokemonBDSP/Inference/PokemonBDSP_TransitionWatchers.h" +#include "PokemonBDSP/Inference/Battles/PokemonBDSP_BattleMenuDetector.h" +#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_OverworldDetector.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "Actions/PokemonBDSP_AutoStory_Talks.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkDpad.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkScreen.h" +#include "PokemonBDSP_AutoStory_Walks.h" +#include "Actions/PokemonBDSP_AutoStory_WildBattle.h" +#include "PokemonBDSP_AutoStory_StoryPlaces.h" +#include "PokemonBDSP_AutoStory_StoryStepIO.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; +using Pokemon::Direction; + + +namespace{ + +// One rule for scenes and map changes: v2's long B mashes, with an end +// check. Press through the text, and keep pressing B while the screen is +// quiet: a scene can pause while someone walks off (Dawn and Barry after +// Rowan leaves, r23), fade dimly, not to black, to another map (r25), or +// wait on a box the detector misses (r26). B never starts a talk on the +// overworld. Done once the overworld has shown, with no text and a still +// screen, for 5 s of presses: after r31's fade home a scripted walk-in ran +// 5 s with no text before Mom spoke. +// +// A step or scene that ends on another map (`fade`) must fade on the way, +// and only the quiet after the fade counts: r30's scene went quiet for 5 s +// before the fade home, and r28 "went out" a tile beside the door and +// walked a whole route in the house. No fade is a map mismatch. +constexpr double FADE_DARKNESS = 75; // brightness 0 to 255; cave fades reach ~45..64, r24's dim fade goes darker +void settle(VideoStream& stream, ProControllerContext& context, Button button, const std::string& label, bool fade){ + const WallClock none = WallClock::max(); + const WallClock deadline = current_time() + 120s; + ShortDialogDetector dialog; + OverworldDetector overworld; + BattleMenuDetector battle_menu(BattleType::STANDARD); + WallClock quiet_since = none, calm_since = none; + double darkest = 255; + VideoSnapshot previous; + while (current_time() < deadline){ + context.wait_for_all_requests(); + const auto frame = stream.video().snapshot(); + const WallClock now = current_time(); + if (frame && battle_menu.detect(frame)){ + stream.log("[Story] " + label + ": in wild battle during settle; fleeing."); + run_from_wild_battle(stream, context); + continue; + } + if (frame){ + darkest = std::min(darkest, image_average(*frame.frame).sum() / 3); + } + const bool talking = frame && dialog.detect(frame); + const bool still = frame && previous && walk_screen_change(*previous.frame, *frame.frame) < WALK_TAP_BLOCKED_CHANGE; + previous = frame; + const bool calm = !talking && overworld.detect(frame) && (!fade || darkest < FADE_DARKNESS); + calm_since = calm ? (calm_since == none ? now : calm_since) : none; + if (calm && now - calm_since >= 15000ms){ + stream.log(std::format("[Story] {}: settled on calm overworld; darkest frame {:.0f}.", label, darkest)); + return; + } + if (!talking && still && overworld.detect(frame) && (!fade || darkest < FADE_DARKNESS)){ + quiet_since = quiet_since == none ? now : quiet_since; + if (now - quiet_since >= 5000ms){ + stream.log(std::format("[Story] {}: settled; darkest frame {:.0f}.", label, darkest)); + return; + } + }else{ + quiet_since = none; + } + pbf_press_button(context, talking ? button : BUTTON_B, 80ms, 1200ms); + } + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, + fade && darkest >= FADE_DARKNESS + ? label + ": no fade within 120 s, so we are not where the step goes (map mismatch)." + : label + ": not back on a quiet overworld within 120 s.", + stream); +} + +// Holds the D-pad toward a door (with B after the shoes) until the screen goes +// dark or a line shows, up to 12 s. Returns whether it went dark. A line +// stops it: holding on under Mom's "Oh, Kosmos!" wasted 12 s (r45). A wild +// battle in a cave corridor runs away before returning. +DpadPosition dpad_toward(Direction direction){ + return + direction == Direction::NORTH ? DPAD_UP : + direction == Direction::SOUTH ? DPAD_DOWN : + direction == Direction::WEST ? DPAD_LEFT : DPAD_RIGHT; +} + +bool run_into_door(VideoStream& stream, ProControllerContext& context, Direction direction, bool run, Milliseconds timeout){ + const DpadPosition dpad = dpad_toward(direction); + BlackScreenWatcher dark(COLOR_RED, {0.1, 0.1, 0.8, 0.8}, 3 * FADE_DARKNESS, 60); + WhiteScreenWatcher white(COLOR_BLUE, {0.1, 0.1, 0.8, 0.8}, WhiteScreenWatcher::FinderType::PRESENT, 60ms); + ShortDialogWatcher dialog(COLOR_YELLOW); + BattleMenuWatcher battle(BattleType::STANDARD, COLOR_YELLOW); + const int ret = run_until(stream, context, [&](ProControllerContext& context){ + if (run){ + ssf_press_button(context, BUTTON_B, 0ms, timeout, 0ms); + } + pbf_press_dpad(context, dpad, timeout, 0ms); + }, {{dark}, {white}, {dialog}, {battle}}); + if (ret == 3){ + stream.log("[Story] run_into_door: wild battle; fleeing."); + run_from_wild_battle(stream, context); + return false; + } + return ret == 0 || ret == 1; +} + +// What the screen showed after a tap toward a door or a cave mouth. +enum class TapLook{ + NOTHING, + DARK, + BANNER, +}; + +// Looks for the location banner `banner` (and, if `dark_too`, the fade) +// for up to `wait`. +TapLook look_after_tap(VideoStream& stream, ProControllerContext& context, const std::string& banner, Milliseconds wait, bool dark_too){ + const WallClock deadline = current_time() + wait; + while (true){ + context.wait_for_all_requests(); + const VideoSnapshot screen = stream.video().snapshot(); + if (screen && location_banner_matches(read_location_banner(*screen.frame), banner)){ + return TapLook::BANNER; + } + if (dark_too && screen && image_average(*screen.frame).sum() / 3 < FADE_DARKNESS){ + return TapLook::DARK; + } + if (current_time() >= deadline){ + return TapLook::NOTHING; + } + context.wait_for(100ms); + } +} + +} // namespace + + +ConsoleStoryStepIO::ConsoleStoryStepIO(VideoStream& stream, ProControllerContext& context) + : m_stream(stream) + , m_context(context) + , m_escape(run_from_wild_battle) + , m_route(stream, context, m_escape) +{} + +void ConsoleStoryStepIO::fight(const std::string& trainer){ + if (trainer.find("Twins") != std::string::npos){ + talk_or_fight(m_stream, m_context, trainer, TalkExpect::BATTLE, BattleFormat::DOUBLES); + return; + } + fight_route_trainer(m_stream, m_context, trainer); +} + +void ConsoleStoryStepIO::walk_until_stopped(Direction direction, size_t tiles, const std::string& label){ + PokemonBDSP::walk_until_stopped(m_stream, m_context, direction, tiles, label); +} + +bool ConsoleStoryStepIO::run_until_map(Direction direction, Milliseconds limit, const std::string& map, const std::string& label){ + const auto [x, y] = dpad_of(direction); + const WalkResult walk = walk_until_dpad(m_stream, m_context, run_from_wild_battle, {x, y}, limit, label, { + .arrived = [&](const ImageViewRGB32& screen){ return location_banner_matches(read_location_banner(screen), map); }, + .pace = DpadPace::RUN, + }); + m_stream.log(std::format("[Story] {}: ran {} to {}'s banner: {}.", label, direction_name(direction), map, walk_stop_name(walk.stop))); + return walk.stop == WalkStop::ARRIVED; +} + +bool ConsoleStoryStepIO::run_into_door(Direction direction, bool run, Milliseconds timeout){ + return PokemonBDSP::run_into_door(m_stream, m_context, direction, run, timeout); +} + +// A held walk can carry on past a fade that goes unseen: out of the Gym it +// walked on south (r219g), and through the Oreburgh Gate's west mouth on +// west into a pocket by Route 203's pond (r75). Taps during the fade are +// eaten. +std::optional ConsoleStoryStepIO::tap_to_banner(Direction direction, const std::string& map, size_t max_taps){ + const std::string& banner = story_banner_name(map); + for (size_t tap = 1; tap <= max_taps; tap++){ + pbf_press_dpad(m_context, dpad_toward(direction), 160ms, 0ms); + TapLook look = look_after_tap(m_stream, m_context, banner, 1500ms, true); + const bool faded = look == TapLook::DARK; + if (faded){ + look = look_after_tap(m_stream, m_context, banner, 8000ms, false); + } + if (look == TapLook::BANNER){ + m_context.wait_for(1000ms); + return tap; + } + if (faded){ + break; + } + } + return std::nullopt; +} + +void ConsoleStoryStepIO::settle(Button button, const std::string& label, bool fade){ + PokemonBDSP::settle(m_stream, m_context, button, label, fade); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h new file mode 100644 index 0000000000..dd0f6b8a58 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h @@ -0,0 +1,88 @@ +/* BDSP AutoStory - Story step IO + * + * From: https://github.com/PokemonAutomation/ + * + * What the story's walking steps (StoryWalkSteps) do on the console beyond + * the route walker's RouteIO: fights, walks until stopped, runs into doors + * and the settle after a scene. Tests run the steps on a simulation; + * ConsoleStoryStepIO is the console. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StoryStepIO_H +#define PokemonAutomation_PokemonBDSP_AutoStory_StoryStepIO_H + +#include +#include +#include "Common/Cpp/Time.h" +#include "CommonFramework/Tools/VideoStream.h" +#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "Pokemon/Navigation/Pokemon_Grid.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteIO.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Abstract interface for story step input/output operations. +class StoryStepIO{ +public: + virtual ~StoryStepIO() = default; + + // The route walker's console: walks, presses, dialogue watches, the log. + virtual RouteIO& route() = 0; + // Fights a trainer whose dialogue has opened (fight_route_trainer()): + // Route 204's Twins as a pair (01.10), everyone else alone. + virtual void fight(const std::string& trainer) = 0; + // walk_until_stopped(): `direction` until something stops us, for at + // most `tiles` tiles' time. Fails on a door or a dialogue. + virtual void walk_until_stopped(Pokemon::Direction direction, size_t tiles, const std::string& label) = 0; + // Runs `direction` on the D-pad until the location banner of `map` (a + // JSON map's name) shows, for at most `limit`. Returns whether it showed. + virtual bool run_until_map(Pokemon::Direction direction, Milliseconds limit, const std::string& map, const std::string& label) = 0; + // Holds the D-pad toward a door (running with `run`) until the screen + // goes dark or a line shows. Returns whether it went dark. + virtual bool run_into_door(Pokemon::Direction direction, bool run, Milliseconds timeout = Milliseconds(12000)) = 0; + // D-pad taps `direction` one at a time, each looked at, toward a door + // or cave mouth, until the fade (then no more taps) and the location + // banner of `map` (a JSON map's name), for at most `max_taps`. Returns + // the taps it took, or nothing if no banner showed. + virtual std::optional tap_to_banner(Pokemon::Direction direction, const std::string& map, size_t max_taps) = 0; + // Presses through any text until the overworld is quiet; with `fade`, a + // fade must come first (a map change). Fails otherwise. + virtual void settle(Button button, const std::string& label, bool fade) = 0; +}; + + +// The console. +class ConsoleStoryStepIO : public StoryStepIO{ +public: + ConsoleStoryStepIO(VideoStream& stream, ProControllerContext& context); + + virtual RouteIO& route() override{ + return m_route; + } + virtual void fight(const std::string& trainer) override; + virtual void walk_until_stopped(Pokemon::Direction direction, size_t tiles, const std::string& label) override; + virtual bool run_until_map(Pokemon::Direction direction, Milliseconds limit, const std::string& map, const std::string& label) override; + virtual bool run_into_door(Pokemon::Direction direction, bool run, Milliseconds timeout = Milliseconds(12000)) override; + virtual std::optional tap_to_banner(Pokemon::Direction direction, const std::string& map, size_t max_taps) override; + virtual void settle(Button button, const std::string& label, bool fade) override; + +private: + VideoStream& m_stream; + ProControllerContext& m_context; + WildBattleEscape m_escape; + ConsoleRouteIO m_route; +}; + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.cpp new file mode 100644 index 0000000000..ecf0c0fac6 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.cpp @@ -0,0 +1,361 @@ +/* BDSP AutoStory - Story walk steps + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/Color.h" +#include "Common/Cpp/Exceptions.h" +#include "Pokemon/AutoStory/Pokemon_TilePose.h" +#include "Pokemon/Navigation/Pokemon_MapData.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RobustRoute.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_TrainerSight.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WallRecovery.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" +#include "PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP_AutoStory_StoryPlaces.h" +#include "PokemonBDSP_AutoStory_StoryTable.h" +#include "PokemonBDSP_AutoStory_StoryWalkSteps.h" +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; +using Pokemon::Direction; +using Pokemon::GridPose; +using Pokemon::story_arg; +using Pokemon::Outcome; +using Pokemon::Pose; +using Pokemon::RunState; +using Pokemon::StoryStep; +using Pokemon::TilePos; + + +namespace{ + +// "to_catch_grass" as "to catch grass", for the logs. +std::string label_of(const StoryStep& step){ + std::string label = step.name; + std::replace(label.begin(), label.end(), '_', ' '); + return label; +} + +// Fails the step (fail_step() on the console). +[[noreturn]] void fail(RouteIO& io, const std::string& message){ + io.note(message, RouteNote::FAILURE); + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "RouteIO::note() returned from a failure: " + message); +} + +} // namespace + + +void StoryWalkSteps::begin_attempt(const std::string& row){ + m_row = row; + // A row starts from its save: after the shoes' row, the save has them. + m_can_run = row > std::string(RUNNING_SHOES_ROW); + m_trainers.clear(); +} + +void StoryWalkSteps::turn(Direction direction){ + m_io.route().note(std::string("[Route] Turning ") + direction_name(direction) + ".", RouteNote::LOG); + m_io.route().press(route_turn_tap(direction)); +} + +void StoryWalkSteps::log(const std::string& text){ + m_io.route().note(text, RouteNote::LOG); +} + +Pose StoryWalkSteps::walk_to(const Pose& from, const Pose& goal, const StoryStep& step){ + const StoryWorld* maps = story_plan_world(from.map, goal.map); + if (maps == nullptr){ + fail(m_io.route(), std::format("{}: no maps join {} and {}.", step.name, from.map, goal.map)); + } + const std::optional costs = story_route_costs(step.action); + if (!costs){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Unknown route costs: " + step.action); + } + std::vector start; + for (const TilePos& pos : Pokemon::tiles_of(from)){ + start.push_back(story_to_world(*maps, from.map, pos)); + } + std::vector targets; + for (const TilePos& pos : Pokemon::tiles_of(goal)){ + targets.push_back(story_to_world(*maps, goal.map, pos)); + } + // The ones fought stay off m_trainers until the next attempt. + std::vector& sights = m_trainers.try_emplace(maps, story_trainers(*maps)).first->second; + RouteTrainers trainers{ + sights, + [this](const std::string& trainer){ m_io.fight(trainer); } + }; + const RouteWalkPlan plan{ + [&](const TilePos& pos){ return std::find(targets.begin(), targets.end(), pos) != targets.end(); }, + label_of(step), {}, *costs, m_can_run ? MoveSpeed::RUN : MoveSpeed::WALK + }; + const std::vector at = walk_route(m_io.route(), *maps, start, trainers, plan); + sights = std::move(trainers.sights); + std::vector local; + for (const TilePos& pos : at){ + const std::optional tile = story_to_local(*maps, goal.map, pos); + if (!tile){ + fail(m_io.route(), std::format("{}: the walk ended off {}.", step.name, goal.map)); + } + local.push_back(*tile); + } + return Pokemon::tile_pose(goal.map, local, Pokemon::facing_of(goal)); +} + +Outcome StoryWalkSteps::route(RunState& state, const StoryStep& step){ + if (Pokemon::tiles_of(state.pose).empty()){ + return Pokemon::outcome_failed("no_pose", Pokemon::to_string(state.pose)); + } + const GridPose& target = story_arg(step.args, "target"); + return Pokemon::outcome_ok(walk_to(state.pose, Pokemon::pose_of(target), step)); +} + +// A LEGS step "to_dialogue": taps its one leg until a dialogue starts on the +// way, wherever it starts (Dawn's at Sandgem's edge, r35/r36: an exact tile +// and banner raced her scene). The scene's lines are the next step's. +Outcome StoryWalkSteps::leg_to_dialogue(const StoryStep& step){ + const auto& legs_list = story_arg>(step.args, "legs"); + const Pokemon::StoryLeg& leg = legs_list.front(); + turn(leg.direction); + const WalkResult walk = m_io.route().walk_taps(leg.direction, (size_t)leg.tiles, step.name); + if (walk.wild_battles > 0){ + return Pokemon::outcome_failed("interrupted_dialogue_approach", step.name); + } + log(std::format("[{}] {}: taps {} to a dialogue: {} tiles, {}.", m_row, step.name, direction_name(leg.direction), walk.moved_chunks, walk_stop_name(walk.stop))); + if (walk.stop == WalkStop::DIALOG){ + return Pokemon::outcome_ok(); + } + // A scene can lock us before its first line: Dawn walks over first (r37), + // or Mom walks over to us on the mat (r7). + if (m_io.route().dialog_within(10000ms, COLOR_CYAN)){ + return Pokemon::outcome_ok(); + } + return Pokemon::outcome_failed("no_dialogue", step.name); +} + +// A LEGS step "to_map": runs its one leg until the location banner of +// pose_after's map shows, wherever it shows. A straight run along an open +// row needs no tile count (r41, r42: Sandgem's row 11 took us into the lab). +Outcome StoryWalkSteps::leg_to_map(const StoryStep& step){ + const auto& legs_list = story_arg>(step.args, "legs"); + const Pokemon::StoryLeg& leg = legs_list.front(); + const std::string map = step.pose_after ? Pokemon::grid_pose_of(*step.pose_after).map : ""; + turn(leg.direction); + const bool arrived = m_io.run_until_map(leg.direction, Milliseconds(250) * leg.tiles + 2000ms, map, step.name); + return arrived ? Pokemon::outcome_ok() : Pokemon::outcome_failed("no_banner", map); +} + +// A STEP "to_banner": D-pad taps toward a door or cave mouth until the fade +// and pose_after's map's location banner. No banner fails the step right +// there. +Outcome StoryWalkSteps::tap_to_banner(const StoryStep& step){ + const std::string map = step.pose_after ? Pokemon::grid_pose_of(*step.pose_after).map : ""; + const Direction direction = story_arg(step.args, "direction"); + const std::optional taps = m_io.tap_to_banner(direction, map, 30); + if (!taps){ + return Pokemon::outcome_failed("no_banner", map); + } + log(std::format("[{}] {}: {}'s banner after {} taps {}.", m_row, step.name, map, *taps, direction_name(direction))); + return Pokemon::outcome_ok(); +} + +namespace{ +Milliseconds door_timeout(const std::string& map_name, const Pose& pose, Direction direction, bool can_run){ + try{ + const Pokemon::MapData& map = story_map(map_name); + const auto tiles_list = Pokemon::tiles_of(pose); + if (!tiles_list.empty()){ + const size_t tiles = Pokemon::tiles_to_door(map, tiles_list.front(), direction); + if (tiles > 0){ + const Milliseconds per_tile = route_ms_per_tile(direction, can_run ? MoveSpeed::RUN : MoveSpeed::WALK); + return std::max(1500ms, ROUTE_START_DELAY + per_tile * (int64_t)tiles + 800ms); + } + } + }catch (...){} + return Milliseconds(12000); +} +enum class CountedEndPolicy{ EXACT, ONE_SHORT }; +std::vector counted_ends(const std::vector& starts, const Pokemon::StoryLeg& leg, CountedEndPolicy policy){ + std::vector ends; + for (TilePos at : starts){ + for (int i = 0; i < leg.tiles; i++){ + if (policy == CountedEndPolicy::ONE_SHORT && i + 1 == leg.tiles){ + ends.push_back(at); + } + at = at.next(leg.direction); + } + ends.push_back(at); + } + return ends; +} +} // namespace + +Pose StoryWalkSteps::recover_tapped_leg(const Pose& from, const std::vector& starts, + const std::vector& goals, const Pokemon::StoryLeg& leg, const StoryStep& step){ + const StoryWorld* world = story_plan_world(from.map, from.map); + if (world == nullptr){ + fail(m_io.route(), "interrupted leg has no recovery world: " + from.map); + } + std::vector global; + for (const TilePos& at : starts){ + global.push_back(story_to_world(*world, from.map, at)); + } + std::vector possible; + for (const TilePos& at : interrupted_leg_positions(*world, global, leg.direction, (size_t)leg.tiles)){ + const auto local = story_to_local(*world, from.map, at); + if (!local){ + fail(m_io.route(), "interrupted leg crossed an unconfirmed map transition"); + } + possible.push_back(*local); + } + StoryStep recovery = step; + recovery.name += "_recovery"; + recovery.action.clear(); + const Pose found = walk_to(Pokemon::tile_pose(from.map, possible), Pokemon::tile_pose(from.map, goals, leg.direction), recovery); + // The route back can approach from another direction. Establish the + // original leg's facing before publishing that pose to the story runner. + turn(leg.direction); + return found; +} + +// Counted legs retain their normal path. An observed encounter invalidates +// the exact pose; localize from every legal prefix before the next leg. +Outcome StoryWalkSteps::tap_legs(const Pose& from, const StoryStep& step){ + const bool one_short_ok = step.action == "one_short_ok"; + std::vector at = Pokemon::tiles_of(from); + const auto& legs_list = story_arg>(step.args, "legs"); + for (const Pokemon::StoryLeg& leg : legs_list){ + turn(leg.direction); + const size_t tiles = (size_t)leg.tiles; + const WalkResult walk = m_io.route().walk_taps(leg.direction, tiles, step.name); + log(std::format("[{}] {}: taps {} moved {} of {} tiles.", m_row, step.name, direction_name(leg.direction), walk.moved_chunks, tiles)); + const auto next = counted_ends(at, leg, one_short_ok ? CountedEndPolicy::ONE_SHORT : CountedEndPolicy::EXACT); + if (walk.wild_battles > 0){ + if (walk.origin == WalkOrigin::MAY_PRECEDE_LEG){ + return Pokemon::outcome_failed("encounter_origin_unknown", step.name); + } + if (walk.stop != WalkStop::LIMIT && walk.stop != WalkStop::BLOCKED){ + return Pokemon::outcome_failed("interrupted_leg_unconfirmed", step.name); + } + at = Pokemon::tiles_of(recover_tapped_leg(from, at, next, leg, step)); + continue; + } + if (walk.moved_chunks + (one_short_ok ? 1 : 0) < tiles){ + return Pokemon::outcome_failed("short_leg", std::format("{} {} moved {}", direction_name(leg.direction), tiles, walk.moved_chunks)); + } + at = next; + } + return Pokemon::outcome_ok(Pokemon::tile_pose(from.map, at, legs_list.back().direction)); +} + +Outcome StoryWalkSteps::legs(RunState& state, const StoryStep& step){ + if (step.action == "to_wall"){ + const auto& legs_list = story_arg>(step.args, "legs"); + for (const Pokemon::StoryLeg& leg : legs_list){ + m_io.walk_until_stopped(leg.direction, (size_t)leg.tiles, label_of(step)); + } + return Pokemon::outcome_ok(); + } + if (step.action == "to_dialogue"){ + return leg_to_dialogue(step); + } + if (step.action == "to_map"){ + return leg_to_map(step); + } + const bool one_short_ok = step.action == "one_short_ok"; + if (!one_short_ok && !step.action.empty() && step.action != "legs"){ + return Pokemon::outcome_failed("unknown_action", step.action); + } + return tap_legs(state.pose, step); +} + +Outcome StoryWalkSteps::step(RunState& state, const StoryStep& step){ + const Direction direction = story_arg(step.args, "direction"); + if (step.action == "to_banner"){ + turn(direction); + return tap_to_banner(step); + } + if (step.action == "tap"){ + // No separate turn tap first: at 1 tile from the door, a turn tap + // steps onto the warp, causing walk_taps to take an extra step on the far map. + const WalkResult walk = m_io.route().walk_taps(direction, 1, step.name); + if (walk.wild_battles > 0){ + return Pokemon::outcome_failed("interrupted_step", step.name); + } + m_io.settle(BUTTON_B, label_of(step), false); + return Pokemon::outcome_ok(); + } + // A step through a door: walk (or run) on until the screen goes dark, + // from next to the door or from anywhere in its line (r43: tapping 8 tiles + // down the lab took 7 s), then settle for any scene on arrival (Mom's, + // 00.01; Dawn's tour, 00.04). Without the fade, settle must see it itself + // (r33 was outside, "no fade"). No separate turn tap first: at 1 tile + // from the door, a 50 ms turn tap can step in before run_into_door() watches. + if (step.pose_after && Pokemon::grid_pose_of(*step.pose_after).map != state.pose.map){ + const Milliseconds timeout = door_timeout(state.pose.map, state.pose, direction, m_can_run); + const bool faded = m_io.run_into_door(direction, m_can_run, timeout); + log(std::format("[{}] {}: {} through the door (timeout {} ms): {}.", m_row, step.name, direction_name(direction), timeout.count(), faded ? "faded" : "no fade yet")); + m_io.settle(BUTTON_B, label_of(step), !faded); + return Pokemon::outcome_ok(); + } + turn(direction); + const WalkResult walk = m_io.route().walk_taps(direction, 1, step.name); + if (walk.wild_battles > 0){ + return Pokemon::outcome_failed("interrupted_step", step.name); + } + log(std::format("[{}] {}: a step {}, {}.", m_row, step.name, direction_name(direction), walk_stop_name(walk.stop))); + std::vector at; + for (const TilePos& pos : Pokemon::tiles_of(state.pose)){ + at.push_back(pos.next(direction)); + } + return Pokemon::outcome_ok(Pokemon::tile_pose(state.pose.map, at, direction)); +} + +Outcome StoryWalkSteps::talk(RunState& state, const StoryStep& step){ + // Face whom we talk to: a route ends facing its last leg, not its goal + // (r40 reached Mom facing north). They block the tile, so the tap only turns. + if (const std::optional facing = Pokemon::facing_of(state.pose)){ + turn(*facing); + } + m_io.route().press({.kind = RouteInputKind::BUTTON_A, .direction = Direction::NORTH, .hold = 80ms, .release = 200ms, + .count = 1, .end = RouteInputEnd::DONE}); + // Roark's first line in the mine comes after the camera zooms in on + // us, which can take some seconds (r98; the old event waited 8 s). + if (!m_io.route().dialog_within(8000ms, COLOR_YELLOW)){ + fail(m_io.route(), label_of(step) + ": nobody answered."); + } + return Pokemon::outcome_ok(); +} + +Outcome StoryWalkSteps::mash(RunState& state, const StoryStep& step){ + // The step before may have played the scene already (a door step settles + // its arrival scene), so its first line is not required. + if (!m_io.route().dialog_within(15000ms, COLOR_CYAN)){ + m_io.route().note(label_of(step) + ": no line within 15 s; the step before played it.", RouteNote::WARNING); + } + m_io.settle(step.action == "a" ? BUTTON_A : BUTTON_B, label_of(step), + step.pose_after && Pokemon::grid_pose_of(*step.pose_after).map != state.pose.map && + story_plan_world(state.pose.map, Pokemon::grid_pose_of(*step.pose_after).map) == nullptr); + if (m_row == RUNNING_SHOES_ROW && step.name == RUNNING_SHOES_STEP){ + m_can_run = true; + } + return Pokemon::outcome_ok(); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.h new file mode 100644 index 0000000000..44dade0915 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.h @@ -0,0 +1,83 @@ +/* BDSP AutoStory - Story walk steps + * + * From: https://github.com/PokemonAutomation/ + * + * The story steps that walk and talk (ROUTE, LEGS, STEP, TALK, MASH), on a + * StoryStepIO: StoryExecutor runs them on the console, the story's + * simulation tests on a simulated player. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StoryWalkSteps_H +#define PokemonAutomation_PokemonBDSP_AutoStory_StoryWalkSteps_H + +#include +#include +#include +#include "Pokemon/AutoStory/Pokemon_StoryEvent.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" +#include "Pokemon/AutoStory/Pokemon_StoryPose.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryStep.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RoutePlanner.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_TrainerSight.h" +#include "PokemonBDSP_AutoStory_StoryStepIO.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Executes walking and movement-related story steps via StoryStepIO. +class StoryWalkSteps{ +public: + explicit StoryWalkSteps(StoryStepIO& io) + : m_io(io) + {} + + // A new attempt of the row `row`: every trainer can see us again, and + // the pace is the row's save's (a run after the Running Shoes). + void begin_attempt(const std::string& row); + // Whether the player has the Running Shoes: the pace routes are timed at. + bool can_run() const{ + return m_can_run; + } + + // Walks the planned route to `step.target`. Variant: the plan's costs. + Pokemon::Outcome route(Pokemon::RunState& state, const Pokemon::StoryStep& step); + // See StoryExecutor::legs(). + Pokemon::Outcome legs(Pokemon::RunState& state, const Pokemon::StoryStep& step); + // One D-pad step, or a run through a door when the step changes map. + Pokemon::Outcome step(Pokemon::RunState& state, const Pokemon::StoryStep& step); + // A at what we face; a dialogue must open. + Pokemon::Outcome talk(Pokemon::RunState& state, const Pokemon::StoryStep& step); + // Text to a quiet overworld; the Running Shoes' scene gives the shoes. + Pokemon::Outcome mash(Pokemon::RunState& state, const Pokemon::StoryStep& step); + +private: + Pokemon::Pose walk_to(const Pokemon::Pose& from, const Pokemon::Pose& goal, const Pokemon::StoryStep& step); + Pokemon::Pose recover_tapped_leg(const Pokemon::Pose& from, + const std::vector& starts, const std::vector& goals, + const Pokemon::StoryLeg& leg, const Pokemon::StoryStep& step); + Pokemon::Outcome tap_legs(const Pokemon::Pose& from, const Pokemon::StoryStep& step); + Pokemon::Outcome leg_to_dialogue(const Pokemon::StoryStep& step); + Pokemon::Outcome leg_to_map(const Pokemon::StoryStep& step); + Pokemon::Outcome tap_to_banner(const Pokemon::StoryStep& step); + void turn(Pokemon::Direction direction); + void log(const std::string& text); + + StoryStepIO& m_io; + std::string m_row; + bool m_can_run = false; + // The trainers who can still see us on each story_plan_world() world, + // in its tiles, from its first walk this attempt; all of them again at + // each attempt. + std::map> m_trainers; +}; + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.cpp new file mode 100644 index 0000000000..8c3647e593 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.cpp @@ -0,0 +1,409 @@ +/* BDSP AutoStory - Grid simulation + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_TrainerSight.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WalkRules.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_LegBattle.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkDpad.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" +#include "PokemonBDSP_AutoStory_GridSim.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; +using Pokemon::Direction; +using Pokemon::TileKind; +using Pokemon::TilePos; + + +namespace{ + +int floor_div(int value, int by){ + return value >= 0 ? value / by : -((-value + by - 1) / by); +} + +bool across(Direction direction){ + return direction == Direction::EAST || direction == Direction::WEST; +} +// +1 toward the east or south, -1 toward the west or north. +int sign_of(Direction direction){ + return direction == Direction::EAST || direction == Direction::SOUTH ? 1 : -1; +} +int& along(SimPos& pos, Direction direction){ + return across(direction) ? pos.x : pos.y; +} +int along(const SimPos& pos, Direction direction){ + return across(direction) ? pos.x : pos.y; +} +int sideways(const SimPos& pos, Direction direction){ + return across(direction) ? pos.y : pos.x; +} +TilePos tile_at(Direction direction, int along_tile, int side_tile){ + return across(direction) ? TilePos{along_tile, side_tile} : TilePos{side_tile, along_tile}; +} + +// The tiles a player at `pos` moves into with their next tile toward +// `direction`: one on the grid, two off it. `pos` is on the grid along +// `direction`. +std::vector tiles_ahead(const SimPos& pos, Direction direction){ + const int next = along(pos, direction) / SIM_TILE + sign_of(direction); + const int side = sideways(pos, direction); + std::vector tiles{tile_at(direction, next, floor_div(side, SIM_TILE))}; + if (side % SIM_TILE != 0){ + tiles.push_back(tile_at(direction, next, floor_div(side, SIM_TILE) + 1)); + } + return tiles; +} + +bool is_door(const StoryWorld& world, const TilePos& pos){ + const std::optional tile = world.tile(pos); + return tile && *tile == TileKind::DOOR && !world.occupied(pos); +} + +Milliseconds per_tile(Direction direction, SimPace pace, bool can_run){ + return route_ms_per_tile(direction, pace == SimPace::RUN && can_run ? MoveSpeed::RUN : MoveSpeed::WALK); +} + +} // namespace + + +namespace detail{ +SimPos sim_pos(const TilePos& tile){ + return {tile.x * SIM_TILE, tile.y * SIM_TILE}; +} +bool on_grid(const SimPos& pos){ + return pos.x % SIM_TILE == 0 && pos.y % SIM_TILE == 0; +} +TilePos nearest_tile(const SimPos& pos){ + return {floor_div(pos.x + SIM_TILE / 2, SIM_TILE), floor_div(pos.y + SIM_TILE / 2, SIM_TILE)}; +} +std::string to_string(const SimPos& pos){ + return std::format("({:g}, {:g})", pos.x / (double)SIM_TILE, pos.y / (double)SIM_TILE); +} +} // namespace detail + +std::string_view sim_stop_name(SimStop stop){ + switch (stop){ + case SimStop::DONE: + return "done"; + case SimStop::BLOCKED: + return "blocked"; + case SimStop::DIALOG: + return "dialog"; + case SimStop::WARP: + return "warp"; + case SimStop::LIMIT: + return "limit"; + } + return "?"; +} + + +GridSim::GridSim(SimWorld world, const TilePos& start, Direction facing, bool can_run) + : m_world(std::move(world)) +{ + m_player.pos = sim_pos(start); + m_player.facing = facing; + m_player.can_run = can_run; +} + +void GridSim::place(SimWorld world, const TilePos& start, std::optional facing){ + m_world = std::move(world); + m_player.pos = sim_pos(start); + if (facing){ + m_player.facing = *facing; + } + m_player.dialog = false; + m_player.trainer.reset(); + m_player.warped.reset(); + if (m_world.can_run){ + m_player.can_run = *m_world.can_run; + } +} + +// Moves into the next tile toward `direction`, `part` of the way (a whole +// tile or less), from a position on the grid along it; adds what it moved +// to `moved`. A ledge hops two whole tiles and a door warps, however far +// the press goes. Nothing if the player may go on, else why the move ended. +std::optional GridSim::enter_next(Direction direction, int part, int& moved){ + const StoryWorld& world = *m_world.world; + const std::vector ahead = tiles_ahead(m_player.pos, direction); + if (ahead.size() == 1){ + const TilePos from = nearest_tile(m_player.pos); + const std::optional landing = PokemonBDSP::step(world, from, direction); + if (landing && *landing != ahead[0]){ + part = 2 * SIM_TILE; + }else if (!landing && (is_door(world, ahead[0]) || std::find(m_world.doors.begin(), m_world.doors.end(), ahead[0]) != m_world.doors.end())){ + m_player.pos = sim_pos(ahead[0]); + m_player.warped = ahead[0]; + moved += SIM_TILE; + return SimStop::WARP; + }else if (!landing){ + return SimStop::BLOCKED; + } + } + // Off the grid, both tiles must be open: a door frame or a corner stops us. + for (const TilePos& pos : ahead){ + if (ahead.size() > 1 && !standable(world, pos)){ + return SimStop::BLOCKED; + } + } + along(m_player.pos, direction) += sign_of(direction) * part; + moved += part; + return std::nullopt; +} + +// On reaching a whole tile: a scene's trigger or a trainer's sight stops us. +std::optional GridSim::arrive(){ + const TilePos tile = nearest_tile(m_player.pos); + for (const SimTrigger& trigger : m_world.triggers){ + const bool fired = std::find(m_player.fired.begin(), m_player.fired.end(), trigger.name) != m_player.fired.end(); + if (!fired && std::find(trigger.tiles.begin(), trigger.tiles.end(), tile) != trigger.tiles.end()){ + m_player.fired.push_back(trigger.name); + m_player.dialog = true; + return SimStop::DIALOG; + } + } + for (const TrainerSight& trainer : m_world.trainers){ + const bool fought = std::find(m_player.fought.begin(), m_player.fought.end(), trainer.name) != m_player.fought.end(); + const std::vector sight = sight_tiles(*m_world.world, trainer); + if (!fought && std::find(sight.begin(), sight.end(), tile) != sight.end()){ + m_player.dialog = true; + m_player.trainer = trainer.name; + return SimStop::DIALOG; + } + } + return std::nullopt; +} + +// Moves up to `distance` toward `direction`. +SimMove GridSim::advance(Direction direction, int distance){ + m_player.facing = direction; + if (m_player.dialog){ + return {.stop = SimStop::DIALOG}; + } + int moved = 0; + while (moved < distance){ + const int offset = along(m_player.pos, direction) % SIM_TILE; + if (offset != 0){ + // Part of a tile to the next whole one: no new tile is entered. + const int to_whole = sign_of(direction) > 0 ? SIM_TILE - offset : offset; + const int part = std::min(to_whole, distance - moved); + along(m_player.pos, direction) += sign_of(direction) * part; + moved += part; + }else if (const std::optional stop = enter_next(direction, std::min(SIM_TILE, distance - moved), moved)){ + return {.stop = *stop, .distance = moved}; + } + if (along(m_player.pos, direction) % SIM_TILE == 0){ + if (const std::optional stop = arrive()){ + return {.stop = *stop, .distance = moved}; + } + } + } + return {.stop = SimStop::DONE, .distance = moved}; +} + +SimMove GridSim::tap(Direction direction, Milliseconds hold, bool stick){ + const Milliseconds tile_time = route_ms_per_tile(direction, MoveSpeed::WALK); + if (!stick && hold < 100ms){ + m_player.facing = direction; + return {.stop = m_player.dialog ? SimStop::DIALOG : SimStop::DONE, .time = hold}; + } + // The stick moves as far as it is held; the D-pad whole tiles. + int distance = (int)(hold.count() * SIM_TILE / tile_time.count()); + if (!stick){ + distance = std::max(1, (int)((hold + tile_time / 2) / tile_time)) * SIM_TILE; + } + SimMove move = advance(direction, distance); + move.time = hold; + return move; +} + +SimMove GridSim::hold(Direction direction, Milliseconds limit, SimPace pace){ + const Milliseconds tile_time = per_tile(direction, pace, m_player.can_run); + const int64_t tiles = limit < ROUTE_START_DELAY ? 0 : (limit - ROUTE_START_DELAY) / tile_time; + int moved = 0; + SimStop stop = SimStop::LIMIT; + while (moved < tiles * SIM_TILE){ + const SimMove step = advance(direction, SIM_TILE); + moved += step.distance; + if (step.stop != SimStop::DONE){ + stop = step.stop; + break; + } + } + if (stop == SimStop::LIMIT){ + return {.stop = stop, .distance = moved, .time = limit}; + } + // A stop is seen once the screen has held still a while. + const Milliseconds seen = stop == SimStop::BLOCKED ? 800ms : 0ms; + return {.stop = stop, .distance = moved, .time = ROUTE_START_DELAY + tile_time * moved / SIM_TILE + seen}; +} + +bool GridSim::press_a(){ + if (m_player.dialog){ + return true; + } + const StoryWorld& world = *m_world.world; + const TilePos faced = nearest_tile(m_player.pos).next(m_player.facing); + const std::optional tile = world.tile(faced); + m_player.dialog = world.occupied(faced) != StoryWorld::NOBODY || (tile && *tile == TileKind::SIGN); + for (const TrainerSight& trainer : m_world.trainers){ + if (m_player.dialog && trainer.pos == faced){ + m_player.trainer = trainer.name; + } + } + return m_player.dialog; +} + +void GridSim::end_dialogue(){ + if (m_player.trainer){ + m_player.fought.push_back(*m_player.trainer); + } + m_player.trainer.reset(); + m_player.dialog = false; +} + + +bool sim_can_step(const GridSim& sim, Direction direction){ + GridSim probe = sim; + const SimMove move = probe.tap(direction, ROUTE_MS_PER_TILE_ACROSS, false); + return move.distance > 0; +} + + +namespace{ + +WalkStop walk_stop(SimStop stop){ + switch (stop){ + case SimStop::BLOCKED: + return WalkStop::BLOCKED; + case SimStop::DIALOG: + return WalkStop::DIALOG; + case SimStop::WARP: + return WalkStop::TRANSITION; + case SimStop::DONE: + case SimStop::LIMIT: + return WalkStop::LIMIT; + } + return WalkStop::LIMIT; +} + +} // namespace + +WalkResult GridSimRouteIO::walk_to_wall(const RouteWallWalk& walk){ + const SimPace pace = walk.options.pace == DpadPace::RUN ? SimPace::RUN : SimPace::WALK; + const SimMove move = m_sim.hold(walk.direction, walk.limit, pace); + m_notes.push_back(std::format("[Sim] {}: held {} to {} at {}.", walk.label, direction_name(walk.direction), + sim_stop_name(move.stop), to_string(m_sim.player().pos))); + return {.stop = walk_stop(move.stop), .walked = move.time, .wild_battles = 0, .moved_chunks = (size_t)(move.distance / SIM_TILE)}; +} + +std::optional GridSimRouteIO::take_encounter(Direction direction, size_t tiles, size_t tap, const std::string& label){ + const auto found = std::find_if(m_encounters.begin(), m_encounters.end(), [&](const SimWildTap& encounter){ + return encounter.direction == direction && encounter.requested_tiles == tiles + && encounter.tap == tap && encounter.label == label; + }); + if (found == m_encounters.end()){ + return std::nullopt; + } + SimWildTap encounter = *found; + m_encounters.erase(found); + return encounter; +} + +// A tap a tile, as walk_tiles_dpad() taps: two taps in a row that move +// nothing end the walk. +WalkResult GridSimRouteIO::walk_taps(Direction direction, size_t tiles, const std::string& label){ + WalkResult result{.stop = WalkStop::LIMIT, .walked = 0ms, .wild_battles = 0, .moved_chunks = 0}; + result.events.record({}, result.started); + size_t still = 0; + size_t moved_at_battle = 0; + for (size_t tap = 0; tap < tiles && result.stop == WalkStop::LIMIT; tap++){ + const auto encounter = take_encounter(direction, tiles, tap + 1, label); + const SimMove move = encounter && encounter->phase == SimEncounterPhase::BEFORE_MOVEMENT + ? SimMove{} : m_sim.tap(direction, ROUTE_MS_PER_TILE_ACROSS, false); + result.walked += 800ms; + if (encounter){ + // Match the production escape_battle() credit, independently + // of actual simulated displacement. Consumers must not infer + // pose solely from this credit after an interrupted input. + result.wild_battles++; + if (tap == 0 || encounter->origin == WalkOrigin::MAY_PRECEDE_LEG){ + result.origin = WalkOrigin::MAY_PRECEDE_LEG; + } + result.moved_chunks++; + // The console's watchers time the battle from the tap that set + // it off: this one, or for a battle before this tap moved, the + // one before (0: a step before the walk). + record_tap_battle(result, encounter->phase == SimEncounterPhase::BEFORE_MOVEMENT ? tap : tap + 1); + moved_at_battle = result.moved_chunks; + still = 0; + m_notes.push_back("[Sim] cleared wild encounter during " + label); + continue; + } + still = move.distance > 0 ? 0 : still + 1; + result.moved_chunks += move.distance > 0 ? 1 : 0; + if (move.stop == SimStop::DIALOG || move.stop == SimStop::WARP){ + result.stop = walk_stop(move.stop); + }else if (still >= 2){ + result.stop = WalkStop::BLOCKED; + } + } + if (result.wild_battles > 0){ + result.moved_after_battle = result.moved_chunks - moved_at_battle; + } + m_notes.push_back(std::format("[Sim] {}: {} taps {}, {} moved, {} at {}.", label, tiles, direction_name(direction), + result.moved_chunks, walk_stop_name(result.stop), to_string(m_sim.player().pos))); + return result; +} + +void GridSimRouteIO::press(const RouteInput& input){ + for (size_t press = 0; press < input.count; press++){ + switch (input.kind){ + case RouteInputKind::DPAD: + m_sim.tap(input.direction, input.hold, false); + break; + case RouteInputKind::STICK: + m_sim.tap(input.direction, input.hold, true); + break; + case RouteInputKind::BUTTON_A: + m_sim.press_a(); + break; + case RouteInputKind::WAIT: + case RouteInputKind::PAUSE: + break; + } + } +} + +bool GridSimRouteIO::dialog_within(Milliseconds, Color){ + return m_sim.player().dialog; +} + +bool GridSimRouteIO::holding_against_wall(Direction direction, const std::string&){ + return !sim_can_step(m_sim, direction); +} + +void GridSimRouteIO::look(const std::function&){} + +void GridSimRouteIO::note(const std::string& text, RouteNote kind){ + m_notes.push_back(text); + if (kind == RouteNote::FAILURE){ + throw GridSimFailure{text}; + } +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.h new file mode 100644 index 0000000000..426c566185 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.h @@ -0,0 +1,232 @@ +// code-health: allow header_symbols -- grid simulation primitives and types for BDSP overworld movement simulation +/* BDSP AutoStory - Grid simulation + * + * From: https://github.com/PokemonAutomation/ + * + * A player walking on a story world, by the movement measured on the + * console, so walks that go wrong only on the Switch go wrong in tests: + * + * - A D-pad tap of 40 to 80 ms only turns; 160 ms steps a whole tile. + * - The D-pad held walks whole tiles until something stops it. + * - A stick tap moves part of a tile (100 ms about 0.6 tile across), and + * later D-pad moves keep that offset: the player is off the tile grid. + * - Walls, signs and people on their tiles stop the player. Off the grid + * the player covers two tiles, and both must be open: a door frame or + * a tree corner stops a player a part of a tile off. + * - Walking onto a door warps, but only from its exact column or row. + * - A trainer whose sight line the player steps into stops the walk with + * their dialogue; so does a scene's trigger tile. + * + * Positions are in thousandths of a tile, so parts of tiles add up exactly. + * GridSimRouteIO is the walker's RouteIO on a GridSim. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_GridSim_H +#define PokemonAutomation_PokemonBDSP_AutoStory_GridSim_H + +#include +#include +#include +#include +#include +#include "Common/Cpp/Time.h" +#include "Pokemon/Navigation/Pokemon_Grid.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RoutePlanner.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_TrainerSight.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteIO.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace detail{ + +// A tile, in the simulation's units. +inline constexpr int SIM_TILE = 1000; + +// A world position in thousandths of a tile: the player covers the square +// from (x, y) to (x + SIM_TILE, y + SIM_TILE). +struct SimPos{ + int x = 0; + int y = 0; + bool operator==(const SimPos& other) const = default; +}; +// Converts a TilePos to its fixed-point fractional simulation coordinate. +SimPos sim_pos(const Pokemon::TilePos& tile); +// Whether the player stands exactly on a tile. +bool on_grid(const SimPos& pos); +// The tile the player mostly stands on. +Pokemon::TilePos nearest_tile(const SimPos& pos); +// "(12.375, 5)". +std::string to_string(const SimPos& pos); + +// A scene that starts when the player steps onto one of `tiles` (a coord +// event): its dialogue opens and the player stops. It fires once. +struct SimTrigger{ + std::string name; + std::vector tiles; +}; + +} // namespace detail + +// A tile, in the simulation's units. +using detail::SIM_TILE; +// Position in simulation units. +using detail::SimPos; +// Simulation position at the origin of a tile. +using detail::sim_pos; +// Checks if a position is exactly on grid. +using detail::on_grid; +// Returns the nearest tile to a position. +using detail::nearest_tile; +// Formats position as string. +using detail::to_string; +// Trigger region in simulation units. +using detail::SimTrigger; + +// Where the simulated player walks: the world (which must outlive the +// simulation), its trainers and its scene triggers, in world tiles; and +// the tiles that warp like doors though the maps draw them as walls (a +// house's stairs, entered from beside them); and, if the caller knows, +// whether B runs from here on (the Running Shoes, 00.03). +struct SimWorld{ + const StoryWorld* world = nullptr; + std::vector trainers = {}; + std::vector triggers = {}; + std::vector doors = {}; + std::optional can_run = std::nullopt; +}; + +// Reason why simulated player movement stopped. +enum class SimStop{ + DONE, // the input finished + BLOCKED, // stopped by a wall, a person, or a door off its column + DIALOG, // a trainer saw us or a scene started; the player is frozen + WARP, // stepped onto a door + LIMIT, // a held walk ran out of time +}; +// Returns the debug string for a SimStop reason. +std::string_view sim_stop_name(SimStop stop); + +// What one input did. +struct SimMove{ + SimStop stop = SimStop::DONE; + int distance = 0; // thousandths of a tile + Milliseconds time = Milliseconds(0); +}; + +// The player as the simulation has them. +struct SimPlayer{ + SimPos pos; + Pokemon::Direction facing = Pokemon::Direction::NORTH; + // Whether B runs: the Running Shoes (00.03). + bool can_run = false; + // A dialogue is open: a trainer's, a scene's or a talk's. + bool dialog = false; + // The trainer whose dialogue is open, if one is. + std::optional trainer; + // The door the last warp stepped onto. + std::optional warped; + std::vector fought = {}; + std::vector fired = {}; +}; + +// How long a held walk takes per tile in `direction`. +enum class SimPace{ + WALK, + RUN, // B held: a run once the player has the Running Shoes +}; + +// Simulates grid-based player movement and collisions in BDSP. +class GridSim{ +public: + GridSim(SimWorld world, const Pokemon::TilePos& start, Pokemon::Direction facing, bool can_run); + + // One press toward `direction`: the D-pad or the stick, held for `hold`. + SimMove tap(Pokemon::Direction direction, Milliseconds hold, bool stick); + // Holds toward `direction` until something stops the player or `limit` + // runs out. Moves whole tiles, from wherever the player stands. + SimMove hold(Pokemon::Direction direction, Milliseconds limit, SimPace pace); + // A: talks to a person or a sign on the faced tile. Returns whether a + // dialogue is open. + bool press_a(); + // The open dialogue ends: a trainer's battle is won and they see us no more. + void end_dialogue(); + // Puts the player on `start` of `world`: after a warp or a scene. + void place(SimWorld world, const Pokemon::TilePos& start, std::optional facing); + + const SimPlayer& player() const{ + return m_player; + } + const StoryWorld& world() const{ + return *m_world.world; + } + +private: + SimMove advance(Pokemon::Direction direction, int distance); + std::optional enter_next(Pokemon::Direction direction, int part, int& moved); + std::optional arrive(); + + SimWorld m_world; + SimPlayer m_player; +}; + +// Whether the player could take a step toward `direction` from where they stand. +bool sim_can_step(const GridSim& sim, Pokemon::Direction direction); + + +// A deterministic, already-cleared encounter at a tap boundary. This is +// normalized movement/escape evidence, not a model of image detectors or +// battle mechanics. BEFORE leaves the tile unchanged; AFTER completes it. +enum class SimEncounterPhase{ BEFORE_MOVEMENT, AFTER_MOVEMENT }; +// Match one tap and supply its cleared encounter phase and origin evidence. +struct SimWildTap{ + std::string label; + Pokemon::Direction direction = Pokemon::Direction::NORTH; + size_t requested_tiles = 0; + size_t tap = 0; // one-based within the matching walk_taps call + SimEncounterPhase phase = SimEncounterPhase::AFTER_MOVEMENT; + WalkOrigin origin = WalkOrigin::CURRENT_LEG; +}; + +// The walker's RouteIO on a GridSim. A failure throws GridSimFailure. +class GridSimRouteIO : public RouteIO{ +public: + // Every line noted goes to `notes`, which must outlive this. + GridSimRouteIO(GridSim& sim, std::vector& notes, std::vector encounters = {}) + : m_sim(sim) + , m_notes(notes) + , m_encounters(std::move(encounters)) + {} + + virtual WalkResult walk_to_wall(const RouteWallWalk& walk) override; + virtual WalkResult walk_taps(Pokemon::Direction direction, size_t tiles, const std::string& label) override; + virtual void press(const RouteInput& input) override; + virtual bool dialog_within(Milliseconds wait, Color color) override; + virtual bool holding_against_wall(Pokemon::Direction direction, const std::string& label) override; + virtual void look(const std::function& at) override; + virtual void note(const std::string& text, RouteNote kind) override; + +private: + std::optional take_encounter(Pokemon::Direction direction, size_t tiles, size_t tap, const std::string& label); + + GridSim& m_sim; + std::vector& m_notes; + std::vector m_encounters; +}; + +// What GridSimRouteIO throws for RouteNote::FAILURE. +struct GridSimFailure{ + std::string message; +}; + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.cpp new file mode 100644 index 0000000000..76e6cc9d35 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.cpp @@ -0,0 +1,458 @@ +/* BDSP AutoStory Grid Simulation Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The grid simulation's moves against the console's measurements, then the + * route walker (walk_route()) on it: every route of the story table from + * where the table has the player, and the routes to doors that a turn on + * the stick missed on the console (r49, r57). + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Pokemon/AutoStory/Pokemon_TilePose.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_PlanCosts.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RobustRoute.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" +#include "PokemonBDSP_AutoStory_GridSim.h" +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" +#include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.h" +#include "PokemonBDSP_AutoStory_GridSim_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +namespace{ + +using namespace std::chrono_literals; +using Pokemon::Direction; +using Pokemon::GridPose; +using Pokemon::story_arg; +using Pokemon::Pose; +using Pokemon::StoryStep; +using Pokemon::StoryStepKind; +using Pokemon::TilePos; + + +StoryWorld make_world(const std::vector& rows){ + Pokemon::MapData map; + map.name = "Test"; + map.tiles = Pokemon::Grid{(int)rows[0].size(), (int)rows.size(), {}}; + for (const std::string& row : rows){ + for (char glyph : row){ + map.tiles.cells.push_back(*Pokemon::tile_from_glyph(glyph)); + } + } + StoryWorld world; + world.add("Test", map, {0, 0}); + return world; +} + +SimPos at_tile(int x, int y, int part_x = 0, int part_y = 0){ + return {x * SIM_TILE + part_x, y * SIM_TILE + part_y}; +} + + +// One input to the simulation. +enum class SimInputKind{ + DPAD, + STICK, + HOLD, +}; +struct SimInput{ + SimInputKind kind; + Direction direction; + Milliseconds time; +}; + +// Inputs from `start`, and where the player must end and how the last +// input ended. +struct MoveCase{ + std::string name; + TilePos start; + std::vector inputs; + SimPos end; + SimStop stop; +}; + +SimMove apply(GridSim& sim, const SimInput& input){ + switch (input.kind){ + case SimInputKind::DPAD: + return sim.tap(input.direction, input.time, false); + case SimInputKind::STICK: + return sim.tap(input.direction, input.time, true); + case SimInputKind::HOLD: + return sim.hold(input.direction, input.time, SimPace::WALK); + } + return {}; +} + +// A room with a door in row 3 at x 3, a ledge south at x 7, someone +// standing at (5, 1), and a trainer east of row 2 who looks west 3 tiles. +UnitTestResult test_moves(Logger&){ + StoryWorld world = make_world({ + "#########", + "#.......#", + "#.......#", + "###D###v#", + "#.......#", + "#########", + }); + world.block({5, 1}); + const SimWorld setup{.world = &world, .trainers = {{.name = "watcher", .pos = {8, 2}, .facing = Direction::WEST, .range = 3}}}; + constexpr Direction EAST = Direction::EAST; + constexpr Direction SOUTH = Direction::SOUTH; + constexpr Direction WEST = Direction::WEST; + const std::vector cases{ + {.name = "a 50 ms D-pad tap only turns", .start = {2, 1}, .inputs = {{SimInputKind::DPAD, EAST, 50ms}}, .end = at_tile(2, 1), .stop = SimStop::DONE}, + {.name = "a 160 ms D-pad tap steps a tile", .start = {2, 1}, .inputs = {{SimInputKind::DPAD, EAST, 160ms}}, .end = at_tile(3, 1), .stop = SimStop::DONE}, + {.name = "a 100 ms stick tap steps 0.6 tile across", .start = {2, 1}, .inputs = {{SimInputKind::STICK, EAST, 100ms}}, .end = at_tile(2, 1, 625), .stop = SimStop::DONE}, + {.name = "a 100 ms stick tap steps 0.4 tile down", .start = {2, 1}, .inputs = {{SimInputKind::STICK, SOUTH, 100ms}}, .end = at_tile(2, 1, 0, 416), .stop = SimStop::DONE}, + {.name = "the D-pad keeps a stick tap's offset", .start = {2, 1}, + .inputs = {{SimInputKind::STICK, EAST, 100ms}, {SimInputKind::DPAD, EAST, 160ms}}, .end = at_tile(3, 1, 625), .stop = SimStop::DONE}, + {.name = "a held walk stops at someone standing", .start = {2, 1}, .inputs = {{SimInputKind::HOLD, EAST, 5000ms}}, .end = at_tile(4, 1), .stop = SimStop::BLOCKED}, + {.name = "a door warps from its column", .start = {3, 2}, .inputs = {{SimInputKind::HOLD, SOUTH, 3000ms}}, .end = at_tile(3, 3), .stop = SimStop::WARP}, + {.name = "part of a tile off the door's column, the frame stops us", .start = {3, 2}, + .inputs = {{SimInputKind::STICK, WEST, 100ms}, {SimInputKind::HOLD, SOUTH, 3000ms}}, .end = at_tile(2, 2, 375), .stop = SimStop::BLOCKED}, + {.name = "a trainer's sight stops a walk", .start = {1, 2}, .inputs = {{SimInputKind::HOLD, EAST, 5000ms}}, .end = at_tile(5, 2), .stop = SimStop::DIALOG}, + {.name = "a ledge hops two tiles", .start = {7, 2}, .inputs = {{SimInputKind::DPAD, SOUTH, 160ms}}, .end = at_tile(7, 4), .stop = SimStop::DONE}, + }; + for (const MoveCase& item : cases){ + GridSim sim(setup, item.start, Direction::NORTH, false); + SimMove last; + for (const SimInput& input : item.inputs){ + last = apply(sim, input); + } + if (sim.player().pos != item.end || last.stop != item.stop){ + return std::format("{}: expected {} ({}), got {} ({}).", item.name, to_string(item.end), sim_stop_name(item.stop), + to_string(sim.player().pos), sim_stop_name(last.stop)); + } + } + return true; +} + + +// A turn on the stick, as before r49: each turn tap of the D-pad becomes a +// 100 ms stick tap, which moves the player part of a tile. +class StickTurnIO : public RouteIO{ +public: + explicit StickTurnIO(RouteIO& io) + : m_io(io) + {} + virtual WalkResult walk_to_wall(const RouteWallWalk& walk) override{ + return m_io.walk_to_wall(walk); + } + virtual WalkResult walk_taps(Direction direction, size_t tiles, const std::string& label) override{ + return m_io.walk_taps(direction, tiles, label); + } + virtual void press(const RouteInput& input) override{ + if (input.kind != RouteInputKind::DPAD || input.hold >= 100ms){ + m_io.press(input); + return; + } + RouteInput stick = input; + stick.kind = RouteInputKind::STICK; + stick.hold = 100ms; + m_io.press(stick); + } + virtual bool dialog_within(Milliseconds wait, Color color) override{ + return m_io.dialog_within(wait, color); + } + virtual bool holding_against_wall(Direction direction, const std::string& label) override{ + return m_io.holding_against_wall(direction, label); + } + virtual void look(const std::function& at) override{ + m_io.look(at); + } + virtual void note(const std::string& text, RouteNote kind) override{ + m_io.note(text, kind); + } + +private: + RouteIO& m_io; +}; + + +// A route walked on the simulation: where the walker believes it ended, +// where the player is, and why it failed. +struct SimWalk{ + std::vector believed; + SimPos actual; + std::string failure; + std::vector notes; +}; + +// One route to walk: on `world`, from `from` (the walker's belief) with the +// player on `start`, to any of `targets`. +struct SimRoute{ + std::string label; + const StoryWorld* world; + std::vector from; + TilePos start; + std::vector targets; + RobustPlanCosts costs; + bool can_run; +}; + +SimWalk walk_on_sim(const SimRoute& route, bool stick_turns){ + SimWalk out; + GridSim sim({.world = route.world, .trainers = story_trainers(*route.world)}, route.start, Direction::NORTH, route.can_run); + GridSimRouteIO sim_io(sim, out.notes); + StickTurnIO stick_io(sim_io); + RouteIO& io = stick_turns ? static_cast(stick_io) : sim_io; + RouteTrainers trainers{story_trainers(*route.world), [&sim](const std::string&){ sim.end_dialogue(); }}; + const std::vector& targets = route.targets; + try{ + out.believed = walk_route(io, *route.world, route.from, trainers, { + .goal = [&targets](const TilePos& pos){ return std::find(targets.begin(), targets.end(), pos) != targets.end(); }, + .label = route.label, + .avoid = {}, + .costs = route.costs, + .speed = route.can_run ? MoveSpeed::RUN : MoveSpeed::WALK, + }); + }catch (const GridSimFailure& e){ + out.failure = e.message; + } + out.actual = sim.player().pos; + return out; +} + +// Why a walk did not end where it should: on the grid, on one of the +// targets, and on a tile the walker believes. Empty if it did. +std::string walk_error(const SimRoute& route, const SimWalk& walk){ + const auto has = [](const std::vector& tiles, const TilePos& pos){ + return std::find(tiles.begin(), tiles.end(), pos) != tiles.end(); + }; + const TilePos tile = nearest_tile(walk.actual); + if (!walk.failure.empty()){ + return std::format("{} from {}: failed at {}: {}", route.label, describe(*route.world, route.start), + to_string(walk.actual), walk.failure); + } + if (!on_grid(walk.actual) || !has(route.targets, tile) || !has(walk.believed, tile)){ + return std::format("{} from {}: ended at {} ({}), off the grid or off the target.", route.label, + describe(*route.world, route.start), to_string(walk.actual), describe(*route.world, tile)); + } + return ""; +} + + +std::vector world_tiles(const StoryWorld& world, const Pose& pose){ + std::vector tiles; + for (const TilePos& pos : Pokemon::tiles_of(pose)){ + tiles.push_back(story_to_world(world, pose.map, pos)); + } + return tiles; +} + +// Where the table has the player after `step`, from `at`: a route's +// target, a walk's last tile, a step's next tile; a scene or an event's +// end is the table's pose_after, or unknown. +std::optional pose_after_step(const std::optional& at, const StoryStep& step){ + std::optional next; + if (step.kind == StoryStepKind::ROUTE){ + const GridPose& target = story_arg(step.args, "target"); + next = Pokemon::pose_of(target); + }else if (at && step.kind == StoryStepKind::STEP){ + const Direction direction = story_arg(step.args, "direction"); + std::vector tiles; + for (const TilePos& pos : Pokemon::tiles_of(*at)){ + tiles.push_back(pos.next(direction)); + } + next = Pokemon::tile_pose(at->map, tiles, direction); + }else if (at && step.kind == StoryStepKind::LEGS && (step.action.empty() || step.action == "legs" || step.action == "open")){ + std::vector tiles = Pokemon::tiles_of(*at); + const auto& legs_list = story_arg>(step.args, "legs"); + for (const Pokemon::StoryLeg& leg : legs_list){ + for (TilePos& pos : tiles){ + for (int i = 0; i < leg.tiles; i++){ + pos = pos.next(leg.direction); + } + } + } + next = Pokemon::tile_pose(at->map, tiles, legs_list.back().direction); + }else if (step.kind != StoryStepKind::GAME && step.kind != StoryStepKind::SHOP && + step.kind != StoryStepKind::MASH && step.kind != StoryStepKind::LEGS + ){ + next = at; + } + return step.pose_after ? std::optional(Pokemon::pose_of(*step.pose_after)) : next; +} + +// Every ROUTE step of the table that starts from a known pose, from its +// first and its last tile, at the pace StoryExecutor walks it. +std::vector table_routes(){ + std::vector routes; + for (const StoryCheckpoint& row : story_checkpoints()){ + std::optional at = row.start ? std::optional(Pokemon::pose_of(*row.start)) : std::nullopt; + bool shoes = row.id > std::string(RUNNING_SHOES_ROW); + for (const StoryStep& step : row.steps){ + const GridPose* target_ptr = step.kind == StoryStepKind::ROUTE ? &story_arg(step.args, "target") : nullptr; + const StoryWorld* world = at && target_ptr ? story_plan_world(at->map, target_ptr->map) : nullptr; + const std::vector from = world ? world_tiles(*world, *at) : std::vector{}; + std::vector starts; + if (!from.empty()){ + starts.push_back(from.front()); + } + if (from.size() > 1){ + starts.push_back(from.back()); + } + for (const TilePos& start : starts){ + routes.push_back({.label = row.id + " " + step.name, .world = world, .from = from, .start = start, + .targets = world_tiles(*world, Pokemon::pose_of(*target_ptr)), .costs = *story_route_costs(step.action), .can_run = shoes}); + } + at = pose_after_step(at, step); + shoes = shoes || (row.id == RUNNING_SHOES_ROW && step.name == RUNNING_SHOES_STEP); + } + } + return routes; +} + +UnitTestResult test_table_routes(Logger& logger){ + std::string errors; + size_t walked = 0; + for (const SimRoute& route : table_routes()){ + const SimWalk walk = walk_on_sim(route, false); + walked++; + const std::string error = walk_error(route, walk); + logger.log(std::format("{} from {}: {}", route.label, describe(*route.world, route.start), error.empty() ? "on target" : error)); + if (!error.empty()){ + errors += "\n " + error; + } + } + if (walked < 10){ + return std::format("Expected the table's routes; found {}.", walked); + } + return errors.empty() ? UnitTestResult(true) : UnitTestResult("Routes that did not end on their target:" + errors); +} + + +// A route to the tile below a door, then a walk north into it. +struct DoorRoute{ + std::string name; + std::string map; + TilePos from; + std::string door; +}; + +// Whether the route ends below the door and a walk north goes in. +std::string door_route_error(const DoorRoute& item, bool stick_turns){ + const StoryWorld& world = *story_plan_world(item.map, item.map); + const TilePos from = story_to_world(world, item.map, item.from); + const TilePos below = story_to_world(world, item.map, map_point(item.map, item.door).next(Direction::SOUTH)); + const SimRoute route{.label = item.name, .world = &world, .from = {from}, .start = from, .targets = {below}, + .costs = town_plan_costs(), .can_run = true}; + const SimWalk walk = walk_on_sim(route, stick_turns); + const std::string error = walk_error(route, walk); + if (!error.empty()){ + return error; + } + GridSim sim({.world = &world}, below, Direction::NORTH, true); + if (sim.hold(Direction::NORTH, 3000ms, SimPace::RUN).stop != SimStop::WARP){ + return item.name + ": the walk north did not go through the door."; + } + return ""; +} + +// r49 put us a tile west of Sandgem's Center door after a stick "turn", +// and r57 walked the Jubilife School's row a column short: a turn tap on +// the stick moves part of a tile, and every D-pad tap after keeps it. With +// D-pad turns both routes end below their doors; with stick turns they +// must miss, or the simulation would not have caught it. +UnitTestResult test_door_routes(Logger& logger){ + const std::vector routes{ + {.name = "Sandgem: below the Center's door", .map = "SandgemTown", .from = map_point("SandgemTown", "front_row_east_end"), + .door = "pokemon_center_door"}, + {.name = "Jubilife: below the School's door", .map = "JubilifeCity", .from = map_point("JubilifeCity", "pokemon_center_door").next(Direction::SOUTH), + .door = "trainers_school_door"}, + }; + for (const DoorRoute& item : routes){ + const std::string dpad = door_route_error(item, false); + if (!dpad.empty()){ + return "D-pad turns: " + dpad; + } + const std::string stick = door_route_error(item, true); + logger.log(item.name + ", stick turns: " + stick); + if (stick.empty()){ + return item.name + ": with turns on the stick, expected the route to miss the door; it went in."; + } + } + return true; +} + + +struct AutoStoryGridSimInput{ + std::string name; + std::function body; +}; + +struct AutoStoryGridSimExpectation{ +}; + +struct AutoStoryGridSimCase{ + AutoStoryGridSimInput input; + AutoStoryGridSimExpectation expectation; +}; + +class Test_GridSim : public UnitTest{ +public: + explicit Test_GridSim(AutoStoryGridSimCase test_case) + : UnitTest("PokemonBDSP::AutoStory - Grid sim: " + test_case.input.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + try{ + return m_case.input.body(logger); + }catch (Exception& e){ + return std::string(e.name()) + ": " + e.message(); + } + } + +private: + AutoStoryGridSimCase m_case; +}; + +} // namespace + + +const AutoStoryGridSimCase AUTO_STORY_GRID_SIM_CASES[] = { + { + .input = {"taps, holds, stick offsets, doors, people and trainers as measured", test_moves}, + .expectation = {}, + }, + { + .input = {"the table's routes end on their targets", test_table_routes}, + .expectation = {}, + }, + { + .input = {"a stick-tap turn misses the Sandgem Center and Jubilife School doors", test_door_routes}, + .expectation = {}, + }, +}; + +void add_tests_AutoStory_GridSim(UnitTestDatabase& database){ + for (const auto& item : AUTO_STORY_GRID_SIM_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.h new file mode 100644 index 0000000000..dc085dda37 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.h @@ -0,0 +1,28 @@ +/* BDSP AutoStory Grid Simulation Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_GridSim_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_GridSim_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + + +// The grid simulation (PokemonBDSP_AutoStory_GridSim.h): the moves +// measured on the console, the story table's routes walked by the route +// walker on it, and walks that missed doors on the console missing them here. +void add_tests_AutoStory_GridSim(UnitTestDatabase& database); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StorySimulation_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySimulation_Tests.cpp similarity index 94% rename from SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StorySimulation_Tests.cpp rename to SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySimulation_Tests.cpp index 79bab21f0a..8991e40511 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StorySimulation_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySimulation_Tests.cpp @@ -26,15 +26,15 @@ #include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" #include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" #include "Pokemon/AutoStory/Pokemon_TilePose.h" -#include "Data/PokemonBDSP_AutoStory_RobustRoute.h" -#include "PokemonBDSP_AutoStory_PlanGolden.h" -#include "Data/PokemonBDSP_AutoStory_WalkRules.h" -#include "Utils/PokemonBDSP_AutoStory_Location.h" -#include "Data/PokemonBDSP_AutoStory_PlanCosts.h" -#include "PokemonBDSP_AutoStory_StoryPlaces.h" -#include "PokemonBDSP_AutoStory_StoryTable.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RobustRoute.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlanGolden.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WalkRules.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_PlanCosts.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h" #include "PokemonBDSP_AutoStory_StorySimulation_Tests.h" -#include "Utils/PokemonBDSP_AutoStory_StoryLanguage.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -179,7 +179,8 @@ std::string walk_leg_from( } // A LEGS step from `from`, walked tile by tile on its map, a tile short too -// where the step allows it. Walks to walls end where nothing is known. +// where the step allows it. Walks to walls, and walks that end on another +// map (into Sandgem until Dawn's scene starts), end where nothing is known. Walked walk_legs(const Pose& from, const StoryStep& step){ if (step.action == "to_wall"){ return {std::nullopt, "", ""}; diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StorySimulation_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySimulation_Tests.h similarity index 100% rename from SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StorySimulation_Tests.h rename to SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySimulation_Tests.h diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 982ea17164..2e70c9b466 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1730,6 +1730,8 @@ file(GLOB LIBRARY_SOURCES Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.h Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.cpp Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_WildBattle.h + Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_CoordEvents.cpp + Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_CoordEvents.h Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.cpp Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_PlanCosts.cpp @@ -1765,6 +1767,8 @@ file(GLOB LIBRARY_SOURCES Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.cpp Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlanGolden.h Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.h + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RecoveryWalk_Tests.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RecoveryWalk_Tests.h Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RoutePlanner_Tests.cpp Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RoutePlanner_Tests.h Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Segment00_Tests.cpp @@ -1779,18 +1783,26 @@ file(GLOB LIBRARY_SOURCES Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryMaps_Tests.h Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.cpp Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.h - Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StorySimulation_Tests.cpp - Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StorySimulation_Tests.h + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.cpp Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.h Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.cpp Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.h + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.cpp + Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.h Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.cpp Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWorld_Tests.h Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.cpp Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.h Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.cpp Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.h + Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.cpp + Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.h + Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.cpp + Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.h + Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySimulation_Tests.cpp + Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySimulation_Tests.h Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_CountedWallLeg.h Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.cpp Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Indoors.h