Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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 @@ -238,6 +238,8 @@ pa_embed_text_files(SerialProgramsLib Source/VideoGames/PokemonFRLG/Programs/Aut
VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleData.h PokemonAutomation::NintendoSwitch::PokemonFRLG autostory_battle_json)
pa_embed_text_files(SerialProgramsLib Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData
VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.h PokemonAutomation::NintendoSwitch::PokemonFRLG autostory_checkpoint_json)
pa_embed_text_files(SerialProgramsLib Source/VideoGames/PokemonFRLG/Programs/AutoStory/ConsoleFailures
VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryConsoleFailureData.h PokemonAutomation::NintendoSwitch::PokemonFRLG console_failure_json)
pa_embed_text_files(SerialProgramsLib Source/VideoGames/PokemonFRLG/Programs/AutoStory/ReplayData
VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_StoryReplay.h PokemonAutomation::NintendoSwitch::PokemonFRLG story_replay_fixture_json)

Expand Down
133 changes: 133 additions & 0 deletions SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegReplay.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/* Leg Replay
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <stdexcept>
#include "Common/Cpp/Json/JsonArray.h"
#include "Common/Cpp/Json/JsonObject.h"
#include "Common/Cpp/Json/JsonValue.h"
#include "Pokemon_LegReplay.h"

namespace PokemonAutomation{
namespace Pokemon{

namespace{

[[noreturn]] void bad(const std::string& name, const std::string& what){
throw std::runtime_error("console failure " + name + ": " + what);

Check warning on line 19 in SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegReplay.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
}

Direction direction_of(const std::string& name, const std::string& text){
for (const Direction direction : ALL_DIRECTIONS){
if (direction_name(direction) == text){
return direction;
}
}
bad(name, "unknown direction \"" + text + "\"");

Check warning on line 28 in SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegReplay.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
}

TilePos tile_of(const std::string& name, const JsonArray& array){
if (array.size() != 2){
bad(name, "a tile is [x, y]");
}
return {(int)array[0].to_integer_throw(), (int)array[1].to_integer_throw()};
}

ReplayFade fade_of(const std::string& name, const std::string& text){
if (text == "unseen"){
return ReplayFade::UNSEEN;
}
if (text == "black"){
return ReplayFade::BLACK;
}
if (text == "dim"){
return ReplayFade::DIM;
}
if (text == "white"){
return ReplayFade::WHITE;
}
bad(name, "unknown fade \"" + text + "\"");

Check warning on line 51 in SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegReplay.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
}

ReplayFault fault_of(const std::string& name, const JsonObject& json){

Check warning on line 54 in SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegReplay.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::Pokemon::fault_of( const std :: string & name , const JsonObject & json): function_lines=28 (advisory above 25, limit 50)
const std::string kind = json.get_string_throw("kind");
ReplayFault fault;
if (kind == "fade_missed" || kind == "banner_missed"){
fault.kind = kind == "fade_missed" ? ReplayFaultKind::FADE_MISSED : ReplayFaultKind::BANNER_MISSED;
fault.index = (size_t)json.get_integer_throw("index");
}else if (kind == "moved_further" || kind == "hold_through_warp" || kind == "scene_ends_further"){
fault.kind = kind == "moved_further" ? ReplayFaultKind::MOVED_FURTHER
: kind == "hold_through_warp" ? ReplayFaultKind::HOLD_THROUGH_WARP : ReplayFaultKind::SCENE_ENDS_FURTHER;
fault.step = json.get_string_throw("step");
fault.direction = direction_of(name, json.get_string_throw("direction"));
fault.tiles = (size_t)json.get_integer_default("tiles", 1);
fault.fade = fade_of(name, json.get_string_default("fade", "unseen"));
fault.far_pose_of = json.get_string_default("far_pose_of", "");
}else if (kind == "moved_less"){
fault.kind = ReplayFaultKind::MOVED_LESS;
fault.step = json.get_string_throw("step");
fault.index = (size_t)json.get_integer_throw("index");
fault.tiles = (size_t)json.get_integer_throw("tiles");
}else if (kind == "person_stands"){
fault.kind = ReplayFaultKind::PERSON_STANDS;
fault.name = json.get_string_throw("name");
fault.tile = tile_of(name, json.get_array_throw("tile"));
}else{
bad(name, "unknown fault kind \"" + kind + "\"");

Check warning on line 78 in SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegReplay.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
}
return fault;
}

} // namespace


std::string replay_fault_kind_name(ReplayFaultKind kind){
switch (kind){
case ReplayFaultKind::FADE_MISSED:
return "fade_missed";
case ReplayFaultKind::BANNER_MISSED:
return "banner_missed";
case ReplayFaultKind::MOVED_FURTHER:
return "moved_further";
case ReplayFaultKind::HOLD_THROUGH_WARP:
return "hold_through_warp";
case ReplayFaultKind::SCENE_ENDS_FURTHER:
return "scene_ends_further";
case ReplayFaultKind::PERSON_STANDS:
return "person_stands";
case ReplayFaultKind::MOVED_LESS:
return "moved_less";
}
return "";
}

ConsoleFailure parse_console_failure(const std::string& name, const std::string& text){
const JsonValue value = parse_json(text);
const JsonObject& json = value.to_object_throw();
ConsoleFailure out;
out.name = name;
out.checkpoint = json.get_string_throw("checkpoint");
if (const JsonObject* start = json.get_object("start")){
out.start = ReplayTile{start->get_string_throw("map"), tile_of(name, start->get_array_throw("tile"))};
out.start_facing = direction_of(name, start->get_string_throw("facing"));
}
for (const auto& item : json.get_array_throw("faults")){
out.faults.push_back(fault_of(name, item.to_object_throw()));
}
const JsonObject& failed = json.get_object_throw("failed");
out.failed_step = failed.get_string_throw("step");
out.failed_error = failed.get_string_throw("error");
out.frame = json.get_string_throw("frame");
if (const JsonArray* evidence = json.get_array("evidence")){
for (const auto& line : *evidence){
out.evidence.push_back(line.to_string_throw());
}
}
return out;
}


} // namespace Pokemon
} // namespace PokemonAutomation
107 changes: 107 additions & 0 deletions SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegReplay.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/* Leg Replay

Check warning on line 1 in SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegReplay.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/
*
* A console failure as a small fault file (the files of a game's ConsoleFailures folder beside
* a game's simulation tests), made from a console log's [Leg] records and
* raw walk facts by devtools/maps/leg_replay.py. The file keeps only what
* the simulation cannot work out from its map: where the run started if
* not on its checkpoint's start, the detections it missed, the walks that
* went further than counted, and the people the map lacks. Trainers, NPCs
* and walls come from the map. Where the failure happened and the frame it
* was checked against ride along for the reader.
*
* {"checkpoint": "01.02",
* "faults": [{"kind": "hold_through_warp", "step": "past_curtis",
* "direction": "east", "fade": "unseen", "far_pose_of": "into_oreburgh"}],
* "failed": {"step": "into_oreburgh", "error": "no_banner"},
// code-health: allow header_symbols -- the fault file model and its reader are one unit, a split would scatter it

Check warning on line 18 in SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegReplay.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<header>: header_symbols=7 is within limit 8; remove its code-health allow tag
*/

