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 @@ -15,6 +15,7 @@
#include "Pokemon_StoryFailureClasses_Tests.h"
#include "Pokemon_StoryLog_Tests.h"
#include "Types/Pokemon_StoryActionArg_Tests.h"
#include "Types/Pokemon_StoryRunTypes_Tests.h"

namespace PokemonAutomation{
namespace Pokemon{
Expand All @@ -25,6 +26,7 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){
add_tests_StoryActionArg(database);
add_tests_StoryLog(database);
add_tests_StoryFailureClasses(database);
add_tests_StoryRunTypes(database);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* Story Checkpoint Types
*
* From: https://github.com/PokemonAutomation/
*
*/

#include "Pokemon_StoryCheckpointTypes.h"

namespace PokemonAutomation{
namespace Pokemon{


void apply_interrupt_pose(InterruptPose effect, Pose& pose){
switch (effect){
case InterruptPose::UNCHANGED:
return;
case InterruptPose::WITHIN_SET:
if (pose.certainty != PoseCertainty::UNKNOWN){
pose.certainty = PoseCertainty::SET;
}
return;
case InterruptPose::LOST:
pose.where.reset();
pose.certainty = PoseCertainty::UNKNOWN;
return;
}
}



} // namespace Pokemon
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* Story Checkpoint Types

Check warning on line 1 in SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryCheckpointTypes.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<header>: header_symbols=7 (advisory above 6, limit 8)

Check warning on line 1 in SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryCheckpointTypes.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 as the event runner sees it: its events, the interrupts
* that can stop one part-way, its start and end checks, and the report of
* a run of it.
*
*/

#ifndef PokemonAutomation_Pokemon_StoryCheckpointTypes_H
#define PokemonAutomation_Pokemon_StoryCheckpointTypes_H

#include <cstddef>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <string_view>
#include <vector>
#include "Common/Cpp/MoveOnlyFunction.h"
#include "Pokemon_StoryLog.h"
#include "Pokemon_StoryEvent.h"

namespace PokemonAutomation{
namespace Pokemon{


// Pose confidence adjustment following an event interruption.
enum class InterruptPose{
UNCHANGED, // Cleared where we stood (a talk-only trainer).
WITHIN_SET, // Moved an unknown part of a leg: still within the position we had, no longer exactly where.
LOST, // Anywhere.
};
// Policy defining how an interrupted event should be resumed.
enum class ResumePolicy{
RETRY_EVENT, // Run the event again, from its skip_if.
RECHECK_EXPECT, // The action may have finished: done if its expects hold, else retry.
STOP, // Stop the checkpoint.
};

// Something that can stop an action part-way.
struct Interrupt{
std::string name;
Check detect;
Act handle;
InterruptPose pose = InterruptPose::UNCHANGED;
ResumePolicy resume = ResumePolicy::RETRY_EVENT;
};
// Writes what an interrupt did (`effect`) into `pose`.
void apply_interrupt_pose(InterruptPose effect, Pose& pose);


// The stretch of story between two saves, as events.
struct Checkpoint{
std::string id;
// Must hold before the first event (the save it starts from).
std::vector<Condition> start;
std::vector<Interrupt> interrupts;
std::vector<Event> events;
// Must hold after the last event.
std::vector<Condition> end;
// Saves the game after the end check, if set.
Act save;
// More interrupts than this in one event (wild battles, trainers) is
// a loop, not bad luck: the event fails as "too_many_interrupts".
size_t max_interrupts_per_event = 5;
};


// Result report from executing a checkpoint.
struct CheckpointReport{
bool completed = false;
// 1-based number of the event that failed; 0 for the start or end check
// or the save, or when nothing failed.
size_t failed_event = 0;
std::string failed_name;
Outcome failure;
size_t events_run = 0;
size_t events_skipped = 0;
};

// Where the runner writes its log lines.
using EventLogger = MoveOnlyFunction<void(const std::string& line)>;



} // namespace Pokemon
} // namespace PokemonAutomation
#endif
41 changes: 41 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* Story Event
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#include <utility>
#include "Pokemon_StoryEvent.h"

namespace PokemonAutomation{
namespace Pokemon{


Condition condition(std::string name, Check check){
return Condition{std::move(name), std::move(check)};
}

Condition pose_known(std::string map, PoseCertainty need){
std::string name = std::format("pose {}{}", certainty_name(need), map.empty() ? "" : std::format(" on {}", map));
return condition(std::move(name), [map = std::move(map), need](RunState& state){
return (map.empty() || state.pose.map == map) && certainty_covers(state.pose.certainty, need);
});
}

std::string recovery_name(RecoveryKind kind){
switch (kind){
case RecoveryKind::WAIT:
return "wait";
case RecoveryKind::REANCHOR:
return "re-anchor";
case RecoveryKind::STOP:
return "stop";
}
return "?";
}



} // namespace Pokemon
} // namespace PokemonAutomation
92 changes: 92 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* Story Event

Check warning on line 1 in SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvent.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<header>: header_symbols=8 (advisory above 6, limit 8)

Check warning on line 1 in SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvent.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<header>: header_symbols=8 (advisory above 6, limit 8)
*
* From: https://github.com/PokemonAutomation/
*
* One step of a checkpoint as the event runner sees it: what it checks
* before and after its action, when it is already done, and how a failure
* of it is answered.
*
*/

#ifndef PokemonAutomation_Pokemon_StoryEvent_H
#define PokemonAutomation_Pokemon_StoryEvent_H

#include <cstddef>
#include <functional>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <string_view>
#include <vector>
#include "Common/Cpp/MoveOnlyFunction.h"
#include "Pokemon_StoryLog.h"
#include "Pokemon_StoryOutcome.h"

namespace PokemonAutomation{
namespace Pokemon{


// A question about the run state.
using Check = MoveOnlyFunction<bool(RunState& state)>;

// A question the screen (or the run state) answers, named for the log.
struct Condition{
std::string name;
Check check;
};
// `check`, named `name` in the log.
Condition condition(std::string name, Check check);

// Holds while the pose is on `map` (any map if empty) and at least as
// certain as `need`.
Condition pose_known(std::string map, PoseCertainty need);


// How a failed event is answered.
enum class RecoveryKind{
WAIT, // Something transient (a wanderer, a slow scene): wait, try again.
REANCHOR, // The pose is in doubt: confirm it on a landmark, try again.
STOP, // Nothing to do: stop the checkpoint.
};
// "wait", "re-anchor", "stop": for log lines.
std::string recovery_name(RecoveryKind kind);

// Answers a failure named `on` (any failure if empty): runs `prepare`, if
// any, and tries the event again, up to `attempts` times.
struct Recovery{
std::string on;
RecoveryKind kind = RecoveryKind::STOP;
Act prepare;
size_t attempts = 1;
std::string why;
};


// One step of a checkpoint: an action with the conditions around it.
struct Event{
std::string name;
std::string why;
// Skipped when all of these hold (and there is at least one).
std::vector<Condition> skip_if;
// Run when the event is skipped: what the event leaves in the run
// state that is already so (a door it opens, a flag it sets).
std::function<void(RunState&)> when_skipped;
// Must hold before `act`; a failure is "require_failed".
std::vector<Condition> require;
Act act;
// Must hold after `act`; a failure is "expect_failed".
std::vector<Condition> expect;
// Written to the run's pose once the event succeeds. An action that
// learns its pose as it goes (the row that answered) returns it in
// its Outcome instead.
std::optional<Pose> pose_learned;
// In order of escalation.
std::vector<Recovery> recoveries;
};



} // namespace Pokemon
} // namespace PokemonAutomation
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* Declared Pose
*
* From: https://github.com/PokemonAutomation/
*
* A pose a checkpoint declares in its data (its start, a step's
* declared_after), as plain data. A tile game (FRLG, BDSP) declares a
* map, tiles and a facing; a game with no tile grid (PLZA) declares a
* place, a spot in it and a heading. Each game reads only its own kind
* (StoryPoseTraits::Declared); the lint fails a checkpoint that declares
* the other.
*
*/

#ifndef PokemonAutomation_Pokemon_DeclaredPose_H
#define PokemonAutomation_Pokemon_DeclaredPose_H

#include <optional>
#include <string>
#include <variant>
#include <vector>
#include "Pokemon/Navigation/Pokemon_Grid.h"

namespace PokemonAutomation{
namespace Pokemon{


// On one of `tiles` of `map` (any tile when none), facing `facing` if set.
struct TileDeclared{
std::string map;
std::vector<TilePos> tiles;
std::optional<Direction> facing;

bool operator==(const TileDeclared& other) const = default;
};

// In `place` ("LumioseCity"), at `spot` in it if set, heading `heading`
// degrees if set.
struct ZoneDeclared{
std::string place;
std::optional<std::string> spot;
std::optional<int> heading;

bool operator==(const ZoneDeclared& other) const = default;
};

// Either kind; a game reads only its own (StoryPoseTraits::Declared).
using DeclaredPose = std::variant<TileDeclared, ZoneDeclared>;



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