Skip to content
Draft
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
26 changes: 20 additions & 6 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 @@ -180,6 +184,15 @@ struct Move{
};

// Cost incurred when stopping at a node.
// A leg of one step off a warp's arrival 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 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){
if (!node.moving || model.held_at(node.state, (Direction)node.facing)){
Expand All @@ -192,7 +205,7 @@ 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);
int cost = node.one_step && node.from_warp ? 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,7 +296,8 @@ 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){
Expand Down Expand Up @@ -382,7 +396,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 @@ -318,6 +318,28 @@ UnitTestResult test_wild_ends_with_leg(){
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, so after the warp on
// the right the short way pays no open stop. From the start on X, not
// put there by a warp, it does.
const ToyGame game({
"X...#W",
"#.#.#.",
"#...#.",
});
const auto warped = plan(game, {5, 2}, {1, 2}, Direction::NORTH);
if (!warped || warped->size() != 3 || !(*warped)[0].warps || (*warped)[1].tiles != 1 || (*warped)[1].to_wall){
return std::format("After the warp, expected east 1 in the open, then south:{}", warped ? describe(*warped) : " none");
}
const auto started = plan(game, {0, 0}, {1, 2}, Direction::NORTH);
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_unreachable(){
const ToyGame game({
".#.",
Expand Down Expand Up @@ -402,6 +424,10 @@ const LegPlannerTestCase LEG_PLANNER_CASES[] = {
.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 pays no open stop", .test = test_one_tile_open_stop},
.expectation = {.description = "takes one step off a warp's arrival into the open over a longer way round to walls"},
},
};

} // 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 @@ std::vector<MapPosition> leg_tiles(
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(
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),
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
Loading
Loading