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
2 changes: 2 additions & 0 deletions SerialPrograms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ add_library(SerialProgramsLib STATIC ${CORE_LIBRARY_SOURCES} ${LIBRARY_SOURCES})

# Data files built into the program (see cmake/EmbedTextFiles.cmake).
include(cmake/EmbedTextFiles.cmake)
pa_embed_text_files(SerialProgramsLib Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData
VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h PokemonAutomation::NintendoSwitch::PokemonLZA autostory_checkpoint_json)
pa_embed_text_files(SerialProgramsLib Source/VideoGames/PokemonLZA/Programs/AutoStory/ReferenceData
VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPlatformArrivalDetector.h PokemonAutomation::NintendoSwitch::PokemonLZA autostory_reference_json)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@
#include "Inference/PokemonLZA_OverworldPartySelectionDetector.h"
#include "Inference/PokemonLZA_PartyBarDetector.h"
#include "Inference/PokemonLZA_SelectionArrowDetector.h"
#include "Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.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/Actions/PokemonLZA_AutoStoryGateWalk_Tests.h"
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.h"
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox_Tests.h"
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h"
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.h"
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader_Tests.h"
#include "Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h"
#include "Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h"
#include "Programs/AutoStory/PokemonLZA_AutoStoryTable_Tests.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
Expand All @@ -40,7 +43,7 @@



