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 @@ -23,6 +23,8 @@
#include "Inference/PokemonLZA_SelectionArrowDetector.h"
#include "Programs/AutoStory/PokemonLZA_AutoStoryFailures.h"
#include "Programs/AutoStory/PokemonLZA_AutoStoryStartLoad_Tests.h"
#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h"
#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.h"
#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs_Tests.h"
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.h"
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox_Tests.h"
Expand All @@ -38,7 +40,7 @@



void add_tests(UnitTestDatabase& database){

Check warning on line 43 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=30 (advisory above 25, limit 50)
add_tests_AlertEyeDetector(database);
add_tests_BoxCellInfoDetector(database);
add_tests_ButtonDetector(database);
Expand All @@ -62,6 +64,8 @@
add_tests_AutoStoryObjectiveMarkerReader(database);
add_tests_AutoStoryZoneGateReader(database);
add_tests_AutoStoryFailures(database);
add_tests_AutoStoryCatchPlan(database);
add_tests_AutoStoryCatchStop(database);
add_tests_AutoStoryDialogs(database);
add_tests_AutoStoryStartLoad(database);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* PokemonLZA AutoStory - Bracket watch
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <algorithm>
#include <cmath>
#include "PokemonLZA_AutoStoryBracketWatch.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonLZA{


BracketWatch::BracketWatch(std::chrono::milliseconds settle, std::chrono::milliseconds limit)
: m_settle(settle)
, m_limit(limit)
{}

void BracketWatch::add(std::chrono::milliseconds elapsed, const std::optional<BracketSighting>& sighting){
m_last = std::max(m_last, elapsed);
if (!sighting){
return;
}
if (!m_first_seen){
m_first_seen = elapsed;
}
m_sightings.push_back(*sighting);
}

bool BracketWatch::done() const{
if (m_last >= m_limit){
return true;
}
return m_first_seen && m_last - *m_first_seen >= m_settle;
}

std::optional<BracketSighting> BracketWatch::nearest() const{
std::optional<BracketSighting> best;
for (const BracketSighting& sighting : m_sightings){
if (!best || sighting.height > best->height){
best = sighting;
}
}
return best;
}


CameraTurn camera_turn_toward(double x){
const double off = x - 0.5;
CameraTurn turn;
if (std::abs(off) <= 0.06){
return turn;
}
turn.stick_x = off > 0 ? 0.4 : -0.4;
turn.time = std::chrono::milliseconds(std::lround(std::min(1.0, std::abs(off) * 4) * 400) + 80);
return turn;
}

std::vector<SweepLook> camera_sweep(CameraTurn step, int steps){
std::vector<SweepLook> looks;
looks.push_back(SweepLook{});
for (int i = 0; i < steps; i++){
looks.push_back(SweepLook{step, true});
}
const CameraTurn back{-step.stick_x, step.time};
for (int i = 0; i < steps; i++){
looks.push_back(SweepLook{back, false});
}
return looks;
}



} // namespace PokemonLZA
} // namespace NintendoSwitch
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/* PokemonLZA AutoStory - Bracket watch

Check warning on line 1 in SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryBracketWatch.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<header>: header_symbols=7 (advisory above 6, limit 8)
*
* From: https://github.com/PokemonAutomation/
*
* The console-free choice of which wild Pokemon to turn to at a catch
* stop, from the lock-on brackets seen there (LockOnBracketDetector).
*
*/

#ifndef PokemonAutomation_PokemonLZA_AutoStoryBracketWatch_H
#define PokemonAutomation_PokemonLZA_AutoStoryBracketWatch_H

