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
@@ -0,0 +1,46 @@
/* Story Engine Report
*
* From: https://github.com/PokemonAutomation/
*
* What an engine run of one checkpoint reports.
*
*/

#ifndef PokemonAutomation_Pokemon_StoryEnginePolicy_H
#define PokemonAutomation_Pokemon_StoryEnginePolicy_H

#include <optional>
#include "Pokemon/AutoStory/Pokemon_StoryCheckpointTypes.h"
#include "Pokemon/AutoStory/Pokemon_StoryPostconditionCheck.h"

namespace PokemonAutomation{
namespace Pokemon{


// How a checkpoint ended: passed (and its postconditions, where checked,
// did not fail) or not.
enum class CheckpointVerdict{
PASSED,
FAILED,
};

// An engine run of one checkpoint.
struct StoryEngineReport{
// The steps' run. A checkpoint that failed its lint has none.
CheckpointReport run = {};
// PASSED when the steps completed and no postcondition failed.
CheckpointVerdict verdict = CheckpointVerdict::FAILED;
// Checked after steps that completed.
std::optional<PostconditionReport> postconditions = std::nullopt;
// The game saved at the checkpoint's end.
bool saved = false;
// A SAVE step among its steps saved: a reload now returns there, not
// to the save the checkpoint started from.
bool saved_part_way = false;
};



} // namespace Pokemon
} // namespace PokemonAutomation
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "Pokemon_StoryEvents_Tests.h"
#include "Pokemon_StoryFailureClasses_Tests.h"
#include "Pokemon_StoryLog_Tests.h"
#include "Pokemon_StorySavePolicy_Tests.h"
#include "Pokemon_StoryStepExecutor_Tests.h"
#include "Types/Pokemon_StoryActionArg_Tests.h"
#include "Types/Pokemon_StoryRunTypes_Tests.h"
Expand All @@ -38,6 +39,7 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){
add_tests_StoryStepJson(database);
add_tests_AuthoredCheckpointJson(database);
add_tests_StoryEngine(database);
add_tests_StorySavePolicy(database);
add_tests_SceneJudges(database);
add_tests_StoryFiles(database);
add_tests_StoryRunTypes(database);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
/* Story Postcondition Check
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <algorithm>
#include <format>
#include <utility>
#include <variant>
#include "Pokemon_StoryPostconditionCheck.h"
#include "Pokemon_StoryStepExecutor.h"

namespace PokemonAutomation{
namespace Pokemon{


namespace{

std::string verdict_name(PostconditionVerdict verdict){
switch (verdict){
case PostconditionVerdict::PASSED:
return "passed";
case PostconditionVerdict::FAILED:
return "failed";
case PostconditionVerdict::NOT_CHECKED:
return "not checked";
}
return "?";
}

PostconditionResult result(std::string what, PostconditionVerdict verdict, std::string detail){
return PostconditionResult{.what = std::move(what), .verdict = verdict, .detail = std::move(detail)};
}

PostconditionResult not_read(std::string what){
return result(std::move(what), PostconditionVerdict::NOT_CHECKED, "the game can't read it");
}

// The first pose among a step's args (a route's target).
std::optional<GridPose> pose_arg(const StoryStep& step){
for (const auto& [unused, value] : step.args){
if (const GridPose* pose = std::get_if<GridPose>(&value)){
return *pose;
}
}
return std::nullopt;
}

std::string tiles_text(const std::vector<TilePos>& tiles){
std::string text;
for (const TilePos& tile : tiles){
text += (text.empty() ? "" : " or ") + to_string(tile);
}
return text;
}

void check_map(const GridPose& end, const ObservedState& observed, std::vector<PostconditionResult>& out){
if (!observed.map){
out.push_back(not_read("map"));
return;
}
const bool same = *observed.map == end.map;
out.push_back(result(
"map", same ? PostconditionVerdict::PASSED : PostconditionVerdict::FAILED,
same ? end.map : std::format("expected {}, read {}", end.map, *observed.map)
));
}

// The tile and facing mean nothing on another map: not checked then.
void check_tile(const GridPose& end, const ObservedState& observed, bool map_failed, std::vector<PostconditionResult>& out){
if (end.tiles.empty()){
return;
}
if (map_failed){
out.push_back(result("tile", PostconditionVerdict::NOT_CHECKED, "on another map"));
return;
}
if (!observed.tile){
out.push_back(not_read("tile"));
return;
}
const bool on = std::find(end.tiles.begin(), end.tiles.end(), *observed.tile) != end.tiles.end();
out.push_back(result(
"tile", on ? PostconditionVerdict::PASSED : PostconditionVerdict::FAILED,
on ? to_string(*observed.tile) : std::format("expected {}, read {}", tiles_text(end.tiles), to_string(*observed.tile))
));
}

void check_facing(const GridPose& end, const ObservedState& observed, bool map_failed, std::vector<PostconditionResult>& out){
if (!end.facing){
return;
}
if (map_failed){
out.push_back(result("facing", PostconditionVerdict::NOT_CHECKED, "on another map"));
return;
}
if (!observed.facing){
out.push_back(not_read("facing"));
return;
}
const bool same = *observed.facing == *end.facing;
out.push_back(result(
"facing", same ? PostconditionVerdict::PASSED : PostconditionVerdict::FAILED,
same ? direction_name(*end.facing)
: std::format("expected {}, read {}", direction_name(*end.facing), direction_name(*observed.facing))
));
}

void check_position(const StoryCheckpoint& checkpoint, const ObservedState& observed, std::vector<PostconditionResult>& out){
const std::optional<GridPose> end = checkpoint_end_pose(checkpoint);
if (!end){
return;
}
check_map(*end, observed, out);
const bool map_failed = out.back().verdict == PostconditionVerdict::FAILED;
check_tile(*end, observed, map_failed, out);
check_facing(*end, observed, map_failed, out);
}

void check_items(const StoryPostconditions& expected, const ObservedState& observed, std::vector<PostconditionResult>& out){
for (const StoryItem& item : expected.items){
const std::string what = std::format("item {}", item.item);
const auto read = observed.items.find(item.item);
if (read == observed.items.end()){
out.push_back(not_read(what));
continue;
}
const bool same = read->second == item.quantity;
out.push_back(result(
what, same ? PostconditionVerdict::PASSED : PostconditionVerdict::FAILED,
same ? std::format("{}", item.quantity) : std::format("expected {}, read {}", item.quantity, read->second)
));
}
}

void check_flags(const StoryPostconditions& expected, const ObservedState& observed, std::vector<PostconditionResult>& out){
for (const std::string& flag : expected.flags){
const std::string what = std::format("flag {}", flag);
const auto read = observed.flags.find(flag);
if (read == observed.flags.end()){
out.push_back(not_read(what));
continue;
}
out.push_back(result(
what, read->second ? PostconditionVerdict::PASSED : PostconditionVerdict::FAILED,
read->second ? "set" : "expected set, read clear"
));
}
}

std::string summary_verdict(const PostconditionReport& report){
if (report.results.empty()){
return "nothing to check";
}
if (report.count(PostconditionVerdict::FAILED) > 0){
return "failed";
}
return report.all_passed() ? "passed" : "incomplete";
}

} // namespace


size_t PostconditionReport::count(PostconditionVerdict verdict) const{
size_t total = 0;
for (const PostconditionResult& item : results){
total += item.verdict == verdict ? 1 : 0;
}
return total;
}

bool PostconditionReport::all_passed() const{
return !results.empty() && count(PostconditionVerdict::PASSED) == results.size();
}


std::optional<GridPose> checkpoint_end_pose(const StoryCheckpoint& checkpoint){
if (checkpoint.postconditions.position){
return checkpoint.postconditions.position;
}
for (auto step = checkpoint.steps.rbegin(); step != checkpoint.steps.rend(); ++step){
if (step->pose_after){
return grid_pose_of(*step->pose_after);
}
if (std::optional<GridPose> target = pose_arg(*step)){
return target;
}
// Taps and single steps move the player without saying where to.
if (step->kind == StoryStepKind::LEGS || step->kind == StoryStepKind::STEP){
return std::nullopt;
}
}
return std::nullopt;
}

PostconditionReport check_postconditions(const StoryCheckpoint& checkpoint, const ObservedState& observed){
PostconditionReport report;
report.checkpoint = checkpoint.id;
check_position(checkpoint, observed, report.results);
check_items(checkpoint.postconditions, observed, report.results);
check_flags(checkpoint.postconditions, observed, report.results);
return report;
}

std::vector<std::string> postcondition_log_lines(const PostconditionReport& report){
std::vector<std::string> lines;
for (const PostconditionResult& item : report.results){
lines.push_back(std::format(
"[Postcondition] {} {}: {}: {}", report.checkpoint, item.what, verdict_name(item.verdict), item.detail
));
}
lines.push_back(std::format(
"[Postcondition] {}: {} passed, {} failed, {} not checked: {}",
report.checkpoint,
report.count(PostconditionVerdict::PASSED),
report.count(PostconditionVerdict::FAILED),
report.count(PostconditionVerdict::NOT_CHECKED),
summary_verdict(report)
));
return lines;
}



} // namespace Pokemon
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* Story Postcondition Check

Check warning on line 1 in SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPostconditionCheck.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 checkpoint's postconditions (Types/Pokemon_StoryPostconditions.h)
* checked against what the game read once the run was over: a step of its
* own, not the run's verdict, which can be wrong. A postcondition the game
* can't read yet is reported as not checked, never as passed.
*
*/

#ifndef PokemonAutomation_Pokemon_StoryPostconditionCheck_H
#define PokemonAutomation_Pokemon_StoryPostconditionCheck_H

#include <cstddef>
#include <functional>
#include <map>
#include <optional>
#include <string>
#include <vector>
#include "Pokemon/AutoStory/Types/Pokemon_GridPose.h"
#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h"

namespace PokemonAutomation{
namespace Pokemon{


// What the game read after the run. Each part is unset (or, for items and
// flags, absent from the map) when the game can't read it: the matching
// postconditions are then not checked.
struct ObservedState{
std::optional<std::string> map;
std::optional<TilePos> tile;
std::optional<Direction> facing;
// The Bag's count of each item read, 0 for one read and not there.
std::map<std::string, size_t, std::less<>> items;
// Each story flag read, and whether it is set.
std::map<std::string, bool, std::less<>> flags;
};

// The result of one postcondition.
enum class PostconditionVerdict{
PASSED,
FAILED,
NOT_CHECKED,
};

// One postcondition checked: "map", "tile", "facing", "item LIFT_KEY",
// "flag ...", its verdict and what was expected and read.
struct PostconditionResult{
std::string what;
PostconditionVerdict verdict = PostconditionVerdict::NOT_CHECKED;
std::string detail;
};

// Every postcondition of one checkpoint, checked.
struct PostconditionReport{
std::string checkpoint;
std::vector<PostconditionResult> results;

size_t count(PostconditionVerdict verdict) const;
// Every postcondition was read and held: none failed, none unchecked,
// and there was at least one.
bool all_passed() const;
};

// Where `checkpoint` ends: its postconditions' position, else the last
// step's declared pose or target. Unset when a step after that moves the
// player in a way the plan does not place (taps of legs, a single step).
std::optional<GridPose> checkpoint_end_pose(const StoryCheckpoint& checkpoint);

// Checks the end position (map, tile among the pose's tiles, facing),
// then each item count and flag, against `observed`.
PostconditionReport check_postconditions(const StoryCheckpoint& checkpoint, const ObservedState& observed);

// The report as log lines, one per postcondition and a summary:
// [Postcondition] 00.31 map: passed: RocketHideout_B3F
// [Postcondition] 00.31 item RARE_CANDY: failed: expected 2, read 1
// [Postcondition] 00.31 flag hideout_lift_key_grunt: not checked: the game can't read it
// [Postcondition] 00.31: 3 passed, 1 failed, 1 not checked: failed
// The summary is "passed" only when every postcondition passed;
// "incomplete" when none failed and some were not checked.
std::vector<std::string> postcondition_log_lines(const PostconditionReport& report);



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