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 @@ -27,9 +27,19 @@
// the same autosave.
constexpr Milliseconds AUTOSAVE_DEBOUNCE = 5000ms;

// The step after the step named `step` in `row`, if there is one.
std::optional<std::string_view> step_after(const Pokemon::StoryCheckpoint& row, std::string_view step){
for (size_t index = 0; index + 1 < row.steps.size(); index++){
if (row.steps[index].name == step){
return row.steps[index + 1].name;
}
}
return std::nullopt;
}

// `inner` with every look noted in `sightings` as taken in step `step`.
// code-health: allow public_methods -- forwards every call of the engine's backend to the wrapped one unchanged
class SightingBackend : public PlzaBackend{

Check warning on line 42 in SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryAutosaves.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (bypass)

PokemonAutomation::NintendoSwitch::PokemonLZA::SightingBackend: public_methods=11 over limit 7, bypassed by the tag at line 41: "forwards every call of the engine's backend to the wrapped one unchanged"
public:
SightingBackend(PlzaBackend& inner, AutosaveSightings& sightings, std::string_view step)
: m_inner(inner)
Expand Down Expand Up @@ -85,7 +95,8 @@
void AutosaveSightings::restart(){
m_origin.reset();
m_last_badge.reset();
m_seen = false;
m_seen_in.reset();
m_last_seen_in.reset();
}

void AutosaveSightings::note(PlzaBackend& backend, const Pokemon::Observation<ZonePose>& look, std::string_view step){
Expand All @@ -97,21 +108,51 @@
}
const bool new_sighting = !m_last_badge || look.when - *m_last_badge >= AUTOSAVE_DEBOUNCE;
m_last_badge = look.when;
m_seen = true;
if (!m_seen_in){
m_seen_in = std::string(step);
}
m_last_seen_in = std::string(step);
if (new_sighting){
const double seconds = (double)std::chrono::duration_cast<Milliseconds>(look.when - *m_origin).count() / 1000;
backend.log(std::format("[PLZA] autosave badge at {:.1f}s in {}", seconds, step));
}
}

bool AutosaveSightings::finish(){
m_last_end_seen = m_seen;
std::optional<std::string> AutosaveSightings::finish(std::string_view last_step){
std::optional<std::string> seen_in = std::move(m_last_seen_in);
m_last_end_seen = seen_in == last_step;
restart();
return m_last_end_seen;
return seen_in;
}


std::optional<std::string> autosave_step_error(
const Pokemon::StoryCheckpoint& row, Pokemon::StoryRowSave save, std::optional<std::string_view> autosave_step
){
if (!autosave_step){
if (save == Pokemon::StoryRowSave::AUTOSAVE){
return std::format("{}: \"save\": \"autosave\" needs \"autosave_step\", the step a run log showed the badge in", row.id);
}
return std::nullopt;
}
bool found = false;
for (const Pokemon::StoryStep& step : row.steps){
found = found || step.name == *autosave_step;
}
if (!found){
return std::format("{}: autosave_step {} is not one of its steps", row.id, *autosave_step);
}
if (const std::optional<std::string_view> next = step_after(row, *autosave_step)){
return std::format(
"{}: the game autosaves in {}, before {}: the next checkpoint reloads that save, so end {} at {} and start the next one with {}",
row.id, *autosave_step, *next, row.id, *autosave_step, *next
);
}
return std::nullopt;
}


Pokemon::StoryGame<ZonePose> with_autosave_sightings(

Check warning on line 155 in SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryAutosaves.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonLZA::with_autosave_sightings( Pokemon :: StoryGame<ZonePose> game , const Pokemon :: StoryRows & rows , AutosaveSightings & sightings): function_lines=36 (advisory above 25, limit 50)
Pokemon::StoryGame<ZonePose> game, const Pokemon::StoryRows& rows, AutosaveSightings& sightings
){
for (Pokemon::StoryAction<ZonePose>& action : game.actions){
Expand All @@ -123,10 +164,20 @@
};
}
game.save = [rows, &sightings](Pokemon::StoryBackend<ZonePose>& backend, const Pokemon::StoryCheckpoint& checkpoint){
if (sightings.finish()){
const std::string_view last_step = checkpoint.steps.empty() ? std::string_view() : std::string_view(checkpoint.steps.back().name);
const std::optional<std::string> seen_in = sightings.finish(last_step);
if (sightings.last_end_seen()){
backend.log(std::format("[Save] {}: the game autosaved (seen)", checkpoint.id));
return Pokemon::outcome_ok();
}
if (seen_in){
const std::string_view next = step_after(checkpoint, *seen_in).value_or(last_step);
backend.log(std::format(
"[Save] {}: the game autosaved in {}, before {}: not saved, a reload would skip {}",
checkpoint.id, *seen_in, next, next
));
return Pokemon::outcome_not_saved("autosaved before the last step");
}
if (rows.unverified_autosaves.contains(checkpoint.id)){
return Pokemon::outcome_not_saved("save unverified");
}
Expand All @@ -138,6 +189,31 @@
return game;
}

Pokemon::StoryGame<ZonePose> reloading_only_to_start(const Pokemon::StoryGame<ZonePose>& game, const AutosaveSightings& sightings){
Pokemon::StoryGame<ZonePose> guarded = game;
if (!game.reload){
return guarded;
}
guarded.reload = [reload = game.reload, &sightings](Pokemon::StoryBackend<ZonePose>& backend){
if (const std::optional<std::string>& step = sightings.seen_in()){
return Pokemon::outcome_failed("save_moved", std::format(
"the game autosaved in {} during the failed run: a reload would load past the checkpoint's start", *step
));
}
return reload(backend);
};
return guarded;
}

void log_moved_save(PlzaBackend& backend, const AutosaveSightings& sightings, std::string_view checkpoint){
if (const std::optional<std::string>& step = sightings.seen_in()){
backend.log(std::format(
"[Save] {}: the game autosaved in {} before the run failed: the latest save is past {}'s start",
checkpoint, *step, checkpoint
));
}
}



} // namespace PokemonLZA
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@
*
* The game's own autosaves, as a run sees them: the badge (a pencil and
* "Saving...", top right) on any look of any step of a checkpoint. A
* checkpoint whose badge showed is saved for that run; a chained run
* reloads only onto such a save (run_plza_segments()).
* checkpoint whose badge showed in its last step is saved for that run; a
* chained run reloads only onto such a save (run_plza_segments()). The
* next checkpoint starts where that save reloads, so a checkpoint ends on
* its autosave: the plan names the step it shows in ("autosave_step"),
* and the plan check (autosave_step_error()) fails a checkpoint with steps
* after it.
*
*/

