Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 33 additions & 9 deletions SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ namespace LegPlannerInternal{
// they are in the middle of a leg that could go on, whether that leg had a
// wild step, whether it is one step from where a hold to a wall ended, and
// whether such a step was stopped after earlier (see tap_stop_risk() at
// the top of this file).
// the top of this file); whether the leg is one step so far, and whether
// it started on a warp's arrival (at rest: whether a warp put the player
// there).
template <typename State>
struct Node{
State state;
Expand All @@ -158,6 +160,8 @@ struct Node{
bool wild = false;
bool tap_from_wall = false;
bool tapped = false;
bool one_step = false;
bool from_warp = false;

bool operator==(const Node& other) const = default;
};
Expand All @@ -166,8 +170,8 @@ struct Node{
template <typename State, typename StateHash>
struct NodeHash{
size_t operator()(const Node<State>& node) const{
return StateHash()(node.state) * 80 + node.facing * 16 + (node.moving ? 8 : 0) + (node.wild ? 4 : 0)
+ (node.tap_from_wall ? 2 : 0) + (node.tapped ? 1 : 0);
return StateHash()(node.state) * 320 + node.facing * 64 + (node.moving ? 32 : 0) + (node.wild ? 16 : 0)
+ (node.tap_from_wall ? 8 : 0) + (node.tapped ? 4 : 0) + (node.one_step ? 2 : 0) + (node.from_warp ? 1 : 0);
}
};

Expand All @@ -179,9 +183,25 @@ struct Move{
Step<State> step;
};

// Cost incurred when stopping at a node.
// Cost incurred when stopping at a node, before stepping `next` if the
// route goes on.
// A leg of one step off a warp's arrival, followed by the step straight
// back, pays no open stop, nor a wild one: a step can't stop part-way, and
// the warp put the player on a known tile, so stepping off a teleporter or
// a ladder and back on again is one tile each way rather than a hold to a
// wall and back. What the model weighs past the stop it still pays. Only
// straight back: going on from the stop starts a leg from wherever an
// interruption left the player, and free single steps off doors would turn
// every walk out of one into an extra leg. Only off a warp: a route's start
// may be where an open leg left the player, a step after another leg
// starts wherever that leg ended, and steps from walls on land weave runs
// of single tiles round longer holds (see tap_stop_risk() at the top of
// this file).
template <typename Model, typename State>
int stop_cost(const Model& model, const Node<State>& node, const LegCosts& costs){
int stop_cost(
const Model& model, const Node<State>& node, const LegCosts& costs,
std::optional<Direction> next = std::nullopt
){
if (!node.moving || model.held_at(node.state, (Direction)node.facing)){
return 0;
}
Expand All @@ -192,7 +212,10 @@ int stop_cost(const Model& model, const Node<State>& node, const LegCosts& costs
}
}
}
int cost = costs.per_open_stop + (node.wild ? costs.per_open_wild_stop : 0);
const bool straight_back = next && (int)*next == (node.facing + 2) % 4;
int cost = node.one_step && node.from_warp && straight_back
? 0
: costs.per_open_stop + (node.wild ? costs.per_open_wild_stop : 0);
if constexpr (requires { model.open_stop_risk(node.state, Direction::NORTH); }){
cost += model.open_stop_risk(node.state, (Direction)node.facing);
}
Expand Down Expand Up @@ -283,11 +306,12 @@ void expand(
const bool from_wall = current.moving && model.held_at(current.state, (Direction)current.facing);
const Node<State> next{
step->next, (int)step->facing, continues, (extends && current.wild) || step->wild,
!extends && from_wall, current.tapped || (!extends && current.tap_from_wall)
!extends && from_wall, current.tapped || (!extends && current.tap_from_wall),
!extends, step->warped || (extends ? current.from_warp : !current.moving && current.from_warp)
};
int step_cost = costs.per_tile + step->extra_cost;
if (current.facing != (int)direction){
step_cost += costs.per_turn + stop_cost(model, current, costs);
step_cost += costs.per_turn + stop_cost(model, current, costs, direction);
}
relax(next, cost + step_cost, Move<State>{current, direction, *step}, search);
}
Expand Down Expand Up @@ -382,7 +406,7 @@ std::optional<std::vector<Leg<typename Model::State>>> plan_legs(
using State = typename Model::State;
using Node = LegPlannerInternal::Node<State>;

const Node first{start, start_facing ? (int)*start_facing : 4, false, false, false, false};
const Node first{start, start_facing ? (int)*start_facing : 4, false, false, false, false, false, false};
LegPlannerInternal::Search<Model> search;
const std::optional<Node> end = LegPlannerInternal::search_goal(model, first, at_goal, goal_facing, costs, search);
if (!end){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <format>
#include <functional>
#include <optional>
#include <string>
#include <vector>
#include "Common/Cpp/TestRunners/UnitTest.h"
Expand All @@ -23,7 +24,8 @@

// '#' wall, '.' floor, '"' tall grass, 'p' a person (blocks, but may walk
// off, so not a wall to stop against), 'v' entered only going south, 'W'
// a warp to the tile marked 'X'.
// a warp to the tile marked 'X'. If there is a 'Y', 'X' is a teleporter
// too: stepping onto it, not arriving on it, takes the player to 'Y'.
class ToyGame{
public:
using State = TilePos;
Expand All @@ -37,11 +39,14 @@
if (m_rows[y][x] == 'X'){
m_warp_to = {x, y};
}
if (m_rows[y][x] == 'Y'){
m_teleport_to = TilePos{x, y};
}
}
}
}

std::optional<Step<State>> press(const State& state, Direction direction) const{

Check warning on line 49 in SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::Pokemon::ToyGame::press( const State & state , Direction direction) const: function_lines=27 (advisory above 25, limit 50)
const TilePos next = state.next(direction);
const char tile = at(next);
if (tile == '#' || tile == 'p' || (tile == 'v' && direction != Direction::SOUTH)){
Expand All @@ -54,6 +59,12 @@
step.extra_cost = 5;
step.wild = true;
}
if (tile == 'X' && m_teleport_to){
step.next = *m_teleport_to;
step.warped = true;
step.ends_leg = true;
step.extra_cost = 10;
}
if (tile == 'W'){
step.next = m_warp_to;
step.warped = true;
Expand All @@ -77,6 +88,7 @@

std::vector<std::string> m_rows;
TilePos m_warp_to;
std::optional<TilePos> m_teleport_to;
};


Expand Down Expand Up @@ -318,6 +330,48 @@
return true;
}

UnitTestResult test_one_tile_open_stop(){
// From X, east 1 and south to the bottom wall stops in the open after
// one step; east to the wall and round is 2 tiles more and a turn. A
// step off a warp's arrival can't stop part-way, but where it leaves
// the player only matters to the step straight back: going on from
// there, the short way pays the open stop, after the warp on the right
// as from the start on X.
const ToyGame game({
"X...#W",
"#.#.#.",
"#...#.",
});
const auto warped = plan(game, {5, 2}, {1, 2}, Direction::NORTH);

Check warning on line 345 in SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: plan(): literal arguments {5, 2}, {1, 2} need a /*param=*/ comment (Google "Function Arguments", user review on fork #590)
if (!warped || warped->size() < 2 || !(*warped)[0].warps || !(*warped)[1].to_wall){
return std::format("After the warp, expected east to the wall:{}", warped ? describe(*warped) : " none");
}
const auto started = plan(game, {0, 0}, {1, 2}, Direction::NORTH);

Check warning on line 349 in SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: plan(): literal arguments {0, 0}, {1, 2} need a /*param=*/ comment (Google "Function Arguments", user review on fork #590)
if (!started || started->empty() || !(*started)[0].to_wall){
return std::format("From the start, expected east to the wall:{}", started ? describe(*started) : " none");
}
return true;
}

UnitTestResult test_one_tile_off_warp_and_back(){
// W puts the player on the teleporter X, facing east. Using it means
// stepping off and back on: east 1 into the open and straight back
// west, or east 2 to the wall and back. The step off can't stop
// part-way, so the short way pays no open stop.
const ToyGame game({
"..W#Y.",
"####..",
"X..#..",
});
const auto route = plan(game, {0, 0}, {5, 2}, Direction::EAST);

Check warning on line 366 in SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: plan(): literal arguments {0, 0}, {5, 2} need a /*param=*/ comment (Google "Function Arguments", user review on fork #590)
if (!route || route->size() < 3 || !(*route)[0].warps || (*route)[1].tiles != 1 || (*route)[1].to_wall
|| (*route)[2].direction != Direction::WEST || (*route)[2].tiles != 1 || !(*route)[2].warps
){
return std::format("Expected the warp, east 1, then west 1 onto the teleporter:{}", route ? describe(*route) : " none");
}
return true;
}

UnitTestResult test_unreachable(){
const ToyGame game({
".#.",
Expand Down Expand Up @@ -402,6 +456,14 @@
.input = {.name = "a new leg is not wild", .test = test_wild_ends_with_leg},
.expectation = {.description = "clears wild status on subsequent non-grass legs"},
},
{
.input = {.name = "a one-tile leg off a warp, going on, pays its open stop", .test = test_one_tile_open_stop},
.expectation = {.description = "goes round to the wall rather than stop one step off a warp's arrival and turn onward"},
},
{
.input = {.name = "a one-tile leg off a warp and straight back pays no open stop", .test = test_one_tile_off_warp_and_back},
.expectation = {.description = "steps off a teleporter's arrival and back on rather than to a wall and back"},
},
};

} // namespace
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Overworld Route

Check warning on line 1 in SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<file>: file_lines=984 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
Expand Down Expand Up @@ -871,6 +871,49 @@
return tiles;
}

std::vector<MapPosition> revisited_tiles(

Check warning on line 874 in SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::revisited_tiles( const std :: vector<MapData> & maps , const MapPosition & start , const std :: vector<RouteStep> & route , const Pokemon :: GateState & start_gates): function_lines=42 (advisory above 25, limit 50)
const std::vector<MapData>& maps,
const MapPosition& start, const std::vector<RouteStep>& route,
const Pokemon::GateState& start_gates
){
Pokemon::GateState gates = start_gates;
const bool gated = has_gates(maps);
std::vector<MapData> resolved;
if (gated){
resolved = resolve_maps(maps, gates);
}
std::vector<MapPosition> visited{start};
std::vector<MapPosition> revisited;
const auto visit = [&](const MapPosition& tile){
if (std::ranges::find(visited, tile) != visited.end()){
revisited.push_back(tile);
}else{
visited.push_back(tile);
}
};
MapPosition at = start;
for (const RouteStep& step : route){
if (step.action == RouteAction::OPEN_GATE || step.action == RouteAction::PRESS_SWITCH){
Pokemon::apply_gate_change(gates, step.gate_change);
if (gated){
resolved = resolve_maps(maps, gates);
}
}else if (step.action == RouteAction::WALK){
const std::vector<MapPosition> tiles = leg_tiles(gated ? resolved : maps, at, step);
for (size_t c = 1; c < tiles.size(); c++){
visit(tiles[c]);
}
if (!(tiles.back() == step.end)){
visit(step.end);
}
}else if (step.action == RouteAction::SURF){
visit(step.end);
}
at = step.end;
}
return revisited;
}

namespace{

// `step`'s tiles from `player`, on `maps` as the gates now make them.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@
const MapPosition& start, const RouteStep& step
);

// The tiles `route` from `start` steps on again, in the order it does: a
// route that crosses a tile twice walks back over its own way. Counts the
// tiles leg_tiles() walks, each warp's and spinner's end, and each Surf's
// landing. The gates start as `gates`, and the route's OPEN_GATE and
// PRESS_SWITCH steps change them, as in follow_overworld_route().
std::vector<MapPosition> revisited_tiles(

Check warning on line 72 in SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: 'revisited_tiles' is public only for PokemonFRLG_OverworldRoute_Tests.cpp, PokemonFRLG_AutoStory_Tests.cpp; test through the public function, or keep it in the .cpp's anonymous namespace (ignore if a later PR in the stack calls it) (user review on fork #590)
const std::vector<MapData>& maps,
const MapPosition& start, const std::vector<RouteStep>& route,
const Pokemon::GateState& gates
);

// Replays `route` from `start` with the game's step rules and returns where
// it ends. Throws InternalProgramError if a step can't be taken as planned.
// A route with gates (plan_overworld_route_gated()) starts with `gates`; its
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Overworld Route Tests

Check warning on line 1 in SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<file>: file_lines=631 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
Expand All @@ -11,6 +11,8 @@
#include <format>
#include <functional>
#include <string>
#include <string_view>
#include <vector>
#include "Common/Cpp/Exceptions.h"
#include "Common/Cpp/TestRunners/UnitTest.h"
#include "PokemonFRLG_OverworldMaps.h"
Expand Down Expand Up @@ -478,7 +480,84 @@
return true;
}

struct RevisitInput{
std::vector<RouteStep> route;
};
struct RevisitExpectation{
std::vector<MapPosition> revisited;
};
struct RevisitCase{
std::string_view name;
RevisitInput input;
RevisitExpectation expectation;
};

RouteStep sign_room_walk(Direction direction, int tiles, TilePos end, bool to_wall){
RouteStep step;
step.direction = direction;
step.tiles = tiles;
step.to_wall = to_wall;
step.end = MapPosition{"SignRoom", end};
return step;
}

// The routes from the sign room's (1, 3), its south-west corner.
const MapPosition SIGN_ROOM_CORNER{"SignRoom", {1, 3}};
const MapPosition SIGN_ROOM_SOUTH_MIDDLE{"SignRoom", {2, 3}};
const std::vector<RouteStep> EAST_AND_BACK{
sign_room_walk(Direction::EAST, 2, {3, 3}, true),

Check warning on line 508 in SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: sign_room_walk(): literal arguments 2, {3, 3}, true need a /*param=*/ comment (Google "Function Arguments", user review on fork #590)
sign_room_walk(Direction::WEST, 1, {2, 3}, false),
};
const std::vector<RouteStep> EAST_THEN_NORTH{
sign_room_walk(Direction::EAST, 2, {3, 3}, true),
sign_room_walk(Direction::NORTH, 2, {3, 1}, true),
};
const std::vector<RouteStep> ROUND_TO_THE_CORNER{
sign_room_walk(Direction::NORTH, 2, {1, 1}, true),
sign_room_walk(Direction::EAST, 1, {2, 1}, false),
sign_room_walk(Direction::SOUTH, 2, {2, 3}, true),
sign_room_walk(Direction::WEST, 1, {1, 3}, true),
};

// revisited_tiles() on the sign room: a leg back over the one before
// crosses its tiles again; a turn the other way doesn't.
UnitTestResult test_revisited_tiles(){

Check warning on line 524 in SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonFRLG::test_revisited_tiles(): function_lines=31 (advisory above 25, limit 50)
const std::vector<MapData> maps{parse_map(TEST_SIGN_ROOM_JSON, "SignRoom")};
const RevisitCase CASES[] = {
{
.name = "east to the wall, then back west",
.input = {.route = EAST_AND_BACK},
.expectation = {.revisited = {SIGN_ROOM_SOUTH_MIDDLE}},
},
{
.name = "east, then north",
.input = {.route = EAST_THEN_NORTH},
.expectation = {.revisited = {}},
},
{
.name = "round the room to the start",
.input = {.route = ROUND_TO_THE_CORNER},
.expectation = {.revisited = {SIGN_ROOM_CORNER}},
},
};
for (const RevisitCase& test_case : CASES){
const std::vector<MapPosition> revisited = revisited_tiles(maps, SIGN_ROOM_CORNER, test_case.input.route, {});
if (revisited != test_case.expectation.revisited){
std::string text;
for (const MapPosition& tile : revisited){
text += std::format(" {}", to_string(tile));
}
return std::format("{}: revisited{}:{}", test_case.name, text.empty() ? " none" : text, describe(test_case.input.route));
}
}
return true;
}

const RouteTestCase ROUTE_CASES[] = {
{
.input = {.name = "tiles a route crosses twice", .body = test_revisited_tiles},
.expectation = {.description = "revisited_tiles() lists the tiles a route steps on again"},
},
{
.input = {.name = "route through a warp", .body = test_route_through_warp},
.expectation = {.description = "route through a warp"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ inline constexpr std::string_view FRLG_PLAN_GOLDEN_16[] = {
"16.02 walk south 1 (to wall) -> SilphCo_5F (15, 8)",
"16.02 walk west 5 -> SilphCo_5F (10, 8)",
"16.02 walk south 12 (warp) -> SilphCo_9F (22, 18)",
"16.02 walk west 4 (to wall) -> SilphCo_9F (18, 18)",
"16.02 walk east 4 (warp) -> SilphCo_5F (10, 20)",
"16.02 walk west 1 -> SilphCo_9F (21, 18)",
"16.02 walk east 1 (warp) -> SilphCo_5F (10, 20)",
"16.02 walk east 2 (to wall) -> SilphCo_5F (12, 20)",
"16.03 to_3f_trainer from SilphCo_5F (21, 21) facing east to SilphCo_3F (22, 9) facing south",
"16.03 walk west 9 -> SilphCo_5F (12, 21)",
"16.03 walk north 1 (to wall) -> SilphCo_5F (12, 20)",
"16.03 walk west 2 (warp) -> SilphCo_9F (22, 18)",
"16.03 walk west 4 (to wall) -> SilphCo_9F (18, 18)",
"16.03 walk east 4 (warp) -> SilphCo_5F (10, 20)",
"16.03 walk west 1 -> SilphCo_9F (21, 18)",
"16.03 walk east 1 (warp) -> SilphCo_5F (10, 20)",
"16.03 walk north 12 -> SilphCo_5F (10, 8)",
"16.03 walk east 5 -> SilphCo_5F (15, 8)",
"16.03 walk north 1 (warp) -> SilphCo_3F (4, 6)",
Expand Down
Loading
Loading