void add_tests(UnitTestDatabase& database){

Check warning on line 46 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=34 (advisory above 25, limit 50)
add_tests_AlertEyeDetector(database);
add_tests_BoxCellInfoDetector(database);
add_tests_ButtonDetector(database);
Expand All @@ -57,16 +60,20 @@
add_tests_OverworldPartySelectionDetector(database);
add_tests_PartyBarDetector(database);
add_tests_SelectionArrowDetector(database);
add_tests_AutoStory_StoryTable(database);
add_tests_AutoStoryScreenEvents(database);
add_tests_AutoStoryDetectors(database);
add_tests_AutoStoryConfirmChoicesReader(database);
add_tests_AutoStoryDialogChoiceBox(database);
add_tests_AutoStoryObjectiveMarkerReader(database);
add_tests_AutoStoryZoneGateReader(database);
add_tests_AutoStoryGateWalk(database);
add_tests_AutoStoryFailures(database);
add_tests_AutoStoryCatchPlan(database);
add_tests_AutoStoryCatchStop(database);
add_tests_AutoStoryDialogs(database);
add_tests_AutoStorySceneMachine(database);
add_tests_AutoStoryEngine(database);
add_tests_AutoStoryStartLoad(database);

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
/* PokemonLZA AutoStory - Walk to a wild zone's gate Tests
*
* From: https://github.com/PokemonAutomation/
*
* walk_to_zone_gate_entrance() on the engine's scripted backend
* (FakeStoryBackend): each look carries the gate readings a console's
* zone_gate_reader() would set, and the A prompt.
*
*/

#include <algorithm>
#include <format>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "Common/Cpp/TestRunners/UnitTest.h"
#include "Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h"
#include "Pokemon/AutoStory/Engine/Pokemon_StoryFakeBackend.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h"
#include "PokemonLZA_AutoStoryGateWalk.h"
#include "PokemonLZA_AutoStoryGateWalk_Tests.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonLZA{

using namespace std::chrono_literals;
using Pokemon::Observation;
using Pokemon::ZonePose;


namespace{

using FakeBackend = Pokemon::FakeStoryBackend<ZonePose>;

// What a look shows.
struct Look{
// The gate's middle, in thousandths of the width; none: no gate.
std::optional<int> gate_x = std::nullopt;
bool prompt = false;
// The minimap's heading; none: not read.
std::optional<int> heading = std::nullopt;
};

std::vector<Observation<ZonePose>> looks_of(const std::vector<Look>& looks){
std::vector<Observation<ZonePose>> out;
const WallClock start = current_time();
for (const Look& look : looks){
Observation<ZonePose> observation;
observation.when = start + Milliseconds(250) * out.size();
if (look.gate_x){
observation.readings[std::string(ZONE_GATE_X_READING)] = *look.gate_x;
observation.readings[std::string(ZONE_GATE_WIDTH_READING)] = 150;
}
if (look.heading){
observation.readings[std::string(Pokemon::STORY_HEADING)] = *look.heading;
}
if (look.prompt){
observation.events.set(ScreenEvent::INTERACT_PROMPT, true);
}
out.push_back(std::move(observation));
}
return out;
}

std::string joined(const std::vector<std::string>& lines){
std::string text;
for (const std::string& line : lines){
text += (text.empty() ? "" : ", ") + line;
}
return text;
}

std::string slice(int degrees){
return std::format("stick {}deg 250ms walk", degrees);
}


struct GateWalkCase{
std::string name;
struct Input{
// In order; the last repeats.
std::vector<Look> looks;
Milliseconds timeout = 1000ms;
};
Input input;
struct Expectation{
// The failure; none: the walk succeeds.
std::optional<std::string> failure;
std::vector<std::string> inputs;
// In the failure's message, when there is one.
std::string message = "";
};
Expectation expectation;
};

const GateWalkCase GATE_WALK_CASES[] = {
{
.name = "at the prompt already: no walk",
.input = {.looks = {{.gate_x = 500, .prompt = true}}},
.expectation = {.failure = std::nullopt, .inputs = {}},
},
{
.name = "steers toward the gate until the prompt",
.input = {.looks = {{.gate_x = 700}, {.gate_x = 600}, {.gate_x = 505}, {.prompt = true}}},
.expectation = {.failure = std::nullopt, .inputs = {slice(12), slice(6), slice(0)}},
},
{
.name = "a gate lost up close keeps the last angle",
.input = {.looks = {{.gate_x = 250}, {}, {}, {.prompt = true}}},
.expectation = {.failure = std::nullopt, .inputs = {slice(-15), slice(-15), slice(-15)}},
},
{
.name = "no gate in the first 3 seconds fails",
.input = {.looks = {{}}, .timeout = 15000ms},
.expectation = {
.failure = "gate_not_seen", .inputs = std::vector<std::string>(12, slice(0)),
.message = "no gate on 13 looks",
},
},
{
.name = "a gate late in the first second is seen",
.input = {.looks = {{}, {}, {}, {.gate_x = 400}, {.prompt = true}}},
.expectation = {.failure = std::nullopt, .inputs = {slice(0), slice(0), slice(0), slice(-6)}},
},
{
.name = "no prompt by the timeout fails with the last gate read",
.input = {.looks = {{.gate_x = 510}}, .timeout = 1000ms},
.expectation = {
.failure = "no_prompt", .inputs = {slice(0), slice(0), slice(0), slice(0)},
.message = "gate on 5 of 5 looks, last at x = 0.510 width 0.150",
},
},
};

UnitTestResult check_gate_walk(const GateWalkCase& test, CancellableScope& scope){
FakeBackend backend(Pokemon::FakeStoryScript<ZonePose>{
.looks = looks_of(test.input.looks), .battle = Pokemon::outcome_ok(), .kind = Pokemon::StoryBackendKind::SCRIPT,
}, scope);
const Pokemon::Outcome outcome = walk_to_zone_gate_entrance(
backend, PlzaReader(), PlzaReader(), GateWalk{.heading = std::nullopt, .timeout = test.input.timeout}
);
const std::optional<std::string> failure = outcome.succeeded() ? std::nullopt : std::optional<std::string>(outcome.failure);
if (failure != test.expectation.failure){
return std::format("Expected {}, got {}.", test.expectation.failure.value_or("a pass"), to_string(outcome));
}
if (failure && to_string(outcome).find(test.expectation.message) == std::string::npos){
return std::format("Expected \"{}\" in {}.", test.expectation.message, to_string(outcome));
}
if (backend.inputs() != test.expectation.inputs){
return std::format("Expected inputs [{}], got [{}].", joined(test.expectation.inputs), joined(backend.inputs()));
}
return true;
}


// The step `name` of checkpoint `id`, alone in a checkpoint of its own;
// none when the story has no such step.
std::optional<Pokemon::StoryCheckpoint> step_of(std::string_view id, std::string_view name){
for (const Pokemon::StoryCheckpoint& row : story_checkpoints()){
if (row.id != id){
continue;
}
for (const Pokemon::StoryStep& step : row.steps){
if (step.name == name){
Pokemon::StoryCheckpoint one;
one.id = row.id;
one.title = row.title;
one.steps.push_back(step);
return one;
}
}
}
return std::nullopt;
}

// A checkpoint's to_zone_1_gate through PLZA's engine, on looks that read
// 30 degrees left of the plan's heading, then the heading, then the gate
// dead ahead, then the prompt.
struct PlanStepCase{
std::string name;
struct Input{
std::string_view checkpoint;
};
Input input;
struct Expectation{
std::vector<std::string> inputs;
};
Expectation expectation;
};

// 03.06's step faces the plan's heading by the minimap first (a turn of 30, then L
// so the camera follows), then walk the gate's way until the prompt.
const PlanStepCase PLAN_STEP_CASES[] = {
{
.name = "03.06 to_zone_1_gate faces the street, then the gate",
.input = {.checkpoint = "03.06"},
.expectation = {.inputs = {"press L", "stick 30deg 150ms walk, release 100ms", "press L", slice(0)}},
},
};

UnitTestResult check_plan_step(const PlanStepCase& test, CancellableScope& scope){

Check warning on line 204 in SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonLZA::check_plan_step( const PlanStepCase & test , CancellableScope & scope): function_lines=28 (advisory above 25, limit 50)
const std::string_view id = test.input.checkpoint;
const std::optional<Pokemon::StoryCheckpoint> row = step_of(id, "to_zone_1_gate");
if (!row){
return std::format("{} has no to_zone_1_gate step.", id);
}
const Pokemon::StoryStep& step = row->steps[0];
const auto heading = step.args.find("heading");
if (step.action != "walk_to_zone_gate_entrance" || heading == step.args.end()){
return std::format("{}: expected walk_to_zone_gate_entrance with a heading, got {}.", id, step.action);
}
const int facing = (int)std::get<int64_t>(heading->second);
FakeBackend backend(Pokemon::FakeStoryScript<ZonePose>{
.looks = looks_of({{.heading = (facing + 330) % 360}, {.heading = facing}, {.gate_x = 500}, {.prompt = true}}),
.battle = Pokemon::outcome_ok(), .kind = Pokemon::StoryBackendKind::SCRIPT,
}, scope);
const Pokemon::StoryGame<ZonePose> game = make_plza_story_game(StarterChoice::CHIKORITA);
Pokemon::StoryEngine<ZonePose> engine(game, backend);
Pokemon::RunState state;
const Pokemon::StoryEngineReport report = engine.run_checkpoint(*row, state);
if (report.verdict != Pokemon::CheckpointVerdict::PASSED){
return std::format("{}: expected a pass, got {}.", id, to_string(report.run.failure));
}
if (backend.inputs() != test.expectation.inputs){
return std::format("{}: expected inputs [{}], got [{}].", id, joined(test.expectation.inputs), joined(backend.inputs()));
}
return true;
}


class Test_GateWalk : public UnitTest{
public:
explicit Test_GateWalk(const GateWalkCase& test)
: UnitTest(std::format("PokemonLZA::AutoStory - walk_to_zone_gate_entrance: {}", test.name))
, m_test(test)
{}
virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{
return check_gate_walk(m_test, scope);
}

private:
const GateWalkCase& m_test;
};

class Test_GatePlanStep : public UnitTest{
public:
explicit Test_GatePlanStep(const PlanStepCase& test)
: UnitTest(std::format("PokemonLZA::AutoStory - walk_to_zone_gate_entrance: {}", test.name))
, m_test(test)
{}
virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{
return check_plan_step(m_test, scope);
}

private:
const PlanStepCase& m_test;
};

} // namespace


void add_tests_AutoStoryGateWalk(UnitTestDatabase& database){
for (const GateWalkCase& test : GATE_WALK_CASES){
database.add<Test_GateWalk>(test);
}
for (const PlanStepCase& test : PLAN_STEP_CASES){
database.add<Test_GatePlanStep>(test);
}
}



} // namespace PokemonLZA
} // namespace NintendoSwitch
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* PokemonLZA AutoStory - Walk to a wild zone's gate Tests
*
* From: https://github.com/PokemonAutomation/
*
*/

#ifndef PokemonAutomation_PokemonLZA_AutoStoryGateWalk_Tests_H
#define PokemonAutomation_PokemonLZA_AutoStoryGateWalk_Tests_H

#include "Common/Cpp/TestRunners/UnitTestDatabase.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonLZA{


// Registers walk_to_zone_gate_entrance()'s tests, on scripted looks.
void add_tests_AutoStoryGateWalk(UnitTestDatabase& database);



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