#include <chrono>
#include <optional>
#include <vector>

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonLZA{


// Brackets seen around a wild Pokemon (find_lock_on_brackets()): their
// middle and height as shares of the screen.
struct BracketSighting{
double x = 0;
double y = 0;
double height = 0;
};

// The brackets seen while standing at a stop, one frame at a time. The
// game moves them between Pokemon as they walk about, so the watch goes
// on `settle` after the first sighting, to see the others, and ends at
// `limit` when there is none.
class BracketWatch{
public:
BracketWatch(std::chrono::milliseconds settle, std::chrono::milliseconds limit);

// What a frame `elapsed` after the start showed: nothing when no
// brackets.
void add(std::chrono::milliseconds elapsed, const std::optional<BracketSighting>& sighting);

// Whether to stop watching.
bool done() const;

// The tallest brackets seen: the nearest Pokemon, the likeliest to be
// caught (the user). Nothing when none showed.
std::optional<BracketSighting> nearest() const;

const std::vector<BracketSighting>& sightings() const{
return m_sightings;
}

private:
std::chrono::milliseconds m_settle;
std::chrono::milliseconds m_limit;
std::chrono::milliseconds m_last{0};
std::optional<std::chrono::milliseconds> m_first_seen;
std::vector<BracketSighting> m_sightings;
};

// How long a catch stop watches on after the first brackets: none, so
// the lock goes on while the Pokemon is there. Watching 1.5 s more for
// nearer ones gained nothing: every look on the console saw one Pokemon,
// its brackets pulsing between 0.08 and 0.10 tall, and Pokemon walked off
// while it waited (17:2x to 17:4x).
inline constexpr std::chrono::milliseconds BRACKET_SETTLE{0};

// A turn of the camera with the right stick: `stick_x` (right when
// positive) held for `time`.
struct CameraTurn{
double stick_x = 0;
std::chrono::milliseconds time{0};
};

// The turn that brings what is at `x` (0 to 1 across the screen) near the
// middle: none within 0.06 of it, else 0.4 toward it for 80 ms plus up to
// 400 ms (a quarter of the screen off or more), as the walk turns to the
// objective marker.
CameraTurn camera_turn_toward(double x);

// One look of a catch stop's camera sweep: the turn before it, and whether
// to watch for brackets after it.
struct SweepLook{
CameraTurn turn;
bool watch = true;
};

// A catch stop's sweep from facing along the road: a look straight on,
// then `steps` turns of `step` toward the grass right of the road with a
// look after each, then `steps` turns back with no look, so the camera
// ends where it started. From mid-road, the user's catches were in that
// grass (Mareep) or on the road (Scatterbug), the camera turned right
//; the patrol facing along the road saw none.
std::vector<SweepLook> camera_sweep(CameraTurn step, int steps);



} // namespace PokemonLZA
} // namespace NintendoSwitch
} // namespace PokemonAutomation
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* PokemonLZA AutoStory - Catch plan
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <algorithm>
#include <cmath>
#include "PokemonLZA_AutoStoryCatchPlan.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonLZA{


std::string_view LockTarget::caught_text() const{
if (!caught){
return "caught mark unread";
}
return *caught ? "caught" : "not caught";
}

bool lock_target_far(const LockTarget& target){
return target.y < FAR_PLATE_Y;
}

bool same_lock_target(const LockTarget& a, const LockTarget& b){
return std::abs(a.x - b.x) <= 0.03 && std::abs(a.y - b.y) <= 0.03;
}

std::optional<size_t> nearest_uncaught(const std::vector<LockTarget>& targets){
std::optional<size_t> best;
for (size_t i = 0; i < targets.size(); i++){
if (targets[i].caught == true){
continue;
}
if (!best || targets[i].y > targets[*best].y){
best = i;
}
}
return best;
}

std::optional<NewSpeciesSign> ThrowSeen::new_species() const{
if (registered){
return NewSpeciesSign::BANNER;
}
if (plate_caught == false && target_gone){
return NewSpeciesSign::PLATE_NOT_CAUGHT;
}
return std::nullopt;
}

std::string_view ThrowSeen::new_species_text() const{
const std::optional<NewSpeciesSign> sign = new_species();
if (!sign){
return "no new species";
}
if (*sign == NewSpeciesSign::BANNER){
return "a new species: banner";
}
return "a new species: plate was not caught, target gone";
}


LockTargetCycle::LockTargetCycle(size_t max_views)
: m_max_views(max_views)
{}

bool LockTargetCycle::add(const LockTarget& target){
if (m_done){
return false;
}
if (!m_targets.empty() && same_lock_target(target, m_targets.back())){
// The flick found nothing further that way.
m_done = true;
return false;
}
if (m_targets.size() > 1 && same_lock_target(target, m_targets.front())){
// Round to the first again.
m_current = 0;
m_done = true;
return false;
}
m_targets.push_back(target);
m_current = m_targets.size() - 1;
m_done = m_targets.size() >= m_max_views;
return !m_done;
}

int LockTargetCycle::flicks_to(size_t index) const{
return (int)index - (int)m_current;
}



} // namespace PokemonLZA
} // namespace NintendoSwitch
} // namespace PokemonAutomation
Loading
Loading