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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.h"
#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryLines_Tests.h"
#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryLaunch_Tests.h"
#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryMarkerDistance_Tests.h"
#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryNewGame_Tests.h"
#include "Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.h"
#include "Programs/AutoStory/PokemonLZA_AutoStorySceneMachine_Tests.h"
Expand All @@ -41,7 +42,7 @@



void add_tests(UnitTestDatabase& database){

Check warning on line 45 in SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonLZA::add_tests( UnitTestDatabase & database): function_lines=33 (advisory above 25, limit 50)
add_tests_AlertEyeDetector(database);
add_tests_BoxCellInfoDetector(database);
add_tests_ButtonDetector(database);
Expand All @@ -67,6 +68,7 @@
add_tests_AutoStoryNewGame(database);
add_tests_AutoStoryGame(database);
add_tests_AutoStoryLines(database);
add_tests_AutoStoryMarkerDistance(database);
add_tests_AutoStoryObjectiveMarkerReader(database);
add_tests_AutoStorySceneMachine(database);
add_tests_AutoStorySceneReplay(database);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "VideoGames/PokemonLZA/Programs/PokemonLZA_BallThrow.h"
#include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryCaughtMarkReader.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.h"
#include "PokemonLZA_AutoStoryLines.h"
#include "PokemonLZA_AutoStoryMarkerWalk.h"
#include "PokemonLZA_AutoStoryCatches.h"
Expand Down Expand Up @@ -134,6 +133,10 @@ constexpr Milliseconds PATROL_LIMIT(600000);
// How far short of a road stop still counts as there: the walk back to 60
// from nearer the gate stopped at 58 once (console B, 2026-11-14).
constexpr int ROAD_STOP_SLACK = 3;
// The patrol never walks toward the objective below this, read or not:
// 60 is just before the far gate, and past it is the city (console B,
// 2026-11-16, a walk to 100 came out at 37).
constexpr int ROAD_FLOOR = ROAD_SPOTS[std::size(ROAD_SPOTS) - 1] - ROAD_STOP_SLACK;
constexpr Milliseconds STOP_WALK_LIMIT(30000);
// The first walk, from where 03.07 left the player, is the longest.
constexpr Milliseconds FIRST_WALK_LIMIT(90000);
Expand Down Expand Up @@ -328,21 +331,7 @@ std::optional<int> walk_to_stop(StoryEnv& env, PlzaBackend& backend, std::option
if (last && stop > *last){
return walk_back_from_marker(env, backend, stop, STOP_WALK_LIMIT);
}
return walk_toward_marker(env, backend, stop, last ? STOP_WALK_LIMIT : FIRST_WALK_LIMIT);
}

// The objective's distance on the screen now, if it reads.
std::optional<int> objective_distance_now(StoryEnv& env){
env.context.wait_for_all_requests();
const VideoSnapshot frame = env.env.console.video().snapshot();
if (!frame){
return std::nullopt;
}
const std::optional<ObjectiveMarker> marker = read_objective_marker(env.env.console, frame);
if (!marker || marker->distance <= 0){
return std::nullopt;
}
return marker->distance;
return walk_toward_marker(env, backend, stop, ROAD_FLOOR, last ? STOP_WALK_LIMIT : FIRST_WALK_LIMIT);
}

// What the patrol came to: the party bar's last read, the throws.
Expand Down Expand Up @@ -402,7 +391,7 @@ Pokemon::Outcome catch_in_zone(StoryEnv& env, PlzaBackend& backend, const ZoneCa
// Taunie's two rocks stand between 03.07's end and the marker: a step
// right first, around them (a catch's autosave can leave the save
// further on).
const std::optional<int> start = objective_distance_now(env);
const std::optional<int> start = read_marker_distance(env, backend, ROAD_FLOOR);
if (start && *start >= 115){
pbf_move_left_joystick(env.context, /*position=*/{1, 0}, /*hold=*/Milliseconds(1000), /*release=*/Milliseconds(300));
}
Expand All @@ -422,7 +411,7 @@ Pokemon::Outcome catch_in_zone(StoryEnv& env, PlzaBackend& backend, const ZoneCa
}

Pokemon::Outcome walk_back_to_zone_road(StoryEnv& env, PlzaBackend& backend){
std::optional<int> at = objective_distance_now(env);
std::optional<int> at = read_marker_distance(env, backend, std::nullopt);
for (int stop : road_stops_to_gate(at)){
backend.log(std::format("[PLZA] walk_back_to_zone_road: to {} on the road, from {}", stop, at.value_or(-1)));
const bool toward = !at || stop <= *at;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/* PokemonLZA AutoStory - Marker distance
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <algorithm>
#include <iterator>
#include "PokemonLZA_AutoStoryMarkerDistance.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonLZA{


namespace{

bool near(int a, int b){
return a >= b - 12 && a <= b + 12;
}

// How many reads not taken a read must agree with to be taken anyway,
// and how many are kept.
constexpr size_t AGREEING_UNTAKEN = 2;
constexpr size_t UNTAKEN_KEPT = 3;

// The looks in a row with no read taken that each new view gets, and the
// views in turn: the camera nudges, then the sidesteps; after the last,
// the walk is blocked.
constexpr size_t LOOKS_PER_VIEW = 3;
constexpr MarkerMove CAMERA_NUDGES[] = {
MarkerMove::NUDGE_CAMERA_LEFT,
MarkerMove::NUDGE_CAMERA_RIGHT,
};
// The lanes (MarkerDistance::lane()) the sidesteps go to in turn, around
// the marker's line rather than from where the walk stands, so a walk
// does not drift to one side.
constexpr int SIDESTEP_LANES[] = {-1, 1, -2, 2};
constexpr size_t NEW_VIEWS = std::size(CAMERA_NUDGES) + std::size(SIDESTEP_LANES);

// Reads taken in a row with none further on that make a walk held up,
// and how much further on it then is before it leaves the held-up lane.
constexpr size_t HELD_UP_READS = 4;
constexpr int HELD_UP_CLEARED = 6;

} // namespace


bool MarkerDistance::read(int distance){
if (m_floor && distance < *m_floor){
return false;
}
if (!m_last || !near(distance, *m_last)){
size_t agreeing = 0;
for (int untaken : m_untaken){
if (near(distance, untaken)){
agreeing++;
}
}
if (agreeing < AGREEING_UNTAKEN){
if (m_untaken.size() == UNTAKEN_KEPT){
m_untaken.erase(m_untaken.begin());
}
m_untaken.push_back(distance);
return false;
}
}
m_untaken.clear();
m_last = distance;
return true;
}

MarkerMove MarkerDistance::next(std::optional<int> distance, int stop_at){
if (!distance || !read(*distance)){
return unread_move();
}
m_unread_looks = 0;
if (reached(stop_at)){
m_lane = 0;
return MarkerMove::ARRIVED;
}
if (!m_best || progress(*m_best, *m_last) > 0){
m_best = m_last;
m_unmoved_reads = 0;
}else if (++m_unmoved_reads >= HELD_UP_READS && m_held_ups < std::size(SIDESTEP_LANES)){
m_unmoved_reads = 0;
m_held_at = m_best;
return sidestep_to(SIDESTEP_LANES[m_held_ups++]);
}
if (m_held_at && progress(*m_held_at, *m_best) >= HELD_UP_CLEARED){
m_held_at.reset();
}
if (!m_held_at){
m_lane = 0;
}
return MarkerMove::STEP;
}

MarkerMove MarkerDistance::unread_move(){
m_unread_looks++;
const size_t views = m_unread_looks / LOOKS_PER_VIEW;
if (m_unread_looks % LOOKS_PER_VIEW != 0){
// Between new views: a nudged camera stays, else re-centered.
return views > 0 && views <= std::size(CAMERA_NUDGES) ? MarkerMove::REREAD : MarkerMove::RECENTER;
}
if (views <= std::size(CAMERA_NUDGES)){
return CAMERA_NUDGES[views - 1];
}
if (views > NEW_VIEWS){
m_lane = 0;
return MarkerMove::BLOCKED;
}
m_held_at.reset();
return sidestep_to(SIDESTEP_LANES[views - 1 - std::size(CAMERA_NUDGES)]);
}

MarkerMove MarkerDistance::sidestep_to(int lane){
const bool left = lane < m_lane;
m_lane = lane;
return left ? MarkerMove::SIDESTEP_LEFT : MarkerMove::SIDESTEP_RIGHT;
}

int MarkerDistance::progress(int from, int to) const{
return m_direction == MarkerDirection::BACK ? to - from : from - to;
}

bool MarkerDistance::reached(int stop_at) const{
if (m_direction == MarkerDirection::BACK){
return *m_last >= stop_at;
}
return *m_last <= std::max(stop_at, m_floor.value_or(stop_at));
}



} // namespace PokemonLZA
} // namespace NintendoSwitch
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/* PokemonLZA AutoStory - Marker distance
*
* From: https://github.com/PokemonAutomation/
*
* Where a walk by the objective marker is, by the marker's distance
* reads with misreads dropped, and whether it steps on. It steps only on
* a read taken: with none, it stands, re-centers the camera on the marker
* and reads again. On 03.08's walk to 100 (console B, 2026-11-16) the
* digits read 109, then nothing or garbage for 13 s while the walk kept
* stepping, and it came out at 37 in the city, through the far gate.
*
*/

#ifndef PokemonAutomation_PokemonLZA_AutoStoryMarkerDistance_H
#define PokemonAutomation_PokemonLZA_AutoStoryMarkerDistance_H

#include <optional>
#include <vector>

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonLZA{


// Which way a walk by the marker goes: on toward it (the distance drops)
// or back away from it (the distance grows).
enum class MarkerDirection{
TOWARD,
BACK,
};

// What a walk by the marker does after a look. With no read taken, it
// stands and reads again, and every third look in a row with none it
// tries a new view: the camera nudged a little left, then right, then a
// short step to a lane beside the marker's line (lane()): 1 left, 1
// right, 2 left, 2 right; then it ends. Never a step toward or away
// without a read taken: in run 4 of 03.08 (console B, 2026-11-16) an
// NPC's speech bubble sat over the marker's digits, and re-centering put
// the marker right back under it.
enum class MarkerMove{
STEP, // a read taken, the stop not reached: a step on
RECENTER, // no read taken: stand, the camera back onto the marker, read again
REREAD, // no read taken after a nudge: stand, the camera kept where it is, read again
NUDGE_CAMERA_LEFT, // the third look with none: the camera a little left, read again
NUDGE_CAMERA_RIGHT,
SIDESTEP_LEFT, // still none after both nudges, or held up: re-center, step left to lane(), read again
SIDESTEP_RIGHT,
BLOCKED, // still none after every sidestep: the walk ends
ARRIVED, // a read taken at the stop (or, toward, at the floor)
};

// Where a walk by the marker is: its last distance read taken.
class MarkerDistance{
public:
// `floor`: reads below it are never taken, and a walk toward the
// objective ends at it.
MarkerDistance(MarkerDirection direction, std::optional<int> floor)
: m_direction(direction)
, m_floor(floor)
{}

// A distance read (above 0) on a look. Below the floor it is not
// taken: the marker half behind Taunie's rocks read 1, 1 and 4 at
// the start of 03.08 while it showed 120 (runs 2 and 3, console B
// 2026-11-16). Else it is taken when within 12 of position(), or when
// two of the last three reads not taken agree with it (each within
// 12): so the first read is taken only once three agree, and three
// that agree replace a wrong last one (110 read as "1110" on console
// B, 2026-11-13). Returns whether the read is taken.
bool read(int distance);

// A look with `distance` read (none: no digits): STEP or ARRIVED
// only when the read is taken, ARRIVED at `stop_at` or past it in the
// walk's direction (toward: at the floor too); else what the walk
// does for a new view (MarkerMove), by the looks in a row with none.
// 4 reads taken in a row with none further on in the walk's direction
// is a walk held up: a sidestep to the next lane, kept until the walk
// is 6 further on. In run 5 of 03.08 (console B, 2026-11-16) the walk
// back from 60 read 65 for 15 steps back, twice, and ran out of time.
MarkerMove next(std::optional<int> distance, int stop_at);

// The last read taken, if any.
std::optional<int> position() const{
return m_last;
}

// Where the walk should stand across the marker's line, in sidesteps
// (right above 0, left below), at most 2 out. A read taken brings it
// back to 0 (but for a held-up walk's lane), and so do ARRIVED and
// BLOCKED. In run 5 of 03.08 every sidestep was kept and each new
// stretch started left, so the walk ended 7 steps left, against a
// wall off Wild Zone 1's road (console B, 2026-11-16).
int lane() const{
return m_lane;
}

private:
bool reached(int stop_at) const;
// How much further on `to` is than `from` in the walk's direction.
int progress(int from, int to) const;
MarkerMove unread_move();
MarkerMove sidestep_to(int lane);

const MarkerDirection m_direction;
const std::optional<int> m_floor;
std::optional<int> m_last;
std::vector<int> m_untaken; // the last three reads not taken, oldest first
size_t m_unread_looks = 0; // looks in a row with no read taken
std::optional<int> m_best; // the furthest-on read taken
size_t m_unmoved_reads = 0; // reads taken in a row with none further on than m_best
size_t m_held_ups = 0; // held-up sidesteps so far
std::optional<int> m_held_at; // m_best when the walk was last held up, while it keeps that lane
int m_lane = 0;
};



} // namespace PokemonLZA
} // namespace NintendoSwitch
} // namespace PokemonAutomation
#endif
Loading
Loading