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 @@ -36,7 +36,7 @@ enum class StoryPace{

// What a backend plays on. An action declared console_only
// (StoryActionSpec) runs only on CONSOLE: it reads or presses through
// screens nothing else models (menus, a keyboard, a battle AI).
// screens nothing else models (menus, a keyboard).
enum class StoryBackendKind{
CONSOLE,
SIMULATION,
Expand Down Expand Up @@ -78,6 +78,10 @@ class StoryBackend{
virtual void wait(Milliseconds duration) = 0;
// The game now, once every input sent so far has been done.
virtual Observation<PoseType> observe() = 0;
// Fights the battle on screen with the game's battle code, until it
// ends; a battle that cannot be won or does not end is a failed
// Outcome.
virtual Outcome battle() = 0;
virtual void log(std::string_view line) = 0;
virtual CancellableScope& scope() = 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ std::string fake_text(CancellableScope& scope){
return text + looks;
}

// The scripted backend's battles: recorded, ending as scripted (ok when
// the script names no outcome).
std::string fake_battle_text(CancellableScope& scope){
FakeStoryBackend<ZonePose> won({}, scope);
FakeStoryBackend<ZonePose> lost({.battle = outcome_failed("battle_lost", "the party fainted")}, scope);
const Outcome first = won.battle();
const Outcome second = lost.battle();
return std::format(
"{} {} / {} {}", won.inputs().front(), first.succeeded() ? "ok" : first.failure,
lost.inputs().front(), second.succeeded() ? "ok" : second.failure
);
}

const BackendCase CASES[] = {
{
.name = "a look keeps the engine's events",
Expand All @@ -96,6 +109,11 @@ const BackendCase CASES[] = {
.expectation = {.text = "press A, tap east, hold north 400ms run, stick 90deg 150ms walk, release 300ms, wait 100ms, "
"nothing / white fade / white fade"},
},
{
.name = "the scripted backend records a battle and ends it as scripted",
.input = {.read = fake_battle_text},
.expectation = {.text = "battle ok / battle battle_lost"},
},
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ Observation<PoseType> FakeStoryBackend<PoseType>::observe(){
return m_script.looks[look];
}

template <StoryPose PoseType>
Outcome FakeStoryBackend<PoseType>::battle(){
m_inputs.push_back("battle");
return m_script.battle;
}

template <StoryPose PoseType>
void FakeStoryBackend<PoseType>::log(std::string_view line){
m_lines.emplace_back(line);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*
* A scripted game for engine tests, with no console and no model: each
* observe() returns the script's next look (the last one repeats), every
* input and log line is recorded. A fault is a script: a walk whose fade
* never comes.
* input and log line is recorded, and a battle ends as scripted. A fault
* is a script: a walk whose fade never comes.
*
*/

Expand All @@ -32,6 +32,8 @@ struct FakeStoryScript{
// What it says it plays on: SCRIPT, or CONSOLE to run a console_only
// action's code in a test.
StoryBackendKind kind = StoryBackendKind::SCRIPT;
// How each battle() ends.
Outcome battle = outcome_ok();
};

// The backend of the engine's unit tests and of a game's scripted tests,
Expand All @@ -46,7 +48,7 @@ class FakeStoryBackend : public StoryBackend<PoseType>{
// Each input, in order: "press A", "press +" (by button_to_string()),
// "tap north", "hold east 400ms walk", "stick 90deg 150ms walk",
// "stick 90deg 150ms walk, release 300ms" (a release only when not
// zero), "wait 100ms".
// zero), "wait 100ms", "battle".
const std::vector<std::string>& inputs() const{
return m_inputs;
}
Expand All @@ -62,6 +64,7 @@ class FakeStoryBackend : public StoryBackend<PoseType>{
virtual void stick(double degrees, Milliseconds duration, Milliseconds release, StoryPace pace) override;
virtual void wait(Milliseconds duration) override;
virtual Observation<PoseType> observe() override;
virtual Outcome battle() override;
virtual void log(std::string_view line) override;
virtual CancellableScope& scope() override;

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

#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h"
#include "CommonFramework/VideoPipeline/VideoFeed.h"
#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h"
#include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector.h"
#include "PokemonLZA_AutoStoryBattles.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonLZA{

using namespace std::chrono_literals;


void fight_story_battle(
ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context,
BattleAIOption& battle_ai, std::chrono::seconds timeout
){
TrainerBattleState battle_state(battle_ai);
StoryDialogDetector any_dialog;
const WallClock deadline = current_time() + timeout;
bool attacked = false;
while (true){
context.wait_for_all_requests();
const VideoSnapshot screen = console.video().snapshot();
const bool dialog = any_dialog.detect(screen);
if (dialog && attacked){
return;
}
if (current_time() > deadline){
OperationFailedExceptionWithScreenshot::fire(
ErrorReportMode::SEND_ERROR_REPORT,
"fight_story_battle(): no dialogue after the battle within the timeout.",
console
);
}
if (dialog || !battle_state.attempt_one_attack(env, console, context)){
if (!attacked){
pbf_press_button(context, BUTTON_A, /*hold=*/160ms, /*release=*/400ms);
}
continue;
}
attacked = true;
}
}



} // namespace PokemonLZA
} // namespace NintendoSwitch
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* PokemonLZA AutoStory - Battles
*
* From: https://github.com/PokemonAutomation/
*
* The story's battles on the console, over the LZA battle code
* (TrainerBattleState): what PlzaConsoleBackend::battle() runs.
*
*/

#ifndef PokemonAutomation_PokemonLZA_AutoStoryBattles_H
#define PokemonAutomation_PokemonLZA_AutoStoryBattles_H

#include <chrono>
#include "CommonFramework/Tools/ProgramEnvironment.h"
#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h"
#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h"
#include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonLZA{


// A story battle, from its HUD or the talk before it: attacks with
// TrainerBattleState (`battle_ai`'s moves) until a dialogue box shows (the
// speech after a win or a loss). Until the first attack lands, a dialogue
// box or a failed lock-on gets A instead: the trainer's lines and the
// tutorial cards. Story battles show no Run prompt, which upstream's
// battle loops start on. Throws OperationFailedException after `timeout`.
void fight_story_battle(
ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context,
BattleAIOption& battle_ai, std::chrono::seconds timeout
);



} // namespace PokemonLZA
} // namespace NintendoSwitch
} // namespace PokemonAutomation
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "PokemonLZA_AutoStoryControlsHintDetector.h"
#include "PokemonLZA_AutoStoryDetectors_Tests.h"
#include "PokemonLZA_AutoStoryMainMissionBannerDetector.h"
#include "PokemonLZA_AutoStoryMissionCompleteDetector.h"
#include "PokemonLZA_AutoStoryPlatformArrivalDetector.h"

namespace PokemonAutomation{
Expand All @@ -47,6 +48,7 @@ struct DetectorReads{
std::optional<bool> controls_hint = {};
std::optional<bool> advance_arrow = {};
std::optional<bool> mission_banner = {};
std::optional<bool> mission_complete = {};
};

struct DetectorField{
Expand All @@ -59,6 +61,7 @@ const DetectorField DETECTOR_FIELDS[] = {
{"controls hint", &DetectorReads::controls_hint},
{"advance arrow", &DetectorReads::advance_arrow},
{"mission banner", &DetectorReads::mission_banner},
{"mission complete", &DetectorReads::mission_complete},
};

// `reads` with every detector checked: one it leaves unset must not fire.
Expand Down Expand Up @@ -100,12 +103,13 @@ struct StoryDetectors{
ControlsHintDetector hint;
AdvanceArrowDetector arrow;
MainMissionBannerDetector banner;
MissionCompleteDetector complete;

DetectorReads read(const ImageViewRGB32& frame){
return DetectorReads{
.platform = platform.detect(frame), .autosave = autosave.detect(frame),
.controls_hint = hint.detect(frame), .advance_arrow = arrow.detect(frame),
.mission_banner = banner.detect(frame),
.mission_banner = banner.detect(frame), .mission_complete = complete.detect(frame),
};
}
};
Expand Down Expand Up @@ -209,6 +213,25 @@ ImageRGB32 title_card_frame(){
return image;
}

// The mission complete card drawn on a grey scene: the yellow bar, and
// "COMPLETE" as green strokes (three columns in four) above it.
ImageRGB32 mission_complete_frame(){
ImageRGB32 image = solid_frame(0xff606060);
fill_box(/*min_x=*/345, /*min_y=*/390, /*max_x=*/1590, /*max_y=*/585, /*color=*/0xfffad228, image);
for (size_t x = 615; x < 1300; x += 4){
fill_box(x, /*min_y=*/275, x + 3, /*max_y=*/345, /*color=*/0xffa5f07d, image);
}
return image;
}

// mission_complete_frame() with green right above the word: a green
// scene, not the card.
ImageRGB32 mission_complete_green_above_frame(){
ImageRGB32 image = mission_complete_frame();
fill_box(/*min_x=*/615, /*min_y=*/245, /*max_x=*/1300, /*max_y=*/270, /*color=*/0xffa5f07d, image);
return image;
}

constexpr uint32_t WHITE = 0xffffffff;
constexpr uint32_t BLACK = 0xff000000;
constexpr uint32_t GRAY = 0xff808080;
Expand All @@ -217,6 +240,7 @@ constexpr uint32_t BOX_WHITE = 0xfffcfcf7;
constexpr uint32_t BOX_TEXT_DARK = 0xff323648;
constexpr uint32_t HINT_SKY_BLUE = 0xff7dd2fa;
constexpr uint32_t BANNER_YELLOW = 0xfffad228;
constexpr uint32_t COMPLETE_GREEN = 0xffa5f07d;

struct SyntheticFrameCase{
std::string name;
Expand Down Expand Up @@ -327,7 +351,27 @@ const SyntheticFrameCase SYNTHETIC_FRAME_CASES[] = {
{
.name = "a frame all in the bar's yellow",
.input = {.frame = []{ return solid_frame(BANNER_YELLOW); }},
.expectation = {.reads = {.mission_banner = false}},
.expectation = {.reads = {.mission_banner = false, .mission_complete = false}},
},
{
.name = "the mission complete card",
.input = {.frame = mission_complete_frame},
.expectation = {.reads = {.mission_banner = false, .mission_complete = true}},
},
{
.name = "a frame all in the card's green",
.input = {.frame = []{ return solid_frame(COMPLETE_GREEN); }},
.expectation = {.reads = {.mission_complete = false}},
},
{
.name = "the card's word with green right above it",
.input = {.frame = mission_complete_green_above_frame},
.expectation = {.reads = {.mission_complete = false}},
},
{
.name = "the mission banner is not the complete card",
.input = {.frame = []{ return mission_banner_frame(0, BannerIcon::DRAWN); }},
.expectation = {.reads = {.mission_complete = false}},
},
};

Expand Down Expand Up @@ -370,7 +414,7 @@ DetectorReads as_named(std::string_view name){
return DetectorReads{
.platform = has("+platform"), .autosave = has("+autosave"),
.controls_hint = has("+controls_hint"), .advance_arrow = has("+advance_arrow"),
.mission_banner = has("+mission_banner"),
.mission_banner = has("+mission_banner"), .mission_complete = has("+mission_complete"),
};
}

Expand Down Expand Up @@ -437,6 +481,15 @@ std::optional<DetectorReads> arrival_past_mission_banner(std::string_view name){
return want;
}

// The end of Main Mission 01 to Main Mission 02 (01.03's end): as named,
// and the complete card on 0045 and 0046 (named "nothing"); the next
// mission's banner (0133-0139) is not the card.
std::optional<DetectorReads> mission_complete_to_next_banner(std::string_view name){
DetectorReads want = as_named(name);
want.mission_complete = name.starts_with("0045-") || name.starts_with("0046-");
return want;
}

struct FrameFolderCase{
std::string name;
struct Input{
Expand Down Expand Up @@ -478,6 +531,11 @@ const FrameFolderCase FRAME_FOLDER_CASES[] = {
.input = {.folder = "ArrivalPastMissionBanner"},
.expectation = {.frames = 221, .reads = arrival_past_mission_banner},
},
{
.name = "the mission complete card to the next mission's banner",
.input = {.folder = "MissionCompleteToNextBanner"},
.expectation = {.frames = 139, .reads = mission_complete_to_next_banner},
},
};

UnitTestResult check_frame_folder(const FrameFolderCase& test, Logger& logger){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* PokemonLZA AutoStory - Mission complete detector
*
* From: https://github.com/PokemonAutomation/
*
*/

#include "CommonFramework/ImageTypes/ImageViewRGB32.h"
#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h"
#include "PokemonLZA_AutoStoryPixelShares.h"
#include "PokemonLZA_AutoStoryMissionCompleteDetector.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonLZA{


// On 1920x1080: the word's green fills x 615-1300,
// y 271-349, none of it 20 px above or below; the bar's yellow fills
// y 390-585, read left and right of the name as for the banner.
MissionCompleteDetector::MissionCompleteDetector(Color color)
: m_color(color)
, m_word(640.0 / 1920, 285.0 / 1080, 640.0 / 1920, 55.0 / 1080)
, m_above(620.0 / 1920, 245.0 / 1080, 680.0 / 1920, 20.0 / 1080)
, m_below(620.0 / 1920, 358.0 / 1080, 680.0 / 1920, 20.0 / 1080)
, m_bar_left(430.0 / 1920, 440.0 / 1080, 390.0 / 1920, 140.0 / 1080)
, m_bar_right(1100.0 / 1920, 440.0 / 1080, 400.0 / 1920, 140.0 / 1080)
{}

void MissionCompleteDetector::make_overlays(VideoOverlaySet& items) const{
items.add(m_color, m_word);
items.add(m_color, m_above);
items.add(m_color, m_below);
items.add(m_color, m_bar_left);
items.add(m_color, m_bar_right);
}

// On the card the word is 70% green, nothing around it is green and both
// bar boxes are 100% yellow. Other screens have the word 11% green at
// most; the main mission banners, which have the same bar, 2% at most.
bool MissionCompleteDetector::detect(const ImageViewRGB32& screen){
if (complete_green_share(screen, m_word) < 0.40){
return false;
}
if (complete_green_share(screen, m_above) > 0.10 || complete_green_share(screen, m_below) > 0.10){
return false;
}
return banner_yellow_share(screen, m_bar_left) >= 0.6 && banner_yellow_share(screen, m_bar_right) >= 0.6;
}


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