diff --git a/.github/workflows/cpp-ci-serial-programs-base.yml b/.github/workflows/cpp-ci-serial-programs-base.yml index e83ca269e5..673b391abb 100644 --- a/.github/workflows/cpp-ci-serial-programs-base.yml +++ b/.github/workflows/cpp-ci-serial-programs-base.yml @@ -87,7 +87,7 @@ jobs: # Upstream master plus the BDSP AutoStory option-screen templates (22 PNGs the # AutoStoryResources test requires); drop this once they are merged upstream. repository: 'dayfine/Packages' - ref: '7d108a255bfbd4923af1fb0cf49245f2c2483594' + ref: 'b4d7d42516ab68aa84bdbcae52bdc0c9c8171445' path: 'Arduino-Source/Packages' - name: Prepare pinned BDSP map resources @@ -257,7 +257,9 @@ jobs: uses: actions/checkout@v7 if: inputs.run-tests with: - repository: 'PokemonAutomation/CommandLineTests' + # The fork's fixtures: upstream's plus the SystemKeyboardDetector and HomeTileReader screenshots. + repository: 'dayfine/CommandLineTests' + ref: 'eae4697738d5fe18975e77c05a4e01216bed68e7' path: 'CommandLineTests' # The unit tests assume the downloadable resources they need are already downloaded diff --git a/Common/Cpp/HttpServer/HttpServer.cpp b/Common/Cpp/HttpServer/HttpServer.cpp new file mode 100644 index 0000000000..b4c97cbab1 --- /dev/null +++ b/Common/Cpp/HttpServer/HttpServer.cpp @@ -0,0 +1,821 @@ +/* HTTP Server + * + * From: https://github.com/PokemonAutomation/ + * + * Implementation notes: + * - The socket's receive/disconnect callbacks only append data to the + * per-connection buffer and flip flags. One "connection task" per + * connection (from the thread pool) does all parsing and writing, so + * the handler may run for a long time without stalling the socket's + * receive loop. + * - Every write goes through "AbstractServerSocket::send()" with the + * connection's socket id (captured at accept time), so a response + * that outlives its connection - the peer disconnected and the next + * client was accepted in the meantime - is refused by the socket + * instead of reaching the new client's stream. + * - "Expect: 100-continue" is answered as soon as the headers are + * validated, while the body is still pending (a waiting client is + * blocked on that interim response). + * - Task handles live in "m_tasks" under a dedicated "m_task_lock" + * (the socket library's "m_task_lock" pattern): destroying an + * "AsyncTask" handle blocks until that task finishes, and the task's + * final cleanup takes "m_lock", so a handle must never be destroyed + * while holding "m_lock". "on_connect()" may therefore find the + * previous connection's task still finishing (for example, still in a + * long handler call); its handle is kept and reaped later. + * - "stop()" marks the active connection dead before it waits for the + * in-flight task: the socket suppresses its disconnect callback for a + * close_connection() close, so without the mark a handler polling + * "connection_alive()" would never see the shutdown. "stop()" then + * moves the socket out under "m_lock" and destroys it after releasing + * the lock: "~ServerSocket" joins the socket's worker threads, and + * their callbacks take "m_lock". + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Concurrency/ThreadPool.h" +#include "Common/Cpp/HttpServer/HttpServer.h" +#include "Common/Cpp/Sockets/ServerSocket.h" + +namespace PokemonAutomation{ + +namespace{ + + +constexpr size_t MAX_REQUEST_LINE_BYTES = 8192; +constexpr size_t MAX_HEADER_SECTION_BYTES = 65536; +constexpr size_t MAX_HEADER_COUNT = 100; +constexpr size_t MAX_BODY_BYTES = 1u << 20; // 1 MiB +// The total per-connection receive buffer; enforced when data is +// appended, not at parse time. +constexpr size_t MAX_BUFFER_BYTES = 2u << 20; + +// A stalled peer is declared dead after this; loopback writes of a few MB +// finish in milliseconds, so this is very generous. +constexpr std::chrono::milliseconds WRITE_TIMEOUT{30000}; + + +bool is_token_char(char c){ + return std::isalnum((unsigned char)c) != 0 + || std::strchr("-!#$%&'*+.^_`|~", c) != nullptr; +} + + +std::string to_lower(std::string value){ + std::transform(value.begin(), value.end(), value.begin(), [](unsigned char c){ + return (char)std::tolower(c); + }); + return value; +} + + +// Strips the leading and trailing spaces and tabs (OWS). +std::string trim_ows(std::string value){ + static const char OWS[] = " \t"; + const auto begin = value.find_first_not_of(OWS); + if (begin == std::string::npos){ + return ""; + } + const auto end = value.find_last_not_of(OWS); + return value.substr(begin, end - begin + 1); +} + + +// The concrete HandlerContext: "connection_alive()" is simply the +// connection's dead flag. +struct HandlerContextImpl : AbstractHttpServer::HandlerContext{ + explicit HandlerContextImpl(const std::atomic& dead) + : m_dead(dead) + {} + + virtual bool connection_alive() const override{ + return !m_dead.load(std::memory_order_acquire); + } + +private: + const std::atomic& m_dead; +}; + + + +} // anonymous namespace + + + +const char* HttpServer::status_reason(int status){ + switch (status){ + case 100: + return "Continue"; + case 200: + return "OK"; + case 201: + return "Created"; + case 204: + return "No Content"; + case 301: + return "Moved Permanently"; + case 302: + return "Found"; + case 304: + return "Not Modified"; + case 400: + return "Bad Request"; + case 401: + return "Unauthorized"; + case 403: + return "Forbidden"; + case 404: + return "Not Found"; + case 405: + return "Method Not Allowed"; + case 408: + return "Request Timeout"; + case 411: + return "Length Required"; + case 413: + return "Payload Too Large"; + case 426: + return "Upgrade Required"; + case 500: + return "Internal Server Error"; + case 501: + return "Not Implemented"; + case 503: + return "Service Unavailable"; + case 505: + return "Version Not Supported"; + default: + return "Unknown Status"; + } +} + + +std::string HttpServer::format_response(const Response& response, bool keep_alive){ + std::string out; + out += "HTTP/1.1 "; + out += std::to_string(response.status); + out += " "; + const char* reason = response.reason.empty() ? status_reason(response.status) : response.reason.c_str(); + out += reason; + out += "\r\n"; + + for (const auto& header : response.headers){ + // The server sets both of these below. + if (to_lower(header.first) == "content-length" || + to_lower(header.first) == "connection" + ){ + continue; + } + out += header.first; + out += ": "; + out += header.second; + out += "\r\n"; + } + + out += "Content-Length: "; + out += std::to_string(response.body.size()); + out += "\r\n"; + out += "Connection: "; + out += keep_alive ? "keep-alive" : "close"; + out += "\r\n\r\n"; + out += response.body; + + return out; +} + + + +HttpServer::HttpServer(ThreadPool& thread_pool) + : m_thread_pool(thread_pool) +{} + + +HttpServer::~HttpServer(){ + stop(); +} + + +void HttpServer::start(const std::string& address, uint16_t port, Handler handler){ + AbstractServerSocket* socket = nullptr; + { + std::lock_guard lg(m_lock); + if (m_socket != nullptr){ + return; + } + m_handler = std::move(handler); + m_stop_requested.store(false, std::memory_order_release); + m_socket = std::make_unique(m_thread_pool); + // Register before start(): the accept thread is dispatched by + // start() and may report on_listen_finished() as soon as it runs. + m_socket->add_listener(*this); + socket = m_socket.get(); + } + + // Asynchronous: on_listen_finished() fires on a thread from the pool. + socket->start(address, port); +} + + +void HttpServer::stop(){ + m_stop_requested.store(true, std::memory_order_release); + + AbstractServerSocket* socket = nullptr; + { + std::lock_guard lg(m_lock); + socket = m_socket.get(); + } + if (socket == nullptr){ + return; + } + + // Mark the active connection dead: the socket suppresses its + // disconnect callback for a close_connection() close, so without + // this a handler polling connection_alive() would never see the + // shutdown, and the wait for it below would never end. + { + std::lock_guard lg(m_lock); + if (m_connection != nullptr){ + m_connection->dead.store(true, std::memory_order_release); + m_connection->cv.notify_all(); + } + } + + // Kill the active connection, if any: it cancels in-flight writes and + // makes the socket's connection thread exit. The socket keeps + // listening until it is destroyed below, so it may accept a few more + // clients in the meantime; on_connect() sees m_stop_requested and + // closes them without dispatching a connection task. + socket->close_connection(); + + // Wait for the in-flight connection tasks. The handles are moved out + // under m_task_lock (never destroyed while holding m_lock: a task's + // final cleanup takes m_lock, and destroying a handle blocks until + // the task finishes). Loop, because a task may have been dispatched + // between the previous ones being joined and the socket being + // destroyed. + while (true){ + std::vector tasks; + { + std::lock_guard lg(m_task_lock); + tasks.swap(m_tasks); + } + if (tasks.empty()){ + break; + } + for (auto& task : tasks){ + task.wait_and_ignore_exceptions(); + } + } + + // Take the socket out under m_lock and destroy it after releasing + // the lock: "~ServerSocket" joins the socket's worker threads, and + // their callbacks (on_connect and friends) take m_lock. + std::unique_ptr owned; + { + std::lock_guard lg(m_lock); + owned = std::move(m_socket); + m_connection = nullptr; + m_handler = Handler(); + } +} + + +bool HttpServer::is_running() const{ + std::lock_guard lg(m_lock); + return (m_socket != nullptr); +} + + + +void HttpServer::on_listen_finished(const std::string& error_message){ + m_listeners.run_method(&Listener::on_listen_finished, error_message); +} + + +void HttpServer::on_connect(){ + AbstractServerSocket* socket = nullptr; + std::shared_ptr conn; + { + std::lock_guard lg(m_lock); + socket = m_socket.get(); + if (socket != nullptr && + !m_stop_requested.load(std::memory_order_acquire) + ){ + conn = std::make_shared(); + // The previous socket connection is over (the socket only + // accepts a new client once the previous one is gone), but + // its task may still be finishing (for example, still inside + // a long handler call), and the socket's on_disconnect + // callback for it may not have run yet: it runs after the + // socket has already moved back to listening. Mark it dead + // now, before the new connection takes over the socket's + // active fd, so the old task discards its response instead + // of delivering it to the new client. + if (m_connection != nullptr){ + m_connection->dead.store(true, std::memory_order_release); + m_connection->cv.notify_all(); + } + m_connection = conn; + // The token of the connection this one will serve; send() + // refuses to write through a socket that has moved on. + conn->socket_id = socket->connection_id(); + } + } + if (conn == nullptr){ + // Stopping (or the socket is gone): close the connection without + // a connection task. + if (socket != nullptr){ + socket->close_connection(); + } + }else{ + // Dispatch outside m_lock: the task's final cleanup takes + // m_lock, and so does stop(). The task handles live in m_tasks + // under a dedicated m_task_lock (the socket library's + // m_task_lock pattern): destroying an AsyncTask handle blocks + // until its task finishes, so a handle must never be destroyed + // while holding m_lock. + try{ + AsyncTask task = m_thread_pool.dispatch( + [this, conn, socket]{ + connection_task(conn, socket); + } + ); + std::lock_guard lg(m_task_lock); + // Reap finished handles (destroying one that is already + // finished is a no-op). Keep unfinished ones: the + // previous connection's task may still be finishing up + // (for example, still inside a long handler call) by the + // time this connection is accepted; it holds only its + // own (dead) connection. + m_tasks.erase( + std::remove_if(m_tasks.begin(), m_tasks.end(), [](const AsyncTask& task){ + return task.is_finished(); + }), + m_tasks.end() + ); + m_tasks.push_back(std::move(task)); + }catch (...){ + // The task could not be started: drop the connection and + // keep listening. + { + std::lock_guard lg(m_lock); + if (m_connection.get() == conn.get()){ + m_connection = nullptr; + } + } + socket->close_connection(); + } + } + m_listeners.run_method(&Listener::on_connect); +} + + +void HttpServer::on_connect_rejected(){ + m_listeners.run_method(&Listener::on_connect_rejected); +} + + +void HttpServer::on_receive_data(const void* data, size_t bytes){ + std::shared_ptr conn; + AbstractServerSocket* socket = nullptr; + { + std::lock_guard lg(m_lock); + conn = m_connection; + socket = m_socket.get(); + } + if (conn == nullptr){ + return; + } + bool exceeded = false; + { + std::lock_guard lg(conn->lock); + exceeded = conn->buffer.size() + bytes > MAX_BUFFER_BYTES; + if (!exceeded){ + const char* ptr = (const char*)data; + conn->buffer.append(ptr, bytes); + } + } + conn->cv.notify_all(); + if (exceeded && socket != nullptr){ + // The stream is longer than we are willing to buffer. It is + // likely in the middle of a request, so there is no reliable + // response left to send: just end the connection. ("socket" can + // be null only while stop() is tearing the socket down, in which + // case there is no connection task left to wake up.) + end_connection(conn, socket); + } +} + + +void HttpServer::on_disconnect(const std::string& error_message){ + std::shared_ptr conn; + { + std::lock_guard lg(m_lock); + // The socket may already be serving the next client by the time + // this callback runs: the socket's connection thread sets the + // state back to LISTENING before it reports the disconnect, so a + // fast reconnect can be accepted in between. In that case this + // disconnect is about the previous connection, which on_connect() + // has already marked dead; the current one belongs to the new + // client and must not be killed. + if (m_socket == nullptr || + m_socket->state() != AbstractServerSocket::State::CONNECTED + ){ + conn = m_connection; + } + } + if (conn != nullptr){ + conn->dead.store(true, std::memory_order_release); + conn->cv.notify_all(); + } + m_listeners.run_method(&Listener::on_disconnect, error_message); +} + + + +void HttpServer::connection_task( + const std::shared_ptr& conn, + AbstractServerSocket* socket +){ + // The handler may be replaced or cleared by start()/stop() while this + // task is waiting; a copy taken under m_lock is stable for the task's + // whole lifetime, because stop() waits for the task before clearing it. + Handler handler; + { + std::lock_guard lg(m_lock); + handler = m_handler; + } + + // "Expect: 100-continue" is answered at most once, and only while + // the body is still pending. + bool sent_continue = false; + + while (true){ + if (m_stop_requested.load(std::memory_order_acquire) || + conn->dead.load(std::memory_order_acquire) + ){ + break; + } + + ParsedRequest parsed; + size_t buffer_size; + { + std::lock_guard lg(conn->lock); + parsed = try_parse(conn->buffer); + buffer_size = conn->buffer.size(); + } + + if (parsed.state == ParseState::NEED_BODY){ + // The headers are complete and valid, but the body has not + // fully arrived. A client that sent "Expect: 100-continue" is + // still waiting for this before sending the body: answer it + // now, not after the (still missing) body arrives. (If the + // whole request was already buffered, the state is READY and + // the final response alone moves the client on; RFC 9110). + if (parsed.expect_continue && !sent_continue){ + static const char CONTINUE_RESPONSE[] = "HTTP/1.1 100 Continue\r\n\r\n"; + if (!write_raw(conn, socket, CONTINUE_RESPONSE)){ + break; + } + sent_continue = true; + } + } + + if (parsed.state == ParseState::NEED_MORE || + parsed.state == ParseState::NEED_BODY + ){ + // Wait for the rest of this request, a disconnect, or a + // stop. The predicate requires the buffer to have grown past + // what the parse just saw: with a partial request, + // "!buffer.empty()" alone is permanently true and would spin + // this loop at 100% CPU until the request completes. + std::unique_lock lg(conn->lock); + conn->cv.wait_for(lg, std::chrono::milliseconds(10), [this, &conn, buffer_size]{ + return conn->buffer.size() != buffer_size + || conn->dead.load(std::memory_order_relaxed) + || m_stop_requested.load(std::memory_order_relaxed); + }); + continue; + } + + if (parsed.state == ParseState::ERROR_RESPONSE){ + Response response; + response.status = parsed.error_status; + response.headers = std::move(parsed.error_headers); + response.body = parsed.error_reason + "\n"; + write_response(conn, socket, response, false); + end_connection(conn, socket); + break; + } + + // A complete request is ready to be handled. + Response response; + { + HandlerContextImpl context(conn->dead); + try{ + if (handler){ + handler(parsed.request, response, context); + }else{ + response.status = 500; + response.body = "No handler is registered.\n"; + response.connection_close = true; + } + }catch (...){ + response = Response(); + response.status = 500; + response.body = "The handler threw an exception.\n"; + response.connection_close = true; + } + } + + // The connection may have closed or been replaced by a new + // client while the handler was running; discard the response in + // that case. + if (conn->dead.load(std::memory_order_acquire) || + m_stop_requested.load(std::memory_order_acquire) + ){ + break; + } + + const bool keep_alive = parsed.http_1_1 + && !parsed.connection_close_requested + && !response.connection_close; + if (!write_response(conn, socket, response, keep_alive)){ + break; + } + if (!keep_alive){ + end_connection(conn, socket); + break; + } + // Keep-alive: the next request (possibly already buffered) is + // picked up at the top of the loop. + } + + conn->dead.store(true, std::memory_order_release); + { + std::lock_guard lg(m_lock); + if (m_connection.get() == conn.get()){ + m_connection = nullptr; + } + } +} + + +HttpServer::ParsedRequest HttpServer::try_parse(std::string& buffer){ + ParsedRequest result; + + auto error = [&result](int status, const char* reason, std::vector> headers = {}){ + result.state = ParseState::ERROR_RESPONSE; + result.error_status = status; + result.error_reason = reason; + result.error_headers = std::move(headers); + }; + + // The header section ends at the blank line. "header_end" points at + // the "\r\n" that terminates the last header (or the request line + // when there are no headers). + const size_t header_end = buffer.find("\r\n\r\n"); + if (header_end == std::string::npos){ + if (buffer.size() > MAX_HEADER_SECTION_BYTES){ + error(400, "Malformed request."); + } + return result; // NEED_MORE: the headers have not fully arrived yet. + } + // The same limit applies to a complete header section: the + // in-progress check above only fires while the terminator is still + // missing, so a large section that arrives in one piece would + // otherwise sail through. + if (header_end + 4 > MAX_HEADER_SECTION_BYTES){ + error(400, "Malformed request."); + return result; + } + + const size_t line_end = buffer.find("\r\n"); + if (line_end == std::string::npos){ + error(400, "Malformed request."); + return result; + } + if (line_end > MAX_REQUEST_LINE_BYTES){ + error(400, "Request line too long."); + return result; + } + + // "METHOD SP TARGET SP VERSION" + const std::string line = buffer.substr(0, line_end); + const size_t sp1 = line.find(' '); + if (sp1 == std::string::npos || sp1 == 0){ + error(400, "Malformed request."); + return result; + } + std::string method = line.substr(0, sp1); + for (char c : method){ + if (c < 'A' || c > 'Z'){ + error(400, "Malformed request."); + return result; + } + } + const size_t sp2 = line.find(' ', sp1 + 1); + if (sp2 == std::string::npos){ + error(400, "Malformed request."); + return result; + } + std::string target = line.substr(sp1 + 1, sp2 - sp1 - 1); + if (target.empty()){ + error(400, "Malformed request."); + return result; + } + for (char c : target){ + if (c == ' ' || c == '\r' || c == '\n' || c == '\0'){ + error(400, "Malformed request."); + return result; + } + } + const std::string version = line.substr(sp2 + 1); + const bool http_1_1 = (version == "HTTP/1.1"); + const bool http_1_0 = (version == "HTTP/1.0"); + if (!http_1_1 && !http_1_0){ + error(505, "HTTP version not supported."); + return result; + } + + // Headers: [line_end + 2, header_end) + const size_t section_begin = line_end + 2; + std::string section; + if (header_end >= section_begin){ + section = buffer.substr(section_begin, header_end - section_begin); + } + size_t header_count = 0; + size_t pos = 0; + while (pos < section.size()){ + const size_t eol = section.find("\r\n", pos); + const size_t length = (eol == std::string::npos) ? section.size() - pos : eol - pos; + const std::string header_line = section.substr(pos, length); + + if (header_line.empty() || ++header_count > MAX_HEADER_COUNT){ + error(400, "Malformed request."); + return result; + } + + const size_t colon = header_line.find(':'); + if (colon == std::string::npos || colon == 0){ + error(400, "Malformed request."); + return result; + } + std::string name = header_line.substr(0, colon); + for (char c : name){ + if (!is_token_char(c)){ + error(400, "Malformed request."); + return result; + } + } + std::string value = trim_ows(header_line.substr(colon + 1)); + for (char c : value){ + if ((unsigned char)c < 0x20 || (unsigned char)c == 0x7F){ + error(400, "Malformed request."); + return result; + } + } + name = to_lower(std::move(name)); + if (name == "content-length" && result.request.headers.count("content-length") != 0){ + error(400, "Duplicate content-length."); + return result; + } + result.request.headers[std::move(name)] = std::move(value); + + if (eol == std::string::npos){ + break; + } + pos = eol + 2; + } + + // Special headers. + if (result.request.headers.count("transfer-encoding") != 0){ + // "Not Implemented" rather than "Length Required": chunked + // encoding is a (unsupported) representation of the body, not a + // missing length (RFC 9112). + error(501, "Chunked transfer encoding is not supported."); + return result; + } + + uint64_t content_length = 0; + { + const auto content_length_it = result.request.headers.find("content-length"); + if (content_length_it != result.request.headers.end()){ + const std::string& value = content_length_it->second; + if (value.empty() || value.size() > 10){ + error(400, "Malformed content-length."); + return result; + } + for (char c : value){ + if (c < '0' || c > '9'){ + error(400, "Malformed content-length."); + return result; + } + } + content_length = std::stoull(value); + if (content_length > MAX_BODY_BYTES){ + error(413, "Request body too large."); + return result; + } + } + } + + const auto connection_it = result.request.headers.find("connection"); + if (connection_it != result.request.headers.end() && + to_lower(connection_it->second).find("close") != std::string::npos + ){ + result.connection_close_requested = true; + } + + const auto expect_it = result.request.headers.find("expect"); + if (expect_it != result.request.headers.end() && + to_lower(expect_it->second).find("100-continue") != std::string::npos + ){ + result.expect_continue = true; + } + + const size_t consumed = header_end + 4 + (size_t)content_length; + if (buffer.size() < consumed){ + // NEED_BODY: the headers are complete and valid, the body has + // not fully arrived yet. In this state the buffer holds nothing + // but the bytes of this one request ("consumed" is at most a + // header section plus the body cap), so the buffer cannot exceed + // MAX_BUFFER_BYTES here; the overall cap is enforced when data + // is appended instead. + result.state = ParseState::NEED_BODY; + return result; + } + + result.state = ParseState::READY; + result.request.method = std::move(method); + const size_t query = target.find('?'); + result.request.path = target.substr(0, query); + if (query != std::string::npos){ + result.request.query = target.substr(query + 1); + } + result.request.body = buffer.substr(header_end + 4, content_length); + buffer.erase(0, consumed); + result.http_1_1 = http_1_1; + return result; +} + + +void HttpServer::end_connection( + const std::shared_ptr& conn, + AbstractServerSocket* socket +){ + conn->dead.store(true, std::memory_order_release); + conn->cv.notify_all(); + // Close the socket's connection only while it still belongs to this + // connection: by the time a task notices its own connection died, + // the socket may already be serving the next client, and closing + // unconditionally would kill that client's connection instead. + { + std::lock_guard lg(m_lock); + if (m_connection.get() != conn.get()){ + return; + } + } + socket->close_connection(); +} + + +bool HttpServer::write_raw( + const std::shared_ptr& conn, + AbstractServerSocket* socket, + const std::string& bytes +){ + // "conn->socket_id" is the socket's connection token when this + // connection was accepted: if the socket has moved on to the next + // client since then, the send is refused instead of being delivered + // into the new client's socket. + const size_t sent = socket->send(bytes.data(), bytes.size(), WRITE_TIMEOUT, conn->socket_id); + if (sent != bytes.size()){ + // The peer went away, or the write stalled: the connection is + // unusable either way. + end_connection(conn, socket); + return false; + } + return true; +} + + +bool HttpServer::write_response( + const std::shared_ptr& conn, + AbstractServerSocket* socket, + const Response& response, + bool keep_alive +){ + return write_raw(conn, socket, format_response(response, keep_alive)); +} + + + +} diff --git a/Common/Cpp/HttpServer/HttpServer.h b/Common/Cpp/HttpServer/HttpServer.h new file mode 100644 index 0000000000..a770ea88fb --- /dev/null +++ b/Common/Cpp/HttpServer/HttpServer.h @@ -0,0 +1,169 @@ +/* HTTP Server + * + * From: https://github.com/PokemonAutomation/ + * + * The "AbstractServerSocket" implementation of "AbstractHttpServer": a + * minimal HTTP/1.1 server on the platform's default server socket, for a + * single local client that issues one request at a time. The request/ + * response types, the handler contract, and the threading rules are in + * "AbstractHttpServer.h"; this file documents the implementation. + * + */ + +#ifndef PokemonAutomation_HttpServer_H +#define PokemonAutomation_HttpServer_H + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Concurrency/AsyncTask.h" +#include "Common/Cpp/Concurrency/ConditionVariable.h" +#include "Common/Cpp/Concurrency/Mutex.h" +#include "Common/Cpp/HttpServer/AbstractHttpServer.h" +#include "Common/Cpp/Sockets/AbstractServerSocket.h" + +namespace PokemonAutomation{ + +class ThreadPool; + + + +class HttpServer : public AbstractHttpServer, private AbstractServerSocket::Listener{ +public: + // The interface's types, re-exported under this class's name. + using AbstractHttpServer::Request; + using AbstractHttpServer::Response; + using AbstractHttpServer::HandlerContext; + using AbstractHttpServer::Handler; + using AbstractHttpServer::Listener; + + +public: + explicit HttpServer(ThreadPool& thread_pool); + ~HttpServer(); + + virtual void start(const std::string& address, uint16_t port, Handler handler) override; + virtual void stop() override; + virtual bool is_running() const override; + + +private: + // AbstractServerSocket::Listener. + virtual void on_listen_finished(const std::string& error_message) override; + virtual void on_connect() override; + virtual void on_connect_rejected() override; + virtual void on_receive_data(const void* data, size_t bytes) override; + virtual void on_disconnect(const std::string& error_message) override; + + +private: + // One request that is waiting to be parsed out of the buffer. + enum class ParseState{ + NEED_MORE, + NEED_BODY, + READY, + // Named "ERROR_RESPONSE" rather than "ERROR" because "ERROR" is + // a macro on Windows (winerror.h). + ERROR_RESPONSE, + }; + + struct ParsedRequest{ + ParseState state = ParseState::NEED_MORE; + + // ERROR_RESPONSE + int error_status = 400; + std::string error_reason; + std::vector> error_headers; + + // READY / NEED_BODY + Request request; + bool http_1_1 = true; + bool connection_close_requested = false; + bool expect_continue = false; + }; + + // Per-connection state. The connection task is the only thread that + // parses or writes; "on_receive_data"/"on_disconnect" only append + // data and flip flags. + struct Connection{ + Mutex lock; + ConditionVariable cv; + std::string buffer; + std::atomic dead{false}; + + // The server socket's connection_id() when this connection was + // accepted. Passed to every send(), so a write that outlives the + // connection is refused instead of reaching the next client. + uint64_t socket_id = 0; + }; + + // Runs on a thread from the thread pool, one instance per + // connection. + void connection_task( + const std::shared_ptr& conn, + AbstractServerSocket* socket + ); + + // Parses one complete request out of "buffer", removing it on + // success. Must be called with "conn->lock" held. + static ParsedRequest try_parse(std::string& buffer); + + // Marks the connection dead and closes it at the socket level. No-op + // if the socket is no longer connected to this client. + void end_connection( + const std::shared_ptr& conn, + AbstractServerSocket* socket + ); + + // Writes "bytes" to the connection completely; returns false (and + // ends the connection) if the peer went away or the write stalled. + bool write_raw( + const std::shared_ptr& conn, + AbstractServerSocket* socket, + const std::string& bytes + ); + + // Formats and writes the final response. + bool write_response( + const std::shared_ptr& conn, + AbstractServerSocket* socket, + const Response& response, + bool keep_alive + ); + + // The standard reason phrase for "status", or "Unknown Status". + static const char* status_reason(int status); + + // Builds the wire format of a response. + static std::string format_response(const Response& response, bool keep_alive); + + +private: + ThreadPool& m_thread_pool; + + // Guards m_socket, m_connection, and m_handler. Never held across a + // blocking wait, a task destruction, or a socket destruction. + mutable Mutex m_lock; + std::unique_ptr m_socket; + std::shared_ptr m_connection; + Handler m_handler; + + // Guards m_tasks. Never held while an AsyncTask destructor runs: + // the destructor blocks until the task finishes, and the task's + // final cleanup takes m_lock. + Mutex m_task_lock; + std::vector m_tasks; + + // Set by "stop()"; checked by the connection tasks between requests + // and by "HandlerContext". + std::atomic m_stop_requested{false}; +}; + + + +} +#endif diff --git a/Common/Cpp/HttpServer/HttpServer_Tests.cpp b/Common/Cpp/HttpServer/HttpServer_Tests.cpp new file mode 100644 index 0000000000..65b4ca31f9 --- /dev/null +++ b/Common/Cpp/HttpServer/HttpServer_Tests.cpp @@ -0,0 +1,1372 @@ +/* HTTP Server Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Loopback tests that run a real "HttpServer" (the platform's default + * server socket backend) on 127.0.0.1 and drive it with raw + * "ClientSocket" clients. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Concurrency/AsyncTask.h" +#include "Common/Cpp/Concurrency/ConditionVariable.h" +#include "Common/Cpp/Concurrency/Mutex.h" +#include "Common/Cpp/Concurrency/ThreadPool.h" +#include "Common/Cpp/HttpServer/HttpServer.h" +#include "Common/Cpp/Sockets/ClientSocket.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "HttpServer_Tests.h" + +namespace PokemonAutomation{ + +namespace{ + + +struct Waiter{ + Mutex lock; + ConditionVariable cv; + + bool wait_for(std::chrono::milliseconds timeout, const std::function& pred){ + std::unique_lock lg(lock); + return cv.wait_for(lg, timeout, pred); + } + + void notify(){ + std::lock_guard lg(lock); + cv.notify_all(); + } +}; + + +// Long enough that a moderately loaded machine can make it, short enough +// that a real regression is still reported within the CI timeouts. +constexpr std::chrono::seconds HTTP_TEST_WAIT{60}; + + +// A handler gate: handlers notify "started" when they begin, and block on +// "wait_for_release()" until the test releases them. +struct HandlerGate{ + Waiter started; + + Mutex lock; + ConditionVariable cv; + std::atomic released{false}; + + void wait_for_release(std::chrono::milliseconds timeout){ + std::unique_lock lg(lock); + cv.wait_for(lg, timeout, [this]{ + return released.load(std::memory_order_relaxed); + }); + } + + void release(){ + released.store(true, std::memory_order_release); + std::lock_guard lg(lock); + cv.notify_all(); + } +}; + + +// A raw TCP client that accumulates everything it receives. +// "received" and the error strings are protected by "waiter.lock": the +// socket's receive thread writes them, the test thread reads them. +struct TestClient{ + Waiter waiter; + std::string received; + std::atomic connected{0}; + std::atomic disconnected{0}; + std::string connect_error; + std::string disconnect_error; + + struct Listener : AbstractClientSocket::Listener{ + TestClient* client; + + virtual void on_connect_finished(const std::string& error_message) override{ + { + std::lock_guard lg(client->waiter.lock); + client->connect_error = error_message; + } + client->connected++; + client->waiter.notify(); + } + + virtual void on_receive_data(const void* data, size_t bytes) override{ + { + std::lock_guard lg(client->waiter.lock); + client->received.append((const char*)data, bytes); + } + client->waiter.notify(); + } + + virtual void on_disconnect(const std::string& error_message) override{ + { + std::lock_guard lg(client->waiter.lock); + client->disconnect_error = error_message; + } + client->disconnected++; + client->waiter.notify(); + } + } listener_impl; + + ClientSocket socket; + + explicit TestClient(ThreadPool& thread_pool) + : socket(thread_pool) + { + listener_impl.client = this; + socket.add_listener(listener_impl); + } + + bool connect(uint16_t port){ + socket.connect("127.0.0.1", port); + return waiter.wait_for( + HTTP_TEST_WAIT, + [this]{ return connected.load() >= 1; } + ); + } + + bool send_all(const std::string& bytes){ + size_t total = 0; + while (total < bytes.size()){ + const size_t sent = socket.send(bytes.data() + total, bytes.size() - total); + if (sent == 0){ + return false; + } + total += sent; + } + return true; + } + + bool wait_for_bytes(size_t n){ + // The predicate runs with "waiter.lock" held, so it may read + // "received" directly. + return waiter.wait_for( + HTTP_TEST_WAIT, + [this, n]{ return received.size() >= n; } + ); + } + + // A consistent copy of everything received so far, for assertions. + std::string received_snapshot(){ + std::lock_guard lg(waiter.lock); + return received; + } +}; + + +struct TestServerListener : HttpServer::Listener{ + Waiter waiter; + std::atomic listen_finished{0}; + // Protected by "waiter.lock" (written by the socket's thread). + std::string last_listen_error; + std::atomic connected{0}; + std::atomic rejected{0}; + std::atomic disconnected{0}; + + virtual void on_listen_finished(const std::string& error_message) override{ + { + std::lock_guard lg(waiter.lock); + last_listen_error = error_message; + } + listen_finished++; + waiter.notify(); + } + + std::string last_listen_error_snapshot(){ + std::lock_guard lg(waiter.lock); + return last_listen_error; + } + + virtual void on_connect() override{ + connected++; + waiter.notify(); + } + + virtual void on_connect_rejected() override{ + rejected++; + waiter.notify(); + } + + virtual void on_disconnect(const std::string& error_message) override{ + disconnected++; + waiter.notify(); + } +}; + + +class TestHttpServer : public UnitTest{ +public: + TestHttpServer(ThreadPool& thread_pool, std::string name) + : UnitTest(std::move(name)) + , m_thread_pool(thread_pool) + { + m_threads = 2; + } + + +protected: + // Starts the server with "handler" on a free loopback port, waiting + // for the listen to finish. "handler" is copied on each attempt. + bool start_server(HttpServer& server, TestServerListener& listener, HttpServer::Handler handler, uint16_t& port) const{ + for (int attempt = 0; attempt < 8; attempt++){ + uint16_t candidate; + { + std::lock_guard lg(m_rng_lock); + candidate = (uint16_t)(40000 + m_rng() % 20000); + } + const int finished_before = listener.listen_finished.load(); + server.start("127.0.0.1", candidate, handler); + if (!listener.waiter.wait_for( + HTTP_TEST_WAIT, + [finished_before, &listener]{ + return listener.listen_finished.load() > finished_before; + } + )){ + server.stop(); + continue; + } + if (listener.last_listen_error_snapshot().empty()){ + port = candidate; + return true; + } + // A failed listen leaves the server "started but not + // listening"; stop() cleans that up before the next attempt. + server.stop(); + } + return false; + } + +protected: + ThreadPool& m_thread_pool; + +private: + mutable Mutex m_rng_lock; + mutable std::mt19937 m_rng{std::random_device{}()}; +}; + + +// A POST request is parsed (method, path, query, headers, body) and the +// response carries the extra headers and the exact body; the connection +// stays keep-alive. +class Test_Http_PostEcho : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + auto handler = [](const HttpServer::Request& request, HttpServer::Response& response, HttpServer::HandlerContext&){ + response.status = 200; + response.headers.push_back({"X-Test", "yes"}); + response.body = request.path + "|" + request.query + "|" + request.header("X-Client") + "|" + request.body; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!server_listener.waiter.wait_for( + HTTP_TEST_WAIT, + [&server_listener]{ return server_listener.connected.load() >= 1; } + )){ + error = "The server did not call on_connect"; + }else if (!client.send_all( + "POST /mcp?session=abc HTTP/1.1\r\n" + "Host: 127.0.0.1\r\n" + "X-Client: unit-test\r\n" + "Content-Length: 5\r\n" + "\r\n" + "hello" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "X-Test: yes\r\n" + "Content-Length: 32\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "/mcp|session=abc|unit-test|hello"; + if (!client.wait_for_bytes(expected.size())){ + error = "The client did not receive the full response"; + }else if (client.received_snapshot() != expected){ + error = "Bad response:\n" + client.received_snapshot(); + } + } + // The client goes out of scope here and closes the socket. + } + + if (error.empty() && + !server_listener.waiter.wait_for( + HTTP_TEST_WAIT, + [&server_listener]{ return server_listener.disconnected.load() >= 1; } + ) + ){ + error = "The server did not call on_disconnect"; + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// Two requests sent back to back on one connection (the second is already +// buffered while the first is being answered): both must be answered, in +// order. +class Test_Http_PipelinedKeepAlive : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + auto handler = [](const HttpServer::Request& request, HttpServer::Response& response, HttpServer::HandlerContext&){ + response.status = 200; + response.body = "r" + request.body; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all( + "POST /a HTTP/1.1\r\n" + "Content-Length: 3\r\n" + "\r\n" + "one" + "POST /b HTTP/1.1\r\n" + "Content-Length: 3\r\n" + "\r\n" + "two" + )){ + error = "The client did not send the requests"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "Content-Length: 4\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "rone" + "HTTP/1.1 200 OK\r\n" + "Content-Length: 4\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "rtwo"; + if (!client.wait_for_bytes(expected.size())){ + error = "The client did not receive both responses (got " + std::to_string(client.received_snapshot().size()) + " of " + std::to_string(expected.size()) + " bytes)"; + }else if (client.received_snapshot() != expected){ + error = "Bad response:\n" + client.received_snapshot(); + } + } + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// "Connection: close" in the request: the response says so, the server +// closes the connection, and it keeps serving new clients. +class Test_Http_ConnectionClose : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + auto handler = [](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ + response.status = 200; + response.body = "done"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 1\r\n" + "Connection: close\r\n" + "\r\n" + "x" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "Content-Length: 4\r\n" + "Connection: close\r\n" + "\r\n" + "done"; + if (!client.wait_for_bytes(expected.size())){ + error = "The client did not receive the full response"; + }else if (client.received_snapshot() != expected){ + error = "Bad response:\n" + client.received_snapshot(); + }else if (!client.waiter.wait_for( + HTTP_TEST_WAIT, + [&client]{ return client.disconnected.load() >= 1; } + )){ + error = "The server did not close the connection"; + } + } + } + + if (error.empty()){ + // The server must keep listening for a new client. + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not reconnect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 1\r\n" + "\r\n" + "y" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "Content-Length: 4\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "done"; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "The reconnected client did not get a good response:\n" + client.received_snapshot(); + } + } + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// A request line that is not "METHOD SP TARGET SP VERSION" gets a 400 and +// a closed connection; the server recovers and serves a new client. +class Test_Http_BadRequest : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + auto handler = [](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ + response.status = 200; + response.body = "ok"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all("GARBAGE\r\n\r\n")){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 400 Bad Request\r\n" + "Content-Length: 19\r\n" + "Connection: close\r\n" + "\r\n" + "Malformed request.\n"; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "Bad error response:\n" + client.received_snapshot(); + } + } + } + + if (error.empty()){ + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not reconnect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 0\r\n" + "\r\n" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "Content-Length: 2\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "ok"; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "The server did not recover:\n" + client.received_snapshot(); + } + } + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// Chunked request bodies are not supported: 501 and a closed connection, +// and the handler is never called. +class Test_Http_ChunkedRejected : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + std::atomic handler_called{false}; + auto handler = [&handler_called](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ + handler_called = true; + response.status = 200; + response.body = "ok"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 501 Not Implemented\r\n" + "Content-Length: 44\r\n" + "Connection: close\r\n" + "\r\n" + "Chunked transfer encoding is not supported.\n"; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "Bad error response:\n" + client.received_snapshot(); + } + } + } + + if (error.empty() && handler_called.load()){ + error = "The handler was called for a rejected request"; + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// A content-length above the body limit is rejected with 413 before the +// (never sent) body arrives, and the handler is never called. +class Test_Http_BodyTooLarge : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + std::atomic handler_called{false}; + auto handler = [&handler_called](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ + handler_called = true; + response.status = 200; + response.body = "ok"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 2097152\r\n" + "\r\n" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 413 Payload Too Large\r\n" + "Content-Length: 24\r\n" + "Connection: close\r\n" + "\r\n" + "Request body too large.\n"; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "Bad error response:\n" + client.received_snapshot(); + } + } + } + + if (error.empty() && handler_called.load()){ + error = "The handler was called for a rejected request"; + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// A complete header section over the size limit is rejected with 400, +// even though it arrives in one piece (the in-progress limit check only +// fires while the terminator is still missing). +class Test_Http_HeadersTooLarge : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + std::atomic handler_called{false}; + auto handler = [&handler_called](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ + handler_called = true; + response.status = 200; + response.body = "ok"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + // 50 headers of about 1.5 KiB each: about 75 KiB of headers - + // under the 100-header count limit, over the 64 KiB size limit. + std::string request = "POST / HTTP/1.1\r\n"; + for (int i = 0; i < 50; i++){ + request += "X-Filler-"; + request += std::to_string(i); + request += ": "; + request += std::string(1490, 'a'); + request += "\r\n"; + } + request += "\r\n"; + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all(request)){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 400 Bad Request\r\n" + "Content-Length: 19\r\n" + "Connection: close\r\n" + "\r\n" + "Malformed request.\n"; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "Bad error response:\n" + client.received_snapshot(); + } + } + } + + if (error.empty() && handler_called.load()){ + error = "The handler was called for a rejected request"; + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// A connection that sends more data than the receive buffer cap (about +// 2 MiB) is closed without a response, and the server keeps listening. +class Test_Http_BufferCap : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + std::atomic handler_calls{0}; + auto handler = [&handler_calls](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ + handler_calls++; + response.status = 200; + response.body = "ok"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else{ + // A never-completing request far larger than the buffer + // cap: the server closes the connection without a + // response. (The send may fail partway through once the + // server has hung up; that is expected.) + const std::string flood(3u << 20, 'A'); + client.send_all(flood); + if (!client.waiter.wait_for( + HTTP_TEST_WAIT, + [&client]{ return client.disconnected.load() >= 1; } + )){ + error = "The server did not close the over-cap connection"; + } + } + } + + if (error.empty() && handler_calls.load() != 0){ + error = "The handler was called for a capped connection"; + } + + if (error.empty()){ + // The server must still be listening afterwards. + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect after the cap test"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 0\r\n" + "\r\n" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "Content-Length: 2\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "ok"; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "The server did not answer after the cap test:\n" + client.received_snapshot(); + } + } + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// "Expect: 100-continue" is answered with "100 Continue" as soon as the +// headers are validated - before the body arrives, which is what a +// client waiting for the interim response is waiting for - and the +// final response carries the body. +class Test_Http_ExpectContinue : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + auto handler = [](const HttpServer::Request& request, HttpServer::Response& response, HttpServer::HandlerContext&){ + response.status = 200; + response.body = request.body; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 5\r\n" + "Expect: 100-continue\r\n" + "\r\n" + )){ + error = "The client did not send the headers"; + }else{ + const std::string continue_response = "HTTP/1.1 100 Continue\r\n\r\n"; + if (!client.wait_for_bytes(continue_response.size()) + || client.received_snapshot().compare(0, continue_response.size(), continue_response) != 0 + ){ + error = "The server did not answer the interim response:\n" + client.received_snapshot(); + }else if (!client.send_all("hello")){ + error = "The client did not send the body"; + }else{ + const std::string expected = + "HTTP/1.1 100 Continue\r\n\r\n" + "HTTP/1.1 200 OK\r\n" + "Content-Length: 5\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "hello"; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "Bad response:\n" + client.received_snapshot(); + } + } + } + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// The client disconnects while the handler is still running: the handler +// sees it via "HandlerContext::connection_alive()", its response is +// discarded, and the server keeps serving new clients. +class Test_Http_DisconnectDuringHandler : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + HandlerGate gate; + std::atomic call_count{0}; + std::atomic first_call_alive{false}; + std::atomic first_call_saw_disconnect{false}; + + auto handler = [&gate, &call_count, &first_call_alive, &first_call_saw_disconnect]( + const HttpServer::Request&, + HttpServer::Response& response, + HttpServer::HandlerContext& context + ){ + const int call = call_count.fetch_add(1) + 1; + if (call == 1){ + first_call_alive = context.connection_alive(); + gate.started.notify(); + gate.wait_for_release(HTTP_TEST_WAIT); + first_call_saw_disconnect = !context.connection_alive(); + } + response.status = 200; + response.body = "ok"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 1\r\n" + "\r\n" + "x" + )){ + error = "The client did not send the request"; + }else if (!gate.started.wait_for( + HTTP_TEST_WAIT, + [&first_call_alive]{ return first_call_alive.load(); } + )){ + error = "The handler did not start"; + } + // The client goes out of scope here and closes the socket, + // while the handler is still blocked on the gate. + } + + if (error.empty() && + !server_listener.waiter.wait_for( + HTTP_TEST_WAIT, + [&server_listener]{ return server_listener.disconnected.load() >= 1; } + ) + ){ + error = "The server did not call on_disconnect"; + } + + if (error.empty()){ + gate.release(); + } + + if (error.empty()){ + // The server must keep serving new clients. + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not reconnect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 1\r\n" + "\r\n" + "y" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "Content-Length: 2\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "ok"; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "The server did not recover:\n" + client.received_snapshot(); + } + } + } + + if (error.empty()){ + // Wait for the first handler to have recorded its observation. + if (!gate.started.wait_for( + HTTP_TEST_WAIT, + [&first_call_saw_disconnect, &call_count]{ + return call_count.load() >= 1 && first_call_saw_disconnect.load(); + } + )){ + error = "The handler did not record the disconnect"; + } + } + + if (error.empty()){ + if (!first_call_alive.load()){ + error = "The handler did not see the connection as alive on entry"; + }else if (!first_call_saw_disconnect.load()){ + error = "The handler did not see the disconnect via connection_alive()"; + } + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// The client disconnects while the handler is still running, and a new +// client connects before the handler returns: the old task must discard +// its response instead of writing it into the new client's socket. +class Test_Http_ReconnectWhileHandlerRunning : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + HandlerGate gate; + std::atomic first_call_alive{false}; + + auto handler = [&gate, &first_call_alive]( + const HttpServer::Request& request, + HttpServer::Response& response, + HttpServer::HandlerContext& context + ){ + if (request.path == "/a"){ + first_call_alive = context.connection_alive(); + gate.started.notify(); + gate.wait_for_release(HTTP_TEST_WAIT); + } + response.status = 200; + response.body = (request.path == "/a") ? "A" : "ok"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + TestClient client_a(m_thread_pool); + TestClient client_b(m_thread_pool); + { + if (!client_a.connect(port)){ + error = "The first client did not connect"; + }else if (!client_a.send_all( + "POST /a HTTP/1.1\r\n" + "Content-Length: 1\r\n" + "\r\n" + "x" + )){ + error = "The first client did not send the request"; + }else if (!gate.started.wait_for( + HTTP_TEST_WAIT, + [&first_call_alive]{ return first_call_alive.load(); } + )){ + error = "The handler did not start"; + } + } + + if (error.empty()){ + // Disconnect the first client (closing its socket while it is + // still in scope, so its connection task keeps running) and + // wait for the server to report the disconnect. + client_a.socket.close(); + if (!server_listener.waiter.wait_for( + HTTP_TEST_WAIT, + [&server_listener]{ return server_listener.disconnected.load() >= 1; } + )){ + error = "The server did not report the first client's disconnect"; + } + } + + if (error.empty()){ + // Connect the second client while the first connection's + // handler is still blocked on the gate. Waiting for the + // second on_connect guarantees the second connection has + // replaced the first one in the server by the time the gate + // is released. + if (!client_b.connect(port)){ + error = "The second client did not connect"; + }else if (!server_listener.waiter.wait_for( + HTTP_TEST_WAIT, + [&server_listener]{ return server_listener.connected.load() >= 2; } + )){ + error = "The server did not accept the second client"; + } + } + + if (error.empty()){ + gate.release(); + } + + if (error.empty()){ + // The second client's first bytes must be its own response: + // the first client's late response was discarded, not + // written into this socket. + if (!client_b.send_all( + "POST /b HTTP/1.1\r\n" + "Content-Length: 1\r\n" + "\r\n" + "y" + )){ + error = "The second client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "Content-Length: 2\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "ok"; + if (!client_b.wait_for_bytes(expected.size()) || client_b.received_snapshot() != expected){ + error = "The second client received a stray response:\n" + client_b.received_snapshot(); + } + } + } + + if (error.empty() && !first_call_alive.load()){ + error = "The handler did not see the connection as alive on entry"; + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// "stop()" while the handler is still running blocks until the handler +// returns, and "start()" works afterwards. +class Test_Http_StopDuringHandler : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + HandlerGate gate; + std::atomic handler_started{false}; + std::atomic saw_disconnect_on_stop{false}; + + auto handler = [&gate, &handler_started, &saw_disconnect_on_stop]( + const HttpServer::Request&, + HttpServer::Response& response, + HttpServer::HandlerContext& context + ){ + handler_started = true; + gate.started.notify(); + gate.wait_for_release(HTTP_TEST_WAIT); + // Then stay alive while the connection is alive: the pattern + // a long-running tool call uses. stop() marks the connection + // dead before it waits for this handler (the socket's + // close_connection() close suppresses the disconnect + // callback), so the loop must terminate - without that mark + // this would spin forever and stop() would hang. + while (context.connection_alive()){ + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + saw_disconnect_on_stop = !context.connection_alive(); + response.status = 200; + response.body = "late"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 1\r\n" + "\r\n" + "x" + )){ + error = "The client did not send the request"; + }else if (!gate.started.wait_for( + HTTP_TEST_WAIT, + [&handler_started]{ return handler_started.load(); } + )){ + error = "The handler did not start"; + } + } + + if (error.empty()){ + // stop() blocks until the in-flight handler returns. Release + // the handler from a pool thread so this thread can call it. + // The handle is kept (dispatch is [[nodiscard]]); its + // destructor waits for the trivial release task to finish. + AsyncTask releaser_task = m_thread_pool.dispatch([&gate]{ + gate.release(); + }); + server.stop(); + if (server.is_running()){ + error = "The server is still running after stop()"; + } + } + + if (error.empty() && !saw_disconnect_on_stop.load()){ + error = "The handler did not see the connection as gone after stop()"; + } + + if (error.empty()){ + // Start again (on a fresh port) and serve a fresh request. + auto simple_handler = [](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ + response.status = 200; + response.body = "ok"; + }; + if (!start_server(server, server_listener, simple_handler, port)){ + error = "Failed to restart the server"; + }else{ + TestClient client(m_thread_pool); + if (!client.connect(port)){ + error = "The client did not connect to the restarted server"; + }else if (!client.send_all( + "POST / HTTP/1.1\r\n" + "Content-Length: 0\r\n" + "\r\n" + )){ + error = "The client did not send the request"; + }else{ + const std::string expected = + "HTTP/1.1 200 OK\r\n" + "Content-Length: 2\r\n" + "Connection: keep-alive\r\n" + "\r\n" + "ok"; + if (!client.wait_for_bytes(expected.size()) || client.received_snapshot() != expected){ + error = "The restarted server did not answer:\n" + client.received_snapshot(); + } + } + } + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + +// While one client is connected, a second one is rejected. +class Test_Http_RejectSecondClient : public TestHttpServer{ +public: + using TestHttpServer::TestHttpServer; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + TestServerListener server_listener; + HttpServer server(m_thread_pool); + server.add_listener(server_listener); + + auto handler = [](const HttpServer::Request&, HttpServer::Response& response, HttpServer::HandlerContext&){ + response.status = 200; + response.body = "ok"; + }; + + uint16_t port; + if (!start_server(server, server_listener, handler, port)){ + return "Failed to start the server"; + } + + std::string error; + { + TestClient first(m_thread_pool); + if (!first.connect(port)){ + error = "The first client did not connect"; + }else if (!server_listener.waiter.wait_for( + HTTP_TEST_WAIT, + [&server_listener]{ return server_listener.connected.load() >= 1; } + )){ + error = "The server did not call on_connect"; + }else{ + TestClient second(m_thread_pool); + second.connect(port); // May complete at the TCP level; the server closes it. + if (!server_listener.waiter.wait_for( + HTTP_TEST_WAIT, + [&server_listener]{ return server_listener.rejected.load() >= 1; } + )){ + error = "The server did not call on_connect_rejected"; + } + } + } + + server.stop(); + if (!error.empty()){ + return error; + } + return true; + } +}; + + + +} // anonymous namespace + + +void add_tests_HttpServer(UnitTestDatabase& database, ThreadPool& thread_pool){ + database.add( + thread_pool, "HttpServer - POST request/response with headers, query and body" + ); + database.add( + thread_pool, "HttpServer - Two pipelined requests on one keep-alive connection" + ); + database.add( + thread_pool, "HttpServer - Connection: close and reconnection" + ); + database.add( + thread_pool, "HttpServer - Malformed request line" + ); + database.add( + thread_pool, "HttpServer - Chunked request bodies are rejected" + ); + database.add( + thread_pool, "HttpServer - Oversized content-length is rejected" + ); + database.add( + thread_pool, "HttpServer - An oversized header section is rejected" + ); + database.add( + thread_pool, "HttpServer - A connection over the buffer cap is closed" + ); + database.add( + thread_pool, "HttpServer - Expect: 100-continue" + ); + database.add( + thread_pool, "HttpServer - Disconnect while the handler is running" + ); + database.add( + thread_pool, "HttpServer - Reconnect while the previous handler is still running" + ); + database.add( + thread_pool, "HttpServer - stop() while the handler is running" + ); + database.add( + thread_pool, "HttpServer - A second client is rejected" + ); +} + + + +} diff --git a/Common/Cpp/HttpServer/HttpServer_Tests.h b/Common/Cpp/HttpServer/HttpServer_Tests.h new file mode 100644 index 0000000000..6fe6c25cb1 --- /dev/null +++ b/Common/Cpp/HttpServer/HttpServer_Tests.h @@ -0,0 +1,28 @@ +/* HTTP Server Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_HttpServer_Tests_H +#define PokemonAutomation_HttpServer_Tests_H + +namespace PokemonAutomation{ + +class ThreadPool; +class UnitTestDatabase; + + +// Loopback tests that run a real "HttpServer" (the platform's default +// server socket backend) on 127.0.0.1 and drive it with raw "ClientSocket" +// clients: request/response parsing, keep-alive, pipelining, +// "Connection: close", "Expect: 100-continue", error responses, and the +// lifecycle (disconnect during a handler, "stop()" during a handler, +// restart, second-client rejection). `thread_pool` runs the server and +// the test workers; it must be a long-lived pool (e.g. +// `GlobalThreadPools::unlimited_normal()`). +void add_tests_HttpServer(UnitTestDatabase& database, ThreadPool& thread_pool); + + +} +#endif diff --git a/Common/Cpp/Sockets/AbstractServerSocket.h b/Common/Cpp/Sockets/AbstractServerSocket.h index 4139a0f120..1c2da259ef 100644 --- a/Common/Cpp/Sockets/AbstractServerSocket.h +++ b/Common/Cpp/Sockets/AbstractServerSocket.h @@ -13,6 +13,7 @@ #define PokemonAutomation_Sockets_AbstractServerSocket_H #include +#include #include #include "Common/Cpp/ListenerSet.h" @@ -58,6 +59,18 @@ class AbstractServerSocket{ return m_state.load(std::memory_order_relaxed); } + // A token identifying the connection the socket is currently + // serving. It changes every time a new client is accepted, and it + // is 0 when no connection is active. Capture it when a write + // starts and pass it to "send()", so that a write that outlives its + // connection (the peer disconnected and a new client was accepted + // in the meantime) refuses to reach the new client's socket. + // Implementations keep the underlying counter monotonic (never + // reset it when a connection closes, or a queued write for the old + // connection could match the next connection's id); 0 is only the + // "no connection is active" report, never a counter value. + virtual uint64_t connection_id() = 0; + // Binds to "address" and listens on "port". // Use "0.0.0.0" to accept connections on all interfaces. virtual void start(const std::string& address, uint16_t port) = 0; @@ -69,7 +82,10 @@ class AbstractServerSocket{ virtual size_t send( const void* data, size_t bytes, - std::chrono::milliseconds timeout = std::chrono::milliseconds::max() + std::chrono::milliseconds timeout = std::chrono::milliseconds::max(), + // The connection_id() captured when this write started. 0 skips + // the check: the call applies to whatever connection is active. + uint64_t connection = 0 ) = 0; diff --git a/Common/Cpp/Sockets/ServerSocket_POSIX.cpp b/Common/Cpp/Sockets/ServerSocket_POSIX.cpp index 75b2a14ead..ad38549eb6 100644 --- a/Common/Cpp/Sockets/ServerSocket_POSIX.cpp +++ b/Common/Cpp/Sockets/ServerSocket_POSIX.cpp @@ -83,6 +83,14 @@ void ServerSocket_POSIX::start(const std::string& address, uint16_t port){ } } +uint64_t ServerSocket_POSIX::connection_id(){ + std::lock_guard lg(m_lock); + return m_state.load(std::memory_order_relaxed) == State::CONNECTED + ? m_connection_id + : 0; +} + + void ServerSocket_POSIX::close_connection(){ int socket; { @@ -159,16 +167,16 @@ void ServerSocket_POSIX::close_and_wait() noexcept{ } } -size_t ServerSocket_POSIX::send(const void* data, size_t bytes, std::chrono::milliseconds timeout){ +size_t ServerSocket_POSIX::send(const void* data, size_t bytes, std::chrono::milliseconds timeout, uint64_t connection){ int socket; { std::lock_guard lg(m_lock); - socket = m_state.load(std::memory_order_relaxed) == State::CONNECTED - ? m_client - : -1; - } - if (socket == -1){ - return 0; + if (m_state.load(std::memory_order_relaxed) != State::CONNECTED + || (connection != 0 && m_connection_id != connection) + ){ + return 0; + } + socket = m_client; } const char* ptr = (const char*)data; @@ -184,7 +192,12 @@ size_t ServerSocket_POSIX::send(const void* data, size_t bytes, std::chrono::mil while (bytes > 0){ { std::lock_guard lg(m_lock); - if (m_state.load(std::memory_order_relaxed) != State::CONNECTED){ + // The connection may have been replaced (or closed) since the + // last iteration: in that case this write must stop rather + // than reach the new client's socket. + if (m_state.load(std::memory_order_relaxed) != State::CONNECTED + || (connection != 0 && m_connection_id != connection) + ){ return sent; } } @@ -359,6 +372,7 @@ void ServerSocket_POSIX::accept_thread_loop(std::string address, uint16_t port, fcntl(client_socket, F_SETFL, O_NONBLOCK); m_client = client_socket; m_close_requested = false; + m_connection_id++; m_state.store(State::CONNECTED, std::memory_order_release); accepted = true; } diff --git a/Common/Cpp/Sockets/ServerSocket_POSIX.h b/Common/Cpp/Sockets/ServerSocket_POSIX.h index 6786a631fc..f08b83ca68 100644 --- a/Common/Cpp/Sockets/ServerSocket_POSIX.h +++ b/Common/Cpp/Sockets/ServerSocket_POSIX.h @@ -42,9 +42,15 @@ class ServerSocket_POSIX final : public AbstractServerSocket{ virtual size_t send( const void* data, size_t bytes, - std::chrono::milliseconds timeout = std::chrono::milliseconds::max() + std::chrono::milliseconds timeout = std::chrono::milliseconds::max(), + uint64_t connection = 0 ) override; + // A token identifying the connection the socket is currently + // serving. It changes every time a new client is accepted, and it + // is 0 when no connection is active. + virtual uint64_t connection_id() override; + private: @@ -87,6 +93,12 @@ class ServerSocket_POSIX final : public AbstractServerSocket{ int m_client = -1; bool m_close_requested = false; + // A connection token, incremented (under m_lock) on every accept. + // send() compares it against the token the caller captured, so a + // write that outlived its connection cannot reach the client that + // replaced it. 0 means no connection is active. + uint64_t m_connection_id = 0; + // One task per start() call. start() must never destroy the previous // task's AsyncTask here: its destructor blocks until the task has // finished, and a task from an earlier attempt may still be running diff --git a/Common/Cpp/Sockets/ServerSocket_Qt.cpp b/Common/Cpp/Sockets/ServerSocket_Qt.cpp index fd48c72001..359a45b1f1 100644 --- a/Common/Cpp/Sockets/ServerSocket_Qt.cpp +++ b/Common/Cpp/Sockets/ServerSocket_Qt.cpp @@ -174,13 +174,16 @@ void ServerSocket_Qt::close_and_wait() noexcept{ m_close_requested = false; } -size_t ServerSocket_Qt::send(const void* data, size_t bytes, std::chrono::milliseconds timeout){ +size_t ServerSocket_Qt::send(const void* data, size_t bytes, std::chrono::milliseconds timeout, uint64_t connection){ QTcpSocket* socket; { std::lock_guard lg(m_lock); - socket = m_state.load(std::memory_order_relaxed) == State::CONNECTED - ? m_socket - : nullptr; + if (m_state.load(std::memory_order_relaxed) != State::CONNECTED + || (connection != 0 && m_connection_id != connection) + ){ + return 0; + } + socket = m_socket; } if (socket == nullptr){ return 0; @@ -188,6 +191,7 @@ size_t ServerSocket_Qt::send(const void* data, size_t bytes, std::chrono::millis QSharedPointer send_data = QSharedPointer::create(); send_data->packet.assign((const char*)data, bytes); + send_data->connection = connection; // "time_point::max()" (the infinite default) is never reached; using // it also avoids overflowing "now() + timeout". send_data->deadline = @@ -213,7 +217,8 @@ size_t ServerSocket_Qt::send(const void* data, size_t bytes, std::chrono::millis std::lock_guard lg(m_lock); registered = m_state.load(std::memory_order_relaxed) == State::CONNECTED - && m_socket != nullptr; + && m_socket != nullptr + && (connection == 0 || m_connection_id == connection); if (registered){ m_inflight_sends.push_back(send_data); } @@ -310,6 +315,7 @@ void ServerSocket_Qt::on_new_connection(){ }else{ m_socket = sock; m_close_requested = false; + m_connection_id++; m_state.store(State::CONNECTED, std::memory_order_release); accepted = true; rejected = false; @@ -434,11 +440,28 @@ void ServerSocket_Qt::on_close_connection(QSharedPointer comple completion->cv.notify_all(); } +uint64_t ServerSocket_Qt::connection_id(){ + std::lock_guard lg(m_lock); + return m_state.load(std::memory_order_relaxed) == State::CONNECTED + ? m_connection_id + : 0; +} + + void ServerSocket_Qt::on_send(QSharedPointer send_data){ QTcpSocket* sock; { std::lock_guard lg(m_lock); - sock = m_socket; + if (send_data->connection != 0 + && m_connection_id != send_data->connection + ){ + // The connection this send was queued for was replaced (or + // torn down) while it was waiting in the queue: the write + // loop must not reach into the new connection's socket. + sock = nullptr; + }else{ + sock = m_socket; + } } { diff --git a/Common/Cpp/Sockets/ServerSocket_Qt.h b/Common/Cpp/Sockets/ServerSocket_Qt.h index 0bcf70c0a6..370152e98c 100644 --- a/Common/Cpp/Sockets/ServerSocket_Qt.h +++ b/Common/Cpp/Sockets/ServerSocket_Qt.h @@ -54,6 +54,12 @@ class ServerSocket_Qt final : public QObject, public AbstractServerSocket{ // "close_and_wait()"/"close_connection()" cancel the send. bool done = false; + // The connection_id() the send was queued for; 0 skips the + // check. "on_send" refuses to write if the connection was + // replaced while the send was queued, so it cannot reach the + // new client's socket. + uint64_t connection = 0; + Mutex lock; ConditionVariable cv; }; @@ -86,9 +92,15 @@ class ServerSocket_Qt final : public QObject, public AbstractServerSocket{ virtual size_t send( const void* data, size_t bytes, - std::chrono::milliseconds timeout = std::chrono::milliseconds::max() + std::chrono::milliseconds timeout = std::chrono::milliseconds::max(), + uint64_t connection = 0 ) override; + // A token identifying the connection the socket is currently + // serving. It changes every time a new client is accepted, and it + // is 0 when no connection is active. + virtual uint64_t connection_id() override; + signals: void internal_send(QSharedPointer send_data); @@ -142,6 +154,12 @@ class ServerSocket_Qt final : public QObject, public AbstractServerSocket{ bool m_close_requested = false; bool m_starting = false; + // A connection token, incremented (under m_lock) on every accept. + // "send()" compares it against the token the caller captured, so a + // write that outlived its connection cannot reach the client that + // replaced it. 0 means no connection is active. + uint64_t m_connection_id = 0; + // The "send()" calls that are queued on the event thread but not // finished yet, so that "close_and_wait()"/"close_connection()" can // cancel diff --git a/Common/Cpp/Sockets/ServerSocket_WinSocket.cpp b/Common/Cpp/Sockets/ServerSocket_WinSocket.cpp index 55889f4b38..066a76ab24 100644 --- a/Common/Cpp/Sockets/ServerSocket_WinSocket.cpp +++ b/Common/Cpp/Sockets/ServerSocket_WinSocket.cpp @@ -94,6 +94,14 @@ void ServerSocket_WinSocket::start(const std::string& address, uint16_t port){ } } +uint64_t ServerSocket_WinSocket::connection_id(){ + std::lock_guard lg(m_lock); + return m_state.load(std::memory_order_relaxed) == State::CONNECTED + ? m_connection_id + : 0; +} + + void ServerSocket_WinSocket::close_connection(){ SOCKET socket; { @@ -170,13 +178,16 @@ void ServerSocket_WinSocket::close_and_wait() noexcept{ } } -size_t ServerSocket_WinSocket::send(const void* data, size_t bytes, std::chrono::milliseconds timeout){ +size_t ServerSocket_WinSocket::send(const void* data, size_t bytes, std::chrono::milliseconds timeout, uint64_t connection){ SOCKET socket; { std::lock_guard lg(m_lock); - socket = m_state.load(std::memory_order_relaxed) == State::CONNECTED - ? m_client - : INVALID_SOCKET; + if (m_state.load(std::memory_order_relaxed) != State::CONNECTED + || (connection != 0 && m_connection_id != connection) + ){ + return 0; + } + socket = m_client; } if (socket == INVALID_SOCKET){ return 0; @@ -195,7 +206,12 @@ size_t ServerSocket_WinSocket::send(const void* data, size_t bytes, std::chrono: while (bytes > 0){ { std::lock_guard lg(m_lock); - if (m_state.load(std::memory_order_relaxed) != State::CONNECTED){ + // The connection may have been replaced (or closed) since the + // last iteration: in that case this write must stop rather + // than reach the new client's socket. + if (m_state.load(std::memory_order_relaxed) != State::CONNECTED + || (connection != 0 && m_connection_id != connection) + ){ return sent; } } @@ -379,6 +395,7 @@ void ServerSocket_WinSocket::accept_thread_loop(std::string address, uint16_t po ioctlsocket(client_socket, FIONBIO, &client_non_blocking); m_client = client_socket; m_close_requested = false; + m_connection_id++; m_state.store(State::CONNECTED, std::memory_order_release); accepted = true; } diff --git a/Common/Cpp/Sockets/ServerSocket_WinSocket.h b/Common/Cpp/Sockets/ServerSocket_WinSocket.h index 6b68519ab8..f478e69b46 100644 --- a/Common/Cpp/Sockets/ServerSocket_WinSocket.h +++ b/Common/Cpp/Sockets/ServerSocket_WinSocket.h @@ -45,9 +45,15 @@ class ServerSocket_WinSocket final : public AbstractServerSocket{ virtual size_t send( const void* data, size_t bytes, - std::chrono::milliseconds timeout = std::chrono::milliseconds::max() + std::chrono::milliseconds timeout = std::chrono::milliseconds::max(), + uint64_t connection = 0 ) override; + // A token identifying the connection the socket is currently + // serving. It changes every time a new client is accepted, and it + // is 0 when no connection is active. + virtual uint64_t connection_id() override; + private: // Winsock has to be started before any of its functions can be used, and @@ -96,6 +102,12 @@ class ServerSocket_WinSocket final : public AbstractServerSocket{ SOCKET m_client = INVALID_SOCKET; bool m_close_requested = false; + // A connection token, incremented (under m_lock) on every accept. + // send() compares it against the token the caller captured, so a + // write that outlived its connection cannot reach the client that + // replaced it. 0 means no connection is active. + uint64_t m_connection_id = 0; + // One task per start() call. start() must never destroy the previous // task's AsyncTask here: its destructor blocks until the task has // finished, and a task from an earlier attempt may still be running diff --git a/Common/Cpp/Sockets/Sockets_Tests.cpp b/Common/Cpp/Sockets/Sockets_Tests.cpp index 7864a7d34a..3fe3bd915d 100644 --- a/Common/Cpp/Sockets/Sockets_Tests.cpp +++ b/Common/Cpp/Sockets/Sockets_Tests.cpp @@ -822,6 +822,117 @@ class Test_Sockets_RejectSecondClient : public TestSockets +class Test_Sockets_StaleSend : public TestSockets{ +public: + using TestSockets::TestSockets; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + ServerListener server_listener; + ServerSocketType server(this->m_thread_pool); + server.add_listener(server_listener); + + uint16_t port; + if (!this->start_server(server, server_listener, port)){ + return "Failed to start the server"; + } + + std::string error; + if (server.connection_id() != 0){ + error = "connection_id() was not 0 before any connection"; + } + + uint64_t first_id = 0; + { + ClientListener client_listener; + ClientSocketType client(this->m_thread_pool); + client.add_listener(client_listener); + + if (!this->connect_client(client, client_listener, port, 1)){ + error = "The first client did not finish connecting"; + }else if (!server_listener.waiter.wait_for( + SOCKET_TEST_WAIT, + [&server_listener]{ return server_listener.connected.load() >= 1; } + )){ + error = "The server did not call on_connect"; + }else{ + first_id = server.connection_id(); + if (first_id == 0){ + error = "The server did not assign a connection id"; + } + client.close(); + } + } + + if (error.empty() && !server_listener.waiter.wait_for( + SOCKET_TEST_WAIT, + [&server_listener]{ return server_listener.disconnected.load() >= 1; } + )){ + error = "The server did not call on_disconnect"; + } + + // The counter is monotonic and not reset, but with no active + // connection the id must report 0. + if (error.empty() && server.connection_id() != 0){ + error = "connection_id() was not 0 while no connection was active"; + } + + uint64_t second_id = 0; + { + ClientListener client_listener; + ClientSocketType client(this->m_thread_pool); + client.add_listener(client_listener); + + if (!this->connect_client(client, client_listener, port, 1)){ + error = "The second client did not finish connecting"; + }else if (!server_listener.waiter.wait_for( + SOCKET_TEST_WAIT, + [&server_listener]{ return server_listener.connected.load() >= 2; } + )){ + error = "The server did not call on_connect for the second client"; + }else{ + second_id = server.connection_id(); + if (second_id == first_id){ + error = "The connection id did not change after reconnect"; + } + // A send for the replaced connection must be refused, + // not delivered into the new client's socket. + if (error.empty() && + server.send("stale", 5, std::chrono::milliseconds::max(), first_id) != 0 + ){ + error = "A send for a replaced connection was not refused"; + } + // A send for the current connection still goes through. + if (error.empty() && + server.send("hello", 5, std::chrono::milliseconds::max(), second_id) != 5 + ){ + error = "The send for the current connection was not delivered"; + } + if (error.empty() && !client_listener.waiter.wait_for( + SOCKET_TEST_WAIT, + [&client_listener]{ return client_listener.received.size() >= 5; } + )){ + error = "The second client did not receive the data"; + }else if (error.empty() && + std::memcmp(client_listener.received.data(), "hello", 5) != 0 + ){ + error = "The second client received the wrong data"; + } + client.close(); + } + } + + if (!error.empty()){ + return error; + } + return true; + } +}; + + // Starting a second server on an already-bound port must fail through // on_listen_finished and leave the server not running. template @@ -2676,6 +2787,9 @@ void add_socket_tests(UnitTestDatabase& database, ThreadPool& thread_pool, const database.add>( thread_pool, prefix + "Reject a second client" ); + database.add>( + thread_pool, prefix + "A send for a replaced connection is refused" + ); database.add>( thread_pool, prefix + "Port in use" ); diff --git a/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.cpp b/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.cpp index 9a615e8b4c..abf5aae895 100644 --- a/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.cpp +++ b/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.cpp @@ -4,6 +4,7 @@ * */ +#include #include "Common/Cpp/Json/JsonValue.h" #include "Common/Cpp/Json/JsonObject.h" #include "CommonFramework/GlobalAutoPaths.h" @@ -15,7 +16,10 @@ namespace PokemonAutomation{ -SpriteDatabase::SpriteDatabase(const char* sprite_path, const char* json_path) +SpriteDatabase::SpriteDatabase( + const char* sprite_path, const char* json_path, + const std::function& mirror_horizontally +) : m_backing_image(RESOURCE_PATH() + sprite_path) { std::string path = RESOURCE_PATH() + json_path; @@ -37,6 +41,16 @@ SpriteDatabase::SpriteDatabase(const char* sprite_path, const char* json_path) JsonObject& obj = item.second.to_object_throw(path); int y = (int)obj.get_integer_throw("top", path); int x = (int)obj.get_integer_throw("left", path); + if (mirror_horizontally && mirror_horizontally(slug)){ + if (x < 0 || y < 0 || (size_t)(x + width) > m_backing_image.width() || (size_t)(y + height) > m_backing_image.height()){ + throw FileException(nullptr, PA_CURRENT_FUNCTION, "Sprite out of bounds: " + slug, path); + } + for (int r = y; r < y + (int)height; r++){ + for (int c0 = x, c1 = x + (int)width - 1; c0 < c1; c0++, c1--){ + std::swap(m_backing_image.pixel(c0, r), m_backing_image.pixel(c1, r)); + } + } + } ImageViewRGB32 sprite = extract_box_reference(m_backing_image, ImagePixelBox(x, y, x + width, y + height)); m_database.emplace( diff --git a/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.h b/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.h index 3136ffd6ec..3213479227 100644 --- a/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.h +++ b/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.h @@ -7,6 +7,7 @@ #ifndef PokemonAutomation_CommonTools_Resources_SpriteCompositeImage_H #define PokemonAutomation_CommonTools_Resources_SpriteCompositeImage_H +#include #include #include "CommonFramework/ImageTypes/ImageRGB32.h" @@ -32,7 +33,14 @@ class SpriteDatabase{ // (next pokemon) ... // } // } - SpriteDatabase(const char* sprite_path, const char* json_path); + // + // If "mirror_horizontally" is set, each sprite for which it returns true + // is flipped left-to-right in place. This avoids storing a separate + // flipped copy of the image. + SpriteDatabase( + const char* sprite_path, const char* json_path, + const std::function& mirror_horizontally = nullptr + ); public: struct Sprite{ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEventLog.h b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEventLog.h similarity index 70% rename from SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEventLog.h rename to SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEventLog.h index 62f34e2f6b..2d3039a5e7 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEventLog.h +++ b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEventLog.h @@ -2,31 +2,22 @@ * * From: https://github.com/PokemonAutomation/ * - * Split out of Pokemon_ScreenEvents.h, unchanged; it includes this header. + * The screen events of a stream of frames, in order and timed. * */ -#ifndef PokemonAutomation_Pokemon_ScreenEventLog_H -#define PokemonAutomation_Pokemon_ScreenEventLog_H +#ifndef PokemonAutomation_CommonTools_ScreenEvents_ScreenEventLog_H +#define PokemonAutomation_CommonTools_ScreenEvents_ScreenEventLog_H -#include +#include #include #include -#include #include #include -#include "Common/Cpp/Concurrency/Mutex.h" #include "Common/Cpp/Time.h" -#include "CommonFramework/ImageTools/ImageStats.h" -#include "CommonFramework/ImageTypes/ImageViewRGB32.h" -#include "CommonFramework/VideoPipeline/VideoFeed.h" -#include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h" -#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" -#include "NintendoSwitch/Inference/NintendoSwitch_HomeMenuDetector.h" -#include "Pokemon_ScreenFades.h" +#include "ScreenEventTypes.h" namespace PokemonAutomation{ -namespace Pokemon{ // Whether an event started or stopped showing. @@ -51,8 +42,8 @@ constexpr size_t SCREEN_EVENT_LOG_CAPACITY = 256; // The events of a stream of frames, in order and timed: what rose and fell // when, not only what was seen (ScreenEventWatcher::seen()). The judges that -// care about order read it (Pokemon_Interrupts.h): a flash then a battle is -// an encounter, a box then a battle a trainer's sight. +// care about order read it: a flash then a battle is an encounter, a box +// then a battle a trainer's sight. // // It keeps the newest `capacity` changes and counts the ones it dropped. // Not thread-safe; ScreenEventWatcher guards its own. @@ -95,6 +86,5 @@ std::string describe(const ScreenEventLog& log, WallClock origin); -} // namespace Pokemon -} // namespace PokemonAutomation +} // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEventTypes.h b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEventTypes.h similarity index 76% rename from SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEventTypes.h rename to SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEventTypes.h index c71d62e8ed..49e35ecb4d 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEventTypes.h +++ b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEventTypes.h @@ -2,30 +2,21 @@ * * From: https://github.com/PokemonAutomation/ * - * Split out of Pokemon_ScreenEvents.h, unchanged; it includes this header. + * The words a program uses for what the screen shows: a fade, a dialogue + * box, a battle starting. ScreenEvents.h watches a stream for them. * */ -#ifndef PokemonAutomation_Pokemon_ScreenEventTypes_H -#define PokemonAutomation_Pokemon_ScreenEventTypes_H +#ifndef PokemonAutomation_CommonTools_ScreenEvents_ScreenEventTypes_H +#define PokemonAutomation_CommonTools_ScreenEvents_ScreenEventTypes_H -#include +#include #include -#include #include -#include #include -#include "Common/Cpp/Concurrency/Mutex.h" -#include "Common/Cpp/Time.h" -#include "CommonFramework/ImageTools/ImageStats.h" -#include "CommonFramework/ImageTypes/ImageViewRGB32.h" -#include "CommonFramework/VideoPipeline/VideoFeed.h" -#include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h" -#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" -#include "NintendoSwitch/Inference/NintendoSwitch_HomeMenuDetector.h" +#include namespace PokemonAutomation{ -namespace Pokemon{ // One thing a frame shows. A game reports the ones its detectors can see. @@ -95,9 +86,11 @@ class ScreenEvents{ uint32_t m_bits = 0; }; +// "dialog box, player can move" (joined by `separator`); "nothing" when +// empty. +std::string describe(const ScreenEvents& events, std::string_view separator = ", "); -} // namespace Pokemon -} // namespace PokemonAutomation +} // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEvents.cpp b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents.cpp similarity index 85% rename from SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEvents.cpp rename to SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents.cpp index 367a23ec8d..8b7dcb6c8a 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEvents.cpp +++ b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents.cpp @@ -4,13 +4,16 @@ * */ +#include #include #include "CommonFramework/ImageTools/ImageBoxes.h" -#include "CommonFramework/ImageTypes/ImageRGB32.h" -#include "Pokemon_ScreenEvents.h" +#include "CommonFramework/ImageTools/ImageStats.h" +#include "ScreenEventLog.h" +#include "ScreenEventTypes.h" +#include "ScreenFades.h" +#include "ScreenEvents.h" namespace PokemonAutomation{ -namespace Pokemon{ std::string screen_event_name(ScreenEvent event){ @@ -80,7 +83,7 @@ WarpFade fade_in(const ScreenEvents& events){ } return WarpFade::NONE; } -std::string describe(const ScreenEvents& events){ +std::string describe(const ScreenEvents& events, std::string_view separator){ std::string out; for (size_t c = 0; c < SCREEN_EVENT_COUNT; c++){ const ScreenEvent event = (ScreenEvent)c; @@ -88,7 +91,7 @@ std::string describe(const ScreenEvents& events){ continue; } if (!out.empty()){ - out += ", "; + out += separator; } out += screen_event_name(event); } @@ -150,23 +153,6 @@ std::string describe(const ScreenEventLog& log, WallClock origin){ } -SwitchHomeObserver::SwitchHomeObserver(NintendoSwitch::ConsoleHandle& console, ScreenEventObserver& game) - : m_game(game) - , m_home(console, COLOR_RED) -{} - -ScreenEvents SwitchHomeObserver::observe(const VideoSnapshot& frame, WallClock now){ - if (frame && m_home.detect_only(*frame.frame)){ - return {ScreenEvent::SWITCH_HOME_MENU}; - } - return m_game.observe(frame, now); -} - -void SwitchHomeObserver::make_overlays(VideoOverlaySet& items) const{ - m_game.make_overlays(items); -} - - ScreenEventWatcher::ScreenEventWatcher(ScreenEventObserver& observer, ScreenEvents stop_on) : VisualInferenceCallback("ScreenEventWatcher") , m_observer(observer) @@ -201,5 +187,4 @@ void ScreenEventWatcher::reset(){ -} // namespace Pokemon -} // namespace PokemonAutomation +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEvents.h b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents.h similarity index 59% rename from SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEvents.h rename to SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents.h index ee3080235d..3e90b8d85d 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEvents.h +++ b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents.h @@ -2,38 +2,30 @@ * * From: https://github.com/PokemonAutomation/ * - * What a story step can see on the screen, in words every game shares: a - * fade, a location banner, a dialogue box, a battle starting. Each game - * implements ScreenEventObserver with its own detectors; the judgements - * built on these events (Pokemon_SceneJudges.h) are then the same for all - * of them. + * What an autonomous program can see on the screen, in words every game + * shares: a fade, a location banner, a dialogue box, a battle starting. + * Each game implements ScreenEventObserver with its own detectors; the + * judgements built on these events are then the same for all of them. * * An observer runs during a controller action as a ScreenEventWatcher * (a VisualInferenceCallback for run_until), or is asked frame by frame. * */ -#ifndef PokemonAutomation_Pokemon_ScreenEvents_H -#define PokemonAutomation_Pokemon_ScreenEvents_H +#ifndef PokemonAutomation_CommonTools_ScreenEvents_ScreenEvents_H +#define PokemonAutomation_CommonTools_ScreenEvents_ScreenEvents_H #include #include -#include -#include -#include -#include #include "Common/Cpp/Concurrency/Mutex.h" #include "Common/Cpp/Time.h" -#include "CommonFramework/ImageTools/ImageStats.h" -#include "CommonFramework/ImageTypes/ImageViewRGB32.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" #include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h" -#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" -#include "NintendoSwitch/Inference/NintendoSwitch_HomeMenuDetector.h" -#include "Pokemon_ScreenEventLog.h" +#include "ScreenEventLog.h" +#include "ScreenEventTypes.h" namespace PokemonAutomation{ -namespace Pokemon{ // A game's detectors, as events. Observers may keep state between frames @@ -52,23 +44,6 @@ class ScreenEventObserver{ }; -// A game's observer behind a look at the Switch itself: a frame showing -// the HOME menu shows SWITCH_HOME_MENU and nothing else, as a game's -// detectors may read HOME's plain screen as a quiet overworld (BDSP, -// lab-B 2026-11-09). Every other frame is the game's to read. -class SwitchHomeObserver : public ScreenEventObserver{ -public: - SwitchHomeObserver(NintendoSwitch::ConsoleHandle& console, ScreenEventObserver& game); - - virtual ScreenEvents observe(const VideoSnapshot& frame, WallClock now) override; - virtual void make_overlays(VideoOverlaySet& items) const override; - -private: - ScreenEventObserver& m_game; - NintendoSwitch::HomeMenuDetector m_home; -}; - - // An observer as a run_until callback: it records every event it sees, in // order in a log, and stops the action on any of `stop_on` (none: it only // records). @@ -104,6 +79,5 @@ class ScreenEventWatcher : public VisualInferenceCallback{ -} // namespace Pokemon -} // namespace PokemonAutomation +} // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEvents_Tests.cpp b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents_Tests.cpp similarity index 89% rename from SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEvents_Tests.cpp rename to SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents_Tests.cpp index 82aae9bf03..c12213eed6 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEvents_Tests.cpp +++ b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents_Tests.cpp @@ -13,11 +13,12 @@ #include #include "Common/Cpp/TestRunners/UnitTest.h" #include "CommonFramework/ImageTypes/ImageRGB32.h" -#include "Pokemon_ScreenEvents.h" -#include "Pokemon_ScreenEvents_Tests.h" +#include "ScreenEventTypes.h" +#include "ScreenFades.h" +#include "ScreenEvents.h" +#include "ScreenEvents_Tests.h" namespace PokemonAutomation{ -namespace Pokemon{ namespace{ @@ -131,23 +132,22 @@ UnitTestResult test_watcher(){ } -struct ScreenEventsInput{ - std::string name; - std::function body; -}; - -struct ScreenEventsExpectation{ -}; - struct ScreenEventsCase{ - ScreenEventsInput input; - ScreenEventsExpectation expectation; + struct Input{ + std::string name; + std::function body; + }; + struct Expectation{ + }; + + Input input; + Expectation expectation; }; class Test_ScreenEvents : public UnitTest{ public: explicit Test_ScreenEvents(ScreenEventsCase test_case) - : UnitTest(std::format("Pokemon::ScreenEvents - {}", test_case.input.name)) + : UnitTest(std::format("CommonTools::ScreenEvents - {}", test_case.input.name)) , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ @@ -184,5 +184,4 @@ void add_tests_ScreenEvents(UnitTestDatabase& database){ -} // namespace Pokemon -} // namespace PokemonAutomation +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEvents_Tests.h b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents_Tests.h similarity index 58% rename from SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEvents_Tests.h rename to SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents_Tests.h index 7fe664f59e..3d1c84b44f 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEvents_Tests.h +++ b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents_Tests.h @@ -4,13 +4,12 @@ * */ -#ifndef PokemonAutomation_Pokemon_ScreenEvents_Tests_H -#define PokemonAutomation_Pokemon_ScreenEvents_Tests_H +#ifndef PokemonAutomation_CommonTools_ScreenEvents_ScreenEvents_Tests_H +#define PokemonAutomation_CommonTools_ScreenEvents_ScreenEvents_Tests_H #include "Common/Cpp/TestRunners/UnitTestDatabase.h" namespace PokemonAutomation{ -namespace Pokemon{ // Registers unit tests for the shared screen events. @@ -18,6 +17,5 @@ void add_tests_ScreenEvents(UnitTestDatabase& database); -} // namespace Pokemon -} // namespace PokemonAutomation +} // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenFades.h b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenFades.h similarity index 58% rename from SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenFades.h rename to SerialPrograms/Source/CommonTools/ScreenEvents/ScreenFades.h index 57de9f6a3d..ad452c0761 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenFades.h +++ b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenFades.h @@ -2,39 +2,24 @@ * * From: https://github.com/PokemonAutomation/ * - * Split out of Pokemon_ScreenEvents.h, unchanged; it includes this header. + * A fade to black or white, read from a screen and as screen events. * */ -#ifndef PokemonAutomation_Pokemon_ScreenFades_H -#define PokemonAutomation_Pokemon_ScreenFades_H - -#include -#include -#include -#include -#include -#include -#include "Common/Cpp/Concurrency/Mutex.h" -#include "Common/Cpp/Time.h" +#ifndef PokemonAutomation_CommonTools_ScreenEvents_ScreenFades_H +#define PokemonAutomation_CommonTools_ScreenEvents_ScreenFades_H + #include "CommonFramework/ImageTools/ImageStats.h" #include "CommonFramework/ImageTypes/ImageViewRGB32.h" -#include "CommonFramework/VideoPipeline/VideoFeed.h" -#include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h" -#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" -#include "NintendoSwitch/Inference/NintendoSwitch_HomeMenuDetector.h" -#include "Pokemon_ScreenEventTypes.h" +#include "ScreenEventTypes.h" namespace PokemonAutomation{ -namespace Pokemon{ // The event of a fade: FADE_BLACK, FADE_WHITE, or none for WarpFade::NONE. ScreenEvents fade_events(WarpFade fade); // The fade `events` show: BLACK before WHITE if they have both. WarpFade fade_in(const ScreenEvents& events); -// "dialog box, player can move"; "nothing" when empty. -std::string describe(const ScreenEvents& events); // A fade is uniform over the middle 80% of the screen, the shared @@ -55,6 +40,5 @@ WarpFade fade_of_screen(const ImageViewRGB32& screen); -} // namespace Pokemon -} // namespace PokemonAutomation +} // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/CommonTools/ScreenMachine/ScreenMachine.cpp b/SerialPrograms/Source/CommonTools/ScreenMachine/ScreenMachine.cpp index 587d740ec8..4be721658c 100644 --- a/SerialPrograms/Source/CommonTools/ScreenMachine/ScreenMachine.cpp +++ b/SerialPrograms/Source/CommonTools/ScreenMachine/ScreenMachine.cpp @@ -30,36 +30,6 @@ const ScreenState* classify(const std::vector& states, const ImageV } // namespace -std::string_view screen_event_name(ScreenEvent event){ - switch (event){ - case ScreenEvent::FADE_BLACK: return "fade_black"; - case ScreenEvent::FADE_WHITE: return "fade_white"; - case ScreenEvent::TEXT_BOX: return "text_box"; - case ScreenEvent::BATTLE: return "battle"; - case ScreenEvent::CAN_MOVE: return "can_move"; - case ScreenEvent::STILL: return "still"; - case ScreenEvent::CHOICE_BOX: return "choice_box"; - case ScreenEvent::INTERACT_PROMPT: return "interact_prompt"; - } - return "unknown"; -} - -std::string describe(const ScreenEvents& events, std::string_view separator){ - constexpr ScreenEvent ALL[] = { - ScreenEvent::FADE_BLACK, ScreenEvent::FADE_WHITE, ScreenEvent::TEXT_BOX, - ScreenEvent::BATTLE, ScreenEvent::CAN_MOVE, ScreenEvent::STILL, - ScreenEvent::CHOICE_BOX, ScreenEvent::INTERACT_PROMPT, - }; - std::string text; - for (ScreenEvent event : ALL){ - if (events.has(event)){ - text += text.empty() ? "" : separator; - text += screen_event_name(event); - } - } - return text.empty() ? std::string("nothing") : text; -} - bool same_screen(const VideoSnapshot& a, const VideoSnapshot& b){ if (!a || !b || a->width() != b->width() || a->height() != b->height()){ return false; diff --git a/SerialPrograms/Source/CommonTools/ScreenMachine/ScreenObservation.h b/SerialPrograms/Source/CommonTools/ScreenMachine/ScreenObservation.h index 636e8eec7d..ba1e6e84b3 100644 --- a/SerialPrograms/Source/CommonTools/ScreenMachine/ScreenObservation.h +++ b/SerialPrograms/Source/CommonTools/ScreenMachine/ScreenObservation.h @@ -3,7 +3,7 @@ * From: https://github.com/PokemonAutomation/ * * What one look at the game showed, as facts rather than pixels: the - * events on screen (a fade, a text box, a battle) and named readings. A + * events on screen (a fade, a dialog box, a battle) and named readings. A * console fills it from a frame with the game's detectors; a simulation * fills it from its model and has no frame. A ScreenState that matches on * an observation (ScreenState::matches_observation) therefore runs in @@ -14,58 +14,17 @@ #ifndef PokemonAutomation_CommonTools_ScreenObservation_H #define PokemonAutomation_CommonTools_ScreenObservation_H -#include #include -#include #include #include #include -#include #include "Common/Cpp/Time.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/ScreenEvents/ScreenEventTypes.h" namespace PokemonAutomation{ -// Something a look at the screen can show. Several can hold at once. -enum class ScreenEvent : uint8_t{ - FADE_BLACK, // the screen is (fading to) black: a warp, a battle start - FADE_WHITE, // the screen is (fading to) white - TEXT_BOX, // a dialogue or message box is open - BATTLE, // a battle is showing - CAN_MOVE, // the player can move: no box, menu or scene holds them - STILL, // nothing moved since the previous look - CHOICE_BOX, // a list of answers to pick from is open - INTERACT_PROMPT, // a prompt to interact with what the player faces ("A Talk") -}; - -// The set of events one look showed. -class ScreenEvents{ -public: - ScreenEvents() = default; - explicit ScreenEvents(std::initializer_list events){ - for (ScreenEvent event : events){ - add(event); - } - } - - bool has(ScreenEvent event) const{ - return (m_bits & bit(event)) != 0; - } - void add(ScreenEvent event){ - m_bits |= bit(event); - } - - bool operator==(const ScreenEvents& other) const = default; - -private: - static uint32_t bit(ScreenEvent event){ - return (uint32_t)1 << (uint32_t)event; - } - - uint32_t m_bits = 0; -}; - // One look at the game. struct ScreenObservation{ WallClock when = WallClock::min(); @@ -78,13 +37,6 @@ struct ScreenObservation{ std::optional frame = std::nullopt; }; -// "fade_black", "fade_white", "text_box", "battle", "can_move", "still", -// "choice_box", "interact_prompt". -std::string_view screen_event_name(ScreenEvent event); - -// The events in `events` joined by `separator`, or "nothing" when empty. -std::string describe(const ScreenEvents& events, std::string_view separator = ", "); - // Whether `a` and `b` both have a frame of the same size within the // still-screen RMSD threshold (2.0). bool same_screen(const VideoSnapshot& a, const VideoSnapshot& b); diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index 085886a32d..cdeb54de03 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -4,6 +4,7 @@ * */ +#include "Common/Cpp/HttpServer/HttpServer_Tests.h" #include "Common/Cpp/Json/JsonTools_Tests.h" #include "Common/Cpp/ListenerSet.h" #include "Common/Cpp/MoveOnlyFunction_Tests.h" @@ -26,9 +27,12 @@ #include "CommonFramework/Windows/LabControl.h" #include "CommonTools/VisualDetectors/BlackBorderDetector.h" #include "CommonTools/OCR/OCR_Tests.h" +#include "CommonTools/ScreenEvents/ScreenEvents_Tests.h" #include "NintendoSwitch/Inference/NintendoSwitch_CheckOnlineDetector.h" #include "NintendoSwitch/Inference/NintendoSwitch_FailedToConnectDetector.h" +#include "NintendoSwitch/Inference/NintendoSwitch_HomeTileReader.h" +#include "NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h" #include "NintendoSwitch/Inference/NintendoSwitch_UpdatePopupDetector.h" #include "NintendoSwitch/Programs/Typewriter/NintendoSwitch_Typewriter_Tests.h" #include "NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.h" @@ -42,6 +46,8 @@ #include "Pokemon/Navigation/Pokemon_RouteScorer_Tests.h" #include "Pokemon/Navigation/Pokemon_RuntimeMap_Tests.h" #include "Pokemon/Navigation/Pokemon_WallRecovery_Tests.h" +#include "Pokemon/Navigation/Simulation/Pokemon_SimFaults_Tests.h" +#include "Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.h" #include "PokemonBDSP/PokemonBDSP_Tests.h" #include "VideoGames/PokemonHome/PokemonHome_Tests.h" #include "VideoGames/PokemonSwSh/PokemonSwSh_Tests.h" @@ -65,6 +71,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ UnitTestDatabase ret; add_tests_QtEventThreadPool(ret, GlobalThreadPools::computation_normal()); add_tests_Sockets(ret, GlobalThreadPools::unlimited_normal()); + add_tests_HttpServer(ret, GlobalThreadPools::unlimited_normal()); add_tests_BlackBorderDetector(ret); add_tests_ListenerSet(ret); add_tests_JsonTools(ret); @@ -79,6 +86,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ add_tests_AudioFileLoader(ret); add_tests_LabControl(ret); OCR::add_tests(ret); + add_tests_ScreenEvents(ret); Kernels::add_tests(ret); Pokemon::add_tests_LegPlanner(ret); Pokemon::add_tests_LegWalker(ret); @@ -89,9 +97,13 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ Pokemon::add_tests_RuntimeMap(ret); Pokemon::add_tests_RouteLog(ret); Pokemon::add_tests_RouteScorer(ret); + Pokemon::add_tests_SimFaults(ret); + Pokemon::add_tests_SimGolden(ret); Pokemon::add_tests_AutoStory(ret); NintendoSwitch::add_tests_CheckOnlineDetector(ret); NintendoSwitch::add_tests_FailedToConnectDetector(ret); + NintendoSwitch::add_tests_HomeTileReader(ret); + NintendoSwitch::add_tests_SystemKeyboardDetector(ret); NintendoSwitch::add_tests_UpdatePopupDetector(ret); NintendoSwitch::Typewriter::add_tests_TypewriterTextMapping(ret); NintendoSwitch::PokemonBDSP::add_tests(ret); diff --git a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_HomeScreenObserver.cpp b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_HomeScreenObserver.cpp new file mode 100644 index 0000000000..195d33cdd3 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_HomeScreenObserver.cpp @@ -0,0 +1,34 @@ +/* Home Screen Observer + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/Color.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "NintendoSwitch_HomeScreenObserver.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + + +SwitchHomeObserver::SwitchHomeObserver(ConsoleHandle& console, ScreenEventObserver& game) + : m_game(game) + , m_home(console, COLOR_RED) +{} + +ScreenEvents SwitchHomeObserver::observe(const VideoSnapshot& frame, WallClock now){ + if (frame && m_home.detect_only(*frame.frame)){ + return {ScreenEvent::SWITCH_HOME_MENU}; + } + return m_game.observe(frame, now); +} + +void SwitchHomeObserver::make_overlays(VideoOverlaySet& items) const{ + m_game.make_overlays(items); +} + + + +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_HomeScreenObserver.h b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_HomeScreenObserver.h new file mode 100644 index 0000000000..1916a70edf --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_HomeScreenObserver.h @@ -0,0 +1,42 @@ +/* Home Screen Observer + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_NintendoSwitch_HomeScreenObserver_H +#define PokemonAutomation_NintendoSwitch_HomeScreenObserver_H + +#include "Common/Cpp/Time.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "CommonTools/ScreenEvents/ScreenEventTypes.h" +#include "CommonTools/ScreenEvents/ScreenEvents.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "NintendoSwitch_HomeMenuDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + + +// A game's observer behind a look at the Switch itself: a frame showing +// the HOME menu shows SWITCH_HOME_MENU and nothing else, as a game's +// detectors may read HOME's plain screen as a quiet overworld (BDSP, +// lab-B 2026-11-09). Every other frame is the game's to read. +class SwitchHomeObserver : public ScreenEventObserver{ +public: + SwitchHomeObserver(ConsoleHandle& console, ScreenEventObserver& game); + + virtual ScreenEvents observe(const VideoSnapshot& frame, WallClock now) override; + virtual void make_overlays(VideoOverlaySet& items) const override; + +private: + ScreenEventObserver& m_game; + HomeMenuDetector m_home; +}; + + + +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_HomeTileReader.cpp b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_HomeTileReader.cpp new file mode 100644 index 0000000000..b8913f5001 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_HomeTileReader.cpp @@ -0,0 +1,249 @@ +/* Home Tile Reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonTools/Images/ImageFilter.h" +#include "CommonTools/OCR/OCR_Routines.h" +#include "CommonTools/OCR/OCR_StringNormalization.h" +#include "Tests/ImageUnitTest.h" +#include "NintendoSwitch_HomeTileReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + + +namespace{ + +// The selected tile's frame lines, about 0.008 of the screen tall, just +// outside the tile art: strips across the screen that the art of the +// other tiles stays out of. +const ImageFloatBox FRAME_TOP_STRIP{0.0, 0.252, 1.0, 0.014}; +const ImageFloatBox FRAME_BOTTOM_STRIP{0.0, 0.627, 1.0, 0.014}; +constexpr double FRAME_TOP = 0.256; +constexpr double FRAME_BOTTOM = 0.637; +// A column crosses a frame line when this many of its pixels in the strip +// are the frame's cyan. +constexpr size_t MIN_LINE_PIXELS = 3; +// A tile frame is 0.214 of the screen wide. +constexpr double MIN_FRAME_WIDTH = 0.18; +constexpr double MAX_FRAME_WIDTH = 0.25; + +// The band above the row where HOME writes the selected game's name. The +// name starts 0.005 to 0.01 left of the frame, right of a game card icon +// that ends 0.017 or more left of it. +constexpr double NAME_TOP = 0.19; +constexpr double NAME_HEIGHT = 0.06; +constexpr double NAME_LEAD = 0.015; +constexpr double NAME_RIGHT = 0.98; +// The name's cyan (ARGB), dark text on HOME's light background once +// filtered: red low, green and blue high. +constexpr uint32_t NAME_MINS = 0xff000096; +constexpr uint32_t NAME_MAXS = 0xff96ffff; + +// The frame's cyan, which pulses between about (0, 187, 229) and +// (52, 249, 229), with lighter anti-aliased edges. +bool is_frame_cyan(uint32_t pixel){ + const int red = (pixel >> 16) & 0xff; + const int green = (pixel >> 8) & 0xff; + const int blue = pixel & 0xff; + return green >= 160 && blue >= 160 && green - red >= 60 && blue - red >= 60; +} + +// Per column of `strip`, whether it crosses a frame line. +std::vector frame_line_columns(const ImageViewRGB32& strip){ + std::vector columns(strip.width(), false); + for (size_t x = 0; x < strip.width(); x++){ + size_t cyan = 0; + for (size_t y = 0; y < strip.height(); y++){ + cyan += is_frame_cyan(strip.pixel(x, y)) ? 1 : 0; + } + columns[x] = cyan >= MIN_LINE_PIXELS; + } + return columns; +} + +// The longest run of columns that cross both frame lines, as [begin, end). +std::pair longest_framed_run(const std::vector& top, const std::vector& bottom){ + std::pair best{0, 0}; + size_t begin = 0; + for (size_t x = 0; x <= top.size(); x++){ + const bool framed = x < top.size() && x < bottom.size() && top[x] && bottom[x]; + if (framed){ + continue; + } + if (x - begin > best.second - best.first){ + best = {begin, x}; + } + begin = x + 1; + } + return best; +} + +} // namespace + + +std::optional find_selected_home_tile(const ImageViewRGB32& screen){ + if (screen.width() == 0 || screen.height() == 0){ + return std::nullopt; + } + const std::pair run = longest_framed_run( + frame_line_columns(extract_box_reference(screen, FRAME_TOP_STRIP)), + frame_line_columns(extract_box_reference(screen, FRAME_BOTTOM_STRIP)) + ); + const double width = (double)screen.width(); + const double frame_width = (double)(run.second - run.first) / width; + if (frame_width < MIN_FRAME_WIDTH || frame_width > MAX_FRAME_WIDTH){ + return std::nullopt; + } + return ImageFloatBox((double)run.first / width, FRAME_TOP, frame_width, FRAME_BOTTOM - FRAME_TOP); +} + +std::optional read_selected_home_tile_name(Language language, const ImageViewRGB32& screen){ + const std::optional frame = find_selected_home_tile(screen); + if (!frame){ + return std::nullopt; + } + const double left = std::max(0.0, frame->x - NAME_LEAD); + const ImageViewRGB32 band = extract_box_reference(screen, ImageFloatBox(left, NAME_TOP, NAME_RIGHT - left, NAME_HEIGHT)); + const ImageRGB32 text = to_blackwhite_rgb32_range(band, true, NAME_MINS, NAME_MAXS); + return OCR::ocr_read(language, text); +} + +bool selected_home_tile_is(Language language, const ImageViewRGB32& screen, std::string_view name){ + const std::optional read = read_selected_home_tile_name(language, screen); + if (!read){ + return false; + } + const std::u32string wanted = OCR::normalize_utf32(std::string(name)); + return !wanted.empty() && OCR::normalize_utf32(*read).find(wanted) != std::u32string::npos; +} + + + +namespace{ + +// The names HOME shows for the two games on console B's frames. +constexpr std::string_view FIRERED = "Pokémon FireRed Version"; +constexpr std::string_view LEGENDS_ZA = "Pokémon Legends: Z-A"; + +// Where the first tile's frame starts, and the step to the next tile, as +// parts of the screen's width, before the row scrolls. +constexpr double FIRST_TILE_LEFT = 0.076; +constexpr double TILE_STEP = 0.211; +constexpr double TILE_TOLERANCE = 0.02; + +struct HomeTileCase{ + struct Input{ + std::string_view image; + }; + struct Expectation{ + // The selected tile's place in the row, from 0; nothing: no tile + // is selected. + std::optional tile = std::nullopt; + // The name the selected tile shows: FIRERED or LEGENDS_ZA. + std::string_view game = {}; + }; + std::string_view name; + Input input; + Expectation expectation; +}; + +// Console B, 2026-11-14: HOME after the order changed (the game played +// last moves to the first tile), and the user select over HOME. +const std::vector HOME_TILE_CASES = { + {.name = "FireRed on the first tile 1", + .input = {.image = "NintendoSwitch/HomeTileReader/FireRedFirstTile_1.png"}, + .expectation = {.tile = 0, .game = FIRERED}}, + {.name = "FireRed on the first tile 2", + .input = {.image = "NintendoSwitch/HomeTileReader/FireRedFirstTile_2.png"}, + .expectation = {.tile = 0, .game = FIRERED}}, + {.name = "FireRed on the first tile 3", + .input = {.image = "NintendoSwitch/HomeTileReader/FireRedFirstTile_3.png"}, + .expectation = {.tile = 0, .game = FIRERED}}, + {.name = "Legends Z-A on the first tile", + .input = {.image = "NintendoSwitch/HomeTileReader/LegendsZAFirstTile.png"}, + .expectation = {.tile = 0, .game = LEGENDS_ZA}}, + {.name = "Legends Z-A on the second tile", + .input = {.image = "NintendoSwitch/HomeTileReader/LegendsZASecondTile.png"}, + .expectation = {.tile = 1, .game = LEGENDS_ZA}}, + {.name = "user select over HOME", + .input = {.image = "NintendoSwitch/HomeTileReader/UserSelectOverHome.png"}, + .expectation = {}}, +}; + +// What went wrong with the frame on `screen`; empty when it is where `test` expects. +std::string check_frame(const HomeTileCase& test, const ImageViewRGB32& screen){ + const std::optional frame = find_selected_home_tile(screen); + if (!test.expectation.tile){ + return frame ? std::format("Expected no selected tile, found a frame at x={:.3f}.", frame->x) : ""; + } + if (!frame){ + return std::format("Expected a frame on tile {}, found none.", *test.expectation.tile); + } + const double expected_left = FIRST_TILE_LEFT + TILE_STEP * (double)*test.expectation.tile; + if (std::abs(frame->x - expected_left) > TILE_TOLERANCE){ + return std::format("Expected the frame on tile {} (x={:.3f}), found it at x={:.3f}.", *test.expectation.tile, expected_left, frame->x); + } + return ""; +} + +// What went wrong with the names on `screen`: it must be the game `test` +// expects and not the other one. +std::string check_names(const HomeTileCase& test, const ImageViewRGB32& screen){ + const std::string read = read_selected_home_tile_name(Language::English, screen).value_or(""); + for (std::string_view game : {FIRERED, LEGENDS_ZA}){ + const bool expected = game == test.expectation.game; + if (selected_home_tile_is(Language::English, screen, game) != expected){ + return std::format("Expected the tile {} \"{}\", read \"{}\".", expected ? "to be" : "not to be", game, read); + } + } + return ""; +} + +class Test_HomeTileReader : public ImageUnitTest{ +public: + explicit Test_HomeTileReader(const HomeTileCase& test) + : ImageUnitTest(std::format("NintendoSwitch::HomeTileReader - {}", test.name), {std::string(test.input.image)}) + , m_case(test) + { + // Plus the PaddleOCR (ONNX) recognition session and its working set. + m_memory += PADDLE_OCR_SINGLE_LINE_MEMORY_ESTIMATE; + } + + virtual UnitTestResult run_on_images( + Logger&, CancellableScope&, + const std::vector& images + ) const override{ + std::string error = check_frame(m_case, images[0]); + if (error.empty()){ + error = check_names(m_case, images[0]); + } + return error.empty() ? UnitTestResult(true) : UnitTestResult(error); + } + +private: + HomeTileCase m_case; +}; + +} // namespace + + +void add_tests_HomeTileReader(UnitTestDatabase& database){ + for (const HomeTileCase& test : HOME_TILE_CASES){ + database.add(test); + } +} + + + +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_HomeTileReader.h b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_HomeTileReader.h new file mode 100644 index 0000000000..c0c57f1052 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_HomeTileReader.h @@ -0,0 +1,55 @@ +/* Home Tile Reader + * + * From: https://github.com/PokemonAutomation/ + * + * Which game tile the HOME menu's cursor is on. HOME draws a cyan frame + * around the selected tile of the game row and writes that game's name + * above the row, also in cyan. The frame says that a tile is selected and + * where; the name says which game it is. Neither depends on the order of + * the row, which changes as games are played (the last one played moves + * to the first tile). + * + * Measured on the Switch 1 HOME menu at 1920x1080; positions are parts of + * the screen, so other capture sizes read the same. + * + */ + +#ifndef PokemonAutomation_NintendoSwitch_HomeTileReader_H +#define PokemonAutomation_NintendoSwitch_HomeTileReader_H + +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/Language.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + + +// The selected tile's cyan frame on `screen`; nothing when no tile of the +// game row is selected (the cursor on the icons below it, a dialog or the +// user select over HOME). +std::optional find_selected_home_tile(const ImageViewRGB32& screen); + +// The name HOME writes above the selected tile, as OCR reads it in +// `language` (the console's, not the game's); nothing when no tile is +// selected. +std::optional read_selected_home_tile_name(Language language, const ImageViewRGB32& screen); + +// Whether a tile is selected and its name contains `name` ("Pokémon +// Legends: Z-A", or a distinctive part of it), compared as OCR text: +// case, accents, spaces and punctuation ignored. +bool selected_home_tile_is(Language language, const ImageViewRGB32& screen, std::string_view name); + + +// The screenshot tests in CommandLineTests/NintendoSwitch/HomeTileReader/. +void add_tests_HomeTileReader(UnitTestDatabase& database); + + + +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp index 37951460a9..b08bee33a4 100644 --- a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp @@ -4,168 +4,277 @@ * */ -#include #include -#include -#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include +#include +#include +#include +#include +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" #include "CommonFramework/ImageTools/ImageStats.h" -#include "CommonFramework/VideoPipeline/VideoFeed.h" #include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" #include "CommonTools/Images/SolidColorTest.h" -#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "Tests/ImageUnitTest.h" #include "NintendoSwitch_SystemKeyboardDetector.h" namespace PokemonAutomation{ namespace NintendoSwitch{ -using namespace std::chrono_literals; +namespace{ + +// Centres of the five rows of keys, and of the first column and the spacing +// between columns, as fractions of the screen. +constexpr double KEY_ROW_Y[] = {0.500, 0.593, 0.681, 0.770, 0.858}; +constexpr double KEY_COL_X0 = 0.077; +constexpr double KEY_COL_STEP = 0.075; + +// In the right column, Return spans rows 1-2 and OK rows 3-4. These are the +// gaps between those keys. +constexpr double RIGHT_COLUMN_RETURN_TOP = 0.548; +constexpr double RIGHT_COLUMN_OK_TOP = 0.725; + +// The cursor border and everything else the reader looks at, from the top of the +// digit row to the bottom of the bottom row. +const ImageFloatBox KEYS_AREA(0.0, 0.43, 1.0, 0.49); + +// The cursor border pulses between bright cyan (about 50, 240, 220) and a darker +// teal (about 0, 185, 210), measured on Legends: Z-A. The blue of the OK button +// and of the ABC underline (40, 90, 250) is not green enough to match. +bool is_cursor_pixel(uint32_t pixel){ + const int red = (pixel >> 16) & 0xff; + const int green = (pixel >> 8) & 0xff; + const int blue = pixel & 0xff; + return red < 130 && green > 160 && blue > 170 && green - red > 60; +} + +uint8_t nearest_row(double y){ + uint8_t row = 0; + for (uint8_t r = 1; r < 5; r++){ + if (std::abs(KEY_ROW_Y[r] - y) < std::abs(KEY_ROW_Y[row] - y)){ + row = r; + } + } + return row; +} +// The key whose cursor border has its centre at (x, y), or nothing if that is +// off the keyboard. +std::optional key_at(double x, double y){ + const long col = std::lround((x - KEY_COL_X0) / KEY_COL_STEP); + if (col < 0 || col > 11){ + return std::nullopt; + } + if (col < 11){ + return SystemKeyboardKey{nearest_row(y), (uint8_t)col}; + } + if (y < RIGHT_COLUMN_RETURN_TOP){ + return SystemKeyboardKey{0, 11}; + } + if (y < RIGHT_COLUMN_OK_TOP){ + return SystemKeyboardKey{1, 11}; + } + return SystemKeyboardKey{3, 11}; +} + +} // namespace -// Key grid of the QWERTY layout and where each key sits: rows "1".."z", columns "1".."-". -static const std::string KEY_ROWS[] = {"1234567890-", "qwertyuiop/", "asdfghjkl:'", "zxcvbnm,.?!"}; -static constexpr double KEY_ROW_Y[] = {0.500, 0.593, 0.681, 0.770}; -static constexpr double KEY_COL_X0 = 0.077; -static constexpr double KEY_COL_STEP = 0.075; SystemKeyboardDetector::SystemKeyboardDetector(Color color) : m_color(color) - , m_key_panel(0.15, 0.555, 0.65, 0.020) + , m_key_panel_upper(0.15, 0.532, 0.65, 0.036) + , m_key_panel_lower(0.15, 0.715, 0.65, 0.032) , m_bottom_bar(0.10, 0.935, 0.35, 0.030) , m_ok_button(0.875, 0.760, 0.045, 0.020) {} void SystemKeyboardDetector::make_overlays(VideoOverlaySet& items) const{ - items.add(m_color, m_key_panel); + items.add(m_color, m_key_panel_upper); + items.add(m_color, m_key_panel_lower); items.add(m_color, m_bottom_bar); items.add(m_color, m_ok_button); } bool SystemKeyboardDetector::detect(const ImageViewRGB32& screen){ - if (!is_white(extract_box_reference(screen, m_key_panel)) || - !is_white(extract_box_reference(screen, m_bottom_bar)) - ){ + // Each strip crosses white keys and the slightly brighter gaps between them, + // which gives a stddev sum of about 11 (measured on BDSP and Legends: Z-A). + const double max_panel_stddev = 20; + const bool panel = + is_white(extract_box_reference(screen, m_key_panel_upper), 500, max_panel_stddev) || + is_white(extract_box_reference(screen, m_key_panel_lower), 500, max_panel_stddev); + if (!panel || !is_white(extract_box_reference(screen, m_bottom_bar))){ return false; } - // OK is blue, or flat grey while the field is empty (disabled). + // OK is blue (40, 93, 255), or flat grey (221, 221, 221) while the field is + // empty (disabled). Measured on BDSP and Legends: Z-A. const ImageStats ok = image_stats(extract_box_reference(screen, m_ok_button)); - return is_solid(ok, {0.172, 0.276, 0.552}) || is_grey(ok, 600, 690); + return is_solid(ok, {0.103, 0.240, 0.657}) || is_grey(ok, 600, 690); } -bool find_system_keyboard_cursor(const ImageViewRGB32& screen, int& row, int& col){ +std::optional read_system_keyboard_cursor(const ImageViewRGB32& screen){ // The cursor is a cyan border around one key. Neighbouring keys overlap that border, // so take the centroid of all cyan pixels and map it to the nearest key. - const ImageFloatBox area(0.02, 0.44, 0.84, 0.38); - ImageViewRGB32 keys = extract_box_reference(screen, area); + const ImageViewRGB32 keys = extract_box_reference(screen, KEYS_AREA); double count = 0, sum_x = 0, sum_y = 0; for (size_t y = 0; y < keys.height(); y++){ for (size_t x = 0; x < keys.width(); x++){ - const uint32_t pixel = keys.pixel(x, y); - const int red = (pixel >> 16) & 0xff; - const int green = (pixel >> 8) & 0xff; - const int blue = pixel & 0xff; - if (red < 150 && green > 200 && blue > 190){ + if (is_cursor_pixel(keys.pixel(x, y))){ count++; sum_x += (double)x; sum_y += (double)y; } } } - // The border is about 0.15% of the screen; a few stray pixels are not a cursor. + // The border is about 0.18% of the screen; a few stray pixels are not a cursor. if (count < 0.0005 * screen.width() * screen.height()){ - return false; + return std::nullopt; } - const double cx = area.x + sum_x / count / keys.width() * area.width; - const double cy = area.y + sum_y / count / keys.height() * area.height; - col = (int)std::lround((cx - KEY_COL_X0) / KEY_COL_STEP); - row = 0; - for (int r = 1; r < 4; r++){ - if (std::abs(KEY_ROW_Y[r] - cy) < std::abs(KEY_ROW_Y[row] - cy)){ - row = r; - } - } - return 0 <= col && col < 11; + return key_at( + KEYS_AREA.x + sum_x / count / keys.width() * KEYS_AREA.width, + KEYS_AREA.y + sum_y / count / keys.height() * KEYS_AREA.height + ); } bool system_keyboard_field_has_text(const ImageViewRGB32& screen){ - // Left part of the field, after the caret's position on an empty field. - const ImageStats stats = image_stats(extract_box_reference(screen, ImageFloatBox(0.11, 0.235, 0.19, 0.030))); - return stats.stddev.sum() > 30; + // Left part of the backspace key, clear of its icon and of the cursor border. + // Light grey (221) with an empty field, dark grey (52) with text. + const ImageStats stats = image_stats(extract_box_reference(screen, ImageFloatBox(0.878, 0.470, 0.008, 0.060))); + return stats.average.sum() < 400; } +size_t system_keyboard_text_pixels(const ImageViewRGB32& screen){ + // The field, above its underline, which is white in some games. + const ImageViewRGB32 field = extract_box_reference(screen, ImageFloatBox(0.03, 0.17, 0.87, 0.105)); + size_t count = 0; + for (size_t y = 0; y < field.height(); y++){ + for (size_t x = 0; x < field.width(); x++){ + const uint32_t pixel = field.pixel(x, y); + if (((pixel >> 16) & 0xff) > 190 && ((pixel >> 8) & 0xff) > 190 && (pixel & 0xff) > 190){ + count++; + } + } + } + return count; +} + + namespace{ -// Moves the keyboard's cursor onto (target_row, target_col). A button -// pressed while the keyboard slides in is ignored, so read the cursor again -// after each move until it is on the target. Whether it got there. -bool move_keyboard_cursor(VideoStream& stream, ProControllerContext& context, int target_row, int target_col){ - for (size_t attempt = 0; attempt < 10; attempt++){ - context.wait_for_all_requests(); - int row = -1; - int col = -1; - VideoSnapshot screen = stream.video().snapshot(); - if (!screen || !find_system_keyboard_cursor(screen, row, col)){ - pbf_wait(context, 300ms); - continue; +// One screenshot and what each function should read from it. +class Test_SystemKeyboardDetector : public ImageUnitTest{ +public: + // `cursor`: the key under the cursor, or nothing for a screen without the keyboard. + Test_SystemKeyboardDetector(const std::string& image, std::optional cursor, bool has_text) + : ImageUnitTest(std::format("NintendoSwitch::SystemKeyboardDetector - {}", image), {image}) + , m_cursor(cursor) + , m_has_text(has_text) + {} + + virtual UnitTestResult run_on_images( + Logger& logger, CancellableScope& scope, + const std::vector& images + ) const override{ + const ImageRGB32& image = images[0]; + const bool keyboard = SystemKeyboardDetector().detect(image); + if (keyboard != m_cursor.has_value()){ + return std::format("detect() returned {}.", keyboard); } - if (row == target_row && col == target_col){ + if (!keyboard){ return true; } - for (; row < target_row; row++){ - pbf_press_dpad(context, DPAD_DOWN, 100ms, 200ms); + const std::optional cursor = read_system_keyboard_cursor(image); + if (cursor != m_cursor){ + return cursor + ? std::format("Read the cursor at ({}, {}).", cursor->row, cursor->col) + : std::format("Read no cursor."); } - for (; row > target_row; row--){ - pbf_press_dpad(context, DPAD_UP, 100ms, 200ms); + const bool has_text = system_keyboard_field_has_text(image); + if (has_text != m_has_text){ + return std::format("system_keyboard_field_has_text() returned {}.", has_text); } - for (; col < target_col; col++){ - pbf_press_dpad(context, DPAD_RIGHT, 100ms, 200ms); + // An empty field has no white text pixels; one typed character has many. + const size_t text_pixels = system_keyboard_text_pixels(image); + if ((text_pixels >= 20) != m_has_text){ + return std::format("system_keyboard_text_pixels() returned {}.", text_pixels); } - for (; col > target_col; col--){ - pbf_press_dpad(context, DPAD_LEFT, 100ms, 200ms); + return true; + } + +private: + std::optional m_cursor; + bool m_has_text; +}; + +std::string test_image_path(std::string_view image){ + return std::format("NintendoSwitch/SystemKeyboardDetector/{}", image); +} + +// The cursor on keys other than "1". +void add_cursor_tests(UnitTestDatabase& database){ + // Legends: Z-A, English and Spanish prompts. The + // border pulses between bright cyan and a darker teal; T, / and ! are dark. + const std::pair cursors[] = { + {"PLZA-Cursor-T.png", {1, 4}}, + {"PLZA-Cursor-Slash.png", {1, 10}}, + {"PLZA-Cursor-Exclamation.png", {3, 10}}, + {"PLZA-Cursor-0.png", {0, 9}}, + {"PLZA-Cursor-Minus.png", {0, 10}}, + {"PLZA-Cursor-Backspace.png", {0, 11}}, + {"PLZA-Cursor-Return.png", {1, 11}}, + {"PLZA-Cursor-OK.png", {3, 11}}, + {"PLZA-Cursor-Globe.png", {4, 0}}, + {"PLZA-Cursor-NTilde.png", {4, 5}}, + {"PLZA-Cursor-Space.png", {4, 8}}, + }; + for (const auto& [image, cursor] : cursors){ + database.add(test_image_path(image), cursor, false); + } +} + +// Tests whose screen is not captured yet. +void add_planned_tests(UnitTestDatabase& database){ + const SystemKeyboardKey key_1{0, 0}; + // Screens still to be captured: AZERTY needs a console whose system language + // uses it (the layout follows the system, not the game). Each test is added + // once its image is in the test repo. + struct Planned{ + std::string_view image; + std::optional cursor; + bool has_text; + }; + const Planned planned[] = { + {"AZERTY-Empty.png", key_1, false}, + {"PLZA-Korean.png", std::nullopt, false}, + }; + for (const Planned& test : planned){ + const std::string image = test_image_path(test.image); + if (Filesystem::exists(std::format("{}{}", UNIT_TEST_RESOURCE_PATH(), image))){ + database.add(image, test.cursor, test.has_text); } - pbf_wait(context, 300ms); } - return false; } -} // namespace +} // namespace -void type_on_system_keyboard(VideoStream& stream, ProControllerContext& context, const std::string& text){ - for (char ch : text){ - const char key = (char)std::tolower((unsigned char)ch); - int target_row = -1; - int target_col = -1; - for (int r = 0; r < 4 && target_row < 0; r++){ - const size_t p = KEY_ROWS[r].find(key); - if (p != std::string::npos && key != '\0'){ - target_row = r; - target_col = (int)p; - } - } - if (target_row < 0){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - std::string("System keyboard: no key for '") + ch + "'.", - stream - ); - } - if (!move_keyboard_cursor(stream, context, target_row, target_col)){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - std::string("System keyboard: the cursor did not reach '") + key + "'.", - stream - ); - } - if (std::isupper((unsigned char)ch)){ - pbf_press_button(context, BUTTON_LCLICK, 100ms, 250ms); - } - pbf_press_button(context, BUTTON_A, 80ms, 300ms); +void add_tests_SystemKeyboardDetector(UnitTestDatabase& database){ + const SystemKeyboardKey key_1{0, 0}; + // The OK button is blue with text in the field and grey without. + database.add(test_image_path("BDSP-PlayerName-Empty.png"), key_1, false); + database.add(test_image_path("BDSP-FriendName-Typed.png"), key_1, true); + for (std::string_view language : {"English", "French", "German", "Italian", "Japanese", "Spanish"}){ + database.add(test_image_path(std::format("PLZA-{}-Empty.png", language)), key_1, false); + } + for (std::string_view image : {"BDSP-Dialog.png", "HomeMenu.png", "HomeMenu-OnlineLicensePopup.png", "UserSelect.png"}){ + database.add(test_image_path(image), std::nullopt, false); } - context.wait_for_all_requests(); + add_cursor_tests(database); + add_planned_tests(database); } -} // namespace NintendoSwitch -} // namespace PokemonAutomation +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h index 6091d9a14f..3a5bbf7f03 100644 --- a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h +++ b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h @@ -2,28 +2,37 @@ * * From: https://github.com/PokemonAutomation/ * - * The Switch's own software keyboard, which games open for names (e.g. the - * player's name in BDSP): detect it, find the key under its cursor, and type - * a string by moving the cursor and checking where it landed. + * The Switch's own on-screen keyboard, which games open for names (e.g. the + * player's name in BDSP and in Legends: Z-A): detect it, read the key under its + * cursor, and tell whether its text field is empty. Typing with it is in + * Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h. * - * Measured on 1920x1080 captures of the QWERTY layout. + * Supported: the Latin keyboard (QWERTY; AZERTY shares the key grid but has no + * test screenshot yet). Its layout follows the console's system language, not + * the game's. Measured on 1920x1080 Switch 1 captures of BDSP's and Legends: + * Z-A's name entry, the latter with the game in English, Spanish, German, + * Italian, French and Japanese (all QWERTY on an English console). Not + * supported: the Korean keyboard, whose candidate strip + * moves the keys down (the detector returns false on it), and probably the + * Chinese ones (not tested). Switch 2 is not tested. * */ #ifndef PokemonAutomation_NintendoSwitch_SystemKeyboardDetector_H #define PokemonAutomation_NintendoSwitch_SystemKeyboardDetector_H -#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "CommonFramework/ImageTools/ImageBoxes.h" -#include "CommonFramework/Tools/VideoStream.h" #include "CommonTools/VisualDetector.h" -#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" namespace PokemonAutomation{ namespace NintendoSwitch{ -// White key panel and bottom bar, and the blue OK button. +// The white key panel, the white bar below it, and the OK button (blue, or grey +// while the field is empty). Works wherever the cursor is. class SystemKeyboardDetector : public StaticScreenDetector{ public: SystemKeyboardDetector(Color color = COLOR_CYAN); @@ -33,11 +42,16 @@ class SystemKeyboardDetector : public StaticScreenDetector{ private: Color m_color; - ImageFloatBox m_key_panel; + // Two strips across the key panel, one over the gap between rows 0 and 1 and + // one over the gap between rows 2 and 3. The cursor's border reaches into at + // most one of them, so the panel is white if either is. + ImageFloatBox m_key_panel_upper; + ImageFloatBox m_key_panel_lower; ImageFloatBox m_bottom_bar; ImageFloatBox m_ok_button; }; -// Periodic watcher for the Switch system on-screen keyboard. + +// Finds SystemKeyboardDetector on the video, checking every 250 ms. class SystemKeyboardWatcher : public DetectorToFinder{ public: SystemKeyboardWatcher(Color color = COLOR_CYAN) @@ -46,23 +60,40 @@ class SystemKeyboardWatcher : public DetectorToFinder{ }; -// The key under the cursor, as row (0: digits .. 3: "zxcv") and column (0..10). +// A key of the keyboard. +// Rows 0-3 are the character rows (0: digits .. 3: "zxcv"), row 4 is the bottom +// row (globe, Shift, ABC, #+=, the accent pages and Space). +// Columns 0-10 are the character columns. Column 11 is the right column, whose +// keys are taller: backspace is (0, 11), Return (1, 11) and OK (3, 11). +// A row-4 key reports the character column nearest its centre (Space: 8). +struct SystemKeyboardKey{ + uint8_t row; + uint8_t col; + + bool operator==(const SystemKeyboardKey&) const = default; +}; + +// The key under the cursor, or nothing if no cursor border is visible. // The keyboard remembers the cursor between uses, so read it, don't assume it. -// Returns false if no cursor border is visible. -bool find_system_keyboard_cursor(const ImageViewRGB32& screen, int& row, int& col); +std::optional read_system_keyboard_cursor(const ImageViewRGB32& screen); -// Whether the text field holds any text. Pressing B on an empty field closes the +// Whether the text field holds any text: the backspace key is dark then and light +// grey while the field is empty. Pressing B on an empty field closes the // keyboard, so delete only while this is true. bool system_keyboard_field_has_text(const ImageViewRGB32& screen); -// Types `text` (letters, digits and the symbols on the QWERTY grid). For each character, -// reads the cursor, moves it with the D-pad and checks it arrived before pressing A; an -// uppercase letter is typed with Shift (left stick click). Throws, with a screenshot, if -// the cursor can't be read or doesn't arrive. -void type_on_system_keyboard(VideoStream& stream, ProControllerContext& context, const std::string& text); +// The number of white pixels in the text field. Each typed character adds some +// (a "1" adds about 110 at 1920x1080), so comparing two counts tells whether a +// key press reached the field. +size_t system_keyboard_text_pixels(const ImageViewRGB32& screen); + + + +// Register the tests of this file. +void add_tests_SystemKeyboardDetector(UnitTestDatabase& database); -} // namespace NintendoSwitch -} // namespace PokemonAutomation +} // namespace NintendoSwitch +} // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h index 7048582ea0..1aa290cdc1 100644 --- a/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h +++ b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h @@ -8,10 +8,12 @@ #define PokemonAutomation_NintendoSwitch_KeyboardCodeEntry_H #include +#include #include "Controllers/StandardHid/StandardHid_Keyboard.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" #include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardEntryMappings.h" +#include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_CodeEntryTools.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -19,6 +21,13 @@ namespace FastCodeEntry{ +// The D-pad moves from "source" to "destination" on the on-screen keyboard, +// wrapping around the sides when that is shorter, then ENTER_CHAR. +std::vector keyboard_get_path( + KeyboardEntryPosition source, + KeyboardEntryPosition destination +); + // Automatically select between keyboard or pro controller depending on the // controller type. void keyboard_enter_code( diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.cpp b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.cpp new file mode 100644 index 0000000000..511e36ecad --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.cpp @@ -0,0 +1,249 @@ +/* System Keyboard Typing + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/NintendoSwitch_Settings.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h" +#include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_CodeEntryTools.h" +#include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardCodeEntry.h" +#include "NintendoSwitch_SystemKeyboardTyping.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace FastCodeEntry{ + +using namespace std::chrono_literals; + + +namespace{ + +// Waits after each press. They are longer than the code entry delays because +// the next step reads the screen: the cursor animation and the capture latency +// must be over first. +constexpr Milliseconds MOVE_RELEASE = 200ms; +constexpr Milliseconds WRAP_RELEASE = 400ms; // Wrapping around a side animates for longer. +constexpr Milliseconds SETTLE = 300ms; // Before reading the cursor after moving it. +constexpr Milliseconds SHIFT_RELEASE = 250ms; +constexpr Milliseconds KEY_RELEASE = 300ms; + +// A press made while the keyboard is still sliding in is ignored, so the +// cursor is read again after each try until it is on the target. +constexpr size_t MAX_MOVE_TRIES = 10; + +// Reads of the text field after a key press, SETTLE apart, before giving up. +constexpr size_t MAX_FIELD_READS = 3; + +// The fewest white pixels a typed character adds to the field ("." or "'"). +constexpr size_t MIN_CHARACTER_PIXELS = 10; + + +// The key of each character of `text`. Throws UserSetupError, listing every +// character that has no key, before anything is typed. +std::vector text_keys(Logger& logger, const std::string& text, KeyboardLayout layout){ + const std::map& positions = KEYBOARD_POSITIONS(layout); + std::vector keys; + std::string missing; + for (char ch : text){ + // Letters are listed under their uppercase key. + auto iter = positions.find((char)std::toupper((unsigned char)ch)); + if (iter == positions.end()){ + missing += ch; + }else{ + keys.emplace_back(iter->second); + } + } + if (!missing.empty()){ + throw UserSetupError( + logger, + std::format("System keyboard: \"{}\" has characters with no key on the keyboard: \"{}\".", text, missing) + ); + } + return keys; +} + +// How long to hold each button: the hold of the code entry timings for Switch 1. +Milliseconds button_hold(ProControllerContext& context){ + const ConsoleSettings& settings = ConsoleSettings::instance(); + const CodeEntryDelays delays = context->performance_class() == ControllerPerformanceClass::SerialPABotBase_Wired + ? CodeEntryDelays(settings.CODEBOARD_ENTRY_SWITCH1_WIRED) + : CodeEntryDelays(settings.CODEBOARD_ENTRY_SWITCH1_WIRELESS); + return delays.hold; +} + +// The D-pad direction of a move. +DpadPosition move_direction(CodeEntryAction action){ + switch (action){ + case CodeEntryAction::NORM_MOVE_UP: + case CodeEntryAction::WRAP_MOVE_UP: + return DpadPosition::DPAD_UP; + case CodeEntryAction::NORM_MOVE_RIGHT: + case CodeEntryAction::WRAP_MOVE_RIGHT: + return DpadPosition::DPAD_RIGHT; + case CodeEntryAction::NORM_MOVE_DOWN: + case CodeEntryAction::WRAP_MOVE_DOWN: + return DpadPosition::DPAD_DOWN; + case CodeEntryAction::NORM_MOVE_LEFT: + case CodeEntryAction::WRAP_MOVE_LEFT: + return DpadPosition::DPAD_LEFT; + default: + return DpadPosition::DPAD_NONE; + } +} + +// The moves toward `target`. Off the character grid (the right column or the +// bottom row), one step back onto it, after which the cursor is read again. +std::vector path_toward(SystemKeyboardKey cursor, KeyboardEntryPosition target){ + if (cursor.col == 11){ + return {CodeEntryAction::NORM_MOVE_LEFT}; + } + if (cursor.row == 4){ + return {CodeEntryAction::NORM_MOVE_UP}; + } + std::vector path = keyboard_get_path({cursor.row, cursor.col}, target); + std::erase_if(path, [](CodeEntryAction action){ return !is_move(action); }); + return path; +} + +// The moves as letters for the log, e.g. "DLL" or "R*" for a wrap to the right. +std::string path_to_string(const std::vector& path){ + std::string ret; + for (CodeEntryAction action : path){ + switch (move_direction(action)){ + case DpadPosition::DPAD_UP: + ret += 'U'; + break; + case DpadPosition::DPAD_RIGHT: + ret += 'R'; + break; + case DpadPosition::DPAD_DOWN: + ret += 'D'; + break; + case DpadPosition::DPAD_LEFT: + ret += 'L'; + break; + default: + ret += '?'; + break; + } + if (is_wrap(action)){ + ret += '*'; + } + } + return ret; +} + +std::optional read_cursor(VideoStream& stream, ProControllerContext& context){ + context.wait_for_all_requests(); + VideoSnapshot screen = stream.video().snapshot(); + if (!screen){ + return std::nullopt; + } + return read_system_keyboard_cursor(screen); +} + +[[noreturn]] void throw_cursor_failure( + VideoStream& stream, + std::optional last, KeyboardEntryPosition target, char ch +){ + std::string message = last + ? std::format("the cursor is at ({}, {}), not on '{}' at ({}, {})", last->row, last->col, ch, target.row, target.col) + : std::format("the cursor could not be read while moving to '{}' at ({}, {})", ch, target.row, target.col); + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("System keyboard: {}, after {} tries.", message, MAX_MOVE_TRIES), + stream + ); +} + +void move_cursor( + VideoStream& stream, ProControllerContext& context, Milliseconds hold, + KeyboardEntryPosition target, char ch +){ + std::optional last; + for (size_t attempt = 0; attempt < MAX_MOVE_TRIES; attempt++){ + const std::optional cursor = read_cursor(stream, context); + if (!cursor){ + stream.logger().log(std::format("System keyboard: no cursor seen, try {}.", attempt + 1)); + pbf_wait(context, SETTLE); + continue; + } + last = cursor; + if (cursor->row == target.row && cursor->col == target.col){ + return; + } + const std::vector path = path_toward(*cursor, target); + stream.logger().log(std::format( + "System keyboard: cursor ({}, {}) -> '{}' ({}, {}): {}, try {}.", + cursor->row, cursor->col, ch, target.row, target.col, path_to_string(path), attempt + 1 + )); + for (CodeEntryAction action : path){ + pbf_press_dpad(context, move_direction(action), hold, is_wrap(action) ? WRAP_RELEASE : MOVE_RELEASE); + } + pbf_wait(context, SETTLE); + } + throw_cursor_failure(stream, last, target, ch); +} + +size_t read_text_pixels(VideoStream& stream, ProControllerContext& context){ + context.wait_for_all_requests(); + VideoSnapshot screen = stream.video().snapshot(); + return screen ? system_keyboard_text_pixels(screen) : 0; +} + +// Types `ch` with the cursor on its key, and checks that the text field grew. +void press_key(VideoStream& stream, ProControllerContext& context, Milliseconds hold, char ch){ + const size_t before = read_text_pixels(stream, context); + if (std::isupper((unsigned char)ch)){ + pbf_press_button(context, BUTTON_LCLICK, hold, SHIFT_RELEASE); + } + pbf_press_button(context, BUTTON_A, hold, KEY_RELEASE); + size_t after = 0; + for (size_t read = 0; read < MAX_FIELD_READS; read++){ + after = read_text_pixels(stream, context); + if (after >= before + MIN_CHARACTER_PIXELS){ + return; + } + pbf_wait(context, SETTLE); + } + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::format("System keyboard: pressed '{}', but the text field did not change ({} -> {} white pixels).", ch, before, after), + stream + ); +} + +} // namespace + + + +void type_on_system_keyboard( + VideoStream& stream, ProControllerContext& context, + const std::string& text, + KeyboardLayout layout +){ + const std::vector keys = text_keys(stream.logger(), text, layout); + const Milliseconds hold = button_hold(context); + for (size_t c = 0; c < text.size(); c++){ + move_cursor(stream, context, hold, keys[c], text[c]); + press_key(stream, context, hold, text[c]); + } + context.wait_for_all_requests(); +} + + + +} // namespace FastCodeEntry +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h new file mode 100644 index 0000000000..3d2e2dcc2f --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h @@ -0,0 +1,56 @@ +/* System Keyboard Typing + * + * From: https://github.com/PokemonAutomation/ + * + * Types text on the Switch's on-screen keyboard with a Pro Controller, reading + * the cursor from the video before each key. Unlike keyboard_enter_code(), + * which moves blind from an assumed starting key, it starts wherever the + * keyboard left its cursor and corrects moves the keyboard ignored. + * + * Supported keyboards are those of NintendoSwitch_SystemKeyboardDetector.h: the + * Latin keyboard, QWERTY or AZERTY. Console-tested on Switch 1 (2026-11-13): + * the player's name in Legends: Z-A with the game in English, Spanish, German, + * Italian, French and Japanese (QWERTY, English system language), and earlier + * the player's and rival's names in BDSP. AZERTY is not tested. Not + * supported: the Korean and Chinese keyboards. Switch 2 is not tested. + * + */ + +#ifndef PokemonAutomation_NintendoSwitch_SystemKeyboardTyping_H +#define PokemonAutomation_NintendoSwitch_SystemKeyboardTyping_H + +#include +#include "CommonFramework/Tools/VideoStream.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_KeyboardEntryMappings.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace FastCodeEntry{ + + +// Types `text` on the open keyboard: letters, digits and the symbols on the +// keyboard's first page. An uppercase letter is typed with Shift (left stick +// click), which assumes the keyboard opened with Shift off, as BDSP's and +// Legends: Z-A's do. +// +// Before the first press, throws UserSetupError if `text` has a character with +// no key. While typing, throws OperationFailedException, with a screenshot, if +// the cursor can't be read or doesn't reach a key, or if a key press doesn't +// add to the text field. The check counts the field's white pixels, so it +// catches a dropped press but not a wrong case. +// +// Presses are held for the "Fast Code Entry Timings" hold of Switch 1 with the +// controller's connection type, as keyboard_enter_code() does. The waits after +// them are fixed, since each move is read back from the video. +void type_on_system_keyboard( + VideoStream& stream, ProControllerContext& context, + const std::string& text, + KeyboardLayout layout = KeyboardLayout::QWERTY +); + + +} // namespace FastCodeEntry +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_AuthoredCheckpointJson_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_AuthoredCheckpointJson_Tests.cpp index 147fa168a9..ae61bbc538 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_AuthoredCheckpointJson_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_AuthoredCheckpointJson_Tests.cpp @@ -33,24 +33,24 @@ enum class Failure{ MISSING_RESOURCE, }; -struct AuthoredInput{ - std::string name; - std::string json; - std::string game = "frlg"; - bool resource = false; - std::string file = "plan"; -}; +struct AuthoredCase{ + struct Input{ + std::string name; + std::string json; + std::string game = "frlg"; + bool resource = false; + std::string file = "plan"; + }; -struct AuthoredExpectation{ - std::string checkpoint = "00.05"; - std::string execution = R"({"menu":"before_pokedex"})"; - std::vector events = {"first", "second"}; - Failure failure = Failure::NONE; -}; + struct Expectation{ + std::string checkpoint = "00.05"; + std::string execution = R"({"menu":"before_pokedex"})"; + std::vector events = {"first", "second"}; + Failure failure = Failure::NONE; + }; -struct AuthoredCase{ - AuthoredInput input; - AuthoredExpectation expectation; + Input input; + Expectation expectation; }; const AuthoredCase CASES[] = { diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryDevFiles.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryDevFiles.cpp new file mode 100644 index 0000000000..8da82c99d2 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryDevFiles.cpp @@ -0,0 +1,38 @@ +/* Story Dev Files + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Common/Cpp/Filesystem/FileIO.h" +#include "Common/Cpp/Filesystem/Filesystem.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "Pokemon_StoryDevFiles.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +std::string story_dev_folder(std::string_view game){ + return std::format("{}AutoStoryDev/{}/", USER_FILE_PATH(), game); +} + +std::vector read_story_dev_files(std::string_view folder, std::string_view game){ + const Filesystem::Path directory(std::string{folder}); + std::vector files; + const std::string prefix = std::format("{}-", game); + for (const Filesystem::Path& path : Filesystem::list_directory(directory)){ + const std::string name = path.filename().string(); + if (!name.starts_with(prefix) || !name.ends_with(".plan.json") || !Filesystem::is_regular_file(path)){ + continue; + } + files.push_back({.name = name, .path = path.string(), .text = file_to_string(path.string())}); + } + return files; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryDevFiles.h b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryDevFiles.h new file mode 100644 index 0000000000..fd572d6567 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryDevFiles.h @@ -0,0 +1,41 @@ +/* Story Dev Files + * + * From: https://github.com/PokemonAutomation/ + * + * For lab tuning: checkpoint files on disk that a run reads in place of + * the built-in ones of the same name (load_story_data() in + * Pokemon_StoryFiles.h), so a walk can be retimed without a rebuild. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryDevFiles_H +#define PokemonAutomation_Pokemon_StoryDevFiles_H + +#include +#include +#include + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// A checkpoint file read from disk. +struct StoryDevFile{ + std::string name; // "plza-02.01.plan.json" + std::string path; + std::string text; +}; + +// The folder whose checkpoint files replace `game`'s built-in ones: +// /AutoStoryDev// (USER_FILE_PATH()). +std::string story_dev_folder(std::string_view game); + +// The -*.plan.json files in `folder`, by name; none if it does not +// exist. +std::vector read_story_dev_files(std::string_view folder, std::string_view game); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp new file mode 100644 index 0000000000..cb1824b759 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp @@ -0,0 +1,95 @@ +/* Story Files + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Pokemon_StoryFiles.h" + +namespace PokemonAutomation{ +namespace Pokemon{ +namespace detail{ + + +void check_dev_file_names(std::span builtin, std::span dev_files){ + for (const StoryDevFile& dev : dev_files){ + bool known = false; + for (const auto& [name, text] : builtin){ + known |= dev.name == std::format("{}.json", name); + } + if (!known){ + throw ParseException(std::format("{}: no built-in checkpoint file is named {}", dev.path, dev.name)); + } + } +} + +const StoryDevFile* find_dev_file(std::span dev_files, std::string_view builtin_name){ + const std::string file_name = std::format("{}.json", builtin_name); + for (const StoryDevFile& file : dev_files){ + if (file.name == file_name){ + return &file; + } + } + return nullptr; +} + +ParseException naming_path(const ParseException& error, std::string_view path){ + const std::string message = error.message(); + if (message.find(path) != std::string::npos){ + return error; + } + return ParseException(std::format("{}: {}", path, message)); +} + +void check_dev_checkpoint( + std::string_view path, std::string_view id, std::string_view builtin_id, const std::vector& problems +){ + if (id != builtin_id){ + throw ParseException(std::format("{}: checkpoint id {} does not match the built-in {}", path, id, builtin_id)); + } + if (!problems.empty()){ + throw ParseException(std::format("{}: {}", path, problems.front())); + } +} + +void check_in_story_order( + const std::function& in_story_order, std::string_view id, std::string_view source +){ + if (in_story_order && !in_story_order(id)){ + throw ParseException(std::format("{}: checkpoint {} is not in the game's story order", source, id)); + } +} + +void add_story_row( + std::string_view id, const StoryRowExecution& execution, RowSave start, + std::optional previous_id, std::string_view source, StoryRows& rows +){ + if (execution.reload != StoryReload::NEVER){ + if (start == RowSave::WITHOUT_SAVE){ + throw ParseException(std::format("{}: reload_when_chained on a checkpoint with no save to reload", source)); + } + if (!previous_id || !rows.autosaves.contains(*previous_id)){ + throw ParseException(std::format( + "{}: reload_when_chained after a checkpoint that does not end on the game's autosave: the reload would load an older save", + source + )); + } + rows.reload_when_chained.emplace(id); + } + if (execution.reload == StoryReload::ALWAYS){ + rows.reload_always.emplace(id); + } + if (execution.autosave){ + rows.autosaves.emplace(id); + } + if (execution.dev_only){ + rows.dev_only.emplace(id); + } +} + + + +} // namespace detail +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h new file mode 100644 index 0000000000..7b423becd6 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.h @@ -0,0 +1,222 @@ +/* Story Files + * + * From: https://github.com/PokemonAutomation/ + * + * A game's story read from its checkpoint files: the checked-in ones, + * built into the program (cmake/EmbedTextFiles.cmake), each replaced by + * the dev file of the same name if there is one (Pokemon_StoryDevFiles.h), + * so a walk can be retimed on the console without a rebuild. The game + * reads each file into its own checkpoint type; this reads the shared + * execution fields (Pokemon_StoryRowExecution.h) into the rows they mark, + * and checks what holds for every game: a dev file replaces a built-in + * file, keeps its checkpoint's id and passes the game's lint, and a row + * reloads only a save it can count on. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryFiles_H +#define PokemonAutomation_Pokemon_StoryFiles_H + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Pokemon_AuthoredCheckpointJson.h" +#include "Pokemon_StoryDevFiles.h" +#include "Pokemon_StoryRowExecution.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// What a game reads from one checkpoint file. +template +struct StoryFileRow{ + Checkpoint checkpoint; + StoryRowExecution execution; +}; + +// How a game reads its checkpoint files. +template +struct StoryFileReader{ + // The file `text`'s checkpoint and shared execution fields + // (read_story_row_execution()); `source` names the file in errors. + // Throws ParseException when it is malformed. + std::function(std::string_view text, std::string_view source)> read; + // The problems the game finds in a dev file's checkpoint; the first + // one refuses the file. None set: no lint. + std::function(const Checkpoint&)> lint = {}; + // Whether a row starts with no save to load (a new game): it may not + // reload. None set: every row starts from a save. + std::function starts_without_save = {}; + // Whether the game plays checkpoint `a` before `b`, when that is not + // the order of its files' names. None set: file-name order. + std::function plays_before = {}; + // Whether the game's story order lists checkpoint `id`: a file whose + // id it does not list is refused, not skipped or played out of place. + // None set: the game keeps no list, and every id plays. + std::function in_story_order = {}; +}; + + +// The story of the built-in checkpoint files `builtin`, in their order or +// the game's (StoryFileReader::plays_before), each of `dev_files` in place +// of the built-in file of its name. Throws ParseException naming the file +// when a file does not parse, a dev file matches no built-in file, changes +// its checkpoint's id or fails the game's lint, a file's checkpoint id is +// not in the game's story order (StoryFileReader::in_story_order), or a +// row reloads a save it cannot count on in the order the story plays. +template +StoryData load_story_data( + std::span builtin, std::span dev_files, + const StoryFileReader& reader +); + +// Who a run's rows are listed for. +enum class StoryRowsShown{ + USER, // every row but the dev-only ones + DEVELOPER, // every row (the program's Developer Mode) +}; + +// The rows of `story` a run may play, in order. The start and end +// dropdowns list these, so their indices name these rows. +template +std::vector story_rows_for_mode(const StoryData& story, StoryRowsShown shown); + +// The rows a run from `first` to `last` of `rows` plays, in order. Empty +// if `first` comes after `last` or `last` is past the end. +template +std::vector story_run_rows(std::span rows, size_t first, size_t last); + + + +namespace detail{ + +// Throws ParseException naming the dev file that matches no built-in file. +void check_dev_file_names(std::span builtin, std::span dev_files); +// The dev file that replaces the built-in file `builtin_name` (its name +// without ".json"); null if none. +const StoryDevFile* find_dev_file(std::span dev_files, std::string_view builtin_name); +// `error`, its message prefixed with `path` unless it already names it. +ParseException naming_path(const ParseException& error, std::string_view path); +// Throws ParseException naming `path` when the dev file's checkpoint `id` +// is not the built-in file's `builtin_id`, or `problems` is not empty. +void check_dev_checkpoint( + std::string_view path, std::string_view id, std::string_view builtin_id, const std::vector& problems +); + +// Throws ParseException naming `source` and `id` when `in_story_order` +// is set and does not list `id`. +void check_in_story_order( + const std::function& in_story_order, std::string_view id, std::string_view source +); + +// Whether a row starts from a save, which a reload can load. +enum class RowSave{ + FROM_SAVE, + WITHOUT_SAVE, // a new game +}; + +// Adds row `id` to the sets of `rows`. Throws ParseException naming +// `source` when it reloads and starts WITHOUT_SAVE or the row before it, +// `previous_id`, does not end on the game's autosave: a reload loads the +// last save, which must be that autosave. +void add_story_row( + std::string_view id, const StoryRowExecution& execution, RowSave start, + std::optional previous_id, std::string_view source, StoryRows& rows +); + +// The dev file `dev`'s row, in place of the built-in file `builtin_name`: +// it must parse, keep the built-in checkpoint's id and pass the lint. +template +StoryFileRow read_dev_file( + const StoryDevFile& dev, std::string_view builtin_text, std::string_view builtin_name, + const StoryFileReader& reader +){ + StoryFileRow row; + try{ + row = reader.read(dev.text, dev.path); + }catch (const ParseException& error){ + // Malformed JSON says nothing of where it came from. + throw naming_path(error, dev.path); + } + const std::string builtin_id = reader.read(builtin_text, builtin_name).checkpoint.id; + const std::vector problems = reader.lint ? reader.lint(row.checkpoint) : std::vector{}; + check_dev_checkpoint(dev.path, row.checkpoint.id, builtin_id, problems); + return row; +} + +} // namespace detail + + +template +StoryData load_story_data( + std::span builtin, std::span dev_files, + const StoryFileReader& reader +){ + detail::check_dev_file_names(builtin, dev_files); + StoryData data; + struct SourcedRow{ + StoryFileRow row; + std::string_view source; + }; + std::vector rows; + for (const auto& [name, text] : builtin){ + const StoryDevFile* dev = detail::find_dev_file(dev_files, name); + if (dev == nullptr){ + rows.push_back({reader.read(text, name), name}); + continue; + } + rows.push_back({detail::read_dev_file(*dev, text, name, reader), dev->path}); + data.dev_files.emplace_back(dev->name, dev->path); + } + for (const auto& [row, source] : rows){ + detail::check_in_story_order(reader.in_story_order, row.checkpoint.id, source); + } + if (reader.plays_before){ + std::stable_sort(rows.begin(), rows.end(), [&](const SourcedRow& a, const SourcedRow& b){ + return reader.plays_before(a.row.checkpoint, b.row.checkpoint); + }); + } + // A reload loads the save the row before it in play ends on. + for (auto& [row, source] : rows){ + const bool new_game = reader.starts_without_save && reader.starts_without_save(row.checkpoint); + detail::add_story_row( + row.checkpoint.id, row.execution, + new_game ? detail::RowSave::WITHOUT_SAVE : detail::RowSave::FROM_SAVE, + data.checkpoints.empty() ? std::nullopt : std::optional(data.checkpoints.back().id), + source, data + ); + data.checkpoints.push_back(std::move(row.checkpoint)); + } + return data; +} + +template +std::vector story_rows_for_mode(const StoryData& story, StoryRowsShown shown){ + std::vector rows; + for (const Checkpoint& row : story.checkpoints){ + if (shown == StoryRowsShown::DEVELOPER || !story.dev_only.contains(row.id)){ + rows.push_back(row); + } + } + return rows; +} + +template +std::vector story_run_rows(std::span rows, size_t first, size_t last){ + if (first > last || last >= rows.size()){ + return {}; + } + return std::vector(rows.begin() + first, rows.begin() + last + 1); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp new file mode 100644 index 0000000000..5ba2235da8 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp @@ -0,0 +1,383 @@ +/* Story Files Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon_StoryFiles.h" +#include "Pokemon_StoryFiles_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +// A checkpoint file of the test game: {"id", "title", "execution"}, its +// execution the shared fields and the game's own "note". Its lint refuses +// the title "unlinted"; 00.00 is its new game, which starts without a save. +std::string file_text(std::string_view id, std::string_view execution, std::string_view title = "Row"){ + return std::format(R"({{"id": "{}", "title": "{}", "execution": {}}})", id, title, execution); +} + +StoryFileRow read_test_file(std::string_view text, std::string_view source){ + const JsonValue value = parse_json(std::string(text)); + const JsonObject& object = value.to_object_throw(std::string(source)); + StoryFileRow row; + row.checkpoint.id = object.get_string_throw("id", std::string(source)); + row.checkpoint.title = object.get_string_throw("title", std::string(source)); + row.execution = read_story_row_execution(object.get_object_throw("execution", std::string(source)), source, {"note"}); + return row; +} + +const StoryFileReader TEST_READER{ + .read = read_test_file, + .lint = [](const StoryCheckpoint& checkpoint){ + return checkpoint.title == "unlinted" ? std::vector{"title is unlinted"} : std::vector{}; + }, + .starts_without_save = [](const StoryCheckpoint& checkpoint){ + return checkpoint.id == "00.00"; + }, +}; + +// "a b": the strings, space-separated. +std::string joined(const std::vector& texts){ + std::string text; + for (const std::string& part : texts){ + text += (text.empty() ? "" : " ") + part; + } + return text; +} +std::string joined(const std::set>& texts){ + return joined(std::vector(texts.begin(), texts.end())); +} + +std::vector ids(const std::vector& rows){ + std::vector result; + for (const StoryCheckpoint& row : rows){ + result.push_back(row.id); + } + return result; +} + +// "rows 01.00 01.01 | autosave 01.00 | chained 01.01 | always | dev_only | dev x.json" +std::string summary(const StoryData& data){ + std::vector dev; + for (const auto& [name, path] : data.dev_files){ + dev.push_back(std::format("{}@{}", name, path)); + } + std::vector titles; + for (const StoryCheckpoint& row : data.checkpoints){ + titles.push_back(row.title); + } + return std::format( + "rows {} | titles {} | autosave {} | chained {} | always {} | dev_only {} | dev {}", + joined(ids(data.checkpoints)), joined(titles), joined(data.autosaves), joined(data.reload_when_chained), + joined(data.reload_always), joined(data.dev_only), joined(dev) + ); +} + + +struct LoadCase{ + std::string name; + struct Input{ + std::vector builtin; + std::vector dev_files = {}; + // Whether the game plays its rows in id order, not file-name order. + bool ordered_by_id = false; + // The ids the game's story order lists; none: the game lists none. + std::optional> story_order = std::nullopt; + }; + Input input; + struct Expectation{ + // summary() of the story read; none when it fails. + std::optional summary = std::nullopt; + // What the ParseException's message contains when it fails. + std::string error = {}; + }; + Expectation expectation; +}; + +const EmbeddedJsonFile ROW_0 = {"test-01.00.plan", file_text("01.00", R"({"save": "autosave"})")}; +const EmbeddedJsonFile ROW_1 = {"test-01.01.plan", file_text("01.01", R"({"reload_when_chained": true, "note": 1})")}; +const EmbeddedJsonFile ROW_2 = {"test-01.02.plan", file_text("01.02", R"({"dev_only": true, "save": "menu"})")}; + +StoryDevFile dev_file(std::string_view name, std::string text){ + return StoryDevFile{.name = std::string(name), .path = std::format("/dev/{}", name), .text = std::move(text)}; +} + +// Named to come after 01.01 by file name, before it by id. +const EmbeddedJsonFile ROW_0B_MENU = {"test-01.10.plan", file_text("01.00b", R"({"save": "menu"})")}; + +const LoadCase LOAD_CASES[] = { + { + .name = "built-in files in order, their shared execution fields read", + .input = {.builtin = {ROW_0, ROW_1, ROW_2}}, + .expectation = {.summary = "rows 01.00 01.01 01.02 | titles Row Row Row | autosave 01.00 | chained 01.01 | always | dev_only 01.02 | dev "}, + }, + { + .name = "reload_when_chained always", + .input = {.builtin = {ROW_0, {"test-01.01.plan", file_text("01.01", R"({"reload_when_chained": "always"})")}}}, + .expectation = {.summary = "rows 01.00 01.01 | titles Row Row | autosave 01.00 | chained 01.01 | always 01.01 | dev_only | dev "}, + }, + { + .name = "a dev file replaces the built-in file of its name and is named", + .input = { + .builtin = {ROW_0, ROW_1}, + .dev_files = {dev_file("test-01.01.plan.json", file_text("01.01", R"({"reload_when_chained": true})", "Retimed"))}, + }, + .expectation = {.summary = "rows 01.00 01.01 | titles Row Retimed | autosave 01.00 | chained 01.01 | always | dev_only | dev test-01.01.plan.json@/dev/test-01.01.plan.json"}, + }, + { + .name = "a dev file that is not JSON fails naming its path", + .input = {.builtin = {ROW_0}, .dev_files = {dev_file("test-01.00.plan.json", "{ not json")}}, + .expectation = {.error = "/dev/test-01.00.plan.json"}, + }, + { + .name = "a dev file with no built-in file of its name fails", + .input = {.builtin = {ROW_0}, .dev_files = {dev_file("test-09.99.plan.json", file_text("09.99", "{}"))}}, + .expectation = {.error = "/dev/test-09.99.plan.json: no built-in checkpoint file is named test-09.99.plan.json"}, + }, + { + .name = "a dev file that changes its checkpoint's id fails", + .input = {.builtin = {ROW_0}, .dev_files = {dev_file("test-01.00.plan.json", file_text("01.09", R"({"save": "autosave"})"))}}, + .expectation = {.error = "/dev/test-01.00.plan.json: checkpoint id 01.09 does not match the built-in 01.00"}, + }, + { + .name = "a dev file the game's lint refuses fails", + .input = {.builtin = {ROW_0}, .dev_files = {dev_file("test-01.00.plan.json", file_text("01.00", "{}", "unlinted"))}}, + .expectation = {.error = "/dev/test-01.00.plan.json: title is unlinted"}, + }, + { + .name = "an unknown execution field fails", + .input = {.builtin = {{"test-01.00.plan", file_text("01.00", R"({"dev_mode": true})")}}}, + .expectation = {.error = "unknown execution field dev_mode"}, + }, + { + .name = "dev_only false fails", + .input = {.builtin = {{"test-01.00.plan", file_text("01.00", R"({"dev_only": false})")}}}, + .expectation = {.error = "dev_only is true or left out"}, + }, + { + .name = "a save other than autosave or menu fails", + .input = {.builtin = {{"test-01.00.plan", file_text("01.00", R"({"save": "card"})")}}}, + .expectation = {.error = "save is \"autosave\", \"menu\" or left out"}, + }, + { + .name = "reload_when_chained false fails", + .input = {.builtin = {ROW_0, {"test-01.01.plan", file_text("01.01", R"({"reload_when_chained": false})")}}}, + .expectation = {.error = "reload_when_chained is true, \"always\" or left out"}, + }, + { + .name = "reload_when_chained after a row that does not autosave fails", + .input = {.builtin = {{"test-01.00.plan", file_text("01.00", R"({"save": "menu"})")}, ROW_1}}, + .expectation = {.error = "test-01.01.plan: reload_when_chained after a checkpoint that does not end on the game's autosave"}, + }, + { + .name = "reload_when_chained on the first row fails", + .input = {.builtin = {ROW_1}}, + .expectation = {.error = "does not end on the game's autosave"}, + }, + { + .name = "reload_when_chained on the new game fails", + .input = {.builtin = {ROW_0, {"test-00.00.plan", file_text("00.00", R"({"reload_when_chained": true})")}}}, + .expectation = {.error = "test-00.00.plan: reload_when_chained on a checkpoint with no save to reload"}, + }, + { + .name = "the game's order, its reload after the autosave row it follows there", + .input = {.builtin = {ROW_1, ROW_0}, .ordered_by_id = true}, + .expectation = {.summary = "rows 01.00 01.01 | titles Row Row | autosave 01.00 | chained 01.01 | always | dev_only | dev "}, + }, + { + .name = "the game's order, its reload after a row that does not autosave there fails", + .input = {.builtin = {ROW_0, ROW_1, ROW_0B_MENU}, .ordered_by_id = true}, + .expectation = {.error = "test-01.01.plan: reload_when_chained after a checkpoint that does not end on the game's autosave"}, + }, + { + .name = "every id in the game's story order loads", + .input = {.builtin = {ROW_0, ROW_1}, .story_order = std::vector{"01.00", "01.01"}}, + .expectation = {.summary = "rows 01.00 01.01 | titles Row Row | autosave 01.00 | chained 01.01 | always | dev_only | dev "}, + }, + { + .name = "a built-in file whose id is not in the game's story order fails naming the file and the id", + .input = {.builtin = {ROW_0, ROW_1}, .story_order = std::vector{"01.00"}}, + .expectation = {.error = "test-01.01.plan: checkpoint 01.01 is not in the game's story order"}, + }, + { + .name = "a dev file in the game's story order replaces the built-in file", + .input = { + .builtin = {ROW_0, ROW_1}, + .dev_files = {dev_file("test-01.01.plan.json", file_text("01.01", R"({"reload_when_chained": true})", "Retimed"))}, + .story_order = std::vector{"01.00", "01.01"}, + }, + .expectation = {.summary = "rows 01.00 01.01 | titles Row Retimed | autosave 01.00 | chained 01.01 | always | dev_only | dev test-01.01.plan.json@/dev/test-01.01.plan.json"}, + }, + { + .name = "a dev file whose id is not in the game's story order fails naming its path and the id", + .input = { + .builtin = {ROW_0}, + .dev_files = {dev_file("test-01.00.plan.json", file_text("01.00", R"({"save": "autosave"})"))}, + .story_order = std::vector{"01.01"}, + }, + .expectation = {.error = "/dev/test-01.00.plan.json: checkpoint 01.00 is not in the game's story order"}, + }, +}; + +UnitTestResult check_load(const LoadCase& test){ + StoryFileReader reader = TEST_READER; + if (test.input.ordered_by_id){ + reader.plays_before = [](const StoryCheckpoint& a, const StoryCheckpoint& b){ + return a.id < b.id; + }; + } + if (test.input.story_order){ + reader.in_story_order = [&order = *test.input.story_order](std::string_view id){ + return std::find(order.begin(), order.end(), id) != order.end(); + }; + } + std::string got; + std::string error; + try{ + got = summary(load_story_data(test.input.builtin, test.input.dev_files, reader)); + }catch (const ParseException& e){ + error = e.message(); + } + if (test.expectation.summary){ + if (!error.empty()){ + return std::format("expected \"{}\", failed: {}", *test.expectation.summary, error); + } + if (got != *test.expectation.summary){ + return std::format("expected \"{}\", got \"{}\"", *test.expectation.summary, got); + } + return true; + } + if (error.find(test.expectation.error) == std::string::npos){ + return std::format("expected a failure with \"{}\", got \"{}\"", test.expectation.error, error.empty() ? got : error); + } + return true; +} + + +struct RowsCase{ + std::string name; + struct Input{ + StoryRowsShown shown = StoryRowsShown::USER; + size_t first = 0; + size_t last = 0; + }; + Input input; + struct Expectation{ + std::string rows = {}; + }; + Expectation expectation; +}; + +// 01.00, 01.01, 01.02 with 01.01 dev-only. +const RowsCase ROWS_CASES[] = { + {.name = "Developer Mode lists every row", .input = {.shown = StoryRowsShown::DEVELOPER, .first = 0, .last = 2}, .expectation = {.rows = "01.00 01.01 01.02"}}, + {.name = "a user's run skips the dev-only rows", .input = {.shown = StoryRowsShown::USER, .first = 0, .last = 1}, .expectation = {.rows = "01.00 01.02"}}, + {.name = "a run from one row to a later one", .input = {.shown = StoryRowsShown::DEVELOPER, .first = 1, .last = 2}, .expectation = {.rows = "01.01 01.02"}}, + {.name = "no rows for a start after the end", .input = {.shown = StoryRowsShown::DEVELOPER, .first = 2, .last = 1}, .expectation = {.rows = ""}}, + {.name = "no rows for an end past the table", .input = {.shown = StoryRowsShown::USER, .first = 0, .last = 2}, .expectation = {.rows = ""}}, +}; + +UnitTestResult check_rows(const RowsCase& test){ + StoryData story; + for (std::string_view id : {"01.00", "01.01", "01.02"}){ + StoryCheckpoint row; + row.id = std::string(id); + story.checkpoints.push_back(std::move(row)); + } + story.dev_only.emplace("01.01"); + const std::vector shown = story_rows_for_mode(story, test.input.shown); + const std::string got = joined(ids(story_run_rows(shown, test.input.first, test.input.last))); + if (got != test.expectation.rows){ + return std::format("expected \"{}\", got \"{}\"", test.expectation.rows, got); + } + return true; +} + + +struct ReloadCase{ + std::string name; + struct Input{ + StoryRunPolicy::BoundaryReload boundary_reload = StoryRunPolicy::BoundaryReload::AT_PLAN_BOUNDARIES; + std::string row = {}; + }; + Input input; + struct Expectation{ + bool reloads = false; + }; + Expectation expectation; +}; + +// 01.01 reloads when chained, 01.02 always. +const ReloadCase RELOAD_CASES[] = { + {.name = "at plan boundaries, a when-chained row reloads", .input = {.boundary_reload = StoryRunPolicy::BoundaryReload::AT_PLAN_BOUNDARIES, .row = "01.01"}, .expectation = {.reloads = true}}, + {.name = "straight through, a when-chained row runs on", .input = {.boundary_reload = StoryRunPolicy::BoundaryReload::STRAIGHT_THROUGH, .row = "01.01"}, .expectation = {.reloads = false}}, + {.name = "straight through, an always row reloads", .input = {.boundary_reload = StoryRunPolicy::BoundaryReload::STRAIGHT_THROUGH, .row = "01.02"}, .expectation = {.reloads = true}}, + {.name = "a row with no reload runs on", .input = {.boundary_reload = StoryRunPolicy::BoundaryReload::AT_PLAN_BOUNDARIES, .row = "01.00"}, .expectation = {.reloads = false}}, +}; + +UnitTestResult check_reload(const ReloadCase& test){ + StoryRows rows; + rows.reload_when_chained = {"01.01", "01.02"}; + rows.reload_always = {"01.02"}; + const bool got = story_reloads_before(rows, test.input.boundary_reload, test.input.row); + if (got != test.expectation.reloads){ + return std::format("expected reloads {}, got {}", test.expectation.reloads, got); + } + return true; +} + + +template +class StoryFilesTest : public UnitTest{ +public: + using Check = UnitTestResult (*)(const Case&); + + StoryFilesTest(std::string_view suite, Case test_case, Check check) + : UnitTest(std::format("Pokemon::StoryFiles - {}: {}", suite, test_case.name)) + , m_case(std::move(test_case)) + , m_check(check) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return m_check(m_case); + } + +private: + Case m_case; + Check m_check; +}; + +} // namespace + + +void add_tests_StoryFiles(UnitTestDatabase& database){ + for (const LoadCase& test : LOAD_CASES){ + database.add>("load", test, check_load); + } + for (const RowsCase& test : ROWS_CASES){ + database.add>("rows", test, check_rows); + } + for (const ReloadCase& test : RELOAD_CASES){ + database.add>("reload", test, check_reload); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.h new file mode 100644 index 0000000000..5f0804951a --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.h @@ -0,0 +1,24 @@ +/* Story Files Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryFiles_Tests_H +#define PokemonAutomation_Pokemon_StoryFiles_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers the tests of a story read from built-in and dev checkpoint +// files, and of the shared execution fields. +void add_tests_StoryFiles(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.cpp new file mode 100644 index 0000000000..1175684067 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.cpp @@ -0,0 +1,105 @@ +/* Story Row Execution + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "Pokemon_StoryRowExecution.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +constexpr std::string_view SHARED_FIELDS[] = {"save", "reload_when_chained", "dev_only"}; + +bool is_known_field(std::string_view key, std::initializer_list game_fields){ + for (std::string_view field : SHARED_FIELDS){ + if (key == field){ + return true; + } + } + for (std::string_view field : game_fields){ + if (key == field){ + return true; + } + } + return false; +} + +// Whether `value` is JSON true. +bool is_true(const JsonValue& value){ + return value.is_boolean() && value.to_boolean_default(); +} + +bool read_autosave(const JsonObject& execution, std::string_view where){ + const JsonValue* save = execution.get_value("save"); + if (save == nullptr){ + return false; + } + const std::string* text = save->to_string(); + if (text == nullptr || (*text != "autosave" && *text != "menu")){ + throw ParseException(std::format("{}: save is \"autosave\", \"menu\" or left out", where)); + } + return *text == "autosave"; +} + +StoryReload read_reload(const JsonObject& execution, std::string_view where){ + const JsonValue* reload = execution.get_value("reload_when_chained"); + if (reload == nullptr){ + return StoryReload::NEVER; + } + if (is_true(*reload)){ + return StoryReload::WHEN_CHAINED; + } + const std::string* text = reload->to_string(); + if (text != nullptr && *text == "always"){ + return StoryReload::ALWAYS; + } + throw ParseException(std::format("{}: reload_when_chained is true, \"always\" or left out", where)); +} + +bool read_dev_only(const JsonObject& execution, std::string_view where){ + const JsonValue* dev_only = execution.get_value("dev_only"); + if (dev_only == nullptr){ + return false; + } + if (!is_true(*dev_only)){ + throw ParseException(std::format("{}: dev_only is true or left out", where)); + } + return true; +} + +} // namespace + + +StoryRowExecution read_story_row_execution( + const JsonObject& execution, std::string_view where, std::initializer_list game_fields +){ + for (const auto& [key, value] : execution){ + if (!is_known_field(key, game_fields)){ + throw ParseException(std::format("{}: unknown execution field {}", where, key)); + } + } + return StoryRowExecution{ + .autosave = read_autosave(execution, where), + .reload = read_reload(execution, where), + .dev_only = read_dev_only(execution, where), + }; +} + +bool story_reloads_before(const StoryRows& rows, StoryRunPolicy::BoundaryReload boundary_reload, std::string_view id){ + if (rows.reload_always.contains(id)){ + return true; + } + return boundary_reload == StoryRunPolicy::BoundaryReload::AT_PLAN_BOUNDARIES && rows.reload_when_chained.contains(id); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.h b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.h new file mode 100644 index 0000000000..d820f0b6a1 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryRowExecution.h @@ -0,0 +1,104 @@ +/* Story Row Execution + * + * From: https://github.com/PokemonAutomation/ + * + * The execution fields of a checkpoint file (its JSON "execution") that + * every game reads the same way, and the sets of rows they mark: which + * rows end on the game's own autosave, which a run reloads before, and + * which are for lab runs only, and a game's story: its checkpoints and + * those rows. The game reads its other execution fields itself + * (Pokemon_AuthoredCheckpointJson.h); Pokemon_StoryFiles.h loads a story + * from its files. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryRowExecution_H +#define PokemonAutomation_Pokemon_StoryRowExecution_H + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Json/JsonObject.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// When a run reloads the row's save before it, having played the row +// before it too ("reload_when_chained"). +enum class StoryReload{ + NEVER, // left out: the row runs on from where the row before left + WHEN_CHAINED, // true: at plan boundaries (StoryRunPolicy::BoundaryReload) + ALWAYS, // "always": even straight through; only its save starts its walk +}; + +// What a checkpoint file's shared execution fields say. +struct StoryRowExecution{ + // "save": "autosave": the game saves by itself at the row's end, so the + // program does not. "menu" or left out: the program saves, when the + // save policy lets it. + bool autosave = false; + StoryReload reload = StoryReload::NEVER; + // "dev_only": true: a lab walk the story does not need (developer + // mode lists and plays it, a user's run does not). + bool dev_only = false; +}; + +// The shared fields of `execution`: "save", "reload_when_chained" and +// "dev_only", each optional. Throws ParseException naming `where` for a +// malformed one, and for a field that is neither shared nor one of +// `game_fields`, which the game reads itself. +StoryRowExecution read_story_row_execution( + const JsonObject& execution, std::string_view where, std::initializer_list game_fields +); + + +// The rows of a story the shared execution fields mark, by id. +struct StoryRows{ + std::set> autosaves; + // WHEN_CHAINED and ALWAYS rows; each comes after an autosave row, + // whose autosave is its start save. + std::set> reload_when_chained; + // The ALWAYS rows of reload_when_chained. + std::set> reload_always; + std::set> dev_only; + // The dev files that replaced a built-in checkpoint, as (file name, + // path) (Pokemon_StoryDevFiles.h). + std::vector> dev_files; +}; + +// A game's story: its checkpoints in the order it plays them, and the rows +// their execution fields mark. +template +struct StoryData : StoryRows{ + std::vector checkpoints; +}; + +// How a run plays its checkpoints: the attempts each gets, and whether it +// reloads at the plan's boundaries. +struct StoryRunPolicy{ + // Whether a run of several checkpoints reloads the save at a + // WHEN_CHAINED row (AT_PLAN_BOUNDARIES) or runs on from the state the + // row before left (STRAIGHT_THROUGH). ALWAYS rows reload either way, and + // a failed checkpoint's retries reload as usual. + enum class BoundaryReload{ + AT_PLAN_BOUNDARIES, + STRAIGHT_THROUGH, + }; + size_t attempts_per_checkpoint = 1; + BoundaryReload boundary_reload = BoundaryReload::AT_PLAN_BOUNDARIES; +}; + +// Whether a run reloads the save before row `id` of `rows` when it reaches +// it from the row before. +bool story_reloads_before(const StoryRows& rows, StoryRunPolicy::BoundaryReload boundary_reload, std::string_view id); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp index 0433258cbb..69f213ad13 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp @@ -36,28 +36,28 @@ enum class Record{ CHECKPOINT, }; -// A record to read, and which kind it is. -struct WireInput{ - std::string_view name; - Record record = Record::STEP; - std::string_view json; -}; - // What a read gives. using ReadRecord = std::variant; // "" when the record read is as expected, else what differs. using ReadCheck = std::string (*)(const ReadRecord& record); -// What reading it gives: a record the check accepts, or a rejection. -struct WireExpectation{ - ReadCheck check = nullptr; - bool rejected = false; -}; - struct WireCase{ - WireInput input; - WireExpectation expectation; + // A record to read, and which kind it is. + struct Input{ + std::string_view name; + Record record = Record::STEP; + std::string_view json; + }; + + // What reading it gives: a record the check accepts, or a rejection. + struct Expectation{ + ReadCheck check = nullptr; + bool rejected = false; + }; + + Input input; + Expectation expectation; }; std::string expect(bool ok, std::string_view what){ @@ -201,7 +201,7 @@ std::vector test_actions(){ // `input` read. -ReadRecord read_record(const WireInput& input){ +ReadRecord read_record(const WireCase::Input& input){ static const std::vector actions = test_actions(); const JsonValue value = parse_json(std::string(input.json)); switch (input.record){ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/MIGRATION.md b/SerialPrograms/Source/Pokemon/AutoStory/Engine/MIGRATION.md index e392e047a1..c2c674c1bb 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/MIGRATION.md +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/MIGRATION.md @@ -9,7 +9,7 @@ Each move deletes that game's executor; the last one deletes `StoryStepExecutor` 1. **Backends.** Write a console backend: `StoryBackend` over the console handle and the controller context, `kind()` CONSOLE. `observe()` waits for pending input (`wait_for_all_requests()`), takes a frame and fills `ScreenObservation::events` from the - game's detectors (fade, text box, choice box, interact prompt, battle, can move, still). + game's detectors (the `ScreenEvent`s of `CommonTools/ScreenEvents/ScreenEventTypes.h`). `stick(degrees, duration, release, pace)` holds the move stick at an angle, then lets go for `release` (a free-moving game's walk and turn; `hold()` is the 90-degree case). A game without tiles reports the way the player diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.cpp index 926483bfb0..212d51a1b8 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.cpp @@ -4,7 +4,6 @@ * */ -#include #include "Pokemon_ScreenObservation.h" namespace PokemonAutomation{ @@ -12,24 +11,26 @@ namespace Pokemon{ ScreenObservation screen_observation(const ScreenEvents& events){ - using EngineEvent = PokemonAutomation::ScreenEvent; - ScreenObservation look; - const std::pair same[] = { - {ScreenEvent::FADE_BLACK, EngineEvent::FADE_BLACK}, - {ScreenEvent::FADE_WHITE, EngineEvent::FADE_WHITE}, - {ScreenEvent::DIALOG_BOX, EngineEvent::TEXT_BOX}, - {ScreenEvent::CHOICE_BOX, EngineEvent::TEXT_BOX}, - {ScreenEvent::BATTLE_STARTED, EngineEvent::BATTLE}, - {ScreenEvent::PLAYER_CAN_MOVE, EngineEvent::CAN_MOVE}, - {ScreenEvent::SCREEN_STILL, EngineEvent::STILL}, - {ScreenEvent::CHOICE_BOX, EngineEvent::CHOICE_BOX}, - {ScreenEvent::INTERACT_PROMPT, EngineEvent::INTERACT_PROMPT}, + // The events the story engine reads; the rest are left out. + constexpr ScreenEvent ENGINE_EVENTS[] = { + ScreenEvent::FADE_BLACK, + ScreenEvent::FADE_WHITE, + ScreenEvent::DIALOG_BOX, + ScreenEvent::BATTLE_STARTED, + ScreenEvent::PLAYER_CAN_MOVE, + ScreenEvent::SCREEN_STILL, + ScreenEvent::CHOICE_BOX, + ScreenEvent::INTERACT_PROMPT, }; - for (const auto& [event, engine_event] : same){ + ScreenObservation look; + for (ScreenEvent event : ENGINE_EVENTS){ if (events.has(event)){ - look.events.add(engine_event); + look.events.set(event, true); } } + if (events.has(ScreenEvent::CHOICE_BOX)){ + look.events.set(ScreenEvent::DIALOG_BOX, true); + } return look; } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.h index 673d91c72c..2c1b7d8dd5 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.h @@ -2,7 +2,7 @@ * * From: https://github.com/PokemonAutomation/ * - * Bridges a ScreenEventObserver (Pokemon::ScreenEvents) to the story + * Bridges a ScreenEventObserver (ScreenEvents) to the story * engine's look (ScreenObservation). * */ @@ -11,16 +11,15 @@ #define PokemonAutomation_Pokemon_ScreenObservation_H #include "CommonTools/ScreenMachine/ScreenObservation.h" -#include "Pokemon/AutoStory/Pokemon_ScreenEventTypes.h" +#include "CommonTools/ScreenEvents/ScreenEventTypes.h" namespace PokemonAutomation{ namespace Pokemon{ -// A ScreenEventObserver's events as one look for the story engine: -// DIALOG_BOX is TEXT_BOX; CHOICE_BOX adds TEXT_BOX; BATTLE_STARTED is -// BATTLE; PLAYER_CAN_MOVE is CAN_MOVE; SCREEN_STILL is STILL; the fades -// and INTERACT_PROMPT keep their names. No time and no frame. +// A ScreenEventObserver's events as one look for the story engine: the +// events the engine reads (not LOCATION_BANNER, BATTLE_ENDED or +// SWITCH_HOME_MENU), and CHOICE_BOX adds DIALOG_BOX. No time and no frame. ScreenObservation screen_observation(const ScreenEvents& events); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.cpp new file mode 100644 index 0000000000..d50dccd657 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.cpp @@ -0,0 +1,21 @@ +/* Story Action Not Implemented + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Pokemon_StoryActionNotImplemented.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +StoryActionNotImplemented::StoryActionNotImplemented(std::string message) + : OperationFailedException(ErrorReportMode::NO_ERROR_REPORT, std::move(message)) +{} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.h new file mode 100644 index 0000000000..566ade4269 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionNotImplemented.h @@ -0,0 +1,35 @@ +/* Story Action Not Implemented + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryActionNotImplemented_H +#define PokemonAutomation_Pokemon_StoryActionNotImplemented_H + +#include +#include "CommonFramework/Exceptions/OperationFailedException.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// A story action the game has no code for yet, thrown with what it waits +// for instead of pressing buttons blind. A game's backend lets it end the +// run instead of reloading and trying again, since every attempt would +// fail the same way. +class StoryActionNotImplemented : public OperationFailedException{ +public: + explicit StoryActionNotImplemented(std::string message); + + // code-health: allow char_pointer -- Exception::name() returns a const char pointer in the base class + virtual const char* name() const override{ + return "StoryActionNotImplemented"; + } +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBuiltinActions.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBuiltinActions.cpp index 24e610dca6..6a6481ccba 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBuiltinActions.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBuiltinActions.cpp @@ -51,7 +51,7 @@ Outcome mash_to_overworld(StoryBackend& backend){ using namespace std::chrono_literals; for (size_t c = 0; c < MAX_TEXT_LOOKS; c++){ const Observation now = backend.observe(); - if (now.events.has(ScreenEvent::CAN_MOVE) && !now.events.has(ScreenEvent::TEXT_BOX)){ + if (now.events.has(ScreenEvent::PLAYER_CAN_MOVE) && !now.events.has(ScreenEvent::DIALOG_BOX)){ return outcome_ok(); } backend.press(NintendoSwitch::BUTTON_A, 80ms, 120ms); @@ -71,7 +71,7 @@ Outcome talk(StoryBackend& backend, const StoryActionCall&, RunState&) using namespace std::chrono_literals; backend.press(NintendoSwitch::BUTTON_A, 80ms, 120ms); for (size_t c = 0; c < MAX_OPEN_LOOKS; c++){ - if (backend.observe().events.has(ScreenEvent::TEXT_BOX)){ + if (backend.observe().events.has(ScreenEvent::DIALOG_BOX)){ return mash_to_overworld(backend); } backend.wait(100ms); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp index e215a54d82..b874795b8a 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp @@ -9,6 +9,7 @@ #include #include #include +#include "Pokemon/AutoStory/Pokemon_CheckpointAttempts.h" #include "Pokemon/AutoStory/Pokemon_StoryEvents.h" #include "Pokemon_StoryEngine.h" @@ -234,26 +235,34 @@ template StoryEngineReport StoryEngine::run_with_retries( const StoryCheckpoint& checkpoint, SavePoint point, size_t attempts, RunState& state, CheckpointRun& run ){ - for (size_t attempt = 1; ; attempt++){ - run.attempts = attempt; - StoryEngineReport report = run_checkpoint(checkpoint, point, state); - if (report.verdict == CheckpointVerdict::PASSED){ - run.passed = true; - m_backend.log(checkpoint_pass_line(checkpoint.id, attempt)); - return report; - } - if (attempt >= attempts || repeats_on_reload(report.run.failure)){ - return report; - } - m_backend.log(std::format("[Segment] {}: failed, reloading for attempt {}/{}", checkpoint.id, attempt + 1, attempts)); - const Outcome reloaded = reload(state); - if (!reloaded.succeeded()){ - report.run.completed = false; - report.run.failed_name.clear(); - report.run.failure = reloaded; - return report; - } - } + StoryEngineReport report; + const CheckpointAttempts loop{ + .max_attempts = attempts, + .run = [&](size_t attempt){ + run.attempts = attempt; + report = run_checkpoint(checkpoint, point, state); + if (report.verdict == CheckpointVerdict::PASSED){ + return AttemptResult::PASSED; + } + return repeats_on_reload(report.run.failure) ? AttemptResult::FINAL : AttemptResult::FAILED; + }, + .reset = [&](size_t next_attempt){ + m_backend.log(checkpoint_reset_line(checkpoint.id, next_attempt, attempts)); + const Outcome reloaded = reload(state); + if (!reloaded.succeeded()){ + report.run.completed = false; + report.run.failed_name.clear(); + report.run.failure = reloaded; + return false; + } + return true; + }, + .log = [this](const std::string& line){ + m_backend.log(line); + }, + }; + run.passed = run_checkpoint_attempts(checkpoint.id, loop).passed; + return report; } template diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy_Tests.cpp index 1a208e8862..90ecc3d4db 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy_Tests.cpp @@ -55,7 +55,7 @@ enum class HashRecord{ // A look at B3F (12, 13) or wherever `map` is. Observation end_look(std::string map){ Observation observation; - observation.events = ScreenEvents{ScreenEvent::CAN_MOVE}; + observation.events = ScreenEvents{ScreenEvent::PLAYER_CAN_MOVE}; observation.pose = GridPose{.map = std::move(map), .tiles = {{12, 13}}, .facing = Direction::NORTH}; return observation; } @@ -83,29 +83,29 @@ StoryGame engine_game(std::optional candies){ -// One engine run of 00.31 in `phase`, the player ending on `end_map`. -struct EngineInput{ - std::string_view name; - std::string_view phase; - HashRecord hash = HashRecord::MATCHING; - std::string_view steps = TALK_STEPS; - // Where the talk leaves the player; empty: the talk opens no text. - std::string_view end_map = "RocketHideout_B3F"; - std::optional candies = 2; -}; +struct EngineCase{ + // One engine run of 00.31 in `phase`, the player ending on `end_map`. + struct Input{ + std::string_view name; + std::string_view phase; + HashRecord hash = HashRecord::MATCHING; + std::string_view steps = TALK_STEPS; + // Where the talk leaves the player; empty: the talk opens no text. + std::string_view end_map = "RocketHideout_B3F"; + std::optional candies = 2; + }; -// The save mode chosen, the verdict, whether the game saved (each save -// logs "[Test] save") and a line the run logged. -struct EngineExpectation{ - StorySaveMode mode = StorySaveMode::NONE; - CheckpointVerdict verdict = CheckpointVerdict::PASSED; - size_t saves = 0; - std::string_view line = {}; -}; + // The save mode chosen, the verdict, whether the game saved (each save + // logs "[Test] save") and a line the run logged. + struct Expectation{ + StorySaveMode mode = StorySaveMode::NONE; + CheckpointVerdict verdict = CheckpointVerdict::PASSED; + size_t saves = 0; + std::string_view line = {}; + }; -struct EngineCase{ - EngineInput input; - EngineExpectation expectation; + Input input; + Expectation expectation; }; const EngineCase ENGINE_CASES[] = { @@ -144,7 +144,7 @@ std::vector> engine_looks(std::string_view end_map){ return {end_look("RocketHideout_B3F")}; } Observation text; - text.events = ScreenEvents{ScreenEvent::TEXT_BOX}; + text.events = ScreenEvents{ScreenEvent::DIALOG_BOX}; return {std::move(text), end_look(std::string(end_map))}; } @@ -201,23 +201,23 @@ class StoryEngineSaveTest : public UnitTest{ -// Two checkpoints' events, hashed with the built-in actions. -struct HashInput{ - std::string_view name; - std::string_view a; - std::string_view b; -}; +struct HashCase{ + // Two checkpoints' events, hashed with the built-in actions. + struct Input{ + std::string_view name; + std::string_view a; + std::string_view b; + }; -// Whether they hash the same, and `a`'s hash when pinned: a change to it -// means every recorded verification goes stale. -struct HashExpectation{ - bool same = true; - std::optional hash_a = std::nullopt; -}; + // Whether they hash the same, and `a`'s hash when pinned: a change to it + // means every recorded verification goes stale. + struct Expectation{ + bool same = true; + std::optional hash_a = std::nullopt; + }; -struct HashCase{ - HashInput input; - HashExpectation expectation; + Input input; + Expectation expectation; }; inline constexpr std::string_view ROUTE_B3F = R"([{"id": "go", "kind": "route", "args": {"target": {"map": "RocketHideout_B3F", "tiles": [[12, 13]]}}}])"; @@ -271,23 +271,23 @@ class StoryStepsHashTest : public UnitTest{ -// A checkpoint's "metadata", read. -struct MetadataInput{ - std::string_view name; - std::string_view metadata; -}; +struct MetadataCase{ + // A checkpoint's "metadata", read. + struct Input{ + std::string_view name; + std::string_view metadata; + }; -// The error the read throws (contained), or what it read. -struct MetadataExpectation{ - std::string_view error = {}; - StoryPhase phase = StoryPhase::TESTING; - std::string_view steps_hash = {}; - size_t items = 0; -}; + // The error the read throws (contained), or what it read. + struct Expectation{ + std::string_view error = {}; + StoryPhase phase = StoryPhase::TESTING; + std::string_view steps_hash = {}; + size_t items = 0; + }; -struct MetadataCase{ - MetadataInput input; - MetadataExpectation expectation; + Input input; + Expectation expectation; }; const MetadataCase METADATA_CASES[] = { @@ -341,27 +341,27 @@ class StoryMetadataTest : public UnitTest{ -// What a look showed, classified by ScreenMachine states that match on -// observations. -struct ScreenInput{ - std::string_view name; - std::vector events; -}; +struct ScreenCase{ + // What a look showed, classified by ScreenMachine states that match on + // observations. + struct Input{ + std::string_view name; + std::vector events; + }; -// The state matched; none when no state did. -struct ScreenExpectation{ - std::optional state = std::nullopt; -}; + // The state matched; none when no state did. + struct Expectation{ + std::optional state = std::nullopt; + }; -struct ScreenCase{ - ScreenInput input; - ScreenExpectation expectation; + Input input; + Expectation expectation; }; const ScreenCase SCREEN_CASES[] = { - {.input = {.name = "battle first", .events = {ScreenEvent::BATTLE, ScreenEvent::TEXT_BOX}}, .expectation = {.state = "battle"}}, - {.input = {.name = "text", .events = {ScreenEvent::TEXT_BOX}}, .expectation = {.state = "text"}}, - {.input = {.name = "overworld", .events = {ScreenEvent::CAN_MOVE}}, .expectation = {.state = "overworld"}}, + {.input = {.name = "battle first", .events = {ScreenEvent::BATTLE_STARTED, ScreenEvent::DIALOG_BOX}}, .expectation = {.state = "battle"}}, + {.input = {.name = "text", .events = {ScreenEvent::DIALOG_BOX}}, .expectation = {.state = "text"}}, + {.input = {.name = "overworld", .events = {ScreenEvent::PLAYER_CAN_MOVE}}, .expectation = {.state = "overworld"}}, {.input = {.name = "image-only state skipped", .events = {ScreenEvent::FADE_WHITE}}, .expectation = {}}, }; @@ -377,9 +377,9 @@ ScreenState observation_state(std::string name, ScreenEvent event){ std::string check_screen(const ScreenCase& test){ std::vector states{ - observation_state("battle", ScreenEvent::BATTLE), - observation_state("text", ScreenEvent::TEXT_BOX), - observation_state("overworld", ScreenEvent::CAN_MOVE), + observation_state("battle", ScreenEvent::BATTLE_STARTED), + observation_state("text", ScreenEvent::DIALOG_BOX), + observation_state("overworld", ScreenEvent::PLAYER_CAN_MOVE), }; ScreenState image_only; image_only.name = "image_only"; @@ -390,7 +390,7 @@ std::string check_screen(const ScreenCase& test){ states.push_back(std::move(image_only)); ScreenObservation observation; for (ScreenEvent event : test.input.events){ - observation.events.add(event); + observation.events.set(event, true); } const ScreenState* state = match_screen_state(states, observation); const std::string got = state ? state->name : "none"; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp index d272a19648..145551ad73 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp @@ -143,7 +143,7 @@ StoryGame flaky_game(size_t failures){ // only its zone, run on the scripted backend of its pose type. std::string zone_checkpoint_runs(CancellableScope& scope){ StoryGame game = flaky_game(0); - FakeStoryBackend backend({.looks = {look({ScreenEvent::CAN_MOVE})}}, scope); + FakeStoryBackend backend({.looks = {look({ScreenEvent::PLAYER_CAN_MOVE})}}, scope); StoryEngine engine(game, backend); const StoryCheckpoint checkpoint = read_story_checkpoint(parse_json(R"({ "id": "03.01", "title": "Down to the lobby", "start": {"place": "HotelZ", "spot": "Room", "heading": 180}, @@ -297,7 +297,7 @@ std::vector choice_states(std::vector& acted){ std::vector states{ state("choice", ScreenEvent::CHOICE_BOX), state("prompt", ScreenEvent::INTERACT_PROMPT), - state("text", ScreenEvent::TEXT_BOX), + state("text", ScreenEvent::DIALOG_BOX), }; states[0].terminal = true; return states; @@ -318,7 +318,7 @@ ScreenMachineResult run_on_events( const std::function observe = [&]{ ScreenObservation observation; observation.when = start + std::chrono::seconds(next); - observation.events.add(events[std::min(next, events.size() - 1)]); + observation.events.set(events[std::min(next, events.size() - 1)], true); next++; return observation; }; @@ -329,7 +329,7 @@ std::string observation_machine_ends(CancellableScope& scope){ std::vector acted; std::vector lines; const ScreenMachineResult result = run_on_events( - {ScreenEvent::INTERACT_PROMPT, ScreenEvent::TEXT_BOX, ScreenEvent::CHOICE_BOX}, scope, 60s, acted, lines + {ScreenEvent::INTERACT_PROMPT, ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX}, scope, 60s, acted, lines ); if (!result.succeeded() || result.terminal != "choice"){ return std::format("expected the run to end on choice, got \"{}\"", result.failure); @@ -346,15 +346,15 @@ std::string observation_machine_ends(CancellableScope& scope){ std::string observation_machine_fails(CancellableScope& scope){ std::vector acted; std::vector lines; - ScreenMachineResult result = run_on_events({ScreenEvent::BATTLE}, scope, 60s, acted, lines); + ScreenMachineResult result = run_on_events({ScreenEvent::BATTLE_STARTED}, scope, 60s, acted, lines); if (result.failure != "The screen matches none of the known states."){ return std::format("expected no state to match, got \"{}\"", result.failure); } - result = run_on_events({ScreenEvent::TEXT_BOX}, scope, 60s, acted, lines); + result = run_on_events({ScreenEvent::DIALOG_BOX}, scope, 60s, acted, lines); if (!result.failure.starts_with("Screen \"text\" came up more than 3 times")){ return std::format("expected text to come up too often, got \"{}\"", result.failure); } - result = run_on_events({ScreenEvent::TEXT_BOX, ScreenEvent::INTERACT_PROMPT, ScreenEvent::TEXT_BOX}, scope, 1s, acted, lines); + result = run_on_events({ScreenEvent::DIALOG_BOX, ScreenEvent::INTERACT_PROMPT, ScreenEvent::DIALOG_BOX}, scope, 1s, acted, lines); if (result.failure != "Did not finish within 1s."){ return std::format("expected the looks' time to run out, got \"{}\"", result.failure); } @@ -366,7 +366,7 @@ std::string observation_machine_fails(CancellableScope& scope){ Observation framed_look(WallClock start, int seconds, uint32_t shade, ScreenEvent event){ Observation observation; observation.when = start + std::chrono::seconds(seconds); - observation.events.add(event); + observation.events.set(event, true); ImageRGB32 image(16, 16); image.fill(shade); observation.frame = VideoSnapshot(std::move(image), observation.when); @@ -387,7 +387,7 @@ ScreenMachineResult run_still_states(const std::vector>& l ScreenState text; text.name = "text"; text.matches_observation = [](const ScreenObservation& observation){ - return observation.events.has(ScreenEvent::TEXT_BOX); + return observation.events.has(ScreenEvent::DIALOG_BOX); }; text.act = [&backend]{ backend.press(NintendoSwitch::BUTTON_A, 80ms, 120ms); @@ -396,7 +396,7 @@ ScreenMachineResult run_still_states(const std::vector>& l ScreenState done; done.name = "done"; done.matches_observation = [](const ScreenObservation& observation){ - return observation.events.has(ScreenEvent::CAN_MOVE); + return observation.events.has(ScreenEvent::PLAYER_CAN_MOVE); }; done.max_visits = SIZE_MAX; done.terminal = true; @@ -416,7 +416,7 @@ std::string still_states(CancellableScope& scope){ for (int second = 0; second < 12; second++){ waits.push_back(framed_look(start, second, 0xff000000, ScreenEvent::FADE_BLACK)); } - waits.push_back(framed_look(start, 12, 0xff808080, ScreenEvent::CAN_MOVE)); + waits.push_back(framed_look(start, 12, 0xff808080, ScreenEvent::PLAYER_CAN_MOVE)); ScreenMachineResult result = run_still_states(waits, scope); if (result.terminal != "done"){ return std::format("expected a 12 s still fade to end on done, got \"{}\"", result.failure); @@ -431,7 +431,7 @@ std::string still_states(CancellableScope& scope){ } std::vector> presses; for (int second = 0; second < 12; second++){ - presses.push_back(framed_look(start, second, 0xffffffff, ScreenEvent::TEXT_BOX)); + presses.push_back(framed_look(start, second, 0xffffffff, ScreenEvent::DIALOG_BOX)); } result = run_still_states(presses, scope); if (result.failure != "The screen stopped changing: 8 steps on \"text\" did nothing."){ @@ -520,7 +520,7 @@ std::string handler_sees_step(CancellableScope& scope){ // Gap 6: the save is told its checkpoint, at the end and at a SAVE step. std::string save_gets_checkpoint(CancellableScope& scope){ StoryGame game = flaky_game(0); - FakeStoryBackend backend({.looks = {look({ScreenEvent::CAN_MOVE})}}, scope); + FakeStoryBackend backend({.looks = {look({ScreenEvent::PLAYER_CAN_MOVE})}}, scope); StoryEngine engine(game, backend); StoryStep save; save.kind = StoryStepKind::SAVE; @@ -572,7 +572,7 @@ std::string console_only_refused(CancellableScope& scope){ // would repeat. std::string segment_no_reload_for_console_only(CancellableScope& scope){ const StoryGame game = console_game(); - FakeStoryBackend backend({.looks = {look({ScreenEvent::CAN_MOVE})}}, scope); + FakeStoryBackend backend({.looks = {look({ScreenEvent::PLAYER_CAN_MOVE})}}, scope); StoryEngine engine(game, backend); const std::vector checkpoints{checkpoint_of("01", {game_step("menu", "open_menu")})}; RunState state; @@ -585,22 +585,22 @@ std::string segment_no_reload_for_console_only(CancellableScope& scope){ // Gap 7: the first checkpoint is retried after a reload; one after it // only when the one before saved at its end (#329). -struct SegmentInput{ - std::string_view name; - StoryPhase phase; - // Which checkpoint fails once: 0 or 1. - size_t flaky = 0; -}; +struct SegmentCase{ + struct Input{ + std::string_view name; + StoryPhase phase; + // Which checkpoint fails once: 0 or 1. + size_t flaky = 0; + }; -struct SegmentExpectation{ - bool completed = false; - size_t reloads = 0; - size_t saves = 0; -}; + struct Expectation{ + bool completed = false; + size_t reloads = 0; + size_t saves = 0; + }; -struct SegmentCase{ - SegmentInput input; - SegmentExpectation expectation; + Input input; + Expectation expectation; }; const SegmentCase SEGMENT_CASES[] = { @@ -616,7 +616,7 @@ const SegmentCase SEGMENT_CASES[] = { std::string check_segment(const SegmentCase& test, CancellableScope& scope){ const StoryGame game = flaky_game(1); - FakeStoryBackend backend({.looks = {look({ScreenEvent::CAN_MOVE})}}, scope); + FakeStoryBackend backend({.looks = {look({ScreenEvent::PLAYER_CAN_MOVE})}}, scope); StoryEngine engine(game, backend); std::vector checkpoints; for (size_t c = 0; c < 2; c++){ @@ -651,12 +651,83 @@ class StorySegmentTest : public UnitTest{ SegmentCase m_case; }; +// The engine's retries run through run_checkpoint_attempts(): the same +// reload and pass lines and attempt counts as its own loop had. +struct RetryCase{ + struct Input{ + std::string_view name; + // Calls of the checkpoint's action that fail before one passes. + size_t failures = 0; + size_t attempts = 0; + }; + + struct Expectation{ + bool passed = false; + size_t attempts = 0; + // Lines the run writes once each. + std::vector lines; + }; + + Input input; + Expectation expectation; +}; + +const RetryCase RETRY_CASES[] = { + {.input = {.name = "pass after a reload", .failures = 1, .attempts = 3}, + .expectation = {.passed = true, .attempts = 2, .lines = { + "[Checkpoint] 01: failed, resetting for attempt 2/3", + "[Test] reload", + "[Checkpoint] 01: passed on attempt 2 (1 reset)", + }}}, + {.input = {.name = "stop after the last attempt", .failures = 5, .attempts = 2}, + .expectation = {.passed = false, .attempts = 2, .lines = { + "[Checkpoint] 01: failed, resetting for attempt 2/2", + "[Test] reload", + }}}, +}; + +std::string check_retry(const RetryCase& test, CancellableScope& scope){ + const StoryGame game = flaky_game(test.input.failures); + FakeStoryBackend backend({.looks = {look({ScreenEvent::PLAYER_CAN_MOVE})}}, scope); + StoryEngine engine(game, backend); + const std::vector checkpoints{checkpoint_of("01", {game_step("walk", "flaky")})}; + RunState state; + const SegmentReport report = engine.run_segment("seg", checkpoints, test.input.attempts, state); + const CheckpointRun& run = report.runs.at(0); + if (run.passed != test.expectation.passed || run.attempts != test.expectation.attempts){ + return std::format( + "expected passed {} after {} attempts, got {} after {}: {}", + test.expectation.passed, test.expectation.attempts, run.passed, run.attempts, joined(backend.lines()) + ); + } + for (std::string_view line : test.expectation.lines){ + if (count_lines(backend, line) != 1){ + return std::format("expected \"{}\" once: {}", line, joined(backend.lines())); + } + } + return ""; +} + +class StoryRetryTest : public UnitTest{ +public: + explicit StoryRetryTest(RetryCase test) + : UnitTest(std::format("Pokemon::StoryEngine retry - {}", test.input.name)) + , m_case(std::move(test)) + {} + virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ + const std::string error = check_retry(m_case, scope); + return error.empty() ? UnitTestResult(true) : UnitTestResult(error); + } +private: + RetryCase m_case; +}; + // Gap 7: verify runs a checkpoint 3 times from one save, saving only at // the end of the last, and only when every run passed. std::string verify_saves_after_all_passed(CancellableScope& scope){ for (size_t failures = 0; failures < 2; failures++){ const StoryGame game = flaky_game(failures); - FakeStoryBackend backend({.looks = {look({ScreenEvent::CAN_MOVE})}}, scope); + FakeStoryBackend backend({.looks = {look({ScreenEvent::PLAYER_CAN_MOVE})}}, scope); StoryEngine engine(game, backend); const StoryCheckpoint checkpoint = in_phase(checkpoint_of("00.15b", {game_step("battle", "flaky")}), StoryPhase::VERIFICATION, engine); RunState state; @@ -675,20 +746,20 @@ std::string verify_saves_after_all_passed(CancellableScope& scope){ } -// One check above, by name. -struct RunsInput{ - std::string_view name; - std::string (*check)(CancellableScope& scope); -}; +struct RunsCase{ + // One check above, by name. + struct Input{ + std::string_view name; + std::string (*check)(CancellableScope& scope); + }; -// The check's error: none. -struct RunsExpectation{ - std::string_view error = {}; -}; + // The check's error: none. + struct Expectation{ + std::string_view error = {}; + }; -struct RunsCase{ - RunsInput input; - RunsExpectation expectation; + Input input; + Expectation expectation; }; const RunsCase RUNS_CASES[] = { @@ -735,6 +806,9 @@ void add_tests_StoryEngineRuns(UnitTestDatabase& database){ for (const SegmentCase& test : SEGMENT_CASES){ database.add(test); } + for (const RetryCase& test : RETRY_CASES){ + database.add(test); + } } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine_Tests.cpp index 4548921c9b..5aa882f997 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine_Tests.cpp @@ -127,24 +127,24 @@ StoryStep make_step(StoryStepKind kind, std::string action, StoryActionArgs args -// A step checked and, when it lints clean, dispatched on a backend that -// shows talk text once, then the overworld. -struct RegistryInput{ - std::string_view name; - StoryStep step; -}; - -// The lint errors joined by "; ", then (for a clean step) the failure and -// the inputs sent. -struct RegistryExpectation{ - std::string_view lint = {}; - std::string_view failure = {}; - std::string_view inputs = {}; -}; - struct RegistryCase{ - RegistryInput input; - RegistryExpectation expectation; + // A step checked and, when it lints clean, dispatched on a backend that + // shows talk text once, then the overworld. + struct Input{ + std::string_view name; + StoryStep step; + }; + + // The lint errors joined by "; ", then (for a clean step) the failure and + // the inputs sent. + struct Expectation{ + std::string_view lint = {}; + std::string_view failure = {}; + std::string_view inputs = {}; + }; + + Input input; + Expectation expectation; }; const RegistryCase REGISTRY_CASES[] = { @@ -180,7 +180,7 @@ std::string check_registry(const RegistryCase& test, CancellableScope& scope){ if (!lint.empty()){ return ""; } - FakeStoryBackend backend({.looks = {look({ScreenEvent::TEXT_BOX}), look({ScreenEvent::TEXT_BOX}), look({ScreenEvent::CAN_MOVE})}}, scope); + FakeStoryBackend backend({.looks = {look({ScreenEvent::DIALOG_BOX}), look({ScreenEvent::DIALOG_BOX}), look({ScreenEvent::PLAYER_CAN_MOVE})}}, scope); RunState state; const Outcome outcome = registry.dispatch(backend, test.input.step, state); if (outcome.failure != test.expectation.failure){ @@ -208,26 +208,26 @@ class StoryActionRegistryTest : public UnitTest{ -// One step, read from JSON, run by the step runner on a backend that -// shows the overworld, with `flags` set and the "door_open" reader -// reading `door_open`. -struct RunnerInput{ - std::string_view name; - std::string_view step; - std::vector flags = {}; - std::optional door_open = std::nullopt; -}; - -// The failure, a line the run logged and the map of the outcome's pose. -struct RunnerExpectation{ - std::string_view failure = {}; - std::string_view line = {}; - std::string_view pose_map = {}; -}; - struct RunnerCase{ - RunnerInput input; - RunnerExpectation expectation; + // One step, read from JSON, run by the step runner on a backend that + // shows the overworld, with `flags` set and the "door_open" reader + // reading `door_open`. + struct Input{ + std::string_view name; + std::string_view step; + std::vector flags = {}; + std::optional door_open = std::nullopt; + }; + + // The failure, a line the run logged and the map of the outcome's pose. + struct Expectation{ + std::string_view failure = {}; + std::string_view line = {}; + std::string_view pose_map = {}; + }; + + Input input; + Expectation expectation; }; const RunnerCase RUNNER_CASES[] = { @@ -248,7 +248,7 @@ const RunnerCase RUNNER_CASES[] = { std::string check_runner(const RunnerCase& test, CancellableScope& scope){ const StoryGame game = test_game(test.input.door_open); const StoryActionRegistry registry(game); - FakeStoryBackend backend({.looks = {look({ScreenEvent::CAN_MOVE})}}, scope); + FakeStoryBackend backend({.looks = {look({ScreenEvent::PLAYER_CAN_MOVE})}}, scope); StoryStepRunner runner(registry, backend); RunState state; state.flags.insert(test.input.flags.begin(), test.input.flags.end()); @@ -283,31 +283,31 @@ class StoryStepRunnerTest : public UnitTest{ -// take_warp north to B3F, from B4F, on a scripted game. -struct WarpInput{ - std::string_view name; - std::vector> looks; -}; - -// How take_warp ended: "" for arrived. -struct WarpExpectation{ - std::string_view failure = {}; -}; - struct WarpCase{ - WarpInput input; - WarpExpectation expectation; + // take_warp north to B3F, from B4F, on a scripted game. + struct Input{ + std::string_view name; + std::vector> looks; + }; + + // How take_warp ended: "" for arrived. + struct Expectation{ + std::string_view failure = {}; + }; + + Input input; + Expectation expectation; }; const WarpCase WARP_CASES[] = { {.input = {.name = "fade then B3F", .looks = { - look({}, on_map("RocketHideout_B4F")), look({ScreenEvent::FADE_BLACK}), look({ScreenEvent::CAN_MOVE}, on_map("RocketHideout_B3F")), + look({}, on_map("RocketHideout_B4F")), look({ScreenEvent::FADE_BLACK}), look({ScreenEvent::PLAYER_CAN_MOVE}, on_map("RocketHideout_B3F")), }}, .expectation = {}}, // The screen darkens at the stairs but no fade starts and the map // stays B4F: the old wait took this for arrived (00.31's false pass). - {.input = {.name = "black_without_warp", .looks = {look({ScreenEvent::STILL}, on_map("RocketHideout_B4F"))}}, + {.input = {.name = "black_without_warp", .looks = {look({ScreenEvent::SCREEN_STILL}, on_map("RocketHideout_B4F"))}}, .expectation = {.failure = "warp_no_fade"}}, - {.input = {.name = "fade back to B4F", .looks = {look({ScreenEvent::FADE_BLACK}), look({ScreenEvent::CAN_MOVE}, on_map("RocketHideout_B4F"))}}, + {.input = {.name = "fade back to B4F", .looks = {look({ScreenEvent::FADE_BLACK}), look({ScreenEvent::PLAYER_CAN_MOVE}, on_map("RocketHideout_B4F"))}}, .expectation = {.failure = "warp_wrong_map"}}, }; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.cpp new file mode 100644 index 0000000000..26d7c2b2a8 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.cpp @@ -0,0 +1,280 @@ +/* Story Launch + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "NintendoSwitch/Inference/NintendoSwitch_HomeTileReader.h" +#include "Pokemon_StoryLaunch.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + +using namespace NintendoSwitch; + + +namespace{ + + +// What a look shows, as far as a launch cares. +enum class LaunchScreen{ + HOME, + NEW_GAME, + PROMPT, + TITLE, + OVERWORLD, + OTHER, +}; + +struct LaunchLook{ + LaunchScreen screen = LaunchScreen::OTHER; + // The prompt on screen, for PROMPT. + const StoryLaunchPrompt* prompt = nullptr; +}; + +// `look` as `policy` reads it, `launched` once the launch launched or +// closed the game. HOME first: a game's detectors may read HOME's plain +// screen as a quiet overworld. +LaunchLook read_launch_look(const StoryLaunchPolicy& policy, const ScreenObservation& look, bool launched){ + if (look.events.has(ScreenEvent::SWITCH_HOME_MENU)){ + return {.screen = LaunchScreen::HOME}; + } + if (policy.new_game && policy.new_game(look)){ + return {.screen = LaunchScreen::NEW_GAME}; + } + for (const StoryLaunchPrompt& prompt : policy.prompts){ + if ((launched || !prompt.after_launch) && prompt.matches && prompt.matches(look)){ + return {.screen = LaunchScreen::PROMPT, .prompt = &prompt}; + } + } + if ((launched || !policy.title_after_launch) && policy.title(look)){ + return {.screen = LaunchScreen::TITLE}; + } + const bool overworld = policy.overworld ? policy.overworld(look) : look.events.has(ScreenEvent::PLAYER_CAN_MOVE); + return {.screen = overworld ? LaunchScreen::OVERWORLD : LaunchScreen::OTHER}; +} + +Outcome launch_failed(std::string detail){ + return outcome_failed(std::string(STORY_LAUNCH_FAILED), std::move(detail)); +} + +// Why `policy` cannot launch `request`, before any press; nothing when it +// can. +std::optional launch_problem(const StoryLaunchPolicy& policy, StoryLaunchRequest request){ + if (!policy.tile_selected || !policy.title){ + return launch_failed(std::format("{}: the launch policy has no tile or title check", policy.game)); + } + if (request.save == StoryLaunchRequest::Save::BACKUP && policy.backup_save.empty()){ + return launch_failed(std::format("{}: the game has no backup save", policy.game)); + } + return std::nullopt; +} + + +// One launch: a look, one thing done about it, until the overworld has +// settled or a screen took too many presses. +template +class LaunchDriver{ +public: + LaunchDriver(StoryBackend& backend, const StoryLaunchPolicy& policy, StoryLaunchRequest request) + : m_backend(backend) + , m_policy(policy) + , m_request(request) + {} + + // One look and its answer; the launch's end when this look ended it. + std::optional step(){ + const Observation observation = m_backend.observe(); + const LaunchLook look = read_launch_look(m_policy, observation, m_launched || m_loading); + if (look.screen != LaunchScreen::OVERWORLD){ + m_settled = 0; + } + switch (look.screen){ + case LaunchScreen::HOME: + return on_home(observation); + case LaunchScreen::NEW_GAME: + return launch_failed(std::format("{}: the game shows a new game: no save to load, and a launch never starts one", m_policy.game)); + case LaunchScreen::PROMPT: + return on_prompt(*look.prompt); + case LaunchScreen::TITLE: + return on_title(); + case LaunchScreen::OVERWORLD: + return on_overworld(); + case LaunchScreen::OTHER: + break; + } + return on_other(); + } + +private: + // Whether an overworld up now is not yet the save asked for. + bool needs_load() const{ + return !m_loading && (m_request.running == StoryLaunchRequest::Running::RESTART || m_request.save == StoryLaunchRequest::Save::BACKUP); + } + + // The cursor to the game's tile, then the game closed if it must + // load again, then A on the tile. + std::optional on_home(const ScreenObservation& observation){ + if (m_launch_pressed && m_home_waited < m_policy.launch_wait){ + if (m_home_waited == Milliseconds(0)){ + log("home", "waiting for the game to start"); + } + m_home_waited += m_policy.poll; + m_backend.wait(m_policy.poll); + return std::nullopt; + } + if (!m_policy.tile_selected(observation)){ + return move_to_tile(); + } + if (needs_load()){ + m_loading = true; + return press("home", {.button = BUTTON_X}, m_policy.presses_per_screen, "close the game"); + } + m_launched = true; + m_launch_pressed = true; + m_home_waited = Milliseconds(0); + return press("home", {.button = BUTTON_A}, m_policy.presses_per_screen, "launch the game"); + } + + std::optional move_to_tile(){ + const size_t left = m_policy.tile_search; + if (m_tile_moves >= 3 * left){ + return launch_failed(std::format("{}: the game's tile is not on HOME: {} tiles searched", m_policy.game, m_tile_moves)); + } + const Button button = m_tile_moves < left ? BUTTON_LEFT : BUTTON_RIGHT; + m_tile_moves++; + log("home", std::format("not on the game's tile: {}", button_to_string(button))); + m_backend.press(button, Milliseconds(80), Milliseconds(400)); + return std::nullopt; + } + + // The prompt's lead-in presses, then its press. + std::optional on_prompt(const StoryLaunchPrompt& prompt){ + if (m_presses[prompt.name] < prompt.max_presses){ + for (const StoryLaunchPress& each : prompt.before){ + m_backend.press(each.button, each.hold, each.release); + } + } + return press(prompt.name, prompt.press, prompt.max_presses); + } + + std::optional on_title(){ + m_loading = true; + m_launched = true; + if (m_request.save == StoryLaunchRequest::Save::BACKUP && !m_backup_sent){ + m_backup_sent = true; + log("title", "the backup save"); + for (const StoryLaunchPress& each : m_policy.backup_save){ + m_backend.press(each.button, each.hold, each.release); + } + } + return press("title", m_policy.title_press, m_policy.presses_per_screen); + } + + std::optional on_overworld(){ + if (needs_load()){ + return press("overworld", {.button = BUTTON_HOME}, m_policy.presses_per_screen, "HOME, to load the save"); + } + if (++m_settled >= m_policy.settled_looks){ + log("overworld", std::format("settled for {} looks", m_settled)); + return outcome_ok(); + } + m_backend.wait(m_policy.poll); + return std::nullopt; + } + + // A screen the policy doesn't know: the game's boot or a load once + // launched; before that the game was in front already, left with HOME + // to load the save, or B toward its overworld. + std::optional on_other(){ + if (m_launched || m_loading){ + m_backend.wait(m_policy.poll); + return std::nullopt; + } + if (needs_load()){ + return press("unknown screen", {.button = BUTTON_HOME}, m_policy.presses_per_screen, "HOME, to load the save"); + } + return press("unknown screen", {.button = BUTTON_B}, m_policy.presses_per_screen, "B, toward the overworld"); + } + + // `what` on `screen`, unless `screen` already took `max` presses. + std::optional press(std::string_view screen, const StoryLaunchPress& what, size_t max, std::string_view why = {}){ + size_t& count = m_presses[std::string(screen)]; + if (count >= max){ + return launch_failed(std::format("{}: stuck on {}: {} presses changed nothing", m_policy.game, screen, count)); + } + count++; + log(screen, why.empty() ? button_to_string(what.button) : std::string(why)); + m_backend.press(what.button, what.hold, what.release); + return std::nullopt; + } + + void log(std::string_view screen, std::string_view what){ + m_backend.log(std::format("[Launch] {}: {}: {}", m_policy.game, screen, what)); + } + + StoryBackend& m_backend; + const StoryLaunchPolicy& m_policy; + StoryLaunchRequest m_request; + std::map> m_presses; + size_t m_tile_moves = 0; + size_t m_settled = 0; + // The game was closed from HOME, or showed its title: what comes up + // next is a save loading. + bool m_loading = false; + // A on the game's tile, or the title: until the overworld, a screen + // the policy doesn't know is the game booting or loading. + bool m_launched = false; + // A was pressed on the game's tile, and HOME has shown for + // `m_home_waited` since. + bool m_launch_pressed = false; + Milliseconds m_home_waited = Milliseconds(0); + bool m_backup_sent = false; +}; + + +} // namespace + + +std::function switch_home_tile_named(Language language, std::string name){ + return [language, name = std::move(name)](const ScreenObservation& look){ + return look.frame && look.frame->frame && selected_home_tile_is(language, *look.frame->frame, name); + }; +} + + +template +Outcome launch_story_game(StoryBackend& backend, const StoryLaunchPolicy& policy, StoryLaunchRequest request){ + if (std::optional problem = launch_problem(policy, request)){ + backend.log(std::format("[Launch] {}", problem->detail)); + return std::move(*problem); + } + LaunchDriver driver(backend, policy, request); + for (size_t looks = 0; looks < policy.max_looks; looks++){ + backend.scope().throw_if_cancelled(); + if (std::optional end = driver.step()){ + if (!end->succeeded()){ + backend.log(std::format("[Launch] {}", end->detail)); + } + return std::move(*end); + } + } + const Outcome failed = launch_failed(std::format("{}: no overworld after {} looks", policy.game, policy.max_looks)); + backend.log(std::format("[Launch] {}", failed.detail)); + return failed; +} + +template Outcome launch_story_game(StoryBackend&, const StoryLaunchPolicy&, StoryLaunchRequest); +template Outcome launch_story_game(StoryBackend&, const StoryLaunchPolicy&, StoryLaunchRequest); + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.h new file mode 100644 index 0000000000..eb90245801 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.h @@ -0,0 +1,164 @@ +/* Story Launch + * + * From: https://github.com/PokemonAutomation/ + * + * A run's first event: from wherever the console is (the Switch's HOME + * menu with the game closed or suspended, the game's title, the + * overworld) to the overworld on a save. One driver for every game; the + * game's StoryLaunchPolicy says how its screens look and which press + * answers each. It runs before the first checkpoint, on its own, never + * inside a new game's language or intro steps, and it never starts a new + * game. + * + * The driver looks, does one thing, and looks again. It does not need to + * know every screen: one it doesn't know is waited out (a boot, a fade) + * or, before the game was launched, left with HOME or B. Each screen's + * presses are counted, so a screen that ignores its press fails the + * launch instead of looping, and the launch ends only once the overworld + * shows for several looks in a row. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryLaunch_H +#define PokemonAutomation_Pokemon_StoryLaunch_H + +#include +#include +#include +#include +#include +#include "Common/Cpp/Time.h" +#include "CommonFramework/Language.h" +#include "CommonTools/ScreenMachine/ScreenObservation.h" +#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" +#include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" +#include "Pokemon_StoryBackend.h" +#include "Pokemon_ZonePose.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// What a launch loads. +struct StoryLaunchRequest{ + enum class Save{ + LATEST, // the title's plain load + BACKUP, // the policy's backup_save presses on the title first + // (PLZA: Up, then B + X, the save before the latest) + }; + enum class Running{ + // A game already on the overworld is the start: it is resumed + // from HOME, not reloaded. + KEEP, + // A game already running is closed from HOME, then launched, so + // the save loads; a title on screen already counts as launched. + RESTART, + }; + Save save = Save::LATEST; + Running running = Running::RESTART; +}; + +// One press: `button` (several pressed together when or-ed), held for +// `hold`, then let go for `release`. +struct StoryLaunchPress{ + NintendoSwitch::Button button = NintendoSwitch::BUTTON_A; + Milliseconds hold = Milliseconds(80); + Milliseconds release = Milliseconds(400); +}; + +// A screen on the way that one press answers: the user select, an online +// license check, HOME's close-software dialog, the load menu's Continue, +// a recap of the story so far. +struct StoryLaunchPrompt{ + std::string name = {}; + std::function matches = {}; + StoryLaunchPress press = {}; + // Pressed in order before `press`, each time: the cursor moves to the + // choice `press` picks (the update prompt's last choice). + std::vector before = {}; + // Read only once this launch launched or closed the game: a screen + // the game also shows in play (a dialog box, a choice), where a press + // before the launch would answer the story instead. + bool after_launch = false; + // Presses it may take before the launch is stuck on it. + size_t max_presses = 6; +}; + +// How one game's screens look on the way from HOME to its overworld. +struct StoryLaunchPolicy{ + // "frlg", "bdsp", "plza", for the log. + std::string game = {}; + // On HOME: whether the cursor is on this game's tile. The launch + // presses A on HOME only then, as A launches whatever tile is + // selected. Required; on a Switch, switch_home_tile_named() with the + // name HOME shows for the game. + std::function tile_selected = {}; + // How far the tile search moves each way: Left `tile_search` times + // (the most recently played games are leftmost), then Right twice as + // many. + size_t tile_search = 6; + // The title screen, which leads to the save. Required. + std::function title = {}; + StoryLaunchPress title_press = {}; + // `title` is read only once this launch launched or closed the game: + // a loose check (any quiet screen after the boot) that a game in play + // would pass too. + bool title_after_launch = false; + // Pressed once on the title before `title_press` for Save::BACKUP. + // Empty: the game has no backup save, and a BACKUP launch fails + // before it presses anything. + std::vector backup_save = {}; + // A screen that would start a new game: there is no save. The launch + // fails there without a press. Optional. + std::function new_game = {}; + // The game's other screens on the way, each answered by its press. + std::vector prompts = {}; + // The player in control. Unset: the look shows PLAYER_CAN_MOVE. + std::function overworld = {}; + // Looks in a row the overworld must show before the launch is done. + size_t settled_looks = 3; + // The wait between looks at a screen with nothing to press. + Milliseconds poll = Milliseconds(500); + // After A on the game's tile, how long HOME may still show (its zoom + // into the game, a check or the user select in front of it) before + // the launch takes A again: until then each look at HOME waits, as + // another A there would launch the game again. + Milliseconds launch_wait = Milliseconds(30000); + // Presses HOME, the title, an overworld to leave or a screen the + // policy doesn't know may take before the launch is stuck there. + size_t presses_per_screen = 6; + // Looks before the launch gives up: about `poll` times this of + // loading at most. + size_t max_looks = 360; +}; + +// A `tile_selected` for a Switch game: HOME's cursor is on the tile whose +// name, above the game row, contains `name` ("Pokémon Legends: Z-A"), +// read in `language`, the console's (NintendoSwitch_HomeTileReader.h). +// False on a look without a frame, as in a simulation. +std::function switch_home_tile_named(Language language, std::string name); + +// The failure of a launch that did not reach the overworld; its detail +// says where it stopped. Nothing in the story answers it: no reload, no +// recovery. +inline constexpr std::string_view STORY_LAUNCH_FAILED = "launch_failed"; + +// Drives `backend` to the overworld on the save `request` asks for, by +// `policy`; logs each press as "[Launch] : : ". +// Succeeds once the overworld shows `policy.settled_looks` looks in a +// row; fails as STORY_LAUNCH_FAILED on a new game, a missing tile, a +// screen that took too many presses, or after `policy.max_looks` looks. +template +Outcome launch_story_game(StoryBackend& backend, const StoryLaunchPolicy& policy, StoryLaunchRequest request); + +// Instantiated once, in Pokemon_StoryLaunch.cpp. +extern template Outcome launch_story_game(StoryBackend&, const StoryLaunchPolicy&, StoryLaunchRequest); +// And for ZonePose (Pokemon_ZonePose.h). +extern template Outcome launch_story_game(StoryBackend&, const StoryLaunchPolicy&, StoryLaunchRequest); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch_Tests.cpp new file mode 100644 index 0000000000..1853b06ac0 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch_Tests.cpp @@ -0,0 +1,311 @@ +/* Story Launch Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The launch on a scripted console: each case is the screens the console + * shows, look by look (the last repeats), and the presses the launch must + * make, or where it must fail. The test game's screens are readings its + * policy reads; HOME is the shared SWITCH_HOME_MENU event and the + * overworld the shared PLAYER_CAN_MOVE. + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon_StoryFakeBackend.h" +#include "Pokemon_StoryLaunch.h" +#include "Pokemon_StoryLaunch_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + +using namespace NintendoSwitch; + + +namespace{ + + +// What the scripted console shows. +enum class Screen{ + HOME_ON_GAME, // HOME, the cursor on the game's tile + HOME_OFF_GAME, // HOME, the cursor on another tile + CLOSE_DIALOG, // HOME's "Close software?" + LICENSE_CHECK, // the online license check + BLACK, // a boot or a load + TITLE, + NEW_GAME, // the title's menu without a save + BACKUP_CONFIRM, // "Load the backup save?" + LOAD_MENU, // Continue / New Game + UPDATE, // the update prompt: Up twice to its last choice, then A + GAME_DIALOG, // the game's dialog box, in play or on the way + OVERWORLD, +}; + +inline constexpr std::string_view SCREEN = "screen"; +inline constexpr std::string_view TILE = "home.tile"; + +Observation look(Screen screen){ + Observation observation; + switch (screen){ + case Screen::HOME_ON_GAME: + case Screen::HOME_OFF_GAME: + observation.events = ScreenEvents{ScreenEvent::SWITCH_HOME_MENU}; + observation.readings[std::string(TILE)] = screen == Screen::HOME_ON_GAME ? 1 : 0; + return observation; + case Screen::OVERWORLD: + observation.events = ScreenEvents{ScreenEvent::PLAYER_CAN_MOVE}; + return observation; + default: + observation.readings[std::string(SCREEN)] = (int)screen; + return observation; + } +} + +std::function shows(Screen screen){ + return [screen](const ScreenObservation& observation){ + const auto it = observation.readings.find(SCREEN); + return it != observation.readings.end() && it->second == (int)screen; + }; +} + +// Whether the test game has a backup save. +enum class Backup{ + PLZA, // Up, then B + X + NONE, +}; + +// How the test game finds its tile on HOME. +enum class TileCheck{ + READINGS, // the scripted "home.tile" reading + SWITCH_NAME, // switch_home_tile_named(), which reads the look's frame +}; + +// How loose the test game's title and dialog checks are. +enum class GameScreens{ + STRICT, // read whenever they show + AFTER_LAUNCH, // read only once the launch launched the game +}; + +// The test game: A on the title, `backup` for the backup save, A on each +// prompt (Up, Up, A on the update prompt; B on the game's dialog), and +// small bounds so a failure takes few looks. +StoryLaunchPolicy test_policy(Backup backup, GameScreens game_screens, TileCheck tile_check){ + StoryLaunchPolicy policy; + policy.game = "test"; + policy.tile_selected = [](const ScreenObservation& observation){ + const auto it = observation.readings.find(TILE); + return it != observation.readings.end() && it->second == 1; + }; + if (tile_check == TileCheck::SWITCH_NAME){ + policy.tile_selected = switch_home_tile_named(Language::English, "Pokémon Legends: Z-A"); + } + policy.tile_search = 2; + policy.title = shows(Screen::TITLE); + if (backup == Backup::PLZA){ + policy.backup_save = {{.button = BUTTON_UP}, {.button = BUTTON_B | BUTTON_X, .hold = Milliseconds(1000)}}; + } + policy.new_game = shows(Screen::NEW_GAME); + policy.prompts = { + {.name = "close software", .matches = shows(Screen::CLOSE_DIALOG)}, + {.name = "license check", .matches = shows(Screen::LICENSE_CHECK)}, + {.name = "backup confirm", .matches = shows(Screen::BACKUP_CONFIRM)}, + {.name = "load menu", .matches = shows(Screen::LOAD_MENU)}, + {.name = "update", .matches = shows(Screen::UPDATE), .before = {{.button = BUTTON_UP}, {.button = BUTTON_UP}}}, + {.name = "game dialog", .matches = shows(Screen::GAME_DIALOG), .press = {.button = BUTTON_B}, + .after_launch = game_screens == GameScreens::AFTER_LAUNCH}, + }; + policy.title_after_launch = game_screens == GameScreens::AFTER_LAUNCH; + policy.presses_per_screen = 3; + policy.launch_wait = Milliseconds(2000); + policy.max_looks = 12; + return policy; +} + +// `screens`, then the overworld for as many looks as it takes to settle. +std::vector then_settled(std::vector screens){ + screens.insert(screens.end(), {Screen::OVERWORLD, Screen::OVERWORLD, Screen::OVERWORLD}); + return screens; +} + +using Save = StoryLaunchRequest::Save; +using Running = StoryLaunchRequest::Running; + +// One launch. +struct LaunchCase{ + struct Input{ + std::vector screens; + StoryLaunchRequest request = {}; + Backup backup = Backup::PLZA; + GameScreens game_screens = GameScreens::STRICT; + TileCheck tile_check = TileCheck::READINGS; + }; + struct Expectation{ + // Empty: the launch succeeds; else a part of its failure's detail. + std::string_view failure = {}; + // The inputs in order, by space: a button ("A", "HOME", "B+X") + // or "wait". + std::string_view inputs = {}; + }; + std::string_view name; + Input input; + Expectation expectation; +}; + +const std::vector LAUNCH_CASES = { + {.name = "game closed on HOME", + .input = {.screens = then_settled({Screen::HOME_ON_GAME, Screen::HOME_ON_GAME, Screen::BLACK, Screen::TITLE, Screen::BLACK})}, + .expectation = {.inputs = "X A wait A wait wait wait"}}, + {.name = "game open on its title", + .input = {.screens = then_settled({Screen::TITLE, Screen::BLACK})}, + .expectation = {.inputs = "A wait wait wait"}}, + {.name = "game on the overworld kept", + .input = {.screens = then_settled({}), .request = {.running = Running::KEEP}}, + .expectation = {.inputs = "wait wait"}}, + {.name = "game on the overworld restarted", + .input = {.screens = then_settled({Screen::OVERWORLD, Screen::HOME_ON_GAME, Screen::CLOSE_DIALOG, Screen::HOME_ON_GAME, + Screen::BLACK, Screen::TITLE})}, + .expectation = {.inputs = "HOME X A A wait A wait wait"}}, + {.name = "backup save from the overworld", + .input = {.screens = then_settled({Screen::OVERWORLD, Screen::HOME_ON_GAME, Screen::HOME_ON_GAME, Screen::TITLE, + Screen::BACKUP_CONFIRM, Screen::BLACK}), + .request = {.save = Save::BACKUP, .running = Running::KEEP}}, + .expectation = {.inputs = "HOME X A Up B+X A A wait wait wait"}}, + {.name = "backup save the game does not have", + .input = {.screens = then_settled({}), .request = {.save = Save::BACKUP}, .backup = Backup::NONE}, + .expectation = {.failure = "no backup save", .inputs = ""}}, + {.name = "license check back to HOME", + .input = {.screens = then_settled({Screen::HOME_ON_GAME, Screen::HOME_ON_GAME, Screen::LICENSE_CHECK, Screen::HOME_ON_GAME, + Screen::BLACK, Screen::TITLE, Screen::LOAD_MENU})}, + .expectation = {.inputs = "X A A wait wait A A wait wait"}}, + // Console B, 2026-11-14 02:41 (FRLG): HOME showed for about 1.5 s + // after the A on the tile, and each look there pressed A again. + {.name = "HOME after the launch press waits", + .input = {.screens = then_settled({Screen::HOME_ON_GAME, Screen::HOME_ON_GAME, Screen::HOME_ON_GAME, Screen::HOME_ON_GAME, + Screen::HOME_ON_GAME, Screen::BLACK, Screen::TITLE})}, + .expectation = {.inputs = "X A wait wait wait wait A wait wait"}}, + {.name = "HOME past the launch wait takes A again", + .input = {.screens = then_settled({Screen::HOME_ON_GAME, Screen::HOME_ON_GAME, Screen::HOME_ON_GAME, Screen::HOME_ON_GAME, + Screen::HOME_ON_GAME, Screen::HOME_ON_GAME, Screen::HOME_ON_GAME, Screen::TITLE})}, + .expectation = {.inputs = "X A wait wait wait wait A A wait wait"}}, + {.name = "cursor on another tile", + .input = {.screens = then_settled({Screen::HOME_OFF_GAME, Screen::HOME_OFF_GAME, Screen::HOME_ON_GAME}), + .request = {.running = Running::KEEP}}, + .expectation = {.inputs = "Left Left A wait wait"}}, + {.name = "game not on HOME", + .input = {.screens = {Screen::HOME_OFF_GAME}}, + .expectation = {.failure = "tile is not on HOME", .inputs = "Left Left Right Right Right Right"}}, + {.name = "Switch tile check never launches without a frame", + .input = {.screens = {Screen::HOME_ON_GAME}, .tile_check = TileCheck::SWITCH_NAME}, + .expectation = {.failure = "tile is not on HOME", .inputs = "Left Left Right Right Right Right"}}, + {.name = "title ignores A", + .input = {.screens = {Screen::TITLE}}, + .expectation = {.failure = "stuck on title", .inputs = "A A A"}}, + {.name = "no save never starts a new game", + .input = {.screens = {Screen::NEW_GAME}}, + .expectation = {.failure = "new game", .inputs = ""}}, + {.name = "black screen after launch times out", + .input = {.screens = {Screen::HOME_ON_GAME, Screen::HOME_ON_GAME, Screen::BLACK}}, + .expectation = {.failure = "no overworld after 12 looks", .inputs = "X A wait wait wait wait wait wait wait wait wait wait"}}, + {.name = "kept game off the overworld takes B", + .input = {.screens = then_settled({Screen::LOAD_MENU, Screen::BLACK}), .request = {.running = Running::KEEP}}, + .expectation = {.inputs = "A B wait wait"}}, + {.name = "update prompt takes its lead-in presses", + .input = {.screens = then_settled({Screen::HOME_ON_GAME, Screen::HOME_ON_GAME, Screen::UPDATE, Screen::BLACK, Screen::TITLE})}, + .expectation = {.inputs = "X A Up Up A wait A wait wait"}}, + {.name = "strict title in front is pressed", + .input = {.screens = then_settled({Screen::GAME_DIALOG, Screen::TITLE, Screen::BLACK})}, + .expectation = {.inputs = "B A wait wait wait"}}, + {.name = "loose title in front is left with HOME", + .input = {.screens = then_settled({Screen::GAME_DIALOG, Screen::TITLE, Screen::HOME_ON_GAME, Screen::HOME_ON_GAME, + Screen::BLACK, Screen::GAME_DIALOG, Screen::TITLE}), + .game_screens = GameScreens::AFTER_LAUNCH}, + .expectation = {.inputs = "HOME HOME X A wait B A wait wait"}}, +}; + +// The fake backend's record of `input` ("A" -> "press A"). +std::string recorded(std::string_view input){ + if (input == "wait"){ + return "wait 500ms"; + } + static const std::map BUTTONS = { + {"A", BUTTON_A}, {"B", BUTTON_B}, {"X", BUTTON_X}, {"HOME", BUTTON_HOME}, + {"Up", BUTTON_UP}, {"Left", BUTTON_LEFT}, {"Right", BUTTON_RIGHT}, {"B+X", BUTTON_B | BUTTON_X}, + }; + const auto it = BUTTONS.find(input); + return it == BUTTONS.end() ? std::format("unknown input {}", input) : std::format("press {}", button_to_string(it->second)); +} + +// `inputs` (by space) as the fake backend records them. +std::vector expected_inputs(std::string_view inputs){ + std::vector expected; + while (!inputs.empty()){ + const size_t space = inputs.find(' '); + expected.push_back(recorded(inputs.substr(0, space))); + inputs = space == std::string_view::npos ? std::string_view() : inputs.substr(space + 1); + } + return expected; +} + +std::string describe_inputs(const std::vector& inputs){ + std::string text; + for (const std::string& input : inputs){ + text += text.empty() ? input : std::format(", {}", input); + } + return std::format("[{}]", text); +} + +std::string check_launch(const LaunchCase& test, CancellableScope& scope){ + std::vector> looks; + for (Screen screen : test.input.screens){ + looks.push_back(look(screen)); + } + FakeStoryBackend backend({.looks = std::move(looks)}, scope); + const Outcome outcome = launch_story_game(backend, test_policy(test.input.backup, test.input.game_screens, test.input.tile_check), test.input.request); + if (test.expectation.failure.empty() && !outcome.succeeded()){ + return std::format("expected success, got {}", to_string(outcome)); + } + if (!test.expectation.failure.empty()){ + if (outcome.failure != STORY_LAUNCH_FAILED || outcome.detail.find(test.expectation.failure) == std::string::npos){ + return std::format("expected a launch failure with \"{}\", got {}", test.expectation.failure, to_string(outcome)); + } + } + const std::vector expected = expected_inputs(test.expectation.inputs); + if (backend.inputs() != expected){ + return std::format("expected inputs {}, got {}", describe_inputs(expected), describe_inputs(backend.inputs())); + } + return ""; +} + +class StoryLaunchTest : public UnitTest{ +public: + explicit StoryLaunchTest(LaunchCase test) + : UnitTest(std::format("Pokemon::StoryLaunch - {}", test.name)) + , m_case(std::move(test)) + {} + virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ + const std::string error = check_launch(m_case, scope); + return error.empty() ? UnitTestResult(true) : UnitTestResult(error); + } +private: + LaunchCase m_case; +}; + + +} // namespace + + +void add_tests_StoryLaunch(UnitTestDatabase& database){ + for (const LaunchCase& test : LAUNCH_CASES){ + database.add(test); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch_Tests.h new file mode 100644 index 0000000000..efba4cc847 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryLaunch_Tests.h @@ -0,0 +1,23 @@ +/* Story Launch Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryLaunch_Tests_H +#define PokemonAutomation_Pokemon_StoryLaunch_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for the launch from HOME to the overworld on a scripted console. +void add_tests_StoryLaunch(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStoryStats.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStoryStats.cpp deleted file mode 100644 index 42ca1aea91..0000000000 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStoryStats.cpp +++ /dev/null @@ -1,22 +0,0 @@ -/* AutoStory Stats - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#include "Pokemon_AutoStoryStats.h" - -namespace PokemonAutomation{ -namespace Pokemon{ - - -void record_story_counts(const StoryRunCounts& counts, AutoStoryStats& stats){ - stats.m_segment += counts.segments; - stats.m_checkpoint += counts.checkpoints; - stats.m_reset += counts.resets; -} - - - -} // namespace Pokemon -} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStoryStats.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStoryStats.h index 8daa049cf6..47951b2635 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStoryStats.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStoryStats.h @@ -7,7 +7,8 @@ #ifndef PokemonAutomation_Pokemon_AutoStoryStats_H #define PokemonAutomation_Pokemon_AutoStoryStats_H -#include +#include +#include #include "CommonFramework/ProgramStats/StatsTracking.h" namespace PokemonAutomation{ @@ -32,19 +33,6 @@ struct AutoStoryStats : public StatsTracker{ }; -// What a story run adds to the program's stats. -struct StoryRunCounts{ - size_t segments = 0; - // Checkpoints passed (and saved at). - size_t checkpoints = 0; - // Game resets or reloads after failed attempts. - size_t resets = 0; -}; - -// Adds `counts` to the shared AutoStory stats. -void record_story_counts(const StoryRunCounts& counts, AutoStoryStats& stats); - - } // namespace Pokemon } // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h index 906540637b..d1ae2ba817 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -13,21 +13,24 @@ #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "Data/Pokemon_AuthoredCheckpointJson_Tests.h" +#include "Data/Pokemon_StoryFiles_Tests.h" #include "Data/Pokemon_StoryStepJson_Tests.h" #include "Engine/Pokemon_StoryEnginePolicy_Tests.h" +#include "Engine/Pokemon_StoryLaunch_Tests.h" #include "Engine/Pokemon_StoryEngineRuns_Tests.h" #include "Engine/Pokemon_StoryEngine_Tests.h" #include "Pokemon/Options/Pokemon_StoryRangeOptions_Tests.h" #include "Pokemon_AutoStorySelect_Tests.h" -#include "Pokemon_StoryRun_Tests.h" -#include "Pokemon_AutoStoryStats_Tests.h" +#include "Pokemon_CheckpointRetry_Tests.h" #include "Pokemon_Interrupts_Tests.h" #include "Pokemon_SceneJudges_Tests.h" -#include "Pokemon_ScreenEvents_Tests.h" #include "Pokemon_StoryActions_Tests.h" #include "Pokemon_StoryEvents_Tests.h" #include "Pokemon_StoryInterrupts_Tests.h" #include "Pokemon_StoryLog_Tests.h" +#include "Pokemon_StoryRngHandoff_Tests.h" +#include "Pokemon_StoryRunCounts_Tests.h" +#include "Pokemon_StoryRun_Tests.h" #include "Pokemon_StorySavePolicy_Tests.h" #include "Pokemon_StorySegments_Tests.h" #include "Pokemon_StoryStepExecutor_Tests.h" @@ -42,8 +45,10 @@ namespace Pokemon{ inline void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_AutoStorySelect(database); add_tests_StoryRun(database); - add_tests_AutoStoryStats(database); + add_tests_StoryRunCounts(database); + add_tests_CheckpointRetry(database); add_tests_StoryLog(database); + add_tests_StoryRngHandoff(database); add_tests_StoryRangeOptions(database); add_tests_StoryEvents(database); add_tests_StorySegments(database); @@ -51,11 +56,12 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_StoryStepJson(database); add_tests_StoryActionArg(database); add_tests_AuthoredCheckpointJson(database); + add_tests_StoryFiles(database); add_tests_StorySavePolicy(database); add_tests_StoryEngine(database); add_tests_StoryEnginePolicy(database); add_tests_StoryEngineRuns(database); - add_tests_ScreenEvents(database); + add_tests_StoryLaunch(database); add_tests_SceneJudges(database); add_tests_Interrupts(database); add_tests_Transitions(database); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.cpp new file mode 100644 index 0000000000..39d63731ce --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.cpp @@ -0,0 +1,36 @@ +/* Checkpoint Attempts + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Pokemon_StoryLog.h" +#include "Pokemon_CheckpointAttempts.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +CheckpointAttemptsResult run_checkpoint_attempts(std::string_view checkpoint, const CheckpointAttempts& attempts){ + CheckpointAttemptsResult result; + for (size_t attempt = 1;; attempt++){ + result.attempts = attempt; + const AttemptResult outcome = attempts.run(attempt); + if (outcome == AttemptResult::PASSED){ + result.passed = true; + attempts.log(checkpoint_pass_line(checkpoint, attempt, attempts.add_checkpoint_prefix)); + return result; + } + if (outcome == AttemptResult::FINAL || attempt >= attempts.max_attempts){ + return result; + } + if (!attempts.reset(attempt + 1)){ + return result; + } + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h new file mode 100644 index 0000000000..3834e234f5 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h @@ -0,0 +1,74 @@ +/* Checkpoint Attempts + * + * From: https://github.com/PokemonAutomation/ + * + * The retry loop every AutoStory runs a checkpoint in: run an attempt, and + * when it fails go back to the checkpoint's start save and try again, + * until an attempt passes, fails in a way another attempt would repeat, + * the last attempt fails, or a reset fails. SV's loop + * (Pokemon_CheckpointRetry.h), the story segments and the story engine all + * run their checkpoints through it, so each writes the same pass line + * (Pokemon_StoryLog.h) and counts attempts the same way. The game supplies + * the attempt and the reset. + * + */ + +#ifndef PokemonAutomation_Pokemon_CheckpointAttempts_H +#define PokemonAutomation_Pokemon_CheckpointAttempts_H + +#include +#include +#include +#include + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// How one attempt at a checkpoint ended. +enum class AttemptResult{ + PASSED, + // Failed; another attempt from the start save may pass. + FAILED, + // Failed in a way another attempt would repeat (the game is not + // running): no more attempts. + FINAL, +}; + +// Writes one log line. +using CheckpointLogger = std::function; + +// What the game does at each step of the retry loop. +struct CheckpointAttempts{ + // Attempts before the loop gives up. + size_t max_attempts; + // Runs attempt `attempt`, counting from 1. + std::function run; + // Goes back to the checkpoint's start save before attempt + // `next_attempt`, writing its own log line. Returns false when it could + // not, which ends the loop. + std::function reset; + CheckpointLogger log; + // False when `checkpoint` is the game's own text that already says it + // is a checkpoint, so the pass line leaves out "[Checkpoint] " + // (checkpoint_start_line()). + bool add_checkpoint_prefix = true; +}; + +// What the loop came to. +struct CheckpointAttemptsResult{ + bool passed = false; + // Attempts run, counting the last one. + size_t attempts = 0; +}; + +// Runs `checkpoint` until an attempt passes, an attempt is FINAL, +// `max_attempts` attempts failed, or a reset failed. Writes the pass line. +// Exceptions from `run` and `reset` pass through. +CheckpointAttemptsResult run_checkpoint_attempts(std::string_view checkpoint, const CheckpointAttempts& attempts); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.cpp new file mode 100644 index 0000000000..6f8deb807b --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.cpp @@ -0,0 +1,70 @@ +/* Checkpoint Retry + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Pokemon_StoryLog.h" +#include "Pokemon_CheckpointRetry.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +size_t run_checkpoint_with_retries( + std::string_view checkpoint_text, + const CheckpointRetryPolicy& policy, + const CheckpointRetryActions& actions, + AutoStoryStats& stats +){ + actions.log(checkpoint_start_line(checkpoint_text, /*events=*/std::nullopt, /*add_checkpoint_prefix=*/false)); + const CheckpointAttempts attempts{ + .max_attempts = policy.max_attempts, + .run = [&](size_t attempt){ + actions.log(checkpoint_attempt_line( + checkpoint_text, attempt, policy.max_attempts, /*add_checkpoint_prefix=*/false + )); + try{ + actions.run(attempt); + return AttemptResult::PASSED; + }catch (OperationFailedException& e){ + if (attempt == policy.report_at_failure){ + actions.report(e); + } + if (attempt >= policy.max_attempts){ + actions.give_up(e); + throw; + } + return AttemptResult::FAILED; + } + }, + .reset = [&](size_t next_attempt){ + actions.log(checkpoint_reset_line( + checkpoint_text, next_attempt, /*attempts=*/std::nullopt, /*add_checkpoint_prefix=*/false + )); + actions.reset(); + stats.m_reset++; + actions.update_stats(); + return true; + }, + .log = actions.log, + .add_checkpoint_prefix = false, + }; + return run_checkpoint_attempts(checkpoint_text, attempts).attempts; +} + + +void record_checkpoint_saved( + const CheckpointStep& update_stats, + const std::function& notify, + AutoStoryStats& stats +){ + stats.m_checkpoint++; + update_stats(); + notify(CHECKPOINT_SAVED_MESSAGE); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.h new file mode 100644 index 0000000000..35afade804 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.h @@ -0,0 +1,87 @@ +/* Checkpoint Retry + * + * From: https://github.com/PokemonAutomation/ + * + * SV's checkpoint retry policy on the shared loop (Pokemon_CheckpointAttempts.h): + * an attempt fails by throwing OperationFailedException; on a failure SV + * resets the game to the checkpoint's save and tries again, reports once + * for debugging after some failures, and gives up after the last attempt. + * The game supplies what it does at each step; the policy, the counters + * and the log lines (Pokemon_StoryLog.h) live here. + * + */ + +#ifndef PokemonAutomation_Pokemon_CheckpointRetry_H +#define PokemonAutomation_Pokemon_CheckpointRetry_H + +#include +#include +#include +#include +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "Pokemon_AutoStoryStats.h" +#include "Pokemon_CheckpointAttempts.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// SV's policy: how often a checkpoint retries and when it reports. +struct CheckpointRetryPolicy{ + // Attempts before the checkpoint gives up. + size_t max_attempts; + // The failure count at which the game reports once for debugging. + size_t report_at_failure; +}; + +// A game step that takes nothing: a reset, a stats update. +using CheckpointStep = std::function; +// A game step on the failure that reached a policy count. +using CheckpointFailureStep = std::function; + +// What SV does at each step of the loop, where a failed attempt throws. +struct CheckpointRetryActions{ + // Runs attempt `attempt`, counting from 1. Throws + // OperationFailedException when the attempt fails. + std::function run; + // Sends the debugging report, with the exception of the failure that + // reached the policy's count. + CheckpointFailureStep report; + // Throws the game's final error after the last attempt failed. + CheckpointFailureStep give_up; + // Resets the game to the checkpoint's save. + CheckpointStep reset; + // Publishes changed stats. + CheckpointStep update_stats; + CheckpointLogger log; +}; + +// Runs the checkpoint through run_checkpoint_attempts() with the start, +// attempt and reset lines, until an attempt passes, and returns that +// attempt's number. SV's lines name the checkpoint by `checkpoint_text` +// ("Checkpoint:: 004 - 02 ..."), its display text. After the last attempt +// fails it calls `give_up` and rethrows. Each reset adds to `stats.m_reset`. +size_t run_checkpoint_with_retries( + std::string_view checkpoint_text, + const CheckpointRetryPolicy& policy, + const CheckpointRetryActions& actions, + AutoStoryStats& stats +); + + +// The status message sent after the game saved at a checkpoint. +inline constexpr std::string_view CHECKPOINT_SAVED_MESSAGE = "Saved at checkpoint."; + +// Counts a checkpoint the game just saved at, publishes the stats and +// sends CHECKPOINT_SAVED_MESSAGE through `notify`. +void record_checkpoint_saved( + const CheckpointStep& update_stats, + const std::function& notify, + AutoStoryStats& stats +); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.cpp new file mode 100644 index 0000000000..948c522412 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.cpp @@ -0,0 +1,366 @@ +/* Checkpoint Retry Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon_CheckpointRetry.h" +#include "Pokemon_CheckpointRetry_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + +namespace{ + +// Thrown by the test's give_up, so the test can tell it from the attempt's. +struct GaveUp{}; + +// A checkpoint whose first `failures` attempts fail. +struct CheckpointRetryInput{ + std::string name; + CheckpointRetryPolicy policy; + size_t failures; +}; + +struct CheckpointRetryExpectation{ + // The attempt that passed; 0 if the loop gave up. + size_t passed_on; + // The failure counts at which the loop reported. + std::vector reports; + size_t resets; + std::vector log; +}; + +// One retry loop run and what it must do. +struct CheckpointRetryCase{ + CheckpointRetryInput input; + CheckpointRetryExpectation expectation; +}; + +// What the loop did through the test's actions. +struct CheckpointRetryRecord{ + size_t passed_on = 0; + std::vector reports; + size_t resets = 0; + std::vector log; +}; + +CheckpointRetryRecord run_case(const CheckpointRetryInput& input, AutoStoryStats& stats){ + CheckpointRetryRecord record; + size_t attempts = 0; + CheckpointRetryActions actions{ + .run = [&](size_t attempt){ + attempts = attempt; + if (attempt <= input.failures){ + throw OperationFailedException(ErrorReportMode::NO_ERROR_REPORT, "failed"); + } + }, + .report = [&](const OperationFailedException&){ + record.reports.push_back(attempts); + }, + .give_up = [](const OperationFailedException&){ + throw GaveUp(); + }, + .reset = [&](){ + record.resets++; + }, + .update_stats = [](){}, + .log = [&](const std::string& line){ + record.log.push_back(line); + }, + }; + try{ + record.passed_on = run_checkpoint_with_retries("Checkpoint:: 01", input.policy, actions, stats); + }catch (GaveUp&){ + record.passed_on = 0; + } + return record; +} + +class Test_CheckpointRetry : public UnitTest{ +public: + Test_CheckpointRetry(CheckpointRetryCase test_case) + : UnitTest(std::format("Pokemon::CheckpointRetry - {}", test_case.input.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + AutoStoryStats stats; + const CheckpointRetryRecord record = run_case(m_case.input, stats); + const CheckpointRetryExpectation& expected = m_case.expectation; + if (record.passed_on != expected.passed_on){ + return std::format("Expected a pass on attempt {}, got {}", expected.passed_on, record.passed_on); + } + if (record.reports != expected.reports){ + return std::format("Expected {} reports, got {}", expected.reports.size(), record.reports.size()); + } + if (record.resets != expected.resets || stats.m_reset.load() != expected.resets){ + return std::format( + "Expected {} resets, got {} (stats {})", + expected.resets, record.resets, stats.m_reset.load() + ); + } + if (record.log != expected.log){ + return std::format("Expected {} log lines, got {}", expected.log.size(), record.log.size()); + } + return true; + } + +private: + const CheckpointRetryCase m_case; +}; + +const CheckpointRetryCase CHECKPOINT_RETRY_CASES[] = { + { + .input = {.name = "one-shot pass", .policy = {.max_attempts = 3, .report_at_failure = 2}, .failures = 0}, + .expectation = { + .passed_on = 1, + .reports = {}, + .resets = 0, + .log = { + "Checkpoint:: 01: start", + "Checkpoint:: 01: attempt 1/3", + "Checkpoint:: 01: passed on attempt 1 (one-shot)", + }, + }, + }, + { + .input = {.name = "pass after a reset", .policy = {.max_attempts = 3, .report_at_failure = 2}, .failures = 1}, + .expectation = { + .passed_on = 2, + .reports = {}, + .resets = 1, + .log = { + "Checkpoint:: 01: start", + "Checkpoint:: 01: attempt 1/3", + "Checkpoint:: 01: failed, resetting for attempt 2", + "Checkpoint:: 01: attempt 2/3", + "Checkpoint:: 01: passed on attempt 2 (1 reset)", + }, + }, + }, + { + .input = {.name = "report once at its failure count", .policy = {.max_attempts = 5, .report_at_failure = 2}, .failures = 3}, + .expectation = { + .passed_on = 4, + .reports = {2}, + .resets = 3, + .log = { + "Checkpoint:: 01: start", + "Checkpoint:: 01: attempt 1/5", + "Checkpoint:: 01: failed, resetting for attempt 2", + "Checkpoint:: 01: attempt 2/5", + "Checkpoint:: 01: failed, resetting for attempt 3", + "Checkpoint:: 01: attempt 3/5", + "Checkpoint:: 01: failed, resetting for attempt 4", + "Checkpoint:: 01: attempt 4/5", + "Checkpoint:: 01: passed on attempt 4 (3 resets)", + }, + }, + }, + { + .input = {.name = "give up after the last attempt, without a reset", .policy = {.max_attempts = 2, .report_at_failure = 2}, .failures = 5}, + .expectation = { + .passed_on = 0, + .reports = {2}, + .resets = 1, + .log = { + "Checkpoint:: 01: start", + "Checkpoint:: 01: attempt 1/2", + "Checkpoint:: 01: failed, resetting for attempt 2", + "Checkpoint:: 01: attempt 2/2", + }, + }, + }, +}; + + +// A checkpoint whose attempts end as `results` says, in order, and whose +// resets fail from `reset_fails_at` on (0: never). +struct CheckpointAttemptsInput{ + std::string name; + size_t max_attempts; + std::vector results; + size_t reset_fails_at = 0; +}; + +struct CheckpointAttemptsExpectation{ + bool passed; + size_t attempts; + // The attempt each reset was for. + std::vector resets; + std::vector log; +}; + +// One run of the shared loop and what it must do. +struct CheckpointAttemptsCase{ + CheckpointAttemptsInput input; + CheckpointAttemptsExpectation expectation; +}; + +class Test_CheckpointAttempts : public UnitTest{ +public: + Test_CheckpointAttempts(CheckpointAttemptsCase test_case) + : UnitTest(std::format("Pokemon::CheckpointRetry - attempts: {}", test_case.input.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const CheckpointAttemptsInput& input = m_case.input; + std::vector resets; + std::vector log; + const CheckpointAttempts attempts{ + .max_attempts = input.max_attempts, + .run = [&](size_t attempt){ + return attempt <= input.results.size() ? input.results[attempt - 1] : AttemptResult::FINAL; + }, + .reset = [&](size_t next_attempt){ + resets.push_back(next_attempt); + return input.reset_fails_at == 0 || next_attempt < input.reset_fails_at; + }, + .log = [&](const std::string& line){ + log.push_back(line); + }, + }; + const CheckpointAttemptsResult result = run_checkpoint_attempts("01", attempts); + const CheckpointAttemptsExpectation& expected = m_case.expectation; + if (result.passed != expected.passed || result.attempts != expected.attempts){ + return std::format( + "Expected passed {} after {} attempts, got {} after {}", + expected.passed, expected.attempts, result.passed, result.attempts + ); + } + if (resets != expected.resets){ + return std::format("Expected {} resets, got {}", expected.resets.size(), resets.size()); + } + if (log != expected.log){ + return std::format("Expected {} log lines, got {}", expected.log.size(), log.size()); + } + return true; + } + +private: + const CheckpointAttemptsCase m_case; +}; + +const CheckpointAttemptsCase CHECKPOINT_ATTEMPTS_CASES[] = { + { + .input = {.name = "pass after a reset", .max_attempts = 3, .results = {AttemptResult::FAILED, AttemptResult::PASSED}}, + .expectation = { + .passed = true, + .attempts = 2, + .resets = {2}, + .log = {"[Checkpoint] 01: passed on attempt 2 (1 reset)"}, + }, + }, + { + .input = {.name = "a final failure stops without a reset", .max_attempts = 3, .results = {AttemptResult::FINAL}}, + .expectation = {.passed = false, .attempts = 1, .resets = {}, .log = {}}, + }, + { + .input = {.name = "no reset after the last attempt", .max_attempts = 2, .results = {AttemptResult::FAILED, AttemptResult::FAILED}}, + .expectation = {.passed = false, .attempts = 2, .resets = {2}, .log = {}}, + }, + { + .input = { + .name = "a failed reset stops", + .max_attempts = 3, + .results = {AttemptResult::FAILED, AttemptResult::FAILED}, + .reset_fails_at = 3, + }, + .expectation = {.passed = false, .attempts = 2, .resets = {2, 3}, .log = {}}, + }, +}; + + +// `saves` checkpoint saves in a row. +struct CheckpointSavedInput{ + std::string name; + size_t saves; +}; + +struct CheckpointSavedExpectation{ + uint64_t checkpoints; + size_t updates; + std::vector messages; +}; + +// Checkpoint saves and what they must count and send. +struct CheckpointSavedCase{ + CheckpointSavedInput input; + CheckpointSavedExpectation expectation; +}; + +class Test_CheckpointSaved : public UnitTest{ +public: + Test_CheckpointSaved(CheckpointSavedCase test_case) + : UnitTest(std::format("Pokemon::CheckpointRetry - {}", test_case.input.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + AutoStoryStats stats; + size_t updates = 0; + std::vector messages; + for (size_t i = 0; i < m_case.input.saves; i++){ + record_checkpoint_saved( + [&](){ updates++; }, + [&](std::string_view message){ messages.emplace_back(message); }, + stats + ); + } + const CheckpointSavedExpectation& expected = m_case.expectation; + if (stats.m_checkpoint.load() != expected.checkpoints || updates != expected.updates){ + return std::format( + "Expected {} checkpoints and {} updates, got {} and {}", + expected.checkpoints, expected.updates, stats.m_checkpoint.load(), updates + ); + } + if (messages != expected.messages){ + return std::format("Expected {} messages, got {}", expected.messages.size(), messages.size()); + } + return true; + } + +private: + const CheckpointSavedCase m_case; +}; + +const CheckpointSavedCase CHECKPOINT_SAVED_CASES[] = { + { + .input = {.name = "each save counts, publishes and notifies", .saves = 2}, + .expectation = { + .checkpoints = 2, + .updates = 2, + .messages = {"Saved at checkpoint.", "Saved at checkpoint."}, + }, + }, +}; + + +} // namespace + + +void add_tests_CheckpointRetry(UnitTestDatabase& database){ + for (const CheckpointAttemptsCase& test_case : CHECKPOINT_ATTEMPTS_CASES){ + database.add(test_case); + } + for (const CheckpointRetryCase& test_case : CHECKPOINT_RETRY_CASES){ + database.add(test_case); + } + for (const CheckpointSavedCase& test_case : CHECKPOINT_SAVED_CASES){ + database.add(test_case); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.h new file mode 100644 index 0000000000..b7b5ccb6f1 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry_Tests.h @@ -0,0 +1,23 @@ +/* Checkpoint Retry Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_CheckpointRetry_Tests_H +#define PokemonAutomation_Pokemon_CheckpointRetry_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for the checkpoint retry loop and checkpoint saves. +void add_tests_CheckpointRetry(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_FadeProfiles.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_FadeProfiles.h index a4dd2ac915..7a2ac6ac4e 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_FadeProfiles.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_FadeProfiles.h @@ -2,7 +2,8 @@ * * From: https://github.com/PokemonAutomation/ * - * Split out of Pokemon_Transitions.h, unchanged; it includes this header. + * Per-game fade thresholds (FadeProfile) and the transition kinds the tracker + * reports. Their definitions are in Pokemon_Transitions.cpp. * */ @@ -10,12 +11,8 @@ #define PokemonAutomation_Pokemon_FadeProfiles_H #include -#include -#include #include #include "Common/Cpp/Time.h" -#include "CommonFramework/ImageTools/ImageStats.h" -#include "CommonFramework/VideoPipeline/VideoFeed.h" namespace PokemonAutomation{ namespace Pokemon{ @@ -64,7 +61,7 @@ struct FadeProfile{ }; // BDSP. The uniform tier is the shared walk's (FADE_MAX_STDDEV_SUM, the -// Oreburgh Gate's textured floor is no fade, lab-B 2026-11-08); the partial +// Oreburgh Gate's textured floor is no fade); the partial // tier is run_into_door()'s 60, which caught the doorway's dim fade frames. // A cave mouth's fade is dim (45 to 64 a channel), hence the dark sum of 225. // The flash and white-fade holds are provisional: no console frames of an diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_InterruptHandlers.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_InterruptHandlers.h index c574b2503f..8a0501aa1a 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_InterruptHandlers.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_InterruptHandlers.h @@ -2,7 +2,8 @@ * * From: https://github.com/PokemonAutomation/ * - * Split out of Pokemon_StoryInterrupts.h, unchanged; it includes this header. + * The table of a game's interrupt handlers and the screen look they read. + * Pokemon_StoryInterrupts.h builds on it. * */ @@ -16,7 +17,7 @@ #include #include #include "Pokemon_Interrupts.h" -#include "Pokemon_ScreenEvents.h" +#include "CommonTools/ScreenEvents/ScreenEvents.h" #include "Pokemon_StoryEvents.h" #include "Pokemon_StoryStepExecutor.h" diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Interrupts.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Interrupts.cpp index 53aedfbe7c..69a2ccfbbe 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Interrupts.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Interrupts.cpp @@ -5,7 +5,10 @@ */ #include +#include #include +#include +#include "CommonTools/ScreenEvents/ScreenFades.h" #include "Pokemon_Interrupts.h" namespace PokemonAutomation{ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Interrupts.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Interrupts.h index b7637ad044..989460cf65 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Interrupts.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Interrupts.h @@ -6,7 +6,7 @@ * (ScreenEventLog), the same way for every game: a wild encounter, a * trainer's sight, a dialog, a warp. One frame cannot tell them apart; the * order can. A white flash then a battle is an encounter, a white fade with - * no battle after it a cave exit (BDSP's walk fled one as a battle, #268); + * no battle after it a cave exit (BDSP's walk once fled one as a battle); * a box then a flash then a battle is a trainer's lines; a flash then a box * then a battle is a wild battle's opening text. * @@ -22,7 +22,7 @@ #include #include #include "Common/Cpp/Time.h" -#include "Pokemon_ScreenEvents.h" +#include "CommonTools/ScreenEvents/ScreenEvents.h" #include "Pokemon_Transitions.h" #include "Pokemon_WarpJudge.h" diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Interrupts_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Interrupts_Tests.cpp index f321c506ab..423d4769eb 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Interrupts_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Interrupts_Tests.cpp @@ -8,7 +8,6 @@ */ #include -#include #include #include #include @@ -17,7 +16,8 @@ #include "CommonFramework/ImageTypes/ImageRGB32.h" #include "Pokemon_Interrupts.h" #include "Pokemon_Interrupts_Tests.h" -#include "Pokemon_ScreenEvents.h" +#include "CommonTools/ScreenEvents/ScreenEvents.h" +#include "Pokemon_TestExpect.h" namespace PokemonAutomation{ namespace Pokemon{ @@ -28,13 +28,6 @@ using namespace std::chrono_literals; const WallClock T0 = WallClock() + std::chrono::hours(1); -UnitTestResult expect_equal(const std::string& what, const std::string& got, const std::string& expected){ - if (got == expected){ - return true; - } - return std::format("{}: expected \"{}\", got \"{}\"", what, expected, got); -} - // One frame of a synthetic scene: its events, at `ms` after T0. struct Frame{ int ms; @@ -186,17 +179,16 @@ UnitTestResult test_watcher_log(){ } -struct InterruptsInput{ - std::string name; - std::function body; -}; - -struct InterruptsExpectation{ -}; - struct InterruptsCase{ - InterruptsInput input; - InterruptsExpectation expectation; + struct Input{ + std::string name; + UnitTestResult (*body)(); + }; + struct Expectation{ + }; + + Input input; + Expectation expectation; }; class Test_Interrupts : public UnitTest{ @@ -214,23 +206,22 @@ class Test_Interrupts : public UnitTest{ }; -// A synthetic scene, and the window of it to judge. -struct ClassifyInput{ - std::string name; - std::vector frames; - int until_ms = 0; - InterruptKnobs knobs = {}; - int window_from_ms = 0; -}; - -struct ClassifyExpectation{ - // describe(verdict, T0). - std::string verdict; -}; - struct ClassifyCase{ - ClassifyInput input; - ClassifyExpectation expectation; + // A synthetic scene, and the window of it to judge. + struct Input{ + std::string name; + std::vector frames; + int until_ms = 0; + InterruptKnobs knobs = {}; + int window_from_ms = 0; + }; + struct Expectation{ + // describe(verdict, T0). + std::string verdict; + }; + + Input input; + Expectation expectation; }; class Test_InterruptClassify : public UnitTest{ @@ -240,7 +231,7 @@ class Test_InterruptClassify : public UnitTest{ , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - const ClassifyInput& input = m_case.input; + const ClassifyCase::Input& input = m_case.input; const ScreenEventLog log = log_of(input.frames, input.until_ms); const InterruptWindow window{T0 + Milliseconds(input.window_from_ms), T0 + Milliseconds(input.until_ms)}; const InterruptVerdict verdict = classify_interrupt(log, window, input.knobs); @@ -252,22 +243,21 @@ class Test_InterruptClassify : public UnitTest{ }; -// A synthetic scene, classified over all of it, and the lags to time it. -struct StepTimeInput{ - std::string name; - std::vector frames; - int until_ms = 0; - std::vector lags; -}; - -struct StepTimeExpectation{ - // "+700 ms" from T0, or "none". - std::string step; -}; - struct StepTimeCase{ - StepTimeInput input; - StepTimeExpectation expectation; + // A synthetic scene, classified over all of it, and the lags to time it. + struct Input{ + std::string name; + std::vector frames; + int until_ms = 0; + std::vector lags; + }; + struct Expectation{ + // "+700 ms" from T0, or "none". + std::string step; + }; + + Input input; + Expectation expectation; }; class Test_InterruptStepTime : public UnitTest{ @@ -277,7 +267,7 @@ class Test_InterruptStepTime : public UnitTest{ , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - const StepTimeInput& input = m_case.input; + const StepTimeCase::Input& input = m_case.input; const InterruptVerdict verdict = classify_interrupt(log_of(input.frames, input.until_ms), whole(input.until_ms), {}); const std::optional step = interrupt_step_time(verdict, input.lags); const std::string got = step diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp index 6e5fa9f4f8..9349d8f3b6 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp @@ -5,6 +5,7 @@ */ #include +#include "CommonTools/ScreenEvents/ScreenFades.h" #include "Pokemon_SceneJudges.h" namespace PokemonAutomation{ @@ -33,41 +34,40 @@ std::string describe(const WarpSeen& seen){ } -SceneMove TalkOver::look(const ScreenEvents& look, WallClock now){ - if (look.has(ScreenEvent::BATTLE_STARTED)){ - m_quiet_since = WallClock::max(); - m_calm_since = WallClock::max(); +SceneMove TalkOver::look(const ScreenEvents& events, WallClock now){ + if (events.has(ScreenEvent::BATTLE_STARTED)){ + m_quiet_since.reset(); + m_calm_since.reset(); return SceneMove::BATTLE; } - m_fade_showing = fade_in(look); + m_fade_showing = fade_in(events); if (m_fade_showing != WarpFade::NONE){ m_fade = m_fade_showing; } - m_banner |= look.has(ScreenEvent::LOCATION_BANNER); - const bool box = look.has(ScreenEvent::DIALOG_BOX); + m_banner |= events.has(ScreenEvent::LOCATION_BANNER); + const bool box = events.has(ScreenEvent::DIALOG_BOX); // A fade is never calm: BDSP's overworld reads a black frame as no // box, and black stays still, so a closing fade counted as quiet and - // ended Mom's scene as it went down (lab-B 2026-11-09 14:33). + // ended the scene as it went down. const bool calm = !box && m_fade_showing == WarpFade::NONE && - look.has(ScreenEvent::PLAYER_CAN_MOVE) && warped(); - const bool quiet = calm && look.has(m_rule.door ? ScreenEvent::PLAYER_STILL : ScreenEvent::SCREEN_STILL); + events.has(ScreenEvent::PLAYER_CAN_MOVE) && warped(); + const bool quiet = calm && events.has(m_rule.door ? ScreenEvent::PLAYER_STILL : ScreenEvent::SCREEN_STILL); if (!calm){ - m_calm_since = WallClock::max(); - }else if (m_calm_since == WallClock::max()){ + m_calm_since.reset(); + }else if (!m_calm_since.has_value()){ m_calm_since = now; } if (!quiet){ - m_quiet_since = WallClock::max(); - }else if (m_quiet_since == WallClock::max()){ + m_quiet_since.reset(); + }else if (!m_quiet_since.has_value()){ m_quiet_since = now; } const Milliseconds quiet_for = m_rule.door ? TalkOverRule::DOOR_QUIET : TALK_OVER_QUIET; - const Milliseconds calm_for = m_rule.door ? TalkOverRule::DOOR_CALM : TALK_OVER_CALM; + const Milliseconds calm_for = m_rule.door ? TalkOverRule::DOOR_CALM : m_rule.calm; const bool over = - (quiet && now - m_quiet_since >= quiet_for) || - (calm && now - m_calm_since >= calm_for); - + (quiet && now - *m_quiet_since >= quiet_for) || + (calm && now - *m_calm_since >= calm_for); if (over && m_rule.end_on_quiet){ return SceneMove::DONE; } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h index 594c90c5c8..f12c67dbe1 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h @@ -2,8 +2,8 @@ * * From: https://github.com/PokemonAutomation/ * - * Judgements a story step makes from screen events (Pokemon_ScreenEvents.h) - * over time, not from one look (#268's design note): a warp is a fade or + * Judgements a story step makes from screen events (CommonTools/ScreenEvents/ScreenEvents.h) + * over time, not from one look: a warp is a fade or * the banner of the map arrived on, and a talk or a scene is over when the * overworld has been quiet for a while. Pure: tests drive them with * scripted scenes, and every game shares them. @@ -13,15 +13,16 @@ #ifndef PokemonAutomation_Pokemon_SceneJudges_H #define PokemonAutomation_Pokemon_SceneJudges_H +#include #include #include "Common/Cpp/Time.h" -#include "Pokemon_ScreenEvents.h" +#include "CommonTools/ScreenEvents/ScreenEvents.h" namespace PokemonAutomation{ namespace Pokemon{ -// What a walk or a step saw of a warp (#268): a fade or the location +// What a walk or a step saw of a warp: a fade or the location // banner of the map arrived on. Either one alone is the warp. struct WarpSeen{ WarpFade fade = WarpFade::NONE; @@ -48,13 +49,22 @@ enum class SceneMove{ DONE, // The talk or scene is over. }; +// The overworld with no box and a still screen this long ends it: a scene +// pauses while someone walks off, a scripted walk-in can run 5 s with no +// text before someone speaks, and a speech pauses for a fanfare. The clock +// is the last look that was not quiet. +inline constexpr Milliseconds TALK_OVER_QUIET = Milliseconds(5000); +// The overworld with no box this long ends it, still or not (the rule's +// `calm`): wanderers or grass can keep the screen from ever being still. +inline constexpr Milliseconds TALK_OVER_CALM = Milliseconds(15000); + // What ends a talk-over, besides the quiet. struct TalkOverRule{ // A map change must come first: a fade or the banner. Only the quiet - // after it counts: r30's scene went quiet for 5 s before the fade home. + // after it counts: a scene can go quiet for 5 s before the fade home. bool warp = false; // False when a battle is to follow (a trainer's lines pause for more - // than 4 s before it, r210d): no quiet ends the talk then. + // than 4 s before it): no quiet ends the talk then. bool end_on_quiet = true; // The settle after a door or a warp, with no text to come: only the // fade back up and a short still spell around the player count, so @@ -69,19 +79,13 @@ struct TalkOverRule{ // The overworld back up this long ends it, still or not (someone // walking past the player the whole time). static constexpr Milliseconds DOOR_CALM = Milliseconds(2500); + // How long the overworld with no box ends it, still or not. Shorter + // for a game whose PLAYER_CAN_MOVE cue shows only briefly between + // lines: PLZA's party bar, held 1 s, is the overworld; its city is + // rarely still. + Milliseconds calm = TALK_OVER_CALM; }; -// The overworld with no box and a still screen this long ends it: a scene -// pauses while someone walks off (Dawn and Barry after Rowan leaves, r23), -// a scripted walk-in ran 5 s with no text before Mom spoke (r31), and -// Roark's badge speech pauses for the fanfare (lab-B 2026-10-31 09:45 and -// 2026-11-08 15:06). The clock is the last look that was not quiet. -constexpr Milliseconds TALK_OVER_QUIET = Milliseconds(5000); -// The overworld with no box this long ends it, still or not: wanderers or -// grass can keep the screen from ever being still. -constexpr Milliseconds TALK_OVER_CALM = Milliseconds(15000); - - // When a talk or a scene is over, from looks alone. A look is a frame's // events: DIALOG_BOX, PLAYER_CAN_MOVE, SCREEN_STILL (PLAYER_STILL for a // door's settle), a fade, @@ -96,7 +100,7 @@ class TalkOver{ : m_rule(rule) {} - SceneMove look(const ScreenEvents& look, WallClock now); + SceneMove look(const ScreenEvents& events, WallClock now); // Whether the warp the rule asks for has shown (true without one). bool warped() const; @@ -109,8 +113,9 @@ class TalkOver{ WarpFade m_fade = WarpFade::NONE; WarpFade m_fade_showing = WarpFade::NONE; bool m_banner = false; - WallClock m_quiet_since = WallClock::max(); - WallClock m_calm_since = WallClock::max(); + // Since when the screen has been quiet, and calm; none when it is not. + std::optional m_quiet_since; + std::optional m_calm_since; }; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp index 7073e27efb..a567c1db2a 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges_Tests.cpp @@ -7,13 +7,14 @@ */ #include -#include +#include #include #include #include #include "Common/Cpp/TestRunners/UnitTest.h" #include "Pokemon_SceneJudges.h" #include "Pokemon_SceneJudges_Tests.h" +#include "Pokemon_TestExpect.h" namespace PokemonAutomation{ namespace Pokemon{ @@ -22,13 +23,6 @@ namespace{ using namespace std::chrono_literals; -UnitTestResult expect_equal(const std::string& what, const std::string& got, const std::string& expected){ - if (got == expected){ - return true; - } - return std::format("{}: expected \"{}\", got \"{}\"", what, expected, got); -} - UnitTestResult test_warp_seen(){ WarpSeen seen = note_warp({}, {ScreenEvent::DIALOG_BOX}, "Route 203"); @@ -43,17 +37,17 @@ UnitTestResult test_warp_seen(){ // A scripted scene: a look per beat, a second apart. Returns the beat -// TalkOver first says `want` at, or -1. -int first_beat(const std::vector& beats, SceneMove want, TalkOver& judge){ +// TalkOver first says `want` at, if any. +std::optional first_beat(const std::vector& beats, SceneMove want, TalkOver& judge){ const Milliseconds every = 1000ms; WallClock now = WallClock(); for (size_t c = 0; c < beats.size(); c++){ if (judge.look(beats[c], now) == want){ - return (int)c; + return c; } now += every; } - return -1; + return std::nullopt; } const ScreenEvents QUIET{ScreenEvent::PLAYER_CAN_MOVE, ScreenEvent::SCREEN_STILL}; @@ -77,7 +71,7 @@ UnitTestResult test_talk_over(){ TalkOverRule rule; std::vector beats; SceneMove want; - int beat; + std::optional beat; // Unset: never says `want`. }; const std::vector rows = { {"5 s of quiet ends it", {}, repeat(QUIET, 8), SceneMove::DONE, 5}, @@ -85,45 +79,52 @@ UnitTestResult test_talk_over(){ {"a box is answered", {}, {BOX}, SceneMove::NEXT, 0}, {"no box is a B press", {}, {QUIET}, SceneMove::B, 0}, {"15 s calm but moving ends it", {}, repeat(CALM, 20), SceneMove::DONE, 15}, + // PLZA: its party bar never shows mid-scene for long, and its + // city never stands still. + {"a shorter calm rule ends it sooner", {.calm = 1000ms}, repeat(CALM, 5), SceneMove::DONE, 1}, + {"a shorter calm rule still waits out a box", {.calm = 1000ms}, + concat(concat({CALM}, {BOX}), repeat(CALM, 5)), SceneMove::DONE, 3}, {"a warp rule waits for the fade", {.warp = true}, concat(concat(repeat(QUIET, 8), {ScreenEvents{ScreenEvent::FADE_BLACK}}), repeat(QUIET, 8)), SceneMove::DONE, 14}, {"a banner is the warp too", {.warp = true}, concat({ScreenEvents{ScreenEvent::LOCATION_BANNER, ScreenEvent::PLAYER_CAN_MOVE}}, repeat(QUIET, 8)), SceneMove::DONE, 6}, - {"no quiet ends a talk before a battle", {.end_on_quiet = false}, repeat(QUIET, 30), SceneMove::DONE, -1}, + {"no quiet ends a talk before a battle", {.end_on_quiet = false}, repeat(QUIET, 30), SceneMove::DONE, std::nullopt}, {"a battle", {.end_on_quiet = false}, concat(repeat(QUIET, 6), {ScreenEvents{ScreenEvent::BATTLE_STARTED}}), SceneMove::BATTLE, 6}, - // Mom's Running Shoes scene (lab-B 2026-11-09 14:33): quiet, then - // the scene's closing fade. It ended at the fade's start and the - // next step's input landed in the fade. + // Quiet, then the scene's closing fade: the scene must not end at + // the fade's start, or the next step's input lands in the fade. {"a closing fade starts the quiet over", {}, concat(concat(repeat(QUIET, 4), repeat(FADED, 3)), repeat(QUIET, 8)), SceneMove::DONE, 12}, - {"a fade still down never ends it", {}, concat(repeat(QUIET, 3), repeat(FADED, 20)), SceneMove::DONE, -1}, - {"a fade is never calm", {}, concat(repeat(CALM, 10), repeat(CALM_FADED, 10)), SceneMove::DONE, -1}, + {"a fade still down never ends it", {}, concat(repeat(QUIET, 3), repeat(FADED, 20)), SceneMove::DONE, std::nullopt}, + {"a fade is never calm", {}, concat(repeat(CALM, 10), repeat(CALM_FADED, 10)), SceneMove::DONE, std::nullopt}, }; for (const Row& row : rows){ TalkOver judge(row.rule); - const int got = first_beat(row.beats, row.want, judge); + const std::optional got = first_beat(row.beats, row.want, judge); if (got != row.beat){ - return std::format("{}: expected beat {}, got {} ({})", row.name, row.beat, got, judge.describe()); + const auto show = [](const std::optional& beat){ + return beat ? std::to_string(*beat) : std::string("none"); + }; + return std::format("{}: expected beat {}, got {} ({})", row.name, show(row.beat), show(got), judge.describe()); } } return true; } -struct SceneJudgesInput{ - std::string name; - std::function body; -}; +struct SceneJudgesCase{ + struct Input{ + std::string name; + UnitTestResult (*body)(); + }; -struct SceneJudgesExpectation{ -}; + struct Expectation{ + }; -struct SceneJudgesCase{ - SceneJudgesInput input; - SceneJudgesExpectation expectation; + Input input; + Expectation expectation; }; class Test_SceneJudges : public UnitTest{ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryActions.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryActions.cpp index 5ecbd69f35..2e7376990e 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryActions.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryActions.cpp @@ -107,13 +107,25 @@ Outcome TableExecutor::run(const StoryStep& step, RunState& state){ } try{ Outcome outcome = run_action(step, state); - if (outcome.succeeded() || !cleared_after_failure(step, state, cleared)){ + if (outcome.succeeded()){ + return outcome; + } + switch (look_after_failure(step, state, cleared)){ + case AfterFailure::RETRY: + continue; + case AfterFailure::GAME_NOT_RUNNING: + return outcome_game_not_running(); + case AfterFailure::KEEP: return outcome; } }catch (OperationFailedException& e){ - if (cleared_after_failure(step, state, cleared)){ + const AfterFailure after = look_after_failure(step, state, cleared); + if (after == AfterFailure::RETRY){ continue; } + if (after == AfterFailure::GAME_NOT_RUNNING){ + return outcome_game_not_running(); + } if (m_guard.stops_on_failure(step)){ throw; } @@ -153,11 +165,15 @@ std::optional TableExecutor::clear_interrupts(const StoryStep& step, Ru return std::nullopt; } -// Whether an interrupt explains the failure of `step`: one showed after -// it failed and is cleared, so the step runs again. -bool TableExecutor::cleared_after_failure(const StoryStep& step, RunState& state, size_t& cleared){ +// What explains the failure of `step`, if anything: an interrupt that +// showed after it failed and is cleared, or the game not running. +AfterFailure TableExecutor::look_after_failure(const StoryStep& step, RunState& state, size_t& cleared){ const size_t before = cleared; - return !clear_interrupts(step, state, cleared) && cleared > before; + const std::optional failed = clear_interrupts(step, state, cleared); + if (failed){ + return failed->failure == GAME_NOT_RUNNING ? AfterFailure::GAME_NOT_RUNNING : AfterFailure::KEEP; + } + return cleared > before ? AfterFailure::RETRY : AfterFailure::KEEP; } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryActions.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryActions.h index 3e2792a695..23711eb83c 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryActions.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryActions.h @@ -23,7 +23,6 @@ #include #include #include -#include #include #include #include @@ -109,6 +108,17 @@ class StepGuard{ }; +// What a look at the screen after a step failed found. +enum class AfterFailure{ + // Nothing explains the failure: it is the step's own. + KEEP, + // An interrupt explains it and is cleared: the step runs again. + RETRY, + // The game is not running (the Switch's HOME menu): the failure is + // that, and the step is not run again. + GAME_NOT_RUNNING, +}; + // Runs each step with the table's action for it, inside `guard`. class TableExecutor : public StoryStepExecutor{ public: @@ -138,7 +148,7 @@ class TableExecutor : public StoryStepExecutor{ private: std::optional clear_interrupts(const StoryStep& step, RunState& state, size_t& cleared); - bool cleared_after_failure(const StoryStep& step, RunState& state, size_t& cleared); + AfterFailure look_after_failure(const StoryStep& step, RunState& state, size_t& cleared); const StoryActionTable& m_table; StepGuard& m_guard; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryActions_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryActions_Tests.cpp index 4e3b98983a..a4e8020210 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryActions_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryActions_Tests.cpp @@ -253,18 +253,18 @@ UnitTestResult test_guard_stop_rethrows(){ } -struct StoryActionsInput{ - std::string_view name; - UnitTestResult (*test)(); -}; +struct StoryActionsCase{ + struct Input{ + std::string_view name; + UnitTestResult (*test)(); + }; -struct StoryActionsExpectation{ - std::string_view description; -}; + struct Expectation{ + std::string_view description; + }; -struct StoryActionsCase{ - StoryActionsInput input; - StoryActionsExpectation expectation; + Input input; + Expectation expectation; }; class Test_StoryActions : public UnitTest{ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryActions_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryActions_Tests.h index d3593d58f6..6f6f908d18 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryActions_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryActions_Tests.h @@ -13,7 +13,7 @@ namespace PokemonAutomation{ namespace Pokemon{ -// Registers unit tests for story action tables, the variant parser and the table executor. +// Registers unit tests for story action tables, checkpoint lint and the table executor. void add_tests_StoryActions(UnitTestDatabase& database); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvent.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvent.h index 48f20e317e..ffda3cee12 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvent.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvent.h @@ -10,6 +10,7 @@ #define PokemonAutomation_Pokemon_StoryEvent_H #include +#include #include #include #include @@ -66,6 +67,9 @@ struct Event{ std::string why; // Skipped when all of these hold (and there is at least one). std::vector 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 when_skipped; // Must hold before `act`; a failure is "require_failed". std::vector require; Act act; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvents.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvents.cpp index 8afcdfe404..c2cb6142df 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvents.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvents.cpp @@ -14,6 +14,14 @@ namespace PokemonAutomation{ namespace Pokemon{ +std::string event_log_line(std::string_view checkpoint, size_t number, std::string_view event, std::string_view status){ + return std::format("[Event] {}/{} {}: {}", checkpoint, number, event, status); +} + +std::string checkpoint_log_line(std::string_view checkpoint, std::string_view text){ + return std::format("[Checkpoint] {}: {}", checkpoint, text); +} + std::string certainty_name(PoseCertainty certainty){ switch (certainty){ case PoseCertainty::TILE: @@ -370,7 +378,7 @@ Outcome run_event_logging_throws(EventRun& run){ }catch (...){ const std::string what = current_exception_text(); log_status(std::format("failed: {}", what), run); - run.log(std::format("[Checkpoint] {}: failed at {} {}: {}", run.checkpoint.id, run.number, run.event.name, what)); + run.log(checkpoint_log_line(run.checkpoint.id, std::format("failed at {} {}: {}", run.number, run.event.name, what))); throw; } } @@ -379,7 +387,7 @@ CheckpointReport stop_checkpoint(EventLogger& log, const std::string& id, Outcom std::string where = report.failed_event == 0 ? "" : std::format(" at {} {}", report.failed_event, report.failed_name); - log(std::format("[Checkpoint] {}: failed{}: {}", id, where, to_string(failure))); + log(checkpoint_log_line(id, std::format("failed{}: {}", where, to_string(failure)))); report.failure = std::move(failure); return report; } @@ -412,6 +420,9 @@ CheckpointReport run_checkpoint(RunState& state, Checkpoint& checkpoint, EventLo EventRun run{checkpoint, c + 1, event, state, log, std::vector(event.recoveries.size(), 0), 0}; if (already_done(event, state)){ log_status("skipped", run); + if (event.when_skipped){ + event.when_skipped(state); + } report.events_skipped++; continue; } @@ -428,7 +439,7 @@ CheckpointReport run_checkpoint(RunState& state, Checkpoint& checkpoint, EventLo return stop_checkpoint(log, id, std::move(finished), report); } report.completed = true; - log(std::format("[Checkpoint] {}: done, {} run, {} skipped", id, report.events_run, report.events_skipped)); + log(checkpoint_log_line(id, std::format("done, {} run, {} skipped", report.events_run, report.events_skipped))); return report; } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvents.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvents.h index 3e94a6996f..860d7d5f45 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvents.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvents.h @@ -42,6 +42,17 @@ namespace PokemonAutomation{ namespace Pokemon{ +// The runner's own log lines, beside the checkpoint lines of +// Pokemon_StoryLog.h. One event's line: +// [Event] 00.04/3 talk_mom: start +// [Event] 00.04/3 talk_mom: failed: expect_failed: has_parcel +std::string event_log_line(std::string_view checkpoint, size_t number, std::string_view event, std::string_view status); + +// Any other line about a checkpoint, such as how its run ended: +// [Checkpoint] 00.04: done, 3 run, 0 skipped +// [Checkpoint] 00.04: failed at 2 talk_mom: expect_failed: has_parcel +std::string checkpoint_log_line(std::string_view checkpoint, std::string_view text); + // Problems an author should fix before running: unnamed or empty events, // missing actions, recoveries out of escalation order. std::vector lint_checkpoint(const Checkpoint& checkpoint); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvents_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvents_Tests.cpp index a1d485644e..cafb66cd54 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvents_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvents_Tests.cpp @@ -611,18 +611,18 @@ UnitTestResult test_lint(){ } -struct StoryEventInput{ - std::string name; - UnitTestResult (*test)(); -}; +struct StoryEventTestCase{ + struct Input{ + std::string name; + UnitTestResult (*test)(); + }; -struct StoryEventExpectation{ - std::string_view description; -}; + struct Expectation{ + std::string_view description; + }; -struct StoryEventTestCase{ - StoryEventInput input; - StoryEventExpectation expectation; + Input input; + Expectation expectation; }; class Test_StoryEvents : public UnitTest{ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryInterrupts.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryInterrupts.cpp index 0012c5d2eb..78ab78df44 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryInterrupts.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryInterrupts.cpp @@ -10,6 +10,8 @@ #include #include #include "Common/Cpp/Exceptions.h" +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "CommonTools/ScreenEvents/ScreenFades.h" #include "Pokemon_StoryInterrupts.h" namespace PokemonAutomation{ @@ -146,6 +148,17 @@ std::string describe(const ScreenSettle& settle){ } +// The handler's outcome. A handler drives the console, which it can lose: +// that is a failed outcome too, not an exception for the executor's caller. +Outcome run_handler(const InterruptHandler& handler, const InterruptVerdict& verdict, RunState& state){ + try{ + return handler.handle(verdict, state); + }catch (OperationFailedException& e){ + return outcome_failed("operation_failed", e.message()); + } +} + + InterruptCheck clear_story_interrupt(const StoryInterrupts& interrupts, const StoryStep& step, RunState& state){ InterruptCheck out; out.verdict = settled_verdict(interrupts, step.kind, out.screen); @@ -171,7 +184,7 @@ InterruptCheck clear_story_interrupt(const StoryInterrupts& interrupts, const St out.outcome = outcome_failed("unhandled_interrupt", std::format("{} before {}", interrupt_kind_name(kind), step.name)); return out; } - const Outcome handled = handler->handle(out.verdict, state); + const Outcome handled = run_handler(*handler, out.verdict, state); if (!handled.succeeded()){ out.outcome = outcome_failed("interrupt_failed", std::format("{}: {}", interrupt_kind_name(kind), to_string(handled))); return out; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryInterrupts.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryInterrupts.h index 2bceba34c6..82f9d3b965 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryInterrupts.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryInterrupts.h @@ -34,17 +34,12 @@ #ifndef PokemonAutomation_Pokemon_StoryInterrupts_H #define PokemonAutomation_Pokemon_StoryInterrupts_H -#include -#include -#include -#include #include -#include +#include "Pokemon_InterruptHandlers.h" #include "Pokemon_Interrupts.h" -#include "Pokemon_ScreenEvents.h" +#include "CommonTools/ScreenEvents/ScreenEvents.h" #include "Pokemon_StoryEvents.h" #include "Pokemon_StoryStepExecutor.h" -#include "Pokemon_InterruptHandlers.h" namespace PokemonAutomation{ namespace Pokemon{ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryInterrupts_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryInterrupts_Tests.cpp index 234e0d926b..e994e11745 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryInterrupts_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryInterrupts_Tests.cpp @@ -347,8 +347,8 @@ UnitTestResult test_unsettled_before_a_battle(){ } // A battle's opening still playing when a walk step begins: the walk -// before it ended in grass or in a trainer's sight (BDSP fault sweep, 51 -// steps of 00.01-01.11). The looks wait for the battle, past max_looks, +// before it ended in grass or in a trainer's sight. The looks wait for the +// battle, past max_looks, // and its handler clears it before the step. UnitTestResult test_opening_before_a_walk(ScreenLook opening, ScreenLook battle, const std::string& kind, const std::string& handler){ FakeGame game; @@ -492,8 +492,7 @@ UnitTestResult test_screen_before_each_step(){ }); } -// The program started on the HOME menu (lab-B 2026-11-09 15:31: "before -// save: settled overworld", then X on HOME and a 22-tile walk on it). The +// The program started with the game closed and the HOME menu showing. The // screen is told as HOME and the step fails at once, before any input, // and before a step that starts on a battle's opening too. UnitTestResult test_home_menu_before_a_step(){ @@ -568,6 +567,78 @@ UnitTestResult test_home_menu_stops_the_segment(){ return true; } +// A handler that drives the console can lose it. That fails the step as +// interrupt_failed, like a handler that returns a failure: it must not +// escape the executor, and the step is not run. +UnitTestResult test_handler_throws(){ + FakeGame game; + game.screens = {dialog()}; + StoryInterrupts interrupts = fake_interrupts(game); + interrupts.handlers = InterruptHandlerTable(); + interrupts.handlers.add(InterruptKind::DIALOG, InterruptHandler{ + .handle = [&game](const InterruptVerdict&, RunState&) -> Outcome{ + game.trace.push_back("clear dialog"); + throw OperationFailedException(ErrorReportMode::NO_ERROR_REPORT, "the console is gone"); + }, + .pose = InterruptPose::UNCHANGED, + }); + try{ + const FakeRun run = run_step(game, interrupts, make(StoryStepKind::ROUTE, "walk")); + if (run.outcome.failure != "interrupt_failed"){ + return std::format("Expected interrupt_failed, got {}", to_string(run.outcome)); + } + }catch (OperationFailedException& e){ + return std::format("The handler's failure escaped the executor: {}", e.message()); + } + return expect_trace(game, {"look", "clear dialog", "told dialog interrupt_failed: dialog: operation_failed: the console is gone"}); +} + +// The same loss after a step failed, while the executor looks for what +// explains the failure: the step's own failure is kept. +UnitTestResult test_handler_throws_after_a_failure(){ + FakeGame game; + game.screens = {quiet(), dialog()}; + StoryInterrupts interrupts = fake_interrupts(game); + interrupts.handlers = InterruptHandlerTable(); + interrupts.handlers.add(InterruptKind::DIALOG, InterruptHandler{ + .handle = [&game](const InterruptVerdict&, RunState&) -> Outcome{ + game.trace.push_back("clear dialog"); + throw OperationFailedException(ErrorReportMode::NO_ERROR_REPORT, "the console is gone"); + }, + .pose = InterruptPose::UNCHANGED, + }); + try{ + const FakeRun run = run_step(game, interrupts, make(StoryStepKind::ROUTE, "walk"), FakeAction{.failures = 1}); + if (run.outcome.failure != "dialog"){ + return std::format("Expected the step's own failure, got {}", to_string(run.outcome)); + } + }catch (OperationFailedException& e){ + return std::format("The handler's failure escaped the executor: {}", e.message()); + } + return true; +} + +// A step that fails and then shows the HOME menu did not fail on its own: +// the game is not running, so the segment must stop instead of recovering +// or reloading with the game closed. +UnitTestResult test_home_menu_after_a_failed_step(){ + for (bool throws : {false, true}){ + FakeGame game; + game.screens = {quiet(), home_menu()}; + StoryInterrupts interrupts = fake_interrupts(game); + const FakeRun run = run_step(game, interrupts, make(StoryStepKind::ROUTE, "walk"), FakeAction{.failures = 1, .throws = throws}); + if (run.outcome.failure != "game_not_running"){ + return std::format("Expected game_not_running after a step that {} and then showed HOME, got {}", + throws ? "threw" : "failed", to_string(run.outcome)); + } + const size_t runs = static_cast(std::count(game.trace.begin(), game.trace.end(), "run walk")); + if (runs != 1){ + return std::format("Expected the step run once, got {} runs", runs); + } + } + return true; +} + UnitTestResult test_settle_of_a_look(){ struct Row{ std::string name; @@ -592,18 +663,18 @@ UnitTestResult test_settle_of_a_look(){ } -struct StoryInterruptsInput{ - std::string_view name; - UnitTestResult (*test)(); -}; +struct StoryInterruptsCase{ + struct Input{ + std::string_view name; + UnitTestResult (*test)(); + }; -struct StoryInterruptsExpectation{ - std::string_view description; -}; + struct Expectation{ + std::string_view description; + }; -struct StoryInterruptsCase{ - StoryInterruptsInput input; - StoryInterruptsExpectation expectation; + Input input; + Expectation expectation; }; class Test_StoryInterrupts : public UnitTest{ @@ -717,6 +788,18 @@ const StoryInterruptsCase STORY_INTERRUPTS_CASES[] = { .input = {.name = "the HOME menu stops the segment", .test = test_home_menu_stops_the_segment}, .expectation = {.description = "no recovery in place, no reload"}, }, + { + .input = {.name = "a handler that throws fails the step", .test = test_handler_throws}, + .expectation = {.description = "interrupt_failed, the step not run, nothing escapes"}, + }, + { + .input = {.name = "a handler that throws after a failed step", .test = test_handler_throws_after_a_failure}, + .expectation = {.description = "the step's own failure, nothing escapes"}, + }, + { + .input = {.name = "the HOME menu after a failed step", .test = test_home_menu_after_a_failed_step}, + .expectation = {.description = "game_not_running instead of the step's failure, run once"}, + }, }; } // namespace diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp index f931e3bd51..5fed3725bf 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp @@ -20,44 +20,63 @@ std::string resets_text(size_t attempt){ return std::format("{} reset{}", attempt - 1, attempt == 2 ? "" : "s"); } -} // namespace +// "[Checkpoint] 00.04", or the game's own checkpoint text unchanged. +std::string checkpoint_prefix(std::string_view checkpoint, bool add_checkpoint_prefix){ + if (!add_checkpoint_prefix){ + return std::string(checkpoint); + } + return std::format("[Checkpoint] {}", checkpoint); +} +} // namespace -std::string event_log_line(std::string_view checkpoint, size_t number, std::string_view event, std::string_view status){ - return std::format("[Event] {}/{} {}: {}", checkpoint, number, event, status); -} -std::string checkpoint_start_line(std::string_view checkpoint, std::optional events){ +std::string checkpoint_start_line( + std::string_view checkpoint, std::optional events, bool add_checkpoint_prefix +){ + const std::string prefix = checkpoint_prefix(checkpoint, add_checkpoint_prefix); if (!events){ - return std::format("[Checkpoint] {}: start", checkpoint); + return std::format("{}: start", prefix); } - return std::format("[Checkpoint] {}: start, {} events", checkpoint, *events); + return std::format("{}: start, {} events", prefix, *events); } -std::string checkpoint_attempt_line(std::string_view checkpoint, size_t attempt, std::optional attempts){ +std::string checkpoint_attempt_line( + std::string_view checkpoint, size_t attempt, std::optional attempts, bool add_checkpoint_prefix +){ + const std::string prefix = checkpoint_prefix(checkpoint, add_checkpoint_prefix); if (!attempts){ - return std::format("[Checkpoint] {}: attempt {}", checkpoint, attempt); + return std::format("{}: attempt {}", prefix, attempt); } - return std::format("[Checkpoint] {}: attempt {}/{}", checkpoint, attempt, *attempts); + return std::format("{}: attempt {}/{}", prefix, attempt, *attempts); } -std::string checkpoint_pass_line(std::string_view checkpoint, size_t attempt, size_t in_place){ +std::string checkpoint_pass_line(std::string_view checkpoint, size_t attempt, bool add_checkpoint_prefix){ return std::format( - "[Checkpoint] {}: passed on attempt {} ({}){}", - checkpoint, attempt, resets_text(attempt), - in_place == 0 ? "" : std::format(", {} in place", in_place) + "{}: passed on attempt {} ({})", + checkpoint_prefix(checkpoint, add_checkpoint_prefix), attempt, resets_text(attempt) ); } -std::string checkpoint_reset_line(std::string_view checkpoint, size_t next_attempt){ - return std::format("[Checkpoint] {}: failed, resetting for attempt {}", checkpoint, next_attempt); +std::string checkpoint_reset_line( + std::string_view checkpoint, size_t next_attempt, std::optional attempts, bool add_checkpoint_prefix +){ + const std::string prefix = checkpoint_prefix(checkpoint, add_checkpoint_prefix); + if (!attempts){ + return std::format("{}: failed, resetting for attempt {}", prefix, next_attempt); + } + return std::format("{}: failed, resetting for attempt {}/{}", prefix, next_attempt, *attempts); } -std::string segment_reload_line(std::string_view segment, std::string_view checkpoint, size_t next_attempt, size_t attempts){ - return std::format( - "[Segment] {}: {} failed, reloading for attempt {}/{}", - segment, checkpoint, next_attempt, attempts - ); +std::string story_short_line(std::string_view line){ + if (!line.starts_with('[')){ + return std::string(line); + } + const size_t end = line.find("] "); + if (end == std::string_view::npos){ + return std::string(line); + } + return std::string(line.substr(end + 2)); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.h index 539a3d3424..11bfb009d2 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.h @@ -2,12 +2,13 @@ * * From: https://github.com/PokemonAutomation/ * - * The log lines an AutoStory program writes around its checkpoints, in - * one format for every game: the story engine (Pokemon_StoryEvents.h, - * Pokemon_StorySegments.h) writes them, and a game with its own checkpoint - * loop (SV's) writes the same lines, so the lab watcher and whoever reads - * a log find each checkpoint's start, attempts, resets and pass the same - * way. Nothing here runs a story. + * A story is split at three levels: a segment is a chapter of + * checkpoints, a checkpoint is the stretch between two saves and the unit + * a run retries, and an event is one step inside a checkpoint. This file + * writes the checkpoint lines: start, attempt, reset and pass, in one + * format for every game, so the program developer and user find each + * checkpoint's attempts the same way in any game's log. Nothing here runs + * a story. * */ @@ -23,38 +24,59 @@ namespace PokemonAutomation{ namespace Pokemon{ -// One event's line: -// [Event] 00.04/3 talk_mom: start -// [Event] 00.04/3 talk_mom: failed: expect_failed: has_parcel -std::string event_log_line(std::string_view checkpoint, size_t number, std::string_view event, std::string_view status); +// The lines below start with "[Checkpoint] " and then `checkpoint`. A game +// whose checkpoint text already says it is a checkpoint (SV's +// "Checkpoint:: 004 - 02 ...") passes that text as `checkpoint` and +// `add_checkpoint_prefix` false, and the line starts with the text alone: +// Checkpoint:: 004 - 02 ...: start // A checkpoint starts; the story engine also says it has `events` events: // [Checkpoint] 00.04: start // [Checkpoint] 00.04: start, 3 events -std::string checkpoint_start_line(std::string_view checkpoint, std::optional events = std::nullopt); +std::string checkpoint_start_line( + std::string_view checkpoint, + std::optional events = std::nullopt, + bool add_checkpoint_prefix = true +); // An attempt at a checkpoint starts, counting from 1, out of the // program's limit of `attempts` if it has one: // [Checkpoint] 00.04: attempt 2 // [Checkpoint] 00.04: attempt 2/3 std::string checkpoint_attempt_line( - std::string_view checkpoint, size_t attempt, std::optional attempts = std::nullopt + std::string_view checkpoint, + size_t attempt, + std::optional attempts = std::nullopt, + bool add_checkpoint_prefix = true ); -// A checkpoint passed on attempt `attempt`, after `in_place` recoveries in -// place: +// A checkpoint passed on attempt `attempt`, counting from 1; every attempt +// before it ended in a reset: // [Checkpoint] 00.03: passed on attempt 1 (one-shot) -// [Checkpoint] 00.04: passed on attempt 3 (2 resets), 1 in place -std::string checkpoint_pass_line(std::string_view checkpoint, size_t attempt, size_t in_place = 0); +// [Checkpoint] 00.04: passed on attempt 3 (2 resets) +std::string checkpoint_pass_line( + std::string_view checkpoint, + size_t attempt, + bool add_checkpoint_prefix = true +); // An attempt failed and the game resets to the checkpoint's save for -// attempt `next_attempt`: +// attempt `next_attempt`, out of the limit of `attempts` if it has one: // [Checkpoint] 00.04: failed, resetting for attempt 2 -std::string checkpoint_reset_line(std::string_view checkpoint, size_t next_attempt); +// [Checkpoint] 00.04: failed, resetting for attempt 2/3 +std::string checkpoint_reset_line( + std::string_view checkpoint, + size_t next_attempt, + std::optional attempts = std::nullopt, + bool add_checkpoint_prefix = true +); -// The story engine reloads within segment `segment`, out of `attempts`: -// [Segment] 00: 00.02 failed, reloading for attempt 2/3 -std::string segment_reload_line(std::string_view segment, std::string_view checkpoint, size_t next_attempt, size_t attempts); +// The full lines above go to the log. The video overlay, which wraps its +// lines, shows each one short, without its leading "[Tag] ": +// [Checkpoint] 12.03: start, 18 events -> 12.03: start, 18 events +// Any line with a leading "[Tag] " is shortened the same way. A line +// without a tag, like SV's checkpoint text, is returned unchanged. +std::string story_short_line(std::string_view line); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp index 1cc32c258b..759ed9b3d7 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp @@ -5,7 +5,9 @@ */ #include +#include #include +#include #include #include "Common/Cpp/TestRunners/UnitTest.h" #include "Pokemon_StoryLog.h" @@ -16,27 +18,27 @@ namespace Pokemon{ namespace{ -// A formatter's line, made when the table is built. -struct StoryLogInput{ - std::string name; - std::string line; -}; +// One formatter call and the line it must make. +struct StoryLogTestCase{ + // A formatter's line, made when the table is built. + struct Input{ + std::string line; + }; -// The exact line: the lab watcher and the engine tests read these. -struct StoryLogExpectation{ - std::string line; -}; + // The exact line: the lab watcher and the engine tests read these. + struct Expectation{ + std::string line; + }; -// One formatter call and the line it must make. -struct StoryLogCase{ - StoryLogInput input; - StoryLogExpectation expectation; + std::string name; + Input input; + Expectation expectation; }; class Test_StoryLog : public UnitTest{ public: - Test_StoryLog(StoryLogCase test_case) - : UnitTest(std::format("Pokemon::StoryLog - {}", test_case.input.name)) + Test_StoryLog(StoryLogTestCase test_case) + : UnitTest(std::format("Pokemon::StoryLog - {}", test_case.name)) , m_case(std::move(test_case)) {} @@ -48,49 +50,104 @@ class Test_StoryLog : public UnitTest{ } private: - const StoryLogCase m_case; + const StoryLogTestCase m_case; }; -const StoryLogCase STORY_LOG_CASES[] = { - { - .input = {.name = "event", .line = event_log_line("00.04", 3, "talk_mom", "start")}, - .expectation = {.line = "[Event] 00.04/3 talk_mom: start"}, - }, +// SV's text for a checkpoint, which already says "Checkpoint::". +constexpr std::string_view SV_TEXT = "Checkpoint:: 004 - 02. Start point: X"; + +const StoryLogTestCase STORY_LOG_CASES[] = { { - .input = {.name = "checkpoint start", .line = checkpoint_start_line("00.04")}, + .name = "checkpoint start", + .input = {.line = checkpoint_start_line("00.04")}, .expectation = {.line = "[Checkpoint] 00.04: start"}, }, { - .input = {.name = "checkpoint start with its events", .line = checkpoint_start_line("00.04", 3)}, + .name = "checkpoint start with its events", + .input = {.line = checkpoint_start_line("00.04", /*events=*/3)}, .expectation = {.line = "[Checkpoint] 00.04: start, 3 events"}, }, { - .input = {.name = "attempt", .line = checkpoint_attempt_line("00.04", 2)}, + .name = "attempt", + .input = {.line = checkpoint_attempt_line("00.04", 2)}, .expectation = {.line = "[Checkpoint] 00.04: attempt 2"}, }, { - .input = {.name = "attempt out of a limit", .line = checkpoint_attempt_line("00.04", 2, 3)}, + .name = "attempt out of a limit", + .input = {.line = checkpoint_attempt_line("00.04", 2, /*attempts=*/3)}, .expectation = {.line = "[Checkpoint] 00.04: attempt 2/3"}, }, { - .input = {.name = "one-shot pass", .line = checkpoint_pass_line("00.03", 1)}, + .name = "one-shot pass", + .input = {.line = checkpoint_pass_line("00.03", 1)}, .expectation = {.line = "[Checkpoint] 00.03: passed on attempt 1 (one-shot)"}, }, { - .input = {.name = "pass after one reset", .line = checkpoint_pass_line("00.04", 2)}, + .name = "pass after one reset", + .input = {.line = checkpoint_pass_line("00.04", 2)}, .expectation = {.line = "[Checkpoint] 00.04: passed on attempt 2 (1 reset)"}, }, { - .input = {.name = "pass after resets and recoveries in place", .line = checkpoint_pass_line("00.04", 3, 1)}, - .expectation = {.line = "[Checkpoint] 00.04: passed on attempt 3 (2 resets), 1 in place"}, + .name = "pass after resets", + .input = {.line = checkpoint_pass_line("00.04", 3)}, + .expectation = {.line = "[Checkpoint] 00.04: passed on attempt 3 (2 resets)"}, }, { - .input = {.name = "reset", .line = checkpoint_reset_line("00.04", 2)}, + .name = "reset", + .input = {.line = checkpoint_reset_line("00.04", 2)}, .expectation = {.line = "[Checkpoint] 00.04: failed, resetting for attempt 2"}, }, { - .input = {.name = "reload within a segment", .line = segment_reload_line("00", "00.02", 2, 3)}, - .expectation = {.line = "[Segment] 00: 00.02 failed, reloading for attempt 2/3"}, + .name = "reset out of a limit", + .input = {.line = checkpoint_reset_line("00.04", 2, /*attempts=*/3)}, + .expectation = {.line = "[Checkpoint] 00.04: failed, resetting for attempt 2/3"}, + }, + { + .name = "start without the checkpoint prefix", + .input = {.line = checkpoint_start_line(SV_TEXT, /*events=*/std::nullopt, /*add_checkpoint_prefix=*/false)}, + .expectation = {.line = "Checkpoint:: 004 - 02. Start point: X: start"}, + }, + { + .name = "attempt without the checkpoint prefix", + .input = {.line = checkpoint_attempt_line(SV_TEXT, 2, /*attempts=*/100, /*add_checkpoint_prefix=*/false)}, + .expectation = {.line = "Checkpoint:: 004 - 02. Start point: X: attempt 2/100"}, + }, + { + .name = "pass without the checkpoint prefix", + .input = {.line = checkpoint_pass_line(SV_TEXT, 2, /*add_checkpoint_prefix=*/false)}, + .expectation = {.line = "Checkpoint:: 004 - 02. Start point: X: passed on attempt 2 (1 reset)"}, + }, + { + .name = "reset without the checkpoint prefix", + .input = {.line = checkpoint_reset_line(SV_TEXT, 2, /*attempts=*/std::nullopt, /*add_checkpoint_prefix=*/false)}, + .expectation = {.line = "Checkpoint:: 004 - 02. Start point: X: failed, resetting for attempt 2"}, + }, + { + .name = "short checkpoint start", + .input = {.line = story_short_line("[Checkpoint] 12.03: start, 18 events")}, + .expectation = {.line = "12.03: start, 18 events"}, + }, + { + .name = "short line with another tag", + .input = {.line = story_short_line("[Tag] 12.03: some text")}, + .expectation = {.line = "12.03: some text"}, + }, + { + .name = "short line without the checkpoint prefix", + .input = {.line = story_short_line( + checkpoint_start_line(SV_TEXT, /*events=*/std::nullopt, /*add_checkpoint_prefix=*/false) + )}, + .expectation = {.line = "Checkpoint:: 004 - 02. Start point: X: start"}, + }, + { + .name = "short line with an unclosed tag", + .input = {.line = story_short_line("[Tag 12.03")}, + .expectation = {.line = "[Tag 12.03"}, + }, + { + .name = "short empty line", + .input = {.line = story_short_line("")}, + .expectation = {.line = ""}, }, }; @@ -99,7 +156,7 @@ const StoryLogCase STORY_LOG_CASES[] = { void add_tests_StoryLog(UnitTestDatabase& database){ - for (const StoryLogCase& test_case : STORY_LOG_CASES){ + for (const StoryLogTestCase& test_case : STORY_LOG_CASES){ database.add(test_case); } } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPreflight.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPreflight.cpp index 2b56c6b435..5266a751e7 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPreflight.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPreflight.cpp @@ -26,6 +26,12 @@ namespace{ // Color bars are seven flat vertical stripes of different colors. constexpr size_t BAR_COUNT = 7; +// Each stripe is sampled in its middle, away from its edges: inset by this +// fraction of its width on each side, and from the top for this fraction of +// the frame's height. +constexpr double BAR_INSET = 0.25; +constexpr double BAR_TOP = 0.1; +constexpr double BAR_HEIGHT = 0.5; // A stripe flatter than this (stddev sum) is one solid color. constexpr double BAR_MAX_STDDEV = 30; // Two stripes this far apart (RGB distance) are different bars. @@ -37,7 +43,7 @@ bool shows_color_bars(const ImageViewRGB32& frame){ std::array bars; for (size_t c = 0; c < BAR_COUNT; c++){ const double width = 1.0 / BAR_COUNT; - const ImageFloatBox stripe(c * width + width * 0.25, 0.1, width * 0.5, 0.5); + const ImageFloatBox stripe(c * width + width * BAR_INSET, BAR_TOP, width * (1 - 2 * BAR_INSET), BAR_HEIGHT); const ImageStats stats = image_stats(extract_box_reference(frame, stripe)); if (stats.stddev.sum() > BAR_MAX_STDDEV){ return false; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp new file mode 100644 index 0000000000..1890518dd4 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.cpp @@ -0,0 +1,68 @@ +/* Story RNG Handoff + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/Color.h" +#include "Common/Cpp/Exceptions.h" +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "Pokemon_StoryRngHandoff.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +RngHandoffMode rng_handoff_mode(const RngHandoffOptions& options){ + if (options.run){ + return RngHandoffMode::RUN; + } + return options.stop ? RngHandoffMode::STOP : RngHandoffMode::OFF; +} + +bool RngHandoffResult::succeeded() const{ + return failure.empty(); +} + +std::optional run_rng_handoff_or_user_turn( + Logger& logger, const std::string& program, const RngHandoffCall& call +){ + const RngHandoffResult result = call(); + if (!result.succeeded()){ + throw_and_log(logger, ErrorReportMode::SEND_ERROR_REPORT, result.failure); + } + if (!result.user_turn.empty()){ + logger.log(program + " stopped for the user: " + result.user_turn, COLOR_BLUE); + return result.user_turn; + } + logger.log(program + " succeeded: " + result.summary, COLOR_BLUE); + return std::nullopt; +} + +Outcome run_rng_handoff(Logger& logger, const std::string& program, const RngHandoffCall& call){ + const std::optional user_turn = run_rng_handoff_or_user_turn(logger, program, call); + if (user_turn){ + // A story action has no stop to give the user's turn to. + throw_and_log(logger, ErrorReportMode::SEND_ERROR_REPORT, *user_turn); + } + return outcome_ok(); +} + +std::string report_rng_handoff_stop( + Logger& logger, const std::string& message, const std::vector& lines, + const std::function& show +){ + std::string text = message; + logger.log(message, COLOR_BLUE); + show(message); + for (const std::string& line : lines){ + logger.log(line, COLOR_BLUE); + show(line); + text += "\n" + line; + } + return text; +} + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h new file mode 100644 index 0000000000..ddc21f3286 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff.h @@ -0,0 +1,96 @@ +/* Story RNG Handoff + * + * From: https://github.com/PokemonAutomation/ + * + * The part of an AutoStory run that hands the game to an RNG program (BDSP + * Starter RNG, FRLG Starter RNG and Gift RNG) and takes it back. A game + * supplies the program's call; the contract around it is shared: + * + * - A handoff row has a mode: OFF (the story plays it by hand), STOP (the run + * ends after the row's handoff save, for the user to run the RNG program) + * or RUN (the story calls the RNG program itself and continues). + * - The call ends in an RngHandoffResult: no failure means the game stands + * where the story goes on; a failure is the reason it does not. A game + * whose program can also end where the user takes over (FRLG: the target + * hit but not shiny, the attempts spent) gives that end a user_turn, which + * is a stop and not an error. What the story runs after a continuation + * (FRLG: the rows after a shiny) is the game's, built from the call. + * - A failed RUN stops the program. It never retries the row: the program's + * own reset loop is the retry, and a reload of the save would lose a + * shiny it caught. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryRngHandoff_H +#define PokemonAutomation_Pokemon_StoryRngHandoff_H + +#include +#include +#include +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "Pokemon_StoryOutcome.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// What a handoff row does with its RNG program. +enum class RngHandoffMode{ + OFF, + STOP, + RUN, +}; + +// A handoff's two options. +struct RngHandoffOptions{ + bool stop = false; + bool run = false; +}; + +// The mode for a handoff's two options: RUN takes precedence over STOP. +RngHandoffMode rng_handoff_mode(const RngHandoffOptions& options); + + +// How an RNG program's call ended, as the story needs it. +struct RngHandoffResult{ + // Empty: the game stands where the story goes on. Otherwise why it does not. + std::string failure; + // What a success did, for the log: the resets used, the target hit. + std::string summary; + // Not empty (and no failure): the program ended where the user takes + // over, with this for them. The run stops without an error report. + std::string user_turn; + + bool succeeded() const; +}; + +// The call a handoff row runs in place of its steps. +using RngHandoffCall = std::function; + +// Runs `call` for the RNG program `program` ("Starter RNG"). A success is +// logged with its summary and returned as an ok Outcome. A failure throws +// OperationFailedException with an error report, so that no row retry +// reruns the program's reset loop. +Outcome run_rng_handoff(Logger& logger, const std::string& program, const RngHandoffCall& call); + +// As run_rng_handoff(), for a program whose call may end in the user's turn: +// returns that message, which is logged, and the run stops there. Returns +// nothing when the game stands where the story goes on. +std::optional run_rng_handoff_or_user_turn( + Logger& logger, const std::string& program, const RngHandoffCall& call +); + +// A run that stops for the user to run an RNG program, as the game's program +// reports it: `message` (what to do) and then `lines` (the RNG plan's +// attempts), each logged and passed to `show` (the overlay's log). Returns +// the whole text, one line each, for the program's finish notification. +std::string report_rng_handoff_stop( + Logger& logger, const std::string& message, const std::vector& lines, + const std::function& show +); + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp new file mode 100644 index 0000000000..a5733d5bce --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.cpp @@ -0,0 +1,189 @@ +/* Story RNG Handoff Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "Pokemon_StoryRngHandoff.h" +#include "Pokemon_StoryRngHandoff_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + +namespace{ + +struct RngHandoffInput{ + std::string_view name; + UnitTestResult (*body)(Logger& logger); +}; + +struct RngHandoffExpectation{ + std::string_view description; +}; + +struct RngHandoffTestCase{ + RngHandoffInput input; + RngHandoffExpectation expectation; +}; + +class Test_RngHandoff : public UnitTest{ +public: + explicit Test_RngHandoff(RngHandoffTestCase scenario) + : UnitTest("Pokemon::StoryRngHandoff - " + std::string(scenario.input.name)) + , m_scenario(scenario) + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + return m_scenario.input.body(logger); + } + +private: + RngHandoffTestCase m_scenario; +}; + +// rng_handoff_mode()'s two options. +UnitTestResult test_mode(Logger&){ + struct Case{ + RngHandoffOptions options; + RngHandoffMode expected; + }; + const Case cases[] = { + {.options = {.stop = false, .run = false}, .expected = RngHandoffMode::OFF}, + {.options = {.stop = true, .run = false}, .expected = RngHandoffMode::STOP}, + {.options = {.stop = false, .run = true}, .expected = RngHandoffMode::RUN}, + {.options = {.stop = true, .run = true}, .expected = RngHandoffMode::RUN}, + }; + for (const Case& item : cases){ + if (rng_handoff_mode(item.options) != item.expected){ + return std::format("stop={}, run={}: wrong mode", item.options.stop, item.options.run); + } + } + return true; +} + +// A success is an ok Outcome and calls the program once. +UnitTestResult test_success(Logger& logger){ + int calls = 0; + const Outcome outcome = run_rng_handoff(logger, "Test RNG", [&]{ + calls++; + return RngHandoffResult{.summary = "2 resets"}; + }); + if (!outcome.succeeded()){ + return "Expected an ok outcome"; + } + if (calls != 1){ + return std::format("Expected 1 call, got {}", calls); + } + return true; +} + +// A failure throws, once: nothing reruns the program. +UnitTestResult test_failure(Logger& logger){ + int calls = 0; + try{ + run_rng_handoff(logger, "Test RNG", [&]{ + calls++; + return RngHandoffResult{.failure = "out of resets"}; + }); + }catch (OperationFailedException&){ + if (calls != 1){ + return std::format("Expected 1 call, got {}", calls); + } + return true; + } + return "Expected OperationFailedException"; +} + +// The user's turn is returned, not thrown, once; a success has none; a +// failure throws even with one. +UnitTestResult test_user_turn(Logger& logger){ + int calls = 0; + const std::optional turn = run_rng_handoff_or_user_turn(logger, "Test RNG", [&]{ + calls++; + return RngHandoffResult{.user_turn = "save by hand"}; + }); + if (!turn || *turn != "save by hand"){ + return "Expected the user's turn"; + } + if (calls != 1){ + return std::format("Expected 1 call, got {}", calls); + } + const std::optional none = run_rng_handoff_or_user_turn(logger, "Test RNG", []{ + return RngHandoffResult{.summary = "shiny"}; + }); + if (none){ + return "A success has no user's turn"; + } + try{ + run_rng_handoff_or_user_turn(logger, "Test RNG", []{ + return RngHandoffResult{.failure = "out of resets", .user_turn = "ignored"}; + }); + }catch (OperationFailedException&){ + return true; + } + return "A failure must throw even with a user's turn"; +} + +// A stop reports its message, then each line, to the log and to `show`; +// the notification text is one line each. +UnitTestResult test_stop_report(Logger& logger){ + std::vector shown; + const auto show = [&](const std::string& line){ + shown.push_back(line); + }; + const std::string text = report_rng_handoff_stop(logger, "Stopped for RNG.", {"Attempt 1", "Attempt 2"}, show); + if (text != "Stopped for RNG.\nAttempt 1\nAttempt 2"){ + return std::format("Wrong notification text: {}", text); + } + if (shown != std::vector{"Stopped for RNG.", "Attempt 1", "Attempt 2"}){ + return "The overlay must get the message and then each line"; + } + shown.clear(); + if (report_rng_handoff_stop(logger, "Stopped.", {}, show) != "Stopped." || shown.size() != 1){ + return "A stop without lines is its message alone"; + } + return true; +} + +const RngHandoffTestCase RNG_HANDOFF_CASES[] = { + { + .input = {.name = "RUN takes precedence over STOP", .body = test_mode}, + .expectation = {.description = "RUN wins when both options are on"}, + }, + { + .input = {.name = "a stop reports its message and lines", .body = test_stop_report}, + .expectation = {.description = "log, overlay and notification text agree"}, + }, + { + .input = {.name = "a success is ok", .body = test_success}, + .expectation = {.description = "an ok outcome after one call"}, + }, + { + .input = {.name = "a failure throws without a retry", .body = test_failure}, + .expectation = {.description = "OperationFailedException after one call"}, + }, + { + .input = {.name = "a user's turn is returned, not an error", .body = test_user_turn}, + .expectation = {.description = "the message comes back; a failure still throws"}, + }, +}; + +} // namespace + + +void add_tests_StoryRngHandoff(UnitTestDatabase& database){ + for (const RngHandoffTestCase& scenario : RNG_HANDOFF_CASES){ + database.add(scenario); + } +} + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.h new file mode 100644 index 0000000000..aca6f82c1b --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRngHandoff_Tests.h @@ -0,0 +1,23 @@ +/* Story RNG Handoff Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryRngHandoff_Tests_H +#define PokemonAutomation_Pokemon_StoryRngHandoff_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for the shared RNG handoff contract. +void add_tests_StoryRngHandoff(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRun.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRun.h index 9215d63a1c..d888b9f73d 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRun.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRun.h @@ -19,8 +19,8 @@ * run before anything is saved. * - The first checkpoint's own save at its start runs only for a new * game: after a load the save is already there, and from the game as - * it is the place is unconfirmed (console B, 2026-11-11: a run on the - * HOME menu "saved" first). + * it is the place is unconfirmed (the game may be on the HOME + * menu). * - A failure leaves the game where it failed. Nothing here closes the * game; the user (or the next run from the save) resumes from there. * diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRunCounts_Tests.cpp similarity index 63% rename from SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.cpp rename to SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRunCounts_Tests.cpp index 453a975516..04ca85e73a 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRunCounts_Tests.cpp @@ -1,4 +1,4 @@ -/* AutoStory Stats Tests +/* Story Run Counts Tests * * From: https://github.com/PokemonAutomation/ * @@ -10,37 +10,38 @@ #include #include #include "Common/Cpp/TestRunners/UnitTest.h" -#include "Pokemon_AutoStoryStats.h" -#include "Pokemon_AutoStoryStats_Tests.h" +#include "Pokemon/AutoStory/Pokemon_AutoStoryStats.h" +#include "Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.h" +#include "Pokemon_StoryRunCounts_Tests.h" namespace PokemonAutomation{ namespace Pokemon{ namespace{ -// Counts added to fresh stats, in order. -struct StoryCountsInput{ - std::string name; - std::vector added; -}; - -// The stats after them: counts add up rather than replace. -struct StoryCountsExpectation{ - uint64_t segments; - uint64_t checkpoints; - uint64_t resets; -}; - // Counts recorded into the AutoStory stats and the totals they must reach. -struct StoryCountsCase{ - StoryCountsInput input; - StoryCountsExpectation expectation; +struct StoryCountsTestCase{ + // Counts added to fresh stats, in order. + struct Input{ + std::string name; + std::vector added; + }; + + // The stats after them: counts add up rather than replace. + struct Expectation{ + uint64_t segments; + uint64_t checkpoints; + uint64_t resets; + }; + + Input input; + Expectation expectation; }; class Test_StoryCounts : public UnitTest{ public: - Test_StoryCounts(StoryCountsCase test_case) - : UnitTest(std::format("Pokemon::AutoStoryStats - {}", test_case.input.name)) + Test_StoryCounts(StoryCountsTestCase test_case) + : UnitTest(std::format("Pokemon::StoryRunCounts - {}", test_case.input.name)) , m_case(std::move(test_case)) {} @@ -49,7 +50,7 @@ class Test_StoryCounts : public UnitTest{ for (const StoryRunCounts& counts : m_case.input.added){ record_story_counts(counts, stats); } - const StoryCountsExpectation& expected = m_case.expectation; + const StoryCountsTestCase::Expectation& expected = m_case.expectation; if (stats.m_segment != expected.segments || stats.m_checkpoint != expected.checkpoints || stats.m_reset != expected.resets){ return std::format( "Expected {} segments, {} checkpoints and {} resets, got {}, {} and {}", @@ -61,10 +62,10 @@ class Test_StoryCounts : public UnitTest{ } private: - const StoryCountsCase m_case; + const StoryCountsTestCase m_case; }; -const StoryCountsCase STORY_COUNTS_CASES[] = { +const StoryCountsTestCase STORY_COUNTS_CASES[] = { { .input = {.name = "a segment's counts in the stats", .added = {{.segments = 1, .checkpoints = 2, .resets = 3}}}, .expectation = {.segments = 1, .checkpoints = 2, .resets = 3}, @@ -81,8 +82,8 @@ const StoryCountsCase STORY_COUNTS_CASES[] = { } // namespace -void add_tests_AutoStoryStats(UnitTestDatabase& database){ - for (const StoryCountsCase& test_case : STORY_COUNTS_CASES){ +void add_tests_StoryRunCounts(UnitTestDatabase& database){ + for (const StoryCountsTestCase& test_case : STORY_COUNTS_CASES){ database.add(test_case); } } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRunCounts_Tests.h similarity index 57% rename from SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.h rename to SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRunCounts_Tests.h index 21deca69ec..d0f804d208 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRunCounts_Tests.h @@ -1,11 +1,11 @@ -/* AutoStory Stats Tests +/* Story Run Counts Tests * * From: https://github.com/PokemonAutomation/ * */ -#ifndef PokemonAutomation_Pokemon_AutoStoryStats_Tests_H -#define PokemonAutomation_Pokemon_AutoStoryStats_Tests_H +#ifndef PokemonAutomation_Pokemon_StoryRunCounts_Tests_H +#define PokemonAutomation_Pokemon_StoryRunCounts_Tests_H #include "Common/Cpp/TestRunners/UnitTestDatabase.h" @@ -14,7 +14,7 @@ namespace Pokemon{ // Registers unit tests for the story counts added to the AutoStory stats. -void add_tests_AutoStoryStats(UnitTestDatabase& database); +void add_tests_StoryRunCounts(UnitTestDatabase& database); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRun_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRun_Tests.cpp index a45f39b5a4..b1c187331b 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRun_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRun_Tests.cpp @@ -62,21 +62,21 @@ struct FakeGame{ }; // A start as the program asks for it, and what the game must have done. -struct StartInput{ - std::string name; - StoryStart start; - std::string place_error; - StoryPreflight preflight; -}; -struct StartExpectation{ - bool succeeds; - size_t loads; - size_t rereads; - bool save_loaded; -}; struct StartCase{ - StartInput input; - StartExpectation expectation; + struct Input{ + std::string name; + StoryStart start; + std::string place_error; + StoryPreflight preflight; + }; + struct Expectation{ + bool succeeds; + size_t loads; + size_t rereads; + bool save_loaded; + }; + Input input; + Expectation expectation; }; constexpr StoryPreflight READY{.video_alive = true, .controller_ready = true, .game_running = true}; @@ -134,8 +134,8 @@ class Test_StoryStart : public UnitTest{ }; const StartCase START_CASES[] = { - // Console B, 2026-11-11: a BDSP run started at 00.04b with the game - // left at 00.04d walked from 00.04d's position. + // A game left at a later checkpoint must not be walked from: a + // checkpoint with a save loads that save first. { .input = {.name = "a checkpoint with a save loads it first", .start = StoryStart::FROM_SAVE, .place_error = "", .preflight = READY}, .expectation = {.succeeds = true, .loads = 1, .rereads = 1, .save_loaded = true}, @@ -152,9 +152,8 @@ const StartCase START_CASES[] = { .input = {.name = "a live start loads nothing", .start = StoryStart::LIVE, .place_error = "", .preflight = READY}, .expectation = {.succeeds = true, .loads = 0, .rereads = 0, .save_loaded = false}, }, - // Console B, 2026-11-11 13:32: the game was on HOME; the run saved, - // pressed X/R/ZL on HOME, then "loaded" nothing. The preflight stops it - // before any press. + // The game is on the HOME menu: every press would go to the console's + // menus, so the preflight stops the run before any press. { .input = {.name = "the HOME menu stops the run before the load", .start = StoryStart::FROM_SAVE, .place_error = "", .preflight = ON_HOME}, .expectation = {.succeeds = false, .loads = 0, .rereads = 0, .save_loaded = false}, @@ -176,16 +175,16 @@ const StartCase START_CASES[] = { // A preflight's checks, and the failure it reads as: the setup's, not // the story's, naming every check that failed. -struct PreflightInput{ - std::string name; - StoryPreflight preflight; -}; -struct PreflightExpectation{ - std::string outcome; -}; struct PreflightCase{ - PreflightInput input; - PreflightExpectation expectation; + struct Input{ + std::string name; + StoryPreflight preflight; + }; + struct Expectation{ + std::string outcome; + }; + Input input; + Expectation expectation; }; class Test_StoryPreflight : public UnitTest{ @@ -222,16 +221,16 @@ const PreflightCase PREFLIGHT_CASES[] = { // Whether the first checkpoint saves at its start: only a new game's. After // a load the save is already there; from the game as it is the place is -// unconfirmed (console B, 2026-11-11: "save" ran first on HOME). -struct FirstSaveInput{ - StoryStart start; -}; -struct FirstSaveExpectation{ - bool saves; -}; +// unconfirmed (the game may be on the HOME menu). struct FirstSaveCase{ - FirstSaveInput input; - FirstSaveExpectation expectation; + struct Input{ + StoryStart start; + }; + struct Expectation{ + bool saves; + }; + Input input; + Expectation expectation; }; class Test_StoryFirstSave : public UnitTest{ @@ -260,10 +259,24 @@ const FirstSaveCase FIRST_SAVE_CASES[] = { // A failed attempt's reload loads the save through the game. +struct ReloadCase{ + struct Input{ + std::string name; + }; + struct Expectation{ + size_t loads; + size_t rereads; + std::string log_line; + }; + Input input; + Expectation expectation; +}; + class Test_StoryReload : public UnitTest{ public: - Test_StoryReload() - : UnitTest("Pokemon::StoryRun - a retry reloads the save") + Test_StoryReload(const ReloadCase& test_case) + : UnitTest(std::format("Pokemon::StoryRun - {}", test_case.input.name)) + , m_case(test_case) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ @@ -272,20 +285,46 @@ class Test_StoryReload : public UnitTest{ SegmentHooks hooks = story_segment_hooks(game); RunState state; const Outcome outcome = hooks.reload(state); - if (!outcome.succeeded() || fake.loads != 1 || fake.rereads != 1 || !fake.logged("[Story] retry: loading the save")){ - return std::format("Expected one load and reread, got {} and {} ({})", fake.loads, fake.rereads, to_string(outcome)); + const ReloadCase::Expectation& expected = m_case.expectation; + if (!outcome.succeeded() || fake.loads != expected.loads || fake.rereads != expected.rereads || !fake.logged(expected.log_line)){ + return std::format("Expected {} load and {} reread, got {} and {} ({})", expected.loads, expected.rereads, fake.loads, fake.rereads, to_string(outcome)); } return true; } + +private: + const ReloadCase m_case; +}; + +const ReloadCase RELOAD_CASES[] = { + { + .input = {.name = "a retry reloads the save"}, + .expectation = {.loads = 1, .rereads = 1, .log_line = "[Story] retry: loading the save"}, + }, }; // A run that stopped short throws, logs the failure line and touches // nothing in the game: no reload, no save, no reset to HOME. +struct FailureCase{ + struct Input{ + std::string name; + std::string checkpoint; + std::string event; + std::string stopped_after; + }; + struct Expectation{ + std::string message; + }; + Input input; + Expectation expectation; +}; + class Test_StoryFailureLeavesGame : public UnitTest{ public: - Test_StoryFailureLeavesGame() - : UnitTest("Pokemon::StoryRun - a failed run leaves the game where it failed") + Test_StoryFailureLeavesGame(const FailureCase& test_case) + : UnitTest(std::format("Pokemon::StoryRun - {}", test_case.input.name)) + , m_case(test_case) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ @@ -293,18 +332,18 @@ class Test_StoryFailureLeavesGame : public UnitTest{ StoryGameHooks game = fake.hooks(); SegmentReport report; report.completed = false; - report.failed_checkpoint = "00.04b"; - report.failed_event = "talk_mom"; + report.failed_checkpoint = m_case.input.checkpoint; + report.failed_event = m_case.input.event; report.failure = outcome_failed("expect_failed", "dialogue opened"); AutoStoryStats stats; std::string message; try{ - finish_story_run(report, "00.05", stats, game); + finish_story_run(report, m_case.input.stopped_after, stats, game); return "Expected an OperationFailedException"; }catch (OperationFailedException& e){ message = e.message(); } - if (message != "AutoStory stopped at 00.04b talk_mom: expect_failed: dialogue opened"){ + if (message != m_case.expectation.message){ return std::format("Unexpected message: {}", message); } if (!fake.logged(STORY_LEFT_WHERE_FAILED)){ @@ -315,14 +354,37 @@ class Test_StoryFailureLeavesGame : public UnitTest{ } return true; } + +private: + const FailureCase m_case; +}; + +const FailureCase FAILURE_CASES[] = { + { + .input = {.name = "a failed run leaves the game where it failed", .checkpoint = "00.04b", .event = "talk_mom", .stopped_after = "00.05"}, + .expectation = {.message = "AutoStory stopped at 00.04b talk_mom: expect_failed: dialogue opened"}, + }, }; // A completed run logs and returns the line the finish notification sends. +struct FinishedCase{ + struct Input{ + std::string name; + std::string last_checkpoint; + }; + struct Expectation{ + std::string line; + }; + Input input; + Expectation expectation; +}; + class Test_StoryFinished : public UnitTest{ public: - Test_StoryFinished() - : UnitTest("Pokemon::StoryRun - a completed run is done through its last checkpoint") + Test_StoryFinished(const FinishedCase& test_case) + : UnitTest(std::format("Pokemon::StoryRun - {}", test_case.input.name)) + , m_case(test_case) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ @@ -331,20 +393,44 @@ class Test_StoryFinished : public UnitTest{ SegmentReport report; report.completed = true; AutoStoryStats stats; - const std::string done = finish_story_run(report, "00.05", stats, game); - if (done != "AutoStory: done through 00.05." || !fake.logged(done)){ + const std::string done = finish_story_run(report, m_case.input.last_checkpoint, stats, game); + if (done != m_case.expectation.line || !fake.logged(done)){ return std::format("Unexpected finish line: {}", done); } return true; } + +private: + const FinishedCase m_case; +}; + +const FinishedCase FINISHED_CASES[] = { + { + .input = {.name = "a completed run is done through its last checkpoint", .last_checkpoint = "00.05"}, + .expectation = {.line = "AutoStory: done through 00.05."}, + }, }; // A verify run short of its passes throws with the summary. +struct VerifyCase{ + struct Input{ + std::string name; + std::string checkpoint; + size_t passes; + }; + struct Expectation{ + std::string message; + }; + Input input; + Expectation expectation; +}; + class Test_StoryVerifyFailed : public UnitTest{ public: - Test_StoryVerifyFailed() - : UnitTest("Pokemon::StoryRun - an unverified checkpoint fails with its summary") + Test_StoryVerifyFailed(const VerifyCase& test_case) + : UnitTest(std::format("Pokemon::StoryRun - {}", test_case.input.name)) + , m_case(test_case) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ @@ -352,20 +438,30 @@ class Test_StoryVerifyFailed : public UnitTest{ StoryGameHooks game = fake.hooks(); VerifyPolicy policy; VerifyReport report; - report.passes = 1; + report.passes = m_case.input.passes; report.verified = false; std::string message; try{ - finish_story_verify("00.15b", policy, report, game); + finish_story_verify(m_case.input.checkpoint, policy, report, game); return "Expected an OperationFailedException"; }catch (OperationFailedException& e){ message = e.message(); } - if (message != "AutoStory verify: 00.15b 1/3 passed, 2 needed." || !fake.logged(STORY_LEFT_WHERE_FAILED)){ + if (message != m_case.expectation.message || !fake.logged(STORY_LEFT_WHERE_FAILED)){ return std::format("Unexpected message: {}", message); } return true; } + +private: + const VerifyCase m_case; +}; + +const VerifyCase VERIFY_CASES[] = { + { + .input = {.name = "an unverified checkpoint fails with its summary", .checkpoint = "00.15b", .passes = 1}, + .expectation = {.message = "AutoStory verify: 00.15b 1/3 passed, 2 needed."}, + }, }; } // namespace @@ -381,10 +477,18 @@ void add_tests_StoryRun(UnitTestDatabase& database){ for (const FirstSaveCase& test_case : FIRST_SAVE_CASES){ database.add(test_case); } - database.add(); - database.add(); - database.add(); - database.add(); + for (const ReloadCase& test_case : RELOAD_CASES){ + database.add(test_case); + } + for (const FailureCase& test_case : FAILURE_CASES){ + database.add(test_case); + } + for (const FinishedCase& test_case : FINISHED_CASES){ + database.add(test_case); + } + for (const VerifyCase& test_case : VERIFY_CASES){ + database.add(test_case); + } } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySavePolicy_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySavePolicy_Tests.cpp index bae25ec95a..72dcc9961a 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySavePolicy_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySavePolicy_Tests.cpp @@ -391,21 +391,21 @@ UnitTestResult test_end_pose_from_the_plan(){ } -// The scenario to run. -struct SavePolicyInput{ - std::string name; - UnitTestResult (*test)() = nullptr; -}; - -// What the scenario checks; it returns its own failure. -struct SavePolicyExpectation{ - std::string_view description; -}; - // A save-policy scenario and its expected result. struct SavePolicyCase{ - SavePolicyInput input; - SavePolicyExpectation expectation; + // The scenario to run. + struct Input{ + std::string name; + UnitTestResult (*test)() = nullptr; + }; + + // What the scenario checks; it returns its own failure. + struct Expectation{ + std::string_view description; + }; + + Input input; + Expectation expectation; }; class Test_StorySavePolicy : public UnitTest{ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp index c85971adf9..c46ab2764e 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp @@ -6,6 +6,7 @@ #include #include +#include "Pokemon_CheckpointAttempts.h" #include "Pokemon_StoryLog.h" #include "Pokemon_StorySegments.h" @@ -15,22 +16,13 @@ namespace Pokemon{ namespace{ -// "[Event] 00.03/2 x: start" as "00.03/2 x: start", for the status sink. -std::string without_tag(const std::string& line){ - if (line.empty() || line[0] != '['){ - return line; - } - const size_t end = line.find("] "); - return end == std::string::npos ? line : line.substr(end + 2); -} - // A line for the log, and without its tag for the status sink, if set. void say(const std::string& line, SegmentHooks& hooks){ if (hooks.log){ hooks.log(line); } if (hooks.status){ - hooks.status(without_tag(line)); + hooks.status(story_short_line(line)); } } @@ -121,7 +113,7 @@ Act checked_save(Act save, std::string id, SegmentHooks& hooks){ } const Outcome checked = hooks.check_postconditions(id, state); if (!checked.succeeded()){ - say(std::format("[Checkpoint] {}: postconditions failed, not saving", id), hooks); + say(checkpoint_log_line(id, "postconditions failed, not saving"), hooks); return outcome_failed("postconditions_failed", to_string(checked)); } return save(state); @@ -152,7 +144,7 @@ void set_save(const Row& row, const SegmentPolicy& policy, SegmentHooks& hooks, const SavePoint point = row.last ? SavePoint::RUN_END : SavePoint::CHECKPOINT_END; if (!story_save_allowed(mode, CheckpointVerdict::PASSED, point)){ checkpoint.save = [&hooks, id = row.spec.id, mode](RunState&){ - say(std::format("[Checkpoint] {}: not saving (save mode {})", id, story_save_mode_name(mode)), hooks); + say(checkpoint_log_line(id, std::format("not saving (save mode {})", story_save_mode_name(mode))), hooks); return outcome_ok(); }; return; @@ -172,45 +164,52 @@ size_t row_attempts(const Row& row, const SegmentPolicy& policy){ return 1; } -// One row to success, reloading and rebuilding after each failed attempt. -// Returns the last attempt's report, or the reload's failure; `run` says -// how many attempts it took. +// One row to success through run_checkpoint_attempts(), reloading and +// rebuilding after each failed attempt. Returns the last attempt's report, +// or the reload's failure; `run` says how many attempts it took. CheckpointReport run_row( const Segment& segment, const Row& row, const SegmentPolicy& policy, SegmentHooks& hooks, SegmentReport& report, CheckpointRun& run, RunState& state ){ - const size_t attempts = row_attempts(row, policy); - for (size_t attempt = 1; ; attempt++){ - run.attempts = attempt; - Checkpoint checkpoint = row.spec.build(); - set_save(row, policy, hooks, checkpoint); - EventLogger log = [&hooks](const std::string& line){ say(line, hooks); }; - CheckpointReport result = run_checkpoint(state, checkpoint, log); - if (result.completed){ - run.passed = true; - say(checkpoint_pass_line(row.spec.id, attempt), hooks); - return result; - } - if (result.failure.failure == GAME_NOT_RUNNING){ - say(std::format("[Segment] {}: {} failed; the game is not running, so no reload", segment.id, row.spec.id), hooks); - return result; - } - if (attempt >= attempts){ - if (attempts < policy.attempts_per_checkpoint){ + const size_t max_attempts = row_attempts(row, policy); + CheckpointReport result; + const CheckpointAttempts attempts{ + .max_attempts = max_attempts, + .run = [&](size_t attempt){ + run.attempts = attempt; + Checkpoint checkpoint = row.spec.build(); + set_save(row, policy, hooks, checkpoint); + EventLogger log = [&hooks](const std::string& line){ say(line, hooks); }; + result = run_checkpoint(state, checkpoint, log); + if (result.completed){ + return AttemptResult::PASSED; + } + if (result.failure.failure == GAME_NOT_RUNNING){ + say(std::format("[Segment] {}: {} failed; the game is not running, so no reload", segment.id, row.spec.id), hooks); + return AttemptResult::FINAL; + } + if (attempt >= max_attempts && max_attempts < policy.attempts_per_checkpoint){ say(std::format( "[Segment] {}: {} failed; save mode {} left no save at its start, so no reload", segment.id, row.spec.id, story_save_mode_name(policy.save_mode) ), hooks); } - return result; - } - say(segment_reload_line(segment.id, row.spec.id, attempt + 1, policy.attempts_per_checkpoint), hooks); - report.reloads++; - Outcome reloaded = reload(state, hooks); - if (!reloaded.succeeded()){ - return failed_report(std::move(reloaded)); - } - } + return AttemptResult::FAILED; + }, + .reset = [&](size_t next_attempt){ + say(checkpoint_reset_line(row.spec.id, next_attempt, policy.attempts_per_checkpoint), hooks); + report.reloads++; + Outcome reloaded = reload(state, hooks); + if (!reloaded.succeeded()){ + result = failed_report(std::move(reloaded)); + return false; + } + return true; + }, + .log = [&hooks](const std::string& line){ say(line, hooks); }, + }; + run.passed = run_checkpoint_attempts(row.spec.id, attempts).passed; + return result; } SegmentReport stop_segment(const std::string& id, Outcome failure, SegmentHooks& hooks, SegmentReport& report){ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h index ddb4325878..280504487f 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h @@ -68,7 +68,7 @@ struct SegmentHooks{ // allows (story_save_allowed()). Logs through `hooks.log`, // around the checkpoint lines: // [Segment] 00: start, 3 checkpoints -// [Segment] 00: 00.02 failed, reloading for attempt 2/3 +// [Checkpoint] 00.02: failed, resetting for attempt 2/3 // [Checkpoint] 00.02: passed on attempt 2 (1 reset) // [Checkpoint] 00.03: passed on attempt 1 (one-shot) // [Segment] 00: 2 of 3 checkpoints one-shot diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.cpp index c4073dd8d5..89f1967edb 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.cpp @@ -204,7 +204,7 @@ UnitTestResult test_reloads_a_failed_checkpoint(){ if (!report.completed || report.reloads != 1 || lab.world.trace != expected){ return std::format("Expected a reload, then the walk again:{}", lab.all()); } - if (!lab.has("[Segment] 00: 00.02 failed, reloading for attempt 2/3")){ + if (!lab.has("[Checkpoint] 00.02: failed, resetting for attempt 2/3")){ return std::format("Expected the reload logged:{}", lab.all()); } return true; @@ -407,18 +407,18 @@ UnitTestResult test_describe(){ } -struct StorySegmentInput{ - std::string name; - UnitTestResult (*test)(); -}; +struct StorySegmentTestCase{ + struct Input{ + std::string name; + UnitTestResult (*test)(); + }; -struct StorySegmentExpectation{ - std::string_view description; -}; + struct Expectation{ + std::string_view description; + }; -struct StorySegmentTestCase{ - StorySegmentInput input; - StorySegmentExpectation expectation; + Input input; + Expectation expectation; }; class Test_StorySegments : public UnitTest{ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor_Tests.cpp index 1d9ef32865..e22aa36eea 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor_Tests.cpp @@ -727,18 +727,18 @@ UnitTestResult test_lint(){ } -struct StoryStepInput{ - std::string name; - UnitTestResult (*test)(); -}; +struct StoryStepTestCase{ + struct Input{ + std::string name; + UnitTestResult (*test)(); + }; -struct StoryStepExpectation{ - std::string_view description; -}; + struct Expectation{ + std::string_view description; + }; -struct StoryStepTestCase{ - StoryStepInput input; - StoryStepExpectation expectation; + Input input; + Expectation expectation; }; class Test_StoryStepExecutor : public UnitTest{ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySwitchPrompts.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySwitchPrompts.cpp new file mode 100644 index 0000000000..3dcb5a4d94 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySwitchPrompts.cpp @@ -0,0 +1,53 @@ +/* AutoStory Switch Launch Prompts + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "NintendoSwitch/NintendoSwitch_Settings.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "NintendoSwitch/Inference/NintendoSwitch_CloseGameDetector.h" +#include "NintendoSwitch/Inference/NintendoSwitch_StartGameUserSelectDetector.h" +#include "NintendoSwitch/Inference/NintendoSwitch_UpdatePopupDetector.h" +#include "Pokemon_StorySwitchPrompts.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + +using namespace std::chrono_literals; +using namespace NintendoSwitch; + + +namespace{ + +// Whether `detector` fires on the look's frame; false without one. +template +std::function frame_shows(std::shared_ptr detector){ + return [detector = std::move(detector)](const ScreenObservation& look){ + return look.frame && look.frame->frame && detector->detect(*look.frame->frame); + }; +} + +} // namespace + + +std::vector switch_launch_prompts(ConsoleHandle& console){ + const StoryLaunchPress scroll_up{.button = BUTTON_UP, .hold = 40ms, .release = 40ms}; + return { + {.name = "close software", .matches = frame_shows(std::make_shared(console)), + .press = {.button = BUTTON_A, .hold = 160ms, .release = ConsoleSettings::instance().CLOSE_GAME_DELAY}}, + {.name = "user select", .matches = frame_shows(std::make_shared(console)), + .press = {.button = BUTTON_A, .hold = 160ms}}, + {.name = "update prompt", .matches = frame_shows(std::make_shared(console)), + .press = {.button = BUTTON_A, .hold = 160ms}, .before = {scroll_up, scroll_up}}, + }; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySwitchPrompts.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySwitchPrompts.h new file mode 100644 index 0000000000..5bfa8dba26 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySwitchPrompts.h @@ -0,0 +1,33 @@ +/* AutoStory Switch Launch Prompts + * + * From: https://github.com/PokemonAutomation/ + * + * The Switch's own screens between HOME and a game, each answered by one + * press: HOME's "Close software?", the user select and the update prompt. + * They are the same in every game, so each game's StoryLaunchPolicy + * (Engine/Pokemon_StoryLaunch.h) starts its prompts with these. + * + */ + +#ifndef PokemonAutomation_Pokemon_StorySwitchPrompts_H +#define PokemonAutomation_Pokemon_StorySwitchPrompts_H + +#include +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// On `console`'s frames: "Close software?" takes A, then the console's +// close delay (Switch settings); the user select takes A on the user +// highlighted; the update prompt takes Up twice, to "Start Software", then +// A. A look without a frame (a simulation) shows none of them. +std::vector switch_launch_prompts(NintendoSwitch::ConsoleHandle& console); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_TestExpect.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_TestExpect.h new file mode 100644 index 0000000000..203bed4d21 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_TestExpect.h @@ -0,0 +1,31 @@ +/* Test Expect + * + * From: https://github.com/PokemonAutomation/ + * + * Checks the unit tests of the AutoStory judges share. + * + */ + +#ifndef PokemonAutomation_Pokemon_TestExpect_H +#define PokemonAutomation_Pokemon_TestExpect_H + +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Passes when `got` is `expected`; else fails naming `what`. +inline UnitTestResult expect_equal(const std::string& what, const std::string& got, const std::string& expected){ + if (got == expected){ + return true; + } + return std::format("{}: expected \"{}\", got \"{}\"", what, expected, got); +} + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Transitions.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Transitions.cpp index e243313477..28de36ccf6 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Transitions.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Transitions.cpp @@ -130,18 +130,22 @@ TransitionTracker::Level TransitionTracker::level_of(const ImageStats& middle) c } std::optional TransitionTracker::push(const TransitionSample& sample){ + // No stats (an empty or dropped frame) would read as uniform and dark. + if (sample.middle.count == 0){ + return std::nullopt; + } switch (level_of(sample.middle)){ case Level::SCENE: return on_scene(sample); case Level::PARTIAL_DARK: - return on_partial(sample, false); + return on_partial(sample, /*white=*/false); case Level::PARTIAL_WHITE: - return on_partial(sample, true); + return on_partial(sample, /*white=*/true); case Level::DARK: - on_uniform(sample, false); + on_uniform(sample, /*white=*/false); return std::nullopt; case Level::WHITE: - on_uniform(sample, true); + on_uniform(sample, /*white=*/true); return std::nullopt; } return std::nullopt; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Transitions_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Transitions_Tests.cpp index ef4818a42e..a91b6fa631 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Transitions_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Transitions_Tests.cpp @@ -9,6 +9,8 @@ */ #include +#include +#include #include #include #include @@ -48,6 +50,8 @@ const ImageStats FALLING_DARK = stats(60, 60, 60, 10); const ImageStats DARK = stats(5, 5, 5, 1); const ImageStats FALLING_WHITE = stats(160, 160, 160, 10); const ImageStats WHITE = stats(250, 250, 250, 1); +// An empty or dropped frame: no stats at all. +const ImageStats EMPTY; SceneChange compare_means(const TransitionSample& before, const TransitionSample& after){ const FloatPixel& a = before.middle.average; @@ -78,25 +82,25 @@ std::string run_tracker(const std::vector& spans, TransitionTracker& track } -// A synthetic stream of frames, through one game's profile. -struct TrackInput{ - std::string name; - std::vector spans; - const FadeProfile* profile = &BDSP_FADE_PROFILE; - bool compare = true; -}; - -struct TrackExpectation{ - // The transitions completed, as run_tracker() puts them. - std::string transitions; - // What judge_warp() makes of the tracker's current() at the end; empty - // for none. - std::string warp = ""; -}; - struct TrackCase{ - TrackInput input; - TrackExpectation expectation; + // A synthetic stream of frames, through one game's profile. + struct Input{ + std::string name; + std::vector spans; + const FadeProfile* profile = &BDSP_FADE_PROFILE; + bool compare = true; + }; + + struct Expectation{ + // The transitions completed, as run_tracker() puts them. + std::string transitions; + // What judge_warp() makes of the tracker's current() at the end; empty + // for none. + std::string warp = ""; + }; + + Input input; + Expectation expectation; }; class Test_TransitionTracker : public UnitTest{ @@ -106,7 +110,7 @@ class Test_TransitionTracker : public UnitTest{ , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - const TrackInput& input = m_case.input; + const TrackCase::Input& input = m_case.input; TransitionTracker tracker(*input.profile, input.compare ? compare_means : TransitionTracker::CompareScenes()); const std::string got = run_tracker(input.spans, tracker); if (got != m_case.expectation.transitions){ @@ -161,29 +165,40 @@ UnitTestResult test_sample_of_frame(){ } -// Captured frames: CommandLineTests//Transitions// holds -// frames named by their time in ms ("0.png", "250.png"), and expected.txt -// with the transitions run_tracker() should report (no scene comparison). -struct CapturedInput{ - std::string game; - const FadeProfile* profile; -}; - -// Each case folder's expected.txt holds its expectation. -struct CapturedExpectation{ -}; - struct CapturedCase{ - CapturedInput input; - CapturedExpectation expectation; + // Captured frames: CommandLineTests//Transitions// holds + // frames named by their time in ms ("0.png", "250.png"), and expected.txt + // with the transitions run_tracker() should report (no scene comparison). + struct Input{ + std::string game; + const FadeProfile* profile; + }; + + // Each case folder's expected.txt holds its expectation. + struct Expectation{ + }; + + Input input; + Expectation expectation; }; std::string track_captured_case(const Filesystem::Path& folder, const FadeProfile& profile){ std::vector> frames; for (const Filesystem::Path& file : Filesystem::list_directory(folder)){ - if (file.extension().string() == ".png"){ - frames.emplace_back(std::stoi(file.stem().string()), file); + std::string extension = file.extension().string(); + std::transform(extension.begin(), extension.end(), extension.begin(), [](unsigned char c){ + return static_cast(std::tolower(c)); + }); + if (extension != ".png"){ + continue; + } + const std::string stem = file.stem().string(); + int ms = 0; + const auto parsed = std::from_chars(stem.data(), stem.data() + stem.size(), ms); + if (parsed.ec != std::errc() || parsed.ptr != stem.data() + stem.size()){ + return std::format("frame {} is not named by its time in ms", file.filename().string()); } + frames.emplace_back(ms, file); } std::sort(frames.begin(), frames.end(), [](const auto& a, const auto& b){ return a.first < b.first; @@ -226,22 +241,22 @@ class Test_CapturedTransitions : public UnitTest{ } private: - CapturedInput m_input; + CapturedCase::Input m_input; }; -struct TransitionsInput{ - std::string name; - UnitTestResult (*body)(); -}; +struct TransitionsCase{ + struct Input{ + std::string name; + UnitTestResult (*body)(); + }; -// The body checks its own expectations. -struct TransitionsExpectation{ -}; + // The body checks its own expectations. + struct Expectation{ + }; -struct TransitionsCase{ - TransitionsInput input; - TransitionsExpectation expectation; + Input input; + Expectation expectation; }; class Test_Transitions : public UnitTest{ @@ -255,7 +270,7 @@ class Test_Transitions : public UnitTest{ } private: - TransitionsInput m_input; + TransitionsCase::Input m_input; }; } // namespace @@ -331,6 +346,22 @@ const TrackCase TRACK_CASES[] = { {{0, 100, ROUTE}, {200, 500, DARK}, {600, 2500, DIM_FLOOR}}}, .expectation = {"black fade +200 ms, peak +200 ms, rise +600 ms, end +1700 ms, other scene", "warped"}, }, + { + .input = {"empty frames in a scene start no fade", + {{0, 100, ROUTE}, {200, 300, EMPTY}, {400, 500, ROUTE}}}, + .expectation = {"none"}, + }, + { + .input = {"an empty frame in a fall is skipped", + {{0, 100, ROUTE}, {200, 200, EMPTY}, {300, 300, FALLING_DARK}, {400, 600, DARK}}}, + .expectation = {"none", "fade still showing"}, + }, + { + .input = {"an empty frame in the rise does not move the rise", + {{0, 100, ROUTE}, {200, 200, FALLING_DARK}, {300, 500, DARK}, {600, 600, FALLING_DARK}, + {700, 700, EMPTY}, {800, 900, HOUSE}}}, + .expectation = {"black fade +200 ms, peak +300 ms, rise +600 ms, end +800 ms, other scene", "warped"}, + }, { .input = {"a fade in progress is still showing", {{0, 100, ROUTE}, {200, 200, FALLING_DARK}, {300, 600, DARK}}}, diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_WarpJudge.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_WarpJudge.h index ab112a4362..9ced24c3a3 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_WarpJudge.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_WarpJudge.h @@ -2,7 +2,8 @@ * * From: https://github.com/PokemonAutomation/ * - * Split out of Pokemon_Interrupts.h, unchanged; it includes this header. + * The warp judge: whether a fade was a warp, from a game's timing. Its + * definitions are in Pokemon_Interrupts.cpp. * */ @@ -13,7 +14,7 @@ #include #include #include "Common/Cpp/Time.h" -#include "Pokemon_ScreenEvents.h" +#include "CommonTools/ScreenEvents/ScreenEvents.h" #include "Pokemon_Transitions.h" namespace PokemonAutomation{ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg_Tests.cpp index 50cda4ade3..d26fb413e3 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg_Tests.cpp @@ -36,21 +36,21 @@ const StoryActionSpec& mart_spec(){ return spec; } -// Args checked against the Mart's spec, then filled in. -struct ArgsInput{ - std::string_view name; - StoryActionArgs args; -}; - -// The errors joined by "; ", and the args with their fallbacks. -struct ArgsExpectation{ - std::string_view error = {}; - StoryActionArgs filled = {}; -}; - struct ArgsCase{ - ArgsInput input; - ArgsExpectation expectation; + // Args checked against the Mart's spec, then filled in. + struct Input{ + std::string_view name; + StoryActionArgs args; + }; + + // The errors joined by "; ", and the args with their fallbacks. + struct Expectation{ + std::string_view error = {}; + StoryActionArgs filled = {}; + }; + + Input input; + Expectation expectation; }; const ArgsCase CASES[] = { diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.cpp index 2908ffd8d5..a0f08de28a 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.cpp @@ -10,6 +10,12 @@ namespace PokemonAutomation{ namespace Pokemon{ +void record_story_counts(const StoryRunCounts& counts, AutoStoryStats& stats){ + stats.m_segment += counts.segments; + stats.m_checkpoint += counts.checkpoints; + stats.m_reset += counts.resets; +} + bool CheckpointRun::one_shot() const{ return passed && attempts == 1; } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.h index f6113a6466..1e782e6ed9 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.h @@ -42,6 +42,19 @@ struct SegmentRange{ }; +// What a story run adds to the program's stats. +struct StoryRunCounts{ + size_t segments = 0; + // Checkpoints passed (and saved at). + size_t checkpoints = 0; + // Game resets or reloads after failed attempts. + size_t resets = 0; +}; + +// Adds `counts` to the shared AutoStory stats. +void record_story_counts(const StoryRunCounts& counts, AutoStoryStats& stats); + + // Summary of checkpoint attempts and pass status. struct CheckpointRun{ std::string id; diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.cpp index ceafa9304d..788bfe9b84 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.cpp @@ -68,14 +68,8 @@ std::string describe(const ConnectedMaps& world, const std::optional& return std::format("{} {}", world.map(place->map).name, to_string(place->tile)); } -// Where a step or a warp should put the player, or nothing for nowhere. -struct PlaceExpectation{ - std::optional place; -}; - // Why `actual` is not `expected`, if it is not. -std::optional mismatch(const ConnectedMaps& world, const std::optional& actual, const PlaceExpectation& expectation){ - const std::optional& expected = expectation.place; +std::optional mismatch(const ConnectedMaps& world, const std::optional& actual, const std::optional& expected){ std::optional wanted; if (expected){ wanted = MapPlace{*world.find(expected->map), expected->tile}; @@ -87,14 +81,18 @@ std::optional mismatch(const ConnectedMaps& world, const std::optio } -struct StepInput{ - std::string_view name; - NamedPlace from; - Direction direction; -}; struct StepCase{ - StepInput input; - PlaceExpectation expectation; + struct Input{ + std::string_view name; + NamedPlace from; + Direction direction; + }; + // Where the step should put the player, or nothing for nowhere. + struct Expectation{ + std::optional place; + }; + Input input; + Expectation expectation; }; class Test_ConnectedMaps_Step : public UnitTest{ @@ -104,9 +102,9 @@ class Test_ConnectedMaps_Step : public UnitTest{ {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ const ConnectedMaps world(test_maps()); - const StepInput& input = m_scenario.input; + const StepCase::Input& input = m_scenario.input; const MapPlace from{*world.find(input.from.map), input.from.tile}; - if (std::optional error = mismatch(world, world.neighbour(from, input.direction), m_scenario.expectation)){ + if (std::optional error = mismatch(world, world.neighbour(from, input.direction), m_scenario.expectation.place)){ return *error; } return true; @@ -159,13 +157,17 @@ const StepCase STEP_CASES[] = { }; -struct WarpInput{ - std::string_view name; - MapWarp warp; -}; struct WarpCase{ - WarpInput input; - PlaceExpectation expectation; + struct Input{ + std::string_view name; + MapWarp warp; + }; + // Where the warp should put the player, or nothing for nowhere. + struct Expectation{ + std::optional place; + }; + Input input; + Expectation expectation; }; class Test_ConnectedMaps_Warp : public UnitTest{ @@ -175,7 +177,7 @@ class Test_ConnectedMaps_Warp : public UnitTest{ {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ const ConnectedMaps world(test_maps()); - if (std::optional error = mismatch(world, world.warp_arrival(m_scenario.input.warp), m_scenario.expectation)){ + if (std::optional error = mismatch(world, world.warp_arrival(m_scenario.input.warp), m_scenario.expectation.place)){ return *error; } return true; @@ -204,17 +206,17 @@ const WarpCase WARP_CASES[] = { }; -struct StalePlaceInput{ - std::string_view name; - size_t map; -}; -struct StalePlaceExpectation{ - // Whether reading the place throws InternalProgramError. - bool rejected = false; -}; struct StalePlaceCase{ - StalePlaceInput input; - StalePlaceExpectation expectation; + struct Input{ + std::string_view name; + size_t map; + }; + struct Expectation{ + // Whether reading the place throws InternalProgramError. + bool rejected = false; + }; + Input input; + Expectation expectation; }; // A place whose map index is past the loaded maps (from another world, or diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner.h index 4ebb84e9c2..043b7e5306 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner.h @@ -25,6 +25,24 @@ * is whether holding `direction` keeps the player on this tile: a wall, but * not a person who may walk off or an edge to a map that isn't loaded. * + * A model may also have + * + * int open_stop_risk(const State& state, Direction direction) const; + * + * the extra cost of a leg that ends on `state` walking `direction` where + * the player could keep going: what a tile past the stop would cost, e.g. + * a tile a trainer may see, and + * + * std::optional tap_stop_risk(const State& state, Direction direction) const; + * + * for such a leg of one step from where a hold to a wall ended, the extra + * cost to weigh instead of open_stop_risk() and LegCosts::per_open_wild_stop, + * or nothing to weigh those. A step can't stop part-way, so where an + * interruption stopping it would leave the player is no guess: e.g. on + * open water, a hold to a wall and a tile on starts a leg down a column + * with no wall of its own. Once a route: a run of them, wall to wall, + * would weave round the open stops it can't avoid. + * */ #ifndef PokemonAutomation_Pokemon_LegPlanner_H @@ -128,14 +146,18 @@ std::optional>> plan_legs( namespace LegPlannerInternal{ // A search node: the player, the way they face (4: not known), whether -// they are in the middle of a leg that could go on, and whether that leg -// had a wild step. +// they are in the middle of a leg that could go on, whether that leg had a +// wild step, whether it is one step from where a hold to a wall ended, and +// whether such a step was stopped after earlier (see tap_stop_risk() at +// the top of this file). template struct Node{ State state; int facing = 4; bool moving = false; bool wild = false; + bool tap_from_wall = false; + bool tapped = false; bool operator==(const Node& other) const = default; }; @@ -144,7 +166,8 @@ struct Node{ template struct NodeHash{ size_t operator()(const Node& node) const{ - return StateHash()(node.state) * 20 + node.facing * 4 + (node.moving ? 2 : 0) + (node.wild ? 1 : 0); + return StateHash()(node.state) * 80 + node.facing * 16 + (node.moving ? 8 : 0) + (node.wild ? 4 : 0) + + (node.tap_from_wall ? 2 : 0) + (node.tapped ? 1 : 0); } }; @@ -162,7 +185,18 @@ int stop_cost(const Model& model, const Node& node, const LegCosts& costs if (!node.moving || model.held_at(node.state, (Direction)node.facing)){ return 0; } - return costs.per_open_stop + (node.wild ? costs.per_open_wild_stop : 0); + if constexpr (requires { model.tap_stop_risk(node.state, Direction::NORTH); }){ + if (node.tap_from_wall && !node.tapped){ + if (const std::optional risk = model.tap_stop_risk(node.state, (Direction)node.facing)){ + return costs.per_open_stop + *risk; + } + } + } + int cost = costs.per_open_stop + (node.wild ? costs.per_open_wild_stop : 0); + if constexpr (requires { model.open_stop_risk(node.state, Direction::NORTH); }){ + cost += model.open_stop_risk(node.state, (Direction)node.facing); + } + return cost; } // Combines atomic movement steps into contiguous legs. @@ -246,7 +280,11 @@ void expand( const bool continues = step->action == 0 && !step->ends_leg; const bool extends = current.moving && current.facing == (int)direction && step->action == 0 && !step->starts_leg; - const Node next{step->next, (int)step->facing, continues, (extends && current.wild) || step->wild}; + const bool from_wall = current.moving && model.held_at(current.state, (Direction)current.facing); + const Node next{ + step->next, (int)step->facing, continues, (extends && current.wild) || step->wild, + !extends && from_wall, current.tapped || (!extends && current.tap_from_wall) + }; int step_cost = costs.per_tile + step->extra_cost; if (current.facing != (int)direction){ step_cost += costs.per_turn + stop_cost(model, current, costs); @@ -344,7 +382,7 @@ std::optional>> plan_legs( using State = typename Model::State; using Node = LegPlannerInternal::Node; - const Node first{start, start_facing ? (int)*start_facing : 4, false, false}; + const Node first{start, start_facing ? (int)*start_facing : 4, false, false, false, false}; LegPlannerInternal::Search search; const std::optional end = LegPlannerInternal::search_goal(model, first, at_goal, goal_facing, costs, search); if (!end){ diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner_Tests.cpp index 0d2db0642b..eea9549166 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegPlanner_Tests.cpp @@ -329,18 +329,18 @@ UnitTestResult test_unreachable(){ } -struct LegPlannerInput{ - std::string name; - UnitTestResult (*test)(); -}; +struct LegPlannerTestCase{ + struct Input{ + std::string name; + UnitTestResult (*test)(); + }; -struct LegPlannerExpectation{ - std::string_view description; -}; + struct Expectation{ + std::string_view description; + }; -struct LegPlannerTestCase{ - LegPlannerInput input; - LegPlannerExpectation expectation; + Input input; + Expectation expectation; }; class Test_LegPlanner : public UnitTest{ diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegTypes.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegTypes.h index c1a7401c3b..c8b79dc4e6 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegTypes.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegTypes.h @@ -2,7 +2,8 @@ * * From: https://github.com/PokemonAutomation/ * - * Split out of Pokemon_LegWalker.h, unchanged; it includes this header. + * A leg of a route walk: what to walk (LegSpec) and what the walk found (LegResult). + * Pokemon_LegWalker.h builds on it. * */ diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegWalker.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegWalker.h index 31be625cb4..c00ecc7910 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegWalker.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegWalker.h @@ -68,8 +68,7 @@ LegResult walk_leg_with(const LegSpec& leg, const LegCalls& calls, size_t max_in // keeps whether it fired. A warp's fade can be over before the hold onto // the warp ends, so a wait for it that starts after the hold sees nothing: // the walk watches the hold with this, then waits on with the same watcher -// if it has not fired (FRLG 00.27, console B, 2026-11-09 01:41, the warp to -// CeruleanCity: the watch started 1.3 s into the hold and missed the fade). +// if it has not fired. class WatchAlong : public VisualInferenceCallback{ public: explicit WatchAlong(VisualInferenceCallback& watcher) diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegWalker_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegWalker_Tests.cpp index e676450cb0..10e2593ab0 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegWalker_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegWalker_Tests.cpp @@ -57,13 +57,13 @@ struct ScriptedLeg{ // An interrupt set off by step `step`, seen DELAY after it ends. HoldOutcome after_step(int step){ - return HoldOutcome{0, TILE * step + DELAY}; + return HoldOutcome{.interrupt = 0, .elapsed = TILE * step + DELAY}; } UnitTestResult test_hold_times(){ - const LegSpec open{Direction::EAST, 4, false, TILE}; - const LegSpec wall{Direction::EAST, 4, true, TILE}; + const LegSpec open{.direction = Direction::EAST, .tiles = 4, .to_wall = false, .tile_time = TILE}; + const LegSpec wall{.direction = Direction::EAST, .tiles = 4, .to_wall = true, .tile_time = TILE}; if (leg_hold(open, 4) != TILE * 3 + TILE / 2){ return std::format("Open leg: {}ms", leg_hold(open, 4).count()); } @@ -83,7 +83,7 @@ UnitTestResult test_hold_times(){ } UnitTestResult test_tiles_before_interrupt(){ - const LegSpec leg{Direction::EAST, 8, false, TILE}; + const LegSpec leg{.direction = Direction::EAST, .tiles = 8, .to_wall = false, .tile_time = TILE}; for (int step = 0; step <= 8; step++){ for (Milliseconds jitter : {-100ms, 0ms, 100ms}){ const int walked = tiles_before_interrupt(leg, TILE * step + DELAY + jitter, DELAY, 8); @@ -104,7 +104,7 @@ UnitTestResult test_tiles_before_interrupt(){ UnitTestResult test_wall_leg_walked_again(){ ScriptedLeg script; script.outcomes = {after_step(2)}; - const LegSpec leg{Direction::NORTH, 6, true, TILE}; + const LegSpec leg{.direction = Direction::NORTH, .tiles = 6, .to_wall = true, .tile_time = TILE}; const LegResult result = walk_leg_with(leg, script.calls(), 5); if (script.holds.size() != 2 || script.holds[1] != leg_hold(leg, 6)){ return "Expected the whole leg held again."; @@ -118,7 +118,7 @@ UnitTestResult test_wall_leg_walked_again(){ UnitTestResult test_open_leg_rest_walked(){ ScriptedLeg script; script.outcomes = {after_step(2)}; - const LegSpec leg{Direction::NORTH, 6, false, TILE}; + const LegSpec leg{.direction = Direction::NORTH, .tiles = 6, .to_wall = false, .tile_time = TILE}; const LegResult result = walk_leg_with(leg, script.calls(), 5); if (script.holds.size() != 2 || script.holds[1] != leg_hold(leg, 4)){ return "Expected the remaining 4 tiles held."; @@ -132,7 +132,7 @@ UnitTestResult test_open_leg_rest_walked(){ UnitTestResult test_open_leg_interrupted_at_end(){ ScriptedLeg script; script.outcomes = {after_step(6)}; - const LegSpec leg{Direction::NORTH, 6, false, TILE}; + const LegSpec leg{.direction = Direction::NORTH, .tiles = 6, .to_wall = false, .tile_time = TILE}; walk_leg_with(leg, script.calls(), 5); if (script.holds.size() != 1){ return "Nothing was left to walk after the last step."; @@ -145,7 +145,7 @@ UnitTestResult test_open_leg_interrupted_at_end(){ UnitTestResult test_open_leg_stops(){ ScriptedLeg script; script.outcomes = {after_step(2)}; - LegSpec leg{Direction::NORTH, 6, false, TILE}; + LegSpec leg{.direction = Direction::NORTH, .tiles = 6, .to_wall = false, .tile_time = TILE}; leg.stop_on_interrupt = true; const LegResult result = walk_leg_with(leg, script.calls(), 5); if (script.holds.size() != 1 || !result.stopped || result.exact || script.cleared.size() != 1){ @@ -157,12 +157,12 @@ UnitTestResult test_open_leg_stops(){ return true; } -// Console B, 2026-11-01, run 16: held south into a sign, the leg to a wall -// was interrupted by its message 600ms into every hold, six times. +// Held south into a sign: the leg to a wall is interrupted by the sign's +// message 600ms into every hold, again and again. UnitTestResult test_wall_leg_blocked(){ ScriptedLeg script; - script.outcomes = {HoldOutcome{0, 2200ms}, HoldOutcome{0, 600ms}, HoldOutcome{0, 600ms}}; - LegSpec leg{Direction::SOUTH, 23, true, TILE}; + script.outcomes = {HoldOutcome{.interrupt = 0, .elapsed = 2200ms}, HoldOutcome{.interrupt = 0, .elapsed = 600ms}, HoldOutcome{.interrupt = 0, .elapsed = 600ms}}; + LegSpec leg{.direction = Direction::SOUTH, .tiles = 23, .to_wall = true, .tile_time = TILE}; leg.stop_if_blocked = true; const LegResult result = walk_leg_with(leg, script.calls(), 5); if (script.holds.size() != 2 || !result.stopped){ @@ -176,7 +176,7 @@ UnitTestResult test_too_many_interruptions(){ for (int c = 0; c < 4; c++){ script.outcomes.push_back(after_step(0)); } - const LegSpec leg{Direction::NORTH, 6, true, TILE}; + const LegSpec leg{.direction = Direction::NORTH, .tiles = 6, .to_wall = true, .tile_time = TILE}; try{ walk_leg_with(leg, script.calls(), 3); }catch (std::runtime_error&){ @@ -224,18 +224,18 @@ UnitTestResult test_watch_along(){ } -struct LegWalkerInput{ - std::string name; - UnitTestResult (*test)(); -}; +struct LegWalkerTestCase{ + struct Input{ + std::string name; + UnitTestResult (*test)(); + }; -struct LegWalkerExpectation{ - std::string_view description; -}; + struct Expectation{ + std::string_view description; + }; -struct LegWalkerTestCase{ - LegWalkerInput input; - LegWalkerExpectation expectation; + Input input; + Expectation expectation; }; class Test_LegWalker : public UnitTest{ diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData_Tests.cpp index 25c3ecfcb8..fcf299992e 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData_Tests.cpp @@ -401,18 +401,18 @@ UnitTestResult test_built_in_maps(){ return true; } -struct MapDataTestInput{ - std::string name; - UnitTestResult (*test)(); -}; +struct MapDataTestCase{ + struct Input{ + std::string name; + UnitTestResult (*test)(); + }; -struct MapDataTestExpectation{ - std::string_view description; -}; + struct Expectation{ + std::string_view description; + }; -struct MapDataTestCase{ - MapDataTestInput input; - MapDataTestExpectation expectation; + Input input; + Expectation expectation; }; class Test_MapData : public UnitTest{ diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapFeatures.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapFeatures.h index e7af9fd8be..b39d50bbf7 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapFeatures.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapFeatures.h @@ -2,7 +2,7 @@ * * From: https://github.com/PokemonAutomation/ * - * Split out of Pokemon_MapData.h, unchanged; it includes this header. + * The features a map's tiles can carry. Pokemon_MapData.h builds on it. * */ diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapTiles.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapTiles.h index 6f3cf1373e..63ebf79735 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapTiles.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapTiles.h @@ -2,7 +2,7 @@ * * From: https://github.com/PokemonAutomation/ * - * Split out of Pokemon_MapData.h, unchanged; it includes this header. + * A map's tile grid. Pokemon_MapData.h builds on it. * */ diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MoveMechanics_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MoveMechanics_Tests.cpp index c0de39e0f5..fced1d74eb 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MoveMechanics_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MoveMechanics_Tests.cpp @@ -18,18 +18,18 @@ namespace Pokemon{ namespace{ -struct MoveMechanicsInput{ - std::string_view game; - MoveMechanics mechanics; -}; -struct MoveMechanicsExpectation{ - // How the walker presses an open leg on foot (the user's pick, - // 2026-11-11: BDSP taps, FRLG timed holds). - OpenLegPress open_legs_on_foot; -}; struct MoveMechanicsCase{ - MoveMechanicsInput input; - MoveMechanicsExpectation expectation; + struct Input{ + std::string_view game; + MoveMechanics mechanics; + }; + struct Expectation{ + // How the walker presses an open leg on foot (BDSP taps, FRLG + // timed holds). + OpenLegPress open_legs_on_foot; + }; + Input input; + Expectation expectation; }; // Why `faster` is not faster than `slower` on both axes, if it is not. diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLog.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLog.h index 17cdaef020..4a907f056d 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLog.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLog.h @@ -4,8 +4,7 @@ * * The log lines a route walk writes, shared by the games' walkers so that * every game's log reads the same and devtools/maps can draw any run's - * routes from it. These are BDSP AutoStory's route lines, lifted from - * PokemonBDSP_AutoStory_RouteWalk.cpp, with what the other games need: + * routes from it. These are BDSP AutoStory's route lines, with what the other games need: * * [Route] To the grass: plan from Route 202 (15, 25), Route 202 (16, 25): east 15 (to wall) to Route 202 (23, 25), Route 202 (30, 26); north 5 (to wall) to Route 202 (23, 24) * [Route] Leg 1/2: east 15 (to wall) to Route 202 (23, 25), Route 202 (30, 26) diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLogLegs.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLogLegs.h index bfe6778a8e..c1da9ce723 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLogLegs.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLogLegs.h @@ -2,7 +2,7 @@ * * From: https://github.com/PokemonAutomation/ * - * Split out of Pokemon_RouteLog.h, unchanged; it includes this header. + * The legs of a route as its log lines name them. Pokemon_RouteLog.h builds on it. * */ diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLogTypes.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLogTypes.h index c98a70d243..cbdd909b62 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLogTypes.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLogTypes.h @@ -2,7 +2,7 @@ * * From: https://github.com/PokemonAutomation/ * - * Split out of Pokemon_RouteLog.h, unchanged; it includes this header. + * The types of a route log's lines. Pokemon_RouteLog.h builds on it. * */ diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLog_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLog_Tests.cpp index 2039c5c62e..a024bcaa41 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLog_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLog_Tests.cpp @@ -23,16 +23,16 @@ namespace Pokemon{ namespace{ -struct RouteLogInput{ - std::string name; - std::string line; -}; -struct RouteLogExpectation{ - std::string expected; -}; struct RouteLogTestCase{ - RouteLogInput input; - RouteLogExpectation expectation; + struct Input{ + std::string name; + std::string line; + }; + struct Expectation{ + std::string expected; + }; + Input input; + Expectation expectation; }; class Test_RouteLog : public UnitTest{ diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RuntimeMap_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RuntimeMap_Tests.cpp index 343416ca4d..81a28b60e1 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RuntimeMap_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RuntimeMap_Tests.cpp @@ -3,14 +3,15 @@ * From: https://github.com/PokemonAutomation/ */ #include -#include #include #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/Json/JsonArray.h" #include "Common/Cpp/Json/JsonObject.h" #include "Pokemon_MapData.h" #include "Pokemon_RuntimeMap.h" -#ifndef PA_RUNTIME_MAP_STANDALONE +#ifdef PA_RUNTIME_MAP_STANDALONE +#include +#else #include "Pokemon_RuntimeMap_Tests.h" #endif @@ -18,19 +19,19 @@ namespace PokemonAutomation{ namespace Pokemon{ namespace{ -struct RuntimeMapInput{ - std::string_view name; - std::string_view fields; -}; - -struct RuntimeMapExpectation{ - std::string_view error = {}; - std::string_view normalized_fields = {}; -}; - struct RuntimeMapCase{ - RuntimeMapInput input; - RuntimeMapExpectation expectation; + struct Input{ + std::string_view name; + std::string_view fields; + }; + + struct Expectation{ + std::string_view error = {}; + std::string_view normalized_fields = {}; + }; + + Input input; + Expectation expectation; }; const std::string GOLDEN_V2 = R"( diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_WallRecovery_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_WallRecovery_Tests.cpp index f563a0f9aa..e3a68cfaa6 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_WallRecovery_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_WallRecovery_Tests.cpp @@ -69,21 +69,21 @@ std::string describe_holds(std::span holds){ } // Inputs and expectations are separate aggregates; each registered test owns one row. -struct CorridorInput{ - std::string name; - std::vector positions; - std::optional person; - size_t budget; -}; +struct CorridorTestCase{ + struct Input{ + std::string name; + std::vector positions; + std::optional person; + size_t budget; + }; -struct CorridorExpectation{ - std::optional first_anchor; - std::vector holds; -}; + struct Expectation{ + std::optional first_anchor; + std::vector holds; + }; -struct CorridorTestCase{ - CorridorInput input; - CorridorExpectation expectation; + Input input; + Expectation expectation; }; class Test_WallRecovery : public UnitTest{ @@ -187,20 +187,20 @@ class CornerPolicy{ } }; -struct CornerInput{ - std::string name; - std::array positions; - size_t budget; -}; +struct CornerTestCase{ + struct Input{ + std::string name; + std::array positions; + size_t budget; + }; -struct CornerExpectation{ - std::optional first_anchor; - std::vector holds; -}; + struct Expectation{ + std::optional first_anchor; + std::vector holds; + }; -struct CornerTestCase{ - CornerInput input; - CornerExpectation expectation; + Input input; + Expectation expectation; }; class Test_WallRecoveryCorner : public UnitTest{ diff --git a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimFaults.cpp b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimFaults.cpp new file mode 100644 index 0000000000..6d0bf64fb4 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimFaults.cpp @@ -0,0 +1,94 @@ +/* Sim Faults + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Pokemon_SimFaults.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +const std::vector& fault_classes(){ + static const std::vector classes{ + FaultClass::SCENE_END_POSE, + FaultClass::SCENE_EARLY, + FaultClass::BATTLE_OPENING, + FaultClass::WILD_BATTLES, + FaultClass::LATE_FADE, + FaultClass::SLOW_FADE_IN, + FaultClass::WARP_NO_OP, + FaultClass::DARK_CAVE, + FaultClass::LOST_TAP, + FaultClass::UNSEEN_STEP, + }; + return classes; +} + +std::string_view fault_class_name(FaultClass fault){ + switch (fault){ + case FaultClass::SCENE_END_POSE: + return "scene end pose"; + case FaultClass::SCENE_EARLY: + return "scene early"; + case FaultClass::BATTLE_OPENING: + return "battle opening"; + case FaultClass::WILD_BATTLES: + return "wild battles"; + case FaultClass::LATE_FADE: + return "late fade"; + case FaultClass::SLOW_FADE_IN: + return "slow fade-in"; + case FaultClass::WARP_NO_OP: + return "warp no-op"; + case FaultClass::DARK_CAVE: + return "dark cave"; + case FaultClass::LOST_TAP: + return "lost tap"; + case FaultClass::UNSEEN_STEP: + return "unseen step"; + } + return "?"; +} + + +FaultOccurrences::FaultOccurrences(std::set faulted) + : m_numbers(std::move(faulted)) +{} + +bool FaultOccurrences::next(){ + m_chances++; + if (!m_numbers.contains(m_chances)){ + return false; + } + m_faulted++; + return true; +} + + +SimFaults::SimFaults(std::map> faulted){ + for (auto& [fault, numbers] : faulted){ + m_classes.emplace(fault, FaultOccurrences(std::move(numbers))); + } +} + +bool SimFaults::next(FaultClass fault){ + return m_classes[fault].next(); +} + +int SimFaults::chances(FaultClass fault) const{ + const auto iter = m_classes.find(fault); + return iter == m_classes.end() ? 0 : iter->second.chances(); +} + +int SimFaults::faulted(FaultClass fault) const{ + const auto iter = m_classes.find(fault); + return iter == m_classes.end() ? 0 : iter->second.faulted(); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimFaults.h b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimFaults.h new file mode 100644 index 0000000000..f58b09b854 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimFaults.h @@ -0,0 +1,109 @@ +/* Sim Faults + * + * From: https://github.com/PokemonAutomation/ + * + * The faults the games' overworld simulations inject, shared by BDSP's + * and FRLG's: the classes of failure the consoles showed, and a counter + * per class that says which of a fault's chances to fault. A chance is + * each time the sim reaches a point where the game could fail that way + * (a tap, a warp, a step's end); a test names the chances it faults by + * number, from 1 in the order the sim reaches them. + * + * How a class is injected is each game's (its sim's knobs and its fault + * sweep); a game may inject only some of the classes. + * + */ + +#ifndef PokemonAutomation_Pokemon_SimFaults_H +#define PokemonAutomation_Pokemon_SimFaults_H + +#include +#include +#include +#include + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// A class of failure a console showed, which a sim can inject. +enum class FaultClass{ + // A step whose end the game sets (a talk, a scene, a battle) leaves the + // player on another walkable tile next to the expected one. + SCENE_END_POSE, + // A scene starts on the last tiles of the walk before it. + SCENE_EARLY, + // A battle's opening is still playing when a step starts. + BATTLE_OPENING, + // Wild battles while walking, more than a clean run meets. + WILD_BATTLES, + // A warp's fade shows late. + LATE_FADE, + // Every warp's fade-in runs long: a move right after it goes nowhere. + SLOW_FADE_IN, + // A step onto a warp shows the fade's black but does not warp. + WARP_NO_OP, + // A dark cave walked without Flash. + DARK_CAVE, + // A tap the game drops. + LOST_TAP, + // A step tap that moved the player, read as not moved. + UNSEEN_STEP, +}; + +// Every fault class, in the enum's order. +const std::vector& fault_classes(); + +// The class's name for logs: "scene end pose". +std::string_view fault_class_name(FaultClass fault); + + +// Which chances of one fault to fault: those numbered in `faulted`, +// counted from 1 as next() reaches them. +class FaultOccurrences{ +public: + FaultOccurrences() = default; + explicit FaultOccurrences(std::set faulted); + + // Counts one more chance: whether it is one to fault. + bool next(); + + // The chances counted so far. + int chances() const{ + return m_chances; + } + // How many of them were faulted. + int faulted() const{ + return m_faulted; + } + +private: + std::set m_numbers; + int m_chances = 0; + int m_faulted = 0; +}; + + +// A FaultOccurrences per fault class: a class not given counts its +// chances and faults none. +class SimFaults{ +public: + SimFaults() = default; + explicit SimFaults(std::map> faulted); + + // Counts one more chance of `fault`: whether it is one to fault. + bool next(FaultClass fault); + + // The chances of `fault` counted so far, and how many were faulted. + int chances(FaultClass fault) const; + int faulted(FaultClass fault) const; + +private: + std::map m_classes; +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimFaults_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimFaults_Tests.cpp new file mode 100644 index 0000000000..73b884ff6f --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimFaults_Tests.cpp @@ -0,0 +1,182 @@ +/* Sim Faults Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The fault counters on their own: which chances a counter faults, what + * it counts, that each class counts apart, and the classes' names. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Pokemon_SimFaults.h" +#include "Pokemon_SimFaults_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +struct SimFaultsTestCase{ + struct Input{ + std::string name; + UnitTestResult (*body)(); + }; + + struct Expectation{ + std::string_view description; + }; + + Input input; + Expectation expectation; +}; + +class Test_SimFaults : public UnitTest{ +public: + explicit Test_SimFaults(SimFaultsTestCase scenario) + : UnitTest("Pokemon::Navigation - Sim faults: " + scenario.input.name) + , m_scenario(std::move(scenario)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return m_scenario.input.body(); + } + +private: + SimFaultsTestCase m_scenario; +}; + +// What `chances` chances of FaultClass::LOST_TAP did with `faulted` +// named, taken as a game's sim takes them, a chance of +// FaultClass::WARP_NO_OP (none named) after each. +struct Counted{ + std::vector faulted_at; + int chances = 0; + int faulted = 0; + int other_chances = 0; + int other_faulted = 0; + int never_reached = 0; +}; + +Counted count(const std::set& faulted, int chances){ + SimFaults faults({{FaultClass::LOST_TAP, faulted}}); + Counted out; + for (int chance = 1; chance <= chances; chance++){ + if (faults.next(FaultClass::LOST_TAP)){ + out.faulted_at.push_back(chance); + } + faults.next(FaultClass::WARP_NO_OP); + } + out.chances = faults.chances(FaultClass::LOST_TAP); + out.faulted = faults.faulted(FaultClass::LOST_TAP); + out.other_chances = faults.chances(FaultClass::WARP_NO_OP); + out.other_faulted = faults.faulted(FaultClass::WARP_NO_OP); + out.never_reached = faults.chances(FaultClass::DARK_CAVE); + return out; +} + +// "[1, 3]". +std::string numbers_text(const std::vector& numbers){ + std::string out; + for (int number : numbers){ + if (!out.empty()){ + out += ", "; + } + out += std::format("{}", number); + } + return std::format("[{}]", out); +} + +UnitTestResult expect_counted(const std::set& faulted, int chances, const std::vector& faulted_at){ + const Counted counted = count(faulted, chances); + if (counted.faulted_at != faulted_at){ + return std::format("Faulted chances {}, expected {}.", numbers_text(counted.faulted_at), numbers_text(faulted_at)); + } + if (counted.chances != chances || counted.faulted != static_cast(faulted_at.size())){ + return std::format("Counted {} chances, {} faulted; expected {}, {}.", counted.chances, counted.faulted, chances, faulted_at.size()); + } + if (counted.other_chances != chances || counted.other_faulted != 0){ + return std::format("The other class counted {} chances, {} faulted; expected {}, 0.", counted.other_chances, counted.other_faulted, chances); + } + if (counted.never_reached != 0){ + return std::format("A class never reached counted {} chances.", counted.never_reached); + } + return true; +} + +UnitTestResult test_none_named(){ + return expect_counted({}, 3, {}); +} + +UnitTestResult test_named_from_one(){ + return expect_counted({1, 3}, 4, {1, 3}); +} + +UnitTestResult test_never_reached(){ + return expect_counted({2, 9}, 3, {2}); +} + +UnitTestResult test_no_chances(){ + return expect_counted({1}, 0, {}); +} + +UnitTestResult test_class_names(){ + std::map seen; + for (FaultClass fault : fault_classes()){ + const std::string_view name = fault_class_name(fault); + if (name == "?" || !seen.emplace(name, fault).second){ + return std::format("Fault class {} has no name of its own: {}", static_cast(fault), name); + } + } + return true; +} + +// fault_classes() restates the enum, so it must list every value: the +// names stop at the first value past the enum's last (fault_class_name() +// is a switch, so a value with no name fails to build). +UnitTestResult test_classes_match_enum(){ + const std::vector& classes = fault_classes(); + size_t named = 0; + while (fault_class_name(static_cast(named)) != "?"){ + named++; + } + if (named != classes.size()){ + return std::format("The enum has {} classes, fault_classes() lists {}.", named, classes.size()); + } + for (size_t c = 0; c < classes.size(); c++){ + if (classes[c] != static_cast(c)){ + return std::format("fault_classes() lists {} at position {}, not in the enum's order.", static_cast(classes[c]), c); + } + } + return true; +} + +const SimFaultsTestCase SIM_FAULTS_CASES[] = { + {.input = {.name = "none named faults none", .body = test_none_named}, .expectation = {.description = "3 chances, none faulted"}}, + {.input = {.name = "the named chances are faulted, from 1", .body = test_named_from_one}, .expectation = {.description = "1 and 3 of 4 faulted"}}, + {.input = {.name = "a chance never reached is not faulted", .body = test_never_reached}, .expectation = {.description = "2 of 3 faulted, 9 never reached"}}, + {.input = {.name = "no chances, nothing counted", .body = test_no_chances}, .expectation = {.description = "0 chances, none faulted"}}, + {.input = {.name = "each class has its own name", .body = test_class_names}, .expectation = {.description = "no two classes share a name"}}, + {.input = {.name = "fault_classes() lists every class in order", .body = test_classes_match_enum}, .expectation = {.description = "the list is the enum's values, in order"}}, +}; + +} // namespace + + +void add_tests_SimFaults(UnitTestDatabase& database){ + for (const SimFaultsTestCase& scenario : SIM_FAULTS_CASES){ + database.add(scenario); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimFaults_Tests.h b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimFaults_Tests.h new file mode 100644 index 0000000000..000b660a23 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimFaults_Tests.h @@ -0,0 +1,23 @@ +/* Sim Faults Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_SimFaults_Tests_H +#define PokemonAutomation_Pokemon_SimFaults_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers the sim fault counter tests. +void add_tests_SimFaults(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.cpp b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.cpp new file mode 100644 index 0000000000..0ab221c99e --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.cpp @@ -0,0 +1,132 @@ +/* Sim Golden + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/Filesystem/FileIO.h" +#include "Pokemon_SimGolden.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +// Line `begin` of `text` up to its "\n", without a "\r" at its end. +std::string_view line_at(std::string_view text, size_t begin){ + if (begin >= text.size()){ + return {}; + } + const size_t end = std::min(text.find('\n', begin), text.size()); + std::string_view line = text.substr(begin, end - begin); + if (line.ends_with('\r')){ + line.remove_suffix(1); + } + return line; +} + +// Where the line after the one at `begin` starts; past the end if none. +size_t next_line(std::string_view text, size_t begin){ + if (begin >= text.size()){ + return text.size() + 1; + } + const size_t end = text.find('\n', begin); + return end == std::string_view::npos ? text.size() + 1 : end + 1; +} + +} // namespace + + +std::string with_crlf(std::string_view text){ + std::string out; + out.reserve(text.size() + text.size() / 16); + for (char ch : text){ + if (ch == '\r'){ + continue; + } + if (ch == '\n'){ + out += '\r'; + } + out += ch; + } + return out; +} + +bool sim_golden_update_requested(){ + // code-health: allow char_pointer -- std::getenv returns a const char pointer + const char* value = std::getenv(std::string(SIM_GOLDEN_UPDATE_VARIABLE).c_str()); + return value != nullptr && value[0] != '\0' && std::string_view(value) != "0"; +} + +std::string sim_golden_path(std::string_view source_file, std::string_view name){ + const size_t slash = source_file.find_last_of("/\\"); + std::string out = slash == std::string_view::npos ? std::string() : std::string(source_file.substr(0, slash + 1)); + out += name; + return out; +} + +std::string sim_golden_text(std::string_view about, const std::vector& lines){ + std::string out = "# "; + out += about; + out += "\n"; + for (const std::string& line : lines){ + out += line; + out += "\n"; + } + return out; +} + +std::optional first_golden_difference(std::string_view golden, std::string_view actual){ + size_t a = 0; + size_t b = 0; + for (size_t line = 1; a < golden.size() || b < actual.size(); line++){ + const std::string_view golden_line = line_at(golden, a); + const std::string_view actual_line = line_at(actual, b); + const bool golden_ended = a >= golden.size(); + const bool actual_ended = b >= actual.size(); + if (golden_line != actual_line || golden_ended != actual_ended){ + return std::format( + "line {}:\n--- golden: {}\n+++ actual: {}", line, + golden_ended ? "(end of file)" : golden_line, + actual_ended ? "(end of file)" : actual_line + ); + } + a = next_line(golden, a); + b = next_line(actual, b); + } + return std::nullopt; +} + +UnitTestResult check_sim_golden(Logger& logger, const std::string& path, const std::string& text){ + if (sim_golden_update_requested()){ + const std::string crlf = with_crlf(text); + FileIO file; + if (!file.open(path, FileMode::WRITE | FileMode::BINARY) || file.write(crlf) != crlf.size()){ + return "Could not write " + path + "."; + } + logger.log("Wrote " + path + "."); + return true; + } + std::string golden; + if (!file_to_string(path, golden)){ + return std::format("No golden at {}: run with {}=1 to write it.", path, SIM_GOLDEN_UPDATE_VARIABLE); + } + const std::optional difference = first_golden_difference(golden, text); + if (difference.has_value()){ + return std::format( + "{} differs from the simulation at {}\nRun with {}=1 to rewrite it if the change is meant.", + path, *difference, SIM_GOLDEN_UPDATE_VARIABLE + ); + } + return true; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.h b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.h new file mode 100644 index 0000000000..23486fe72e --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden.h @@ -0,0 +1,63 @@ +/* Sim Golden + * + * From: https://github.com/PokemonAutomation/ + * + * Golden files for the games' overworld simulations: what a simulation + * observably does (the legs it walks, where steps end, the events it + * shows, how injected faults turn out), written as text a line per fact + * and checked in beside the test that writes it. A refactor of a + * simulation that should change nothing must leave its golden as it is; + * a change that should, shows as the golden's diff in review. + * + * A golden test builds its text and hands it to check_sim_golden(). With + * SIM_GOLDEN_UPDATE_VARIABLE set to anything but "" or "0", the golden is + * rewritten instead, with CRLF line endings as the source tree keeps. + * + */ + +#ifndef PokemonAutomation_Pokemon_SimGolden_H +#define PokemonAutomation_Pokemon_SimGolden_H + +#include +#include +#include +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "Common/Cpp/TestRunners/UnitTest.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Set it to rewrite the golden files instead of checking them. +inline constexpr std::string_view SIM_GOLDEN_UPDATE_VARIABLE = "PA_UPDATE_SIM_GOLDENS"; + +// Whether SIM_GOLDEN_UPDATE_VARIABLE asks for the goldens to be rewritten. +bool sim_golden_update_requested(); + +// The file `name` in the directory of `source_file`, a test's __FILE__. +std::string sim_golden_path(std::string_view source_file, std::string_view name); + +// The golden text: `about` as a "# " comment line, then `lines`, each +// ended by "\n". +std::string sim_golden_text(std::string_view about, const std::vector& lines); + +// `text` with every "\n" made "\r\n" and no "\r" of its own: the line +// endings the goldens are written with. +std::string with_crlf(std::string_view text); + +// The first line where `golden` and `actual` differ, carriage returns +// ignored: "line N:\n--- golden: ...\n+++ actual: ...". None if they are +// the same. +std::optional first_golden_difference(std::string_view golden, std::string_view actual); + +// Checks `text` against the golden file at `path`, or rewrites the file +// with it when SIM_GOLDEN_UPDATE_VARIABLE is set. Fails with the first +// difference, or with how to write a golden that is missing. +UnitTestResult check_sim_golden(Logger& logger, const std::string& path, const std::string& text); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.cpp new file mode 100644 index 0000000000..552dcd42cb --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.cpp @@ -0,0 +1,151 @@ +/* Sim Golden Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The golden file plumbing on its own: paths beside a test, the text, and + * the first difference a check reports. + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Pokemon_SimGolden.h" +#include "Pokemon_SimGolden_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +struct SimGoldenTestCase{ + struct Input{ + std::string name; + UnitTestResult (*body)(Logger& logger); + }; + + struct Expectation{ + std::string_view description; + }; + + Input input; + Expectation expectation; +}; + +class Test_SimGolden : public UnitTest{ +public: + explicit Test_SimGolden(SimGoldenTestCase scenario) + : UnitTest("Pokemon::Navigation - Sim golden: " + scenario.input.name) + , m_scenario(std::move(scenario)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + return m_scenario.input.body(logger); + } + +private: + SimGoldenTestCase m_scenario; +}; + +UnitTestResult expect_equal(std::string_view what, std::string_view actual, std::string_view expected){ + if (actual == expected){ + return true; + } + return std::string(what) + ": expected \"" + std::string(expected) + "\", got \"" + std::string(actual) + "\"."; +} + +// The difference as text for expect_equal(): "(none)" when the texts match. +std::string difference_of(std::string_view golden, std::string_view actual){ + return first_golden_difference(golden, actual).value_or("(none)"); +} + +UnitTestResult test_path_beside_source(Logger&){ + UnitTestResult result = expect_equal("posix", sim_golden_path("/a/b/Test.cpp", "x.golden.txt"), "/a/b/x.golden.txt"); + if (result.result != UnitTestResult::PASSED){ + return result; + } + result = expect_equal("windows", sim_golden_path("C:\\a\\Test.cpp", "x.txt"), "C:\\a\\x.txt"); + if (result.result != UnitTestResult::PASSED){ + return result; + } + return expect_equal("no directory", sim_golden_path("Test.cpp", "x.txt"), "x.txt"); +} + +UnitTestResult test_text(Logger&){ + return expect_equal("text", sim_golden_text("about", {"a", "b"}), "# about\na\nb\n"); +} + +UnitTestResult test_same_text_ignoring_crlf(Logger&){ + return expect_equal("difference", difference_of("# x\r\na\r\nb\r\n", "# x\na\nb\n"), "(none)"); +} + +UnitTestResult test_first_differing_line(Logger&){ + return expect_equal( + "difference", difference_of("# x\r\na\r\nb\r\n", "# x\na\nc\n"), + "line 3:\n--- golden: b\n+++ actual: c" + ); +} + +UnitTestResult test_extra_line(Logger&){ + UnitTestResult result = expect_equal( + "longer actual", difference_of("a\n", "a\nb\n"), + "line 2:\n--- golden: (end of file)\n+++ actual: b" + ); + if (result.result != UnitTestResult::PASSED){ + return result; + } + return expect_equal( + "shorter actual", difference_of("a\nb\n", "a\n"), + "line 2:\n--- golden: b\n+++ actual: (end of file)" + ); +} + +UnitTestResult test_with_crlf(Logger&){ + UnitTestResult result = expect_equal("bare LF", with_crlf("a\nb\n"), "a\r\nb\r\n"); + if (result.result != UnitTestResult::PASSED){ + return result; + } + result = expect_equal("CRLF", with_crlf("a\r\nb\r\n"), "a\r\nb\r\n"); + if (result.result != UnitTestResult::PASSED){ + return result; + } + return expect_equal("mixed and a stray CR", with_crlf("a\r\nb\rc\n"), "a\r\nbc\r\n"); +} + +UnitTestResult test_missing_golden_fails(Logger& logger){ + if (sim_golden_update_requested()){ + return UnitTestResult(UnitTestResult::SKIPPED, "the goldens are being rewritten"); + } + const UnitTestResult result = check_sim_golden(logger, sim_golden_path(__FILE__, "no_such.golden.txt"), "x\n"); + if (result.result == UnitTestResult::PASSED){ + return "Expected a missing golden to fail the check."; + } + return true; +} + +const SimGoldenTestCase SIM_GOLDEN_CASES[] = { + {.input = {.name = "the golden's path is beside the test's source", .body = test_path_beside_source}, .expectation = {}}, + {.input = {.name = "the text is a comment line, then a line each", .body = test_text}, .expectation = {}}, + {.input = {.name = "CRLF and LF text compare the same", .body = test_same_text_ignoring_crlf}, .expectation = {}}, + {.input = {.name = "the first differing line is reported", .body = test_first_differing_line}, .expectation = {}}, + {.input = {.name = "a line more or less is reported", .body = test_extra_line}, .expectation = {}}, + {.input = {.name = "goldens are written with exactly one CRLF a line", .body = test_with_crlf}, .expectation = {}}, + {.input = {.name = "a missing golden fails", .body = test_missing_golden_fails}, .expectation = {}}, +}; + +} // namespace + + +void add_tests_SimGolden(UnitTestDatabase& database){ + for (const SimGoldenTestCase& scenario : SIM_GOLDEN_CASES){ + database.add(scenario); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.h b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.h new file mode 100644 index 0000000000..3119fb8b6a --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Simulation/Pokemon_SimGolden_Tests.h @@ -0,0 +1,23 @@ +/* Sim Golden Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_SimGolden_Tests_H +#define PokemonAutomation_Pokemon_SimGolden_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers the sim golden file tests. +void add_tests_SimGolden(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRangeOptions_Tests.cpp b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRangeOptions_Tests.cpp index 4cd40b9e7e..a0f621aafa 100644 --- a/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRangeOptions_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRangeOptions_Tests.cpp @@ -47,25 +47,25 @@ const StringSelectDatabase& main_story_database(){ return database; } -// The start and end set by slug, as saved settings load them; empty -// leaves the default. -struct StoryRangeInput{ - std::string name; - std::string start_slug; - std::string end_slug; -}; - -// The indexes in the dropdown, and whether the range is valid. -struct StoryRangeExpectation{ - size_t start; - size_t end; - bool valid; -}; - // A range chosen in the main story's dropdowns and what the group reads from it. struct StoryRangeCase{ - StoryRangeInput input; - StoryRangeExpectation expectation; + // The start and end set by slug, as saved settings load them; empty + // leaves the default. + struct Input{ + std::string name; + std::string start_slug; + std::string end_slug; + }; + + // The indexes in the dropdown, and whether the range is valid. + struct Expectation{ + size_t start; + size_t end; + bool valid; + }; + + Input input; + Expectation expectation; }; class Test_StoryRange : public UnitTest{ @@ -83,7 +83,7 @@ class Test_StoryRange : public UnitTest{ if (!m_case.input.end_slug.empty()){ range.END_CHECKPOINT.set_by_slug(m_case.input.end_slug); } - const StoryRangeExpectation& expected = m_case.expectation; + const StoryRangeCase::Expectation& expected = m_case.expectation; if (range.start_index() != expected.start || range.end_index() != expected.end){ return std::format( "Expected indexes {} to {}, got {} to {}", diff --git a/SerialPrograms/Source/Pokemon/Pokemon_AdvRng.cpp b/SerialPrograms/Source/Pokemon/Pokemon_AdvRng.cpp index aef20552e7..4f888bd9d1 100644 --- a/SerialPrograms/Source/Pokemon/Pokemon_AdvRng.cpp +++ b/SerialPrograms/Source/Pokemon/Pokemon_AdvRng.cpp @@ -270,6 +270,18 @@ std::string base_species_slug(const std::string& species){ return species; } +bool species_matches(const std::string& observed, const std::string& candidate){ + const std::string base = base_species_slug(observed); + if (base != base_species_slug(candidate)){ + return false; + } + // Accept matches with the base slug in case form couldn't be read + if (observed == base || candidate == base){ + return true; + } + return observed == candidate; +} + int slot_to_unownform(AdvEncounterSlot slot){ if (slot.species.find("unown") == std::string::npos){ return -1; @@ -521,7 +533,7 @@ bool check_for_match(AdvPokemonResult res, AdvRngFilters target, int16_t gender_ } bool check_for_match(AdvWildPokemonResult res, AdvRngFilters target, int16_t gender_threshold, uint16_t tid_xor_sid){ - return (base_species_slug(target.species) == base_species_slug(res.species)) + return species_matches(target.species, res.species) && (target.level == res.level) && (target.nature == AdvNature::Any || (res.nature == target.nature)) && (target.ability == AdvAbility::Any || (res.ability == target.ability)) diff --git a/SerialPrograms/Source/Pokemon/Pokemon_AdvRng.h b/SerialPrograms/Source/Pokemon/Pokemon_AdvRng.h index 85a3ad8707..616063c46d 100644 --- a/SerialPrograms/Source/Pokemon/Pokemon_AdvRng.h +++ b/SerialPrograms/Source/Pokemon/Pokemon_AdvRng.h @@ -202,11 +202,14 @@ Pokemon::NatureAdjustments nature_to_adjustment(AdvNature nature); Pokemon::AdvNature string_to_nature(const std::string& nature_string); std::string nature_to_string(const AdvNature& nature); -// Forms that cannot be told apart from an observation (e.g. Unown letters) share a base slug. -// Encounter slots carry the form-specific slug, while an observed species only ever carries -// the base slug, so both sides must be folded before they are compared. +// Forms of the same species (e.g. Unown letters) share a base slug. +// Encounter slots carry the form-specific slug (e.g. "unown-b"). std::string base_species_slug(const std::string& species); +// Return true if an observed species is consistent with a candidate species. +// Species with forms match by base slug, unless both sides name a specific form +bool species_matches(const std::string& observed, const std::string& candidate); + std::string gender_to_string(const AdvGender& gender); AdvGender gender_from_gender_value(uint8_t gender_value, int16_t threshold); diff --git a/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp index c51b846fd4..4b6a4ff384 100644 --- a/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp @@ -12,6 +12,7 @@ #include "Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_FaultSweep_Tests.h" #include "Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_SimFailureCases_Tests.h" #include "Programs/AutoStory/PokemonBDSP_AutoStory_RecoveryWalk_Tests.h" +#include "Programs/AutoStory/PokemonBDSP_AutoStory_Launch_Tests.h" #include "Programs/AutoStory/PokemonBDSP_AutoStory_Maps_Tests.h" #include "Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.h" #include "Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.h" @@ -40,6 +41,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_DialogDetector(database); add_tests_MenuDetector(database); add_tests_AutoStory_Actions(database); + add_tests_AutoStory_Launch(database); add_tests_AutoStory_Maps(database); add_tests_AutoStory_StoryWorld(database); add_tests_AutoStory_RoutePlanner(database); diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.cpp index 40fa26813b..ee112c5be7 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.cpp @@ -26,8 +26,8 @@ const StoryEvents& story_events(){ } StoryEvent console_event(std::function action){ - return [action = std::move(action)](StoryExecutor& executor, Pokemon::RunState&, const Pokemon::StoryStep&){ - action(executor.stream(), executor.context()); + return [action = std::move(action)](StoryEnv& console, Pokemon::RunState&, const Pokemon::StoryStep&){ + action(console.env.console, console.context); return Pokemon::outcome_ok(); }; } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.h index a39c9738d9..d8243f3f72 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.h @@ -4,14 +4,14 @@ * From: https://github.com/PokemonAutomation/ * * The story's steps that only BDSP has, by name: a step of the table whose - * variant names one of these runs it (StoryExecutor::run()). Some are the + * variant names one of these runs it (story_game()). Some are the * scripted scenes (the Azure Flute, the lake's briefcase, the starter, * Dawn's catching tutorial and the catch); the rest do what the shared * steps cannot, as they ran on the console: the Poke Mart lady's Potions * and the stairs into Jubilife (00.04c, 00.04d), a clown who wanders * (00.05b), the Gym's tries from the Pokemon Center (01.04) and Rock Smash * (01.09). Each fails by throwing, with a screenshot; the executor turns - * that into a failed step. + * that into a failed step (TableExecutor::run()). * * They are in files by area: _Events_Lake (00.00 to 00.04), _Sandgem * (00.04c, 00.04d), _Jubilife (00.05b), _Oreburgh (01.04) and _Route204 @@ -25,7 +25,7 @@ #include #include #include -#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryCheckpoint.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -35,7 +35,7 @@ namespace PokemonBDSP{ // Runs a step on the console. Returns where it left the player when it // knows; throws OperationFailedException when it fails. using StoryEvent = std::function; // Map of story event names to their execution functions. using StoryEvents = std::map; diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Jubilife.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Jubilife.cpp index 86413db20a..a05d0b5471 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Jubilife.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Jubilife.cpp @@ -55,9 +55,9 @@ void step_along(VideoStream& stream, ProControllerContext& context, int east, co route_turn(stream, context, east > 0 ? Direction::EAST : Direction::WEST); walk_tiles_dpad(stream, context, run_from_wild_battle, {east > 0 ? 1.0 : -1.0, 0}, 1, label); } -Pokemon::Outcome talk_to_wanderer(StoryExecutor& executor, Pokemon::RunState&, const Pokemon::StoryStep& step){ - VideoStream& stream = executor.stream(); - ProControllerContext& context = executor.context(); +Pokemon::Outcome talk_to_wanderer(StoryEnv& console, Pokemon::RunState&, const Pokemon::StoryStep& step){ + VideoStream& stream = console.env.console; + ProControllerContext& context = console.context; const std::string label = "Along the row below " + step.name; // Home, east, west, west, east: each move one tile on the D-pad. const int moves[] = {0, 1, -1, -1, 1}; diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Lake.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Lake.cpp index 0c7841c7fe..e7328cb336 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Lake.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events_Lake.cpp @@ -62,11 +62,11 @@ void starter_briefcase(VideoStream& stream, ProControllerContext& context){ // 00.02: the starter from the program's option, in the briefcase: Turtwig // is first, Chimchar a press right, Piplup two; ZL picks, then Up and ZL // answer "this one?" with Yes. -Pokemon::Outcome choose_starter(StoryExecutor& executor, Pokemon::RunState&, const Pokemon::StoryStep&){ - VideoStream& stream = executor.stream(); - ProControllerContext& context = executor.context(); +Pokemon::Outcome choose_starter(StoryEnv& console, Pokemon::RunState&, const Pokemon::StoryStep&){ + VideoStream& stream = console.env.console; + ProControllerContext& context = console.context; size_t rights = 0; - switch (executor.env().starter){ + switch (console.starter){ case StarterChoice::TURTWIG: rights = 0; break; diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.cpp index e24634f69d..9598f6651d 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_NewGame.cpp @@ -17,6 +17,7 @@ #include "NintendoSwitch/Inference/NintendoSwitch_HomeMenuDetector.h" #include "NintendoSwitch/Inference/NintendoSwitch_StartGameUserSelectDetector.h" #include "NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h" +#include "NintendoSwitch/Programs/FastCodeEntry/NintendoSwitch_SystemKeyboardTyping.h" #include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" #include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" #include "PokemonBDSP/Inference/PokemonBDSP_MenuDetector.h" @@ -188,7 +189,7 @@ void enter_player_name(VideoStream& stream, ProControllerContext& context, StepT context.wait_for_all_requests(); } trace.action("Typing the player's name: " + name); - type_on_system_keyboard(stream, context, name); + FastCodeEntry::type_on_system_keyboard(stream, context, name); pbf_press_button(context, BUTTON_PLUS, 80ms, 0ms); if (!wait_for_keyboard(stream, context, true, 5s)){ trace.fail("The keyboard stayed open after PLUS (OK)."); diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_ScreenEvents.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_ScreenEvents.cpp index 630819f397..8e3802a032 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_ScreenEvents.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_ScreenEvents.cpp @@ -14,8 +14,8 @@ namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonBDSP{ -using Pokemon::ScreenEvent; -using Pokemon::ScreenEvents; +using PokemonAutomation::ScreenEvent; +using PokemonAutomation::ScreenEvents; namespace{ // Around the player, whom the camera keeps in the middle: PLAYER_STILL's @@ -46,7 +46,7 @@ ScreenEvents BdspScreenObserver::observe(const VideoSnapshot& frame, WallClock n out.set(ScreenEvent::PLAYER_STILL, m_previous && walk_screen_change( extract_box_reference(*m_previous.frame, PLAYER_BOX), extract_box_reference(screen, PLAYER_BOX)) < WALK_TAP_BLOCKED_CHANGE); - out.merge(Pokemon::fade_events(fade_of_screen(screen))); + out.merge(fade_events(fade_of_screen(screen))); out.set(ScreenEvent::LOCATION_BANNER, !m_banner.empty() && location_banner_matches(m_reads.read(screen, now), m_banner)); m_previous = frame; return out; diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_ScreenEvents.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_ScreenEvents.h index b9fb719e77..dfdd560a93 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_ScreenEvents.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_ScreenEvents.h @@ -2,7 +2,7 @@ * * From: https://github.com/PokemonAutomation/ * - * BDSP's detectors as the shared screen events (Pokemon_ScreenEvents.h): + * BDSP's detectors as the shared screen events (CommonTools/ScreenEvents/ScreenEvents.h): * the dialogue box, the overworld, a still screen, a fade, the expected * map's location banner and a battle's start. * @@ -13,7 +13,7 @@ #include #include "CommonTools/VisualDetectors/BlackScreenDetector.h" -#include "Pokemon/AutoStory/Pokemon_ScreenEvents.h" +#include "CommonTools/ScreenEvents/ScreenEvents.h" #include "PokemonBDSP/Inference/Battles/PokemonBDSP_BattleMenuDetector.h" #include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" @@ -36,13 +36,13 @@ enum class BattleCue{ // The shared events from BDSP's detectors. Not the choice box, the // interact prompt or a battle's end: no AutoStory step reads them yet. -class BdspScreenObserver : public Pokemon::ScreenEventObserver{ +class BdspScreenObserver : public ScreenEventObserver{ public: // `banner`: the expected map's banner name; LOCATION_BANNER is only it // ("" for none). BdspScreenObserver(BattleCue cue, std::string banner); - virtual Pokemon::ScreenEvents observe(const VideoSnapshot& frame, WallClock now) override; + virtual ScreenEvents observe(const VideoSnapshot& frame, WallClock now) override; virtual void make_overlays(VideoOverlaySet& items) const override; private: diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.cpp index 9e10c1ac1f..f99b5e105e 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.cpp @@ -11,8 +11,8 @@ #include #include "CommonFramework/Exceptions/OperationFailedException.h" #include "CommonFramework/Notifications/ProgramNotifications.h" +#include "CommonFramework/StaticGlobals.h" #include "CommonFramework/VideoPipeline/VideoOverlay.h" -#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" #include "Pokemon/AutoStory/Pokemon_StoryPreflight.h" #include "Pokemon/AutoStory/Pokemon_StoryRun.h" #include "Pokemon/AutoStory/Pokemon_AutoStorySelect.h" @@ -21,19 +21,18 @@ #include "Pokemon/AutoStory/Pokemon_StorySegments.h" #include "Pokemon/Options/Pokemon_GameLanguages.h" #include "Pokemon/Options/Pokemon_StoryRunOptions.h" -#include "PokemonBDSP/Programs/PokemonBDSP_GameEntry.h" #include "PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Actions.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_StoryLanguage.h" -#include "PokemonBDSP_AutoStory_StoryTable.h" +#include "PokemonBDSP_AutoStory_ConsoleStory.h" +#include "PokemonBDSP_AutoStory_Launch.h" +#include "PokemonBDSP_AutoStory_StoryData.h" #include "PokemonBDSP_AutoStory.h" namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonBDSP{ -using namespace std::chrono_literals; - BDSPAutoStory_Descriptor::BDSPAutoStory_Descriptor() : SingleSwitchProgramDescriptor( @@ -56,12 +55,17 @@ std::unique_ptr BDSPAutoStory_Descriptor::make_stats() const{ namespace{ +// The rows a run lists: dev_only ones only in Developer Mode. +Pokemon::StoryRowsShown rows_shown(){ + return STATIC_GLOBALS.DEVELOPER_MODE ? Pokemon::StoryRowsShown::DEVELOPER : Pokemon::StoryRowsShown::USER; +} + // The start and end dropdowns' entries: segments 00 and 01 joined, 00.00 // to 01.11. The options hold a reference to it. const StringSelectDatabase& checkpoint_database(){ static const StringSelectDatabase database = []{ std::vector labels; - for (const StoryCheckpoint& row : story_checkpoints()){ + for (const StoryCheckpoint& row : Pokemon::story_rows_for_mode(load_bdsp_story({}), rows_shown())){ labels.push_back(story_checkpoint_label(row)); } return Pokemon::story_checkpoint_database(labels); @@ -79,19 +83,17 @@ std::set rows_that_stop(const std::vector& rows){ return ids; } -// What BDSP lends the shared shell: the console's preflight; a load is a -// reset from HOME, which loads the save the row made at its start; the -// engine's lines go to the console's log. +// What BDSP lends the shared shell: the console's preflight; a load is the +// shared launch from HOME (launch_bdsp_save()), which closes the game and +// loads the save the row made at its start; the engine's lines go to the +// console's log. Pokemon::StoryGameHooks story_game(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ Pokemon::StoryGameHooks game; game.preflight = [&env, &context]{ return Pokemon::StoryPreflight::read(env.console, context); }; game.load_save = [&env, &context](Pokemon::RunState&){ - if (!reset_game_from_home(env, env.console, context, true, 1000ms)){ - return Pokemon::outcome_failed("reload_failed", "Failed to reset the game from HOME."); - } - return Pokemon::outcome_ok(); + return launch_bdsp_save(env.console, context); }; game.log = [&env](const std::string& line){ env.log(line, COLOR_BLUE); @@ -234,10 +236,27 @@ std::string run_story( return done; } +// The rows for this run: the built-in checkpoints, with those of +// /AutoStoryDev/bdsp/ in their place, read now so a run after +// an edit plays the edited walks. Logs each dev file it uses; a bad one +// fails the run. +std::vector load_run_rows(ConsoleHandle& console){ + const std::string folder = Pokemon::story_dev_folder("bdsp"); + try{ + const Pokemon::StoryData story = load_bdsp_story(Pokemon::read_story_dev_files(folder, "bdsp")); + for (const auto& [name, path] : story.dev_files){ + console.log(std::format("AutoStory: using dev checkpoint {} from {}", name, path)); + } + return Pokemon::story_rows_for_mode(story, rows_shown()); + }catch (const ParseException& error){ + throw UserSetupError(console, std::format("AutoStory: bad dev checkpoint in {}: {}", folder, error.message())); + } +} + } // namespace void BDSPAutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ - const std::vector& checkpoints = story_checkpoints(); + const std::vector checkpoints = load_run_rows(env.console); const size_t first = RUN.start_index(); const size_t last = RUN.end_index(); if (first > last || last >= checkpoints.size()){ @@ -247,7 +266,7 @@ void BDSPAutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerCo set_story_language(RUN.language()); StoryEnv story{env, context, STARTER, PLAYER_NAME}; - StoryExecutor executor(story); + ConsoleStory executor(story); executor.set_stop_rows(rows_that_stop(checkpoints)); // A start with a save always loads it first, so the run never walks // from wherever the game was left (console B, 2026-11-11: a run from diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.h index 154eccade4..70a8182d08 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory.h @@ -19,7 +19,7 @@ #include "CommonFramework/Notifications/EventNotificationsTable.h" #include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" #include "Pokemon/Options/Pokemon_StoryRunOptions.h" -#include "PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP_AutoStory_StoryCheckpoint.h" namespace PokemonAutomation{ namespace NintendoSwitch{ diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_ConsoleStory.cpp similarity index 72% rename from SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.cpp rename to SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_ConsoleStory.cpp index 4bfb37b98b..e288bca0d6 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_ConsoleStory.cpp @@ -1,4 +1,4 @@ -/* BDSP AutoStory - Story executor +/* BDSP AutoStory - Console story * * From: https://github.com/PokemonAutomation/ * @@ -6,7 +6,9 @@ #include #include +#include #include +#include #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/Filesystem/Filesystem.h" #include "Common/Cpp/PrettyPrint.h" @@ -48,7 +50,7 @@ #include "Actions/PokemonBDSP_AutoStory_Talks.h" #include "PokemonBDSP_AutoStory_Walks.h" #include "Actions/PokemonBDSP_AutoStory_WildBattle.h" -#include "PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP_AutoStory_ConsoleStory.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -65,23 +67,6 @@ using Pokemon::TilePos; using Pokemon::story_arg; -std::optional story_route_costs(const std::string& action){ - if (action.empty() || action == ROUTE_ONTO_TRIGGER || action == ROUTE_INTO_SCENE || action == "route"){ - return route_plan_costs(); - } - if (action == "town"){ - return town_plan_costs(); - } - if (action == "robust"){ - return RobustPlanCosts(); - } - if (action == "line"){ - return waypoint_plan_costs(); - } - return std::nullopt; -} - - namespace{ // "to_catch_grass" as "to catch grass", for the logs. @@ -132,42 +117,43 @@ void ConsoleStepGuard::on_interrupt(const StoryStep& step, const Pokemon::Interr } void ConsoleStepGuard::on_screen(const StoryStep& step, const Pokemon::ScreenSettle& screen){ - m_stream.log(std::format("[Screen] before {}: {}", step.name, Pokemon::describe(screen))); + m_stream.log(std::format("[Screen] before {}: {}", step.name, describe(screen))); } // The table and the guard are members, built after the base: it only // keeps references to them until a step runs. -StoryExecutor::StoryExecutor(StoryEnv& env) - : Pokemon::TableExecutor(m_table, m_guard) +ConsoleStory::ConsoleStory(StoryEnv& env) + : Pokemon::TableExecutor(m_no_table, m_guard) , m_env(env) , m_io(env.env.console, env.context) , m_walks(m_io) , m_guard(env.env.console, m_checkpoint) - , m_table(story_action_table(m_walks, console_actions())) + , m_backend(Pokemon::StoryBackendKind::CONSOLE, [&env](std::string_view line){ env.env.console.log(std::string(line)); }, env.context) + , m_steps(m_walks, console_actions(), m_backend) , m_look(env.env.console, env.context) , m_interrupts(story_interrupts([this](bool fresh){ return m_look.look(fresh); }, console_interrupt_actions())) { set_interrupts(&m_interrupts); } -StoryBoundActions StoryExecutor::console_actions(){ - const auto method = [this](Outcome (StoryExecutor::*action)(RunState&, const StoryStep&)){ +StoryBoundActions ConsoleStory::console_actions(){ + const auto method = [this](Outcome (ConsoleStory::*action)(RunState&, const StoryStep&)){ return [this, action](const StoryStep& step, RunState& state){ return (this->*action)(state, step); }; }; return StoryBoundActions{ - .new_game = method(&StoryExecutor::new_game), - .options = method(&StoryExecutor::options), - .answer = method(&StoryExecutor::answer), - .battle = method(&StoryExecutor::battle), - .train = method(&StoryExecutor::train), - .heal = method(&StoryExecutor::heal), - .save = method(&StoryExecutor::save), - .event = [this](const std::string& name) -> Pokemon::StoryActionFunction{ + .new_game = method(&ConsoleStory::new_game), + .options = method(&ConsoleStory::options), + .answer = method(&ConsoleStory::answer), + .battle = method(&ConsoleStory::battle), + .train = method(&ConsoleStory::train), + .heal = method(&ConsoleStory::heal), + .save = method(&ConsoleStory::save), + .event = [this](const std::string& name) -> StoryBoundAction{ return [this, event = story_events().at(name)](const StoryStep& step, RunState& state){ - return event(*this, state, step); + return event(m_env, state, step); }; }, }; @@ -175,7 +161,7 @@ StoryBoundActions StoryExecutor::console_actions(){ // A wild battle is run from, a trainer who saw us fought, a dialog or a // warp's scene pressed through to the quiet overworld. -StoryInterruptActions StoryExecutor::console_interrupt_actions(){ +StoryInterruptActions ConsoleStory::console_interrupt_actions(){ const auto settle = [this](std::string label){ return [this, label](const Pokemon::InterruptVerdict&, RunState&){ m_io.settle(BUTTON_B, label, SettleKind::TALK, ""); @@ -196,24 +182,28 @@ StoryInterruptActions StoryExecutor::console_interrupt_actions(){ }; } -void StoryExecutor::begin_attempt(const Pokemon::StoryCheckpoint& data){ +void ConsoleStory::begin_attempt(const Pokemon::StoryCheckpoint& data){ m_checkpoint = data.id; m_walks.begin_attempt(data.id, data.steps); } +Outcome ConsoleStory::run_action(const StoryStep& step, RunState& state){ + return m_steps.act(step, state); +} + -Outcome StoryExecutor::new_game(RunState&, const StoryStep&){ +Outcome ConsoleStory::new_game(RunState&, const StoryStep&){ play_new_game_intro(m_env.env, context(), m_env.player_name); return Pokemon::outcome_ok(); } -Outcome StoryExecutor::options(RunState&, const StoryStep&){ +Outcome ConsoleStory::options(RunState&, const StoryStep&){ verify_tutorial_settings(stream(), context()); context().wait_for_all_requests(); return Pokemon::outcome_ok(); } -Outcome StoryExecutor::answer(RunState&, const StoryStep& step){ +Outcome ConsoleStory::answer(RunState&, const StoryStep& step){ wait_for_dialogue(stream(), context(), label_of(step)); const bool yes = story_arg(step.args, "yes"); pbf_press_button(context(), yes ? BUTTON_A : BUTTON_B, 80ms, 1000ms); @@ -221,7 +211,7 @@ Outcome StoryExecutor::answer(RunState&, const StoryStep& step){ return Pokemon::outcome_ok(); } -Outcome StoryExecutor::battle(RunState&, const StoryStep& step){ +Outcome ConsoleStory::battle(RunState&, const StoryStep& step){ // "starter": the Starly battle at the lake (00.02). It opens on its own, // with no talk, and the starter has no Poke Balls to throw; the scene // goes on whoever wins. @@ -246,7 +236,7 @@ Outcome StoryExecutor::battle(RunState&, const StoryStep& step){ return Pokemon::outcome_ok(); } -Outcome StoryExecutor::train(RunState&, const StoryStep& step){ +Outcome ConsoleStory::train(RunState&, const StoryStep& step){ const int64_t battles = story_arg(step.args, "battles"); for (int64_t battle = 0; battle < battles; battle++){ const std::string label = std::format("Training battle {}/{}", battle + 1, battles); @@ -257,7 +247,7 @@ Outcome StoryExecutor::train(RunState&, const StoryStep& step){ return Pokemon::outcome_ok(); } -Outcome StoryExecutor::heal(RunState&, const StoryStep& step){ +Outcome ConsoleStory::heal(RunState&, const StoryStep& step){ // From the planned tile below the door, run in like any door. The lit- // doorway lineup is for when the run in finds no door: people on the // way can leave the route unsure of the column (r59, Jubilife). Below @@ -306,17 +296,19 @@ Outcome StoryExecutor::heal(RunState&, const StoryStep& step){ return Pokemon::outcome_ok(); } -Outcome StoryExecutor::save(RunState&, const StoryStep& step){ - if (step.action.empty()){ +Outcome ConsoleStory::save(RunState&, const StoryStep& step){ + const auto saves_in = step.args.find(SAVES_IN_ARG); + const std::string* map = saves_in == step.args.end() ? nullptr : std::get_if(&saves_in->second); + if (map == nullptr || map->empty()){ save_game(m_env.env, context()); return Pokemon::outcome_ok(); } - // A row's start save, its action the map the card must name. With a - // map, save_game() throws only when the card names another one, having + // A row's start save, its SAVES_IN_ARG the map the card must name. With + // a map, save_game() throws only when the card names another one, having // backed out unsaved. That stops the program: a reset would load an // older save and run this row from wherever that is. try{ - save_game(m_env.env, context(), step.action); + save_game(m_env.env, context(), *map); }catch (OperationFailedException& e){ throw FatalProgramException( ErrorReportMode::NO_ERROR_REPORT, @@ -327,56 +319,7 @@ Outcome StoryExecutor::save(RunState&, const StoryStep& step){ return Pokemon::outcome_ok(); } -Outcome StoryExecutor::game(RunState&, const StoryStep& step){ - return Pokemon::outcome_failed("unknown_event", step.action); -} - -Pokemon::Segment make_story_segment( - const std::vector& rows, Pokemon::StoryStepExecutor& executor, - std::set& saved -){ - static const StoryStep SAVE_AT_START = []{ - StoryStep step; - step.kind = Pokemon::StoryStepKind::SAVE; - step.name = "save"; - step.why = "the save a failed step resets to"; - return step; - }(); - Pokemon::Segment segment; - segment.id = "BDSP"; - for (const StoryCheckpoint& row : rows){ - segment.checkpoints.push_back(Pokemon::CheckpointSpec{ - .id = row.id, .title = row.title, .saves = false, - .build = [&row, &executor, &saved]{ - Pokemon::Checkpoint steps = Pokemon::build_story_checkpoint(row, executor); - if (row.on_failure != StoryFailure::RELOAD_AND_RETRY){ - return steps; - } - StoryStep save_at_start = SAVE_AT_START; - save_at_start.action = row.saves_in; - Pokemon::Event save = Pokemon::story_event(save_at_start, executor); - save.skip_if.push_back(Pokemon::condition("saved at the start", [&saved, id = row.id](RunState&){ - return saved.contains(id); - })); - // A save that fails is a failed first attempt: the retry - // resets and does not save again. - save.act = [&executor, &saved, id = row.id, save_at_start](RunState& state){ - saved.insert(id); - return executor.run(save_at_start, state); - }; - Pokemon::Checkpoint checkpoint; - checkpoint.id = row.id; - checkpoint.events.push_back(std::move(save)); - for (Pokemon::Event& event : steps.events){ - checkpoint.events.push_back(std::move(event)); - } - return checkpoint; - }, - }); - } - return segment; -} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_ConsoleStory.h similarity index 57% rename from SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h rename to SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_ConsoleStory.h index 419d93af74..941caef321 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_ConsoleStory.h @@ -1,19 +1,17 @@ -/* BDSP AutoStory - Story executor - * -// code-health: allow header_symbols -- BDSP story executor and checkpoint execution primitives +/* BDSP AutoStory - Console story * * From: https://github.com/PokemonAutomation/ * - * BDSP's way of doing the shared story steps - * (Pokemon/AutoStory/Pokemon_StorySteps.h) on the console. A route is - * planned on the joined maps that hold its start and its target - * (story_plan_world()), from the tiles the run's pose holds, and walked by - * the walker, which fights the trainers who see us and runs from wild - * battles. A step whose variant names one of the story's events - * (story_events(), PokemonBDSP_AutoStory_Events.h) runs that event, - * whatever its kind: the scripted scenes and the console-measured walks - * of segment 01. It is the console's binding of the story's action table - * (story_action_table()); the simulation binds the same table. + * BDSP's story steps on the console: the story's actions (story_game(), + * PokemonBDSP_AutoStory_StoryActions.h) bound to the console and run by the + * shared story engine's step runner. A route is planned on the joined maps + * that hold its start and its target (story_plan_world()), from the tiles + * the run's pose holds, and walked by the walker, which fights the + * trainers who see us and runs from wild battles. A step whose variant + * names one of the story's events (story_events(), + * PokemonBDSP_AutoStory_Events.h) runs that event, whatever its kind: the + * scripted scenes and the console-measured walks of segment 01. The + * simulation binds the same actions. * * Every step runs inside a guard (ConsoleStepGuard) that turns a failed * console step into a failed Outcome, with a screenshot and the @@ -24,28 +22,20 @@ * */ -#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StoryExecutor_H -#define PokemonAutomation_PokemonBDSP_AutoStory_StoryExecutor_H +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_ConsoleStory_H +#define PokemonAutomation_PokemonBDSP_AutoStory_ConsoleStory_H #include -#include #include #include -#include #include -#include #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" -#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RobustRoute.h" -#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h" #include "Pokemon/AutoStory/Pokemon_StoryActions.h" -#include "Pokemon/AutoStory/Pokemon_StorySegments.h" -#include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" -#include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" #include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" #include "Pokemon/AutoStory/Types/Pokemon_StoryStep.h" -#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RoutePlanner.h" #include "PokemonBDSP_AutoStory_StoryActions.h" +#include "PokemonBDSP_AutoStory_StoryCheckpoint.h" #include "PokemonBDSP_AutoStory_StoryInterrupts.h" #include "PokemonBDSP_AutoStory_StoryStepIO.h" #include "PokemonBDSP_AutoStory_StoryWalkSteps.h" @@ -55,58 +45,6 @@ namespace NintendoSwitch{ namespace PokemonBDSP{ -// Starter Pokemon options for BDSP story automation. -enum class StarterChoice{ - TURTWIG, - CHIMCHAR, - PIPLUP, -}; - -// What a row does when one of its steps fails. -enum class StoryFailure{ - // Save before the first step; on a failure reset to that save and run - // the row again from its first step. - RELOAD_AND_RETRY, - // No save and no retry: the failure ends the program (00.00, before - // the game has anything worth saving). - STOP, -}; - -// A row of the story table: the shared checkpoint, and what a failure does. -struct StoryCheckpoint : public Pokemon::StoryCheckpoint{ - StoryFailure on_failure = StoryFailure::RELOAD_AND_RETRY; - // The map the save screen's card names where the row starts ("Route - // 203"). The save at its start is refused anywhere else and the program - // stops: r76/r77 started 01.06 a walk away from its start and saved over - // the good save there. Empty for a row that does not save first. - std::string saves_in; -}; - -// What the story's steps act on. -struct StoryEnv{ - SingleSwitchProgramEnvironment& env; - ProControllerContext& context; - StarterChoice starter = StarterChoice::CHIMCHAR; - std::string player_name; -}; - - -// The variant of a ROUTE step whose target is the next scene's trigger: -// the scene starts there and stops the walk, as a wall would (00.04's -// Mom's call on (6, 9)). It plans with the route costs. -inline constexpr std::string_view ROUTE_ONTO_TRIGGER = "trigger"; - -// The variant of a ROUTE step that, once at its target, runs its one leg -// ("legs") until a scene's dialogue starts. It plans with the route costs. -inline constexpr std::string_view ROUTE_INTO_SCENE = "into_scene"; - -// The plan costs a ROUTE step's variant names: "", ROUTE_ONTO_TRIGGER and -// ROUTE_INTO_SCENE the route costs, "town" town_plan_costs(), "robust" the planner's -// defaults, "line" waypoint_plan_costs() (a stretch of a line a human -// walked). Nothing for another name. -std::optional story_route_costs(const std::string& variant); - - // What the console does around each story step: the step's log line, and // a failed step's "checkpoint_reattempt_loop" line and screenshot. class ConsoleStepGuard : public Pokemon::StepGuard{ @@ -135,11 +73,12 @@ class ConsoleStepGuard : public Pokemon::StepGuard{ }; -// Does BDSP's kinds of story step on the console: the story's action -// table, its rows that need the console bound to the methods below. -class StoryExecutor : public Pokemon::TableExecutor{ +// Does BDSP's kinds of story step on the console: the story's actions on +// the engine (StoryEngineSteps), those that need the console bound to the +// methods below. +class ConsoleStory : public Pokemon::TableExecutor{ public: - explicit StoryExecutor(StoryEnv& env); + explicit ConsoleStory(StoryEnv& env); StoryEnv& env(){ return m_env; @@ -178,9 +117,10 @@ class StoryExecutor : public Pokemon::TableExecutor{ // In front of a Pokemon Center, its lit doorway on screen: in, the // nurse, and out (heal_at_pokemon_center()). Pokemon::Outcome heal(Pokemon::RunState& state, const Pokemon::StoryStep& step); + // The game; a row's start save (SAVES_IN_ARG) only where the card names its map. Pokemon::Outcome save(Pokemon::RunState& state, const Pokemon::StoryStep& step); - // The event its variant names (story_events()). - Pokemon::Outcome game(Pokemon::RunState& state, const Pokemon::StoryStep& step); + // The engine's action for the step. + virtual Pokemon::Outcome run_action(const Pokemon::StoryStep& step, Pokemon::RunState& state) override; private: StoryBoundActions console_actions(); @@ -209,25 +149,18 @@ class StoryExecutor : public Pokemon::TableExecutor{ // variant "a": A through it, for lines that give an item. StoryWalkSteps m_walks; std::string m_checkpoint; - std::map m_run_counts; ConsoleStepGuard m_guard; - // The story's actions, its console rows bound to this executor. - Pokemon::StoryActionTable m_table; + // TableExecutor's table: empty, as run_action() runs the engine's actions. + Pokemon::StoryActionTable m_no_table; + StoryLogBackend m_backend; + // The story's actions, those that need the console bound to this. + StoryEngineSteps m_steps; // What is cleared between steps, looked for on the screen. ConsoleScreenLook m_look; Pokemon::StoryInterrupts m_interrupts; }; -// `rows` (kept alive by the caller) as one segment. A row that reloads on -// failure saves first, unless `saved` says it has in this run: a retry -// starts from that save. `executor` and `saved` must outlive the segment. -Pokemon::Segment make_story_segment( - const std::vector& rows, Pokemon::StoryStepExecutor& executor, - std::set& saved -); - - } // namespace PokemonBDSP } // namespace NintendoSwitch diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Launch.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Launch.cpp new file mode 100644 index 0000000000..3aab24ab8c --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Launch.cpp @@ -0,0 +1,168 @@ +/* BDSP AutoStory - Launch + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/VisualDetectors/BlackScreenDetector.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch/Inference/NintendoSwitch_HomeMenuDetector.h" +#include "Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.h" +#include "Pokemon/AutoStory/Pokemon_StorySwitchPrompts.h" +#include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" +#include "Detect/PokemonBDSP_AutoStory_ScreenEvents.h" +#include "PokemonBDSP_AutoStory_Launch.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using namespace std::chrono_literals; +using Pokemon::GridPose; +using Pokemon::StoryLaunchPolicy; +using Pokemon::StoryLaunchPrompt; + + +namespace{ + +// The names HOME shows above the game's tile, in the console's language +// (console B's is English). Either version of the game. +constexpr std::string_view DIAMOND_TILE = "Pokémon Brilliant Diamond"; +constexpr std::string_view PEARL_TILE = "Pokémon Shining Pearl"; +constexpr Language HOME_LANGUAGE = Language::English; + +constexpr std::string_view NO_LAUNCH_WALK = "a launch never walks"; + +// Nothing BDSP's events show: no fade, box, battle or overworld. +bool quiet(const ScreenObservation& look){ + for (ScreenEvent event : { + ScreenEvent::FADE_BLACK, ScreenEvent::FADE_WHITE, ScreenEvent::DIALOG_BOX, + ScreenEvent::BATTLE_STARTED, ScreenEvent::PLAYER_CAN_MOVE, + }){ + if (look.events.has(event)){ + return false; + } + } + return true; +} + + +// The console, for the launch alone: presses, and looks that read HOME, +// the boot's black and BDSP's screen events (the Switch's prompts read +// the look's frame themselves). +// code-health: allow public_methods -- the StoryBackend interface; nothing to split +class BdspLaunchBackend : public Pokemon::StoryBackend{ +public: + BdspLaunchBackend(ConsoleHandle& console, ProControllerContext& context) + : m_console(console) + , m_context(context) + , m_home(console, COLOR_RED) + , m_black(COLOR_BLUE, {0.2, 0.2, 0.6, 0.6}) + , m_observer(BattleCue::WILD_MENU, "") + {} + + virtual Pokemon::StoryBackendKind kind() const override{ + return Pokemon::StoryBackendKind::CONSOLE; + } + virtual void press(Button button, Milliseconds hold, Milliseconds release) override{ + pbf_press_button(m_context, button, hold, release); + } + virtual void tap(Pokemon::Direction) override{ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::string(NO_LAUNCH_WALK)); + } + virtual void hold(Pokemon::Direction, Milliseconds, Pokemon::StoryPace) override{ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::string(NO_LAUNCH_WALK)); + } + virtual void stick(double, Milliseconds, Milliseconds, Pokemon::StoryPace) override{ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::string(NO_LAUNCH_WALK)); + } + virtual void wait(Milliseconds duration) override{ + m_context.wait_for(duration); + } + virtual Pokemon::Observation observe() override; + virtual Pokemon::Outcome battle(const Pokemon::StoryActionArgs&) override{ + return Pokemon::outcome_failed("unsupported_step", "a launch fights no battle"); + } + virtual void log(std::string_view line) override{ + m_console.log(std::string(line), COLOR_BLUE); + } + virtual CancellableScope& scope() override{ + return m_context; + } + +private: + ConsoleHandle& m_console; + ProControllerContext& m_context; + HomeMenuDetector m_home; + // The boot's and the load's black, as gamemenu_to_ingame() reads it. + BlackScreenDetector m_black; + BdspScreenObserver m_observer; +}; + +Pokemon::Observation BdspLaunchBackend::observe(){ + m_context.wait_for_all_requests(); + VideoSnapshot frame = m_console.video().snapshot(); + Pokemon::Observation look; + if (frame && m_black.detect(*frame.frame)){ + // The game booting or loading: never HOME, whose dark theme is + // dark too. + look.events.set(ScreenEvent::FADE_BLACK, true); + }else if (frame && m_home.detect(*frame.frame)){ + // Alone: BDSP's detectors may read HOME's plain screen as quiet. + look.events.set(ScreenEvent::SWITCH_HOME_MENU, true); + }else if (frame){ + static_cast(look) = Pokemon::screen_observation(m_observer.observe(frame, current_time())); + }else{ + // No frame: nothing to read, so not a quiet title either. + look.events.set(ScreenEvent::FADE_BLACK, true); + } + look.when = current_time(); + look.frame = std::move(frame); + return look; +} + +} // namespace + + +StoryLaunchPolicy bdsp_launch_policy(std::vector switch_prompts){ + StoryLaunchPolicy policy; + policy.game = "bdsp"; + policy.tile_selected = [ + diamond = Pokemon::switch_home_tile_named(HOME_LANGUAGE, std::string(DIAMOND_TILE)), + pearl = Pokemon::switch_home_tile_named(HOME_LANGUAGE, std::string(PEARL_TILE)) + ](const ScreenObservation& look){ + return diamond(look) || pearl(look); + }; + // Read only after the launch: a menu in play is quiet too. + policy.title = quiet; + policy.title_after_launch = true; + policy.title_press = {.button = BUTTON_ZL, .hold = 160ms, .release = 1000ms}; + policy.prompts = std::move(switch_prompts); + // The title takes ZL through the boot's logos too, a second apart, + // and through the frames of the load before the overworld shows. + policy.presses_per_screen = 20; + return policy; +} + +Pokemon::Outcome launch_bdsp_save(ConsoleHandle& console, ProControllerContext& context){ + BdspLaunchBackend backend(console, context); + const StoryLaunchPolicy policy = bdsp_launch_policy(Pokemon::switch_launch_prompts(console)); + const Pokemon::StoryLaunchRequest request{ + .save = Pokemon::StoryLaunchRequest::Save::LATEST, + .running = Pokemon::StoryLaunchRequest::Running::RESTART, + }; + return Pokemon::launch_story_game(backend, policy, request); +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Launch.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Launch.h new file mode 100644 index 0000000000..4aee6dcdd8 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Launch.h @@ -0,0 +1,42 @@ +/* BDSP AutoStory - Launch + * + * From: https://github.com/PokemonAutomation/ + * + * How BDSP's screens look on the way from HOME to the overworld, for the + * shared launch (Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.h). + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Launch_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Launch_H + +#include +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryLaunch.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// BDSP: the tile is "Pokémon Brilliant Diamond" or "Pokémon Shining +// Pearl" on HOME (Pokemon::switch_home_tile_named()); `switch_prompts` +// answer the Switch's own screens (Pokemon::switch_launch_prompts() on a +// console). After the launch, a quiet screen (no fade, box, battle or +// overworld) is the boot's logos or the title and takes ZL, as +// reset_game_from_home() mashed it: ZL does nothing on the overworld, so +// one pressed as the load ends is harmless. The title loads the save +// straight into the overworld, with no load menu or recap. +Pokemon::StoryLaunchPolicy bdsp_launch_policy(std::vector switch_prompts); + +// The game closed from HOME and launched again onto its save, by the +// shared launch on `console`. Fails as Pokemon::STORY_LAUNCH_FAILED. +Pokemon::Outcome launch_bdsp_save(ConsoleHandle& console, ProControllerContext& context); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Launch_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Launch_Tests.cpp new file mode 100644 index 0000000000..02fd8e50bc --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Launch_Tests.cpp @@ -0,0 +1,217 @@ +/* BDSP AutoStory - Launch Tests + * + * From: https://github.com/PokemonAutomation/ + * + * BDSP's launch policy driving the shared launch on a scripted console: + * each case is the screens the console shows, look by look (the last + * repeats), and the presses the launch must make, or where it must fail. + * The looks are events only, no frames: nothing is read in a language. + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryFakeBackend.h" +#include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" +#include "PokemonBDSP_AutoStory_Launch.h" +#include "PokemonBDSP_AutoStory_Launch_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using Pokemon::GridPose; + + +namespace{ + + +// The scripted console's "Close software?": the Switch's prompts +// (Pokemon::switch_launch_prompts()) read a look's frame, and a scripted +// look has none, so the test answers it from a reading. +constexpr std::string_view CLOSE_SOFTWARE = "test.close_software"; + +// What the scripted console shows. +enum class Screen{ + HOME, + CLOSE_SOFTWARE, + BLACK, // the boot, or the load after the title + QUIET, // nothing BDSP's events show: a logo, the title, a menu + OVERWORLD, // the player in control +}; + +Pokemon::Observation look(Screen screen){ + Pokemon::Observation observation; + switch (screen){ + case Screen::HOME: + observation.events = ScreenEvents{ScreenEvent::SWITCH_HOME_MENU}; + break; + case Screen::CLOSE_SOFTWARE: + observation.readings[std::string(CLOSE_SOFTWARE)] = 1; + break; + case Screen::BLACK: + observation.events = ScreenEvents{ScreenEvent::FADE_BLACK}; + break; + case Screen::QUIET: + break; + case Screen::OVERWORLD: + observation.events = ScreenEvents{ScreenEvent::PLAYER_CAN_MOVE}; + break; + } + return observation; +} + +// `screens`, then the overworld for as many looks as it takes to settle. +std::vector then_settled(std::vector screens){ + screens.insert(screens.end(), {Screen::OVERWORLD, Screen::OVERWORLD, Screen::OVERWORLD}); + return screens; +} + +// One launch. +struct LaunchCase{ + struct Input{ + std::vector screens; + }; + struct Expectation{ + // Empty: the launch succeeds; else a part of its failure's detail. + std::string_view failure = {}; + // The inputs in order, by space: a button ("A", "ZL") or "wait". + std::string inputs = {}; + }; + std::string_view name; + Input input; + Expectation expectation; +}; + +// `count` of `input`, by space. +std::string repeated(std::string_view input, size_t count){ + std::string text; + for (size_t c = 0; c < count; c++){ + text += c == 0 ? std::string(input) : std::format(" {}", input); + } + return text; +} + +const std::vector LAUNCH_CASES = { + {.name = "reload from the overworld", + .input = {.screens = then_settled({Screen::OVERWORLD, Screen::HOME, Screen::CLOSE_SOFTWARE, Screen::HOME, + Screen::BLACK, Screen::QUIET, Screen::BLACK})}, + .expectation = {.inputs = "HOME X A A wait ZL wait wait wait"}}, + {.name = "game closed on HOME", + .input = {.screens = then_settled({Screen::HOME, Screen::HOME, Screen::BLACK, Screen::QUIET, Screen::QUIET, + Screen::BLACK})}, + .expectation = {.inputs = "X A wait ZL ZL wait wait wait"}}, + // A quiet screen before the launch is the game in play (a menu), not + // its title: HOME, to load the save, not ZL. + {.name = "a quiet screen before the launch is not the title", + .input = {.screens = then_settled({Screen::QUIET, Screen::HOME, Screen::CLOSE_SOFTWARE, Screen::HOME, + Screen::BLACK, Screen::QUIET, Screen::BLACK})}, + .expectation = {.inputs = "HOME X A A wait ZL wait wait wait"}}, + {.name = "HOME for 1.5 s after the launch, then the boot", + .input = {.screens = then_settled({Screen::HOME, Screen::HOME, Screen::HOME, Screen::HOME, Screen::HOME, + Screen::BLACK, Screen::QUIET, Screen::BLACK})}, + .expectation = {.inputs = "X A wait wait wait wait ZL wait wait wait"}}, + // The policy's presses_per_screen ZLs, then the launch gives up. + {.name = "a title that never loads", + .input = {.screens = {Screen::HOME, Screen::HOME, Screen::QUIET}}, + .expectation = {.failure = "stuck on title", .inputs = std::format("X A {}", repeated("ZL", 20))}}, +}; + +// The fake backend's record of `input` ("A" -> "press A"). +std::string recorded(std::string_view input){ + if (input == "wait"){ + return "wait 500ms"; + } + static const std::map BUTTONS = { + {"A", BUTTON_A}, {"X", BUTTON_X}, {"ZL", BUTTON_ZL}, {"HOME", BUTTON_HOME}, + }; + const auto it = BUTTONS.find(input); + return it == BUTTONS.end() ? std::format("unknown input {}", input) : std::format("press {}", button_to_string(it->second)); +} + +std::vector expected_inputs(std::string_view inputs){ + std::vector expected; + while (!inputs.empty()){ + const size_t space = inputs.find(' '); + expected.push_back(recorded(inputs.substr(0, space))); + inputs = space == std::string_view::npos ? std::string_view() : inputs.substr(space + 1); + } + return expected; +} + +std::string describe_inputs(const std::vector& inputs){ + std::string text; + for (const std::string& input : inputs){ + text += text.empty() ? input : std::format(", {}", input); + } + return std::format("[{}]", text); +} + +UnitTestResult check_launch(const LaunchCase& test, CancellableScope& scope){ + std::vector> looks; + for (Screen screen : test.input.screens){ + looks.push_back(look(screen)); + } + Pokemon::FakeStoryBackend backend({.looks = std::move(looks)}, scope); + std::vector switch_prompts{{ + .name = "close software", + .matches = [](const ScreenObservation& observation){ + return observation.readings.contains(std::string(CLOSE_SOFTWARE)); + }, + }}; + Pokemon::StoryLaunchPolicy policy = bdsp_launch_policy(std::move(switch_prompts)); + // The scripted HOME has no frame to read a tile name from: its cursor + // is on BDSP's tile. + policy.tile_selected = [](const ScreenObservation&){ + return true; + }; + const Pokemon::Outcome outcome = Pokemon::launch_story_game(backend, policy, {}); + if (test.expectation.failure.empty() && !outcome.succeeded()){ + return std::format("expected success, got {}", Pokemon::to_string(outcome)); + } + if (!test.expectation.failure.empty() + && (outcome.failure != Pokemon::STORY_LAUNCH_FAILED || outcome.detail.find(test.expectation.failure) == std::string::npos) + ){ + return std::format("expected a launch failure with \"{}\", got {}", test.expectation.failure, Pokemon::to_string(outcome)); + } + const std::vector expected = expected_inputs(test.expectation.inputs); + if (backend.inputs() != expected){ + return std::format("expected inputs {}, got {}", describe_inputs(expected), describe_inputs(backend.inputs())); + } + return true; +} + +class BdspLaunchTest : public UnitTest{ +public: + explicit BdspLaunchTest(LaunchCase test) + : UnitTest(std::format("PokemonBDSP::AutoStory - Launch: {}", test.name)) + , m_test(std::move(test)) + {} + virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ + return check_launch(m_test, scope); + } + +private: + LaunchCase m_test; +}; + + +} // namespace + + +void add_tests_AutoStory_Launch(UnitTestDatabase& database){ + for (const LaunchCase& test : LAUNCH_CASES){ + database.add(test); + } +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Launch_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Launch_Tests.h new file mode 100644 index 0000000000..5f1b65a664 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Launch_Tests.h @@ -0,0 +1,26 @@ +/* BDSP AutoStory - Launch Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_Launch_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_Launch_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// BDSP's launch policy driving the shared launch on a scripted console, +// from HOME through the title and the load to the overworld. +void add_tests_AutoStory_Launch(UnitTestDatabase& database); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.cpp index d5f507d724..9d97d4bfef 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_PlannedRoute_Tests.cpp @@ -30,7 +30,7 @@ #include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" #include "Pokemon/AutoStory/Pokemon_TilePose.h" #include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" -#include "PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP_AutoStory_StoryCheckpoint.h" #include "PokemonBDSP_AutoStory_StoryTable.h" #include "Utils/PokemonBDSP_AutoStory_StoryLanguage.h" #include "Utils/PokemonBDSP_AutoStory_RouteTiming.h" diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RouteBench_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RouteBench_Tests.cpp index 0c9fa562f8..7a9b7a8d50 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RouteBench_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RouteBench_Tests.cpp @@ -29,7 +29,7 @@ #include "Data/PokemonBDSP_AutoStory_WallRecovery.h" #include "Utils/PokemonBDSP_AutoStory_RouteChoice.h" #include "Utils/PokemonBDSP_AutoStory_RouteWalk.h" -#include "PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP_AutoStory_StoryCheckpoint.h" #include "PokemonBDSP_AutoStory_StoryPlaces.h" #include "PokemonBDSP_AutoStory_StoryTable.h" #include "PokemonBDSP_AutoStory_TableRoutes.h" diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.cpp index e85849305c..375ff1c970 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_SimulatedRoute_Tests.cpp @@ -30,7 +30,7 @@ #include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" #include "Pokemon/AutoStory/Pokemon_TilePose.h" #include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" -#include "PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP_AutoStory_StoryCheckpoint.h" #include "PokemonBDSP_AutoStory_StoryPlaces.h" #include "PokemonBDSP_AutoStory_StoryTable.h" #include "PokemonBDSP_AutoStory_SimulatedRoute_Tests.h" @@ -60,13 +60,16 @@ struct RouteFailed{ // The console as a small simulator: the player stands on one tile of // `world`, walls and `obstacles` stop them, and every walk and tap moves // them. Fails the case after `max_calls` calls (a walker that loops). +// `speed` is the fastest the player goes: a walk paced to run goes at a +// walk until the player has the running shoes, as the walker counts it. // code-health: allow public_methods -- a RouteIO fake: the interface's seven overrides plus three test knobs class SimulatedRouteIO : public RouteIO{ public: - SimulatedRouteIO(const StoryWorld& world, TilePos at, std::vector obstacles, std::vector& notes) + SimulatedRouteIO(const StoryWorld& world, TilePos at, std::vector obstacles, MoveSpeed speed, std::vector& notes) : m_world(world) , m_at(at) , m_obstacles(std::move(obstacles)) + , m_speed(speed) , m_notes(notes) {} @@ -79,7 +82,7 @@ class SimulatedRouteIO : public RouteIO{ virtual WalkResult walk_to_wall(const RouteWallWalk& walk) override{ count_call(); - const MoveSpeed speed = walk.options.pace == DpadPace::RUN ? MoveSpeed::RUN : MoveSpeed::WALK; + const MoveSpeed speed = walk.options.pace == DpadPace::RUN ? m_speed : MoveSpeed::WALK; const Milliseconds per_tile = route_ms_per_tile(walk.direction, speed); if (m_overshoot && walk.limit > ROUTE_START_DELAY){ // A hold timed for n tiles (ROUTE_START_DELAY, then half a @@ -168,6 +171,7 @@ class SimulatedRouteIO : public RouteIO{ const StoryWorld& m_world; TilePos m_at; std::vector m_obstacles; + MoveSpeed m_speed; std::vector& m_notes; size_t m_calls = 0; size_t m_walked = 0; @@ -197,7 +201,7 @@ UnitTestResult test_blocked_from_several_tiles(Logger&){ } const TilePos goal{52, 41}; std::vector notes; - SimulatedRouteIO io(world, {53, 41}, {{54, 41}}, notes); + SimulatedRouteIO io(world, {53, 41}, {{54, 41}}, MoveSpeed::WALK, notes); RouteTrainers trainers{.sights = {}, .fight = [](const std::string&){}}; try{ walk_route(io, world, from, trainers, { @@ -244,7 +248,7 @@ UnitTestResult test_jubilife_center_held_overshoot(Logger&){ for (const MoveSpeed speed : {MoveSpeed::RUN, MoveSpeed::WALK}){ const std::string_view pace = speed == MoveSpeed::RUN ? "running" : "walking"; std::vector notes; - SimulatedRouteIO io(world, from, {}, notes); + SimulatedRouteIO io(world, from, {}, speed, notes); io.overshoot_timed_holds(true); RouteTrainers trainers{.sights = {}, .fight = [](const std::string&){}}; try{ @@ -273,7 +277,7 @@ UnitTestResult test_jubilife_center_kids(Logger&){ const TilePos from{48, 62}; const TilePos goal{52, 41}; std::vector notes; - SimulatedRouteIO io(world, from, {{52, 46}, {54, 46}}, notes); + SimulatedRouteIO io(world, from, {{52, 46}, {54, 46}}, MoveSpeed::WALK, notes); RouteTrainers trainers{.sights = {}, .fight = [](const std::string&){}}; try{ walk_route(io, world, {from}, trainers, { @@ -354,7 +358,7 @@ std::string sweep_story_route(const StoryRoute& route, bool overshoot, std::vect } for (const TilePos& from : start){ std::vector notes; - SimulatedRouteIO io(*world, from, {}, notes); + SimulatedRouteIO io(*world, from, {}, MoveSpeed::WALK, notes); io.overshoot_timed_holds(overshoot); RouteTrainers trainers{.sights = story_trainers(*world), .fight = [](const std::string&){}}; try{ diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.cpp index 9eb9ef9fde..6e8da5bcb9 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.cpp @@ -4,8 +4,16 @@ * */ +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryGame.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryStepRunner.h" #include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" #include "Actions/PokemonBDSP_AutoStory_Events.h" +#include "PokemonBDSP_AutoStory_StoryCheckpoint.h" #include "PokemonBDSP_AutoStory_StoryData.h" #include "PokemonBDSP_AutoStory_StoryActions.h" @@ -13,77 +21,197 @@ namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonBDSP{ +using Pokemon::GridPose; using Pokemon::Outcome; using Pokemon::RunState; -using Pokemon::StoryAction; using Pokemon::StoryStep; using Pokemon::StoryStepKind; +using GameAction = Pokemon::StoryAction; + namespace{ -// Adds `action` for `kind`'s default and for each of its named actions in -// bdsp_story_actions(), each with that spec's args, so lint_story_checkpoint() -// checks a step's args against the action it names. -void add_with_specs(Pokemon::StoryActionTable& table, StoryStepKind kind, const StoryAction& action){ +// `act` as the engine's handler: it runs the step the engine runs, with +// the step's own args. +Pokemon::StoryActionHandler handler_of(StoryBoundAction act){ + return [act = std::move(act)](Pokemon::StoryBackend&, const Pokemon::StoryActionCall& call, RunState& state){ + return act(call.step, state); + }; +} + +// What a step of a kind BDSP has no action for does. +Outcome unsupported(Pokemon::StoryBackend&, const Pokemon::StoryActionCall& call, RunState&){ + return Pokemon::unsupported_step(call.step); +} + +// The variants a kind's action reads that have no spec of their own: they +// take the kind's default's args. +const std::map>& variants_without_spec(){ + static const std::map> variants{ + {StoryStepKind::LEGS, {"measured"}}, + {StoryStepKind::STEP, {"tap"}}, + }; + return variants; +} + +// Adds `act` for `kind`: first as the kind's default, `name`, which is the +// built-in action's name where there is one, so it takes the built-in's +// place (the engine's default is a kind's first action), with the args of +// the kind's first spec in bdsp_story_actions(); then for each of the +// kind's other specs and variants. A name that is also a story event stays +// the event's. +void add_kind(std::vector& actions, StoryStepKind kind, std::string name, const StoryBoundAction& act){ const std::span specs = bdsp_story_actions(); - StoryAction fallback = action; + Pokemon::StoryActionSpec first{.kind = kind, .name = std::move(name), .args = {}}; if (const Pokemon::StoryActionSpec* spec = Pokemon::find_story_action(specs, kind, "")){ - fallback.args = spec->args; + first.args = spec->args; } - table.add(kind, "", std::move(fallback)); + actions.push_back({.spec = first, .handler = handler_of(act)}); for (const Pokemon::StoryActionSpec& spec : specs){ - // A name that is also a story event stays the event's. - if (spec.kind != kind || spec.name.empty() || story_events().contains(spec.name)){ + if (spec.kind != kind || spec.name.empty() || spec.name == first.name || story_events().contains(spec.name)){ continue; } - StoryAction named = action; - named.args = spec.args; - table.add(kind, spec.name, std::move(named)); + actions.push_back({.spec = spec, .handler = handler_of(act)}); + } + const auto variants = variants_without_spec().find(kind); + if (variants == variants_without_spec().end()){ + return; + } + for (const std::string& variant : variants->second){ + actions.push_back({.spec = {.kind = kind, .name = variant, .args = first.args}, .handler = handler_of(act)}); } } -} // namespace +// Each story event, for a step of any kind, with its spec's args if it has any. +void add_events(std::vector& actions, const StoryBoundActions& bound){ + for (const auto& item : story_events()){ + std::vector args; + for (const Pokemon::StoryActionSpec& spec : bdsp_story_actions()){ + if (spec.name == item.first && !spec.args.empty()){ + args = spec.args; + break; + } + } + const Pokemon::StoryActionHandler event = handler_of(bound.event(item.first)); + for (int kind = 0; kind <= (int)StoryStepKind::GAME; kind++){ + actions.push_back({.spec = {.kind = (StoryStepKind)kind, .name = item.first, .args = args}, .handler = event}); + } + } +} +// BDSP's story actions as the engine's game: the walking steps on +// `walks`, the rest `bound`'s. +Pokemon::StoryGame story_game(StoryWalkSteps& walks, const StoryBoundActions& bound){ + Pokemon::StoryGame game; + game.id = "bdsp"; + std::vector& actions = game.actions; + // A GAME step names its event: an unnamed one finds this. + actions.push_back({.spec = {.kind = StoryStepKind::GAME, .name = "", .args = {}}, .handler = &unsupported}); -Pokemon::StoryActionTable story_action_table(StoryWalkSteps& walks, const StoryBoundActions& bound){ - Pokemon::StoryActionTable table; using WalkMethod = Outcome (StoryWalkSteps::*)(RunState&, const StoryStep&); - const auto walk = [&](StoryStepKind kind, WalkMethod method){ - add_with_specs(table, kind, StoryAction{ - .act = [&walks, method](const StoryStep& step, RunState& state){ - return (walks.*method)(state, step); - }, + const auto walk = [&](StoryStepKind kind, std::string name, WalkMethod method){ + add_kind(actions, kind, std::move(name), [&walks, method](const StoryStep& step, RunState& state){ + return (walks.*method)(state, step); }); }; - walk(StoryStepKind::ROUTE, &StoryWalkSteps::route); - walk(StoryStepKind::LEGS, &StoryWalkSteps::legs); - walk(StoryStepKind::STEP, &StoryWalkSteps::step); - walk(StoryStepKind::TALK, &StoryWalkSteps::talk); - walk(StoryStepKind::MASH, &StoryWalkSteps::mash); - - const auto console = [&](StoryStepKind kind, const Pokemon::StoryActionFunction& act){ - add_with_specs(table, kind, StoryAction{.act = act, .needs_console = true}); - }; - console(StoryStepKind::NEW_GAME, bound.new_game); - console(StoryStepKind::OPTIONS, bound.options); - console(StoryStepKind::ANSWER, bound.answer); - console(StoryStepKind::BATTLE, bound.battle); - console(StoryStepKind::TRAIN, bound.train); - console(StoryStepKind::HEAL, bound.heal); - console(StoryStepKind::SAVE, bound.save); + walk(StoryStepKind::ROUTE, "route", &StoryWalkSteps::route); + walk(StoryStepKind::LEGS, "legs", &StoryWalkSteps::legs); + walk(StoryStepKind::STEP, "face", &StoryWalkSteps::step); + walk(StoryStepKind::TALK, "talk", &StoryWalkSteps::talk); + walk(StoryStepKind::MASH, "mash", &StoryWalkSteps::mash); - for (const auto& item : story_events()){ - StoryAction event{.act = bound.event(item.first), .needs_console = true}; - for (const Pokemon::StoryActionSpec& spec : bdsp_story_actions()){ - if (spec.name == item.first && !spec.args.empty()){ - event.args = spec.args; - break; - } + add_kind(actions, StoryStepKind::NEW_GAME, "new_game", bound.new_game); + add_kind(actions, StoryStepKind::OPTIONS, "options", bound.options); + add_kind(actions, StoryStepKind::ANSWER, "answer", bound.answer); + add_kind(actions, StoryStepKind::BATTLE, "fight", bound.battle); + add_kind(actions, StoryStepKind::TRAIN, "train", bound.train); + add_kind(actions, StoryStepKind::HEAL, "heal", bound.heal); + add_kind(actions, StoryStepKind::SAVE, "save", bound.save); + // A row's start save names the map its card must show (make_story_segment()). + actions.back().spec.args.push_back(Pokemon::StoryArgSpec{ + .key = std::string(SAVES_IN_ARG), .type = Pokemon::StoryArgType::WORD, .required = false, + .fallback = std::nullopt, .why = "the map the save card must name; none saves anywhere", + }); + + // No SHOP or READY of BDSP's own: the built-in ones would need the game's. + for (const Pokemon::StoryActionSpec& spec : Pokemon::builtin_story_actions()){ + if (spec.kind == StoryStepKind::SHOP || spec.kind == StoryStepKind::READY){ + actions.push_back({.spec = spec, .handler = &unsupported}); } - table.add_any_kind(item.first, std::move(event)); } - return table; + add_events(actions, bound); + return game; +} + +// Why the backend's inputs throw. +inline constexpr std::string_view NO_BACKEND_INPUT = "BDSP's story actions play through StoryStepIO, not the backend's inputs."; + +} // namespace + + +StoryLogBackend::StoryLogBackend(Pokemon::StoryBackendKind kind, std::function log, CancellableScope& scope) + : m_kind(kind) + , m_log(std::move(log)) + , m_scope(scope) +{} + +void StoryLogBackend::press(Button, Milliseconds, Milliseconds){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::string(NO_BACKEND_INPUT)); +} +void StoryLogBackend::tap(Pokemon::Direction){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::string(NO_BACKEND_INPUT)); +} +void StoryLogBackend::hold(Pokemon::Direction, Milliseconds, Pokemon::StoryPace){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::string(NO_BACKEND_INPUT)); +} +void StoryLogBackend::stick(double, Milliseconds, Milliseconds, Pokemon::StoryPace){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::string(NO_BACKEND_INPUT)); +} +void StoryLogBackend::wait(Milliseconds){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::string(NO_BACKEND_INPUT)); +} + +Pokemon::Observation StoryLogBackend::observe(){ + return {}; +} + +Outcome StoryLogBackend::battle(const Pokemon::StoryActionArgs&){ + return Pokemon::outcome_failed("unsupported_step", "BDSP fights in its BATTLE action"); +} + +void StoryLogBackend::log(std::string_view line){ + if (m_log){ + m_log(line); + } +} + + +// The game first: the registry and the runner keep references to it. +struct StoryEngineSteps::Engine{ + Engine(StoryWalkSteps& walks, const StoryBoundActions& bound, Pokemon::StoryBackend& backend) + : game(story_game(walks, bound)) + , registry(game) + , runner(registry, backend) + {} + + Pokemon::StoryGame game; + Pokemon::StoryActionRegistry registry; + Pokemon::StoryStepRunner runner; +}; + +StoryEngineSteps::StoryEngineSteps(StoryWalkSteps& walks, const StoryBoundActions& bound, Pokemon::StoryBackend& backend) + : m_engine(std::make_unique(walks, bound, backend)) +{} + +StoryEngineSteps::~StoryEngineSteps() = default; + +Outcome StoryEngineSteps::act(const StoryStep& step, RunState& state){ + return m_engine->runner.act(step, state); +} + +std::vector StoryEngineSteps::check(const StoryStep& step) const{ + return m_engine->registry.check(step); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.h index 6204f74751..8c7616c357 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.h @@ -2,16 +2,28 @@ * * From: https://github.com/PokemonAutomation/ * - * BDSP's story steps as one action table (Pokemon_StoryActions.h), which - * the console (StoryExecutor) and the simulation (SimExecutor) each bind. - * The walking steps (ROUTE, LEGS, STEP, TALK, MASH) are the same code on - * both: StoryWalkSteps on the binding's StoryStepIO. The steps that need - * the console (the intro, the options, answers, battles, training, the - * Center, saves, and the story's events, story_events()) are the - * binding's own: played on the console, stood in for on the simulation. + * BDSP's story steps as the actions of the shared story engine + * (Pokemon/AutoStory/Engine/): one StoryGame, run by the engine's step + * runner (StoryEngineSteps), which the console (ConsoleStory) and the + * simulation (SimExecutor) each bind. The walking steps (ROUTE, LEGS, + * STEP, TALK, MASH) are the same code on both: StoryWalkSteps on the + * binding's StoryStepIO. The steps that need the console (the intro, the + * options, answers, battles, training, the Center, saves, and the story's + * events, story_events()) are the binding's own: played on the console, + * stood in for on the simulation. * - * A GAME step runs only the event its variant names, and BDSP has no SHOP - * or READY step: lint_story_checkpoint() reports a step with neither. + * An action is found by its step's kind and name, as the engine finds it; + * a story event's name finds its event for a step of any kind. A GAME + * step runs only the event its variant names, and BDSP has no SHOP or + * READY step: the engine's built-in ones fail as unsupported_step(). + * + * BDSP's actions drive the game through StoryStepIO, not through the + * engine backend's inputs yet: StoryLogBackend gives the runner the log, + * the scope and what it plays on. The engine has no step guard and no + * look between steps yet: the bindings run each step inside + * TableExecutor's (Pokemon_StoryActions.h), whose run_action() is + * StoryEngineSteps::act(). This header leaves out the engine's StoryGame, + * whose StoryAction would clash with that header's. * */ @@ -19,8 +31,13 @@ #define PokemonAutomation_PokemonBDSP_AutoStory_StoryActions_H #include +#include #include -#include "Pokemon/AutoStory/Pokemon_StoryActions.h" +#include +#include +#include "Common/Cpp/CancellableScope.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h" +#include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" #include "PokemonBDSP_AutoStory_StoryWalkSteps.h" namespace PokemonAutomation{ @@ -28,23 +45,77 @@ namespace NintendoSwitch{ namespace PokemonBDSP{ +// Does `step`, from the run so far. +using StoryBoundAction = std::function; + // The actions a binding does its own way, one per kind that needs the // console. struct StoryBoundActions{ - Pokemon::StoryActionFunction new_game = {}; - Pokemon::StoryActionFunction options = {}; - Pokemon::StoryActionFunction answer = {}; - Pokemon::StoryActionFunction battle = {}; - Pokemon::StoryActionFunction train = {}; - Pokemon::StoryActionFunction heal = {}; - Pokemon::StoryActionFunction save = {}; + StoryBoundAction new_game = {}; + StoryBoundAction options = {}; + StoryBoundAction answer = {}; + StoryBoundAction battle = {}; + StoryBoundAction train = {}; + StoryBoundAction heal = {}; + StoryBoundAction save = {}; // The action for the story event named `name` (story_events()). - std::function event = {}; + std::function event = {}; +}; + + +// The engine's backend for BDSP: the log, the scope and the kind. Its +// inputs throw InternalProgramError and observe() sees nothing: BDSP's +// actions play through StoryStepIO. +// code-health: allow public_methods -- these are the engine's StoryBackend overrides, which the engine fixes; none can move out +class StoryLogBackend : public Pokemon::StoryBackend{ +public: + // `scope` must outlive the backend. + StoryLogBackend(Pokemon::StoryBackendKind kind, std::function log, CancellableScope& scope); + + virtual Pokemon::StoryBackendKind kind() const override{ + return m_kind; + } + virtual void press(Button button, Milliseconds hold, Milliseconds release) override; + virtual void tap(Pokemon::Direction direction) override; + virtual void hold(Pokemon::Direction direction, Milliseconds duration, Pokemon::StoryPace pace) override; + virtual void stick(double degrees, Milliseconds duration, Milliseconds release, Pokemon::StoryPace pace) override; + virtual void wait(Milliseconds duration) override; + virtual Pokemon::Observation observe() override; + virtual Pokemon::Outcome battle(const Pokemon::StoryActionArgs& args) override; + virtual void log(std::string_view line) override; + virtual CancellableScope& scope() override{ + return m_scope; + } + +private: + Pokemon::StoryBackendKind m_kind; + std::function m_log; + CancellableScope& m_scope; }; -// BDSP's story actions: the walking steps on `walks`, the rest `bound`'s. -// `walks` must outlive the table. -Pokemon::StoryActionTable story_action_table(StoryWalkSteps& walks, const StoryBoundActions& bound); + +// BDSP's story actions on the engine: the walking steps on `walks`, the +// rest `bound`'s, run by the engine's step runner on `backend`. `walks` +// and `backend` must outlive it. +class StoryEngineSteps{ +public: + StoryEngineSteps(StoryWalkSteps& walks, const StoryBoundActions& bound, Pokemon::StoryBackend& backend); + ~StoryEngineSteps(); + StoryEngineSteps(const StoryEngineSteps&) = delete; + StoryEngineSteps& operator=(const StoryEngineSteps&) = delete; + + // `step`'s action (StoryStepRunner::act()): unsupported_step() for a + // step with none. + Pokemon::Outcome act(const Pokemon::StoryStep& step, Pokemon::RunState& state); + + // What is wrong with `step` before it runs (StoryActionRegistry::check()): + // no action for it, or args that don't fit its action's. + std::vector check(const Pokemon::StoryStep& step) const; + +private: + struct Engine; + std::unique_ptr m_engine; +}; diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryCheckpoint.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryCheckpoint.cpp new file mode 100644 index 0000000000..ca80cf782f --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryCheckpoint.cpp @@ -0,0 +1,86 @@ +/* BDSP AutoStory - Story checkpoint + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_PlanCosts.h" +#include "PokemonBDSP_AutoStory_StoryCheckpoint.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +using Pokemon::RunState; +using Pokemon::StoryStep; + + +std::optional story_route_costs(const std::string& action){ + if (action.empty() || action == ROUTE_ONTO_TRIGGER || action == ROUTE_INTO_SCENE || action == "route"){ + return route_plan_costs(); + } + if (action == "town"){ + return town_plan_costs(); + } + if (action == "robust"){ + return RobustPlanCosts(); + } + if (action == "line"){ + return waypoint_plan_costs(); + } + return std::nullopt; +} + + +Pokemon::Segment make_story_segment( + const std::vector& rows, Pokemon::StoryStepExecutor& executor, + std::set& saved +){ + static const StoryStep SAVE_AT_START = []{ + StoryStep step; + step.kind = Pokemon::StoryStepKind::SAVE; + step.name = "save"; + step.why = "the save a failed step resets to"; + return step; + }(); + Pokemon::Segment segment; + segment.id = "BDSP"; + for (const StoryCheckpoint& row : rows){ + segment.checkpoints.push_back(Pokemon::CheckpointSpec{ + .id = row.id, .title = row.title, .saves = false, + .build = [&row, &executor, &saved]{ + Pokemon::Checkpoint steps = Pokemon::build_story_checkpoint(row, executor); + if (row.on_failure != StoryFailure::RELOAD_AND_RETRY){ + return steps; + } + StoryStep save_at_start = SAVE_AT_START; + save_at_start.args.emplace(SAVES_IN_ARG, row.saves_in); + Pokemon::Event save = Pokemon::story_event(save_at_start, executor); + save.skip_if.push_back(Pokemon::condition("saved at the start", [&saved, id = row.id](RunState&){ + return saved.contains(id); + })); + // A save that fails is a failed first attempt: the retry + // resets and does not save again. + save.act = [&executor, &saved, id = row.id, save_at_start](RunState& state){ + saved.insert(id); + return executor.run(save_at_start, state); + }; + Pokemon::Checkpoint checkpoint; + checkpoint.id = row.id; + checkpoint.events.push_back(std::move(save)); + for (Pokemon::Event& event : steps.events){ + checkpoint.events.push_back(std::move(event)); + } + return checkpoint; + }, + }); + } + return segment; +} + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryCheckpoint.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryCheckpoint.h new file mode 100644 index 0000000000..e6cdd9f387 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryCheckpoint.h @@ -0,0 +1,102 @@ +/* BDSP AutoStory - Story checkpoint + * +// code-health: allow header_symbols -- the story table's row type, the run's options and the segment its rows make + * + * From: https://github.com/PokemonAutomation/ + * + * A row of BDSP's story table, what the run plays with (the starter, the + * player's name), the plan costs a ROUTE step's variant names, and the + * segment the rows make for the shared runner (Pokemon_StorySegments.h). + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_StoryCheckpoint_H +#define PokemonAutomation_PokemonBDSP_AutoStory_StoryCheckpoint_H + +#include +#include +#include +#include +#include +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" +#include "Pokemon/AutoStory/Pokemon_StorySegments.h" +#include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" +#include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RobustRoute.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// Starter Pokemon options for BDSP story automation. +enum class StarterChoice{ + TURTWIG, + CHIMCHAR, + PIPLUP, +}; + +// What a row does when one of its steps fails. +enum class StoryFailure{ + // Save before the first step; on a failure reset to that save and run + // the row again from its first step. + RELOAD_AND_RETRY, + // No save and no retry: the failure ends the program (00.00, before + // the game has anything worth saving). + STOP, +}; + +// A row of the story table: the shared checkpoint, and what a failure does. +struct StoryCheckpoint : public Pokemon::StoryCheckpoint{ + StoryFailure on_failure = StoryFailure::RELOAD_AND_RETRY; + // The map the save screen's card names where the row starts ("Route + // 203"). The save at its start is refused anywhere else and the program + // stops: r76/r77 started 01.06 a walk away from its start and saved over + // the good save there. Empty for a row that does not save first. + std::string saves_in; +}; + +// What the story's steps act on. +struct StoryEnv{ + SingleSwitchProgramEnvironment& env; + ProControllerContext& context; + StarterChoice starter = StarterChoice::CHIMCHAR; + std::string player_name; +}; + + +// The variant of a ROUTE step whose target is the next scene's trigger: +// the scene starts there and stops the walk, as a wall would (00.04's +// Mom's call on (6, 9)). It plans with the route costs. +inline constexpr std::string_view ROUTE_ONTO_TRIGGER = "trigger"; + +// The variant of a ROUTE step that, once at its target, runs its one leg +// ("legs") until a scene's dialogue starts. It plans with the route costs. +inline constexpr std::string_view ROUTE_INTO_SCENE = "into_scene"; + +// The plan costs a ROUTE step's variant names: "", ROUTE_ONTO_TRIGGER and +// ROUTE_INTO_SCENE the route costs, "town" town_plan_costs(), "robust" the planner's +// defaults, "line" waypoint_plan_costs() (a stretch of a line a human +// walked). Nothing for another name. +std::optional story_route_costs(const std::string& variant); + + +// The arg of a row's start save (a SAVE step) that names the map its card +// must show: the row's saves_in. +inline constexpr std::string_view SAVES_IN_ARG = "saves_in"; + +// `rows` (kept alive by the caller) as one segment. A row that reloads on +// failure saves first, unless `saved` says it has in this run: a retry +// starts from that save. `executor` and `saved` must outlive the segment. +Pokemon::Segment make_story_segment( + const std::vector& rows, Pokemon::StoryStepExecutor& executor, + std::set& saved +); + + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.cpp index a6bea013c2..f913eddee9 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.cpp @@ -4,7 +4,6 @@ * */ -#include #include #include #include "Common/Cpp/Exceptions.h" @@ -127,11 +126,14 @@ std::span bdsp_story_actions(){ return actions; } -StoryCheckpoint read_authored_checkpoint(const JsonValue& value, std::string_view source){ +namespace{ + +Pokemon::StoryFileRow read_story_file(const JsonValue& value, std::string_view source){ StoryFailure on_failure = StoryFailure::RELOAD_AND_RETRY; std::string saves_in; + Pokemon::StoryRowExecution shared; auto read_execution = [&](const JsonObject& execution, std::string_view src){ - Pokemon::validate_json_fields(execution, {"on_failure", "saves_in"}, src); + shared = Pokemon::read_story_row_execution(execution, src, {"on_failure", "saves_in"}); on_failure = read_failure(execution.get_string_throw("on_failure")); saves_in = execution.get_string_throw("saves_in"); if ((on_failure == StoryFailure::RELOAD_AND_RETRY) == saves_in.empty()){ @@ -144,7 +146,13 @@ StoryCheckpoint read_authored_checkpoint(const JsonValue& value, std::string_vie ); result.on_failure = on_failure; result.saves_in = std::move(saves_in); - return result; + return {std::move(result), shared}; +} + +} // namespace + +StoryCheckpoint read_authored_checkpoint(const JsonValue& value, std::string_view source){ + return read_story_file(value, source).checkpoint; } JsonValue authored_checkpoint_document(std::string_view file){ @@ -160,22 +168,29 @@ StoryCheckpoint load_authored_checkpoint(std::string_view file){ return read_authored_checkpoint(authored_checkpoint_document(file), file); } -std::vector load_authored_story(){ - std::vector result; - for (const auto& [name, text] : bdsp_checkpoint_json()){ - if (name.ends_with(".plan")){ - result.push_back(read_authored_checkpoint(parse_json(text), name)); +Pokemon::StoryData load_bdsp_story(std::span dev_files){ + std::vector plans; + for (const Pokemon::EmbeddedJsonFile& file : bdsp_checkpoint_json()){ + if (file.first.ends_with(".plan")){ + plans.push_back(file); } } - std::sort(result.begin(), result.end(), [](const StoryCheckpoint& a, const StoryCheckpoint& b){ - return a.id < b.id; + Pokemon::StoryData story = Pokemon::load_story_data(plans, dev_files, { + .read = [](std::string_view text, std::string_view source){ + return read_story_file(parse_json(std::string(text)), source); + }, + // The story plays its rows in id order (bdsp-mine-01.03 after 01.02b). + .plays_before = [](const StoryCheckpoint& a, const StoryCheckpoint& b){ + return a.id < b.id; + }, }); + const std::vector& result = story.checkpoints; for (size_t c = 1; c < result.size(); c++){ if (result[c].id == result[c - 1].id){ throw ParseException("Two authored checkpoints are " + result[c].id + "."); } } - return result; + return story; } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.h index 052cd044d8..8148579e2a 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData.h @@ -13,8 +13,9 @@ #include #include #include "Common/Cpp/Json/JsonValue.h" +#include "Pokemon/AutoStory/Data/Pokemon_StoryFiles.h" #include "Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h" -#include "PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP_AutoStory_StoryCheckpoint.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -34,8 +35,11 @@ StoryCheckpoint read_authored_checkpoint(const JsonValue& value, std::string_vie // Load an embedded authored checkpoint; a missing file throws InternalProgramError. StoryCheckpoint load_authored_checkpoint(std::string_view file); -// Load all embedded authored checkpoints in id order. -std::vector load_authored_story(); +// The embedded authored checkpoints in id order, each of `dev_files` in +// place of the embedded file of its name (Pokemon_StoryFiles.h). Throws +// ParseException for a bad file, a dev file the shared loader refuses, or +// two checkpoints with one id. +Pokemon::StoryData load_bdsp_story(std::span dev_files); // The embedded authored checkpoint file, parsed but not read. JsonValue authored_checkpoint_document(std::string_view file); diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData_Tests.cpp index 7dca2b0353..eb9e5de025 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryData_Tests.cpp @@ -6,11 +6,13 @@ #include #include +#include #include #include #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/Json/JsonObject.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Pokemon/AutoStory/Data/Pokemon_StoryFiles.h" #include "Pokemon/AutoStory/Data/Pokemon_StoryStepJson.h" #include "PokemonBDSP_AutoStory_StoryData.h" #include "PokemonBDSP_AutoStory_StoryTable.h" @@ -23,18 +25,171 @@ namespace PokemonBDSP{ namespace{ -struct StoryDataInput{ - std::string name; +// The built-in story table, plus one checkpoint file kept out of it. +struct StoryTableInput{ + std::string_view extra_file; +}; + +struct StoryTableExpectation{ + // Every built-in checkpoint id, in story order. 00.00 stops on failure + // with no save; every other row reloads its save and retries. + std::vector ids; + std::string_view extra_file_id; + std::string_view extra_file_saves_in; +}; + +struct StoryTableCase{ + std::string_view name; + StoryTableInput input; + StoryTableExpectation expectation; +}; + +UnitTestResult check_story_table_case(const StoryTableCase& test_case){ + const StoryTableExpectation& expected = test_case.expectation; + const auto& table = story_checkpoints(); + if (table.size() != expected.ids.size()){ + return std::format("Expected {} authored checkpoints, got {}", expected.ids.size(), table.size()); + } + for (size_t i = 0; i < expected.ids.size(); i++){ + if (table[i].id != expected.ids[i]){ + return std::format("Checkpoint index {} mismatch: expected {}, got {}", i, expected.ids[i], table[i].id); + } + const bool first = i == 0; + const StoryFailure policy = first ? StoryFailure::STOP : StoryFailure::RELOAD_AND_RETRY; + if (table[i].on_failure != policy || table[i].saves_in.empty() != first){ + return std::format("{}: wrong on_failure or saves_in \"{}\"", table[i].id, table[i].saves_in); + } + if (table[i].steps.empty()){ + return std::format("{} has no steps", table[i].id); + } + } + + const StoryCheckpoint extra = load_authored_checkpoint(test_case.input.extra_file); + if (extra.id != expected.extra_file_id || extra.on_failure != StoryFailure::RELOAD_AND_RETRY || extra.saves_in != expected.extra_file_saves_in){ + return std::format("Failed to load {} correctly", test_case.input.extra_file); + } + return true; +} + +class Test_StoryTable : public UnitTest{ +public: + explicit Test_StoryTable(StoryTableCase test_case) + : UnitTest(std::format("PokemonBDSP::StoryData - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return check_story_table_case(m_case); + } + +private: + StoryTableCase m_case; +}; + +const StoryTableCase STORY_TABLE_CASES[] = { + { + .name = "All 23 authored checkpoints load", + .input = {.extra_file = "bdsp-mine-01.03.plan"}, + .expectation = { + .ids = { + "00.00", "00.01", "00.02", "00.03", "00.04", "00.04b", "00.04c", "00.04d", + "00.05", "00.05b", "00.05c", + "01.01", "01.02", "01.02b", "01.03", "01.04", "01.05", "01.06", "01.07", "01.08", "01.09", "01.10", "01.11", + }, + .extra_file_id = "01.03", + .extra_file_saves_in = "Oreburgh City", + }, + }, +}; + + +// One checkpoint of the built-in story table, as loaded from its file. +struct CheckpointInput{ + std::string_view id; +}; + +struct CheckpointExpectation{ + std::string_view title; + StoryFailure on_failure = StoryFailure::RELOAD_AND_RETRY; + std::string_view saves_in; + std::vector step_names; +}; + +struct CheckpointCase{ + std::string_view name; + CheckpointInput input; + CheckpointExpectation expectation; +}; + +UnitTestResult check_checkpoint_case(const CheckpointCase& test_case){ + const CheckpointInput& input = test_case.input; + const CheckpointExpectation& expected = test_case.expectation; + const auto& table = story_checkpoints(); + auto it = std::find_if(table.begin(), table.end(), [&](const StoryCheckpoint& cp){ + return cp.id == input.id; + }); + if (it == table.end()){ + return std::format("{} checkpoint not found in table", input.id); + } + const StoryCheckpoint& cp = *it; + if (cp.title != expected.title){ + return std::format("{} title: expected \"{}\", got \"{}\"", input.id, expected.title, cp.title); + } + if (cp.on_failure != expected.on_failure || cp.saves_in != expected.saves_in){ + return std::format("{} policy mismatch (saves_in \"{}\")", input.id, cp.saves_in); + } + if (cp.steps.size() != expected.step_names.size()){ + return std::format("{} expected {} steps, got {}", input.id, expected.step_names.size(), cp.steps.size()); + } + for (size_t i = 0; i < expected.step_names.size(); i++){ + if (cp.steps[i].name != expected.step_names[i]){ + return std::format("{} step {} name mismatch: expected {}, got {}", input.id, i, expected.step_names[i], cp.steps[i].name); + } + } + return true; +} + +class Test_StoryCheckpoint : public UnitTest{ +public: + explicit Test_StoryCheckpoint(CheckpointCase test_case) + : UnitTest(std::format("PokemonBDSP::StoryData - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return check_checkpoint_case(m_case); + } + +private: + CheckpointCase m_case; +}; + +const CheckpointCase CHECKPOINT_CASES[] = { + { + .name = "Parity: 01.02b checkpoint", + .input = {.id = "01.02b"}, + .expectation = { + .title = "Oreburgh Center heal", + .saves_in = "Oreburgh City", + .step_names = {"below_barry", "barry", "barrys_lines", "below_the_centers_door", "heal_oreburgh_first"}, + }, + }, +}; + + +// bdsp-00.01.plan with one change to its envelope (read_authored_checkpoint()). +struct EnvelopeInput{ void (*mutate)(JsonObject&) = nullptr; }; -struct StoryDataExpectation{ +struct EnvelopeExpectation{ bool rejected = false; }; -struct StoryDataCase{ - StoryDataInput input; - StoryDataExpectation expectation; +struct EnvelopeCase{ + std::string_view name; + EnvelopeInput input; + EnvelopeExpectation expectation; }; void extra(JsonObject& root){ @@ -69,80 +224,14 @@ void bad_metadata_phase(JsonObject& root){ root["metadata"] = std::move(meta); } -UnitTestResult check_all_checkpoints_load(){ - const auto& table = story_checkpoints(); - if (table.size() != 22){ - return std::format("Expected 22 authored checkpoints, got {}", table.size()); - } - - const std::vector expected_ids{ - "00.00", "00.01", "00.02", "00.03", "00.04", "00.04b", "00.04c", "00.04d", - "00.05", "00.05b", "00.05c", - "01.01", "01.02", "01.02b", "01.03", "01.04", "01.05", "01.06", "01.07", "01.08", "01.09", "01.10", "01.11" - }; - - for (size_t i = 0; i < expected_ids.size(); i++){ - if (table[i].id != expected_ids[i]){ - return std::format("Checkpoint index {} mismatch: expected {}, got {}", i, expected_ids[i], table[i].id); - } - if (table[i].id == "00.00"){ - if (table[i].on_failure != StoryFailure::STOP || !table[i].saves_in.empty()){ - return "00.00 must have on_failure=stop and empty saves_in"; - } - }else{ - if (table[i].on_failure != StoryFailure::RELOAD_AND_RETRY || table[i].saves_in.empty()){ - return std::format("{} must have on_failure=reload_and_retry and non-empty saves_in", table[i].id); - } - } - if (table[i].steps.empty()){ - return std::format("{} has no steps", table[i].id); - } - } - - const StoryCheckpoint mine = load_authored_checkpoint("bdsp-mine-01.03.plan"); - if (mine.id != "01.03" || mine.on_failure != StoryFailure::RELOAD_AND_RETRY || mine.saves_in != "Oreburgh City"){ - return "Failed to load bdsp-mine-01.03.plan correctly"; - } - - return true; -} - -UnitTestResult check_parity_01_02b(){ - const auto& table = story_checkpoints(); - auto it = std::find_if(table.begin(), table.end(), [](const StoryCheckpoint& cp){ - return cp.id == "01.02b"; - }); - if (it == table.end()){ - return "01.02b checkpoint not found in table"; - } - const StoryCheckpoint& cp = *it; - if (cp.title != "Oreburgh Center heal"){ - return "01.02b title mismatch"; - } - if (cp.on_failure != StoryFailure::RELOAD_AND_RETRY || cp.saves_in != "Oreburgh City"){ - return "01.02b policy mismatch"; - } - if (cp.steps.size() != 5){ - return std::format("01.02b expected 5 steps, got {}", cp.steps.size()); - } - const std::vector expected_step_names{ - "below_barry", "barry", "barrys_lines", "below_the_centers_door", "heal_oreburgh_first" - }; - for (size_t i = 0; i < expected_step_names.size(); i++){ - if (cp.steps[i].name != expected_step_names[i]){ - return std::format("01.02b step {} name mismatch: expected {}, got {}", i, expected_step_names[i], cp.steps[i].name); - } - } - return true; -} - -UnitTestResult run_envelope_case(const StoryDataCase& test_case){ - JsonValue val = authored_checkpoint_document("bdsp-00.01.plan"); +UnitTestResult run_envelope_case(const EnvelopeCase& test_case){ + constexpr std::string_view PLAN_FILE = "bdsp-00.01.plan"; + JsonValue val = authored_checkpoint_document(PLAN_FILE); if (test_case.input.mutate != nullptr){ test_case.input.mutate(val.to_object_throw()); } try{ - StoryCheckpoint cp = read_authored_checkpoint(val, test_case.input.name); + StoryCheckpoint cp = read_authored_checkpoint(val, PLAN_FILE); if (test_case.expectation.rejected){ return "Expected rejection but read_authored_checkpoint succeeded."; } @@ -158,63 +247,217 @@ UnitTestResult run_envelope_case(const StoryDataCase& test_case){ } } -class Test_StoryData : public UnitTest{ +class Test_StoryEnvelope : public UnitTest{ public: - explicit Test_StoryData(StoryDataCase test_case) - : UnitTest("PokemonBDSP::StoryData - " + test_case.input.name) + explicit Test_StoryEnvelope(EnvelopeCase test_case) + : UnitTest(std::format("PokemonBDSP::StoryData - Envelope: {}", test_case.name)) , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - if (m_case.input.name == "All 22 authored checkpoints load"){ - return check_all_checkpoints_load(); - } return run_envelope_case(m_case); } private: - StoryDataCase m_case; + EnvelopeCase m_case; }; -const StoryDataCase STORY_DATA_CASES[] = { +const EnvelopeCase ENVELOPE_CASES[] = { { - .input = {.name = "All 23 authored checkpoints load"}, + .name = "valid 00.01 plan", + .input = {}, .expectation = {.rejected = false}, }, { - .input = {.name = "Parity: 01.02b checkpoint"}, - .expectation = {.rejected = false}, + .name = "extra envelope field", + .input = {.mutate = extra}, + .expectation = {.rejected = true}, }, { - .input = {.name = "Envelope: valid 00.01 plan"}, - .expectation = {.rejected = false}, + .name = "wrong game", + .input = {.mutate = wrong_game}, + .expectation = {.rejected = true}, }, { - .input = {.name = "Envelope: extra envelope field", .mutate = extra}, + .name = "unknown failure policy", + .input = {.mutate = wrong_policy}, .expectation = {.rejected = true}, }, { - .input = {.name = "Envelope: wrong game", .mutate = wrong_game}, + .name = "missing save-card location for reload_and_retry", + .input = {.mutate = missing_card}, .expectation = {.rejected = true}, }, { - .input = {.name = "Envelope: unknown failure policy", .mutate = wrong_policy}, + .name = "stop policy with start save", + .input = {.mutate = stop_with_save}, .expectation = {.rejected = true}, }, { - .input = {.name = "Envelope: missing save-card location for reload_and_retry", .mutate = missing_card}, + .name = "unknown metadata field", + .input = {.mutate = bad_metadata_field}, .expectation = {.rejected = true}, }, { - .input = {.name = "Envelope: stop policy with start save", .mutate = stop_with_save}, + .name = "unknown metadata phase", + .input = {.mutate = bad_metadata_phase}, .expectation = {.rejected = true}, }, +}; + + +// A dev file in place of a built-in checkpoint file (load_bdsp_story()). +struct DevFile{ + std::string file; // the dev file's name + std::string_view builtin; // the built-in file it starts from + void (*mutate)(JsonObject&) = nullptr; +}; + +struct DevFileInput{ + std::vector files; +}; + +struct DevFileExpectation{ + bool rejected = false; + // story_summary() of the story loaded. + std::string summary = {}; +}; + +struct DevFileCase{ + std::string name; + DevFileInput input; + DevFileExpectation expectation; +}; + +void retitle(JsonObject& root){ + root.get_object_throw("checkpoint")["title"] = "Retimed"; +} + +void mark_dev_only(JsonObject& root){ + root.get_object_throw("execution")["dev_only"] = true; +} + +void other_id(JsonObject& root){ + root.get_object_throw("checkpoint")["id"] = "00.99"; +} + +void end_on_autosave(JsonObject& root){ + root.get_object_throw("execution")["save"] = "autosave"; +} + +void reload_when_chained(JsonObject& root){ + root.get_object_throw("execution")["reload_when_chained"] = true; +} + +std::vector dev_files_of(const DevFileInput& input){ + std::vector files; + for (const DevFile& file : input.files){ + JsonValue document = authored_checkpoint_document(file.builtin); + if (file.mutate != nullptr){ + file.mutate(document.to_object_throw()); + } + files.push_back({.name = file.file, .path = std::format("AutoStoryDev/bdsp/{}", file.file), .text = document.dump()}); + } + return files; +} + +// The loaded story in one line: 00.01's title, the dev_only rows, how +// many rows a user sees and the dev files used; or "not in id order". +std::string story_summary(const Pokemon::StoryData& story){ + const bool in_id_order = std::is_sorted(story.checkpoints.begin(), story.checkpoints.end(), + [](const StoryCheckpoint& a, const StoryCheckpoint& b){ return a.id < b.id; }); + if (!in_id_order){ + return "not in id order"; + } + std::string title; + for (const StoryCheckpoint& row : story.checkpoints){ + if (row.id == "00.01"){ + title = row.title; + } + } + std::string dev_only; + for (const std::string& id : story.dev_only){ + dev_only += std::format(" {}", id); + } + std::string used; + for (const auto& [name, path] : story.dev_files){ + used += std::format(" {}", name); + } + const size_t user_rows = Pokemon::story_rows_for_mode(story, Pokemon::StoryRowsShown::USER).size(); + return std::format("{} | dev_only{} | {} user rows | dev{}", title, dev_only, user_rows, used); +} + +UnitTestResult check_dev_file_case(const DevFileCase& test_case){ + std::string summary; + try{ + summary = story_summary(load_bdsp_story(dev_files_of(test_case.input))); + }catch (const ParseException& error){ + if (test_case.expectation.rejected){ + return true; + } + return std::format("Unexpected rejection: {}", error.message()); + } + if (test_case.expectation.rejected){ + return "Expected rejection but load_bdsp_story() succeeded."; + } + if (summary != test_case.expectation.summary){ + return std::format("Got \"{}\", expected \"{}\".", summary, test_case.expectation.summary); + } + return true; +} + +class Test_StoryDevFiles : public UnitTest{ +public: + explicit Test_StoryDevFiles(DevFileCase test_case) + : UnitTest(std::format("PokemonBDSP::StoryData - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return check_dev_file_case(m_case); + } + +private: + DevFileCase m_case; +}; + +const DevFileCase DEV_FILE_CASES[] = { { - .input = {.name = "Envelope: unknown metadata field", .mutate = bad_metadata_field}, + .name = "Dev file: none, the built-in rows in id order", + .input = {}, + .expectation = {.summary = "Bedroom to the Verity Lakefront | dev_only | 23 user rows | dev"}, + }, + { + .name = "Dev file: replaces the built-in file of its name", + .input = {.files = {{.file = "bdsp-00.01.plan.json", .builtin = "bdsp-00.01.plan", .mutate = retitle}}}, + .expectation = {.summary = "Retimed | dev_only | 23 user rows | dev bdsp-00.01.plan.json"}, + }, + { + .name = "Dev file: an out-of-order file's row stays in id order", + .input = {.files = {{.file = "bdsp-mine-01.03.plan.json", .builtin = "bdsp-mine-01.03.plan"}}}, + .expectation = {.summary = "Bedroom to the Verity Lakefront | dev_only | 23 user rows | dev bdsp-mine-01.03.plan.json"}, + }, + { + .name = "Dev file: bdsp-mine-01.03 reloads after 01.02b, the row before it by id", + .input = {.files = { + {.file = "bdsp-01.02b.plan.json", .builtin = "bdsp-01.02b.plan", .mutate = end_on_autosave}, + {.file = "bdsp-mine-01.03.plan.json", .builtin = "bdsp-mine-01.03.plan", .mutate = reload_when_chained}, + }}, + .expectation = {.summary = "Bedroom to the Verity Lakefront | dev_only | 23 user rows | dev bdsp-01.02b.plan.json bdsp-mine-01.03.plan.json"}, + }, + { + .name = "Dev file: a dev_only row is not listed for a user", + .input = {.files = {{.file = "bdsp-00.01.plan.json", .builtin = "bdsp-00.01.plan", .mutate = mark_dev_only}}}, + .expectation = {.summary = "Bedroom to the Verity Lakefront | dev_only 00.01 | 22 user rows | dev bdsp-00.01.plan.json"}, + }, + { + .name = "Dev file: named like no built-in file, refused", + .input = {.files = {{.file = "bdsp-99.99.plan.json", .builtin = "bdsp-00.01.plan"}}}, .expectation = {.rejected = true}, }, { - .input = {.name = "Envelope: unknown metadata phase", .mutate = bad_metadata_phase}, + .name = "Dev file: changes the checkpoint id, refused", + .input = {.files = {{.file = "bdsp-00.01.plan.json", .builtin = "bdsp-00.01.plan", .mutate = other_id}}}, .expectation = {.rejected = true}, }, }; @@ -222,8 +465,17 @@ const StoryDataCase STORY_DATA_CASES[] = { } // namespace void add_tests_AutoStory_StoryData(UnitTestDatabase& database){ - for (const auto& item : STORY_DATA_CASES){ - database.add(item); + for (const auto& item : STORY_TABLE_CASES){ + database.add(item); + } + for (const auto& item : CHECKPOINT_CASES){ + database.add(item); + } + for (const auto& item : ENVELOPE_CASES){ + database.add(item); + } + for (const auto& item : DEV_FILE_CASES){ + database.add(item); } } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryInterrupts.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryInterrupts.h index 000352fcbd..83c7b995f9 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryInterrupts.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryInterrupts.h @@ -22,8 +22,9 @@ #include "Common/Cpp/Time.h" #include "CommonFramework/Tools/VideoStream.h" #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "NintendoSwitch/Inference/NintendoSwitch_HomeScreenObserver.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" -#include "Pokemon/AutoStory/Pokemon_ScreenEvents.h" +#include "CommonTools/ScreenEvents/ScreenEvents.h" #include "Pokemon/AutoStory/Pokemon_StoryInterrupts.h" #include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_ScreenEvents.h" @@ -55,7 +56,7 @@ constexpr Milliseconds BETWEEN_STEPS_LOOK = Milliseconds(300); // The console's look between steps: BDSP's screen events for // BETWEEN_STEPS_LOOK, everything showing in it judged. The Switch's HOME -// menu comes first (Pokemon::SwitchHomeObserver): BDSP's detectors read it +// menu comes first (SwitchHomeObserver): BDSP's detectors read it // as a quiet overworld (lab-B 2026-11-09 15:31). class ConsoleScreenLook{ public: @@ -67,8 +68,8 @@ class ConsoleScreenLook{ VideoStream& m_stream; ProControllerContext& m_context; BdspScreenObserver m_observer; - Pokemon::SwitchHomeObserver m_eyes; - Pokemon::ScreenEventWatcher m_watcher; + SwitchHomeObserver m_eyes; + ScreenEventWatcher m_watcher; WallClock m_start; }; diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.cpp index 3977dedaa1..6c20e4f57e 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.cpp @@ -16,7 +16,7 @@ namespace PokemonBDSP{ const std::vector& story_checkpoints(){ - static const std::vector checkpoints = load_authored_story(); + static const std::vector checkpoints = load_bdsp_story({}).checkpoints; return checkpoints; } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.h index 72504da1b8..39bfeffd90 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.h @@ -16,7 +16,7 @@ #include #include -#include "PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP_AutoStory_StoryCheckpoint.h" namespace PokemonAutomation{ namespace NintendoSwitch{ diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.cpp index a48158f2ca..33ee03e9f7 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.cpp @@ -29,7 +29,7 @@ #include "Data/PokemonBDSP_AutoStory_WallRecovery.h" #include "Actions/PokemonBDSP_AutoStory_Events.h" #include "Data/PokemonBDSP_AutoStory_WalkRules.h" -#include "PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP_AutoStory_StoryCheckpoint.h" #include "PokemonBDSP_AutoStory_StoryPlaces.h" #include "PokemonBDSP_AutoStory_TableRoutes.h" #include "PokemonBDSP_AutoStory_StoryTable.h" diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.cpp index cd37db912c..8472b10b0c 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.cpp @@ -24,7 +24,7 @@ #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkDpad.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h" -#include "PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP_AutoStory_StoryCheckpoint.h" #include "PokemonBDSP_AutoStory_StoryPlaces.h" #include "PokemonBDSP_AutoStory_StoryTable.h" #include "PokemonBDSP_AutoStory_StoryWalkSteps.h" diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.h index 344e2b80c1..9f52467e64 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.h @@ -3,7 +3,7 @@ * From: https://github.com/PokemonAutomation/ * * The story steps that walk and talk (ROUTE, LEGS, STEP, TALK, MASH), on a - * StoryStepIO: StoryExecutor runs them on the console, the story's + * StoryStepIO: ConsoleStory runs them on the console, the story's * simulation tests on a simulated player. * */ @@ -93,7 +93,7 @@ class StoryWalkSteps{ // With a leg, then runs on toward its direction until a dialogue starts, // for its tiles at most (run_to_dialogue()). Pokemon::Outcome route(Pokemon::RunState& state, const Pokemon::StoryStep& step); - // See StoryExecutor::legs(). + // See ConsoleStory's LEGS. Pokemon::Outcome legs(Pokemon::RunState& state, const Pokemon::StoryStep& step); // One D-pad step, or a run through a door when the step changes map. Pokemon::Outcome step(Pokemon::RunState& state, const Pokemon::StoryStep& step); diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_TableRoutes.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_TableRoutes.cpp index 9f55f5a6c2..c6ae3d160a 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_TableRoutes.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_TableRoutes.cpp @@ -8,7 +8,7 @@ #include #include "Data/PokemonBDSP_AutoStory_RobustRoute.h" #include "Data/PokemonBDSP_AutoStory_WallRecovery.h" -#include "PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP_AutoStory_StoryCheckpoint.h" #include "PokemonBDSP_AutoStory_StoryPlaces.h" #include "Utils/PokemonBDSP_AutoStory_RouteChoice.h" #include "PokemonBDSP_AutoStory_StoryTable.h" diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.cpp index 36df0774d0..919d99af30 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.cpp @@ -396,7 +396,7 @@ UnitTestResult test_leg_battle_place(Logger& logger){ if (item.settled){ record_tap_battle(walk, item.tap); }else{ - walk.events.record({Pokemon::ScreenEvent::FADE_WHITE}, walk.started + Milliseconds(800 * (item.tap - 1) + 500)); + walk.events.record({ScreenEvent::FADE_WHITE}, walk.started + Milliseconds(800 * (item.tap - 1) + 500)); } const LegBattlePlace place = place_leg_battle(walk, 10); logger.log(leg_battle_line(item.name, walk, place)); diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.cpp index 0185a04e74..48ed98935e 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.cpp @@ -30,7 +30,7 @@ #include "Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.h" #include "PokemonBDSP_AutoStory_OldStorySteps.h" -#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryCheckpoint.h" #include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.h" #include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h" #include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.h" @@ -311,7 +311,7 @@ std::optional pose_after_step(const std::optional& at, const StorySt } // Every ROUTE step of the table that starts from a known pose, from its -// first and its last tile, at the pace StoryExecutor walks it. +// first and its last tile, at the pace ConsoleStory walks it. std::vector table_routes(){ std::vector routes; for (const StoryCheckpoint& row : story_checkpoints()){ diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StoryActions_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StoryActions_Tests.cpp index 9b795d8b73..95bc677c9a 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StoryActions_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StoryActions_Tests.cpp @@ -10,9 +10,9 @@ #include #include #include +#include "Common/Cpp/CancellableScope.h" #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/TestRunners/UnitTest.h" -#include "Pokemon/AutoStory/Pokemon_StoryActions.h" #include "PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.h" #include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.h" #include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.h" @@ -44,7 +44,7 @@ std::vector> story_steps(){ StoryStep save; save.kind = StoryStepKind::SAVE; save.name = "save"; - save.action = row.saves_in; + save.args.emplace(SAVES_IN_ARG, row.saves_in); out.emplace_back(row.id, std::move(save)); } return out; @@ -65,7 +65,7 @@ std::string old_dispatch(const StoryStep& step){ case StoryStepKind::STEP: case StoryStepKind::TALK: case StoryStepKind::MASH: - return "walk " + kind; + return std::format("walk {}", kind); case StoryStepKind::NEW_GAME: case StoryStepKind::OPTIONS: case StoryStepKind::ANSWER: @@ -75,7 +75,7 @@ std::string old_dispatch(const StoryStep& step){ case StoryStepKind::SAVE: return "console " + kind; default: - return "executor " + kind; + return std::format("executor {}", kind); } } @@ -94,7 +94,7 @@ StoryBoundActions naming_actions(){ .train = named(StoryStepKind::TRAIN), .heal = named(StoryStepKind::HEAL), .save = named(StoryStepKind::SAVE), - .event = [](const std::string& name) -> Pokemon::StoryActionFunction{ + .event = [](const std::string& name) -> StoryBoundAction{ return [name](const StoryStep&, RunState&){ return Pokemon::outcome_failed("event", name); }; @@ -102,19 +102,40 @@ StoryBoundActions naming_actions(){ }; } -// What `table` runs for `step`, in old_dispatch()'s words. A walking row +// Whether `step` runs one of the binding's own actions, which need the +// console: an event, or a kind StoryWalkSteps does not do. +bool needs_console(const StoryStep& step){ + if (story_events().contains(step.action)){ + return true; + } + switch (step.kind){ + case StoryStepKind::ROUTE: + case StoryStepKind::LEGS: + case StoryStepKind::STEP: + case StoryStepKind::TALK: + case StoryStepKind::MASH: + return false; + default: + return true; + } +} + +// What `steps` runs for `step`, in old_dispatch()'s words. A walking step // is not run: it would walk. -std::string table_dispatch(const Pokemon::StoryActionTable& table, const StoryStep& step){ - const Pokemon::StoryAction* action = table.find(step); - if (action == nullptr){ - return std::string("executor ") + Pokemon::story_step_kind_name(step.kind); +std::string engine_dispatch(StoryEngineSteps& steps, const StoryStep& step){ + const std::string kind = Pokemon::story_step_kind_name(step.kind); + if (!steps.check(step).empty()){ + return std::format("executor {}", kind); } - if (!action->needs_console){ - return std::string("walk ") + Pokemon::story_step_kind_name(step.kind); + if (!needs_console(step)){ + return std::format("walk {}", kind); } RunState state; - const Pokemon::Outcome outcome = action->act(step, state); - return outcome.failure + " " + outcome.detail; + const Pokemon::Outcome outcome = steps.act(step, state); + if (outcome.failure == "unsupported_step"){ + return std::format("executor {}", kind); + } + return std::format("{} {}", outcome.failure, outcome.detail); } @@ -122,9 +143,15 @@ UnitTestResult test_every_step_has_an_action(Logger& logger){ SimExecutor sim(logger); size_t steps = 0; for (const StoryCheckpoint& row : story_checkpoints()){ - const std::vector unbound = Pokemon::lint_story_checkpoint(row, sim.actions()); - if (!unbound.empty()){ - return unbound.front(); + for (size_t c = 0; c < row.steps.size(); c++){ + const StoryStep& step = row.steps[c]; + const std::vector problems = sim.steps().check(step); + if (!problems.empty()){ + return std::format( + "{} step {} {}: {} for {} '{}'", + row.id, c + 1, step.name, problems.front(), Pokemon::story_step_kind_name(step.kind), step.action + ); + } } steps += row.steps.size(); } @@ -140,7 +167,9 @@ UnitTestResult test_table_runs_what_the_old_dispatch_ran(Logger& logger){ const std::string step_name; SimStoryStepIO io(sim, notes, misses, knobs, step_name, []{ return false; }, [](const Pokemon::Pose&){}); StoryWalkSteps walks(io); - const Pokemon::StoryActionTable table = story_action_table(walks, naming_actions()); + CancellableHolder scope; + StoryLogBackend backend(Pokemon::StoryBackendKind::SCRIPT, {}, scope); + StoryEngineSteps engine(walks, naming_actions(), backend); std::set> seen; for (const auto& [row, step] : story_steps()){ @@ -148,9 +177,9 @@ UnitTestResult test_table_runs_what_the_old_dispatch_ran(Logger& logger){ continue; } const std::string expected = old_dispatch(step); - const std::string actual = table_dispatch(table, step); + const std::string actual = engine_dispatch(engine, step); if (actual != expected){ - return std::format("{} {} ({} \"{}\"): the table runs \"{}\", the old dispatch ran \"{}\".", + return std::format("{} {} ({} \"{}\"): the engine runs \"{}\", the old dispatch ran \"{}\".", row, step.name, Pokemon::story_step_kind_name(step.kind), step.action, actual, expected); } } @@ -163,8 +192,7 @@ UnitTestResult test_stubbed_steps_need_the_console(Logger& logger){ run_rows_on(sim, logger, {.from = "00.00", .to = "01.11"}); logger.log("Steps the simulation does not play, by row:\n" + stub_report(sim.result())); for (const auto& [row, step] : story_steps()){ - const Pokemon::StoryAction* action = sim.actions().find(step); - if (action != nullptr && action->needs_console){ + if (needs_console(step)){ continue; } for (const SimRun::RowStart& start : sim.result().rows){ diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StoryActions_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StoryActions_Tests.h index ee24d30c9a..613e13f857 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StoryActions_Tests.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StoryActions_Tests.h @@ -15,7 +15,7 @@ namespace PokemonBDSP{ -// The story's action table (story_action_table()): every step of the +// The story's actions on the engine (StoryEngineSteps): every step of the // story has an action, each runs what the executors' own dispatch ran // before the table, and the steps the simulation stubs need the console. void add_tests_AutoStory_StoryActions(UnitTestDatabase& database); diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.cpp index 7e63509263..08e86dfe6f 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.cpp @@ -13,6 +13,7 @@ #include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" #include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" #include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" +#include "PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.h" #include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" #include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.h" @@ -102,16 +103,16 @@ std::string play_scene(const SimScene& scene, const TalkOverRule& rule){ beat_start += scene[beat].length; beat++; } - Pokemon::ScreenEvents look{Pokemon::ScreenEvent::PLAYER_CAN_MOVE, Pokemon::ScreenEvent::SCREEN_STILL, Pokemon::ScreenEvent::PLAYER_STILL}; + ScreenEvents look{ScreenEvent::PLAYER_CAN_MOVE, ScreenEvent::SCREEN_STILL, ScreenEvent::PLAYER_STILL}; if (beat < scene.size()){ const SimBeat& shown = scene[beat]; look = {}; - look.set(Pokemon::ScreenEvent::DIALOG_BOX, shown.line); - look.set(Pokemon::ScreenEvent::PLAYER_CAN_MOVE, shown.overworld); - look.set(Pokemon::ScreenEvent::SCREEN_STILL, !shown.moving); - look.set(Pokemon::ScreenEvent::PLAYER_STILL, !shown.at_player); + look.set(ScreenEvent::DIALOG_BOX, shown.line); + look.set(ScreenEvent::PLAYER_CAN_MOVE, shown.overworld); + look.set(ScreenEvent::SCREEN_STILL, !shown.moving); + look.set(ScreenEvent::PLAYER_STILL, !shown.at_player); - look.merge(Pokemon::fade_events(shown.fade)); + look.merge(fade_events(shown.fade)); } switch (judge.look(look, now)){ case SceneMove::DONE: @@ -675,7 +676,7 @@ StoryBoundActions SimExecutor::sim_actions(){ return heal(state, step); }, .save = stubbed, - .event = [stubbed](const std::string&) -> Pokemon::StoryActionFunction{ + .event = [stubbed](const std::string&) -> StoryBoundAction{ return stubbed; }, }; @@ -688,15 +689,15 @@ constexpr size_t OPENING_LOOKS = 6; Pokemon::ScreenLook SimExecutor::look(bool fresh){ const WallClock start = WallClock() + std::chrono::hours(1); Pokemon::ScreenLook out; - out.log.record({Pokemon::ScreenEvent::PLAYER_CAN_MOVE}, start); + out.log.record({ScreenEvent::PLAYER_CAN_MOVE}, start); if (std::exchange(m_dialog_rose, false)){ - out.log.record({Pokemon::ScreenEvent::DIALOG_BOX, Pokemon::ScreenEvent::PLAYER_CAN_MOVE}, start + 33ms); + out.log.record({ScreenEvent::DIALOG_BOX, ScreenEvent::PLAYER_CAN_MOVE}, start + 33ms); } if (m_battle_opening){ - out.log.record({Pokemon::ScreenEvent::FADE_WHITE}, start + 33ms); + out.log.record({ScreenEvent::FADE_WHITE}, start + 33ms); m_opening_looks = fresh ? 1 : m_opening_looks + 1; if (m_opening_looks >= OPENING_LOOKS){ - out.log.record({Pokemon::ScreenEvent::BATTLE_STARTED}, start + BETWEEN_STEPS_LOOK - 33ms); + out.log.record({ScreenEvent::BATTLE_STARTED}, start + BETWEEN_STEPS_LOOK - 33ms); } } out.window = {.start = start, .end = start + BETWEEN_STEPS_LOOK}; @@ -737,7 +738,7 @@ Outcome SimExecutor::run_action(const StoryStep& step, RunState& state){ } m_dialog_up = false; } - return Pokemon::TableExecutor::run_action(step, state); + return m_steps.act(step, state); } Outcome SimExecutor::run_step(const StoryStep& step, RunState& state){ @@ -748,10 +749,9 @@ Outcome SimExecutor::run_step(const StoryStep& step, RunState& state){ // A scene goes on from where the step before left us. A route walks // on the world it plans on (Jubilife alone has no Route 204 north of it). // An event of a walking kind (talk_to_wanderer) is the console's. - const Pokemon::StoryAction* action = m_table.find(step); const bool walks = (step.kind == StoryStepKind::ROUTE || step.kind == StoryStepKind::LEGS || step.kind == StoryStepKind::STEP || step.kind == StoryStepKind::TALK) && - action != nullptr && !action->needs_console; + !story_events().contains(step.action); const GridPose* target_ptr = step.args.contains("target") ? &story_arg(step.args, "target") : nullptr; const StoryWorld* on = step.kind == StoryStepKind::ROUTE && target_ptr && !state.pose.map.empty() ? story_plan_world(state.pose.map, target_ptr->map) : nullptr; diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.h index d467e53da6..7ca0689533 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.h @@ -21,8 +21,10 @@ #include #include #include +#include #include #include +#include "Common/Cpp/CancellableScope.h" #include "Common/Cpp/Logging/AbstractLogger.h" #include "Common/Cpp/TestRunners/UnitTest.h" #include "CommonFramework/ImageTools/ImageStats.h" @@ -31,7 +33,7 @@ #include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" #include "PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.h" #include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.h" -#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryCheckpoint.h" #include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryInterrupts.h" #include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.h" #include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h" @@ -418,21 +420,22 @@ std::vector story_triggers(); } // namespace detail // The program's walking steps on the simulation, the rest stubbed: the -// story's action table (story_action_table()), its rows that need the +// story's actions on the engine (StoryEngineSteps), those that need the // console bound to the stand-ins below. class SimExecutor : public Pokemon::TableExecutor{ public: // The table and the guard are members, built after the base: it only // keeps references to them until a step runs. explicit SimExecutor(Logger& logger, SimMisses misses = {}, SimWorldKnobs knobs = {}) - : Pokemon::TableExecutor(m_table, m_guard) + : Pokemon::TableExecutor(m_no_table, m_guard) , m_logger(logger) , m_misses(misses) , m_knobs(std::move(knobs)) , m_sim({}, {0, 0}, Pokemon::Direction::NORTH, false) , m_io(m_sim, m_notes, m_misses, m_knobs, m_step, [this]{ return cross(); }, [this](const Pokemon::Pose& far){ cross_to(far); }) , m_walks(m_io) - , m_table(story_action_table(m_walks, sim_actions())) + , m_backend(Pokemon::StoryBackendKind::SIMULATION, [&logger](std::string_view line){ logger.log(std::string(line)); }, m_scope) + , m_steps(m_walks, sim_actions(), m_backend) , m_interrupts(story_interrupts([this](bool fresh){ return look(fresh); }, sim_interrupt_actions())) { if (m_knobs.clear_interrupts){ @@ -452,8 +455,8 @@ class SimExecutor : public Pokemon::TableExecutor{ return m_notes; } // The story's actions as the simulation binds them. - const Pokemon::StoryActionTable& actions() const{ - return m_table; + const StoryEngineSteps& steps() const{ + return m_steps; } virtual void begin_attempt(const Pokemon::StoryCheckpoint& data) override{ m_row = data.id; @@ -526,7 +529,11 @@ class SimExecutor : public Pokemon::TableExecutor{ SimStoryStepIO m_io; StoryWalkSteps m_walks; SimStepGuard m_guard; - Pokemon::StoryActionTable m_table; + // TableExecutor's table: empty, as run_action() runs the engine's actions. + Pokemon::StoryActionTable m_no_table; + CancellableHolder m_scope; + StoryLogBackend m_backend; + StoryEngineSteps m_steps; Pokemon::StoryInterrupts m_interrupts; // The scenes fought so far that SimRun::scenes_fought has. size_t m_scenes_fought = 0; diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim_Tests.cpp index 56af9971a4..140b80d5c3 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim_Tests.cpp @@ -46,7 +46,7 @@ #include "PokemonBDSP_AutoStory_OldStorySteps.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" #include "PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Events.h" -#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryCheckpoint.h" #include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.h" #include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h" #include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.h" diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySimulation_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySimulation_Tests.cpp index e2d0bdc8e9..dc3b944230 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySimulation_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySimulation_Tests.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/TestRunners/UnitTest.h" @@ -129,7 +130,7 @@ void add_golden_lines( } } -// A ROUTE step from `from`, planned as StoryExecutor::walk_to() walks it. +// A ROUTE step from `from`, planned as the ROUTE action walks it. Walked plan_route(const Pose& from, const StoryStep& step){ const GridPose& target = story_arg(step.args, "target"); const Pose goal = Pokemon::pose_of(target); @@ -343,8 +344,9 @@ class FakeGame : public Pokemon::StoryStepExecutor{ } Outcome save(RunState&, const StoryStep& step){ trace.push_back("save in " + m_row); - if (!step.action.empty()){ - trace.push_back("card for " + m_row + ": " + step.action); + const auto saves_in = step.args.find(SAVES_IN_ARG); + if (saves_in != step.args.end()){ + trace.push_back("card for " + m_row + ": " + std::get(saves_in->second)); } return Pokemon::outcome_ok(); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WallBattleSim_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WallBattleSim_Tests.cpp index dc74a850ec..a5ae179f82 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WallBattleSim_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WallBattleSim_Tests.cpp @@ -25,7 +25,7 @@ #include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_PlanCosts.h" #include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_RoutePlanner.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.h" -#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryCheckpoint.h" #include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.h" #include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h" #include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.h" diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_LegBattle.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_LegBattle.cpp index 45a1a5fc66..4ce6ed7b70 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_LegBattle.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_LegBattle.cpp @@ -17,7 +17,7 @@ namespace PokemonBDSP{ using Pokemon::Direction; using Pokemon::InterruptKind; -using Pokemon::ScreenEvent; +using PokemonAutomation::ScreenEvent; using Pokemon::TilePos; @@ -80,7 +80,7 @@ LegBattlePlace place_leg_battle(const WalkResult& walk, size_t tiles, const LegB } std::string leg_battle_line(const std::string& label, const WalkResult& walk, const LegBattlePlace& place){ - std::string out = std::format("{}: battle on the leg: {}", label, Pokemon::describe(place.verdict, walk.started)); + std::string out = std::format("{}: battle on the leg: {}", label, describe(place.verdict, walk.started)); if (place.moved){ return out + std::format("; {} to {} tiles in.", place.moved->min, place.moved->max); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.cpp index 3d5d1b0744..89645c5bbb 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.cpp @@ -43,7 +43,7 @@ std::vector route_tiles(const StoryWorld& maps, const std::vector& tiles){ - return Pokemon::describe(route_tiles(maps, tiles)); + return describe(route_tiles(maps, tiles)); } RouteLogLeg route_log_leg(const TileLeg& leg){ diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.cpp index e69c7ec902..ea3d3af19a 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.cpp @@ -108,7 +108,7 @@ WarpFade WarpFadeWatch::push(const Pokemon::TransitionSample& sample){ if (!done || done->change != Pokemon::SceneChange::DIFFERENT || done->kind == Pokemon::TransitionKind::FLASH){ return WarpFade::NONE; } - m_how = Pokemon::describe(*done, done->start); + m_how = describe(*done, done->start); return done->kind == Pokemon::TransitionKind::FADE_BLACK ? WarpFade::BLACK : WarpFade::WHITE; } @@ -173,7 +173,7 @@ bool battle_follows(VideoStream& stream, ProControllerContext& context, Millisec constexpr Milliseconds WHITE_FLASH_BATTLE_WAIT = 8000ms; // Records that the screen shows `events` now, in `log` if there is one. -void record_now(Pokemon::ScreenEventLog* log, const Pokemon::ScreenEvents& events){ +void record_now(ScreenEventLog* log, const ScreenEvents& events){ if (log != nullptr){ log->record(events, current_time()); } @@ -181,15 +181,15 @@ void record_now(Pokemon::ScreenEventLog* log, const Pokemon::ScreenEvents& event // What the late watch's watcher `ret` saw: 0 the encounter flash, 1 black, // 2 the battle menu, 3 the battle's text box. -Pokemon::ScreenEvents late_watch_events(int ret){ +ScreenEvents late_watch_events(int ret){ switch (ret){ case 0: - return {Pokemon::ScreenEvent::FADE_WHITE}; + return {ScreenEvent::FADE_WHITE}; case 1: - return {Pokemon::ScreenEvent::FADE_BLACK}; + return {ScreenEvent::FADE_BLACK}; case 2: case 3: - return {Pokemon::ScreenEvent::BATTLE_STARTED}; + return {ScreenEvent::BATTLE_STARTED}; default: return {}; } @@ -202,7 +202,7 @@ Pokemon::ScreenEvents late_watch_events(int ret){ // longer, and run from it. Returns whether there was one. bool run_from_late_encounter( VideoStream& stream, ProControllerContext& context, const WildBattleEscape& escape, const std::string& label, - Pokemon::ScreenEventLog* events + ScreenEventLog* events ){ VideoSnapshot before = stream.video().snapshot(); WhiteScreenOverWatcher encounter_flash(COLOR_YELLOW, {0.1, 0.1, 0.8, 0.8}); @@ -220,7 +220,7 @@ bool run_from_late_encounter( if (!battle){ return false; } - record_now(events, {Pokemon::ScreenEvent::BATTLE_STARTED}); + record_now(events, {ScreenEvent::BATTLE_STARTED}); stream.log("[Walk] " + label + ": wild battle after the walk; running."); escape(stream, context); context.wait_for_all_requests(); @@ -473,17 +473,17 @@ class WalkUntil{ } // What hold()'s watcher `ret` saw (see after_watchers()). - static Pokemon::ScreenEvents hold_events(int ret){ + static ScreenEvents hold_events(int ret){ switch (ret){ case 0: - return {Pokemon::ScreenEvent::FADE_WHITE}; + return {ScreenEvent::FADE_WHITE}; case 1: - return {Pokemon::ScreenEvent::FADE_BLACK}; + return {ScreenEvent::FADE_BLACK}; case 2: case 4: - return {Pokemon::ScreenEvent::BATTLE_STARTED}; + return {ScreenEvent::BATTLE_STARTED}; case 3: - return {Pokemon::ScreenEvent::DIALOG_BOX}; + return {ScreenEvent::DIALOG_BOX}; default: return {}; } @@ -491,7 +491,7 @@ class WalkUntil{ enum class BattleMovement{ NONE, STEP_MOVED }; void escape_battle(const std::string& what, BattleMovement movement){ - record_now(&m_result.events, {Pokemon::ScreenEvent::BATTLE_STARTED}); + record_now(&m_result.events, {ScreenEvent::BATTLE_STARTED}); m_result.wild_battles++; if (m_chunks <= 1 && movement == BattleMovement::NONE){ m_result.origin = WalkOrigin::MAY_PRECEDE_LEG; diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h index 6c71372c92..2279edb628 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h @@ -19,7 +19,7 @@ #include "CommonFramework/Tools/VideoStream.h" #include "Controllers/Joystick.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" -#include "Pokemon/AutoStory/Pokemon_ScreenEvents.h" +#include "CommonTools/ScreenEvents/ScreenEvents.h" #include "PokemonBDSP_AutoStory_WalkResult.h" namespace PokemonAutomation{ @@ -73,7 +73,7 @@ WalkResult walk_expecting( // `events`, if given, records what was seen, timed. bool run_from_late_encounter( VideoStream& stream, ProControllerContext& context, const WildBattleEscape& escape, const std::string& label, - Pokemon::ScreenEventLog* events = nullptr + ScreenEventLog* events = nullptr ); // walk_until() toward a wall the route was measured to end at. Seeing the diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h index f6503d71e6..ee4c4b0564 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkResult.h @@ -16,7 +16,8 @@ #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "CommonFramework/ImageTypes/ImageViewRGB32.h" #include "Pokemon/AutoStory/Pokemon_SceneJudges.h" -#include "Pokemon/AutoStory/Pokemon_ScreenEvents.h" +#include "CommonTools/ScreenEvents/ScreenEvents.h" +#include "CommonTools/ScreenEvents/ScreenFades.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -48,20 +49,20 @@ using WalkArrived = std::function; // hold where we are. enum class WalkOrigin{ CURRENT_LEG, MAY_PRECEDE_LEG }; -// The fade and warp judgements are shared (Pokemon_ScreenEvents.h, +// The fade and warp judgements are shared (CommonTools/ScreenEvents/ScreenFades.h, // Pokemon_SceneJudges.h); BDSP's walks and steps fill them. // How a screen's fade looks. -using Pokemon::WarpFade; +using PokemonAutomation::WarpFade; // "black fade", for the log. -using Pokemon::warp_fade_name; +using PokemonAutomation::warp_fade_name; // A fade's most texture (the walk's black-screen watcher uses it too). -using Pokemon::FADE_MAX_STDDEV_SUM; +using PokemonAutomation::FADE_MAX_STDDEV_SUM; // A dark fade's brightest rgb sum (likewise). -using Pokemon::FADE_MAX_DARK_SUM; +using PokemonAutomation::FADE_MAX_DARK_SUM; // The fade a screen's middle shows, from its stats. -using Pokemon::fade_of; +using PokemonAutomation::fade_of; // The fade a screen shows. -using Pokemon::fade_of_screen; +using PokemonAutomation::fade_of_screen; // What a walk or a step saw of a warp: a fade or a banner. using Pokemon::WarpSeen; @@ -83,7 +84,7 @@ struct WalkResult{ // The shared judges read it (Pokemon::classify_interrupt()) to tell // what stopped the walk and when its step was taken. Empty where // nothing recorded it. - Pokemon::ScreenEventLog events{}; + ScreenEventLog events{}; // When the walk's first input went in; `events` are timed against it. WallClock started = {}; // Chunks that moved after the last wild battle was run from: the taps diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.cpp new file mode 100644 index 0000000000..c06199f23f --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.cpp @@ -0,0 +1,145 @@ +/* Ball Reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonFramework/Notifications/ProgramInfo.h" +#include "CommonFramework/Recording/StreamHistorySession.h" +#include "CommonFramework/Tools/ErrorDumper.h" +#include "CommonFramework/Tools/VideoStream.h" +#include "CommonTools/ImageMatch/CroppedImageDictionaryMatcher.h" +#include "CommonTools/ImageMatch/ImageCropper.h" +#include "CommonTools/Resources/SpriteDatabase.h" +#include "Tests/ImageUnitTest.h" +#include "Tests/TestUtils.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" +#include "PokemonFRLG_BallReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +namespace{ + +constexpr double MAX_ALPHA = 0.40; +constexpr double ALPHA_SPREAD = 0.02; +constexpr double MIN_BACKGROUND_DISTANCE = 100; +const ImageFloatBox BALL_BOX(0.410, 0.507, 0.063, 0.084); + +class PokeballSpriteMatcher : public ImageMatch::CroppedImageDictionaryMatcher{ +public: + PokeballSpriteMatcher() + : CroppedImageDictionaryMatcher({1, 128}) + { + const SpriteDatabase sprites("PokemonFRLG/PokeballSprites.png", "PokemonFRLG/PokeballSprites.json"); + for (const auto& item : sprites){ + add(item.first, item.second.sprite); + } + } + +private: + std::vector get_crop_candidates(const ImageViewRGB32& image) const override{ + const FloatPixel background = image_border_stats(image).average; + const ImagePixelBox box = ImageMatch::enclosing_rectangle_with_pixel_filter( + image, + [&](Color pixel){ + double r = (double)pixel.red() - background.r; + double g = (double)pixel.green() - background.g; + double b = (double)pixel.blue() - background.b; + return r*r + g*g + b*b >= MIN_BACKGROUND_DISTANCE * MIN_BACKGROUND_DISTANCE; + } + ); + return {extract_box_reference(image, box)}; + } +}; + +} + + +BallReader::BallReader(VideoStream& stream) + : m_stream(stream) + , m_box_sprite(stream.overlay(), GameSettings::instance().GAME_BOX.inner_to_outer(BALL_BOX)) +{} + +std::string BallReader::read_ball(const ImageViewRGB32& screen) const{ + if (!screen){ + return ""; + } + + static const PokeballSpriteMatcher matcher; + const ImageViewRGB32 image = extract_box_reference(screen, m_box_sprite); + ImageMatch::ImageMatchResult result = matcher.match(image, ALPHA_SPREAD); + result.log(m_stream.logger(), MAX_ALPHA); + result.clear_beyond_alpha(MAX_ALPHA); + + if (result.results.size() != 1){ + dump_image(m_stream.logger(), ProgramInfo(), "FRLG-BallReader", screen); + } + if (result.results.empty()){ + return ""; + } + return result.results.begin()->second; +} + + +class Test_BallReader : public ImageUnitTest{ +public: + Test_BallReader(const std::string& image, std::string expected) + : ImageUnitTest("PokemonFRLG::BallReader - " + image, {"PokemonFRLG/StatsReader/Page1/" + image}) + , m_expected(std::move(expected)) + {} + +private: + UnitTestResult run_on_images( + Logger& logger, CancellableScope&, + const std::vector& images + ) const override{ + DummyAudioFeed audio_feed; + DummyVideoFeed video_feed; + StreamHistorySession history(logger); + DummyVideoOverlay overlay; + VideoStream stream(logger, audio_feed, video_feed, history, overlay); + BallReader reader(stream); + std::string result = reader.read_ball(images[0]); + if (result == m_expected){ + return true; + } + return "Expected: " + m_expected + ", received: " + result; + } + + std::string m_expected; +}; + +void add_tests_BallReader(UnitTestDatabase& database){ + database.add("abra_1_deu.png", "master-ball"); + database.add("bulbasaur_1_eng.png", "poke-ball"); + database.add("bulbasaur_1_fra.png", "poke-ball"); + database.add("bulbasaur_1_jpn.png", "poke-ball"); + database.add("bulbasaur_2_eng.png", "poke-ball"); + database.add("bulbasaur_3_eng.png", "poke-ball"); + database.add("bulbasaur_4_eng.png", "poke-ball"); + database.add("deoxys_1_jpn.png", "master-ball"); + database.add("horsea_1_deu.png", "master-ball"); + database.add("horsea_2_deu.png", "master-ball"); + database.add("horsea_3_deu.png", "master-ball"); + database.add("magikarp_1_spa.png", "poke-ball"); + database.add("moltres_1_eng.png", "master-ball"); + database.add("nidoran_m_eng.png", "ultra-ball"); + database.add("pidgey_1_ita.png", "poke-ball"); + database.add("unown_c_eng.png", "timer-ball"); + database.add("unown_ex_eng.png", "poke-ball"); + database.add("unown_h_eng.png", "net-ball"); + database.add("unown_j_eng.png", "luxury-ball"); + database.add("unown_n_eng.png", "repeat-ball"); + database.add("unown_p_eng.png", "nest-ball"); + database.add("unown_qu_eng.png", "poke-ball"); + database.add("venonat_4k_eng.png", "master-ball"); +} + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.h new file mode 100644 index 0000000000..f7e38d75c4 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.h @@ -0,0 +1,40 @@ +/* Ball Reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_BallReader_H +#define PokemonAutomation_PokemonFRLG_BallReader_H + +#include +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" + +namespace PokemonAutomation{ + +class UnitTestDatabase; +class VideoStream; + +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +class BallReader{ +public: + BallReader(VideoStream& stream); + + // Read the ball slug from the first summary page. Return an empty string if no match is found. + std::string read_ball(const ImageViewRGB32& screen) const; + +private: + VideoStream& m_stream; + OverlayBoxScope m_box_sprite; +}; + +void add_tests_BallReader(UnitTestDatabase& database); + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_PokemonSpriteReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_PokemonSpriteReader.cpp index 6e2c516038..f4e05933ce 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_PokemonSpriteReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_PokemonSpriteReader.cpp @@ -4,10 +4,14 @@ * */ +#include +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "CommonFramework/ImageTypes/ImageViewRGB32.h" -#include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/GlobalSettingsPanel.h" -#include "CommonTools/ImageMatch/ImageCropper.h" +#include "Tests/ImageUnitTest.h" +#include "PokemonFRLG_ShinySymbolDetector.h" #include "VideoGames/PokemonFRLG/Resources/PokemonFRLG_PokemonSprites.h" #include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" #include "PokemonFRLG_PokemonSpriteReader.h" @@ -17,78 +21,286 @@ namespace NintendoSwitch{ namespace PokemonFRLG{ -PokemonSpriteMatcherExact::PokemonSpriteMatcherExact(const std::set* subset, bool flipped) - : ExactImageDictionaryMatcher({1, 256}) +namespace{ + +// Positions are in GBA pixels. The GBA screen is 240 x 160. +constexpr int GBA_WIDTH = 240; +constexpr int GBA_HEIGHT = 160; +constexpr int SPRITE_SIZE = 64; + +ImageFloatBox sprite_box(int x, int y){ + return ImageFloatBox( + (double)x / GBA_WIDTH, (double)y / GBA_HEIGHT, + (double)SPRITE_SIZE / GBA_WIDTH, (double)SPRITE_SIZE / GBA_HEIGHT + ); +} + +// When the summary is opened, the sprite bounces up by as much as 9 pixels. +// the following allow 1 extra pixel of slack in each direction. +constexpr int SUMMARY_X_MIN = 27; +constexpr int SUMMARY_X_MAX = 31; +constexpr int SUMMARY_Y_MIN = 23; +constexpr int SUMMARY_Y_MAX = 34; + +// In battle, the sprite's top edge depends on the species and whether it floats. +constexpr int BATTLE_X = 144; +constexpr int BATTLE_Y_MIN = 0; +constexpr int BATTLE_Y_MAX = 36; + +} + + + +PositionedSpriteMatcher::PositionedSpriteMatcher( + const std::set* subset, + bool flipped, // most sprites are flipped on the summary screen + const std::vector& shiny_variants, // {false, true} to match against both normal and shiny sprites + int x_min, int x_max, int y_min, int y_max +) + : m_x_min(x_min), m_x_max(x_max) + , m_y_min(y_min), m_y_max(y_max) { - for (const auto& item : (flipped ? FLIPPED_POKEMON_SPRITES() : ALL_POKEMON_SPRITES())){ - if (subset == nullptr || subset->find(item.first) != subset->end()){ - add(item.first, item.second.sprite.copy()); + for (bool shiny : shiny_variants){ + for (const auto& item : POKEMON_SPRITES(shiny, flipped)){ + if (subset != nullptr && !sprite_in_species_subset(item.first, *subset)){ + continue; + } + m_templates.emplace_back(new Template{ + sprite_slug_to_species(item.first), + shiny, + ImageMatch::WeightedExactImageMatcher(item.second.sprite.copy(), {1, 256}) + }); } } } +ImageFloatBox PositionedSpriteMatcher::search_box() const{ + return ImageFloatBox( + (double)m_x_min / GBA_WIDTH, (double)m_y_min / GBA_HEIGHT, + (double)(m_x_max - m_x_min + SPRITE_SIZE) / GBA_WIDTH, + (double)(m_y_max - m_y_min + SPRITE_SIZE) / GBA_HEIGHT + ); +} -PokemonSpriteMatcherCropped::PokemonSpriteMatcherCropped(const std::set* subset, double min_euclidean_distance, bool flipped) - : CroppedImageDictionaryMatcher({1, 256}) - , m_min_euclidean_distance_squared(min_euclidean_distance * min_euclidean_distance) -{ - for (const auto& item : (flipped ? FLIPPED_POKEMON_SPRITES() : ALL_POKEMON_SPRITES())){ - if (subset == nullptr || subset->find(item.first) != subset->end()){ - add(item.first, item.second.sprite); +PositionedSpriteMatcher::Result PositionedSpriteMatcher::match( + const ImageViewRGB32& game_screen, double alpha_spread +) const{ + Result ret; + if (!game_screen || m_templates.empty()){ + return ret; + } + + // Every possible sprite position scaled to the template size + std::vector candidates; + for (int y = m_y_min; y <= m_y_max; y++){ + for (int x = m_x_min; x <= m_x_max; x++){ + candidates.emplace_back( + extract_box_reference(game_screen, sprite_box(x, y)).scale_to(SPRITE_SIZE, SPRITE_SIZE) + ); } } -} -auto PokemonSpriteMatcherCropped::get_crop_candidates(const ImageViewRGB32& image) const -> std::vector{ - ImageStats border = image_border_stats(image); - ImagePixelBox box = ImageMatch::enclosing_rectangle_with_pixel_filter( - image, - [&](Color pixel){ - double r = (double)pixel.red() - border.average.r; - double g = (double)pixel.green() - border.average.g; - double b = (double)pixel.blue() - border.average.b; - bool stop = r*r + g*g + b*b >= m_min_euclidean_distance_squared; - return stop; + std::map species_best; + double best_alpha = 10000; + for (const std::unique_ptr