#ifndef PokemonAutomation_Pokemon_LegReplay_H
#define PokemonAutomation_Pokemon_LegReplay_H

#include <cstddef>
#include <optional>
#include <string>
#include <vector>
#include "Pokemon_Direction.h"
#include "Pokemon_Grid.h"

namespace PokemonAutomation{
namespace Pokemon{


// A tile of a named map.
struct ReplayTile{
std::string map;
TilePos tile;
};

// What went differently on the console than the program counted on.
enum class ReplayFaultKind{
FADE_MISSED, // the `index`th fade of the run goes unseen
BANNER_MISSED, // the `index`th map banner of the run goes unseen
MOVED_FURTHER, // the step's counted taps went `tiles` further (onto a warp, `far_pose_of`)
HOLD_THROUGH_WARP, // the step's hold toward `direction` ran on through a warp (`far_pose_of`)
SCENE_ENDS_FURTHER, // the player stood `tiles` toward `direction` past where the scene step leaves them
PERSON_STANDS, // `name`, a person of the map (who wanders or stays put), stood on `tile`
MOVED_LESS, // the `index`th hold of `step`'s walk (from 1) moved `tiles` fewer than it was held for, with
// nothing on screen: the walker took the move as seen
};
// "fade_missed", "banner_missed", "moved_further", "hold_through_warp", "scene_ends_further", "person_stands",
// "moved_less".
std::string replay_fault_kind_name(ReplayFaultKind kind);

// How the walk saw the fade of a warp it went through.
enum class ReplayFade{
UNSEEN,
BLACK,
DIM,
WHITE,
};

// One fault; the fields of its kind (above) are the ones that count.
struct ReplayFault{
ReplayFaultKind kind = ReplayFaultKind::FADE_MISSED;
// FADE_MISSED, BANNER_MISSED: counted within the checkpoint, from 0.
// MOVED_LESS: counted within the step's walk, from 1.
size_t index = 0;
// MOVED_FURTHER, HOLD_THROUGH_WARP, SCENE_ENDS_FURTHER, MOVED_LESS.
std::string step;
Direction direction = Direction::EAST;
size_t tiles = 1;
ReplayFade fade = ReplayFade::UNSEEN;
bool banner_up = false;
// The step of the checkpoint whose pose after it is where the warp lands.
std::string far_pose_of;
// PERSON_STANDS.
std::string name;
TilePos tile;
};

// A console failure: the checkpoint it ran, its faults and what it failed at.
struct ConsoleFailure{
std::string name;
std::string checkpoint;
// Where the run starts if not on the checkpoint's start.
std::optional<ReplayTile> start;
std::optional<Direction> start_facing;
std::vector<ReplayFault> faults;
// What the console did: the step and the start of its error.
std::string failed_step;
std::string failed_error;
// The frame the failure was checked against, a file name in the lab's
// failure screenshots.
std::string frame;
// The log lines the faults were worked out from.
std::vector<std::string> evidence;
};

// Reads a fault file; throws on a missing or unknown field.
ConsoleFailure parse_console_failure(const std::string& name, const std::string& text);


} // namespace Pokemon
} // namespace PokemonAutomation
#endif
Loading
Loading