Skip to content
Merged
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
@@ -1,10 +1,11 @@
/* BDSP AutoStory Walk Tests

Check warning on line 1 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<file>: file_lines=509 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <cstdint>
#include <format>
#include <optional>
#include <string>
#include <vector>
Expand All @@ -18,6 +19,7 @@
#include "Utils/PokemonBDSP_AutoStory_RouteWalk.h"
#include "Utils/PokemonBDSP_AutoStory_Walk.h"
#include "Actions/PokemonBDSP_AutoStory_Talks.h"
#include "Utils/PokemonBDSP_AutoStory_Walk.h"
#include "PokemonBDSP_AutoStory_Walk_Tests.h"
#include "Utils/PokemonBDSP_AutoStory_WalkResult.h"
#include "Utils/PokemonBDSP_AutoStory_WalkScreen.h"
Expand Down Expand Up @@ -264,6 +266,86 @@



// walk_view_shift(): a step scrolls the whole view; scenery that moves on
// its own does not. Synthetic 384x216 screens of 8-pixel blocks in mixed
// colours, as grass and flowers are, with one change each.
uint32_t scenery_at(int x, int y){
const uint32_t block = (uint32_t)((x >> 3) * 73856093u) ^ (uint32_t)((y >> 3) * 19349663u);
const uint32_t hashed = block * 2654435761u;
return 0xff000000 | (hashed & 0x00ffffff);
}
ImageRGB32 scenery(int shift_x, int shift_y){
ImageRGB32 screen(384, 216);
for (int y = 0; y < 216; y++){
for (int x = 0; x < 384; x++){
screen.pixel(x, y) = scenery_at(x + shift_x + 1000, y + shift_y + 1000);
}
}
return screen;
}
// Each colour channel of the pixels in [x0, x1) x [y0, y1) times `scale`,
// or set to `fill` if given.
void paint(ImageRGB32& screen, int x0, int y0, int x1, int y1, double scale, uint32_t fill = 0){

Check warning on line 288 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: paint: output parameter 'ImageRGB32& screen' comes before an input; put inputs (by value, const&) first and non-const references last (Google "Function Parameter Ordering")
for (int y = y0; y < y1; y++){
for (int x = x0; x < x1; x++){
uint32_t p = screen.pixel(x, y);
if (fill != 0){
screen.pixel(x, y) = fill;
continue;
}
uint32_t out = 0xff000000;
for (int shift = 0; shift < 24; shift += 8){
out |= (uint32_t)(((p >> shift) & 0xff) * scale) << shift;
}
screen.pixel(x, y) = out;
}
}
}

UnitTestResult test_view_shift(Logger&){

Check warning on line 305 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonBDSP::test_view_shift( Logger &): function_lines=43 (advisory above 25, limit 50)
const ImageRGB32 before = scenery(0, 0);
struct Case{
std::string name;
ImageRGB32 after;
bool scrolled;
};
std::vector<Case> cases;
cases.push_back({"the same view", scenery(0, 0), false});
cases.push_back({"a step east: the view scrolls 16 pixels west", scenery(16, 0), true});
cases.push_back({"a step south", scenery(0, 12), true});
{
// r83, r84: a cloud's shadow over the top of the view.
ImageRGB32 shadow = scenery(0, 0);
paint(shadow, 0, 0, 384, 90, 0.6);
cases.push_back({"a cloud shadow over the top third", std::move(shadow), false});
}
{
// Jubilife's TV screens, the mine's conveyors.
ImageRGB32 tv = scenery(0, 0);
paint(tv, 20, 20, 110, 80, 0, 0xffffffff);
cases.push_back({"a TV lights up in a corner", std::move(tv), false});
}
{
// A turn changes only the player's sprite.
ImageRGB32 turned = scenery(0, 0);
paint(turned, 175, 75, 210, 150, 0, 0xffff4040);
cases.push_back({"the player turns", std::move(turned), false});
}
for (const Case& item : cases){
const ViewShift shift = walk_view_shift(before, item.after);
if (shift.scrolled() != item.scrolled){
return std::format("{}: {} (shift {}, {}; difference {:.1f} unshifted, {:.1f} shifted).",
item.name, item.scrolled ? "did not read as a scroll" : "read as a scroll",
shift.dx, shift.dy, shift.unshifted, shift.shifted);
}
}
const ViewShift east = walk_view_shift(before, scenery(16, 0));
if (east.dx != -8 || east.dy != 0){
return std::format("A 16-pixel scroll west read as ({}, {}) on the half-size copy, expected (-8, 0).", east.dx, east.dy);
}
return true;
}

// pokemon_center_on_screen(): the Center's warm floor in a room. r79 read
// Jubilife's yellow plaza as a Center and called a won battle lost.
UnitTestResult test_pokemon_center_on_screen(Logger&){
Expand Down Expand Up @@ -405,6 +487,10 @@
.input = {"a Pokemon Center is its floor in a room, not a yellow plaza", test_pokemon_center_on_screen},
.expectation = {},
},
{
.input = {"a step scrolls the view; moving scenery does not", test_view_shift},
.expectation = {},
},
{
.input = {"a wild battle on an open leg's taps, placed by its screen events", test_leg_battle_place},
.expectation = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ void dpad_tiles(VideoStream& stream, ProControllerContext& context, DpadPosition
context.wait_for_all_requests();
const VideoSnapshot after = stream.video().snapshot();
const double change = before && after ? walk_screen_change(*before.frame, *after.frame) : 100;
if (change > 30){
if (!before || !after || tap_moved(*before.frame, *after.frame, 30)){
moved++;
continue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* BDSP AutoStory - Walk

Check warning on line 1 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<file>: file_lines=825 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
Expand All @@ -23,6 +23,7 @@
#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h"
#include "PokemonBDSP/Inference/PokemonBDSP_TransitionWatchers.h"
#include "PokemonBDSP/Inference/Battles/PokemonBDSP_BattleMenuDetector.h"
#include "PokemonBDSP_AutoStory_Indoors.h"
#include "PokemonBDSP_AutoStory_Location.h"
#include "PokemonBDSP_AutoStory_LocationBannerWatcher.h"
#include "PokemonBDSP_AutoStory_Walk.h"
Expand Down Expand Up @@ -113,6 +114,70 @@
}


namespace detail{
bool ViewShift::scrolled() const{
constexpr double LEAST_DIFFERENCE = 3; // standing still in the video: 0 to 1.5
return (dx != 0 || dy != 0) && unshifted >= LEAST_DIFFERENCE && unshifted >= 2 * shifted;
}

ViewShift walk_view_shift(const ImageViewRGB32& before, const ImageViewRGB32& after){

Check warning on line 123 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonBDSP::detail::walk_view_shift( const ImageViewRGB32 & before , const ImageViewRGB32 & after): function_lines=48 (advisory above 25, limit 50)
constexpr int W = 192;
constexpr int H = 108;
// A step scrolls 2 to 10 pixels of the copy in 300 ms.
constexpr int REACH = 12;
const ImageRGB32 a = before.scale_to(W, H);
const ImageRGB32 b = after.scale_to(W, H);
// The player's box, which moves with the camera, not with the scenery.
auto in_player_box = [](int x, int y){
return x >= W * 42 / 100 && x < W * 58 / 100 && y >= H * 30 / 100 && y < H * 75 / 100;
};
auto difference = [&](int dx, int dy){
uint64_t sum = 0;
size_t count = 0;
for (int y = REACH; y < H - REACH; y++){
for (int x = REACH; x < W - REACH; x++){
if (in_player_box(x, y)){
continue;
}
const uint32_t p = a.pixel(x - dx, y - dy);
const uint32_t q = b.pixel(x, y);
for (int shift = 0; shift < 24; shift += 8){
const int c = (int)((p >> shift) & 0xff) - (int)((q >> shift) & 0xff);
sum += c < 0 ? -c : c;
}
count += 3;
}
}
return (double)sum / (double)count;
};
ViewShift best;
best.unshifted = difference(0, 0);
best.shifted = best.unshifted;
for (int dy = -REACH; dy <= REACH; dy++){
for (int dx = -REACH; dx <= REACH; dx++){
if (dx == 0 && dy == 0){
continue;
}
const double d = difference(dx, dy);
if (d < best.shifted){
best.shifted = d;
best.dx = dx;
best.dy = dy;
}
}
}
return best;
}

bool tap_moved(const ImageViewRGB32& before, const ImageViewRGB32& after, double line){
if (indoors(after)){
return walk_screen_change(before, after) >= line;
}
return walk_view_shift(before, after).scrolled();
}
} // namespace detail


namespace{

// The battle's text box, held for 250ms.
Expand Down Expand Up @@ -421,7 +486,7 @@
return {ChunkNext::LOOK, WalkStop::LIMIT};
}

ChunkVerdict look(){

Check warning on line 489 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonBDSP::WalkUntil::look(): function_lines=42 (advisory above 25, limit 50)
m_context.wait_for_all_requests();
VideoSnapshot after = snapshot_or_fail(m_stream, m_spec.label);
const double change = walk_screen_change(*m_before.frame, *after.frame);
Expand All @@ -442,7 +507,11 @@
m_before = std::move(after);
return {ChunkNext::NEXT_CHUNK, WalkStop::LIMIT};
}
const bool still = still_chunk(change);
// Taps count what scrolled the view, not scenery that moved (r84:
// cloud shadows read as steps).
const bool still = m_spec.input == WalkInput::TAPS
? !tap_moved(*m_before.frame, *after.frame, still_line(change))
: change < still_line(change);
if (!still){
// A quiet tap and then a moving one: the first moved too. What
// blocks a tap still blocks the next; the view barely changed
Expand All @@ -464,12 +533,11 @@
// NPC measured 17.2 after steps of 63 to 65 (r250a, Floaroma's old
// woman), over the fixed line of 16, so for taps "still" is also
// anything under 40% of the walk's own steps.
bool still_chunk(double change){
double still_line(double change){
m_largest = std::max(m_largest, change);
const double line = m_spec.input == WalkInput::TAPS
return m_spec.input == WalkInput::TAPS
? std::max(WALK_TAP_BLOCKED_CHANGE, 0.4 * m_largest)
: WALK_BLOCKED_CHANGE;
return change < line;
}

// What hold()'s watcher `ret` saw (see after_watchers()).
Expand Down Expand Up @@ -532,7 +600,7 @@
){
VideoSnapshot after = snapshot_or_fail(m_stream, m_spec.label);
const double change = walk_screen_change(*m_before_still.frame, *after.frame);
if (!still_chunk(change)){
if (tap_moved(*m_before_still.frame, *after.frame, still_line(change))){
m_result.moved_chunks++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,47 @@ constexpr double WALK_TAP_BLOCKED_CHANGE = 16.0;
// More than walking changes in 400ms: a new scene, such as a battle opening.
constexpr double WALK_SCENE_CHANGE = 120.0;

namespace detail{

// How the view scrolled between two frames, on 192x108 copies: the shift
// (dx, dy) of `before` that best lines up with `after`, leaving out the
// middle where the player stands, and the mean difference per colour
// channel (0 to 255) with and without it. Outside, the camera follows the
// player, so a step scrolls everything one way; a TV, a conveyor, a
// passer-by or a cloud shadow changes part of the view with no common
// scroll. In r83's and r84's Floaroma video, frames 300 ms apart while
// standing (screen changes up to 57, cloud shadows, text boxes) never read
// as scrolled; walking read a shift 2 to 6 times better than none.
struct ViewShift{
int dx = 0;
int dy = 0;
double unshifted = 0;
double shifted = 0;

// A shift lines up at least twice as well as none, and none leaves a
// real difference.
bool scrolled() const;
};

// Calculates the view scroll shift between before and after images.
ViewShift walk_view_shift(const ImageViewRGB32& before, const ImageViewRGB32& after);

// Whether the player moved between `before` and `after`, frames around a
// D-pad tap. Outside: only if the view scrolled. Inside, the camera can
// stay put while the player crosses a room (the house's 1F), so there a
// screen change of at least `line` counts, as it did before.
bool tap_moved(const ImageViewRGB32& before, const ImageViewRGB32& after, double line);

} // namespace detail

// View scroll shift calculation.
using detail::ViewShift;
// Calculates the view scroll shift between before and after images.
using detail::walk_view_shift;
// Whether the player moved between before and after frames.
using detail::tap_moved;


// Whether two frames show different scenes: a change walking cannot make
// (WALK_SCENE_CHANGE). UNKNOWN without both frames.
Pokemon::SceneChange walk_scenes_differ(const Pokemon::TransitionSample& before, const Pokemon::TransitionSample& after);
Expand Down
Loading