Skip to content
Merged
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
22 changes: 22 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*
*/

#include <algorithm>
#include <chrono>
#include <format>
#include "CommonTools/ScreenEvents/ScreenFades.h"
#include "Pokemon_SceneJudges.h"
Expand Down Expand Up @@ -75,6 +77,26 @@ bool TalkOver::warped() const{
return !m_rule.warp || m_fade != WarpFade::NONE || m_banner;
}

std::optional<Milliseconds> TalkOver::left(WallClock now) const{
if (!m_rule.end_on_quiet){
return std::nullopt;
}
std::optional<Milliseconds> out;
const auto take = [&](const std::optional<WallClock>& since, Milliseconds need){
if (!since.has_value()){
return;
}
const Milliseconds held = std::chrono::duration_cast<Milliseconds>(now - *since);
const Milliseconds rest = std::max(need - held, Milliseconds(0));
if (!out.has_value() || rest < *out){
out = rest;
}
};
take(m_quiet_since, m_rule.quiet);
take(m_calm_since, m_rule.calm);
return out;
}

std::string TalkOver::describe() const{
if (m_fade == WarpFade::NONE && !m_banner){
return "no fade or banner";
Expand Down
4 changes: 4 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ class TalkOver{

// Whether the warp the rule asks for has shown (true without one).
bool warped() const;
// How much longer the quiet or calm the last look showed must hold to
// end it, as of `now`; none when the screen is neither, or no quiet
// ends it (TalkOverRule::end_on_quiet).
std::optional<Milliseconds> left(WallClock now) const;
// What it has seen, for the log: "black fade, banner read".
std::string describe() const;

Expand Down
19 changes: 18 additions & 1 deletion SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*
*/

#include <algorithm>
#include <chrono>
#include <format>
#include <optional>
Expand Down Expand Up @@ -55,14 +56,26 @@
return std::format("{}: not back on a quiet overworld within {} s.", label, seconds);
}

// How long the next look watches, for a rule that keeps watching: until
// the quiet (or calm) the judge saw at `now` has held long enough, at most
// a whole look. None: a plain look().
std::optional<Milliseconds> next_span(const SettleRule& rule, const TalkOver& judge, WallClock now){
if (!rule.keep_watching){
return std::nullopt;
}
return std::min(rule.look_every, judge.left(now).value_or(rule.look_every));
}

} // namespace


SettleEnd run_settle(const SettleRule& rule, std::string_view label, SettleIO& io){

Check warning on line 72 in SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::Pokemon::run_settle( const SettleRule & rule , std :: string_view label , SettleIO & io): function_lines=47 (advisory above 25, limit 50)
TalkOver judge(rule.judge);
std::optional<WallClock> deadline;
// For a rule that keeps watching: how long the next look watches.
std::optional<Milliseconds> span;
while (true){
const SettleLook look = io.look();
const SettleLook look = span.has_value() ? io.watch(*span) : io.look();
if (!deadline.has_value()){
deadline = look.when + rule.cap;
}
Expand All @@ -75,6 +88,7 @@
judge.look(look.events, *look.since);
}
const SceneMove move = judge.look(look.events, look.when);
span = next_span(rule, judge, look.when);
switch (move){
case SceneMove::DONE:
io.log(std::format("[Settle] {}: over; {}.", label, judge.describe()));
Expand All @@ -91,6 +105,9 @@
continue;
case SceneMove::NEXT:
case SceneMove::B:
if (rule.keep_watching){
continue;
}
io.press(move, rule.look_every);
continue;
}
Expand Down
14 changes: 14 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ struct SettleRule{
// other unsettled screen, until the overworld is quiet after it. No
// flee_battle(), no press; its look() is the wait.
bool watch_battles = false;
// For a game that only watches: once the screen is quiet (or calm),
// the next look watches only until it has held long enough
// (SettleIO::watch()), so the settle ends when the quiet does, timed
// from the change that began it, not at the end of the next whole
// look. It never presses: its looks are the wait. So a game that
// presses through text (BDSP) must not turn it on as it is: it would
// sit in the talk until `cap`.
bool keep_watching = false;
};

// The rule of `kind`.
Expand All @@ -74,6 +82,12 @@ class SettleIO{

// The screen now.
virtual SettleLook look() = 0;
// For a rule that keeps watching: the screen after watching it for
// `span`, with since when it has shown that (SettleLook::since). A
// game whose looks take a set time keeps this default: look().
virtual SettleLook watch(Milliseconds){
return look();
}
// Answers a look (SceneMove::NEXT: the talk's button; SceneMove::B:
// B), then waits `wait`. A game that only watches waits.
virtual void press(SceneMove move, Milliseconds wait) = 0;
Expand Down
169 changes: 169 additions & 0 deletions SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle_Tests.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Settle Tests

Check warning on line 1 in SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<file>: file_lines=579 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
Expand Down Expand Up @@ -389,6 +389,172 @@
},
};



// The screen from a time on, in ms from the settle's start.
struct ScreenFrom{
Milliseconds from;
ScreenEvents events;
};

// The screen a watching game sees, as a timeline: a look watches the
// span it is asked for (look(): the rule's look_every) and ends on what
// the timeline shows then, since the change that began it.
class TimelineSettleIO : public SettleIO{

Check warning on line 403 in SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Settle_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::Pokemon::TimelineSettleIO: public_methods=6 (advisory above 5, limit 7)
public:
TimelineSettleIO(std::vector<ScreenFrom> timeline, Milliseconds look_every)
: m_timeline(std::move(timeline))
, m_look_every(look_every)
{}

virtual SettleLook look() override{
return watch(m_look_every);
}
virtual SettleLook watch(Milliseconds span) override{
m_seen.now += span;
m_seen.looks++;
const ScreenFrom* showing = &m_timeline.front();
for (const ScreenFrom& item : m_timeline){
if (item.from <= m_seen.now){
showing = &item;
}
}
return {.events = showing->events, .when = WallClock() + m_seen.now, .since = WallClock() + showing->from};
}
virtual void press(SceneMove, Milliseconds) override{
m_seen.presses++;
}
virtual bool flee_battle() override{
return true;
}
virtual void log(std::string_view) override{}

// What the settle did: when its last look ended (from its start), its
// looks and its press() calls.
struct Seen{
Milliseconds now = Milliseconds(0);
size_t looks = 0;
size_t presses = 0;
};
const Seen& seen() const{
return m_seen;
}

private:
const std::vector<ScreenFrom> m_timeline;
const Milliseconds m_look_every;
Seen m_seen;
};

// run_settle() for a game that watches a timeline: when it ends.
struct TimingTestCase{
struct Input{
SettleRule rule;
std::vector<ScreenFrom> timeline;
};

struct Expectation{
SettleEnd::Result result;
// When it ended, from its start.
Milliseconds ends_at;
// The looks it took.
size_t looks;
// The press() calls: a rule that keeps watching makes none.
size_t presses = 0;
};

std::string name;
Input input;
Expectation expectation;
};

class Test_Timing : public UnitTest{
public:
explicit Test_Timing(TimingTestCase test_case)
: UnitTest(std::format("Pokemon::Settle - timing: {}", test_case.name))
, m_case(std::move(test_case))
{}

virtual UnitTestResult run(Logger&, CancellableScope&) const override{
TimelineSettleIO io(m_case.input.timeline, m_case.input.rule.look_every);
const SettleEnd end = run_settle(m_case.input.rule, "Step", io);
if (end.result != m_case.expectation.result){
return std::format("expected {}, got {} ({})", result_name(m_case.expectation.result), result_name(end.result), end.text);
}
const TimelineSettleIO::Seen& seen = io.seen();
const UnitTestResult ends_at = expect_equal(
"ends at (ms)", std::to_string(seen.now.count()), std::to_string(m_case.expectation.ends_at.count())
);
if (ends_at.result != UnitTestResult::PASSED){
return ends_at;
}
const UnitTestResult looks = expect_equal("looks", std::to_string(seen.looks), std::to_string(m_case.expectation.looks));
if (looks.result != UnitTestResult::PASSED){
return looks;
}
return expect_equal("presses", std::to_string(seen.presses), std::to_string(m_case.expectation.presses));
}

private:
const TimingTestCase m_case;
};

// A door's settle looked at every 400 ms, as FRLG watches, that keeps
// watching or not.
SettleRule timed_door(bool keep_watching){
SettleRule rule = watched_door(true);
rule.keep_watching = keep_watching;
return rule;
}

// The overworld with the player walking: calm, never quiet.
const ScreenEvents PLAYER_WALKING{ScreenEvent::PLAYER_CAN_MOVE};

const TimingTestCase TIMING_CASES[] = {
{
// Quiet from 550 ms: 1 s of it at 1550 ms, seen at the look that
// ends at 1600 ms.
.name = "without keeping watch, it ends at a whole look's end",
.input = {.rule = timed_door(false), .timeline = {{0ms, BOX}, {550ms, QUIET}}},
.expectation = {.result = SettleEnd::Result::QUIET, .ends_at = 1600ms, .looks = 4, .presses = 3},
},
{
.name = "keeping watch, it ends 1 s after the box closed",
.input = {.rule = timed_door(true), .timeline = {{0ms, BOX}, {550ms, QUIET}}},
.expectation = {.result = SettleEnd::Result::QUIET, .ends_at = 1550ms, .looks = 4},
},
{
// Quiet from 210 ms: 1 s of it at 1210 ms, seen only at the look
// that ends at 1600 ms.
.name = "without keeping watch, a quiet due early in a look waits for its end",
.input = {.rule = timed_door(false), .timeline = {{0ms, BOX}, {210ms, QUIET}}},
.expectation = {.result = SettleEnd::Result::QUIET, .ends_at = 1600ms, .looks = 4, .presses = 3},
},
{
// Looks of 400, 400, 400, then the 10 ms left: almost a whole look
// saved.
.name = "keeping watch, a quiet due early in a look ends it then",
.input = {.rule = timed_door(true), .timeline = {{0ms, BOX}, {210ms, QUIET}}},
.expectation = {.result = SettleEnd::Result::QUIET, .ends_at = 1210ms, .looks = 4},
},
{
.name = "keeping watch, a quiet the screen had from the start ends it at 1 s",
.input = {.rule = timed_door(true), .timeline = {{0ms, QUIET}}},
.expectation = {.result = SettleEnd::Result::QUIET, .ends_at = 1000ms, .looks = 3},
},
{
// Quiet from 0, broken by a box at 700 ms; quiet again from 900 ms.
.name = "keeping watch, a quiet broken before its end starts over",
.input = {.rule = timed_door(true), .timeline = {{0ms, QUIET}, {700ms, BOX}, {900ms, QUIET}}},
.expectation = {.result = SettleEnd::Result::QUIET, .ends_at = 1900ms, .looks = 5},
},
{
.name = "keeping watch, the calm of a walking player ends it at 2.5 s",
.input = {.rule = timed_door(true), .timeline = {{0ms, PLAYER_WALKING}}},
.expectation = {.result = SettleEnd::Result::QUIET, .ends_at = 2500ms, .looks = 7},
},
};

} // namespace


Expand All @@ -402,6 +568,9 @@
for (const WatchTestCase& item : WATCH_CASES){
database.add<Test_Watch>(item);
}
for (const TimingTestCase& item : TIMING_CASES){
database.add<Test_Timing>(item);
}
}


Expand Down
Loading