#ifndef PokemonAutomation_PokemonLZA_AutoStoryAutosaves_H
#define PokemonAutomation_PokemonLZA_AutoStoryAutosaves_H

#include <optional>
#include <string>
#include <string_view>
#include "Common/Cpp/Time.h"
#include "Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.h"
#include "Pokemon/AutoStory/Engine/Pokemon_StoryGame.h"
#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h"
#include "PokemonLZA_AutoStoryBackend.h"

namespace PokemonAutomation{
Expand All @@ -26,8 +32,11 @@ namespace PokemonLZA{

// The game's autosave badges (AUTOSAVE_READING) a run sees in the
// checkpoint under way. The story game made with it notes every look of
// every step, and its save counts a checkpoint saved when the badge showed
// during it: the save is verified for this run.
// every step, and its save counts a checkpoint saved when the badge last
// showed in its last step: the save is verified for this run. A badge in
// an earlier step saves the game short of the checkpoint's end, where a
// reload would skip the steps after it (console B, 2026-11-16: 03.07
// reloaded 03.06's autosave from before the run to Wild Zone 1's gate).
class AutosaveSightings{
public:
// A checkpoint starts: no badge seen in it yet.
Expand All @@ -37,29 +46,61 @@ class AutosaveSightings{
// "[PLZA] autosave badge at <t>s in <step>", t from the checkpoint's
// first look.
void note(PlzaBackend& backend, const Pokemon::Observation<Pokemon::ZonePose>& look, std::string_view step);
// The checkpoint ends: whether a badge showed during it, kept for
// The checkpoint ends with step `last_step`: the step the badge last
// showed in, if it did. Whether that is `last_step` is kept for
// last_end_seen().
bool finish();
// Whether the badge showed during the checkpoint that last ended.
std::optional<std::string> finish(std::string_view last_step);
// Whether the badge showed in the last step of the checkpoint that
// last ended.
bool last_end_seen() const{
return m_last_end_seen;
}
// The step of the checkpoint under way the badge first showed in, if
// it did: the game's latest save is then past the checkpoint's start.
const std::optional<std::string>& seen_in() const{
return m_seen_in;
}

private:
std::optional<WallClock> m_origin;
std::optional<WallClock> m_last_badge;
bool m_seen = false;
std::optional<std::string> m_seen_in;
std::optional<std::string> m_last_seen_in;
bool m_last_end_seen = false;
};

// Why `row` cannot end on the game's autosave in its step `autosave_step`
// (the plan's "autosave_step"; `save` is the plan's "save"), none when it
// can: the step must be one of its steps and its last, and an "autosave"
// row must name it. Checked when the plan loads, so a checkpoint that
// would start the next one mid-way fails there, not on the console.
std::optional<std::string> autosave_step_error(
const Pokemon::StoryCheckpoint& row, Pokemon::StoryRowSave save, std::optional<std::string_view> autosave_step
);

// `game` with every action's looks noted in `sightings`, which must
// outlive it, and its save counting the autosave `sightings` saw in the
// checkpoint ("[Save] <id>: the game autosaved (seen)"); else nothing
// saved (Pokemon::NOT_SAVED), worded by the plan's "save" in `rows`.
// checkpoint's last step ("[Save] <id>: the game autosaved (seen)"); one
// seen only before it saves nothing ("[Save] <id>: the game autosaved in
// <step>, before <next>"); else nothing saved (Pokemon::NOT_SAVED),
// worded by the plan's "save" in `rows`.
Pokemon::StoryGame<Pokemon::ZonePose> with_autosave_sightings(
Pokemon::StoryGame<Pokemon::ZonePose> game, const Pokemon::StoryRows& rows, AutosaveSightings& sightings
);

// `game` whose reload fails as "save_moved" while `sightings` has seen an
// autosave in the checkpoint under way: the game's latest save is then
// past that checkpoint's start, and a retry from it would start elsewhere
// (console B 2026-11-16: 03.04 autosaved in pokedex_app, failed after it,
// and the next 03.04 walked from outside the Center).
Pokemon::StoryGame<Pokemon::ZonePose> reloading_only_to_start(
const Pokemon::StoryGame<Pokemon::ZonePose>& game, const AutosaveSightings& sightings
);

// After `checkpoint` failed: logs "[Save] <checkpoint>: the game autosaved
// in <step> before the run failed" when `sightings` saw an autosave in it.
void log_moved_save(PlzaBackend& backend, const AutosaveSightings& sightings, std::string_view checkpoint);



} // namespace PokemonLZA
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* PokemonLZA AutoStory - Story game

Check warning on line 1 in SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<file>: file_lines=1000 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
Expand Down Expand Up @@ -893,7 +893,7 @@
case StartCheck::UNCHECKED:
break;
}
backend.log(std::format("[Checkpoint] {}: off the station platform, as at its start", row.id));
backend.log(std::format("[Checkpoint] {}: off the station platform; START UNCHECKED: nothing reads this row's own start", row.id));
return Pokemon::outcome_ok();
}

Expand Down Expand Up @@ -946,12 +946,13 @@

} // namespace

Pokemon::SegmentReport run_plza_segments(

Check warning on line 949 in SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonLZA::run_plza_segments( const Pokemon :: StoryGame<ZonePose> & game , const Pokemon :: StoryRows & story , std :: span<const Pokemon::StoryCheckpoint> rows , size_t attempts_per_checkpoint , PlzaBackend & backend , Pokemon :: RunState & state , AutosaveSightings & sightings): function_lines=48 (advisory above 25, limit 50)
const Pokemon::StoryGame<ZonePose>& game, const Pokemon::StoryRows& story,
std::span<const Pokemon::StoryCheckpoint> rows, size_t attempts_per_checkpoint,
PlzaBackend& backend, Pokemon::RunState& state, AutosaveSightings& sightings
){
Pokemon::StoryEngine<ZonePose> engine(game, backend);
const Pokemon::StoryGame<ZonePose> retry_game = reloading_only_to_start(game, sightings);
Pokemon::StoryEngine<ZonePose> engine(retry_game, backend);
Pokemon::SegmentReport total;
for (size_t index = 0; index < rows.size(); index++){
const Pokemon::StoryCheckpoint& row = rows[index];
Expand Down Expand Up @@ -986,15 +987,14 @@
);
add_segment(part, total);
if (!part.completed){
log_moved_save(backend, sightings, row.id);
return total;
}
}
total.completed = true;
return total;
}



} // namespace PokemonLZA
} // namespace NintendoSwitch
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ Pokemon::Outcome load_checkpoint_start(
// the row's start is loaded if the row before's autosave was seen.
// With no autosave seen, a reload would rewind to an older save: the run
// fails there as "no_start_save". A failed load fails the run too.
// A row that saw an autosave and then failed is not retried: the save
// moved past its start, so its reload fails as "save_moved", and the run
// logs "[Save] <id>: the game autosaved in <step> before the run failed".
Pokemon::SegmentReport run_plza_segments(
const Pokemon::StoryGame<Pokemon::ZonePose>& game, const Pokemon::StoryRows& story,
std::span<const Pokemon::StoryCheckpoint> rows, size_t attempts_per_checkpoint,
Expand Down
Loading
Loading